OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "core/dom/IntersectionObserver.h" | 5 #include "core/dom/IntersectionObserver.h" |
6 | 6 |
7 #include "bindings/core/v8/ExceptionState.h" | 7 #include "bindings/core/v8/ExceptionState.h" |
8 #include "core/css/parser/CSSParserTokenRange.h" | 8 #include "core/css/parser/CSSParserTokenRange.h" |
9 #include "core/css/parser/CSSTokenizer.h" | 9 #include "core/css/parser/CSSTokenizer.h" |
10 #include "core/dom/ExceptionCode.h" | 10 #include "core/dom/ExceptionCode.h" |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 m_bottomMargin = rootMargin[2]; | 145 m_bottomMargin = rootMargin[2]; |
146 m_leftMargin = rootMargin[3]; | 146 m_leftMargin = rootMargin[3]; |
147 break; | 147 break; |
148 default: | 148 default: |
149 ASSERT_NOT_REACHED(); | 149 ASSERT_NOT_REACHED(); |
150 break; | 150 break; |
151 } | 151 } |
152 root.document().ensureIntersectionObserverController().addTrackedObserver(*t
his); | 152 root.document().ensureIntersectionObserverController().addTrackedObserver(*t
his); |
153 } | 153 } |
154 | 154 |
| 155 #if ENABLE(OILPAN) |
| 156 void IntersectionObserver::clearWeakMembers(Visitor* visitor) |
| 157 { |
| 158 if (Heap::isHeapObjectAlive(m_root)) |
| 159 return; |
| 160 disconnect(); |
| 161 m_root = nullptr; |
| 162 } |
| 163 #endif |
| 164 |
155 LayoutObject* IntersectionObserver::rootLayoutObject() const | 165 LayoutObject* IntersectionObserver::rootLayoutObject() const |
156 { | 166 { |
157 Node* rootNode = root(); | 167 Node* rootNode = root(); |
158 if (rootNode->isDocumentNode()) | 168 if (rootNode->isDocumentNode()) |
159 return toDocument(rootNode)->layoutView(); | 169 return toDocument(rootNode)->layoutView(); |
160 return toElement(rootNode)->layoutObject(); | 170 return toElement(rootNode)->layoutObject(); |
161 } | 171 } |
162 | 172 |
163 bool IntersectionObserver::isDescendantOfRoot(const Element* target) const | 173 bool IntersectionObserver::isDescendantOfRoot(const Element* target) const |
164 { | 174 { |
(...skipping 14 matching lines...) Expand all Loading... |
179 } | 189 } |
180 if (rootNode->isDocumentNode()) { | 190 if (rootNode->isDocumentNode()) { |
181 ASSERT(targetDocument == rootNode); | 191 ASSERT(targetDocument == rootNode); |
182 return true; | 192 return true; |
183 } | 193 } |
184 return target->isDescendantOf(rootNode); | 194 return target->isDescendantOf(rootNode); |
185 } | 195 } |
186 | 196 |
187 void IntersectionObserver::observe(Element* target, ExceptionState& exceptionSta
te) | 197 void IntersectionObserver::observe(Element* target, ExceptionState& exceptionSta
te) |
188 { | 198 { |
189 checkRootAndDetachIfNeeded(); | |
190 if (!m_root) { | 199 if (!m_root) { |
191 exceptionState.throwDOMException(HierarchyRequestError, "Invalid observe
r: root element or containing document has been deleted."); | 200 exceptionState.throwDOMException(HierarchyRequestError, "Invalid observe
r: root element or containing document has been deleted."); |
192 return; | 201 return; |
193 } | 202 } |
194 if (!target) { | 203 if (!target) { |
195 exceptionState.throwTypeError("Observation target must be an element."); | 204 exceptionState.throwTypeError("Observation target must be an element."); |
196 return; | 205 return; |
197 } | 206 } |
198 if (m_root.get() == target) { | 207 if (m_root.get() == target) { |
199 exceptionState.throwDOMException(HierarchyRequestError, "Cannot use the
same element for root and target."); | 208 exceptionState.throwDOMException(HierarchyRequestError, "Cannot use the
same element for root and target."); |
(...skipping 14 matching lines...) Expand all Loading... |
214 if (target->ensureIntersectionObserverData().getObservationFor(*this)) | 223 if (target->ensureIntersectionObserverData().getObservationFor(*this)) |
215 return; | 224 return; |
216 | 225 |
217 IntersectionObservation* observation = new IntersectionObservation(*this, *t
arget, shouldReportRootBounds); | 226 IntersectionObservation* observation = new IntersectionObservation(*this, *t
arget, shouldReportRootBounds); |
218 target->ensureIntersectionObserverData().addObservation(*observation); | 227 target->ensureIntersectionObserverData().addObservation(*observation); |
219 m_observations.add(observation); | 228 m_observations.add(observation); |
220 } | 229 } |
221 | 230 |
222 void IntersectionObserver::unobserve(Element* target, ExceptionState&) | 231 void IntersectionObserver::unobserve(Element* target, ExceptionState&) |
223 { | 232 { |
224 checkRootAndDetachIfNeeded(); | |
225 if (!target || !target->intersectionObserverData()) | 233 if (!target || !target->intersectionObserverData()) |
226 return; | 234 return; |
227 // TODO(szager): unobserve callback | 235 // TODO(szager): unobserve callback |
228 if (IntersectionObservation* observation = target->intersectionObserverData(
)->getObservationFor(*this)) | 236 if (IntersectionObservation* observation = target->intersectionObserverData(
)->getObservationFor(*this)) |
229 observation->disconnect(); | 237 observation->disconnect(); |
230 } | 238 } |
231 | 239 |
232 void IntersectionObserver::computeIntersectionObservations(double timestamp) | 240 void IntersectionObserver::computeIntersectionObservations(double timestamp) |
233 { | 241 { |
234 checkRootAndDetachIfNeeded(); | |
235 if (!m_root) | 242 if (!m_root) |
236 return; | 243 return; |
237 for (auto& observation : m_observations) | 244 for (auto& observation : m_observations) |
238 observation->computeIntersectionObservations(timestamp); | 245 observation->computeIntersectionObservations(timestamp); |
239 } | 246 } |
240 | 247 |
241 void IntersectionObserver::disconnect() | 248 void IntersectionObserver::disconnect() |
242 { | 249 { |
243 HeapVector<Member<IntersectionObservation>> observationsToDisconnect; | 250 for (auto& observation : m_observations) |
244 copyToVector(m_observations, observationsToDisconnect); | 251 observation->clearRootAndRemoveFromTarget(); |
245 for (auto& observation : observationsToDisconnect) | 252 m_observations.clear(); |
246 observation->disconnect(); | |
247 ASSERT(m_observations.isEmpty()); | |
248 } | 253 } |
249 | 254 |
250 void IntersectionObserver::removeObservation(IntersectionObservation& observatio
n) | 255 void IntersectionObserver::removeObservation(IntersectionObservation& observatio
n) |
251 { | 256 { |
252 m_observations.remove(&observation); | 257 m_observations.remove(&observation); |
253 } | 258 } |
254 | 259 |
255 HeapVector<Member<IntersectionObserverEntry>> IntersectionObserver::takeRecords(
) | 260 HeapVector<Member<IntersectionObserverEntry>> IntersectionObserver::takeRecords(
) |
256 { | 261 { |
257 checkRootAndDetachIfNeeded(); | |
258 HeapVector<Member<IntersectionObserverEntry>> entries; | 262 HeapVector<Member<IntersectionObserverEntry>> entries; |
259 entries.swap(m_entries); | 263 entries.swap(m_entries); |
260 return entries; | 264 return entries; |
261 } | 265 } |
262 | 266 |
263 void IntersectionObserver::enqueueIntersectionObserverEntry(IntersectionObserver
Entry& entry) | 267 void IntersectionObserver::enqueueIntersectionObserverEntry(IntersectionObserver
Entry& entry) |
264 { | 268 { |
265 m_entries.append(&entry); | 269 m_entries.append(&entry); |
266 toDocument(m_callback->executionContext())->ensureIntersectionObserverContro
ller().scheduleIntersectionObserverForDelivery(*this); | 270 toDocument(m_callback->executionContext())->ensureIntersectionObserverContro
ller().scheduleIntersectionObserverForDelivery(*this); |
267 } | 271 } |
(...skipping 24 matching lines...) Expand all Loading... |
292 unsigned IntersectionObserver::firstThresholdGreaterThan(float ratio) const | 296 unsigned IntersectionObserver::firstThresholdGreaterThan(float ratio) const |
293 { | 297 { |
294 unsigned result = 0; | 298 unsigned result = 0; |
295 while (result < m_thresholds.size() && m_thresholds[result] < ratio) | 299 while (result < m_thresholds.size() && m_thresholds[result] < ratio) |
296 ++result; | 300 ++result; |
297 return result; | 301 return result; |
298 } | 302 } |
299 | 303 |
300 void IntersectionObserver::deliver() | 304 void IntersectionObserver::deliver() |
301 { | 305 { |
302 checkRootAndDetachIfNeeded(); | |
303 | 306 |
304 if (m_entries.isEmpty()) | 307 if (m_entries.isEmpty()) |
305 return; | 308 return; |
306 | 309 |
307 HeapVector<Member<IntersectionObserverEntry>> entries; | 310 HeapVector<Member<IntersectionObserverEntry>> entries; |
308 entries.swap(m_entries); | 311 entries.swap(m_entries); |
309 m_callback->handleEvent(entries, *this); | 312 m_callback->handleEvent(entries, *this); |
310 } | 313 } |
311 | 314 |
312 void IntersectionObserver::setActive(bool active) | 315 void IntersectionObserver::setActive(bool active) |
313 { | 316 { |
314 checkRootAndDetachIfNeeded(); | |
315 for (auto& observation : m_observations) | 317 for (auto& observation : m_observations) |
316 observation->setActive(m_root && active && isDescendantOfRoot(observatio
n->target())); | 318 observation->setActive(m_root && active && isDescendantOfRoot(observatio
n->target())); |
317 } | 319 } |
318 | 320 |
319 bool IntersectionObserver::hasPercentMargin() const | 321 bool IntersectionObserver::hasPercentMargin() const |
320 { | 322 { |
321 return (m_topMargin.type() == Percent | 323 return (m_topMargin.type() == Percent |
322 || m_rightMargin.type() == Percent | 324 || m_rightMargin.type() == Percent |
323 || m_bottomMargin.type() == Percent | 325 || m_bottomMargin.type() == Percent |
324 || m_leftMargin.type() == Percent); | 326 || m_leftMargin.type() == Percent); |
325 } | 327 } |
326 | 328 |
327 void IntersectionObserver::checkRootAndDetachIfNeeded() | |
328 { | |
329 #if ENABLE(OILPAN) | |
330 // TODO(szager): Pre-oilpan, ElementIntersectionObserverData::dispose() will
take | |
331 // care of this cleanup. When oilpan ships, there will be a potential leak
of the | |
332 // callback's execution context when the root goes away. For a detailed exp
lanation: | |
333 // | |
334 // https://goo.gl/PC2Baj | |
335 // | |
336 // When that happens, this method should catch most potential leaks, but a c
omplete | |
337 // solution will still be needed, along the lines described in the above lin
k. | |
338 if (m_root) | |
339 return; | |
340 disconnect(); | |
341 #endif | |
342 } | |
343 | |
344 DEFINE_TRACE(IntersectionObserver) | 329 DEFINE_TRACE(IntersectionObserver) |
345 { | 330 { |
| 331 #if ENABLE(OILPAN) |
| 332 visitor->template registerWeakMembers<IntersectionObserver, &IntersectionObs
erver::clearWeakMembers>(this); |
| 333 #endif |
346 visitor->trace(m_callback); | 334 visitor->trace(m_callback); |
347 visitor->trace(m_root); | |
348 visitor->trace(m_observations); | 335 visitor->trace(m_observations); |
349 visitor->trace(m_entries); | 336 visitor->trace(m_entries); |
350 } | 337 } |
351 | 338 |
352 } // namespace blink | 339 } // namespace blink |
OLD | NEW |