1/*
2 * Copyright (C) 2019 Igalia S.L.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27
28#if ENABLE(RESIZE_OBSERVER)
29#include "ResizeObserver.h"
30
31#include "Element.h"
32#include "ResizeObserverEntry.h"
33
34namespace WebCore {
35
36Ref<ResizeObserver> ResizeObserver::create(Document& document, Ref<ResizeObserverCallback>&& callback)
37{
38 return adoptRef(*new ResizeObserver(document, WTFMove(callback)));
39}
40
41ResizeObserver::ResizeObserver(Document& document, Ref<ResizeObserverCallback>&& callback)
42 : ActiveDOMObject(callback->scriptExecutionContext())
43 , m_document(makeWeakPtr(document))
44 , m_callback(WTFMove(callback))
45{
46 suspendIfNeeded();
47}
48
49ResizeObserver::~ResizeObserver()
50{
51 disconnect();
52 if (m_document)
53 m_document->removeResizeObserver(*this);
54}
55
56void ResizeObserver::observe(Element& target)
57{
58 if (!m_callback)
59 return;
60
61 auto position = m_observations.findMatching([&](auto& observation) {
62 return observation->target() == &target;
63 });
64
65 if (position != notFound)
66 return;
67
68 auto& observerData = target.ensureResizeObserverData();
69 observerData.observers.append(makeWeakPtr(this));
70
71 m_observations.append(ResizeObservation::create(&target));
72 m_pendingTargets.append(target);
73
74 if (m_document) {
75 m_document->addResizeObserver(*this);
76 m_document->scheduleTimedRenderingUpdate();
77 }
78}
79
80void ResizeObserver::unobserve(Element& target)
81{
82 if (!removeTarget(target))
83 return;
84
85 removeObservation(target);
86}
87
88void ResizeObserver::disconnect()
89{
90 removeAllTargets();
91}
92
93void ResizeObserver::targetDestroyed(Element& target)
94{
95 removeObservation(target);
96}
97
98size_t ResizeObserver::gatherObservations(size_t deeperThan)
99{
100 m_hasSkippedObservations = false;
101 size_t minObservedDepth = maxElementDepth();
102 for (const auto& observation : m_observations) {
103 LayoutSize currentSize;
104 if (observation->elementSizeChanged(currentSize)) {
105 size_t depth = observation->targetElementDepth();
106 if (depth > deeperThan) {
107 observation->updateObservationSize(currentSize);
108 m_activeObservations.append(observation.get());
109 minObservedDepth = std::min(depth, minObservedDepth);
110 } else
111 m_hasSkippedObservations = true;
112 }
113 }
114 return minObservedDepth;
115}
116
117void ResizeObserver::deliverObservations()
118{
119 Vector<Ref<ResizeObserverEntry>> entries;
120 for (const auto& observation : m_activeObservations) {
121 ASSERT(observation->target());
122 entries.append(ResizeObserverEntry::create(observation->target(), observation->computeContentRect()));
123 }
124 m_activeObservations.clear();
125 m_callback->handleEvent(entries, *this);
126}
127
128bool ResizeObserver::removeTarget(Element& target)
129{
130 auto* observerData = target.resizeObserverData();
131 if (!observerData)
132 return false;
133
134 auto& observers = observerData->observers;
135 return observers.removeFirst(this);
136}
137
138void ResizeObserver::removeAllTargets()
139{
140 for (auto& observation : m_observations) {
141 bool removed = removeTarget(*observation->target());
142 ASSERT_UNUSED(removed, removed);
143 }
144 m_pendingTargets.clear();
145 m_activeObservations.clear();
146 m_observations.clear();
147}
148
149bool ResizeObserver::removeObservation(const Element& target)
150{
151 m_pendingTargets.removeFirstMatching([&target](auto& pendingTarget) {
152 return pendingTarget.ptr() == &target;
153 });
154
155 m_activeObservations.removeFirstMatching([&target](auto& observation) {
156 return observation->target() == &target;
157 });
158
159 return m_observations.removeFirstMatching([&target](auto& observation) {
160 return observation->target() == &target;
161 });
162}
163
164bool ResizeObserver::hasPendingActivity() const
165{
166 return (hasObservations() && m_document) || !m_activeObservations.isEmpty();
167}
168
169const char* ResizeObserver::activeDOMObjectName() const
170{
171 return "ResizeObserver";
172}
173
174bool ResizeObserver::canSuspendForDocumentSuspension() const
175{
176 return true;
177}
178
179void ResizeObserver::stop()
180{
181 disconnect();
182 m_callback = nullptr;
183}
184
185} // namespace WebCore
186
187#endif // ENABLE(RESIZE_OBSERVER)
188