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#include "ChildListMutationScope.h"
33
34#include "MutationObserverInterestGroup.h"
35#include "MutationRecord.h"
36#include "StaticNodeList.h"
37#include <wtf/NeverDestroyed.h>
38#include <wtf/StdLibExtras.h>
39
40namespace WebCore {
41
42typedef HashMap<ContainerNode*, ChildListMutationAccumulator*> AccumulatorMap;
43static AccumulatorMap& accumulatorMap()
44{
45 static NeverDestroyed<AccumulatorMap> map;
46 return map;
47}
48
49ChildListMutationAccumulator::ChildListMutationAccumulator(ContainerNode& target, std::unique_ptr<MutationObserverInterestGroup> observers)
50 : m_target(target)
51 , m_lastAdded(nullptr)
52 , m_observers(WTFMove(observers))
53{
54}
55
56ChildListMutationAccumulator::~ChildListMutationAccumulator()
57{
58 if (!isEmpty())
59 enqueueMutationRecord();
60 accumulatorMap().remove(m_target.ptr());
61}
62
63Ref<ChildListMutationAccumulator> ChildListMutationAccumulator::getOrCreate(ContainerNode& target)
64{
65 AccumulatorMap::AddResult result = accumulatorMap().add(&target, nullptr);
66 RefPtr<ChildListMutationAccumulator> accumulator;
67 if (!result.isNewEntry)
68 accumulator = result.iterator->value;
69 else {
70 accumulator = adoptRef(new ChildListMutationAccumulator(target, MutationObserverInterestGroup::createForChildListMutation(target)));
71 result.iterator->value = accumulator.get();
72 }
73 ASSERT(accumulator);
74 return accumulator.releaseNonNull();
75}
76
77inline bool ChildListMutationAccumulator::isAddedNodeInOrder(Node& child)
78{
79 return isEmpty() || (m_lastAdded == child.previousSibling() && m_nextSibling == child.nextSibling());
80}
81
82void ChildListMutationAccumulator::childAdded(Node& childRef)
83{
84 ASSERT(hasObservers());
85
86 Ref<Node> child(childRef);
87
88 if (!isAddedNodeInOrder(child))
89 enqueueMutationRecord();
90
91 if (isEmpty()) {
92 m_previousSibling = child->previousSibling();
93 m_nextSibling = child->nextSibling();
94 }
95
96 m_lastAdded = child.ptr();
97 m_addedNodes.append(child.get());
98}
99
100inline bool ChildListMutationAccumulator::isRemovedNodeInOrder(Node& child)
101{
102 return isEmpty() || m_nextSibling == &child;
103}
104
105void ChildListMutationAccumulator::willRemoveChild(Node& childRef)
106{
107 ASSERT(hasObservers());
108
109 Ref<Node> child(childRef);
110
111 if (!m_addedNodes.isEmpty() || !isRemovedNodeInOrder(child))
112 enqueueMutationRecord();
113
114 if (isEmpty()) {
115 m_previousSibling = child->previousSibling();
116 m_nextSibling = child->nextSibling();
117 m_lastAdded = child->previousSibling();
118 } else
119 m_nextSibling = child->nextSibling();
120
121 m_removedNodes.append(child.get());
122}
123
124void ChildListMutationAccumulator::enqueueMutationRecord()
125{
126 ASSERT(hasObservers());
127 ASSERT(!isEmpty());
128
129 auto record = MutationRecord::createChildList(m_target, StaticNodeList::create(WTFMove(m_addedNodes)), StaticNodeList::create(WTFMove(m_removedNodes)), WTFMove(m_previousSibling), WTFMove(m_nextSibling));
130 m_observers->enqueueMutationRecord(WTFMove(record));
131 m_lastAdded = nullptr;
132 ASSERT(isEmpty());
133}
134
135bool ChildListMutationAccumulator::isEmpty()
136{
137 bool result = m_removedNodes.isEmpty() && m_addedNodes.isEmpty();
138#ifndef NDEBUG
139 if (result) {
140 ASSERT(!m_previousSibling);
141 ASSERT(!m_nextSibling);
142 ASSERT(!m_lastAdded);
143 }
144#endif
145 return result;
146}
147
148} // namespace WebCore
149