OLD | NEW |
(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/page/scrolling/RootScroller.h" |
| 6 |
| 7 #include "core/dom/Element.h" |
| 8 #include "core/frame/FrameHost.h" |
| 9 #include "core/frame/FrameView.h" |
| 10 #include "core/layout/LayoutBox.h" |
| 11 #include "core/page/Page.h" |
| 12 #include "core/page/scrolling/ViewportScrollCallback.h" |
| 13 #include "core/paint/PaintLayerScrollableArea.h" |
| 14 #include "platform/scroll/ScrollableArea.h" |
| 15 |
| 16 namespace blink { |
| 17 |
| 18 namespace { |
| 19 |
| 20 Document* topDocument(FrameHost* frameHost) |
| 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 { |
| 32 if (!element.layoutObject() || !element.layoutObject()->isBox()) |
| 33 return nullptr; |
| 34 |
| 35 LayoutBox* box = toLayoutBox(element.layoutObject()); |
| 36 |
| 37 if (box->isDocumentElement() && element.document().view()) |
| 38 return element.document().view()->getScrollableArea(); |
| 39 |
| 40 return static_cast<PaintInvalidationCapableScrollableArea*>( |
| 41 box->getScrollableArea()); |
| 42 } |
| 43 |
| 44 } // namespace |
| 45 |
| 46 RootScroller::RootScroller(FrameHost& frameHost) |
| 47 : m_frameHost(&frameHost) |
| 48 { |
| 49 } |
| 50 |
| 51 DEFINE_TRACE(RootScroller) |
| 52 { |
| 53 visitor->trace(m_frameHost); |
| 54 visitor->trace(m_viewportApplyScroll); |
| 55 visitor->trace(m_rootScroller); |
| 56 } |
| 57 |
| 58 bool RootScroller::set(Element& newRootScroller) |
| 59 { |
| 60 if (!isValid(newRootScroller)) |
| 61 return false; |
| 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 } |
| 97 |
| 98 Element* RootScroller::get() const |
| 99 { |
| 100 if (!m_frameHost) |
| 101 return nullptr; |
| 102 |
| 103 return m_rootScroller; |
| 104 } |
| 105 |
| 106 void RootScroller::resetToDefault() |
| 107 { |
| 108 if (!m_frameHost) |
| 109 return; |
| 110 |
| 111 Document* document = topDocument(m_frameHost); |
| 112 if (!document) |
| 113 return; |
| 114 |
| 115 if (Element* defaultRootElement = document->documentElement()) |
| 116 set(*defaultRootElement); |
| 117 } |
| 118 |
| 119 void RootScroller::didUpdateTopDocumentLayout() |
| 120 { |
| 121 if (m_rootScroller && isValid(*m_rootScroller)) |
| 122 return; |
| 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 } |
| 158 |
| 159 } // namespace blink |
OLD | NEW |