1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef DEBUG
8 static_assert(false, "This should not be compiled in !DEBUG");
9 #endif  // DEBUG
10 
11 #include "nsAutoLayoutPhase.h"
12 #include "nsPresContext.h"
13 #include "nsContentUtils.h"
14 
nsAutoLayoutPhase(nsPresContext * aPresContext,nsLayoutPhase aPhase)15 nsAutoLayoutPhase::nsAutoLayoutPhase(nsPresContext* aPresContext,
16                                      nsLayoutPhase aPhase)
17     : mPresContext(aPresContext), mPhase(aPhase), mCount(0) {
18   Enter();
19 }
20 
~nsAutoLayoutPhase()21 nsAutoLayoutPhase::~nsAutoLayoutPhase() {
22   Exit();
23   MOZ_ASSERT(mCount == 0, "imbalanced");
24 }
25 
Enter()26 void nsAutoLayoutPhase::Enter() {
27   switch (mPhase) {
28     case eLayoutPhase_Paint:
29       MOZ_ASSERT(mPresContext->mLayoutPhaseCount[eLayoutPhase_Paint] == 0,
30                  "recurring into paint");
31       MOZ_ASSERT(
32           mPresContext->mLayoutPhaseCount[eLayoutPhase_DisplayListBuilding] ==
33               0,
34           "recurring into paint from display list building");
35       MOZ_ASSERT(mPresContext->mLayoutPhaseCount[eLayoutPhase_Reflow] == 0,
36                  "painting in the middle of reflow");
37       MOZ_ASSERT(mPresContext->mLayoutPhaseCount[eLayoutPhase_FrameC] == 0,
38                  "painting in the middle of frame construction");
39       break;
40     case eLayoutPhase_DisplayListBuilding:
41       // It's fine and expected to be in a paint here.
42       MOZ_ASSERT(
43           mPresContext->mLayoutPhaseCount[eLayoutPhase_DisplayListBuilding] ==
44               0,
45           "recurring into display list building");
46       MOZ_ASSERT(mPresContext->mLayoutPhaseCount[eLayoutPhase_Reflow] == 0,
47                  "display list building in the middle of reflow");
48       MOZ_ASSERT(mPresContext->mLayoutPhaseCount[eLayoutPhase_FrameC] == 0,
49                  "display list building in the middle of frame construction");
50       break;
51     case eLayoutPhase_Reflow:
52       MOZ_ASSERT(mPresContext->mLayoutPhaseCount[eLayoutPhase_Paint] == 0,
53                  "reflowing in the middle of a paint");
54       MOZ_ASSERT(
55           mPresContext->mLayoutPhaseCount[eLayoutPhase_DisplayListBuilding] ==
56               0,
57           "reflowing in the middle of a display list building");
58       MOZ_ASSERT(mPresContext->mLayoutPhaseCount[eLayoutPhase_Reflow] == 0,
59                  "recurring into reflow");
60       MOZ_ASSERT(mPresContext->mLayoutPhaseCount[eLayoutPhase_FrameC] == 0,
61                  "reflowing in the middle of frame construction");
62       break;
63     case eLayoutPhase_FrameC:
64       MOZ_ASSERT(mPresContext->mLayoutPhaseCount[eLayoutPhase_Paint] == 0,
65                  "constructing frames in the middle of a paint");
66       MOZ_ASSERT(
67           mPresContext->mLayoutPhaseCount[eLayoutPhase_DisplayListBuilding] ==
68               0,
69           "constructing frames in the middle of a display list building");
70       MOZ_ASSERT(mPresContext->mLayoutPhaseCount[eLayoutPhase_Reflow] == 0,
71                  "constructing frames in the middle of reflow");
72       MOZ_ASSERT(mPresContext->mLayoutPhaseCount[eLayoutPhase_FrameC] == 0,
73                  "recurring into frame construction");
74       MOZ_ASSERT(!nsContentUtils::IsSafeToRunScript(),
75                  "constructing frames and scripts are not blocked");
76       break;
77     case eLayoutPhase_COUNT:
78       break;
79   }
80 
81   ++(mPresContext->mLayoutPhaseCount[mPhase]);
82   ++mCount;
83 }
84 
Exit()85 void nsAutoLayoutPhase::Exit() {
86   MOZ_ASSERT(mCount > 0 && mPresContext->mLayoutPhaseCount[mPhase] > 0,
87              "imbalanced");
88   --(mPresContext->mLayoutPhaseCount[mPhase]);
89   --mCount;
90 }
91