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.
33 #include "MutationObserver.h"
35 #include "Dictionary.h"
37 #include "ExceptionCode.h"
38 #include "MutationCallback.h"
39 #include "MutationObserverRegistration.h"
40 #include "MutationRecord.h"
43 #include <wtf/HashSet.h>
44 #include <wtf/MainThread.h>
45 #include <wtf/Vector.h>
49 static unsigned s_observerPriority = 0;
51 struct MutationObserver::ObserverLessThan {
52 bool operator()(const RefPtr<MutationObserver>& lhs, const RefPtr<MutationObserver>& rhs)
54 return lhs->m_priority < rhs->m_priority;
58 PassRefPtr<MutationObserver> MutationObserver::create(PassRefPtr<MutationCallback> callback)
60 ASSERT(isMainThread());
61 return adoptRef(new MutationObserver(callback));
64 MutationObserver::MutationObserver(PassRefPtr<MutationCallback> callback)
65 : m_callback(callback)
66 , m_priority(s_observerPriority++)
70 MutationObserver::~MutationObserver()
72 ASSERT(m_registrations.isEmpty());
75 bool MutationObserver::validateOptions(MutationObserverOptions options)
77 return (options & (Attributes | CharacterData | ChildList))
78 && ((options & Attributes) || !(options & AttributeOldValue))
79 && ((options & Attributes) || !(options & AttributeFilter))
80 && ((options & CharacterData) || !(options & CharacterDataOldValue));
83 void MutationObserver::observe(Node* node, const Dictionary& optionsDictionary, ExceptionCode& ec)
92 MutationObserverOptions value;
93 } booleanOptions[] = {
94 { "childList", ChildList },
95 { "attributes", Attributes },
96 { "characterData", CharacterData },
97 { "subtree", Subtree },
98 { "attributeOldValue", AttributeOldValue },
99 { "characterDataOldValue", CharacterDataOldValue }
101 MutationObserverOptions options = 0;
102 for (unsigned i = 0; i < sizeof(booleanOptions) / sizeof(booleanOptions[0]); ++i) {
104 if (optionsDictionary.get(booleanOptions[i].name, value) && value)
105 options |= booleanOptions[i].value;
108 HashSet<AtomicString> attributeFilter;
109 if (optionsDictionary.get("attributeFilter", attributeFilter))
110 options |= AttributeFilter;
112 if (!validateOptions(options)) {
117 node->registerMutationObserver(this, options, attributeFilter);
120 Vector<RefPtr<MutationRecord> > MutationObserver::takeRecords()
122 Vector<RefPtr<MutationRecord> > records;
123 records.swap(m_records);
127 void MutationObserver::disconnect()
130 HashSet<MutationObserverRegistration*> registrations(m_registrations);
131 for (HashSet<MutationObserverRegistration*>::iterator iter = registrations.begin(); iter != registrations.end(); ++iter)
132 (*iter)->unregister();
135 void MutationObserver::observationStarted(MutationObserverRegistration* registration)
137 ASSERT(!m_registrations.contains(registration));
138 m_registrations.add(registration);
141 void MutationObserver::observationEnded(MutationObserverRegistration* registration)
143 ASSERT(m_registrations.contains(registration));
144 m_registrations.remove(registration);
147 typedef HashSet<RefPtr<MutationObserver> > MutationObserverSet;
149 static MutationObserverSet& activeMutationObservers()
151 DEFINE_STATIC_LOCAL(MutationObserverSet, activeObservers, ());
152 return activeObservers;
155 static MutationObserverSet& suspendedMutationObservers()
157 DEFINE_STATIC_LOCAL(MutationObserverSet, suspendedObservers, ());
158 return suspendedObservers;
161 void MutationObserver::enqueueMutationRecord(PassRefPtr<MutationRecord> mutation)
163 ASSERT(isMainThread());
164 m_records.append(mutation);
165 activeMutationObservers().add(this);
168 void MutationObserver::setHasTransientRegistration()
170 ASSERT(isMainThread());
171 activeMutationObservers().add(this);
174 HashSet<Node*> MutationObserver::getObservedNodes() const
176 HashSet<Node*> observedNodes;
177 for (HashSet<MutationObserverRegistration*>::const_iterator iter = m_registrations.begin(); iter != m_registrations.end(); ++iter)
178 (*iter)->addRegistrationNodesToSet(observedNodes);
179 return observedNodes;
182 bool MutationObserver::canDeliver()
184 return !m_callback->scriptExecutionContext()->activeDOMObjectsAreSuspended();
187 void MutationObserver::deliver()
189 ASSERT(canDeliver());
191 // Calling clearTransientRegistrations() can modify m_registrations, so it's necessary
192 // to make a copy of the transient registrations before operating on them.
193 Vector<MutationObserverRegistration*, 1> transientRegistrations;
194 for (HashSet<MutationObserverRegistration*>::iterator iter = m_registrations.begin(); iter != m_registrations.end(); ++iter) {
195 if ((*iter)->hasTransientRegistrations())
196 transientRegistrations.append(*iter);
198 for (size_t i = 0; i < transientRegistrations.size(); ++i)
199 transientRegistrations[i]->clearTransientRegistrations();
201 if (m_records.isEmpty())
204 Vector<RefPtr<MutationRecord> > records;
205 records.swap(m_records);
207 m_callback->call(records, this);
210 void MutationObserver::deliverAllMutations()
212 ASSERT(isMainThread());
213 static bool deliveryInProgress = false;
214 if (deliveryInProgress)
216 deliveryInProgress = true;
218 if (!suspendedMutationObservers().isEmpty()) {
219 Vector<RefPtr<MutationObserver> > suspended;
220 copyToVector(suspendedMutationObservers(), suspended);
221 for (size_t i = 0; i < suspended.size(); ++i) {
222 if (!suspended[i]->canDeliver())
225 suspendedMutationObservers().remove(suspended[i]);
226 activeMutationObservers().add(suspended[i]);
230 while (!activeMutationObservers().isEmpty()) {
231 Vector<RefPtr<MutationObserver> > observers;
232 copyToVector(activeMutationObservers(), observers);
233 activeMutationObservers().clear();
234 std::sort(observers.begin(), observers.end(), ObserverLessThan());
235 for (size_t i = 0; i < observers.size(); ++i) {
236 if (observers[i]->canDeliver())
237 observers[i]->deliver();
239 suspendedMutationObservers().add(observers[i]);
243 deliveryInProgress = false;
246 } // namespace WebCore