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
73 if (m_document) {
74 m_document->addResizeObserver(*this);
75 m_document->scheduleRenderingUpdate();
76 }
77}
78
79void ResizeObserver::unobserve(Element& target)
80{
81 if (!removeTarget(target))
82 return;
83
84 removeObservation(target);
85}
86
87void ResizeObserver::disconnect()
88{
89 removeAllTargets();
90}
91
92void ResizeObserver::targetDestroyed(Element& target)
93{
94 removeObservation(target);
95}
96
97size_t ResizeObserver::gatherObservations(size_t deeperThan)
98{
99 m_hasSkippedObservations = false;
100 size_t minObservedDepth = maxElementDepth();
101 for (const auto& observation : m_observations) {
102 LayoutSize currentSize;
103 if (observation->elementSizeChanged(currentSize)) {
104 size_t depth = observation->targetElementDepth();
105 if (depth > deeperThan) {
106 observation->updateObservationSize(currentSize);
107 m_activeObservations.append(observation.get());
108 minObservedDepth = std::min(depth, minObservedDepth);
109 } else
110 m_hasSkippedObservations = true;
111 }
112 }
113 return minObservedDepth;
114}
115
116void ResizeObserver::deliverObservations()
117{
118 Vector<Ref<ResizeObserverEntry>> entries;
119 for (const auto& observation : m_activeObservations) {
120 ASSERT(observation->target());
121 entries.append(ResizeObserverEntry::create(observation->target(), observation->computeContentRect()));
122 }
123 m_activeObservations.clear();
124 m_callback->handleEvent(entries, *this);
125}
126
127bool ResizeObserver::removeTarget(Element& target)
128{
129 auto* observerData = target.resizeObserverData();
130 if (!observerData)
131 return false;
132
133 auto& observers = observerData->observers;
134 return observers.removeFirst(this);
135}
136
137void ResizeObserver::removeAllTargets()
138{
139 for (auto& observation : m_observations) {
140 bool removed = removeTarget(*observation->target());
141 ASSERT_UNUSED(removed, removed);
142 }
143 m_observations.clear();
144}
145
146bool ResizeObserver::removeObservation(const Element& target)
147{
148 m_activeObservations.removeFirstMatching([&target](auto& observation) {
149 return observation->target() == &target;
150 });
151
152 return m_observations.removeFirstMatching([&target](auto& observation) {
153 return observation->target() == &target;
154 });
155}
156
157bool ResizeObserver::hasPendingActivity() const
158{
159 return (hasObservations() && m_document) || !m_activeObservations.isEmpty();
160}
161
162const char* ResizeObserver::activeDOMObjectName() const
163{
164 return "ResizeObserver";
165}
166
167bool ResizeObserver::canSuspendForDocumentSuspension() const
168{
169 return true;
170}
171
172void ResizeObserver::stop()
173{
174 disconnect();
175 m_callback = nullptr;
176 m_observations.clear();
177 m_activeObservations.clear();
178}
179
180} // namespace WebCore
181
182#endif // ENABLE(RESIZE_OBSERVER)
183