Chromium Code Reviews| 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/frame/FrameView.h" | 9 #include "core/frame/FrameView.h" |
| 10 #include "core/layout/LayoutBox.h" | 10 #include "core/layout/LayoutBox.h" |
| 11 #include "core/layout/LayoutPart.h" | |
| 11 #include "core/layout/LayoutText.h" | 12 #include "core/layout/LayoutText.h" |
| 12 #include "core/layout/LayoutView.h" | 13 #include "core/layout/LayoutView.h" |
| 13 #include "core/paint/PaintLayer.h" | 14 #include "core/paint/PaintLayer.h" |
| 14 | 15 |
| 15 namespace blink { | 16 namespace blink { |
| 16 | 17 |
| 17 IntersectionObservation::IntersectionObservation(IntersectionObserver& observer, Element& target, bool shouldReportRootBounds) | 18 IntersectionObservation::IntersectionObservation(IntersectionObserver& observer, Element& target, bool shouldReportRootBounds) |
| 18 : m_observer(observer) | 19 : m_observer(observer) |
| 19 , m_target(target.ensureIntersectionObserverData().createWeakPtr(&target)) | 20 , m_target(target.ensureIntersectionObserverData().createWeakPtr(&target)) |
| 20 , m_active(true) | |
| 21 , m_shouldReportRootBounds(shouldReportRootBounds) | 21 , m_shouldReportRootBounds(shouldReportRootBounds) |
| 22 , m_lastThresholdIndex(0) | 22 , m_lastThresholdIndex(0) |
| 23 { | 23 { |
| 24 } | 24 } |
| 25 | 25 |
| 26 Element* IntersectionObservation::target() const | 26 Element* IntersectionObservation::target() const |
| 27 { | 27 { |
| 28 return toElement(m_target.get()); | 28 return toElement(m_target.get()); |
| 29 } | 29 } |
| 30 | 30 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 81 geometry.targetRect.moveBy(-scrollPosition); | 81 geometry.targetRect.moveBy(-scrollPosition); |
| 82 geometry.intersectionRect.moveBy(-scrollPosition); | 82 geometry.intersectionRect.moveBy(-scrollPosition); |
| 83 geometry.rootRect.moveBy(-scrollPosition); | 83 geometry.rootRect.moveBy(-scrollPosition); |
| 84 } | 84 } |
| 85 | 85 |
| 86 static void mapRectToDocumentCoordinates(LayoutObject& layoutObject, LayoutRect& rect) | 86 static void mapRectToDocumentCoordinates(LayoutObject& layoutObject, LayoutRect& rect) |
| 87 { | 87 { |
| 88 rect = LayoutRect(layoutObject.localToAbsoluteQuad(FloatQuad(FloatRect(rect) ), UseTransforms | ApplyContainerFlip | TraverseDocumentBoundaries).boundingBox( )); | 88 rect = LayoutRect(layoutObject.localToAbsoluteQuad(FloatQuad(FloatRect(rect) ), UseTransforms | ApplyContainerFlip | TraverseDocumentBoundaries).boundingBox( )); |
| 89 } | 89 } |
| 90 | 90 |
| 91 static bool isContainingBlockChainDescendant(LayoutObject* descendant, LayoutObj ect* ancestor) | |
| 92 { | |
| 93 LocalFrame* ancestorFrame = ancestor->document().frame(); | |
| 94 LocalFrame* descendantFrame = descendant->document().frame(); | |
| 95 | |
| 96 if (ancestor->isLayoutView()) | |
| 97 return (descendantFrame && descendantFrame->tree().top() == ancestorFram e); | |
|
esprehn
2016/02/11 06:15:23
remove parens
szager1
2016/02/11 18:17:24
Done.
| |
| 98 | |
| 99 if (ancestorFrame != descendantFrame) | |
| 100 return false; | |
| 101 | |
| 102 while (descendant && descendant != ancestor) | |
| 103 descendant = descendant->containingBlock(); | |
| 104 return descendant; | |
| 105 } | |
| 106 | |
| 91 bool IntersectionObservation::computeGeometry(IntersectionGeometry& geometry) | 107 bool IntersectionObservation::computeGeometry(IntersectionGeometry& geometry) |
| 92 { | 108 { |
| 93 ASSERT(m_target); | 109 // Pre-oilpan, there will be a delay between the time when the target Elemen t gets deleted |
| 110 // (because its ref count dropped to zero) and when this IntersectionObserva tion gets | |
| 111 // deleted (during the next gc run, because the target Element is the only t hing keeping | |
| 112 // the IntersectionObservation alive). During that interval, we need to che ck that m_target | |
| 113 // hasn't been cleared. | |
| 114 Element* targetElement = target(); | |
| 115 if (!targetElement || !targetElement->inDocument()) | |
| 116 return false; | |
| 117 LayoutObject* targetLayoutObject = targetElement->layoutObject(); | |
| 118 ASSERT(m_observer); | |
| 94 LayoutObject* rootLayoutObject = m_observer->rootLayoutObject(); | 119 LayoutObject* rootLayoutObject = m_observer->rootLayoutObject(); |
| 95 LayoutObject* targetLayoutObject = target()->layoutObject(); | 120 // TODO(szager): Support SVG |
| 96 if (!rootLayoutObject->isBoxModelObject()) | 121 if (!targetLayoutObject || !(targetLayoutObject->isBoxModelObject() || targe tLayoutObject->isText())) |
|
esprehn
2016/02/11 06:15:23
if (!targetLayoutObject)
return false;
if (!targ
szager1
2016/02/11 18:17:24
Done.
| |
| 97 return false; | 122 return false; |
| 98 if (!targetLayoutObject->isBoxModelObject() && !targetLayoutObject->isText() ) | 123 if (!rootLayoutObject || !rootLayoutObject->isBoxModelObject()) |
| 124 return false; | |
| 125 if (!isContainingBlockChainDescendant(targetLayoutObject, rootLayoutObject)) | |
| 99 return false; | 126 return false; |
| 100 | 127 |
| 101 // Initialize targetRect and intersectionRect to bounds of target, in target 's coordinate space. | 128 // Initialize targetRect and intersectionRect to bounds of target, in target 's coordinate space. |
| 102 initializeGeometry(geometry); | 129 initializeGeometry(geometry); |
| 103 | 130 |
| 104 // TODO(szager): Support intersection observations for zero-area targets. F or now, we just | 131 // TODO(szager): Support intersection observations for zero-area targets. F or now, we just |
| 105 // punt on the observation. | 132 // punt on the observation. |
| 106 if (!geometry.targetRect.size().width() || !geometry.targetRect.size().heigh t()) | 133 if (!geometry.targetRect.size().width() || !geometry.targetRect.size().heigh t()) |
| 107 return false; | 134 return false; |
| 108 | 135 |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 119 clipToFrameView(geometry); | 146 clipToFrameView(geometry); |
| 120 | 147 |
| 121 if (geometry.intersectionRect.size().isZero()) | 148 if (geometry.intersectionRect.size().isZero()) |
| 122 geometry.intersectionRect = LayoutRect(); | 149 geometry.intersectionRect = LayoutRect(); |
| 123 | 150 |
| 124 return true; | 151 return true; |
| 125 } | 152 } |
| 126 | 153 |
| 127 void IntersectionObservation::computeIntersectionObservations(DOMHighResTimeStam p timestamp) | 154 void IntersectionObservation::computeIntersectionObservations(DOMHighResTimeStam p timestamp) |
| 128 { | 155 { |
| 129 // Pre-oilpan, there will be a delay between the time when the target Elemen t gets deleted | |
| 130 // (because its ref count dropped to zero) and when this IntersectionObserva tion gets | |
| 131 // deleted (during the next gc run, because the target Element is the only t hing keeping | |
| 132 // the IntersectionObservation alive). During that interval, we need to che ck that m_target | |
| 133 // hasn't been cleared. | |
| 134 Element* targetElement = target(); | |
| 135 if (!targetElement || !isActive()) | |
| 136 return; | |
| 137 LayoutObject* targetLayoutObject = targetElement->layoutObject(); | |
| 138 // TODO(szager): Support SVG | |
| 139 if (!targetLayoutObject || (!targetLayoutObject->isBox() && !targetLayoutObj ect->isInline())) | |
| 140 return; | |
| 141 | |
| 142 IntersectionGeometry geometry; | 156 IntersectionGeometry geometry; |
| 143 if (!computeGeometry(geometry)) | 157 if (!computeGeometry(geometry)) |
| 144 return; | 158 return; |
| 145 | 159 |
| 146 float intersectionArea = geometry.intersectionRect.size().width().toFloat() * geometry.intersectionRect.size().height().toFloat(); | 160 float intersectionArea = geometry.intersectionRect.size().width().toFloat() * geometry.intersectionRect.size().height().toFloat(); |
| 147 float targetArea = geometry.targetRect.size().width().toFloat() * geometry.t argetRect.size().height().toFloat(); | 161 float targetArea = geometry.targetRect.size().width().toFloat() * geometry.t argetRect.size().height().toFloat(); |
| 148 if (!targetArea) | 162 if (!targetArea) |
| 149 return; | 163 return; |
| 150 float newVisibleRatio = intersectionArea / targetArea; | 164 float newVisibleRatio = intersectionArea / targetArea; |
| 151 unsigned newThresholdIndex = observer().firstThresholdGreaterThan(newVisible Ratio); | 165 unsigned newThresholdIndex = observer().firstThresholdGreaterThan(newVisible Ratio); |
| 152 IntRect snappedRootBounds = pixelSnappedIntRect(geometry.rootRect); | 166 IntRect snappedRootBounds = pixelSnappedIntRect(geometry.rootRect); |
| 153 IntRect* rootBoundsPointer = m_shouldReportRootBounds ? &snappedRootBounds : nullptr; | 167 IntRect* rootBoundsPointer = m_shouldReportRootBounds ? &snappedRootBounds : nullptr; |
| 154 if (m_lastThresholdIndex != newThresholdIndex) { | 168 if (m_lastThresholdIndex != newThresholdIndex) { |
| 155 IntersectionObserverEntry* newEntry = new IntersectionObserverEntry( | 169 IntersectionObserverEntry* newEntry = new IntersectionObserverEntry( |
| 156 timestamp, | 170 timestamp, |
| 157 pixelSnappedIntRect(geometry.targetRect), | 171 pixelSnappedIntRect(geometry.targetRect), |
| 158 rootBoundsPointer, | 172 rootBoundsPointer, |
| 159 pixelSnappedIntRect(geometry.intersectionRect), | 173 pixelSnappedIntRect(geometry.intersectionRect), |
| 160 targetElement); | 174 target()); |
| 161 observer().enqueueIntersectionObserverEntry(*newEntry); | 175 observer().enqueueIntersectionObserverEntry(*newEntry); |
| 162 } | 176 } |
| 163 setLastThresholdIndex(newThresholdIndex); | 177 setLastThresholdIndex(newThresholdIndex); |
| 164 } | 178 } |
| 165 | 179 |
| 166 void IntersectionObservation::disconnect() | 180 void IntersectionObservation::disconnect() |
| 167 { | 181 { |
| 168 IntersectionObserver* observer = m_observer; | 182 IntersectionObserver* observer = m_observer; |
| 169 clearRootAndRemoveFromTarget(); | 183 clearRootAndRemoveFromTarget(); |
| 170 observer->removeObservation(*this); | 184 observer->removeObservation(*this); |
| 171 } | 185 } |
| 172 | 186 |
| 173 void IntersectionObservation::clearRootAndRemoveFromTarget() | 187 void IntersectionObservation::clearRootAndRemoveFromTarget() |
| 174 { | 188 { |
| 175 if (m_target) | 189 if (m_target) |
| 176 target()->ensureIntersectionObserverData().removeObservation(observer()) ; | 190 target()->ensureIntersectionObserverData().removeObservation(observer()) ; |
| 177 m_observer.clear(); | 191 m_observer.clear(); |
| 178 } | 192 } |
| 179 | 193 |
| 180 DEFINE_TRACE(IntersectionObservation) | 194 DEFINE_TRACE(IntersectionObservation) |
| 181 { | 195 { |
| 182 visitor->trace(m_observer); | 196 visitor->trace(m_observer); |
| 183 visitor->trace(m_target); | 197 visitor->trace(m_target); |
| 184 } | 198 } |
| 185 | 199 |
| 186 } // namespace blink | 200 } // namespace blink |
| OLD | NEW |