1/*
2 * Copyright (C) 2011 Google Inc. All rights reserved.
3 * Copyright (C) 2018 Apple Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include "config.h"
33
34#include "MutationObserver.h"
35
36#include "Document.h"
37#include "GCReachableRef.h"
38#include "HTMLSlotElement.h"
39#include "InspectorInstrumentation.h"
40#include "Microtasks.h"
41#include "MutationCallback.h"
42#include "MutationObserverRegistration.h"
43#include "MutationRecord.h"
44#include <algorithm>
45#include <wtf/IsoMallocInlines.h>
46#include <wtf/MainThread.h>
47#include <wtf/NeverDestroyed.h>
48
49namespace WebCore {
50
51WTF_MAKE_ISO_ALLOCATED_IMPL(MutationObserver);
52
53static unsigned s_observerPriority = 0;
54
55Ref<MutationObserver> MutationObserver::create(Ref<MutationCallback>&& callback)
56{
57 ASSERT(isMainThread());
58 return adoptRef(*new MutationObserver(WTFMove(callback)));
59}
60
61MutationObserver::MutationObserver(Ref<MutationCallback>&& callback)
62 : m_callback(WTFMove(callback))
63 , m_priority(s_observerPriority++)
64{
65}
66
67MutationObserver::~MutationObserver()
68{
69 ASSERT(m_registrations.isEmpty());
70}
71
72bool MutationObserver::validateOptions(MutationObserverOptions options)
73{
74 return (options & (Attributes | CharacterData | ChildList))
75 && ((options & Attributes) || !(options & AttributeOldValue))
76 && ((options & Attributes) || !(options & AttributeFilter))
77 && ((options & CharacterData) || !(options & CharacterDataOldValue));
78}
79
80ExceptionOr<void> MutationObserver::observe(Node& node, const Init& init)
81{
82 MutationObserverOptions options = 0;
83
84 if (init.childList)
85 options |= ChildList;
86 if (init.subtree)
87 options |= Subtree;
88 if (init.attributeOldValue.valueOr(false))
89 options |= AttributeOldValue;
90 if (init.characterDataOldValue.valueOr(false))
91 options |= CharacterDataOldValue;
92
93 HashSet<AtomString> attributeFilter;
94 if (init.attributeFilter) {
95 for (auto& value : init.attributeFilter.value())
96 attributeFilter.add(value);
97 options |= AttributeFilter;
98 }
99
100 if (init.attributes ? init.attributes.value() : (options & (AttributeFilter | AttributeOldValue)))
101 options |= Attributes;
102
103 if (init.characterData ? init.characterData.value() : (options & CharacterDataOldValue))
104 options |= CharacterData;
105
106 if (!validateOptions(options))
107 return Exception { TypeError };
108
109 node.registerMutationObserver(*this, options, attributeFilter);
110
111 return { };
112}
113
114auto MutationObserver::takeRecords() -> TakenRecords
115{
116 return { WTFMove(m_records), WTFMove(m_pendingTargets) };
117}
118
119void MutationObserver::disconnect()
120{
121 m_pendingTargets.clear();
122 m_records.clear();
123 HashSet<MutationObserverRegistration*> registrations(m_registrations);
124 for (auto* registration : registrations)
125 registration->node().unregisterMutationObserver(*registration);
126}
127
128void MutationObserver::observationStarted(MutationObserverRegistration& registration)
129{
130 ASSERT(!m_registrations.contains(&registration));
131 m_registrations.add(&registration);
132}
133
134void MutationObserver::observationEnded(MutationObserverRegistration& registration)
135{
136 ASSERT(m_registrations.contains(&registration));
137 m_registrations.remove(&registration);
138}
139
140typedef HashSet<RefPtr<MutationObserver>> MutationObserverSet;
141
142static MutationObserverSet& activeMutationObservers()
143{
144 static NeverDestroyed<MutationObserverSet> activeObservers;
145 return activeObservers;
146}
147
148static MutationObserverSet& suspendedMutationObservers()
149{
150 static NeverDestroyed<MutationObserverSet> suspendedObservers;
151 return suspendedObservers;
152}
153
154// https://dom.spec.whatwg.org/#signal-slot-list
155static Vector<GCReachableRef<HTMLSlotElement>>& signalSlotList()
156{
157 static NeverDestroyed<Vector<GCReachableRef<HTMLSlotElement>>> list;
158 return list;
159}
160
161static bool mutationObserverCompoundMicrotaskQueuedFlag;
162
163class MutationObserverMicrotask final : public Microtask {
164 WTF_MAKE_FAST_ALLOCATED;
165private:
166 Result run() final
167 {
168 MutationObserver::notifyMutationObservers();
169 return Result::Done;
170 }
171};
172
173static void queueMutationObserverCompoundMicrotask()
174{
175 if (mutationObserverCompoundMicrotaskQueuedFlag)
176 return;
177 mutationObserverCompoundMicrotaskQueuedFlag = true;
178 MicrotaskQueue::mainThreadQueue().append(std::make_unique<MutationObserverMicrotask>());
179}
180
181void MutationObserver::enqueueMutationRecord(Ref<MutationRecord>&& mutation)
182{
183 ASSERT(isMainThread());
184 ASSERT(mutation->target());
185 m_pendingTargets.add(*mutation->target());
186 m_records.append(WTFMove(mutation));
187 activeMutationObservers().add(this);
188
189 queueMutationObserverCompoundMicrotask();
190}
191
192void MutationObserver::enqueueSlotChangeEvent(HTMLSlotElement& slot)
193{
194 ASSERT(isMainThread());
195 ASSERT(signalSlotList().findMatching([&slot](auto& entry) { return entry.ptr() == &slot; }) == notFound);
196 signalSlotList().append(slot);
197
198 queueMutationObserverCompoundMicrotask();
199}
200
201void MutationObserver::setHasTransientRegistration()
202{
203 ASSERT(isMainThread());
204 activeMutationObservers().add(this);
205
206 queueMutationObserverCompoundMicrotask();
207}
208
209HashSet<Node*> MutationObserver::observedNodes() const
210{
211 HashSet<Node*> observedNodes;
212 for (auto* registration : m_registrations)
213 registration->addRegistrationNodesToSet(observedNodes);
214 return observedNodes;
215}
216
217bool MutationObserver::canDeliver()
218{
219 return m_callback->canInvokeCallback();
220}
221
222void MutationObserver::deliver()
223{
224 ASSERT(canDeliver());
225
226 // Calling takeTransientRegistrations() can modify m_registrations, so it's necessary
227 // to make a copy of the transient registrations before operating on them.
228 Vector<MutationObserverRegistration*, 1> transientRegistrations;
229 Vector<std::unique_ptr<HashSet<GCReachableRef<Node>>>, 1> nodesToKeepAlive;
230 HashSet<GCReachableRef<Node>> pendingTargets;
231 pendingTargets.swap(m_pendingTargets);
232 for (auto* registration : m_registrations) {
233 if (registration->hasTransientRegistrations())
234 transientRegistrations.append(registration);
235 }
236 for (auto& registration : transientRegistrations)
237 nodesToKeepAlive.append(registration->takeTransientRegistrations());
238
239 if (m_records.isEmpty()) {
240 ASSERT(m_pendingTargets.isEmpty());
241 return;
242 }
243
244 Vector<Ref<MutationRecord>> records;
245 records.swap(m_records);
246
247 // FIXME: Keep mutation observer callback as long as its observed nodes are alive. See https://webkit.org/b/179224.
248 if (m_callback->hasCallback()) {
249 auto* context = m_callback->scriptExecutionContext();
250 if (!context)
251 return;
252
253 InspectorInstrumentationCookie cookie = InspectorInstrumentation::willFireObserverCallback(*context, "MutationObserver"_s);
254 m_callback->handleEvent(*this, records, *this);
255 InspectorInstrumentation::didFireObserverCallback(cookie);
256 }
257}
258
259void MutationObserver::notifyMutationObservers()
260{
261 // https://dom.spec.whatwg.org/#notify-mutation-observers
262 // 1. Unset mutation observer compound microtask queued flag.
263 mutationObserverCompoundMicrotaskQueuedFlag = false;
264
265 ASSERT(isMainThread());
266 static bool deliveryInProgress = false;
267 if (deliveryInProgress)
268 return;
269 deliveryInProgress = true;
270
271 if (!suspendedMutationObservers().isEmpty()) {
272 for (auto& observer : copyToVector(suspendedMutationObservers())) {
273 if (!observer->canDeliver())
274 continue;
275
276 suspendedMutationObservers().remove(observer);
277 activeMutationObservers().add(observer);
278 }
279 }
280
281 while (!activeMutationObservers().isEmpty() || !signalSlotList().isEmpty()) {
282 // 2. Let notify list be a copy of unit of related similar-origin browsing contexts' list of MutationObserver objects.
283 auto notifyList = copyToVector(activeMutationObservers());
284 activeMutationObservers().clear();
285 std::sort(notifyList.begin(), notifyList.end(), [](auto& lhs, auto& rhs) {
286 return lhs->m_priority < rhs->m_priority;
287 });
288
289 // 3. Let signalList be a copy of unit of related similar-origin browsing contexts' signal slot list.
290 // 4. Empty unit of related similar-origin browsing contexts' signal slot list.
291 Vector<GCReachableRef<HTMLSlotElement>> slotList;
292 if (!signalSlotList().isEmpty()) {
293 slotList.swap(signalSlotList());
294 for (auto& slot : slotList)
295 slot->didRemoveFromSignalSlotList();
296 }
297
298 // 5. For each MutationObserver object mo in notify list, execute a compound microtask subtask
299 for (auto& observer : notifyList) {
300 if (observer->canDeliver())
301 observer->deliver();
302 else
303 suspendedMutationObservers().add(observer);
304 }
305
306 // 6. For each slot slot in signalList, in order, fire an event named slotchange, with its bubbles attribute set to true, at slot.
307 for (auto& slot : slotList)
308 slot->dispatchSlotChangeEvent();
309 }
310
311 deliveryInProgress = false;
312}
313
314} // namespace WebCore
315