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#include "MutationObserver.h"
34
35#include "Dictionary.h"
36#include "Document.h"
37#include "ExceptionCode.h"
38#include "MutationCallback.h"
39#include "MutationObserverRegistration.h"
40#include "MutationRecord.h"
41#include "Node.h"
42#include <algorithm>
43#include <wtf/HashSet.h>
44#include <wtf/MainThread.h>
45#include <wtf/Vector.h>
46
47namespace WebCore {
48
49static unsigned s_observerPriority = 0;
50
51struct MutationObserver::ObserverLessThan {
52    bool operator()(const RefPtr<MutationObserver>& lhs, const RefPtr<MutationObserver>& rhs)
53    {
54        return lhs->m_priority < rhs->m_priority;
55    }
56};
57
58PassRefPtr<MutationObserver> MutationObserver::create(PassRefPtr<MutationCallback> callback)
59{
60    ASSERT(isMainThread());
61    return adoptRef(new MutationObserver(callback));
62}
63
64MutationObserver::MutationObserver(PassRefPtr<MutationCallback> callback)
65    : m_callback(callback)
66    , m_priority(s_observerPriority++)
67{
68}
69
70MutationObserver::~MutationObserver()
71{
72    ASSERT(m_registrations.isEmpty());
73}
74
75bool MutationObserver::validateOptions(MutationObserverOptions options)
76{
77    return (options & (Attributes | CharacterData | ChildList))
78        && ((options & Attributes) || !(options & AttributeOldValue))
79        && ((options & Attributes) || !(options & AttributeFilter))
80        && ((options & CharacterData) || !(options & CharacterDataOldValue));
81}
82
83void MutationObserver::observe(Node* node, const Dictionary& optionsDictionary, ExceptionCode& ec)
84{
85    if (!node) {
86        ec = NOT_FOUND_ERR;
87        return;
88    }
89
90    static const struct {
91        const char* name;
92        MutationObserverOptions value;
93    } booleanOptions[] = {
94        { "childList", ChildList },
95        { "attributes", Attributes },
96        { "characterData", CharacterData },
97        { "subtree", Subtree },
98        { "attributeOldValue", AttributeOldValue },
99        { "characterDataOldValue", CharacterDataOldValue }
100    };
101    MutationObserverOptions options = 0;
102    for (unsigned i = 0; i < sizeof(booleanOptions) / sizeof(booleanOptions[0]); ++i) {
103        bool value = false;
104        if (optionsDictionary.get(booleanOptions[i].name, value) && value)
105            options |= booleanOptions[i].value;
106    }
107
108    HashSet<AtomicString> attributeFilter;
109    if (optionsDictionary.get("attributeFilter", attributeFilter))
110        options |= AttributeFilter;
111
112    if (!validateOptions(options)) {
113        ec = SYNTAX_ERR;
114        return;
115    }
116
117    node->registerMutationObserver(this, options, attributeFilter);
118}
119
120Vector<RefPtr<MutationRecord> > MutationObserver::takeRecords()
121{
122    Vector<RefPtr<MutationRecord> > records;
123    records.swap(m_records);
124    return records;
125}
126
127void MutationObserver::disconnect()
128{
129    m_records.clear();
130    HashSet<MutationObserverRegistration*> registrations(m_registrations);
131    for (HashSet<MutationObserverRegistration*>::iterator iter = registrations.begin(); iter != registrations.end(); ++iter)
132        MutationObserverRegistration::unregisterAndDelete(*iter);
133}
134
135void MutationObserver::observationStarted(MutationObserverRegistration* registration)
136{
137    ASSERT(!m_registrations.contains(registration));
138    m_registrations.add(registration);
139}
140
141void MutationObserver::observationEnded(MutationObserverRegistration* registration)
142{
143    ASSERT(m_registrations.contains(registration));
144    m_registrations.remove(registration);
145}
146
147typedef HashSet<RefPtr<MutationObserver> > MutationObserverSet;
148
149static MutationObserverSet& activeMutationObservers()
150{
151    DEFINE_STATIC_LOCAL(MutationObserverSet, activeObservers, ());
152    return activeObservers;
153}
154
155static MutationObserverSet& suspendedMutationObservers()
156{
157    DEFINE_STATIC_LOCAL(MutationObserverSet, suspendedObservers, ());
158    return suspendedObservers;
159}
160
161void MutationObserver::enqueueMutationRecord(PassRefPtr<MutationRecord> mutation)
162{
163    ASSERT(isMainThread());
164    m_records.append(mutation);
165    activeMutationObservers().add(this);
166}
167
168void MutationObserver::setHasTransientRegistration()
169{
170    ASSERT(isMainThread());
171    activeMutationObservers().add(this);
172}
173
174HashSet<Node*> MutationObserver::getObservedNodes() const
175{
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;
180}
181
182bool MutationObserver::canDeliver()
183{
184    return !m_callback->scriptExecutionContext()->activeDOMObjectsAreSuspended();
185}
186
187void MutationObserver::deliver()
188{
189    ASSERT(canDeliver());
190
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);
197    }
198    for (size_t i = 0; i < transientRegistrations.size(); ++i)
199        transientRegistrations[i]->clearTransientRegistrations();
200
201    if (m_records.isEmpty())
202        return;
203
204    Vector<RefPtr<MutationRecord> > records;
205    records.swap(m_records);
206
207    m_callback->call(records, this);
208}
209
210void MutationObserver::deliverAllMutations()
211{
212    ASSERT(isMainThread());
213    static bool deliveryInProgress = false;
214    if (deliveryInProgress)
215        return;
216    deliveryInProgress = true;
217
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())
223                continue;
224
225            suspendedMutationObservers().remove(suspended[i]);
226            activeMutationObservers().add(suspended[i]);
227        }
228    }
229
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();
238            else
239                suspendedMutationObservers().add(observers[i]);
240        }
241    }
242
243    deliveryInProgress = false;
244}
245
246} // namespace WebCore
247