Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(135)

Unified Diff: Source/WebKit/chromium/src/ViewportAttributesManager.cpp

Issue 14813025: Refactor viewport initialization logic out of WebViewImpl. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Address code review comments Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: Source/WebKit/chromium/src/ViewportAttributesManager.cpp
diff --git a/Source/WebKit/chromium/src/ViewportAttributesManager.cpp b/Source/WebKit/chromium/src/ViewportAttributesManager.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..57a3cde68d9be56a5c4b58c28dc03a6363e36aab
--- /dev/null
+++ b/Source/WebKit/chromium/src/ViewportAttributesManager.cpp
@@ -0,0 +1,163 @@
+/*
+ * Copyright (C) 2013 Google Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include "ViewportAttributesManager.h"
+
+using namespace WebCore;
+
+namespace WebKit {
+
+static const float defaultMinimumScale = 0.25f;
+static const float defaultMaximumScale = 5.0f;
+
+ViewportAttributesManager::ViewportAttributesManager()
+ : m_lastContentsWidth(0)
+ , m_needsReset(false)
+ , m_constraintsDirty(false)
+{
+ m_finalConstraints = defaultConstraints();
+}
+
+ViewportAttributes ViewportAttributesManager::defaultConstraints() const
+{
+ return ViewportAttributes(-1, defaultMinimumScale, defaultMaximumScale);
+}
+
+void ViewportAttributesManager::updatePageDefinedConstraints(const ViewportArguments& arguments, IntSize viewSize, int layoutFallbackWidth)
+{
+ m_pageDefinedConstraints = arguments.resolve(viewSize, viewSize, layoutFallbackWidth);
+
+ // Only set initialScale to a value if it was explicitly set.
+ if (arguments.zoom == ViewportArguments::ValueAuto)
+ m_pageDefinedConstraints.initialScale = -1;
+
+ m_constraintsDirty = true;
+}
+
+void ViewportAttributesManager::setUserAgentConstraints(ViewportAttributes userAgentConstraints)
+{
+ m_userAgentConstraints = userAgentConstraints;
+ m_constraintsDirty = true;
+}
+
+void ViewportAttributesManager::computeFinalConstraints()
+{
+ m_finalConstraints = defaultConstraints();
+ m_finalConstraints.overrideWith(m_pageDefinedConstraints);
+ m_finalConstraints.overrideWith(m_userAgentConstraints);
+
+ m_constraintsDirty = false;
+}
+
+void ViewportAttributesManager::adjustFinalConstraintsToContentsSize(IntSize viewSize, IntSize contentsSize, int nonOverlayScrollbarWidth)
+{
+ if (!viewSize.width() || !contentsSize.width())
+ return;
+
+ // Clamp the minimum scale so that the viewport can't exceed the document
+ // width.
+ float viewWidthNotIncludingScrollbars = viewSize.width() - nonOverlayScrollbarWidth;
+ m_finalConstraints.minimumScale = std::max(m_finalConstraints.minimumScale, viewWidthNotIncludingScrollbars / contentsSize.width());
+
+ // If the initial scale wasn't defined by any of the attributes, set
+ // it to minimum scale now that we know the real value.
+ if (m_finalConstraints.initialScale == -1)
+ m_finalConstraints.initialScale = m_finalConstraints.minimumScale;
+}
+
+void ViewportAttributesManager::setNeedsReset(bool needsReset)
+{
+ m_needsReset = needsReset;
+ if (needsReset)
+ m_constraintsDirty = true;
+}
+
+void ViewportAttributesManager::didChangeContentsSize(IntSize contentsSize, float pageScaleFactor)
+{
+ // If a large fixed-width element expanded the size of the document
+ // late in loading and our initial scale is not constrained, reset the
+ // page scale factor to the new minimum scale.
+ if (contentsSize.width() > m_lastContentsWidth
+ && pageScaleFactor == finalConstraints().minimumScale
+ && userAgentConstraints().initialScale == -1 && pageDefinedConstraints().initialScale == -1)
+ setNeedsReset(true);
+
+ m_constraintsDirty = true;
+ m_lastContentsWidth = contentsSize.width();
+}
+
+static float computeDeprecatedTargetDensityDPIFactor(const ViewportArguments& arguments, float deviceScaleFactor)
+{
+ if (arguments.deprecatedTargetDensityDPI == ViewportArguments::ValueDeviceDPI)
+ return 1.0f / deviceScaleFactor;
+
+ float targetDPI = -1.0f;
+ if (arguments.deprecatedTargetDensityDPI == ViewportArguments::ValueLowDPI)
+ targetDPI = 120.0f;
+ else if (arguments.deprecatedTargetDensityDPI == ViewportArguments::ValueMediumDPI)
+ targetDPI = 160.0f;
+ else if (arguments.deprecatedTargetDensityDPI == ViewportArguments::ValueHighDPI)
+ targetDPI = 240.0f;
+ else if (arguments.deprecatedTargetDensityDPI != ViewportArguments::ValueAuto)
+ targetDPI = arguments.deprecatedTargetDensityDPI;
+ return targetDPI > 0 ? (deviceScaleFactor * 120.0f) / targetDPI : 1.0f;
+}
+
+static float getLayoutWidthForNonWideViewport(const FloatSize& deviceSize, float initialScale)
+{
+ return initialScale == -1 ? deviceSize.width() : deviceSize.width() / initialScale;
+}
+
+void ViewportAttributesManager::adjustPageDefinedConstraintsForAndroidWebView(const ViewportArguments& arguments, IntSize viewSize, int layoutFallbackWidth, float deviceScaleFactor, bool useWideViewport, bool initializeAtMinimumPageScale)
+{
+ float initialScale = m_pageDefinedConstraints.initialScale;
+ if (arguments.zoom == -1 && !initializeAtMinimumPageScale) {
+ if (arguments.width == -1 || (useWideViewport && arguments.width != ViewportArguments::ValueDeviceWidth))
+ m_pageDefinedConstraints.initialScale = 1.0f;
+ }
+
+ float targetDensityDPIFactor = computeDeprecatedTargetDensityDPIFactor(arguments, deviceScaleFactor);
+ if (m_pageDefinedConstraints.initialScale != -1)
+ m_pageDefinedConstraints.initialScale *= targetDensityDPIFactor;
+ m_pageDefinedConstraints.minimumScale *= targetDensityDPIFactor;
+ m_pageDefinedConstraints.maximumScale *= targetDensityDPIFactor;
+
+ if (useWideViewport && arguments.width == -1 && arguments.zoom != 1.0f)
+ m_pageDefinedConstraints.layoutSize.setWidth(layoutFallbackWidth);
+ else {
+ if (!useWideViewport)
+ m_pageDefinedConstraints.layoutSize.setWidth(getLayoutWidthForNonWideViewport(viewSize, initialScale));
+ if (!useWideViewport || arguments.width == -1 || arguments.width == ViewportArguments::ValueDeviceWidth)
+ m_pageDefinedConstraints.layoutSize.scale(1.0f / targetDensityDPIFactor);
+ }
+}
+
+} // namespace WebKit

Powered by Google App Engine
This is Rietveld 408576698