Index: third_party/WebKit/Source/core/frame/ScrollAndScaleEmulator.cpp |
diff --git a/third_party/WebKit/Source/core/frame/ScrollAndScaleEmulator.cpp b/third_party/WebKit/Source/core/frame/ScrollAndScaleEmulator.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..fee87f2f2422b2efe54a7f274a4d2160b9da9f6a |
--- /dev/null |
+++ b/third_party/WebKit/Source/core/frame/ScrollAndScaleEmulator.cpp |
@@ -0,0 +1,146 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "core/frame/ScrollAndScaleEmulator.h" |
+ |
+#include "core/frame/Frame.h" |
+#include "core/frame/FrameHost.h" |
+#include "core/frame/FrameView.h" |
+#include "core/frame/PageScaleConstraintsSet.h" |
+#include "core/frame/Settings.h" |
+#include "core/frame/VisualViewport.h" |
+#include "core/layout/compositing/PaintLayerCompositor.h" |
+#include "core/page/Page.h" |
+#include "core/page/scrolling/ScrollingCoordinator.h" |
+#include "platform/geometry/FloatSize.h" |
+ |
+namespace blink { |
+ |
+// static |
+ScrollAndScaleEmulator* ScrollAndScaleEmulator::create(FrameHost* frameHost) |
+{ |
+ DCHECK(frameHost->page().mainFrame()); |
+ DCHECK(frameHost->page().mainFrame()->isLocalFrame()); |
+ |
+ return new ScrollAndScaleEmulator(frameHost); |
+} |
+ |
+DEFINE_TRACE(ScrollAndScaleEmulator) |
+{ |
+ visitor->trace(m_frameHost); |
+} |
+ |
+void ScrollAndScaleEmulator::update(const IntPoint& framePosition, const DoublePoint& visualViewportPosition, float pageScale) |
+{ |
+ bool changed = false; |
+ |
+ if (m_framePosition != framePosition) { |
+ m_framePosition = framePosition; |
+ changed = true; |
+ } |
+ |
+ if (m_visualViewportPosition != visualViewportPosition) { |
+ m_visualViewportPosition = visualViewportPosition; |
+ changed = true; |
+ } |
+ |
+ if (m_pageScale != pageScale) { |
+ m_pageScale = pageScale; |
+ changed = true; |
+ } |
+ |
+ if (changed) { |
+ if (FrameView* view = m_frameHost->page().deprecatedLocalMainFrame()->view()) |
+ view->setScrollAndScaleEmulator(this); |
+ m_frameHost->setUserAgentPageScaleConstraints(pageScaleConstraints()); |
+ m_frameHost->visualViewport().setScrollAndScaleEmulator(this); |
+ } |
+} |
+ |
+void ScrollAndScaleEmulator::clear() |
+{ |
+ setThreadedScrollingEnabled(m_originalThreadedScrollingEnabled); |
+ if (FrameView* view = m_frameHost->page().deprecatedLocalMainFrame()->view()) |
+ view->setScrollAndScaleEmulator(nullptr); |
+ m_frameHost->setUserAgentPageScaleConstraints(m_originalPageScaleConstraints); |
+ m_frameHost->visualViewport().setScrollAndScaleEmulator(nullptr); |
+ |
+ m_framePosition = IntPoint(-1, -1); |
+ m_visualViewportPosition = DoublePoint(-1, -1); |
+ m_pageScale = 0; |
+ m_frameHost = nullptr; |
+} |
+ |
+IntPoint ScrollAndScaleEmulator::applyFramePositionOverride(const IntPoint& position) |
+{ |
+ return applyPositionOverride(position, m_framePosition); |
+} |
+ |
+DoublePoint ScrollAndScaleEmulator::applyVisualViewportPositionOverride(const DoublePoint& position) |
+{ |
+ return applyPositionOverride(position, m_visualViewportPosition); |
+} |
+ |
+IntSize ScrollAndScaleEmulator::mainFrameSize(const IntSize& webViewSize) |
+{ |
+ // We may have overridden the minimum scale in the user-agent constraints, |
+ // so reconstruct the original constraints before computing the frame size. |
+ PageScaleConstraintsSet constraints = m_frameHost->pageScaleConstraintsSet(); |
+ constraints.setUserAgentConstraints(m_originalPageScaleConstraints); |
+ constraints.computeFinalConstraints(); |
+ |
+ FloatSize frameSize(webViewSize); |
+ frameSize.scale(1 / constraints.finalConstraints().minimumScale); |
+ return expandedIntSize(frameSize); |
+} |
+ |
+ScrollAndScaleEmulator::ScrollAndScaleEmulator(FrameHost* frameHost) |
+ : m_framePosition(IntPoint(-1, -1)) |
+ , m_visualViewportPosition(DoublePoint(-1, -1)) |
+ , m_pageScale(0) |
+ , m_frameHost(frameHost) |
+ , m_originalThreadedScrollingEnabled(frameHost->settings().threadedScrollingEnabled()) |
+ , m_originalPageScaleConstraints(frameHost->pageScaleConstraintsSet().userAgentConstraints()) |
+{ |
+ // Force main thread scrolling, because we do not apply scroll position |
+ // overrides in the compositor thread. |
+ // TODO(eseckler): This does not prevent scroll position changes in the |
+ // context of pinch gestures on the compositor thread (crbug.com/626277). |
+ setThreadedScrollingEnabled(false); |
+} |
+ |
+void ScrollAndScaleEmulator::setThreadedScrollingEnabled(bool enabled) |
+{ |
+ m_frameHost->settings().setThreadedScrollingEnabled(enabled); |
+ |
+ // Ensure that the setting is propagated through ScrollingCoordinator::mainThreadScrollingReasons(). |
+ m_frameHost->page().scrollingCoordinator()->notifyGeometryChanged(); |
+ |
+ FrameView* view = m_frameHost->page().deprecatedLocalMainFrame()->view(); |
+ if (PaintLayerCompositor* compositor = |
+ (view && !view->layoutViewItem().isNull()) |
+ ? view->layoutViewItem().compositor() |
+ : nullptr) |
+ compositor->setNeedsCompositingUpdate(CompositingUpdateAfterCompositingInputChange); |
+} |
+ |
+PageScaleConstraints ScrollAndScaleEmulator::pageScaleConstraints() |
+{ |
+ if (m_pageScale > 0) |
+ return PageScaleConstraints(m_pageScale, m_pageScale, m_pageScale); |
+ return m_originalPageScaleConstraints; |
+} |
+ |
+template <typename Point> |
+Point ScrollAndScaleEmulator::applyPositionOverride(const Point& position, const Point& override) |
+{ |
+ Point newPosition = position; |
+ if (override.x() >= 0) |
+ newPosition.setX(override.x()); |
+ if (override.y() >= 0) |
+ newPosition.setY(override.y()); |
+ return newPosition; |
+} |
+ |
+} // namespace blink |