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 blink { 39 40 PassOwnPtrWillBeRawPtr<MutationObserverRegistration> MutationObserverRegistration::create(MutationObserver& observer, Node* registrationNode, MutationObserverOptions options, const HashSet<AtomicString>& attributeFilter) 41 { 42 return adoptPtrWillBeNoop(new MutationObserverRegistration(observer, registrationNode, options, attributeFilter)); 43 } 44 45 MutationObserverRegistration::MutationObserverRegistration(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 54 MutationObserverRegistration::~MutationObserverRegistration() 55 { 56 #if !ENABLE(OILPAN) 57 dispose(); 58 #endif 59 } 60 61 void MutationObserverRegistration::dispose() 62 { 63 clearTransientRegistrations(); 64 m_observer->observationEnded(this); 65 m_observer.clear(); 66 } 67 68 void MutationObserverRegistration::resetObservation(MutationObserverOptions options, const HashSet<AtomicString>& attributeFilter) 69 { 70 clearTransientRegistrations(); 71 m_options = options; 72 m_attributeFilter = attributeFilter; 73 } 74 75 void MutationObserverRegistration::observedSubtreeNodeWillDetach(Node& node) 76 { 77 if (!isSubtree()) 78 return; 79 80 node.registerTransientMutationObserver(this); 81 m_observer->setHasTransientRegistration(); 82 83 if (!m_transientRegistrationNodes) { 84 m_transientRegistrationNodes = adoptPtrWillBeNoop(new NodeHashSet); 85 86 ASSERT(m_registrationNode); 87 ASSERT(!m_registrationNodeKeepAlive); 88 m_registrationNodeKeepAlive = PassRefPtrWillBeRawPtr<Node>(m_registrationNode.get()); // Balanced in clearTransientRegistrations. 89 } 90 m_transientRegistrationNodes->add(&node); 91 } 92 93 void MutationObserverRegistration::clearTransientRegistrations() 94 { 95 if (!m_transientRegistrationNodes) { 96 ASSERT(!m_registrationNodeKeepAlive); 97 return; 98 } 99 100 for (NodeHashSet::iterator iter = m_transientRegistrationNodes->begin(); iter != m_transientRegistrationNodes->end(); ++iter) 101 (*iter)->unregisterTransientMutationObserver(this); 102 103 m_transientRegistrationNodes.clear(); 104 105 ASSERT(m_registrationNodeKeepAlive); 106 m_registrationNodeKeepAlive = nullptr; // Balanced in observeSubtreeNodeWillDetach. 107 } 108 109 void MutationObserverRegistration::unregister() 110 { 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. 114 } 115 116 bool MutationObserverRegistration::shouldReceiveMutationFrom(Node& node, MutationObserver::MutationType type, const QualifiedName* attributeName) const 117 { 118 ASSERT((type == MutationObserver::Attributes && attributeName) || !attributeName); 119 if (!(m_options & type)) 120 return false; 121 122 if (m_registrationNode != &node && !isSubtree()) 123 return false; 124 125 if (type != MutationObserver::Attributes || !(m_options & MutationObserver::AttributeFilter)) 126 return true; 127 128 if (!attributeName->namespaceURI().isNull()) 129 return false; 130 131 return m_attributeFilter.contains(attributeName->localName()); 132 } 133 134 void MutationObserverRegistration::addRegistrationNodesToSet(WillBeHeapHashSet<RawPtrWillBeMember<Node> >& nodes) const 135 { 136 ASSERT(m_registrationNode); 137 nodes.add(m_registrationNode.get()); 138 if (!m_transientRegistrationNodes) 139 return; 140 for (NodeHashSet::const_iterator iter = m_transientRegistrationNodes->begin(); iter != m_transientRegistrationNodes->end(); ++iter) 141 nodes.add(iter->get()); 142 } 143 144 void MutationObserverRegistration::trace(Visitor* visitor) 145 { 146 visitor->trace(m_observer); 147 visitor->trace(m_registrationNode); 148 visitor->trace(m_registrationNodeKeepAlive); 149 #if ENABLE(OILPAN) 150 visitor->trace(m_transientRegistrationNodes); 151 #endif 152 } 153 154 } // namespace blink 155