2 * Copyright (C) 2011 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #include "core/dom/MutationObserver.h"
35 #include "bindings/v8/Dictionary.h"
36 #include "bindings/v8/ExceptionState.h"
37 #include "core/dom/Document.h"
38 #include "core/dom/ExceptionCode.h"
39 #include "core/dom/Microtask.h"
40 #include "core/dom/MutationCallback.h"
41 #include "core/dom/MutationObserverRegistration.h"
42 #include "core/dom/MutationRecord.h"
43 #include "core/dom/Node.h"
44 #include "core/inspector/InspectorInstrumentation.h"
45 #include "wtf/MainThread.h"
49 static unsigned s_observerPriority = 0;
51 struct MutationObserver::ObserverLessThan {
52 bool operator()(const RefPtrWillBeMember<MutationObserver>& lhs, const RefPtrWillBeMember<MutationObserver>& rhs)
54 return lhs->m_priority < rhs->m_priority;
58 PassRefPtrWillBeRawPtr<MutationObserver> MutationObserver::create(PassOwnPtr<MutationCallback> callback)
60 ASSERT(isMainThread());
61 return adoptRefWillBeNoop(new MutationObserver(callback));
64 MutationObserver::MutationObserver(PassOwnPtr<MutationCallback> callback)
65 : m_callback(callback)
66 , m_priority(s_observerPriority++)
68 ScriptWrappable::init(this);
71 MutationObserver::~MutationObserver()
74 ASSERT(m_registrations.isEmpty());
76 if (!m_records.isEmpty())
77 InspectorInstrumentation::didClearAllMutationRecords(m_callback->executionContext(), this);
80 void MutationObserver::observe(Node* node, const Dictionary& optionsDictionary, ExceptionState& exceptionState)
83 exceptionState.throwDOMException(NotFoundError, "The provided node was null.");
87 MutationObserverOptions options = 0;
89 bool attributeOldValue = false;
90 bool attributeOldValuePresent = optionsDictionary.get("attributeOldValue", attributeOldValue);
91 if (attributeOldValue)
92 options |= AttributeOldValue;
94 HashSet<AtomicString> attributeFilter;
95 bool attributeFilterPresent = optionsDictionary.get("attributeFilter", attributeFilter);
96 if (attributeFilterPresent)
97 options |= AttributeFilter;
99 bool attributes = false;
100 bool attributesPresent = optionsDictionary.get("attributes", attributes);
101 if (attributes || (!attributesPresent && (attributeOldValuePresent || attributeFilterPresent)))
102 options |= Attributes;
104 bool characterDataOldValue = false;
105 bool characterDataOldValuePresent = optionsDictionary.get("characterDataOldValue", characterDataOldValue);
106 if (characterDataOldValue)
107 options |= CharacterDataOldValue;
109 bool characterData = false;
110 bool characterDataPresent = optionsDictionary.get("characterData", characterData);
111 if (characterData || (!characterDataPresent && characterDataOldValuePresent))
112 options |= CharacterData;
114 bool childListValue = false;
115 if (optionsDictionary.get("childList", childListValue) && childListValue)
116 options |= ChildList;
118 bool subtreeValue = false;
119 if (optionsDictionary.get("subtree", subtreeValue) && subtreeValue)
122 if (!(options & Attributes)) {
123 if (options & AttributeOldValue) {
124 exceptionState.throwTypeError("The options object may only set 'attributeOldValue' to true when 'attributes' is true or not present.");
127 if (options & AttributeFilter) {
128 exceptionState.throwTypeError("The options object may only set 'attributeFilter' when 'attributes' is true or not present.");
132 if (!((options & CharacterData) || !(options & CharacterDataOldValue))) {
133 exceptionState.throwTypeError("The options object may only set 'characterDataOldValue' to true when 'characterData' is true or not present.");
137 if (!(options & (Attributes | CharacterData | ChildList))) {
138 exceptionState.throwTypeError("The options object must set at least one of 'attributes', 'characterData', or 'childList' to true.");
142 node->registerMutationObserver(*this, options, attributeFilter);
145 MutationRecordVector MutationObserver::takeRecords()
147 MutationRecordVector records;
148 records.swap(m_records);
149 InspectorInstrumentation::didClearAllMutationRecords(m_callback->executionContext(), this);
153 void MutationObserver::disconnect()
156 InspectorInstrumentation::didClearAllMutationRecords(m_callback->executionContext(), this);
157 MutationObserverRegistrationSet registrations(m_registrations);
158 for (MutationObserverRegistrationSet::iterator iter = registrations.begin(); iter != registrations.end(); ++iter)
159 (*iter)->unregister();
160 ASSERT(m_registrations.isEmpty());
163 void MutationObserver::observationStarted(MutationObserverRegistration* registration)
165 ASSERT(!m_registrations.contains(registration));
166 m_registrations.add(registration);
169 void MutationObserver::observationEnded(MutationObserverRegistration* registration)
171 ASSERT(m_registrations.contains(registration));
172 m_registrations.remove(registration);
175 static MutationObserverSet& activeMutationObservers()
178 DEFINE_STATIC_LOCAL(Persistent<MutationObserverSet>, activeObservers, (new MutationObserverSet()));
179 return *activeObservers;
181 DEFINE_STATIC_LOCAL(MutationObserverSet, activeObservers, ());
182 return activeObservers;
186 static MutationObserverSet& suspendedMutationObservers()
189 DEFINE_STATIC_LOCAL(Persistent<MutationObserverSet>, suspendedObservers, (new MutationObserverSet()));
190 return *suspendedObservers;
192 DEFINE_STATIC_LOCAL(MutationObserverSet, suspendedObservers, ());
193 return suspendedObservers;
197 static void activateObserver(PassRefPtrWillBeRawPtr<MutationObserver> observer)
199 if (activeMutationObservers().isEmpty())
200 Microtask::enqueueMicrotask(WTF::bind(&MutationObserver::deliverMutations));
202 activeMutationObservers().add(observer);
205 void MutationObserver::enqueueMutationRecord(PassRefPtrWillBeRawPtr<MutationRecord> mutation)
207 ASSERT(isMainThread());
208 m_records.append(mutation);
209 activateObserver(this);
210 InspectorInstrumentation::didEnqueueMutationRecord(m_callback->executionContext(), this);
213 void MutationObserver::setHasTransientRegistration()
215 ASSERT(isMainThread());
216 activateObserver(this);
219 HashSet<Node*> MutationObserver::getObservedNodes() const
221 HashSet<Node*> observedNodes;
222 for (MutationObserverRegistrationSet::const_iterator iter = m_registrations.begin(); iter != m_registrations.end(); ++iter)
223 (*iter)->addRegistrationNodesToSet(observedNodes);
224 return observedNodes;
227 bool MutationObserver::canDeliver()
229 return !m_callback->executionContext()->activeDOMObjectsAreSuspended();
232 void MutationObserver::deliver()
234 ASSERT(canDeliver());
236 // Calling clearTransientRegistrations() can modify m_registrations, so it's necessary
237 // to make a copy of the transient registrations before operating on them.
238 WillBeHeapVector<RawPtrWillBeMember<MutationObserverRegistration>, 1> transientRegistrations;
239 for (MutationObserverRegistrationSet::iterator iter = m_registrations.begin(); iter != m_registrations.end(); ++iter) {
240 if ((*iter)->hasTransientRegistrations())
241 transientRegistrations.append(*iter);
243 for (size_t i = 0; i < transientRegistrations.size(); ++i)
244 transientRegistrations[i]->clearTransientRegistrations();
246 if (m_records.isEmpty())
249 MutationRecordVector records;
250 records.swap(m_records);
252 InspectorInstrumentation::willDeliverMutationRecords(m_callback->executionContext(), this);
253 m_callback->call(records, this);
254 InspectorInstrumentation::didDeliverMutationRecords(m_callback->executionContext());
257 void MutationObserver::resumeSuspendedObservers()
259 ASSERT(isMainThread());
260 if (suspendedMutationObservers().isEmpty())
263 MutationObserverVector suspended;
264 copyToVector(suspendedMutationObservers(), suspended);
265 for (size_t i = 0; i < suspended.size(); ++i) {
266 if (suspended[i]->canDeliver()) {
267 suspendedMutationObservers().remove(suspended[i]);
268 activateObserver(suspended[i]);
273 void MutationObserver::deliverMutations()
275 ASSERT(isMainThread());
276 MutationObserverVector observers;
277 copyToVector(activeMutationObservers(), observers);
278 activeMutationObservers().clear();
279 std::sort(observers.begin(), observers.end(), ObserverLessThan());
280 for (size_t i = 0; i < observers.size(); ++i) {
281 if (observers[i]->canDeliver())
282 observers[i]->deliver();
284 suspendedMutationObservers().add(observers[i]);
288 void MutationObserver::trace(Visitor* visitor)
290 visitor->trace(m_records);
291 visitor->trace(m_registrations);
294 } // namespace WebCore