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 #ifndef ChildListMutationScope_h 32 #define ChildListMutationScope_h 33 34 #include "core/dom/Document.h" 35 #include "core/dom/MutationObserver.h" 36 #include "core/dom/Node.h" 37 #include "platform/heap/Handle.h" 38 #include "wtf/Noncopyable.h" 39 #include "wtf/OwnPtr.h" 40 #include "wtf/RefCounted.h" 41 42 namespace WebCore { 43 44 class MutationObserverInterestGroup; 45 46 // ChildListMutationAccumulator is not meant to be used directly; ChildListMutationScope is the public interface. 47 // 48 // One ChildListMutationAccumulator for a given Node is shared between all the 49 // active ChildListMutationScopes for that Node. Once the last ChildListMutationScope 50 // is destructed the accumulator enqueues a mutation record for the recorded 51 // mutations and the accumulator can be garbage collected. 52 class ChildListMutationAccumulator FINAL : public RefCountedWillBeGarbageCollected<ChildListMutationAccumulator> { 53 DECLARE_EMPTY_DESTRUCTOR_WILL_BE_REMOVED(ChildListMutationAccumulator); 54 public: 55 static PassRefPtrWillBeRawPtr<ChildListMutationAccumulator> getOrCreate(Node&); 56 57 void childAdded(PassRefPtrWillBeRawPtr<Node>); 58 void willRemoveChild(PassRefPtrWillBeRawPtr<Node>); 59 60 bool hasObservers() const { return m_observers; } 61 62 // Register and unregister mutation scopes that are using this mutation 63 // accumulator. 64 void enterMutationScope() { m_mutationScopes++; } 65 void leaveMutationScope(); 66 67 void trace(Visitor*); 68 69 private: 70 ChildListMutationAccumulator(PassRefPtrWillBeRawPtr<Node>, PassOwnPtrWillBeRawPtr<MutationObserverInterestGroup>); 71 72 void enqueueMutationRecord(); 73 bool isEmpty(); 74 bool isAddedNodeInOrder(Node*); 75 bool isRemovedNodeInOrder(Node*); 76 77 RefPtrWillBeMember<Node> m_target; 78 79 WillBeHeapVector<RefPtrWillBeMember<Node> > m_removedNodes; 80 WillBeHeapVector<RefPtrWillBeMember<Node> > m_addedNodes; 81 RefPtrWillBeMember<Node> m_previousSibling; 82 RefPtrWillBeMember<Node> m_nextSibling; 83 RawPtrWillBeMember<Node> m_lastAdded; 84 85 OwnPtrWillBeMember<MutationObserverInterestGroup> m_observers; 86 87 unsigned m_mutationScopes; 88 }; 89 90 class ChildListMutationScope FINAL { 91 WTF_MAKE_NONCOPYABLE(ChildListMutationScope); 92 STACK_ALLOCATED(); 93 public: 94 explicit ChildListMutationScope(Node& target) 95 { 96 if (target.document().hasMutationObserversOfType(MutationObserver::ChildList)) { 97 m_accumulator = ChildListMutationAccumulator::getOrCreate(target); 98 // Register another user of the accumulator. 99 m_accumulator->enterMutationScope(); 100 } 101 } 102 103 ~ChildListMutationScope() 104 { 105 if (m_accumulator) { 106 // Unregister a user of the accumulator. If this is the last user 107 // the accumulator will enqueue a mutation record for the mutations. 108 m_accumulator->leaveMutationScope(); 109 } 110 } 111 112 void childAdded(Node& child) 113 { 114 if (m_accumulator && m_accumulator->hasObservers()) 115 m_accumulator->childAdded(&child); 116 } 117 118 void willRemoveChild(Node& child) 119 { 120 if (m_accumulator && m_accumulator->hasObservers()) 121 m_accumulator->willRemoveChild(&child); 122 } 123 124 private: 125 RefPtrWillBeMember<ChildListMutationAccumulator> m_accumulator; 126 }; 127 128 } // namespace WebCore 129 130 #endif // ChildListMutationScope_h 131