| 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 // Note that the spec says the initial value of m_lastThresholdIndex |
| 20 // should be -1, but since m_lastThresholdIndex is unsigned, we use a |
| 21 // different sentinel value. |
| 22 m_lastThresholdIndex(kMaxThresholdIndex - 1) {} |
| 20 | 23 |
| 21 void IntersectionObservation::computeIntersectionObservations( | 24 void IntersectionObservation::computeIntersectionObservations( |
| 22 DOMHighResTimeStamp timestamp) { | 25 DOMHighResTimeStamp timestamp) { |
| 23 DCHECK(observer()); | 26 DCHECK(observer()); |
| 24 if (!m_target) | 27 if (!m_target) |
| 25 return; | 28 return; |
| 26 Vector<Length> rootMargin(4); | 29 Vector<Length> rootMargin(4); |
| 27 rootMargin[0] = m_observer->topMargin(); | 30 rootMargin[0] = m_observer->topMargin(); |
| 28 rootMargin[1] = m_observer->rightMargin(); | 31 rootMargin[1] = m_observer->rightMargin(); |
| 29 rootMargin[2] = m_observer->bottomMargin(); | 32 rootMargin[2] = m_observer->bottomMargin(); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 56 geometry.intersectionRect().size().height().toFloat(); | 59 geometry.intersectionRect().size().height().toFloat(); |
| 57 float targetArea = geometry.targetRect().size().width().toFloat() * | 60 float targetArea = geometry.targetRect().size().width().toFloat() * |
| 58 geometry.targetRect().size().height().toFloat(); | 61 geometry.targetRect().size().height().toFloat(); |
| 59 newVisibleRatio = intersectionArea / targetArea; | 62 newVisibleRatio = intersectionArea / targetArea; |
| 60 } | 63 } |
| 61 newThresholdIndex = observer()->firstThresholdGreaterThan(newVisibleRatio); | 64 newThresholdIndex = observer()->firstThresholdGreaterThan(newVisibleRatio); |
| 62 } else { | 65 } else { |
| 63 newVisibleRatio = 0; | 66 newVisibleRatio = 0; |
| 64 newThresholdIndex = 0; | 67 newThresholdIndex = 0; |
| 65 } | 68 } |
| 69 |
| 70 RELEASE_ASSERT(newThresholdIndex < kMaxThresholdIndex); |
| 71 |
| 66 if (m_lastThresholdIndex != newThresholdIndex) { | 72 if (m_lastThresholdIndex != newThresholdIndex) { |
| 67 IntRect snappedRootBounds = geometry.rootIntRect(); | 73 IntRect snappedRootBounds = geometry.rootIntRect(); |
| 68 IntRect* rootBoundsPointer = | 74 IntRect* rootBoundsPointer = |
| 69 m_shouldReportRootBounds ? &snappedRootBounds : nullptr; | 75 m_shouldReportRootBounds ? &snappedRootBounds : nullptr; |
| 70 IntersectionObserverEntry* newEntry = new IntersectionObserverEntry( | 76 IntersectionObserverEntry* newEntry = new IntersectionObserverEntry( |
| 71 timestamp, newVisibleRatio, geometry.targetIntRect(), rootBoundsPointer, | 77 timestamp, newVisibleRatio, geometry.targetIntRect(), rootBoundsPointer, |
| 72 geometry.intersectionIntRect(), target()); | 78 geometry.intersectionIntRect(), target()); |
| 73 observer()->enqueueIntersectionObserverEntry(*newEntry); | 79 observer()->enqueueIntersectionObserverEntry(*newEntry); |
| 74 setLastThresholdIndex(newThresholdIndex); | 80 setLastThresholdIndex(newThresholdIndex); |
| 75 } | 81 } |
| 76 } | 82 } |
| 77 | 83 |
| 78 void IntersectionObservation::disconnect() { | 84 void IntersectionObservation::disconnect() { |
| 79 DCHECK(observer()); | 85 DCHECK(observer()); |
| 80 if (m_target) | 86 if (m_target) |
| 81 target()->ensureIntersectionObserverData().removeObservation(*observer()); | 87 target()->ensureIntersectionObserverData().removeObservation(*observer()); |
| 82 m_observer.clear(); | 88 m_observer.clear(); |
| 83 } | 89 } |
| 84 | 90 |
| 85 DEFINE_TRACE(IntersectionObservation) { | 91 DEFINE_TRACE(IntersectionObservation) { |
| 86 visitor->trace(m_observer); | 92 visitor->trace(m_observer); |
| 87 visitor->trace(m_target); | 93 visitor->trace(m_target); |
| 88 } | 94 } |
| 89 | 95 |
| 90 } // namespace blink | 96 } // namespace blink |
| OLD | NEW |