1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 #include "mozilla/widget/nsAutoRollup.h"
7 
8 namespace mozilla {
9 namespace widget {
10 
11 /*static*/
12 uint32_t nsAutoRollup::sCount = 0;
13 /*static*/
14 StaticRefPtr<nsIContent> nsAutoRollup::sLastRollup;
15 
nsAutoRollup()16 nsAutoRollup::nsAutoRollup() {
17   // remember if sLastRollup was null, and only clear it upon destruction
18   // if so. This prevents recursive usage of nsAutoRollup from clearing
19   // sLastRollup when it shouldn't.
20   mWasClear = !sLastRollup;
21   sCount++;
22 }
23 
nsAutoRollup(nsIContent * aRollup)24 nsAutoRollup::nsAutoRollup(nsIContent* aRollup) {
25   MOZ_ASSERT(!sLastRollup);
26   mWasClear = true;
27   sCount++;
28   SetLastRollup(aRollup);
29 }
30 
~nsAutoRollup()31 nsAutoRollup::~nsAutoRollup() {
32   if (sLastRollup && mWasClear) {
33     sLastRollup = nullptr;
34   }
35   sCount--;
36 }
37 
38 /*static*/
SetLastRollup(nsIContent * aLastRollup)39 void nsAutoRollup::SetLastRollup(nsIContent* aLastRollup) {
40   // There must be at least one nsAutoRollup on the stack.
41   MOZ_ASSERT(sCount);
42 
43   sLastRollup = aLastRollup;
44 }
45 
46 /*static*/
GetLastRollup()47 nsIContent* nsAutoRollup::GetLastRollup() { return sLastRollup.get(); }
48 
49 }  // namespace widget
50 }  // namespace mozilla
51