Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(150)

Side by Side Diff: third_party/WebKit/Source/core/dom/IntersectionObservation.cpp

Issue 1672273002: Enforce containing block requirement for IntersectionObserver. (Closed) Base URL: https://chromium.googlesource.com/chromium/src@master
Patch Set: nits Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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() == ancestorFrame ;
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)
97 return false; 122 return false;
98 if (!targetLayoutObject->isBoxModelObject() && !targetLayoutObject->isText() ) 123 if (!targetLayoutObject->isBoxModelObject() && !targetLayoutObject->isText() )
99 return false; 124 return false;
125 if (!rootLayoutObject || !rootLayoutObject->isBoxModelObject())
126 return false;
127 if (!isContainingBlockChainDescendant(targetLayoutObject, rootLayoutObject))
128 return false;
100 129
101 // Initialize targetRect and intersectionRect to bounds of target, in target 's coordinate space. 130 // Initialize targetRect and intersectionRect to bounds of target, in target 's coordinate space.
102 initializeGeometry(geometry); 131 initializeGeometry(geometry);
103 132
104 // TODO(szager): Support intersection observations for zero-area targets. F or now, we just 133 // TODO(szager): Support intersection observations for zero-area targets. F or now, we just
105 // punt on the observation. 134 // punt on the observation.
106 if (!geometry.targetRect.size().width() || !geometry.targetRect.size().heigh t()) 135 if (!geometry.targetRect.size().width() || !geometry.targetRect.size().heigh t())
107 return false; 136 return false;
108 137
109 // Clip intersectionRect to the root, and map it to root coordinates. 138 // Clip intersectionRect to the root, and map it to root coordinates.
110 clipToRoot(geometry.intersectionRect); 139 clipToRoot(geometry.intersectionRect);
111 140
112 // Map targetRect into document coordinates. 141 // Map targetRect into document coordinates.
113 mapRectToDocumentCoordinates(*targetLayoutObject, geometry.targetRect); 142 mapRectToDocumentCoordinates(*targetLayoutObject, geometry.targetRect);
114 143
115 // Map intersectionRect into document coordinates. 144 // Map intersectionRect into document coordinates.
116 mapRectToDocumentCoordinates(*rootLayoutObject, geometry.intersectionRect); 145 mapRectToDocumentCoordinates(*rootLayoutObject, geometry.intersectionRect);
117 146
118 // Clip intersectionRect to FrameView visible area if necessary, and map all geometry to frame coordinates. 147 // Clip intersectionRect to FrameView visible area if necessary, and map all geometry to frame coordinates.
119 clipToFrameView(geometry); 148 clipToFrameView(geometry);
120 149
121 if (geometry.intersectionRect.size().isZero()) 150 if (geometry.intersectionRect.size().isZero())
122 geometry.intersectionRect = LayoutRect(); 151 geometry.intersectionRect = LayoutRect();
123 152
124 return true; 153 return true;
125 } 154 }
126 155
127 void IntersectionObservation::computeIntersectionObservations(DOMHighResTimeStam p timestamp) 156 void IntersectionObservation::computeIntersectionObservations(DOMHighResTimeStam p timestamp)
128 { 157 {
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; 158 IntersectionGeometry geometry;
143 if (!computeGeometry(geometry)) 159 if (!computeGeometry(geometry))
144 return; 160 return;
145 161
146 float intersectionArea = geometry.intersectionRect.size().width().toFloat() * geometry.intersectionRect.size().height().toFloat(); 162 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(); 163 float targetArea = geometry.targetRect.size().width().toFloat() * geometry.t argetRect.size().height().toFloat();
148 if (!targetArea) 164 if (!targetArea)
149 return; 165 return;
150 float newVisibleRatio = intersectionArea / targetArea; 166 float newVisibleRatio = intersectionArea / targetArea;
151 unsigned newThresholdIndex = observer().firstThresholdGreaterThan(newVisible Ratio); 167 unsigned newThresholdIndex = observer().firstThresholdGreaterThan(newVisible Ratio);
152 IntRect snappedRootBounds = pixelSnappedIntRect(geometry.rootRect); 168 IntRect snappedRootBounds = pixelSnappedIntRect(geometry.rootRect);
153 IntRect* rootBoundsPointer = m_shouldReportRootBounds ? &snappedRootBounds : nullptr; 169 IntRect* rootBoundsPointer = m_shouldReportRootBounds ? &snappedRootBounds : nullptr;
154 if (m_lastThresholdIndex != newThresholdIndex) { 170 if (m_lastThresholdIndex != newThresholdIndex) {
155 IntersectionObserverEntry* newEntry = new IntersectionObserverEntry( 171 IntersectionObserverEntry* newEntry = new IntersectionObserverEntry(
156 timestamp, 172 timestamp,
157 pixelSnappedIntRect(geometry.targetRect), 173 pixelSnappedIntRect(geometry.targetRect),
158 rootBoundsPointer, 174 rootBoundsPointer,
159 pixelSnappedIntRect(geometry.intersectionRect), 175 pixelSnappedIntRect(geometry.intersectionRect),
160 targetElement); 176 target());
161 observer().enqueueIntersectionObserverEntry(*newEntry); 177 observer().enqueueIntersectionObserverEntry(*newEntry);
162 } 178 }
163 setLastThresholdIndex(newThresholdIndex); 179 setLastThresholdIndex(newThresholdIndex);
164 } 180 }
165 181
166 void IntersectionObservation::disconnect() 182 void IntersectionObservation::disconnect()
167 { 183 {
168 IntersectionObserver* observer = m_observer; 184 IntersectionObserver* observer = m_observer;
169 clearRootAndRemoveFromTarget(); 185 clearRootAndRemoveFromTarget();
170 observer->removeObservation(*this); 186 observer->removeObservation(*this);
171 } 187 }
172 188
173 void IntersectionObservation::clearRootAndRemoveFromTarget() 189 void IntersectionObservation::clearRootAndRemoveFromTarget()
174 { 190 {
175 if (m_target) 191 if (m_target)
176 target()->ensureIntersectionObserverData().removeObservation(observer()) ; 192 target()->ensureIntersectionObserverData().removeObservation(observer()) ;
177 m_observer.clear(); 193 m_observer.clear();
178 } 194 }
179 195
180 DEFINE_TRACE(IntersectionObservation) 196 DEFINE_TRACE(IntersectionObservation)
181 { 197 {
182 visitor->trace(m_observer); 198 visitor->trace(m_observer);
183 visitor->trace(m_target); 199 visitor->trace(m_target);
184 } 200 }
185 201
186 } // namespace blink 202 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698