| 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/IntersectionObservation.h" | 5 #include "core/dom/IntersectionObservation.h" |
| 6 | 6 |
| 7 #include "core/dom/ElementRareData.h" | 7 #include "core/dom/ElementRareData.h" |
| 8 #include "core/dom/IntersectionObserver.h" | 8 #include "core/dom/IntersectionObserver.h" |
| 9 #include "core/layout/IntersectionGeometry.h" | 9 #include "core/layout/IntersectionGeometry.h" |
| 10 | 10 |
| 11 namespace blink { | 11 namespace blink { |
| 12 | 12 |
| 13 IntersectionObservation::IntersectionObservation(IntersectionObserver& observer, | 13 IntersectionObservation::IntersectionObservation(IntersectionObserver& observer, |
| 14 Element& target, | 14 Element& target, |
| 15 bool shouldReportRootBounds) | 15 bool shouldReportRootBounds) |
| 16 : m_observer(observer), | 16 : m_observer(observer), |
| 17 m_target(&target), | 17 m_target(&target), |
| 18 m_shouldReportRootBounds(shouldReportRootBounds), | 18 m_shouldReportRootBounds(shouldReportRootBounds), |
| 19 m_lastThresholdIndex(0) {} | 19 m_lastThresholdIndex(0) {} |
| 20 | 20 |
| 21 void IntersectionObservation::computeIntersectionObservations( | 21 void IntersectionObservation::computeIntersectionObservations( |
| 22 DOMHighResTimeStamp timestamp) { | 22 DOMHighResTimeStamp timestamp) { |
| 23 DCHECK(observer()); |
| 23 if (!m_target) | 24 if (!m_target) |
| 24 return; | 25 return; |
| 25 Vector<Length> rootMargin(4); | 26 Vector<Length> rootMargin(4); |
| 26 rootMargin[0] = m_observer->topMargin(); | 27 rootMargin[0] = m_observer->topMargin(); |
| 27 rootMargin[1] = m_observer->rightMargin(); | 28 rootMargin[1] = m_observer->rightMargin(); |
| 28 rootMargin[2] = m_observer->bottomMargin(); | 29 rootMargin[2] = m_observer->bottomMargin(); |
| 29 rootMargin[3] = m_observer->leftMargin(); | 30 rootMargin[3] = m_observer->leftMargin(); |
| 30 IntersectionGeometry geometry(m_observer->root(), *target(), rootMargin, | 31 IntersectionGeometry geometry(m_observer->root(), *target(), rootMargin, |
| 31 m_shouldReportRootBounds); | 32 m_shouldReportRootBounds); |
| 32 geometry.computeGeometry(); | 33 geometry.computeGeometry(); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 50 if (geometry.targetRect().isEmpty()) { | 51 if (geometry.targetRect().isEmpty()) { |
| 51 newVisibleRatio = 1; | 52 newVisibleRatio = 1; |
| 52 } else { | 53 } else { |
| 53 float intersectionArea = | 54 float intersectionArea = |
| 54 geometry.intersectionRect().size().width().toFloat() * | 55 geometry.intersectionRect().size().width().toFloat() * |
| 55 geometry.intersectionRect().size().height().toFloat(); | 56 geometry.intersectionRect().size().height().toFloat(); |
| 56 float targetArea = geometry.targetRect().size().width().toFloat() * | 57 float targetArea = geometry.targetRect().size().width().toFloat() * |
| 57 geometry.targetRect().size().height().toFloat(); | 58 geometry.targetRect().size().height().toFloat(); |
| 58 newVisibleRatio = intersectionArea / targetArea; | 59 newVisibleRatio = intersectionArea / targetArea; |
| 59 } | 60 } |
| 60 newThresholdIndex = observer().firstThresholdGreaterThan(newVisibleRatio); | 61 newThresholdIndex = observer()->firstThresholdGreaterThan(newVisibleRatio); |
| 61 } else { | 62 } else { |
| 62 newVisibleRatio = 0; | 63 newVisibleRatio = 0; |
| 63 newThresholdIndex = 0; | 64 newThresholdIndex = 0; |
| 64 } | 65 } |
| 65 if (m_lastThresholdIndex != newThresholdIndex) { | 66 if (m_lastThresholdIndex != newThresholdIndex) { |
| 66 IntRect snappedRootBounds = geometry.rootIntRect(); | 67 IntRect snappedRootBounds = geometry.rootIntRect(); |
| 67 IntRect* rootBoundsPointer = | 68 IntRect* rootBoundsPointer = |
| 68 m_shouldReportRootBounds ? &snappedRootBounds : nullptr; | 69 m_shouldReportRootBounds ? &snappedRootBounds : nullptr; |
| 69 IntersectionObserverEntry* newEntry = new IntersectionObserverEntry( | 70 IntersectionObserverEntry* newEntry = new IntersectionObserverEntry( |
| 70 timestamp, newVisibleRatio, geometry.targetIntRect(), rootBoundsPointer, | 71 timestamp, newVisibleRatio, geometry.targetIntRect(), rootBoundsPointer, |
| 71 geometry.intersectionIntRect(), target()); | 72 geometry.intersectionIntRect(), target()); |
| 72 observer().enqueueIntersectionObserverEntry(*newEntry); | 73 observer()->enqueueIntersectionObserverEntry(*newEntry); |
| 73 setLastThresholdIndex(newThresholdIndex); | 74 setLastThresholdIndex(newThresholdIndex); |
| 74 } | 75 } |
| 75 } | 76 } |
| 76 | 77 |
| 77 void IntersectionObservation::disconnect() { | 78 void IntersectionObservation::disconnect() { |
| 78 IntersectionObserver* observer = m_observer; | 79 DCHECK(observer()); |
| 79 clearRootAndRemoveFromTarget(); | |
| 80 observer->removeObservation(*this); | |
| 81 } | |
| 82 | |
| 83 void IntersectionObservation::clearRootAndRemoveFromTarget() { | |
| 84 if (m_target) | 80 if (m_target) |
| 85 target()->ensureIntersectionObserverData().removeObservation(observer()); | 81 target()->ensureIntersectionObserverData().removeObservation(*observer()); |
| 86 m_observer.clear(); | 82 m_observer.clear(); |
| 87 } | 83 } |
| 88 | 84 |
| 89 DEFINE_TRACE(IntersectionObservation) { | 85 DEFINE_TRACE(IntersectionObservation) { |
| 90 visitor->trace(m_observer); | 86 visitor->trace(m_observer); |
| 91 visitor->trace(m_target); | 87 visitor->trace(m_target); |
| 92 } | 88 } |
| 93 | 89 |
| 94 } // namespace blink | 90 } // namespace blink |
| OLD | NEW |