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/frame/ScrollAndScaleEmulator.h" |
| 6 |
| 7 #include "core/frame/Frame.h" |
| 8 #include "core/frame/FrameHost.h" |
| 9 #include "core/frame/FrameView.h" |
| 10 #include "core/frame/PageScaleConstraintsSet.h" |
| 11 #include "core/frame/Settings.h" |
| 12 #include "core/frame/VisualViewport.h" |
| 13 #include "core/layout/compositing/PaintLayerCompositor.h" |
| 14 #include "core/page/Page.h" |
| 15 #include "core/page/scrolling/ScrollingCoordinator.h" |
| 16 #include "platform/geometry/FloatSize.h" |
| 17 |
| 18 namespace blink { |
| 19 |
| 20 // static |
| 21 ScrollAndScaleEmulator* ScrollAndScaleEmulator::create(FrameHost* frameHost) |
| 22 { |
| 23 DCHECK(frameHost->page().mainFrame()); |
| 24 DCHECK(frameHost->page().mainFrame()->isLocalFrame()); |
| 25 |
| 26 return new ScrollAndScaleEmulator(frameHost); |
| 27 } |
| 28 |
| 29 DEFINE_TRACE(ScrollAndScaleEmulator) |
| 30 { |
| 31 visitor->trace(m_frameHost); |
| 32 } |
| 33 |
| 34 void ScrollAndScaleEmulator::update(const IntPoint& framePosition, const DoubleP
oint& visualViewportPosition, float pageScale) |
| 35 { |
| 36 bool changed = false; |
| 37 |
| 38 if (m_framePosition != framePosition) { |
| 39 m_framePosition = framePosition; |
| 40 changed = true; |
| 41 } |
| 42 |
| 43 if (m_visualViewportPosition != visualViewportPosition) { |
| 44 m_visualViewportPosition = visualViewportPosition; |
| 45 changed = true; |
| 46 } |
| 47 |
| 48 if (m_pageScale != pageScale) { |
| 49 m_pageScale = pageScale; |
| 50 changed = true; |
| 51 } |
| 52 |
| 53 if (changed) { |
| 54 if (FrameView* view = m_frameHost->page().deprecatedLocalMainFrame()->vi
ew()) |
| 55 view->setScrollAndScaleEmulator(this); |
| 56 m_frameHost->setUserAgentPageScaleConstraints(pageScaleConstraints()); |
| 57 m_frameHost->visualViewport().setScrollAndScaleEmulator(this); |
| 58 } |
| 59 } |
| 60 |
| 61 void ScrollAndScaleEmulator::clear() |
| 62 { |
| 63 setThreadedScrollingEnabled(m_originalThreadedScrollingEnabled); |
| 64 if (FrameView* view = m_frameHost->page().deprecatedLocalMainFrame()->view()
) |
| 65 view->setScrollAndScaleEmulator(nullptr); |
| 66 m_frameHost->setUserAgentPageScaleConstraints(m_originalPageScaleConstraints
); |
| 67 m_frameHost->visualViewport().setScrollAndScaleEmulator(nullptr); |
| 68 |
| 69 m_framePosition = IntPoint(-1, -1); |
| 70 m_visualViewportPosition = DoublePoint(-1, -1); |
| 71 m_pageScale = 0; |
| 72 m_frameHost = nullptr; |
| 73 } |
| 74 |
| 75 IntPoint ScrollAndScaleEmulator::applyFramePositionOverride(const IntPoint& posi
tion) |
| 76 { |
| 77 return applyPositionOverride(position, m_framePosition); |
| 78 } |
| 79 |
| 80 DoublePoint ScrollAndScaleEmulator::applyVisualViewportPositionOverride(const Do
ublePoint& position) |
| 81 { |
| 82 return applyPositionOverride(position, m_visualViewportPosition); |
| 83 } |
| 84 |
| 85 IntSize ScrollAndScaleEmulator::mainFrameSize(const IntSize& webViewSize) |
| 86 { |
| 87 // We may have overridden the minimum scale in the user-agent constraints, |
| 88 // so reconstruct the original constraints before computing the frame size. |
| 89 PageScaleConstraintsSet constraints = m_frameHost->pageScaleConstraintsSet()
; |
| 90 constraints.setUserAgentConstraints(m_originalPageScaleConstraints); |
| 91 constraints.computeFinalConstraints(); |
| 92 |
| 93 FloatSize frameSize(webViewSize); |
| 94 frameSize.scale(1 / constraints.finalConstraints().minimumScale); |
| 95 return expandedIntSize(frameSize); |
| 96 } |
| 97 |
| 98 ScrollAndScaleEmulator::ScrollAndScaleEmulator(FrameHost* frameHost) |
| 99 : m_framePosition(IntPoint(-1, -1)) |
| 100 , m_visualViewportPosition(DoublePoint(-1, -1)) |
| 101 , m_pageScale(0) |
| 102 , m_frameHost(frameHost) |
| 103 , m_originalThreadedScrollingEnabled(frameHost->settings().threadedScrolling
Enabled()) |
| 104 , m_originalPageScaleConstraints(frameHost->pageScaleConstraintsSet().userAg
entConstraints()) |
| 105 { |
| 106 // Force main thread scrolling, because we do not apply scroll position |
| 107 // overrides in the compositor thread. |
| 108 // TODO(eseckler): This does not prevent scroll position changes in the |
| 109 // context of pinch gestures on the compositor thread (crbug.com/626277). |
| 110 setThreadedScrollingEnabled(false); |
| 111 } |
| 112 |
| 113 void ScrollAndScaleEmulator::setThreadedScrollingEnabled(bool enabled) |
| 114 { |
| 115 m_frameHost->settings().setThreadedScrollingEnabled(enabled); |
| 116 |
| 117 // Ensure that the setting is propagated through ScrollingCoordinator::mainT
hreadScrollingReasons(). |
| 118 m_frameHost->page().scrollingCoordinator()->notifyGeometryChanged(); |
| 119 |
| 120 FrameView* view = m_frameHost->page().deprecatedLocalMainFrame()->view(); |
| 121 if (PaintLayerCompositor* compositor = |
| 122 (view && !view->layoutViewItem().isNull()) |
| 123 ? view->layoutViewItem().compositor() |
| 124 : nullptr) |
| 125 compositor->setNeedsCompositingUpdate(CompositingUpdateAfterCompositingI
nputChange); |
| 126 } |
| 127 |
| 128 PageScaleConstraints ScrollAndScaleEmulator::pageScaleConstraints() |
| 129 { |
| 130 if (m_pageScale > 0) |
| 131 return PageScaleConstraints(m_pageScale, m_pageScale, m_pageScale); |
| 132 return m_originalPageScaleConstraints; |
| 133 } |
| 134 |
| 135 template <typename Point> |
| 136 Point ScrollAndScaleEmulator::applyPositionOverride(const Point& position, const
Point& override) |
| 137 { |
| 138 Point newPosition = position; |
| 139 if (override.x() >= 0) |
| 140 newPosition.setX(override.x()); |
| 141 if (override.y() >= 0) |
| 142 newPosition.setY(override.y()); |
| 143 return newPosition; |
| 144 } |
| 145 |
| 146 } // namespace blink |
OLD | NEW |