Home | History | Annotate | Download | only in dom
      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 WebCore {
     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         // Once the document starts shuting down, we cannot return
     62         // to the style/layout/rendering states.
     63         Stopping,
     64         Stopped,
     65         Disposed,
     66     };
     67 
     68     class Scope {
     69         WTF_MAKE_NONCOPYABLE(Scope);
     70     public:
     71         Scope(DocumentLifecycle&, State finalState);
     72         ~Scope();
     73 
     74         void setFinalState(State finalState) { m_finalState = finalState; }
     75 
     76     private:
     77         DocumentLifecycle& m_lifecycle;
     78         State m_finalState;
     79     };
     80 
     81     class DeprecatedTransition {
     82         WTF_MAKE_NONCOPYABLE(DeprecatedTransition);
     83     public:
     84         DeprecatedTransition(State from, State to);
     85         ~DeprecatedTransition();
     86 
     87         State from() const { return m_from; }
     88         State to() const { return m_to; }
     89 
     90     private:
     91         DeprecatedTransition* m_previous;
     92         State m_from;
     93         State m_to;
     94     };
     95 
     96     class DetachScope {
     97         WTF_MAKE_NONCOPYABLE(DetachScope);
     98     public:
     99         explicit DetachScope(DocumentLifecycle& documentLifecycle)
    100             : m_documentLifecycle(documentLifecycle)
    101         {
    102             m_documentLifecycle.incrementDetachCount();
    103         }
    104 
    105         ~DetachScope()
    106         {
    107             m_documentLifecycle.decrementDetachCount();
    108         }
    109 
    110     private:
    111         DocumentLifecycle& m_documentLifecycle;
    112     };
    113 
    114     DocumentLifecycle();
    115     ~DocumentLifecycle();
    116 
    117     bool isActive() const { return m_state > Inactive && m_state < Stopping; }
    118     State state() const { return m_state; }
    119 
    120     bool stateAllowsTreeMutations() const;
    121     bool stateAllowsRenderTreeMutations() const;
    122     bool stateAllowsDetach() const;
    123 
    124     void advanceTo(State);
    125     void ensureStateAtMost(State);
    126 
    127     void incrementDetachCount() { m_detachCount++; }
    128     void decrementDetachCount()
    129     {
    130         ASSERT(m_detachCount > 0);
    131         m_detachCount--;
    132     }
    133 
    134 private:
    135 #if ASSERT_ENABLED
    136     bool canAdvanceTo(State) const;
    137     bool canRewindTo(State) const;
    138 #endif
    139 
    140     State m_state;
    141     int m_detachCount;
    142 };
    143 
    144 inline bool DocumentLifecycle::stateAllowsTreeMutations() const
    145 {
    146     // FIXME: We should not allow mutations in InPreLayout or AfterPerformLayout either,
    147     // but we need to fix MediaList listeners and plugins first.
    148     return m_state != InStyleRecalc
    149         && m_state != InPerformLayout
    150         && m_state != InCompositingUpdate;
    151 }
    152 
    153 inline bool DocumentLifecycle::stateAllowsRenderTreeMutations() const
    154 {
    155     return m_detachCount || m_state == InStyleRecalc;
    156 }
    157 
    158 inline bool DocumentLifecycle::stateAllowsDetach() const
    159 {
    160     return m_state == VisualUpdatePending
    161         || m_state == InStyleRecalc
    162         || m_state == StyleClean
    163         || m_state == InPreLayout
    164         || m_state == LayoutClean
    165         || m_state == CompositingClean
    166         || m_state == Stopping;
    167 }
    168 
    169 }
    170 
    171 #endif
    172