Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / dom / ViewportDescription.cpp
1 /*
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.
10  *
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.
15  *
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.
20  *
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.
25  *
26  */
27
28 #include "config.h"
29 #include "core/dom/ViewportDescription.h"
30
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"
38
39 using namespace std;
40
41 namespace WebCore {
42
43 static const float& compareIgnoringAuto(const float& value1, const float& value2, const float& (*compare) (const float&, const float&))
44 {
45     if (value1 == ViewportDescription::ValueAuto)
46         return value2;
47
48     if (value2 == ViewportDescription::ValueAuto)
49         return value1;
50
51     return compare(value1, value2);
52 }
53
54 float ViewportDescription::resolveViewportLength(const Length& length, const FloatSize& initialViewportSize, Direction direction)
55 {
56     if (length.isAuto())
57         return ViewportDescription::ValueAuto;
58
59     if (length.isFixed())
60         return length.getFloatValue();
61
62     if (length.type() == ExtendToZoom)
63         return ViewportDescription::ValueExtendToZoom;
64
65     if (length.type() == Percent && direction == Horizontal)
66         return initialViewportSize.width() * length.getFloatValue() / 100.0f;
67
68     if (length.type() == Percent && direction == Vertical)
69         return initialViewportSize.height() * length.getFloatValue() / 100.0f;
70
71     if (length.type() == DeviceWidth)
72         return initialViewportSize.width();
73
74     if (length.type() == DeviceHeight)
75         return initialViewportSize.height();
76
77     ASSERT_NOT_REACHED();
78     return ViewportDescription::ValueAuto;
79 }
80
81 PageScaleConstraints ViewportDescription::resolve(const FloatSize& initialViewportSize, Length legacyFallbackWidth) const
82 {
83     float resultWidth = ValueAuto;
84
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);
98         }
99     }
100
101     float resultMaxWidth = resolveViewportLength(copyMaxWidth, initialViewportSize, Horizontal);
102     float resultMinWidth = resolveViewportLength(copyMinWidth, initialViewportSize, Horizontal);
103
104     float resultHeight = ValueAuto;
105     float resultMaxHeight = resolveViewportLength(maxHeight, initialViewportSize, Vertical);
106     float resultMinHeight = resolveViewportLength(minHeight, initialViewportSize, Vertical);
107
108     float resultZoom = zoom;
109     float resultMinZoom = minZoom;
110     float resultMaxZoom = maxZoom;
111     bool resultUserZoom = userZoom;
112
113     // 1. Resolve min-zoom and max-zoom values.
114     if (resultMinZoom != ViewportDescription::ValueAuto && resultMaxZoom != ViewportDescription::ValueAuto)
115         resultMaxZoom = max(resultMinZoom, resultMaxZoom);
116
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);
120
121     float extendZoom = compareIgnoringAuto(resultZoom, resultMaxZoom, min);
122
123     // 3. Resolve non-"auto" lengths to pixel lengths.
124     if (extendZoom == ViewportDescription::ValueAuto) {
125         if (resultMaxWidth == ViewportDescription::ValueExtendToZoom)
126             resultMaxWidth = ViewportDescription::ValueAuto;
127
128         if (resultMaxHeight == ViewportDescription::ValueExtendToZoom)
129             resultMaxHeight = ViewportDescription::ValueAuto;
130
131         if (resultMinWidth == ViewportDescription::ValueExtendToZoom)
132             resultMinWidth = resultMaxWidth;
133
134         if (resultMinHeight == ViewportDescription::ValueExtendToZoom)
135             resultMinHeight = resultMaxHeight;
136     } else {
137         float extendWidth = initialViewportSize.width() / extendZoom;
138         float extendHeight = initialViewportSize.height() / extendZoom;
139
140         if (resultMaxWidth == ViewportDescription::ValueExtendToZoom)
141             resultMaxWidth = extendWidth;
142
143         if (resultMaxHeight == ViewportDescription::ValueExtendToZoom)
144             resultMaxHeight = extendHeight;
145
146         if (resultMinWidth == ViewportDescription::ValueExtendToZoom)
147             resultMinWidth = compareIgnoringAuto(extendWidth, resultMaxWidth, max);
148
149         if (resultMinHeight == ViewportDescription::ValueExtendToZoom)
150             resultMinHeight = compareIgnoringAuto(extendHeight, resultMaxHeight, max);
151     }
152
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);
156
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);
160
161     // 6-7. Resolve width value.
162     if (resultWidth == ViewportDescription::ValueAuto) {
163         if (resultHeight == ViewportDescription::ValueAuto || !initialViewportSize.height())
164             resultWidth = initialViewportSize.width();
165         else
166             resultWidth = resultHeight * (initialViewportSize.width() / initialViewportSize.height());
167     }
168
169     // 8. Resolve height value.
170     if (resultHeight == ViewportDescription::ValueAuto) {
171         if (!initialViewportSize.width())
172             resultHeight = initialViewportSize.height();
173         else
174             resultHeight = resultWidth * initialViewportSize.height() / initialViewportSize.width();
175     }
176
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);
184         }
185     }
186
187     // If user-scalable = no, lock the min/max scale to the computed initial
188     // scale.
189     if (!resultUserZoom)
190         resultMinZoom = resultMaxZoom = resultZoom;
191
192     // Only set initialScale to a value if it was explicitly set.
193     if (zoom == ViewportDescription::ValueAuto)
194         resultZoom = ViewportDescription::ValueAuto;
195
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);
202     return result;
203 }
204
205 void ViewportDescription::reportMobilePageStats(const LocalFrame* mainFrame) const
206 {
207 #if OS(ANDROID)
208     enum ViewportUMAType {
209         NoViewportTag,
210         DeviceWidth,
211         ConstantWidth,
212         MetaWidthOther,
213         MetaHandheldFriendly,
214         MetaMobileOptimized,
215         XhtmlMobileProfile,
216         TypeCount
217     };
218
219     if (!mainFrame || !mainFrame->host() || !mainFrame->view() || !mainFrame->document())
220         return;
221
222     // Avoid chrome:// pages like the new-tab page (on Android new tab is non-http).
223     if (!mainFrame->document()->url().protocolIsInHTTPFamily())
224         return;
225
226     if (!isSpecifiedByAuthor()) {
227         if (mainFrame->document()->isMobileDocument())
228             blink::Platform::current()->histogramEnumeration("Viewport.MetaTagType", XhtmlMobileProfile, TypeCount);
229         else
230             blink::Platform::current()->histogramEnumeration("Viewport.MetaTagType", NoViewportTag, TypeCount);
231
232         return;
233     }
234
235     if (isMetaViewportType()) {
236         if (maxWidth.type() == WebCore::Fixed) {
237             blink::Platform::current()->histogramEnumeration("Viewport.MetaTagType", ConstantWidth, TypeCount);
238
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);
248             }
249
250         } else if (maxWidth.type() == WebCore::DeviceWidth || maxWidth.type() == WebCore::ExtendToZoom) {
251             blink::Platform::current()->histogramEnumeration("Viewport.MetaTagType", DeviceWidth, TypeCount);
252         } else {
253             // Overflow bucket for cases we may be unaware of.
254             blink::Platform::current()->histogramEnumeration("Viewport.MetaTagType", MetaWidthOther, TypeCount);
255         }
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);
260     }
261 #endif
262 }
263
264 } // namespace WebCore