• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #ifndef DocumentLifecycle_h
32 #define DocumentLifecycle_h
33 
34 #include "wtf/Assertions.h"
35 #include "wtf/Noncopyable.h"
36 
37 namespace blink {
38 
39 class DocumentLifecycle {
40     WTF_MAKE_NONCOPYABLE(DocumentLifecycle);
41 public:
42     enum State {
43         Uninitialized,
44         Inactive,
45 
46         // When the document is active, it traverses these states.
47 
48         VisualUpdatePending,
49 
50         InStyleRecalc,
51         StyleClean,
52 
53         InPreLayout,
54         InPerformLayout,
55         AfterPerformLayout,
56         LayoutClean,
57 
58         InCompositingUpdate,
59         CompositingClean,
60 
61         InPaintInvalidation,
62         PaintInvalidationClean,
63 
64         // Once the document starts shuting down, we cannot return
65         // to the style/layout/rendering states.
66         Stopping,
67         Stopped,
68         Disposed,
69     };
70 
71     class Scope {
72         WTF_MAKE_NONCOPYABLE(Scope);
73     public:
74         Scope(DocumentLifecycle&, State finalState);
75         ~Scope();
76 
setFinalState(State finalState)77         void setFinalState(State finalState) { m_finalState = finalState; }
78 
79     private:
80         DocumentLifecycle& m_lifecycle;
81         State m_finalState;
82     };
83 
84     class DeprecatedTransition {
85         WTF_MAKE_NONCOPYABLE(DeprecatedTransition);
86     public:
87         DeprecatedTransition(State from, State to);
88         ~DeprecatedTransition();
89 
from()90         State from() const { return m_from; }
to()91         State to() const { return m_to; }
92 
93     private:
94         DeprecatedTransition* m_previous;
95         State m_from;
96         State m_to;
97     };
98 
99     class DetachScope {
100         WTF_MAKE_NONCOPYABLE(DetachScope);
101     public:
DetachScope(DocumentLifecycle & documentLifecycle)102         explicit DetachScope(DocumentLifecycle& documentLifecycle)
103             : m_documentLifecycle(documentLifecycle)
104         {
105             m_documentLifecycle.incrementDetachCount();
106         }
107 
~DetachScope()108         ~DetachScope()
109         {
110             m_documentLifecycle.decrementDetachCount();
111         }
112 
113     private:
114         DocumentLifecycle& m_documentLifecycle;
115     };
116 
117     DocumentLifecycle();
118     ~DocumentLifecycle();
119 
isActive()120     bool isActive() const { return m_state > Inactive && m_state < Stopping; }
state()121     State state() const { return m_state; }
122 
123     bool stateAllowsTreeMutations() const;
124     bool stateAllowsRenderTreeMutations() const;
125     bool stateAllowsDetach() const;
126 
127     void advanceTo(State);
128     void ensureStateAtMost(State);
129 
incrementDetachCount()130     void incrementDetachCount() { m_detachCount++; }
decrementDetachCount()131     void decrementDetachCount()
132     {
133         ASSERT(m_detachCount > 0);
134         m_detachCount--;
135     }
136 
137 private:
138 #if ENABLE(ASSERT)
139     bool canAdvanceTo(State) const;
140     bool canRewindTo(State) const;
141 #endif
142 
143     State m_state;
144     int m_detachCount;
145 };
146 
stateAllowsTreeMutations()147 inline bool DocumentLifecycle::stateAllowsTreeMutations() const
148 {
149     // FIXME: We should not allow mutations in InPreLayout or AfterPerformLayout either,
150     // but we need to fix MediaList listeners and plugins first.
151     return m_state != InStyleRecalc
152         && m_state != InPerformLayout
153         && m_state != InCompositingUpdate;
154 }
155 
stateAllowsRenderTreeMutations()156 inline bool DocumentLifecycle::stateAllowsRenderTreeMutations() const
157 {
158     return m_detachCount || m_state == InStyleRecalc;
159 }
160 
stateAllowsDetach()161 inline bool DocumentLifecycle::stateAllowsDetach() const
162 {
163     return m_state == VisualUpdatePending
164         || m_state == InStyleRecalc
165         || m_state == StyleClean
166         || m_state == InPreLayout
167         || m_state == LayoutClean
168         || m_state == CompositingClean
169         || m_state == PaintInvalidationClean
170         || m_state == Stopping;
171 }
172 
173 }
174 
175 #endif
176