1 /* 2 * Copyright (C) 1999 Lars Knoll (knoll (at) kde.org) 3 * (C) 1999 Antti Koivisto (koivisto (at) kde.org) 4 * (C) 2001 Dirk Mueller (mueller (at) kde.org) 5 * (C) 2006 Alexey Proskuryakov (ap (at) 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 namespace WebCore { 40 41 static const float& compareIgnoringAuto(const float& value1, const float& value2, const float& (*compare) (const float&, const float&)) 42 { 43 if (value1 == ViewportDescription::ValueAuto) 44 return value2; 45 46 if (value2 == ViewportDescription::ValueAuto) 47 return value1; 48 49 return compare(value1, value2); 50 } 51 52 float ViewportDescription::resolveViewportLength(const Length& length, const FloatSize& initialViewportSize, Direction direction) 53 { 54 if (length.isAuto()) 55 return ViewportDescription::ValueAuto; 56 57 if (length.isFixed()) 58 return length.getFloatValue(); 59 60 if (length.type() == ExtendToZoom) 61 return ViewportDescription::ValueExtendToZoom; 62 63 if (length.type() == Percent && direction == Horizontal) 64 return initialViewportSize.width() * length.getFloatValue() / 100.0f; 65 66 if (length.type() == Percent && direction == Vertical) 67 return initialViewportSize.height() * length.getFloatValue() / 100.0f; 68 69 if (length.type() == DeviceWidth) 70 return initialViewportSize.width(); 71 72 if (length.type() == DeviceHeight) 73 return initialViewportSize.height(); 74 75 ASSERT_NOT_REACHED(); 76 return ViewportDescription::ValueAuto; 77 } 78 79 PageScaleConstraints ViewportDescription::resolve(const FloatSize& initialViewportSize, Length legacyFallbackWidth) const 80 { 81 float resultWidth = ValueAuto; 82 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); 96 } 97 } 98 99 float resultMaxWidth = resolveViewportLength(copyMaxWidth, initialViewportSize, Horizontal); 100 float resultMinWidth = resolveViewportLength(copyMinWidth, initialViewportSize, Horizontal); 101 102 float resultHeight = ValueAuto; 103 float resultMaxHeight = resolveViewportLength(maxHeight, initialViewportSize, Vertical); 104 float resultMinHeight = resolveViewportLength(minHeight, initialViewportSize, Vertical); 105 106 float resultZoom = zoom; 107 float resultMinZoom = minZoom; 108 float resultMaxZoom = maxZoom; 109 bool resultUserZoom = userZoom; 110 111 // 1. Resolve min-zoom and max-zoom values. 112 if (resultMinZoom != ViewportDescription::ValueAuto && resultMaxZoom != ViewportDescription::ValueAuto) 113 resultMaxZoom = std::max(resultMinZoom, resultMaxZoom); 114 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); 118 119 float extendZoom = compareIgnoringAuto(resultZoom, resultMaxZoom, std::min); 120 121 // 3. Resolve non-"auto" lengths to pixel lengths. 122 if (extendZoom == ViewportDescription::ValueAuto) { 123 if (resultMaxWidth == ViewportDescription::ValueExtendToZoom) 124 resultMaxWidth = ViewportDescription::ValueAuto; 125 126 if (resultMaxHeight == ViewportDescription::ValueExtendToZoom) 127 resultMaxHeight = ViewportDescription::ValueAuto; 128 129 if (resultMinWidth == ViewportDescription::ValueExtendToZoom) 130 resultMinWidth = resultMaxWidth; 131 132 if (resultMinHeight == ViewportDescription::ValueExtendToZoom) 133 resultMinHeight = resultMaxHeight; 134 } else { 135 float extendWidth = initialViewportSize.width() / extendZoom; 136 float extendHeight = initialViewportSize.height() / extendZoom; 137 138 if (resultMaxWidth == ViewportDescription::ValueExtendToZoom) 139 resultMaxWidth = extendWidth; 140 141 if (resultMaxHeight == ViewportDescription::ValueExtendToZoom) 142 resultMaxHeight = extendHeight; 143 144 if (resultMinWidth == ViewportDescription::ValueExtendToZoom) 145 resultMinWidth = compareIgnoringAuto(extendWidth, resultMaxWidth, std::max); 146 147 if (resultMinHeight == ViewportDescription::ValueExtendToZoom) 148 resultMinHeight = compareIgnoringAuto(extendHeight, resultMaxHeight, std::max); 149 } 150 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); 154 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); 158 159 // 6-7. Resolve width value. 160 if (resultWidth == ViewportDescription::ValueAuto) { 161 if (resultHeight == ViewportDescription::ValueAuto || !initialViewportSize.height()) 162 resultWidth = initialViewportSize.width(); 163 else 164 resultWidth = resultHeight * (initialViewportSize.width() / initialViewportSize.height()); 165 } 166 167 // 8. Resolve height value. 168 if (resultHeight == ViewportDescription::ValueAuto) { 169 if (!initialViewportSize.width()) 170 resultHeight = initialViewportSize.height(); 171 else 172 resultHeight = resultWidth * initialViewportSize.height() / initialViewportSize.width(); 173 } 174 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); 182 } 183 } 184 185 // If user-scalable = no, lock the min/max scale to the computed initial 186 // scale. 187 if (!resultUserZoom) 188 resultMinZoom = resultMaxZoom = resultZoom; 189 190 // Only set initialScale to a value if it was explicitly set. 191 if (zoom == ViewportDescription::ValueAuto) 192 resultZoom = ViewportDescription::ValueAuto; 193 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); 200 return result; 201 } 202 203 void ViewportDescription::reportMobilePageStats(const LocalFrame* mainFrame) const 204 { 205 #if OS(ANDROID) 206 enum ViewportUMAType { 207 NoViewportTag, 208 DeviceWidth, 209 ConstantWidth, 210 MetaWidthOther, 211 MetaHandheldFriendly, 212 MetaMobileOptimized, 213 XhtmlMobileProfile, 214 TypeCount 215 }; 216 217 if (!mainFrame || !mainFrame->host() || !mainFrame->view() || !mainFrame->document()) 218 return; 219 220 // Avoid chrome:// pages like the new-tab page (on Android new tab is non-http). 221 if (!mainFrame->document()->url().protocolIsInHTTPFamily()) 222 return; 223 224 if (!isSpecifiedByAuthor()) { 225 if (mainFrame->document()->isMobileDocument()) 226 blink::Platform::current()->histogramEnumeration("Viewport.MetaTagType", XhtmlMobileProfile, TypeCount); 227 else 228 blink::Platform::current()->histogramEnumeration("Viewport.MetaTagType", NoViewportTag, TypeCount); 229 230 return; 231 } 232 233 if (isMetaViewportType()) { 234 if (maxWidth.type() == WebCore::Fixed) { 235 blink::Platform::current()->histogramEnumeration("Viewport.MetaTagType", ConstantWidth, TypeCount); 236 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 blink::Platform::current()->histogramSparse("Viewport.OverviewZoom", overviewZoomPercent); 246 } 247 248 } else if (maxWidth.type() == WebCore::DeviceWidth || maxWidth.type() == WebCore::ExtendToZoom) { 249 blink::Platform::current()->histogramEnumeration("Viewport.MetaTagType", DeviceWidth, TypeCount); 250 } else { 251 // Overflow bucket for cases we may be unaware of. 252 blink::Platform::current()->histogramEnumeration("Viewport.MetaTagType", MetaWidthOther, TypeCount); 253 } 254 } else if (type == ViewportDescription::HandheldFriendlyMeta) { 255 blink::Platform::current()->histogramEnumeration("Viewport.MetaTagType", MetaHandheldFriendly, TypeCount); 256 } else if (type == ViewportDescription::MobileOptimizedMeta) { 257 blink::Platform::current()->histogramEnumeration("Viewport.MetaTagType", MobileOptimizedMeta, TypeCount); 258 } 259 #endif 260 } 261 262 } // namespace WebCore 263