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

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

Issue 1449623002: IntersectionObserver: second cut. (Closed) Base URL: https://chromium.googlesource.com/chromium/src@master
Patch Set: Add RuntimeEnabled flags to all idl's, fix test expectations. Created 4 years, 11 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "core/dom/IntersectionObservation.h"
6
7 #include "core/dom/ElementRareData.h"
8 #include "core/dom/IntersectionObserver.h"
9 #include "core/frame/FrameView.h"
10 #include "core/layout/LayoutBox.h"
11 #include "core/layout/LayoutText.h"
12 #include "core/layout/LayoutView.h"
13 #include "core/paint/PaintLayer.h"
14
15 namespace blink {
16
17 IntersectionObservation::IntersectionObservation(IntersectionObserver& observer, Element& target, bool shouldReportRootBounds)
18 : m_observer(observer)
19 , m_target(target.ensureIntersectionObserverData().createWeakPtr(&target))
20 , m_active(true)
21 , m_shouldReportRootBounds(shouldReportRootBounds)
22 , m_lastThresholdIndex(0)
23 {
24 }
25
26 void IntersectionObservation::initializeGeometry(IntersectionGeometry& geometry)
27 {
28 LayoutObject* targetLayoutObject = m_target->layoutObject();
29 if (targetLayoutObject->isBoxModelObject())
30 geometry.targetRect = toLayoutBoxModelObject(targetLayoutObject)->visual OverflowRect();
31 else
32 geometry.targetRect = toLayoutText(targetLayoutObject)->visualOverflowRe ct();
33 geometry.intersectionRect = geometry.targetRect;
34 }
35
36 void IntersectionObservation::clipToRoot(LayoutRect& rect)
37 {
38 // Map and clip rect into root element coordinates.
39 // TODO(szager): the writing mode flipping needs a test.
40 LayoutObject* rootLayoutObject = m_observer->rootLayoutObject();
41 LayoutObject* targetLayoutObject = m_target->layoutObject();
42 targetLayoutObject->mapToVisibleRectInAncestorSpace(toLayoutBoxModelObject(r ootLayoutObject), rect, nullptr);
43 if (rootLayoutObject->hasOverflowClip()) {
44 LayoutBox* rootLayoutBox = toLayoutBox(rootLayoutObject);
45 LayoutRect clipRect(LayoutPoint(), LayoutSize(rootLayoutBox->layer()->si ze()));
46 rootLayoutBox->flipForWritingMode(rect);
47 rect.intersect(clipRect);
48 rootLayoutBox->flipForWritingMode(rect);
49 }
50 }
51
52 void IntersectionObservation::clipToFrameView(IntersectionGeometry& geometry)
53 {
54 Element* rootElement = m_observer->root();
55 LayoutObject* rootLayoutObject = m_observer->rootLayoutObject();
56 if (rootElement == rootElement->document().documentElement()) {
57 geometry.rootRect = LayoutRect(rootElement->document().view()->visibleCo ntentRect());
58 geometry.intersectionRect.intersect(geometry.rootRect);
59 } else {
60 if (rootLayoutObject->isBox())
61 geometry.rootRect = LayoutRect(toLayoutBox(rootLayoutObject)->absolu teContentBox());
62 else
63 geometry.rootRect = LayoutRect(rootLayoutObject->absoluteBoundingBox Rect());
64 }
65
66 LayoutPoint scrollPosition(rootElement->document().view()->scrollPosition()) ;
67 geometry.targetRect.moveBy(-scrollPosition);
68 geometry.intersectionRect.moveBy(-scrollPosition);
69 geometry.rootRect.moveBy(-scrollPosition);
70 }
71
72 static void mapRectToDocumentCoordinates(LayoutObject& layoutObject, LayoutRect& rect)
73 {
74 rect = LayoutRect(layoutObject.localToAbsoluteQuad(FloatQuad(FloatRect(rect) ), UseTransforms | ApplyContainerFlip | TraverseDocumentBoundaries).boundingBox( ));
75 }
76
77 bool IntersectionObservation::computeGeometry(IntersectionGeometry& geometry)
78 {
79 LayoutObject* rootLayoutObject = m_observer->rootLayoutObject();
80 LayoutObject* targetLayoutObject = m_target->layoutObject();
haraken 2016/01/10 12:22:28 Given that m_target is a WeakMember, it can be cle
szager1 2016/01/10 17:18:26 All of the un-checked uses of m_target are in priv
81 if (!rootLayoutObject->isBoxModelObject())
82 return false;
83 if (!targetLayoutObject->isBoxModelObject() && !targetLayoutObject->isText() )
84 return false;
85
86 // Initialize targetRect and intersectionRect to bounds of target, in target 's coordinate space.
87 initializeGeometry(geometry);
88
89 // TODO(szager): Support intersection observations for zero-area targets. F or now, we just
90 // punt on the observation.
91 if (!geometry.targetRect.size().width() || !geometry.targetRect.size().heigh t())
92 return false;
93
94 // Clip intersectionRect to the root, and map it to root coordinates.
95 clipToRoot(geometry.intersectionRect);
96
97 // Map targetRect into document coordinates.
98 mapRectToDocumentCoordinates(*targetLayoutObject, geometry.targetRect);
99
100 // Map intersectionRect into document coordinates.
101 mapRectToDocumentCoordinates(*rootLayoutObject, geometry.intersectionRect);
102
103 // Clip intersectionRect to FrameView visible area if necessary, and map all geometry to frame coordinates.
104 clipToFrameView(geometry);
105
106 if (geometry.intersectionRect.size().isZero())
107 geometry.intersectionRect = LayoutRect();
108 if (!m_shouldReportRootBounds)
109 geometry.rootRect = LayoutRect();
110
111 return true;
112 }
113
114 void IntersectionObservation::computeIntersectionObservations(double timestamp)
115 {
116 Element* targetElement = target();
117 if (!targetElement || !isActive())
118 return;
119 LayoutObject* targetLayoutObject = targetElement->layoutObject();
120 // TODO(szager): Support SVG
121 if (!targetLayoutObject || (!targetLayoutObject->isBox() && !targetLayoutObj ect->isInline()))
122 return;
123
124 IntersectionGeometry geometry;
125 if (!computeGeometry(geometry))
126 return;
127
128 float intersectionArea = geometry.intersectionRect.size().width().toFloat() * geometry.intersectionRect.size().height().toFloat();
129 float targetArea = geometry.targetRect.size().width().toFloat() * geometry.t argetRect.size().height().toFloat();
130 if (!targetArea)
131 return;
132 float newVisibleRatio = intersectionArea / targetArea;
133 unsigned newThresholdIndex = observer().firstThresholdGreaterThan(newVisible Ratio);
134 if (m_lastThresholdIndex != newThresholdIndex) {
135 IntersectionObserverEntry* newEntry = new IntersectionObserverEntry(
136 timestamp / 1000.0,
137 pixelSnappedIntRect(geometry.targetRect),
138 pixelSnappedIntRect(geometry.rootRect),
139 pixelSnappedIntRect(geometry.intersectionRect),
140 targetElement);
141 observer().enqueueIntersectionObserverEntry(*newEntry);
142 }
143 setLastThresholdIndex(newThresholdIndex);
144 }
145
146 void IntersectionObservation::disconnect()
147 {
148 if (m_target) {
149 m_target->ensureIntersectionObserverData().removeObservation(this->obser ver());
150 m_target.clear();
151 }
152 m_observer->removeObservation(*this);
153 m_observer.clear();
154 }
155
156 DEFINE_TRACE(IntersectionObservation)
157 {
158 visitor->trace(m_observer);
159 visitor->trace(m_target);
160 }
161
162 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698