11e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)/*
21e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles) * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
31e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles) *           (C) 1999 Antti Koivisto (koivisto@kde.org)
41e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles) *           (C) 2001 Dirk Mueller (mueller@kde.org)
51e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles) *           (C) 2006 Alexey Proskuryakov (ap@webkit.org)
61e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles) * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2011 Apple Inc. All rights reserved.
71e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles) * Copyright (C) 2008 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
81e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles) * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
91e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles) * Copyright (C) 2012-2013 Intel Corporation. All rights reserved.
101e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles) *
111e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles) * This library is free software; you can redistribute it and/or
121e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles) * modify it under the terms of the GNU Library General Public
131e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles) * License as published by the Free Software Foundation; either
141e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles) * version 2 of the License, or (at your option) any later version.
151e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles) *
161e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles) * This library is distributed in the hope that it will be useful,
171e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles) * but WITHOUT ANY WARRANTY; without even the implied warranty of
181e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles) * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
191e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles) * Library General Public License for more details.
201e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles) *
211e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles) * You should have received a copy of the GNU Library General Public License
221e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles) * along with this library; see the file COPYING.LIB.  If not, write to
231e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles) * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
241e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles) * Boston, MA 02110-1301, USA.
251e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles) *
261e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles) */
271e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)
281e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)#include "config.h"
291e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)#include "core/dom/ViewportDescription.h"
301e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)
31323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)#include "core/dom/Document.h"
32323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)#include "core/frame/FrameHost.h"
33323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)#include "core/frame/FrameView.h"
34323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)#include "core/frame/LocalFrame.h"
35323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)#include "core/frame/Settings.h"
36323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)#include "platform/weborigin/KURL.h"
37323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)#include "public/platform/Platform.h"
38323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)
39c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)namespace blink {
401e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)
411e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)static const float& compareIgnoringAuto(const float& value1, const float& value2, const float& (*compare) (const float&, const float&))
421e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles){
431e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    if (value1 == ViewportDescription::ValueAuto)
441e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)        return value2;
451e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)
461e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    if (value2 == ViewportDescription::ValueAuto)
471e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)        return value1;
481e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)
491e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    return compare(value1, value2);
501e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)}
511e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)
521e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)float ViewportDescription::resolveViewportLength(const Length& length, const FloatSize& initialViewportSize, Direction direction)
531e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles){
541e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    if (length.isAuto())
551e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)        return ViewportDescription::ValueAuto;
561e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)
571e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    if (length.isFixed())
581e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)        return length.getFloatValue();
591e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)
601e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    if (length.type() == ExtendToZoom)
611e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)        return ViewportDescription::ValueExtendToZoom;
621e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)
6309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    if (length.type() == Percent && direction == Horizontal)
641e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)        return initialViewportSize.width() * length.getFloatValue() / 100.0f;
651e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)
6609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    if (length.type() == Percent && direction == Vertical)
671e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)        return initialViewportSize.height() * length.getFloatValue() / 100.0f;
681e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)
6909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    if (length.type() == DeviceWidth)
7009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        return initialViewportSize.width();
711e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)
7209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    if (length.type() == DeviceHeight)
7309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        return initialViewportSize.height();
741e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)
751e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    ASSERT_NOT_REACHED();
761e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    return ViewportDescription::ValueAuto;
771e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)}
781e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)
79aafa69cb17c9d6606c07663ade5f81388a2c5986Ben MurdochPageScaleConstraints ViewportDescription::resolve(const FloatSize& initialViewportSize, Length legacyFallbackWidth) const
801e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles){
811e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    float resultWidth = ValueAuto;
82aafa69cb17c9d6606c07663ade5f81388a2c5986Ben Murdoch
83aafa69cb17c9d6606c07663ade5f81388a2c5986Ben Murdoch    Length copyMaxWidth = maxWidth;
84aafa69cb17c9d6606c07663ade5f81388a2c5986Ben Murdoch    Length copyMinWidth = minWidth;
85aafa69cb17c9d6606c07663ade5f81388a2c5986Ben Murdoch    // In case the width (used for min- and max-width) is undefined.
86aafa69cb17c9d6606c07663ade5f81388a2c5986Ben Murdoch    if (isLegacyViewportType() && maxWidth.isAuto()) {
87aafa69cb17c9d6606c07663ade5f81388a2c5986Ben Murdoch        // The width viewport META property is translated into 'width' descriptors, setting
88aafa69cb17c9d6606c07663ade5f81388a2c5986Ben Murdoch        // the 'min' value to 'extend-to-zoom' and the 'max' value to the intended length.
89aafa69cb17c9d6606c07663ade5f81388a2c5986Ben Murdoch        // In case the UA-defines a min-width, use that as length.
90aafa69cb17c9d6606c07663ade5f81388a2c5986Ben Murdoch        if (zoom == ViewportDescription::ValueAuto) {
91aafa69cb17c9d6606c07663ade5f81388a2c5986Ben Murdoch            copyMinWidth = Length(ExtendToZoom);
92aafa69cb17c9d6606c07663ade5f81388a2c5986Ben Murdoch            copyMaxWidth = legacyFallbackWidth;
93aafa69cb17c9d6606c07663ade5f81388a2c5986Ben Murdoch        } else if (maxHeight.isAuto()) {
94aafa69cb17c9d6606c07663ade5f81388a2c5986Ben Murdoch            copyMinWidth = Length(ExtendToZoom);
95aafa69cb17c9d6606c07663ade5f81388a2c5986Ben Murdoch            copyMaxWidth = Length(ExtendToZoom);
96aafa69cb17c9d6606c07663ade5f81388a2c5986Ben Murdoch        }
97aafa69cb17c9d6606c07663ade5f81388a2c5986Ben Murdoch    }
98aafa69cb17c9d6606c07663ade5f81388a2c5986Ben Murdoch
99aafa69cb17c9d6606c07663ade5f81388a2c5986Ben Murdoch    float resultMaxWidth = resolveViewportLength(copyMaxWidth, initialViewportSize, Horizontal);
100aafa69cb17c9d6606c07663ade5f81388a2c5986Ben Murdoch    float resultMinWidth = resolveViewportLength(copyMinWidth, initialViewportSize, Horizontal);
101aafa69cb17c9d6606c07663ade5f81388a2c5986Ben Murdoch
1021e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    float resultHeight = ValueAuto;
1031e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    float resultMaxHeight = resolveViewportLength(maxHeight, initialViewportSize, Vertical);
1041e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    float resultMinHeight = resolveViewportLength(minHeight, initialViewportSize, Vertical);
1051e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)
1061e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    float resultZoom = zoom;
1071e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    float resultMinZoom = minZoom;
1081e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    float resultMaxZoom = maxZoom;
109f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    bool resultUserZoom = userZoom;
1101e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)
1111e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    // 1. Resolve min-zoom and max-zoom values.
1121e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    if (resultMinZoom != ViewportDescription::ValueAuto && resultMaxZoom != ViewportDescription::ValueAuto)
1135d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)        resultMaxZoom = std::max(resultMinZoom, resultMaxZoom);
1141e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)
1151e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    // 2. Constrain zoom value to the [min-zoom, max-zoom] range.
1161e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    if (resultZoom != ViewportDescription::ValueAuto)
1175d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)        resultZoom = compareIgnoringAuto(resultMinZoom, compareIgnoringAuto(resultMaxZoom, resultZoom, std::min), std::max);
1181e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)
1195d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)    float extendZoom = compareIgnoringAuto(resultZoom, resultMaxZoom, std::min);
1201e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)
1211e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    // 3. Resolve non-"auto" lengths to pixel lengths.
1221e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    if (extendZoom == ViewportDescription::ValueAuto) {
1231e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)        if (resultMaxWidth == ViewportDescription::ValueExtendToZoom)
1241e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)            resultMaxWidth = ViewportDescription::ValueAuto;
1251e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)
1261e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)        if (resultMaxHeight == ViewportDescription::ValueExtendToZoom)
1271e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)            resultMaxHeight = ViewportDescription::ValueAuto;
1281e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)
1291e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)        if (resultMinWidth == ViewportDescription::ValueExtendToZoom)
1301e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)            resultMinWidth = resultMaxWidth;
1311e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)
1321e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)        if (resultMinHeight == ViewportDescription::ValueExtendToZoom)
1331e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)            resultMinHeight = resultMaxHeight;
1341e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    } else {
1351e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)        float extendWidth = initialViewportSize.width() / extendZoom;
1361e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)        float extendHeight = initialViewportSize.height() / extendZoom;
1371e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)
1381e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)        if (resultMaxWidth == ViewportDescription::ValueExtendToZoom)
1391e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)            resultMaxWidth = extendWidth;
1401e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)
1411e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)        if (resultMaxHeight == ViewportDescription::ValueExtendToZoom)
1421e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)            resultMaxHeight = extendHeight;
1431e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)
1441e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)        if (resultMinWidth == ViewportDescription::ValueExtendToZoom)
1455d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)            resultMinWidth = compareIgnoringAuto(extendWidth, resultMaxWidth, std::max);
1461e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)
1471e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)        if (resultMinHeight == ViewportDescription::ValueExtendToZoom)
1485d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)            resultMinHeight = compareIgnoringAuto(extendHeight, resultMaxHeight, std::max);
1491e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    }
1501e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)
1511e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    // 4. Resolve initial width from min/max descriptors.
1521e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    if (resultMinWidth != ViewportDescription::ValueAuto || resultMaxWidth != ViewportDescription::ValueAuto)
1535d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)        resultWidth = compareIgnoringAuto(resultMinWidth, compareIgnoringAuto(resultMaxWidth, initialViewportSize.width(), std::min), std::max);
1541e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)
1551e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    // 5. Resolve initial height from min/max descriptors.
1561e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    if (resultMinHeight != ViewportDescription::ValueAuto || resultMaxHeight != ViewportDescription::ValueAuto)
1575d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)        resultHeight = compareIgnoringAuto(resultMinHeight, compareIgnoringAuto(resultMaxHeight, initialViewportSize.height(), std::min), std::max);
1581e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)
1591e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    // 6-7. Resolve width value.
1601e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    if (resultWidth == ViewportDescription::ValueAuto) {
1611e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)        if (resultHeight == ViewportDescription::ValueAuto || !initialViewportSize.height())
1621e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)            resultWidth = initialViewportSize.width();
1631e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)        else
1641e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)            resultWidth = resultHeight * (initialViewportSize.width() / initialViewportSize.height());
1651e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    }
1661e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)
1671e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    // 8. Resolve height value.
1681e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    if (resultHeight == ViewportDescription::ValueAuto) {
1691e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)        if (!initialViewportSize.width())
1701e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)            resultHeight = initialViewportSize.height();
1711e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)        else
1721e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)            resultHeight = resultWidth * initialViewportSize.height() / initialViewportSize.width();
1731e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    }
1741e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)
1751e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    // Resolve initial-scale value.
1761e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    if (resultZoom == ViewportDescription::ValueAuto) {
1771e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)        if (resultWidth != ViewportDescription::ValueAuto && resultWidth > 0)
1781e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)            resultZoom = initialViewportSize.width() / resultWidth;
1791e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)        if (resultHeight != ViewportDescription::ValueAuto && resultHeight > 0) {
1801e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)            // if 'auto', the initial-scale will be negative here and thus ignored.
1815d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)            resultZoom = std::max<float>(resultZoom, initialViewportSize.height() / resultHeight);
1821e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)        }
1831e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    }
1841e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)
1851e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    // If user-scalable = no, lock the min/max scale to the computed initial
1861e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    // scale.
1871e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    if (!resultUserZoom)
1881e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)        resultMinZoom = resultMaxZoom = resultZoom;
1891e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)
1901e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    // Only set initialScale to a value if it was explicitly set.
1911e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    if (zoom == ViewportDescription::ValueAuto)
1921e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)        resultZoom = ViewportDescription::ValueAuto;
1931e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)
1941e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    PageScaleConstraints result;
1951e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    result.minimumScale = resultMinZoom;
1961e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    result.maximumScale = resultMaxZoom;
1971e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    result.initialScale = resultZoom;
1981e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    result.layoutSize.setWidth(resultWidth);
1991e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    result.layoutSize.setHeight(resultHeight);
2001e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    return result;
2011e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)}
2021e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)
203323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)void ViewportDescription::reportMobilePageStats(const LocalFrame* mainFrame) const
204323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles){
205323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)#if OS(ANDROID)
206323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)    enum ViewportUMAType {
207323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)        NoViewportTag,
208323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)        DeviceWidth,
209323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)        ConstantWidth,
210323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)        MetaWidthOther,
211323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)        MetaHandheldFriendly,
212323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)        MetaMobileOptimized,
213323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)        XhtmlMobileProfile,
214323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)        TypeCount
215323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)    };
216323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)
217323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)    if (!mainFrame || !mainFrame->host() || !mainFrame->view() || !mainFrame->document())
218323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)        return;
219323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)
220323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)    // Avoid chrome:// pages like the new-tab page (on Android new tab is non-http).
221323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)    if (!mainFrame->document()->url().protocolIsInHTTPFamily())
222323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)        return;
223323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)
224323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)    if (!isSpecifiedByAuthor()) {
225323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)        if (mainFrame->document()->isMobileDocument())
226e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)            Platform::current()->histogramEnumeration("Viewport.MetaTagType", XhtmlMobileProfile, TypeCount);
227323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)        else
228e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)            Platform::current()->histogramEnumeration("Viewport.MetaTagType", NoViewportTag, TypeCount);
229323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)
230323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)        return;
231323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)    }
232323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)
233323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)    if (isMetaViewportType()) {
234c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)        if (maxWidth.type() == blink::Fixed) {
235e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)            Platform::current()->histogramEnumeration("Viewport.MetaTagType", ConstantWidth, TypeCount);
236323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)
237323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)            if (mainFrame->view()) {
238323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)                // To get an idea of how "far" the viewport is from the device's ideal width, we
239323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)                // report the zoom level that we'd need to be at for the entire page to be visible.
240323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)                int viewportWidth = maxWidth.intValue();
241323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)                int windowWidth = mainFrame->document()->settings()->pinchVirtualViewportEnabled()
242323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)                    ? mainFrame->host()->pinchViewport().size().width()
243323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)                    : mainFrame->view()->frameRect().width();
244323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)                int overviewZoomPercent = 100 * windowWidth / static_cast<float>(viewportWidth);
245e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)                Platform::current()->histogramSparse("Viewport.OverviewZoom", overviewZoomPercent);
246323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)            }
247323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)
248c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)        } else if (maxWidth.type() == blink::DeviceWidth || maxWidth.type() == blink::ExtendToZoom) {
249e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)            Platform::current()->histogramEnumeration("Viewport.MetaTagType", DeviceWidth, TypeCount);
250323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)        } else {
251323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)            // Overflow bucket for cases we may be unaware of.
252e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)            Platform::current()->histogramEnumeration("Viewport.MetaTagType", MetaWidthOther, TypeCount);
253323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)        }
254323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)    } else if (type == ViewportDescription::HandheldFriendlyMeta) {
255e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)        Platform::current()->histogramEnumeration("Viewport.MetaTagType", MetaHandheldFriendly, TypeCount);
256323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)    } else if (type == ViewportDescription::MobileOptimizedMeta) {
257e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)        Platform::current()->histogramEnumeration("Viewport.MetaTagType", MobileOptimizedMeta, TypeCount);
258323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)    }
259323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)#endif
260323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)}
261323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)
262c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)} // namespace blink
263