summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/page/viewport_description.cc
blob: ee87f7d8daf088bba7c3bd2424d613ae09a6fe38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
 *           (C) 1999 Antti Koivisto (koivisto@kde.org)
 *           (C) 2001 Dirk Mueller (mueller@kde.org)
 *           (C) 2006 Alexey Proskuryakov (ap@webkit.org)
 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2011 Apple Inc. All rights
 * reserved.
 * Copyright (C) 2008 Torch Mobile Inc. All rights reserved.
 * (http://www.torchmobile.com/)
 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
 * Copyright (C) 2012-2013 Intel Corporation. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 */

#include "third_party/blink/renderer/core/page/viewport_description.h"

#include "build/build_config.h"
#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/frame/local_frame.h"
#include "third_party/blink/renderer/core/frame/local_frame_view.h"
#include "third_party/blink/renderer/core/frame/settings.h"
#include "third_party/blink/renderer/core/frame/visual_viewport.h"
#include "third_party/blink/renderer/core/page/page.h"
#include "third_party/blink/renderer/platform/histogram.h"
#include "third_party/blink/renderer/platform/weborigin/kurl.h"

namespace blink {

static const float& CompareIgnoringAuto(const float& value1,
                                        const float& value2,
                                        const float& (*compare)(const float&,
                                                                const float&)) {
  if (value1 == ViewportDescription::kValueAuto)
    return value2;

  if (value2 == ViewportDescription::kValueAuto)
    return value1;

  return compare(value1, value2);
}

float ViewportDescription::ResolveViewportLength(
    const Length& length,
    const FloatSize& initial_viewport_size,
    Direction direction) {
  if (length.IsAuto())
    return ViewportDescription::kValueAuto;

  if (length.IsFixed())
    return length.GetFloatValue();

  if (length.GetType() == kExtendToZoom)
    return ViewportDescription::kValueExtendToZoom;

  if (length.GetType() == kPercent && direction == kHorizontal)
    return initial_viewport_size.Width() * length.GetFloatValue() / 100.0f;

  if (length.GetType() == kPercent && direction == kVertical)
    return initial_viewport_size.Height() * length.GetFloatValue() / 100.0f;

  if (length.GetType() == kDeviceWidth)
    return initial_viewport_size.Width();

  if (length.GetType() == kDeviceHeight)
    return initial_viewport_size.Height();

  NOTREACHED();
  return ViewportDescription::kValueAuto;
}

PageScaleConstraints ViewportDescription::Resolve(
    const FloatSize& initial_viewport_size,
    Length legacy_fallback_width) const {
  float result_width = kValueAuto;

  Length copy_max_width = max_width;
  Length copy_min_width = min_width;
  // In case the width (used for min- and max-width) is undefined.
  if (IsLegacyViewportType() && max_width.IsAuto()) {
    // The width viewport META property is translated into 'width' descriptors,
    // setting the 'min' value to 'extend-to-zoom' and the 'max' value to the
    // intended length.  In case the UA-defines a min-width, use that as length.
    if (zoom == ViewportDescription::kValueAuto) {
      copy_min_width = Length(kExtendToZoom);
      copy_max_width = legacy_fallback_width;
    } else if (max_height.IsAuto()) {
      copy_min_width = Length(kExtendToZoom);
      copy_max_width = Length(kExtendToZoom);
    }
  }

  float result_max_width =
      ResolveViewportLength(copy_max_width, initial_viewport_size, kHorizontal);
  float result_min_width =
      ResolveViewportLength(copy_min_width, initial_viewport_size, kHorizontal);

  float result_height = kValueAuto;
  float result_max_height =
      ResolveViewportLength(max_height, initial_viewport_size, kVertical);
  float result_min_height =
      ResolveViewportLength(min_height, initial_viewport_size, kVertical);

  float result_zoom = zoom;
  float result_min_zoom = min_zoom;
  float result_max_zoom = max_zoom;
  bool result_user_zoom = user_zoom;

  // Resolve min-zoom and max-zoom values.
  if (result_min_zoom != ViewportDescription::kValueAuto &&
      result_max_zoom != ViewportDescription::kValueAuto)
    result_max_zoom = std::max(result_min_zoom, result_max_zoom);

  // Constrain zoom value to the [min-zoom, max-zoom] range.
  if (result_zoom != ViewportDescription::kValueAuto)
    result_zoom = CompareIgnoringAuto(
        result_min_zoom,
        CompareIgnoringAuto(result_max_zoom, result_zoom, std::min), std::max);

  float extend_zoom =
      CompareIgnoringAuto(result_zoom, result_max_zoom, std::min);

  // Resolve non-"auto" lengths to pixel lengths.
  if (extend_zoom == ViewportDescription::kValueAuto) {
    if (result_max_width == ViewportDescription::kValueExtendToZoom)
      result_max_width = ViewportDescription::kValueAuto;

    if (result_max_height == ViewportDescription::kValueExtendToZoom)
      result_max_height = ViewportDescription::kValueAuto;

    if (result_min_width == ViewportDescription::kValueExtendToZoom)
      result_min_width = result_max_width;

    if (result_min_height == ViewportDescription::kValueExtendToZoom)
      result_min_height = result_max_height;
  } else {
    float extend_width = initial_viewport_size.Width() / extend_zoom;
    float extend_height = initial_viewport_size.Height() / extend_zoom;

    if (result_max_width == ViewportDescription::kValueExtendToZoom)
      result_max_width = extend_width;

    if (result_max_height == ViewportDescription::kValueExtendToZoom)
      result_max_height = extend_height;

    if (result_min_width == ViewportDescription::kValueExtendToZoom)
      result_min_width =
          CompareIgnoringAuto(extend_width, result_max_width, std::max);

    if (result_min_height == ViewportDescription::kValueExtendToZoom)
      result_min_height =
          CompareIgnoringAuto(extend_height, result_max_height, std::max);
  }

  // Resolve initial width from min/max descriptors.
  if (result_min_width != ViewportDescription::kValueAuto ||
      result_max_width != ViewportDescription::kValueAuto)
    result_width = CompareIgnoringAuto(
        result_min_width,
        CompareIgnoringAuto(result_max_width, initial_viewport_size.Width(),
                            std::min),
        std::max);

  // Resolve initial height from min/max descriptors.
  if (result_min_height != ViewportDescription::kValueAuto ||
      result_max_height != ViewportDescription::kValueAuto)
    result_height = CompareIgnoringAuto(
        result_min_height,
        CompareIgnoringAuto(result_max_height, initial_viewport_size.Height(),
                            std::min),
        std::max);

  // Resolve width value.
  if (result_width == ViewportDescription::kValueAuto) {
    if (result_height == ViewportDescription::kValueAuto ||
        !initial_viewport_size.Height())
      result_width = initial_viewport_size.Width();
    else
      result_width = result_height * (initial_viewport_size.Width() /
                                      initial_viewport_size.Height());
  }

  // Resolve height value.
  if (result_height == ViewportDescription::kValueAuto) {
    if (!initial_viewport_size.Width())
      result_height = initial_viewport_size.Height();
    else
      result_height = result_width * initial_viewport_size.Height() /
                      initial_viewport_size.Width();
  }

  // Resolve initial-scale value.
  if (result_zoom == ViewportDescription::kValueAuto) {
    if (result_width != ViewportDescription::kValueAuto && result_width > 0)
      result_zoom = initial_viewport_size.Width() / result_width;
    if (result_height != ViewportDescription::kValueAuto && result_height > 0) {
      // if 'auto', the initial-scale will be negative here and thus ignored.
      result_zoom = std::max<float>(
          result_zoom, initial_viewport_size.Height() / result_height);
    }

    // Reconstrain zoom value to the [min-zoom, max-zoom] range.
    result_zoom = CompareIgnoringAuto(
        result_min_zoom,
        CompareIgnoringAuto(result_max_zoom, result_zoom, std::min), std::max);
  }

  // If user-scalable = no, lock the min/max scale to the computed initial
  // scale.
  if (!result_user_zoom)
    result_min_zoom = result_max_zoom = result_zoom;

  // Only set initialScale to a value if it was explicitly set.
  if (zoom == ViewportDescription::kValueAuto)
    result_zoom = ViewportDescription::kValueAuto;

  PageScaleConstraints result;
  result.minimum_scale = result_min_zoom;
  result.maximum_scale = result_max_zoom;
  result.initial_scale = result_zoom;
  result.layout_size.SetWidth(result_width);
  result.layout_size.SetHeight(result_height);
  return result;
}

void ViewportDescription::ReportMobilePageStats(
    const LocalFrame* main_frame) const {
  if (!main_frame || !main_frame->GetPage() || !main_frame->View() ||
      !main_frame->GetDocument())
    return;

  if (!main_frame->GetSettings() ||
      !main_frame->GetSettings()->GetViewportEnabled())
    return;

  // Avoid chrome:// pages like the new-tab page (on Android new tab is
  // non-http).
  if (!main_frame->GetDocument()->Url().ProtocolIsInHTTPFamily())
    return;

  DEFINE_STATIC_LOCAL(
      EnumerationHistogram, meta_tag_type_histogram,
      ("Viewport.MetaTagType", static_cast<int>(ViewportUMAType::kTypeCount)));
  if (!IsSpecifiedByAuthor()) {
    meta_tag_type_histogram.Count(
        main_frame->GetDocument()->IsMobileDocument()
            ? static_cast<int>(ViewportUMAType::kXhtmlMobileProfile)
            : static_cast<int>(ViewportUMAType::kNoViewportTag));
    return;
  }

  if (IsMetaViewportType()) {
    if (max_width.GetType() == blink::kFixed) {
      meta_tag_type_histogram.Count(
          static_cast<int>(ViewportUMAType::kConstantWidth));

      if (main_frame->View()) {
        // To get an idea of how "far" the viewport is from the device's ideal
        // width, we report the zoom level that we'd need to be at for the
        // entire page to be visible.
        int viewport_width = max_width.IntValue();
        int window_width =
            main_frame->GetPage()->GetVisualViewport().Size().Width();
        int overview_zoom_percent =
            100 * window_width / static_cast<float>(viewport_width);
        DEFINE_STATIC_LOCAL(SparseHistogram, overview_zoom_histogram,
                            ("Viewport.OverviewZoom"));
        overview_zoom_histogram.Sample(overview_zoom_percent);
      }

    } else if (max_width.GetType() == blink::kDeviceWidth ||
               max_width.GetType() == blink::kExtendToZoom) {
      meta_tag_type_histogram.Count(
          static_cast<int>(ViewportUMAType::kDeviceWidth));
    } else {
      // Overflow bucket for cases we may be unaware of.
      meta_tag_type_histogram.Count(
          static_cast<int>(ViewportUMAType::kMetaWidthOther));
    }
  } else if (type == ViewportDescription::kHandheldFriendlyMeta) {
    meta_tag_type_histogram.Count(
        static_cast<int>(ViewportUMAType::kMetaHandheldFriendly));
  } else if (type == ViewportDescription::kMobileOptimizedMeta) {
    meta_tag_type_histogram.Count(
        static_cast<int>(ViewportUMAType::kMetaMobileOptimized));
  }
}

bool ViewportDescription::MatchesHeuristicsForGpuRasterization() const {
  return IsSpecifiedByAuthor();
}

}  // namespace blink