Use a 1-byte enum class for TextDirection
[WebKit-https.git] / Source / WebCore / dom / CustomElementReactionQueue.h
1 /*
2  * Copyright (C) 2015, 2016 Apple 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
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #pragma once
27
28 #include <wtf/Forward.h>
29 #include <wtf/Noncopyable.h>
30 #include <wtf/Vector.h>
31
32 namespace JSC {
33
34 class ExecState;
35
36 }
37
38 namespace WebCore {
39
40 class CustomElementReactionQueueItem;
41 class Document;
42 class Element;
43 class JSCustomElementInterface;
44 class QualifiedName;
45
46 class CustomElementReactionQueue {
47     WTF_MAKE_NONCOPYABLE(CustomElementReactionQueue);
48 public:
49     CustomElementReactionQueue(JSCustomElementInterface&);
50     ~CustomElementReactionQueue();
51
52     static void enqueueElementUpgrade(Element&, bool alreadyScheduledToUpgrade);
53     static void enqueueElementUpgradeIfDefined(Element&);
54     static void enqueueConnectedCallbackIfNeeded(Element&);
55     static void enqueueDisconnectedCallbackIfNeeded(Element&);
56     static void enqueueAdoptedCallbackIfNeeded(Element&, Document& oldDocument, Document& newDocument);
57     static void enqueueAttributeChangedCallbackIfNeeded(Element&, const QualifiedName&, const AtomicString& oldValue, const AtomicString& newValue);
58     static void enqueuePostUpgradeReactions(Element&);
59
60     bool observesStyleAttribute() const;
61     void invokeAll(Element&);
62     void clear();
63
64     static void processBackupQueue();
65
66     class ElementQueue {
67     public:
68         void add(Element&);
69         void processQueue(JSC::ExecState*);
70
71     private:
72         void invokeAll();
73
74         Vector<Ref<Element>> m_elements;
75         bool m_invoking { false };
76     };
77
78 private:
79     static CustomElementReactionQueue& ensureCurrentQueue(Element&);
80     static ElementQueue& ensureBackupQueue();
81     static ElementQueue& backupElementQueue();
82
83     Ref<JSCustomElementInterface> m_interface;
84     Vector<CustomElementReactionQueueItem> m_items;
85 };
86
87 class CustomElementReactionDisallowedScope {
88 public:
89     CustomElementReactionDisallowedScope()
90     {
91 #if !ASSERT_DISABLED
92         s_customElementReactionDisallowedCount++;
93 #endif
94     }
95
96     ~CustomElementReactionDisallowedScope()
97     {
98 #if !ASSERT_DISABLED
99         ASSERT(s_customElementReactionDisallowedCount);
100         s_customElementReactionDisallowedCount--;
101 #endif
102     }
103
104 #if !ASSERT_DISABLED
105     static bool isReactionAllowed() { return !s_customElementReactionDisallowedCount; }
106 #endif
107
108     class AllowedScope {
109 #if !ASSERT_DISABLED
110     public:
111         AllowedScope()
112             : m_originalCount(s_customElementReactionDisallowedCount)
113         {
114             s_customElementReactionDisallowedCount = 0;
115         }
116
117         ~AllowedScope()
118         {
119             s_customElementReactionDisallowedCount = m_originalCount;
120         }
121
122     private:
123         unsigned m_originalCount;
124 #endif
125     };
126
127 private:
128 #if !ASSERT_DISABLED
129     WEBCORE_EXPORT static unsigned s_customElementReactionDisallowedCount;
130
131     friend class AllowedScope;
132 #endif
133 };
134
135 class CustomElementReactionStack : public CustomElementReactionDisallowedScope::AllowedScope {
136 public:
137     ALWAYS_INLINE CustomElementReactionStack(JSC::ExecState* state)
138         : m_previousProcessingStack(s_currentProcessingStack)
139         , m_state(state)
140     {
141         s_currentProcessingStack = this;
142     }
143
144     ALWAYS_INLINE CustomElementReactionStack(JSC::ExecState& state)
145         : CustomElementReactionStack(&state)
146     { }
147
148     ALWAYS_INLINE ~CustomElementReactionStack()
149     {
150         if (UNLIKELY(m_queue))
151             processQueue(m_state);
152         s_currentProcessingStack = m_previousProcessingStack;
153     }
154
155 private:
156     WEBCORE_EXPORT void processQueue(JSC::ExecState*);
157
158     CustomElementReactionQueue::ElementQueue* m_queue { nullptr }; // Use raw pointer to avoid generating delete in the destructor.
159     CustomElementReactionStack* m_previousProcessingStack;
160     JSC::ExecState* m_state;
161
162     WEBCORE_EXPORT static CustomElementReactionStack* s_currentProcessingStack;
163
164     friend CustomElementReactionQueue;
165 };
166
167 }