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 "core/dom/MutationObserverRegistration.h"
35 #include "core/dom/Node.h"
36 #include "core/dom/QualifiedName.h"
40 PassOwnPtrWillBeRawPtr<MutationObserverRegistration> MutationObserverRegistration::create(MutationObserver& observer, Node* registrationNode, MutationObserverOptions options, const HashSet<AtomicString>& attributeFilter)
42 return adoptPtrWillBeNoop(new MutationObserverRegistration(observer, registrationNode, options, attributeFilter));
45 MutationObserverRegistration::MutationObserverRegistration(MutationObserver& observer, Node* registrationNode, MutationObserverOptions options, const HashSet<AtomicString>& attributeFilter)
46 : m_observer(observer)
47 , m_registrationNode(registrationNode)
49 , m_attributeFilter(attributeFilter)
51 m_observer->observationStarted(this);
54 MutationObserverRegistration::~MutationObserverRegistration()
61 void MutationObserverRegistration::dispose()
63 clearTransientRegistrations();
64 m_observer->observationEnded(this);
68 void MutationObserverRegistration::resetObservation(MutationObserverOptions options, const HashSet<AtomicString>& attributeFilter)
70 clearTransientRegistrations();
72 m_attributeFilter = attributeFilter;
75 void MutationObserverRegistration::observedSubtreeNodeWillDetach(Node& node)
80 node.registerTransientMutationObserver(this);
81 m_observer->setHasTransientRegistration();
83 if (!m_transientRegistrationNodes) {
84 m_transientRegistrationNodes = adoptPtr(new NodeHashSet);
86 ASSERT(m_registrationNode);
87 ASSERT(!m_registrationNodeKeepAlive);
88 m_registrationNodeKeepAlive = PassRefPtrWillBeRawPtr<Node>(m_registrationNode.get()); // Balanced in clearTransientRegistrations.
90 m_transientRegistrationNodes->add(&node);
93 void MutationObserverRegistration::clearTransientRegistrations()
95 if (!m_transientRegistrationNodes) {
96 ASSERT(!m_registrationNodeKeepAlive);
100 for (NodeHashSet::iterator iter = m_transientRegistrationNodes->begin(); iter != m_transientRegistrationNodes->end(); ++iter)
101 (*iter)->unregisterTransientMutationObserver(this);
103 m_transientRegistrationNodes.clear();
105 ASSERT(m_registrationNodeKeepAlive);
106 m_registrationNodeKeepAlive = nullptr; // Balanced in observeSubtreeNodeWillDetach.
109 void MutationObserverRegistration::unregister()
111 ASSERT(m_registrationNode);
112 m_registrationNode->unregisterMutationObserver(this);
113 // The above line will cause this object to be deleted, so don't do any more in this function.
116 bool MutationObserverRegistration::shouldReceiveMutationFrom(Node& node, MutationObserver::MutationType type, const QualifiedName* attributeName) const
118 ASSERT((type == MutationObserver::Attributes && attributeName) || !attributeName);
119 if (!(m_options & type))
122 if (m_registrationNode != &node && !isSubtree())
125 if (type != MutationObserver::Attributes || !(m_options & MutationObserver::AttributeFilter))
128 if (!attributeName->namespaceURI().isNull())
131 return m_attributeFilter.contains(attributeName->localName());
134 void MutationObserverRegistration::addRegistrationNodesToSet(HashSet<Node*>& nodes) const
136 ASSERT(m_registrationNode);
137 nodes.add(m_registrationNode.get());
138 if (!m_transientRegistrationNodes)
140 for (NodeHashSet::const_iterator iter = m_transientRegistrationNodes->begin(); iter != m_transientRegistrationNodes->end(); ++iter)
141 nodes.add(iter->get());
144 void MutationObserverRegistration::trace(Visitor* visitor)
146 visitor->trace(m_observer);
147 visitor->trace(m_registrationNode);
148 visitor->trace(m_registrationNodeKeepAlive);
151 } // namespace WebCore