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 "core/dom/MutationObserverRegistration.h"
34
35 #include "core/dom/Node.h"
36 #include "core/dom/QualifiedName.h"
37
38 namespace WebCore {
39
create(PassRefPtr<MutationObserver> observer,Node * registrationNode,MutationObserverOptions options,const HashSet<AtomicString> & attributeFilter)40 PassOwnPtr<MutationObserverRegistration> MutationObserverRegistration::create(PassRefPtr<MutationObserver> observer, Node* registrationNode, MutationObserverOptions options, const HashSet<AtomicString>& attributeFilter)
41 {
42 return adoptPtr(new MutationObserverRegistration(observer, registrationNode, options, attributeFilter));
43 }
44
MutationObserverRegistration(PassRefPtr<MutationObserver> observer,Node * registrationNode,MutationObserverOptions options,const HashSet<AtomicString> & attributeFilter)45 MutationObserverRegistration::MutationObserverRegistration(PassRefPtr<MutationObserver> observer, Node* registrationNode, MutationObserverOptions options, const HashSet<AtomicString>& attributeFilter)
46 : m_observer(observer)
47 , m_registrationNode(registrationNode)
48 , m_options(options)
49 , m_attributeFilter(attributeFilter)
50 {
51 m_observer->observationStarted(this);
52 }
53
~MutationObserverRegistration()54 MutationObserverRegistration::~MutationObserverRegistration()
55 {
56 clearTransientRegistrations();
57 m_observer->observationEnded(this);
58 }
59
resetObservation(MutationObserverOptions options,const HashSet<AtomicString> & attributeFilter)60 void MutationObserverRegistration::resetObservation(MutationObserverOptions options, const HashSet<AtomicString>& attributeFilter)
61 {
62 clearTransientRegistrations();
63 m_options = options;
64 m_attributeFilter = attributeFilter;
65 }
66
observedSubtreeNodeWillDetach(Node * node)67 void MutationObserverRegistration::observedSubtreeNodeWillDetach(Node* node)
68 {
69 if (!isSubtree())
70 return;
71
72 node->registerTransientMutationObserver(this);
73 m_observer->setHasTransientRegistration();
74
75 if (!m_transientRegistrationNodes) {
76 m_transientRegistrationNodes = adoptPtr(new NodeHashSet);
77
78 ASSERT(!m_registrationNodeKeepAlive);
79 m_registrationNodeKeepAlive = m_registrationNode; // Balanced in clearTransientRegistrations.
80 }
81 m_transientRegistrationNodes->add(node);
82 }
83
clearTransientRegistrations()84 void MutationObserverRegistration::clearTransientRegistrations()
85 {
86 if (!m_transientRegistrationNodes) {
87 ASSERT(!m_registrationNodeKeepAlive);
88 return;
89 }
90
91 for (NodeHashSet::iterator iter = m_transientRegistrationNodes->begin(); iter != m_transientRegistrationNodes->end(); ++iter)
92 (*iter)->unregisterTransientMutationObserver(this);
93
94 m_transientRegistrationNodes.clear();
95
96 ASSERT(m_registrationNodeKeepAlive);
97 m_registrationNodeKeepAlive = 0; // Balanced in observeSubtreeNodeWillDetach.
98 }
99
unregister()100 void MutationObserverRegistration::unregister()
101 {
102 m_registrationNode->unregisterMutationObserver(this);
103 // The above line will cause this object to be deleted, so don't do any more in this function.
104 }
105
shouldReceiveMutationFrom(Node * node,MutationObserver::MutationType type,const QualifiedName * attributeName) const106 bool MutationObserverRegistration::shouldReceiveMutationFrom(Node* node, MutationObserver::MutationType type, const QualifiedName* attributeName) const
107 {
108 ASSERT((type == MutationObserver::Attributes && attributeName) || !attributeName);
109 if (!(m_options & type))
110 return false;
111
112 if (m_registrationNode != node && !isSubtree())
113 return false;
114
115 if (type != MutationObserver::Attributes || !(m_options & MutationObserver::AttributeFilter))
116 return true;
117
118 if (!attributeName->namespaceURI().isNull())
119 return false;
120
121 return m_attributeFilter.contains(attributeName->localName());
122 }
123
addRegistrationNodesToSet(HashSet<Node * > & nodes) const124 void MutationObserverRegistration::addRegistrationNodesToSet(HashSet<Node*>& nodes) const
125 {
126 nodes.add(m_registrationNode);
127 if (!m_transientRegistrationNodes)
128 return;
129 for (NodeHashSet::const_iterator iter = m_transientRegistrationNodes->begin(); iter != m_transientRegistrationNodes->end(); ++iter)
130 nodes.add(iter->get());
131 }
132
133 } // namespace WebCore
134