| 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/RootScrollerController.h" | 5 #include "core/page/scrolling/RootScrollerController.h" |
| 6 | 6 |
| 7 #include "core/dom/Document.h" | 7 #include "core/dom/Document.h" |
| 8 #include "core/dom/Element.h" | 8 #include "core/dom/Element.h" |
| 9 #include "core/frame/FrameHost.h" | 9 #include "core/frame/FrameHost.h" |
| 10 #include "core/frame/FrameView.h" | 10 #include "core/frame/FrameView.h" |
| 11 #include "core/frame/TopControls.h" | 11 #include "core/frame/TopControls.h" |
| 12 #include "core/frame/VisualViewport.h" | 12 #include "core/frame/VisualViewport.h" |
| 13 #include "core/layout/LayoutBox.h" | 13 #include "core/layout/LayoutBox.h" |
| 14 #include "core/page/ChromeClient.h" | 14 #include "core/page/ChromeClient.h" |
| 15 #include "core/page/Page.h" | 15 #include "core/page/Page.h" |
| 16 #include "core/page/scrolling/OverscrollController.h" | 16 #include "core/page/scrolling/OverscrollController.h" |
| 17 #include "core/page/scrolling/ViewportScrollCallback.h" | 17 #include "core/page/scrolling/ViewportScrollCallback.h" |
| 18 #include "core/paint/PaintLayerScrollableArea.h" | 18 #include "core/paint/PaintLayerScrollableArea.h" |
| 19 #include "platform/graphics/GraphicsLayer.h" | 19 #include "platform/graphics/GraphicsLayer.h" |
| 20 #include "platform/scroll/ScrollableArea.h" | 20 #include "platform/scroll/ScrollableArea.h" |
| 21 | 21 |
| 22 namespace blink { | 22 namespace blink { |
| 23 | 23 |
| 24 class RootFrameViewport; |
| 25 |
| 24 namespace { | 26 namespace { |
| 25 | 27 |
| 26 ScrollableArea* scrollableAreaFor(const Element& element) | 28 ScrollableArea* scrollableAreaFor(const Element& element) |
| 27 { | 29 { |
| 28 if (!element.layoutObject() || !element.layoutObject()->isBox()) | 30 if (!element.layoutObject() || !element.layoutObject()->isBox()) |
| 29 return nullptr; | 31 return nullptr; |
| 30 | 32 |
| 31 LayoutBox* box = toLayoutBox(element.layoutObject()); | 33 LayoutBox* box = toLayoutBox(element.layoutObject()); |
| 32 | 34 |
| 33 // For a FrameView, we use the layoutViewport rather than the | 35 // For a FrameView, we use the layoutViewport rather than the |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 : m_document(&document) | 86 : m_document(&document) |
| 85 { | 87 { |
| 86 } | 88 } |
| 87 | 89 |
| 88 DEFINE_TRACE(RootScrollerController) | 90 DEFINE_TRACE(RootScrollerController) |
| 89 { | 91 { |
| 90 visitor->trace(m_document); | 92 visitor->trace(m_document); |
| 91 visitor->trace(m_viewportApplyScroll); | 93 visitor->trace(m_viewportApplyScroll); |
| 92 visitor->trace(m_rootScroller); | 94 visitor->trace(m_rootScroller); |
| 93 visitor->trace(m_effectiveRootScroller); | 95 visitor->trace(m_effectiveRootScroller); |
| 96 visitor->trace(m_currentViewportApplyScrollHost); |
| 94 } | 97 } |
| 95 | 98 |
| 96 void RootScrollerController::set(Element* newRootScroller) | 99 void RootScrollerController::set(Element* newRootScroller) |
| 97 { | 100 { |
| 98 m_rootScroller = newRootScroller; | 101 m_rootScroller = newRootScroller; |
| 99 updateEffectiveRootScroller(); | 102 updateEffectiveRootScroller(); |
| 100 } | 103 } |
| 101 | 104 |
| 102 Element* RootScrollerController::get() const | 105 Element* RootScrollerController::get() const |
| 103 { | 106 { |
| 104 return m_rootScroller; | 107 return m_rootScroller; |
| 105 } | 108 } |
| 106 | 109 |
| 107 Element* RootScrollerController::effectiveRootScroller() const | 110 Element* RootScrollerController::effectiveRootScroller() const |
| 108 { | 111 { |
| 109 return m_effectiveRootScroller; | 112 return m_effectiveRootScroller; |
| 110 } | 113 } |
| 111 | 114 |
| 112 void RootScrollerController::didUpdateLayout() | 115 void RootScrollerController::didUpdateLayout() |
| 113 { | 116 { |
| 114 updateEffectiveRootScroller(); | 117 updateEffectiveRootScroller(); |
| 115 } | 118 } |
| 116 | 119 |
| 117 void RootScrollerController::setViewportScrollCallback(ViewportScrollCallback* c
allback) | |
| 118 { | |
| 119 m_viewportApplyScroll = callback; | |
| 120 moveViewportApplyScroll(m_effectiveRootScroller); | |
| 121 } | |
| 122 | |
| 123 void RootScrollerController::updateEffectiveRootScroller() | 120 void RootScrollerController::updateEffectiveRootScroller() |
| 124 { | 121 { |
| 125 bool rootScrollerValid = | 122 bool rootScrollerValid = |
| 126 m_rootScroller && isValidRootScroller(*m_rootScroller); | 123 m_rootScroller && isValidRootScroller(*m_rootScroller); |
| 127 | 124 |
| 128 Element* newEffectiveRootScroller = rootScrollerValid | 125 Element* newEffectiveRootScroller = rootScrollerValid |
| 129 ? m_rootScroller.get() | 126 ? m_rootScroller.get() |
| 130 : defaultEffectiveRootScroller(); | 127 : defaultEffectiveRootScroller(); |
| 131 | 128 |
| 132 if (m_effectiveRootScroller == newEffectiveRootScroller) | 129 if (m_effectiveRootScroller == newEffectiveRootScroller) |
| 133 return; | 130 return; |
| 134 | 131 |
| 135 if (moveViewportApplyScroll(newEffectiveRootScroller)) | 132 m_effectiveRootScroller = newEffectiveRootScroller; |
| 136 m_effectiveRootScroller = newEffectiveRootScroller; | 133 |
| 134 if (m_document->isInMainFrame()) |
| 135 setViewportApplyScrollOnRootScroller(); |
| 137 } | 136 } |
| 138 | 137 |
| 139 bool RootScrollerController::moveViewportApplyScroll(Element* target) | 138 void RootScrollerController::setViewportApplyScrollOnRootScroller() |
| 140 { | 139 { |
| 141 if (!m_viewportApplyScroll || !target) | 140 DCHECK(m_document->isInMainFrame()); |
| 142 return false; | |
| 143 | 141 |
| 144 ScrollableArea* targetScroller = scrollableAreaFor(*target); | 142 if (!m_viewportApplyScroll || !m_effectiveRootScroller) |
| 143 return; |
| 144 |
| 145 ScrollableArea* targetScroller = |
| 146 scrollableAreaFor(*m_effectiveRootScroller); |
| 147 |
| 145 if (!targetScroller) | 148 if (!targetScroller) |
| 146 return false; | 149 return; |
| 147 | 150 |
| 148 if (m_effectiveRootScroller) | 151 if (m_currentViewportApplyScrollHost) |
| 149 m_effectiveRootScroller->removeApplyScroll(); | 152 m_currentViewportApplyScrollHost->removeApplyScroll(); |
| 150 | 153 |
| 151 // Use disable-native-scroll since the ViewportScrollCallback needs to | 154 // Use disable-native-scroll since the ViewportScrollCallback needs to |
| 152 // apply scroll actions both before (TopControls) and after (overscroll) | 155 // apply scroll actions both before (TopControls) and after (overscroll) |
| 153 // scrolling the element so it will apply scroll to the element itself. | 156 // scrolling the element so it will apply scroll to the element itself. |
| 154 target->setApplyScroll(m_viewportApplyScroll, "disable-native-scroll"); | 157 m_effectiveRootScroller->setApplyScroll( |
| 158 m_viewportApplyScroll, "disable-native-scroll"); |
| 159 |
| 160 m_currentViewportApplyScrollHost = m_effectiveRootScroller; |
| 155 | 161 |
| 156 // Ideally, scroll customization would pass the current element to scroll to | 162 // Ideally, scroll customization would pass the current element to scroll to |
| 157 // the apply scroll callback but this doesn't happen today so we set it | 163 // the apply scroll callback but this doesn't happen today so we set it |
| 158 // through a back door here. This is also needed by the | 164 // through a back door here. This is also needed by the |
| 159 // RootViewportScrollCallback to swap the target into the layout viewport | 165 // ViewportScrollCallback to swap the target into the layout viewport |
| 160 // in RootFrameViewport. | 166 // in RootFrameViewport. |
| 161 m_viewportApplyScroll->setScroller(targetScroller); | 167 m_viewportApplyScroll->setScroller(targetScroller); |
| 162 | |
| 163 return true; | |
| 164 } | 168 } |
| 165 | 169 |
| 166 void RootScrollerController::didUpdateCompositing() | 170 void RootScrollerController::didUpdateCompositing() |
| 167 { | 171 { |
| 168 FrameHost& frameHost = *m_document->frameHost(); | 172 FrameHost* frameHost = m_document->frameHost(); |
| 169 | 173 |
| 170 // Let the compositor-side counterpart know about this change. | 174 // Let the compositor-side counterpart know about this change. |
| 171 if (m_document->isInMainFrame()) | 175 if (frameHost && m_document->isInMainFrame()) |
| 172 frameHost.chromeClient().registerViewportLayers(); | 176 frameHost->chromeClient().registerViewportLayers(); |
| 177 } |
| 178 |
| 179 void RootScrollerController::didAttachDocument() |
| 180 { |
| 181 if (!m_document->isInMainFrame()) |
| 182 return; |
| 183 |
| 184 FrameHost* frameHost = m_document->frameHost(); |
| 185 FrameView* frameView = m_document->view(); |
| 186 |
| 187 if (!frameHost || !frameView) |
| 188 return; |
| 189 |
| 190 RootFrameViewport* rootFrameViewport = frameView->getRootFrameViewport(); |
| 191 DCHECK(rootFrameViewport); |
| 192 |
| 193 m_viewportApplyScroll = ViewportScrollCallback::create( |
| 194 &frameHost->topControls(), |
| 195 &frameHost->overscrollController(), |
| 196 *rootFrameViewport); |
| 197 |
| 198 updateEffectiveRootScroller(); |
| 173 } | 199 } |
| 174 | 200 |
| 175 GraphicsLayer* RootScrollerController::rootScrollerLayer() | 201 GraphicsLayer* RootScrollerController::rootScrollerLayer() |
| 176 { | 202 { |
| 177 if (!m_effectiveRootScroller) | 203 if (!m_effectiveRootScroller) |
| 178 return nullptr; | 204 return nullptr; |
| 179 | 205 |
| 180 ScrollableArea* area = scrollableAreaFor(*m_effectiveRootScroller); | 206 ScrollableArea* area = scrollableAreaFor(*m_effectiveRootScroller); |
| 181 | 207 |
| 182 if (!area) | 208 if (!area) |
| 183 return nullptr; | 209 return nullptr; |
| 184 | 210 |
| 185 GraphicsLayer* graphicsLayer = area->layerForScrolling(); | 211 GraphicsLayer* graphicsLayer = area->layerForScrolling(); |
| 186 | 212 |
| 187 // TODO(bokan): We should assert graphicsLayer here and | 213 // TODO(bokan): We should assert graphicsLayer here and |
| 188 // RootScrollerController should do whatever needs to happen to ensure | 214 // RootScrollerController should do whatever needs to happen to ensure |
| 189 // the root scroller gets composited. | 215 // the root scroller gets composited. |
| 190 | 216 |
| 191 return graphicsLayer; | 217 return graphicsLayer; |
| 192 } | 218 } |
| 193 | 219 |
| 220 bool RootScrollerController::isViewportScrollCallback( |
| 221 const ScrollStateCallback* callback) const |
| 222 { |
| 223 if (!callback) |
| 224 return false; |
| 225 |
| 226 return callback == m_viewportApplyScroll.get(); |
| 227 } |
| 228 |
| 194 Element* RootScrollerController::defaultEffectiveRootScroller() | 229 Element* RootScrollerController::defaultEffectiveRootScroller() |
| 195 { | 230 { |
| 196 DCHECK(m_document); | 231 DCHECK(m_document); |
| 197 return m_document->documentElement(); | 232 return m_document->documentElement(); |
| 198 } | 233 } |
| 199 | 234 |
| 200 } // namespace blink | 235 } // namespace blink |
| OLD | NEW |