Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 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" |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 37 | #include "wtf/Noncopyable.h" |
| 38 | #include "wtf/OwnPtr.h" |
| 39 | #include "wtf/RefCounted.h" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 40 | |
| 41 | namespace WebCore { |
| 42 | |
| 43 | class MutationObserverInterestGroup; |
| 44 | |
| 45 | // ChildListMutationAccumulator is not meant to be used directly; ChildListMutationScope is the public interface. |
| 46 | class ChildListMutationAccumulator : public RefCounted<ChildListMutationAccumulator> { |
| 47 | public: |
| 48 | static PassRefPtr<ChildListMutationAccumulator> getOrCreate(Node*); |
| 49 | ~ChildListMutationAccumulator(); |
| 50 | |
| 51 | void childAdded(PassRefPtr<Node>); |
| 52 | void willRemoveChild(PassRefPtr<Node>); |
| 53 | |
| 54 | bool hasObservers() const { return m_observers; } |
| 55 | |
| 56 | private: |
| 57 | ChildListMutationAccumulator(PassRefPtr<Node>, PassOwnPtr<MutationObserverInterestGroup>); |
| 58 | |
| 59 | void enqueueMutationRecord(); |
| 60 | bool isEmpty(); |
| 61 | bool isAddedNodeInOrder(Node*); |
| 62 | bool isRemovedNodeInOrder(Node*); |
| 63 | |
| 64 | RefPtr<Node> m_target; |
| 65 | |
| 66 | Vector<RefPtr<Node> > m_removedNodes; |
| 67 | Vector<RefPtr<Node> > m_addedNodes; |
| 68 | RefPtr<Node> m_previousSibling; |
| 69 | RefPtr<Node> m_nextSibling; |
| 70 | Node* m_lastAdded; |
| 71 | |
| 72 | OwnPtr<MutationObserverInterestGroup> m_observers; |
| 73 | }; |
| 74 | |
| 75 | class ChildListMutationScope { |
| 76 | WTF_MAKE_NONCOPYABLE(ChildListMutationScope); |
| 77 | public: |
| 78 | explicit ChildListMutationScope(Node* target) |
| 79 | { |
| 80 | if (target->document()->hasMutationObserversOfType(MutationObserver::ChildList)) |
| 81 | m_accumulator = ChildListMutationAccumulator::getOrCreate(target); |
| 82 | } |
| 83 | |
| 84 | void childAdded(Node* child) |
| 85 | { |
| 86 | if (m_accumulator && m_accumulator->hasObservers()) |
| 87 | m_accumulator->childAdded(child); |
| 88 | } |
| 89 | |
| 90 | void willRemoveChild(Node* child) |
| 91 | { |
| 92 | if (m_accumulator && m_accumulator->hasObservers()) |
| 93 | m_accumulator->willRemoveChild(child); |
| 94 | } |
| 95 | |
| 96 | private: |
| 97 | RefPtr<ChildListMutationAccumulator> m_accumulator; |
| 98 | }; |
| 99 | |
| 100 | } // namespace WebCore |
| 101 | |
| 102 | #endif // ChildListMutationScope_h |