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/page/scrolling/RootScroller.h" | 5 #include "core/page/scrolling/RootScroller.h" |
6 | 6 |
| 7 #include "core/dom/Document.h" |
7 #include "core/dom/Element.h" | 8 #include "core/dom/Element.h" |
8 #include "core/frame/FrameHost.h" | 9 #include "core/frame/FrameHost.h" |
9 #include "core/frame/FrameView.h" | 10 #include "core/frame/FrameView.h" |
| 11 #include "core/frame/TopControls.h" |
10 #include "core/layout/LayoutBox.h" | 12 #include "core/layout/LayoutBox.h" |
11 #include "core/page/Page.h" | 13 #include "core/page/Page.h" |
| 14 #include "core/page/scrolling/OverscrollController.h" |
12 #include "core/page/scrolling/ViewportScrollCallback.h" | 15 #include "core/page/scrolling/ViewportScrollCallback.h" |
13 #include "core/paint/PaintLayerScrollableArea.h" | 16 #include "core/paint/PaintLayerScrollableArea.h" |
14 #include "platform/scroll/ScrollableArea.h" | 17 #include "platform/scroll/ScrollableArea.h" |
15 | 18 |
16 namespace blink { | 19 namespace blink { |
17 | 20 |
18 namespace { | 21 namespace { |
19 | 22 |
20 Document* topDocument(FrameHost* frameHost) | 23 ScrollableArea* scrollableAreaFor(const Element& element) |
21 { | |
22 DCHECK(frameHost); | |
23 if (!frameHost->page().mainFrame()) | |
24 return nullptr; | |
25 | |
26 DCHECK(frameHost->page().mainFrame()->isLocalFrame()); | |
27 return toLocalFrame(frameHost->page().mainFrame())->document(); | |
28 } | |
29 | |
30 ScrollableArea* scrollableAreaFor(Element& element) | |
31 { | 24 { |
32 if (!element.layoutObject() || !element.layoutObject()->isBox()) | 25 if (!element.layoutObject() || !element.layoutObject()->isBox()) |
33 return nullptr; | 26 return nullptr; |
34 | 27 |
35 LayoutBox* box = toLayoutBox(element.layoutObject()); | 28 LayoutBox* box = toLayoutBox(element.layoutObject()); |
36 | 29 |
37 if (box->isDocumentElement() && element.document().view()) | 30 if (box->isDocumentElement()) |
38 return element.document().view()->getScrollableArea(); | 31 return element.document().view()->getScrollableArea(); |
39 | 32 |
40 return static_cast<PaintInvalidationCapableScrollableArea*>( | 33 return static_cast<PaintInvalidationCapableScrollableArea*>( |
41 box->getScrollableArea()); | 34 box->getScrollableArea()); |
42 } | 35 } |
43 | 36 |
| 37 bool fillsViewport(const Element& element) |
| 38 { |
| 39 DCHECK(element.layoutObject()); |
| 40 DCHECK(element.layoutObject()->isBox()); |
| 41 |
| 42 LayoutObject* layoutObject = element.layoutObject(); |
| 43 |
| 44 // TODO(bokan): Broken for OOPIF. |
| 45 Document& topDocument = element.document().topDocument(); |
| 46 |
| 47 Vector<FloatQuad> quads; |
| 48 layoutObject->absoluteQuads(quads); |
| 49 DCHECK_EQ(quads.size(), 1u); |
| 50 |
| 51 if (!quads[0].isRectilinear()) |
| 52 return false; |
| 53 |
| 54 LayoutRect boundingBox(quads[0].boundingBox()); |
| 55 |
| 56 return boundingBox.location() == LayoutPoint::zero() |
| 57 && boundingBox.size() == topDocument.layoutViewItem().size(); |
| 58 } |
| 59 |
| 60 bool isValidRootScroller(const Element& element) |
| 61 { |
| 62 if (!element.layoutObject()) |
| 63 return false; |
| 64 |
| 65 if (!scrollableAreaFor(element)) |
| 66 return false; |
| 67 |
| 68 if (!fillsViewport(element)) |
| 69 return false; |
| 70 |
| 71 return true; |
| 72 } |
| 73 |
44 } // namespace | 74 } // namespace |
45 | 75 |
46 RootScroller::RootScroller(FrameHost& frameHost) | 76 ViewportScrollCallback* RootScroller::createViewportApplyScroll( |
47 : m_frameHost(&frameHost) | 77 TopControls& topControls, OverscrollController& overscrollController) |
| 78 { |
| 79 return new ViewportScrollCallback(topControls, overscrollController); |
| 80 } |
| 81 |
| 82 RootScroller::RootScroller(Document& document, ViewportScrollCallback* applyScro
llCallback) |
| 83 : m_document(&document) |
| 84 , m_viewportApplyScroll(applyScrollCallback) |
48 { | 85 { |
49 } | 86 } |
50 | 87 |
51 DEFINE_TRACE(RootScroller) | 88 DEFINE_TRACE(RootScroller) |
52 { | 89 { |
53 visitor->trace(m_frameHost); | 90 visitor->trace(m_document); |
54 visitor->trace(m_viewportApplyScroll); | 91 visitor->trace(m_viewportApplyScroll); |
55 visitor->trace(m_rootScroller); | 92 visitor->trace(m_rootScroller); |
| 93 visitor->trace(m_effectiveRootScroller); |
56 } | 94 } |
57 | 95 |
58 bool RootScroller::set(Element& newRootScroller) | 96 void RootScroller::set(Element* newRootScroller) |
59 { | 97 { |
60 if (!isValid(newRootScroller)) | 98 m_rootScroller = newRootScroller; |
61 return false; | 99 updateEffectiveRootScroller(); |
62 | |
63 DCHECK(m_frameHost); | |
64 | |
65 Document* document = topDocument(m_frameHost); | |
66 if (!document) | |
67 return false; | |
68 | |
69 ScrollableArea* newRootScrollableArea = scrollableAreaFor(newRootScroller); | |
70 if (!newRootScrollableArea) | |
71 return false; | |
72 | |
73 if (m_rootScroller) | |
74 m_rootScroller->removeApplyScroll(); | |
75 | |
76 m_rootScroller = &newRootScroller; | |
77 | |
78 createApplyScrollIfNeeded(); | |
79 | |
80 // Ideally, the scrolling infrastructure would pass this to the callback but | |
81 // we don't get that today so we set it manually. | |
82 m_viewportApplyScroll->setScroller(*newRootScrollableArea); | |
83 | |
84 // Installs the viewport scrolling callback (the "applyScroll" in Scroll | |
85 // Customization lingo) on the given element. This callback is | |
86 // responsible for viewport related scroll actions like top controls | |
87 // movement and overscroll glow as well as actually scrolling the element. | |
88 // Use disable-native-scroll since the ViewportScrollCallback needs to | |
89 // apply scroll actions before (TopControls) and after (overscroll) | |
90 // scrolling the element so it applies scroll to the element itself. | |
91 m_rootScroller->setApplyScroll( | |
92 m_viewportApplyScroll, | |
93 "disable-native-scroll"); | |
94 | |
95 return true; | |
96 } | 100 } |
97 | 101 |
98 Element* RootScroller::get() const | 102 Element* RootScroller::get() const |
99 { | 103 { |
100 if (!m_frameHost) | |
101 return nullptr; | |
102 | |
103 return m_rootScroller; | 104 return m_rootScroller; |
104 } | 105 } |
105 | 106 |
106 void RootScroller::resetToDefault() | 107 Element* RootScroller::effectiveRootScroller() const |
107 { | 108 { |
108 if (!m_frameHost) | 109 return m_effectiveRootScroller; |
| 110 } |
| 111 |
| 112 void RootScroller::didUpdateLayout() |
| 113 { |
| 114 updateEffectiveRootScroller(); |
| 115 } |
| 116 |
| 117 void RootScroller::updateEffectiveRootScroller() |
| 118 { |
| 119 bool rootScrollerValid = |
| 120 m_rootScroller && isValidRootScroller(*m_rootScroller); |
| 121 |
| 122 Element* newEffectiveRootScroller = rootScrollerValid |
| 123 ? m_rootScroller.get() |
| 124 : defaultEffectiveRootScroller(); |
| 125 |
| 126 if (m_effectiveRootScroller == newEffectiveRootScroller) |
109 return; | 127 return; |
110 | 128 |
111 Document* document = topDocument(m_frameHost); | 129 moveViewportApplyScroll(newEffectiveRootScroller); |
112 if (!document) | 130 m_effectiveRootScroller = newEffectiveRootScroller; |
| 131 } |
| 132 |
| 133 void RootScroller::moveViewportApplyScroll(Element* target) |
| 134 { |
| 135 if (!m_viewportApplyScroll) |
113 return; | 136 return; |
114 | 137 |
115 if (Element* defaultRootElement = document->documentElement()) | 138 if (m_effectiveRootScroller) |
116 set(*defaultRootElement); | 139 m_effectiveRootScroller->removeApplyScroll(); |
| 140 |
| 141 ScrollableArea* targetScroller = |
| 142 target ? scrollableAreaFor(*target) : nullptr; |
| 143 |
| 144 if (targetScroller) { |
| 145 // Use disable-native-scroll since the ViewportScrollCallback needs to |
| 146 // apply scroll actions both before (TopControls) and after (overscroll) |
| 147 // scrolling the element so it will apply scroll to the element itself. |
| 148 target->setApplyScroll( |
| 149 m_viewportApplyScroll, |
| 150 "disable-native-scroll"); |
| 151 } |
| 152 |
| 153 // Ideally, scroll customization would pass the current element to scroll to |
| 154 // the apply scroll callback but this doesn't happen today so we set it |
| 155 // through a back door here. |
| 156 m_viewportApplyScroll->setScroller(targetScroller); |
117 } | 157 } |
118 | 158 |
119 void RootScroller::didUpdateTopDocumentLayout() | 159 Element* RootScroller::defaultEffectiveRootScroller() |
120 { | 160 { |
121 if (m_rootScroller && isValid(*m_rootScroller)) | 161 DCHECK(m_document); |
122 return; | 162 return m_document->documentElement(); |
123 | |
124 resetToDefault(); | |
125 } | |
126 | |
127 bool RootScroller::isValid(Element& element) const | |
128 { | |
129 if (!m_frameHost) | |
130 return false; | |
131 | |
132 if (element.document() != topDocument(m_frameHost)) | |
133 return false; | |
134 | |
135 if (!element.isInTreeScope()) | |
136 return false; | |
137 | |
138 if (!element.layoutObject()) | |
139 return false; | |
140 | |
141 if (!element.layoutObject()->isLayoutBlockFlow() | |
142 && !element.layoutObject()->isLayoutIFrame()) | |
143 return false; | |
144 | |
145 return true; | |
146 } | |
147 | |
148 void RootScroller::createApplyScrollIfNeeded() | |
149 { | |
150 if (!m_viewportApplyScroll) { | |
151 TopControls& topControls = m_frameHost->topControls(); | |
152 OverscrollController& overscrollController = | |
153 m_frameHost->overscrollController(); | |
154 m_viewportApplyScroll = | |
155 new ViewportScrollCallback(topControls, overscrollController); | |
156 } | |
157 } | 163 } |
158 | 164 |
159 } // namespace blink | 165 } // namespace blink |
OLD | NEW |