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
36namespace blink {
37
38static DocumentLifecycle::DeprecatedTransition* s_deprecatedTransitionStack = 0;
39
40DocumentLifecycle::Scope::Scope(DocumentLifecycle& lifecycle, State finalState)
41    : m_lifecycle(lifecycle)
42    , m_finalState(finalState)
43{
44}
45
46DocumentLifecycle::Scope::~Scope()
47{
48    m_lifecycle.advanceTo(m_finalState);
49}
50
51DocumentLifecycle::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
59DocumentLifecycle::DeprecatedTransition::~DeprecatedTransition()
60{
61    s_deprecatedTransitionStack = m_previous;
62}
63
64DocumentLifecycle::DocumentLifecycle()
65    : m_state(Uninitialized)
66    , m_detachCount(0)
67{
68}
69
70DocumentLifecycle::~DocumentLifecycle()
71{
72}
73
74#if ENABLE(ASSERT)
75
76bool DocumentLifecycle::canAdvanceTo(State state) const
77{
78    if (state > m_state)
79        return true;
80    if (m_state == Disposed) {
81        // FIXME: We can dispose a document multiple times. This seems wrong.
82        // See https://code.google.com/p/chromium/issues/detail?id=301668.
83        return state == Disposed;
84    }
85    if (m_state == StyleClean) {
86        // We can synchronously recalc style.
87        if (state == InStyleRecalc)
88            return true;
89        // We can synchronously perform layout.
90        if (state == InPreLayout)
91            return true;
92        if (state == InPerformLayout)
93            return true;
94        // We can redundant arrive in the style clean state.
95        if (state == StyleClean)
96            return true;
97        return false;
98    }
99    if (m_state == InPreLayout) {
100        if (state == InStyleRecalc)
101            return true;
102        if (state == StyleClean)
103            return true;
104        if (state == InPreLayout)
105            return true;
106        return false;
107    }
108    if (m_state == AfterPerformLayout) {
109        // We can synchronously recompute layout in AfterPerformLayout.
110        // FIXME: Ideally, we would unnest this recursion into a loop.
111        return state == InPreLayout;
112    }
113    if (m_state == LayoutClean) {
114        // We can synchronously recalc style.
115        if (state == InStyleRecalc)
116            return true;
117        // We can synchronously perform layout.
118        if (state == InPreLayout)
119            return true;
120        if (state == InPerformLayout)
121            return true;
122        // We can redundant arrive in the layout clean state. This situation
123        // can happen when we call layout recursively and we unwind the stack.
124        if (state == LayoutClean)
125            return true;
126        if (state == StyleClean)
127            return true;
128        return false;
129    }
130    if (m_state == CompositingClean) {
131        if (state == InStyleRecalc)
132            return true;
133        if (state == InCompositingUpdate)
134            return true;
135        if (state == InPaintInvalidation)
136            return true;
137        return false;
138    }
139    if (m_state == InPaintInvalidation) {
140        if (state == PaintInvalidationClean)
141            return true;
142        return false;
143    }
144    if (m_state == PaintInvalidationClean) {
145        if (state == InStyleRecalc)
146            return true;
147        if (state == InPreLayout)
148            return true;
149        if (state == InCompositingUpdate)
150            return true;
151        return false;
152    }
153    return false;
154}
155
156bool DocumentLifecycle::canRewindTo(State state) const
157{
158    // This transition is bogus, but we've whitelisted it anyway.
159    if (s_deprecatedTransitionStack && m_state == s_deprecatedTransitionStack->from() && state == s_deprecatedTransitionStack->to())
160        return true;
161    return m_state == StyleClean || m_state == AfterPerformLayout || m_state == LayoutClean || m_state == CompositingClean || m_state == PaintInvalidationClean;
162}
163
164#endif
165
166void DocumentLifecycle::advanceTo(State state)
167{
168    ASSERT(canAdvanceTo(state));
169    m_state = state;
170}
171
172void DocumentLifecycle::ensureStateAtMost(State state)
173{
174    ASSERT(state == VisualUpdatePending || state == StyleClean || state == LayoutClean);
175    if (m_state <= state)
176        return;
177    ASSERT(canRewindTo(state));
178    m_state = state;
179}
180
181}
182