2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * (C) 2001 Dirk Mueller (mueller@kde.org)
5 * (C) 2006 Alexey Proskuryakov (ap@webkit.org)
6 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2011 Apple Inc. All rights reserved.
7 * Copyright (C) 2008 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
8 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
9 * Copyright (C) 2012-2013 Intel Corporation. All rights reserved.
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Library General Public
13 * License as published by the Free Software Foundation; either
14 * version 2 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Library General Public License for more details.
21 * You should have received a copy of the GNU Library General Public License
22 * along with this library; see the file COPYING.LIB. If not, write to
23 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 * Boston, MA 02110-1301, USA.
29 #include "core/dom/ViewportDescription.h"
31 #include "core/dom/Document.h"
32 #include "core/frame/FrameHost.h"
33 #include "core/frame/FrameView.h"
34 #include "core/frame/LocalFrame.h"
35 #include "core/frame/Settings.h"
36 #include "platform/weborigin/KURL.h"
37 #include "public/platform/Platform.h"
41 static const float& compareIgnoringAuto(const float& value1, const float& value2, const float& (*compare) (const float&, const float&))
43 if (value1 == ViewportDescription::ValueAuto)
46 if (value2 == ViewportDescription::ValueAuto)
49 return compare(value1, value2);
52 float ViewportDescription::resolveViewportLength(const Length& length, const FloatSize& initialViewportSize, Direction direction)
55 return ViewportDescription::ValueAuto;
58 return length.getFloatValue();
60 if (length.type() == ExtendToZoom)
61 return ViewportDescription::ValueExtendToZoom;
63 if (length.type() == Percent && direction == Horizontal)
64 return initialViewportSize.width() * length.getFloatValue() / 100.0f;
66 if (length.type() == Percent && direction == Vertical)
67 return initialViewportSize.height() * length.getFloatValue() / 100.0f;
69 if (length.type() == DeviceWidth)
70 return initialViewportSize.width();
72 if (length.type() == DeviceHeight)
73 return initialViewportSize.height();
76 return ViewportDescription::ValueAuto;
79 PageScaleConstraints ViewportDescription::resolve(const FloatSize& initialViewportSize, Length legacyFallbackWidth) const
81 float resultWidth = ValueAuto;
83 Length copyMaxWidth = maxWidth;
84 Length copyMinWidth = minWidth;
85 // In case the width (used for min- and max-width) is undefined.
86 if (isLegacyViewportType() && maxWidth.isAuto()) {
87 // The width viewport META property is translated into 'width' descriptors, setting
88 // the 'min' value to 'extend-to-zoom' and the 'max' value to the intended length.
89 // In case the UA-defines a min-width, use that as length.
90 if (zoom == ViewportDescription::ValueAuto) {
91 copyMinWidth = Length(ExtendToZoom);
92 copyMaxWidth = legacyFallbackWidth;
93 } else if (maxHeight.isAuto()) {
94 copyMinWidth = Length(ExtendToZoom);
95 copyMaxWidth = Length(ExtendToZoom);
99 float resultMaxWidth = resolveViewportLength(copyMaxWidth, initialViewportSize, Horizontal);
100 float resultMinWidth = resolveViewportLength(copyMinWidth, initialViewportSize, Horizontal);
102 float resultHeight = ValueAuto;
103 float resultMaxHeight = resolveViewportLength(maxHeight, initialViewportSize, Vertical);
104 float resultMinHeight = resolveViewportLength(minHeight, initialViewportSize, Vertical);
106 float resultZoom = zoom;
107 float resultMinZoom = minZoom;
108 float resultMaxZoom = maxZoom;
109 bool resultUserZoom = userZoom;
111 // 1. Resolve min-zoom and max-zoom values.
112 if (resultMinZoom != ViewportDescription::ValueAuto && resultMaxZoom != ViewportDescription::ValueAuto)
113 resultMaxZoom = std::max(resultMinZoom, resultMaxZoom);
115 // 2. Constrain zoom value to the [min-zoom, max-zoom] range.
116 if (resultZoom != ViewportDescription::ValueAuto)
117 resultZoom = compareIgnoringAuto(resultMinZoom, compareIgnoringAuto(resultMaxZoom, resultZoom, std::min), std::max);
119 float extendZoom = compareIgnoringAuto(resultZoom, resultMaxZoom, std::min);
121 // 3. Resolve non-"auto" lengths to pixel lengths.
122 if (extendZoom == ViewportDescription::ValueAuto) {
123 if (resultMaxWidth == ViewportDescription::ValueExtendToZoom)
124 resultMaxWidth = ViewportDescription::ValueAuto;
126 if (resultMaxHeight == ViewportDescription::ValueExtendToZoom)
127 resultMaxHeight = ViewportDescription::ValueAuto;
129 if (resultMinWidth == ViewportDescription::ValueExtendToZoom)
130 resultMinWidth = resultMaxWidth;
132 if (resultMinHeight == ViewportDescription::ValueExtendToZoom)
133 resultMinHeight = resultMaxHeight;
135 float extendWidth = initialViewportSize.width() / extendZoom;
136 float extendHeight = initialViewportSize.height() / extendZoom;
138 if (resultMaxWidth == ViewportDescription::ValueExtendToZoom)
139 resultMaxWidth = extendWidth;
141 if (resultMaxHeight == ViewportDescription::ValueExtendToZoom)
142 resultMaxHeight = extendHeight;
144 if (resultMinWidth == ViewportDescription::ValueExtendToZoom)
145 resultMinWidth = compareIgnoringAuto(extendWidth, resultMaxWidth, std::max);
147 if (resultMinHeight == ViewportDescription::ValueExtendToZoom)
148 resultMinHeight = compareIgnoringAuto(extendHeight, resultMaxHeight, std::max);
151 // 4. Resolve initial width from min/max descriptors.
152 if (resultMinWidth != ViewportDescription::ValueAuto || resultMaxWidth != ViewportDescription::ValueAuto)
153 resultWidth = compareIgnoringAuto(resultMinWidth, compareIgnoringAuto(resultMaxWidth, initialViewportSize.width(), std::min), std::max);
155 // 5. Resolve initial height from min/max descriptors.
156 if (resultMinHeight != ViewportDescription::ValueAuto || resultMaxHeight != ViewportDescription::ValueAuto)
157 resultHeight = compareIgnoringAuto(resultMinHeight, compareIgnoringAuto(resultMaxHeight, initialViewportSize.height(), std::min), std::max);
159 // 6-7. Resolve width value.
160 if (resultWidth == ViewportDescription::ValueAuto) {
161 if (resultHeight == ViewportDescription::ValueAuto || !initialViewportSize.height())
162 resultWidth = initialViewportSize.width();
164 resultWidth = resultHeight * (initialViewportSize.width() / initialViewportSize.height());
167 // 8. Resolve height value.
168 if (resultHeight == ViewportDescription::ValueAuto) {
169 if (!initialViewportSize.width())
170 resultHeight = initialViewportSize.height();
172 resultHeight = resultWidth * initialViewportSize.height() / initialViewportSize.width();
175 // Resolve initial-scale value.
176 if (resultZoom == ViewportDescription::ValueAuto) {
177 if (resultWidth != ViewportDescription::ValueAuto && resultWidth > 0)
178 resultZoom = initialViewportSize.width() / resultWidth;
179 if (resultHeight != ViewportDescription::ValueAuto && resultHeight > 0) {
180 // if 'auto', the initial-scale will be negative here and thus ignored.
181 resultZoom = std::max<float>(resultZoom, initialViewportSize.height() / resultHeight);
185 // If user-scalable = no, lock the min/max scale to the computed initial
188 resultMinZoom = resultMaxZoom = resultZoom;
190 // Only set initialScale to a value if it was explicitly set.
191 if (zoom == ViewportDescription::ValueAuto)
192 resultZoom = ViewportDescription::ValueAuto;
194 PageScaleConstraints result;
195 result.minimumScale = resultMinZoom;
196 result.maximumScale = resultMaxZoom;
197 result.initialScale = resultZoom;
198 result.layoutSize.setWidth(resultWidth);
199 result.layoutSize.setHeight(resultHeight);
203 void ViewportDescription::reportMobilePageStats(const LocalFrame* mainFrame) const
206 enum ViewportUMAType {
211 MetaHandheldFriendly,
217 if (!mainFrame || !mainFrame->host() || !mainFrame->view() || !mainFrame->document())
220 // Avoid chrome:// pages like the new-tab page (on Android new tab is non-http).
221 if (!mainFrame->document()->url().protocolIsInHTTPFamily())
224 if (!isSpecifiedByAuthor()) {
225 if (mainFrame->document()->isMobileDocument())
226 Platform::current()->histogramEnumeration("Viewport.MetaTagType", XhtmlMobileProfile, TypeCount);
228 Platform::current()->histogramEnumeration("Viewport.MetaTagType", NoViewportTag, TypeCount);
233 if (isMetaViewportType()) {
234 if (maxWidth.type() == blink::Fixed) {
235 Platform::current()->histogramEnumeration("Viewport.MetaTagType", ConstantWidth, TypeCount);
237 if (mainFrame->view()) {
238 // To get an idea of how "far" the viewport is from the device's ideal width, we
239 // report the zoom level that we'd need to be at for the entire page to be visible.
240 int viewportWidth = maxWidth.intValue();
241 int windowWidth = mainFrame->document()->settings()->pinchVirtualViewportEnabled()
242 ? mainFrame->host()->pinchViewport().size().width()
243 : mainFrame->view()->frameRect().width();
244 int overviewZoomPercent = 100 * windowWidth / static_cast<float>(viewportWidth);
245 Platform::current()->histogramSparse("Viewport.OverviewZoom", overviewZoomPercent);
248 } else if (maxWidth.type() == blink::DeviceWidth || maxWidth.type() == blink::ExtendToZoom) {
249 Platform::current()->histogramEnumeration("Viewport.MetaTagType", DeviceWidth, TypeCount);
251 // Overflow bucket for cases we may be unaware of.
252 Platform::current()->histogramEnumeration("Viewport.MetaTagType", MetaWidthOther, TypeCount);
254 } else if (type == ViewportDescription::HandheldFriendlyMeta) {
255 Platform::current()->histogramEnumeration("Viewport.MetaTagType", MetaHandheldFriendly, TypeCount);
256 } else if (type == ViewportDescription::MobileOptimizedMeta) {
257 Platform::current()->histogramEnumeration("Viewport.MetaTagType", MobileOptimizedMeta, TypeCount);