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 #if ENABLE(MUTATION_OBSERVERS)
35 #include "WebKitMutationObserver.h"
38 #include "ExceptionCode.h"
39 #include "MutationCallback.h"
40 #include "MutationObserverRegistration.h"
41 #include "MutationRecord.h"
43 #include "QualifiedName.h"
44 #include <wtf/ListHashSet.h>
48 PassRefPtr<WebKitMutationObserver> WebKitMutationObserver::create(PassRefPtr<MutationCallback> callback)
50 return adoptRef(new WebKitMutationObserver(callback));
53 WebKitMutationObserver::WebKitMutationObserver(PassRefPtr<MutationCallback> callback)
54 : m_callback(callback)
58 WebKitMutationObserver::~WebKitMutationObserver()
60 ASSERT(m_registrations.isEmpty());
63 bool WebKitMutationObserver::validateOptions(MutationObserverOptions options)
65 return (options & (Attributes | CharacterData | ChildList))
66 && ((options & Attributes) || !(options & AttributeOldValue))
67 && ((options & Attributes) || !(options & AttributeFilter))
68 && ((options & CharacterData) || !(options & CharacterDataOldValue));
71 void WebKitMutationObserver::observe(Node* node, MutationObserverOptions options, const HashSet<AtomicString>& attributeFilter, ExceptionCode& ec)
78 if (!validateOptions(options)) {
79 // FIXME: Revisit this once the spec specifies the exception type; SYNTAX_ERR may not be appropriate.
84 MutationObserverRegistration* registration = node->registerMutationObserver(this);
85 registration->resetObservation(options, attributeFilter);
87 if (registration->isSubtree())
88 node->document()->addSubtreeMutationObserverTypes(registration->mutationTypes());
91 void WebKitMutationObserver::disconnect()
93 HashSet<MutationObserverRegistration*> registrations(m_registrations);
94 for (HashSet<MutationObserverRegistration*>::iterator iter = registrations.begin(); iter != registrations.end(); ++iter)
95 (*iter)->unregister();
98 void WebKitMutationObserver::observationStarted(MutationObserverRegistration* registration)
100 ASSERT(!m_registrations.contains(registration));
101 m_registrations.add(registration);
104 void WebKitMutationObserver::observationEnded(MutationObserverRegistration* registration)
106 ASSERT(m_registrations.contains(registration));
107 m_registrations.remove(registration);
110 typedef ListHashSet<RefPtr<WebKitMutationObserver> > MutationObserverSet;
112 static MutationObserverSet& activeMutationObservers()
114 DEFINE_STATIC_LOCAL(MutationObserverSet, activeObservers, ());
115 return activeObservers;
118 void WebKitMutationObserver::enqueueMutationRecord(PassRefPtr<MutationRecord> mutation)
120 m_records.append(mutation);
121 activeMutationObservers().add(this);
124 void WebKitMutationObserver::deliver()
126 MutationRecordArray records;
127 records.swap(m_records);
129 for (HashSet<MutationObserverRegistration*>::iterator iter = m_registrations.begin(); iter != m_registrations.end(); ++iter)
130 (*iter)->clearTransientRegistrations();
132 m_callback->handleEvent(&records, this);
135 void WebKitMutationObserver::deliverAllMutations()
137 while (!activeMutationObservers().isEmpty()) {
138 MutationObserverSet::iterator iter = activeMutationObservers().begin();
139 RefPtr<WebKitMutationObserver> observer = *iter;
140 activeMutationObservers().remove(iter);
145 PassOwnPtr<MutationObserverInterestGroup> MutationObserverInterestGroup::createForChildListMutation(Node* target)
147 return adoptPtr(new MutationObserverInterestGroup(target, WebKitMutationObserver::ChildList));
150 PassOwnPtr<MutationObserverInterestGroup> MutationObserverInterestGroup::createForCharacterDataMutation(Node* target)
152 return adoptPtr(new MutationObserverInterestGroup(target, WebKitMutationObserver::CharacterData));
155 PassOwnPtr<MutationObserverInterestGroup> MutationObserverInterestGroup::createForAttributesMutation(Node* target, const QualifiedName& attributeName)
157 return adoptPtr(new MutationObserverInterestGroup(target, WebKitMutationObserver::Attributes, attributeName.localName()));
160 MutationObserverInterestGroup::MutationObserverInterestGroup(Node* target, WebKitMutationObserver::MutationType type, const AtomicString& attributeName)
162 target->getRegisteredMutationObserversOfType(m_observers, type, attributeName);
163 if (type & WebKitMutationObserver::Attributes)
164 m_oldValueFlag = WebKitMutationObserver::AttributeOldValue;
165 else if (type & WebKitMutationObserver::CharacterData)
166 m_oldValueFlag = WebKitMutationObserver::CharacterDataOldValue;
169 bool MutationObserverInterestGroup::isOldValueRequested()
171 for (HashMap<WebKitMutationObserver*, MutationRecordDeliveryOptions>::iterator iter = m_observers.begin(); iter != m_observers.end(); ++iter) {
172 if (hasOldValue(iter->second))
178 void MutationObserverInterestGroup::enqueueMutationRecord(PassRefPtr<MutationRecord> prpMutation)
180 if (m_observers.isEmpty())
183 RefPtr<MutationRecord> mutation = prpMutation;
184 RefPtr<MutationRecord> mutationWithNullOldValue;
185 for (HashMap<WebKitMutationObserver*, MutationRecordDeliveryOptions>::iterator iter = m_observers.begin(); iter != m_observers.end(); ++iter) {
186 WebKitMutationObserver* observer = iter->first;
187 if (hasOldValue(iter->second)) {
188 observer->enqueueMutationRecord(mutation);
191 if (!mutationWithNullOldValue) {
192 if (mutation->oldValue().isNull())
193 mutationWithNullOldValue = mutation;
195 mutationWithNullOldValue = MutationRecord::createWithNullOldValue(mutation).get();
197 observer->enqueueMutationRecord(mutationWithNullOldValue);
201 } // namespace WebCore
203 #endif // ENABLE(MUTATION_OBSERVERS)