tizen beta release
[framework/web/webkit-efl.git] / Source / WebCore / dom / WebKitMutationObserver.cpp
1 /*
2  * Copyright (C) 2011 Google 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 are
6  * met:
7  *
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
13  * distribution.
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.
17  *
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.
29  */
30
31 #include "config.h"
32
33 #if ENABLE(MUTATION_OBSERVERS)
34
35 #include "WebKitMutationObserver.h"
36
37 #include "Document.h"
38 #include "ExceptionCode.h"
39 #include "MutationCallback.h"
40 #include "MutationObserverRegistration.h"
41 #include "MutationRecord.h"
42 #include "Node.h"
43 #include "QualifiedName.h"
44 #include <wtf/ListHashSet.h>
45
46 namespace WebCore {
47
48 PassRefPtr<WebKitMutationObserver> WebKitMutationObserver::create(PassRefPtr<MutationCallback> callback)
49 {
50     return adoptRef(new WebKitMutationObserver(callback));
51 }
52
53 WebKitMutationObserver::WebKitMutationObserver(PassRefPtr<MutationCallback> callback)
54     : m_callback(callback)
55 {
56 }
57
58 WebKitMutationObserver::~WebKitMutationObserver()
59 {
60     ASSERT(m_registrations.isEmpty());
61 }
62
63 bool WebKitMutationObserver::validateOptions(MutationObserverOptions options)
64 {
65     return (options & (Attributes | CharacterData | ChildList))
66         && ((options & Attributes) || !(options & AttributeOldValue))
67         && ((options & Attributes) || !(options & AttributeFilter))
68         && ((options & CharacterData) || !(options & CharacterDataOldValue));
69 }
70
71 void WebKitMutationObserver::observe(Node* node, MutationObserverOptions options, const HashSet<AtomicString>& attributeFilter, ExceptionCode& ec)
72 {
73     if (!node) {
74         ec = NOT_FOUND_ERR;
75         return;
76     }
77
78     if (!validateOptions(options)) {
79         // FIXME: Revisit this once the spec specifies the exception type; SYNTAX_ERR may not be appropriate.
80         ec = SYNTAX_ERR;
81         return;
82     }
83
84     MutationObserverRegistration* registration = node->registerMutationObserver(this);
85     registration->resetObservation(options, attributeFilter);
86
87     if (registration->isSubtree())
88         node->document()->addSubtreeMutationObserverTypes(registration->mutationTypes());
89 }
90
91 void WebKitMutationObserver::disconnect()
92 {
93     HashSet<MutationObserverRegistration*> registrations(m_registrations);
94     for (HashSet<MutationObserverRegistration*>::iterator iter = registrations.begin(); iter != registrations.end(); ++iter)
95         (*iter)->unregister();
96 }
97
98 void WebKitMutationObserver::observationStarted(MutationObserverRegistration* registration)
99 {
100     ASSERT(!m_registrations.contains(registration));
101     m_registrations.add(registration);
102 }
103
104 void WebKitMutationObserver::observationEnded(MutationObserverRegistration* registration)
105 {
106     ASSERT(m_registrations.contains(registration));
107     m_registrations.remove(registration);
108 }
109
110 typedef ListHashSet<RefPtr<WebKitMutationObserver> > MutationObserverSet;
111
112 static MutationObserverSet& activeMutationObservers()
113 {
114     DEFINE_STATIC_LOCAL(MutationObserverSet, activeObservers, ());
115     return activeObservers;
116 }
117
118 void WebKitMutationObserver::enqueueMutationRecord(PassRefPtr<MutationRecord> mutation)
119 {
120     m_records.append(mutation);
121     activeMutationObservers().add(this);
122 }
123
124 void WebKitMutationObserver::deliver()
125 {
126     MutationRecordArray records;
127     records.swap(m_records);
128
129     for (HashSet<MutationObserverRegistration*>::iterator iter = m_registrations.begin(); iter != m_registrations.end(); ++iter)
130         (*iter)->clearTransientRegistrations();
131
132     m_callback->handleEvent(&records, this);
133 }
134
135 void WebKitMutationObserver::deliverAllMutations()
136 {
137     while (!activeMutationObservers().isEmpty()) {
138         MutationObserverSet::iterator iter = activeMutationObservers().begin();
139         RefPtr<WebKitMutationObserver> observer = *iter;
140         activeMutationObservers().remove(iter);
141         observer->deliver();
142     }
143 }
144
145 PassOwnPtr<MutationObserverInterestGroup> MutationObserverInterestGroup::createForChildListMutation(Node* target)
146 {
147     return adoptPtr(new MutationObserverInterestGroup(target, WebKitMutationObserver::ChildList));
148 }
149
150 PassOwnPtr<MutationObserverInterestGroup> MutationObserverInterestGroup::createForCharacterDataMutation(Node* target)
151 {
152     return adoptPtr(new MutationObserverInterestGroup(target, WebKitMutationObserver::CharacterData));
153 }
154
155 PassOwnPtr<MutationObserverInterestGroup> MutationObserverInterestGroup::createForAttributesMutation(Node* target, const QualifiedName& attributeName)
156 {
157     return adoptPtr(new MutationObserverInterestGroup(target, WebKitMutationObserver::Attributes, attributeName.localName()));
158 }
159
160 MutationObserverInterestGroup::MutationObserverInterestGroup(Node* target, WebKitMutationObserver::MutationType type, const AtomicString& attributeName)
161 {
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;
167 }
168
169 bool MutationObserverInterestGroup::isOldValueRequested()
170 {
171     for (HashMap<WebKitMutationObserver*, MutationRecordDeliveryOptions>::iterator iter = m_observers.begin(); iter != m_observers.end(); ++iter) {
172         if (hasOldValue(iter->second))
173             return true;
174     }
175     return false;
176 }
177
178 void MutationObserverInterestGroup::enqueueMutationRecord(PassRefPtr<MutationRecord> prpMutation)
179 {
180     if (m_observers.isEmpty())
181         return;
182
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);
189             continue;
190         }
191         if (!mutationWithNullOldValue) {
192             if (mutation->oldValue().isNull())
193                 mutationWithNullOldValue = mutation;
194             else
195                 mutationWithNullOldValue = MutationRecord::createWithNullOldValue(mutation).get();
196         }
197         observer->enqueueMutationRecord(mutationWithNullOldValue);
198     }
199 }
200
201 } // namespace WebCore
202
203 #endif // ENABLE(MUTATION_OBSERVERS)