Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / dom / DocumentLifecycle.cpp
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 #include "config.h"
32 #include "core/dom/DocumentLifecycle.h"
33
34 #include "wtf/Assertions.h"
35
36 namespace WebCore {
37
38 static DocumentLifecycle::DeprecatedTransition* s_deprecatedTransitionStack = 0;
39
40 DocumentLifecycle::Scope::Scope(DocumentLifecycle& lifecycle, State finalState)
41     : m_lifecycle(lifecycle)
42     , m_finalState(finalState)
43 {
44 }
45
46 DocumentLifecycle::Scope::~Scope()
47 {
48     m_lifecycle.advanceTo(m_finalState);
49 }
50
51 DocumentLifecycle::DeprecatedTransition::DeprecatedTransition(State from, State to)
52     : m_previous(s_deprecatedTransitionStack)
53     , m_from(from)
54     , m_to(to)
55 {
56     s_deprecatedTransitionStack = this;
57 }
58
59 DocumentLifecycle::DeprecatedTransition::~DeprecatedTransition()
60 {
61     s_deprecatedTransitionStack = m_previous;
62 }
63
64 DocumentLifecycle::DocumentLifecycle()
65     : m_state(Uninitialized)
66 {
67 }
68
69 DocumentLifecycle::~DocumentLifecycle()
70 {
71 }
72
73 #if !ASSERT_DISABLED
74
75 bool DocumentLifecycle::canAdvanceTo(State state) const
76 {
77     // This transition is bogus, but we've whitelisted it anyway.
78     if (s_deprecatedTransitionStack && m_state == s_deprecatedTransitionStack->from() && state == s_deprecatedTransitionStack->to())
79         return true;
80     if (state > m_state)
81         return true;
82     if (m_state == Disposed) {
83         // FIXME: We can dispose a document multiple times. This seems wrong.
84         // See https://code.google.com/p/chromium/issues/detail?id=301668.
85         return state == Disposed;
86     }
87     if (m_state == StyleClean) {
88         if (state == StyleRecalcPending)
89             return true;
90         // We can synchronously recalc style.
91         if (state == InStyleRecalc)
92             return true;
93         // We can synchronously perform layout.
94         if (state == InPreLayout)
95             return true;
96         if (state == InPerformLayout)
97             return true;
98         // We can redundant arrive in the style clean state.
99         if (state == StyleClean)
100             return true;
101         return false;
102     }
103     if (m_state == InPreLayout) {
104         if (state == InStyleRecalc)
105             return true;
106         if (state == StyleClean)
107             return true;
108         if (state == InPreLayout)
109             return true;
110         return false;
111     }
112     if (m_state == AfterPerformLayout) {
113         if (state == InPreLayout)
114             return true;
115         // If we're doing a partial layout, we won't actually end up cleaning
116         // out all the layout dirty bits. Instead, we'll return to StyleClean.
117         if (state == StyleClean)
118             return true;
119         return false;
120     }
121     if (m_state == LayoutClean) {
122         if (state == StyleRecalcPending)
123             return true;
124         // We can synchronously recalc style.
125         if (state == InStyleRecalc)
126             return true;
127         // We can synchronously perform layout.
128         if (state == InPreLayout)
129             return true;
130         if (state == InPerformLayout)
131             return true;
132         // We can redundant arrive in the layout clean state. This situation
133         // can happen when we call layout recursively and we unwind the stack.
134         if (state == LayoutClean)
135             return true;
136         if (state == StyleClean)
137             return true;
138         return false;
139     }
140     if (m_state == CompositingClean) {
141         if (state == StyleRecalcPending)
142             return true;
143         if (state == InStyleRecalc)
144             return true;
145         if (state == InPreLayout)
146             return true;
147         if (state == InCompositingUpdate)
148             return true;
149         return false;
150     }
151     return false;
152 }
153
154 #endif
155
156 void DocumentLifecycle::advanceTo(State state)
157 {
158     ASSERT(canAdvanceTo(state));
159     m_state = state;
160 }
161
162 }