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"
43 static const float& compareIgnoringAuto(const float& value1, const float& value2, const float& (*compare) (const float&, const float&))
45 if (value1 == ViewportDescription::ValueAuto)
48 if (value2 == ViewportDescription::ValueAuto)
51 return compare(value1, value2);
54 float ViewportDescription::resolveViewportLength(const Length& length, const FloatSize& initialViewportSize, Direction direction)
57 return ViewportDescription::ValueAuto;
60 return length.getFloatValue();
62 if (length.type() == ExtendToZoom)
63 return ViewportDescription::ValueExtendToZoom;
65 if (length.type() == Percent && direction == Horizontal)
66 return initialViewportSize.width() * length.getFloatValue() / 100.0f;
68 if (length.type() == Percent && direction == Vertical)
69 return initialViewportSize.height() * length.getFloatValue() / 100.0f;
71 if (length.type() == DeviceWidth)
72 return initialViewportSize.width();
74 if (length.type() == DeviceHeight)
75 return initialViewportSize.height();
78 return ViewportDescription::ValueAuto;
81 PageScaleConstraints ViewportDescription::resolve(const FloatSize& initialViewportSize, Length legacyFallbackWidth) const
83 float resultWidth = ValueAuto;
85 Length copyMaxWidth = maxWidth;
86 Length copyMinWidth = minWidth;
87 // In case the width (used for min- and max-width) is undefined.
88 if (isLegacyViewportType() && maxWidth.isAuto()) {
89 // The width viewport META property is translated into 'width' descriptors, setting
90 // the 'min' value to 'extend-to-zoom' and the 'max' value to the intended length.
91 // In case the UA-defines a min-width, use that as length.
92 if (zoom == ViewportDescription::ValueAuto) {
93 copyMinWidth = Length(ExtendToZoom);
94 copyMaxWidth = legacyFallbackWidth;
95 } else if (maxHeight.isAuto()) {
96 copyMinWidth = Length(ExtendToZoom);
97 copyMaxWidth = Length(ExtendToZoom);
101 float resultMaxWidth = resolveViewportLength(copyMaxWidth, initialViewportSize, Horizontal);
102 float resultMinWidth = resolveViewportLength(copyMinWidth, initialViewportSize, Horizontal);
104 float resultHeight = ValueAuto;
105 float resultMaxHeight = resolveViewportLength(maxHeight, initialViewportSize, Vertical);
106 float resultMinHeight = resolveViewportLength(minHeight, initialViewportSize, Vertical);
108 float resultZoom = zoom;
109 float resultMinZoom = minZoom;
110 float resultMaxZoom = maxZoom;
111 bool resultUserZoom = userZoom;
113 // 1. Resolve min-zoom and max-zoom values.
114 if (resultMinZoom != ViewportDescription::ValueAuto && resultMaxZoom != ViewportDescription::ValueAuto)
115 resultMaxZoom = max(resultMinZoom, resultMaxZoom);
117 // 2. Constrain zoom value to the [min-zoom, max-zoom] range.
118 if (resultZoom != ViewportDescription::ValueAuto)
119 resultZoom = compareIgnoringAuto(resultMinZoom, compareIgnoringAuto(resultMaxZoom, resultZoom, min), max);
121 float extendZoom = compareIgnoringAuto(resultZoom, resultMaxZoom, min);
123 // 3. Resolve non-"auto" lengths to pixel lengths.
124 if (extendZoom == ViewportDescription::ValueAuto) {
125 if (resultMaxWidth == ViewportDescription::ValueExtendToZoom)
126 resultMaxWidth = ViewportDescription::ValueAuto;
128 if (resultMaxHeight == ViewportDescription::ValueExtendToZoom)
129 resultMaxHeight = ViewportDescription::ValueAuto;
131 if (resultMinWidth == ViewportDescription::ValueExtendToZoom)
132 resultMinWidth = resultMaxWidth;
134 if (resultMinHeight == ViewportDescription::ValueExtendToZoom)
135 resultMinHeight = resultMaxHeight;
137 float extendWidth = initialViewportSize.width() / extendZoom;
138 float extendHeight = initialViewportSize.height() / extendZoom;
140 if (resultMaxWidth == ViewportDescription::ValueExtendToZoom)
141 resultMaxWidth = extendWidth;
143 if (resultMaxHeight == ViewportDescription::ValueExtendToZoom)
144 resultMaxHeight = extendHeight;
146 if (resultMinWidth == ViewportDescription::ValueExtendToZoom)
147 resultMinWidth = compareIgnoringAuto(extendWidth, resultMaxWidth, max);
149 if (resultMinHeight == ViewportDescription::ValueExtendToZoom)
150 resultMinHeight = compareIgnoringAuto(extendHeight, resultMaxHeight, max);
153 // 4. Resolve initial width from min/max descriptors.
154 if (resultMinWidth != ViewportDescription::ValueAuto || resultMaxWidth != ViewportDescription::ValueAuto)
155 resultWidth = compareIgnoringAuto(resultMinWidth, compareIgnoringAuto(resultMaxWidth, initialViewportSize.width(), min), max);
157 // 5. Resolve initial height from min/max descriptors.
158 if (resultMinHeight != ViewportDescription::ValueAuto || resultMaxHeight != ViewportDescription::ValueAuto)
159 resultHeight = compareIgnoringAuto(resultMinHeight, compareIgnoringAuto(resultMaxHeight, initialViewportSize.height(), min), max);
161 // 6-7. Resolve width value.
162 if (resultWidth == ViewportDescription::ValueAuto) {
163 if (resultHeight == ViewportDescription::ValueAuto || !initialViewportSize.height())
164 resultWidth = initialViewportSize.width();
166 resultWidth = resultHeight * (initialViewportSize.width() / initialViewportSize.height());
169 // 8. Resolve height value.
170 if (resultHeight == ViewportDescription::ValueAuto) {
171 if (!initialViewportSize.width())
172 resultHeight = initialViewportSize.height();
174 resultHeight = resultWidth * initialViewportSize.height() / initialViewportSize.width();
177 // Resolve initial-scale value.
178 if (resultZoom == ViewportDescription::ValueAuto) {
179 if (resultWidth != ViewportDescription::ValueAuto && resultWidth > 0)
180 resultZoom = initialViewportSize.width() / resultWidth;
181 if (resultHeight != ViewportDescription::ValueAuto && resultHeight > 0) {
182 // if 'auto', the initial-scale will be negative here and thus ignored.
183 resultZoom = max<float>(resultZoom, initialViewportSize.height() / resultHeight);
187 // If user-scalable = no, lock the min/max scale to the computed initial
190 resultMinZoom = resultMaxZoom = resultZoom;
192 // Only set initialScale to a value if it was explicitly set.
193 if (zoom == ViewportDescription::ValueAuto)
194 resultZoom = ViewportDescription::ValueAuto;
196 PageScaleConstraints result;
197 result.minimumScale = resultMinZoom;
198 result.maximumScale = resultMaxZoom;
199 result.initialScale = resultZoom;
200 result.layoutSize.setWidth(resultWidth);
201 result.layoutSize.setHeight(resultHeight);
205 void ViewportDescription::reportMobilePageStats(const LocalFrame* mainFrame) const
208 enum ViewportUMAType {
213 MetaHandheldFriendly,
219 if (!mainFrame || !mainFrame->host() || !mainFrame->view() || !mainFrame->document())
222 // Avoid chrome:// pages like the new-tab page (on Android new tab is non-http).
223 if (!mainFrame->document()->url().protocolIsInHTTPFamily())
226 if (!isSpecifiedByAuthor()) {
227 if (mainFrame->document()->isMobileDocument())
228 blink::Platform::current()->histogramEnumeration("Viewport.MetaTagType", XhtmlMobileProfile, TypeCount);
230 blink::Platform::current()->histogramEnumeration("Viewport.MetaTagType", NoViewportTag, TypeCount);
235 if (isMetaViewportType()) {
236 if (maxWidth.type() == WebCore::Fixed) {
237 blink::Platform::current()->histogramEnumeration("Viewport.MetaTagType", ConstantWidth, TypeCount);
239 if (mainFrame->view()) {
240 // To get an idea of how "far" the viewport is from the device's ideal width, we
241 // report the zoom level that we'd need to be at for the entire page to be visible.
242 int viewportWidth = maxWidth.intValue();
243 int windowWidth = mainFrame->document()->settings()->pinchVirtualViewportEnabled()
244 ? mainFrame->host()->pinchViewport().size().width()
245 : mainFrame->view()->frameRect().width();
246 int overviewZoomPercent = 100 * windowWidth / static_cast<float>(viewportWidth);
247 blink::Platform::current()->histogramSparse("Viewport.OverviewZoom", overviewZoomPercent);
250 } else if (maxWidth.type() == WebCore::DeviceWidth || maxWidth.type() == WebCore::ExtendToZoom) {
251 blink::Platform::current()->histogramEnumeration("Viewport.MetaTagType", DeviceWidth, TypeCount);
253 // Overflow bucket for cases we may be unaware of.
254 blink::Platform::current()->histogramEnumeration("Viewport.MetaTagType", MetaWidthOther, TypeCount);
256 } else if (type == ViewportDescription::HandheldFriendlyMeta) {
257 blink::Platform::current()->histogramEnumeration("Viewport.MetaTagType", MetaHandheldFriendly, TypeCount);
258 } else if (type == ViewportDescription::MobileOptimizedMeta) {
259 blink::Platform::current()->histogramEnumeration("Viewport.MetaTagType", MobileOptimizedMeta, TypeCount);
264 } // namespace WebCore