blob: 98dea1e6b2b4434a2d62d02f471ca13483554bff (
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
|
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/zoom/page_zoom.h"
#include <stddef.h>
#include <algorithm>
#include <cmath>
#include "base/metrics/user_metrics.h"
#include "components/prefs/pref_service.h"
#include "components/zoom/page_zoom_constants.h"
#include "components/zoom/zoom_controller.h"
#include "content/public/browser/host_zoom_map.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/page_zoom.h"
#include "content/public/common/renderer_preferences.h"
using base::UserMetricsAction;
namespace {
enum PageZoomValueType {
PAGE_ZOOM_VALUE_TYPE_FACTOR,
PAGE_ZOOM_VALUE_TYPE_LEVEL,
};
std::vector<double> PresetZoomValues(PageZoomValueType value_type,
double custom_value) {
// Generate a vector of zoom values from an array of known preset
// factors. The values in content::kPresetZoomFactors will already be in
// sorted order.
std::vector<double> zoom_values;
bool found_custom = false;
for (size_t i = 0; i < zoom::kPresetZoomFactorsSize; i++) {
double zoom_value = zoom::kPresetZoomFactors[i];
if (value_type == PAGE_ZOOM_VALUE_TYPE_LEVEL)
zoom_value = content::ZoomFactorToZoomLevel(zoom_value);
if (content::ZoomValuesEqual(zoom_value, custom_value))
found_custom = true;
zoom_values.push_back(zoom_value);
}
// If the preset array did not contain the custom value, append it to the
// vector and then sort.
double min = value_type == PAGE_ZOOM_VALUE_TYPE_LEVEL
? content::ZoomFactorToZoomLevel(content::kMinimumZoomFactor)
: content::kMinimumZoomFactor;
double max = value_type == PAGE_ZOOM_VALUE_TYPE_LEVEL
? content::ZoomFactorToZoomLevel(content::kMaximumZoomFactor)
: content::kMaximumZoomFactor;
if (!found_custom && custom_value > min && custom_value < max) {
zoom_values.push_back(custom_value);
std::sort(zoom_values.begin(), zoom_values.end());
}
return zoom_values;
}
} // namespace anonymous
namespace zoom {
// static
std::vector<double> PageZoom::PresetZoomFactors(double custom_factor) {
return PresetZoomValues(PAGE_ZOOM_VALUE_TYPE_FACTOR, custom_factor);
}
// static
std::vector<double> PageZoom::PresetZoomLevels(double custom_level) {
return PresetZoomValues(PAGE_ZOOM_VALUE_TYPE_LEVEL, custom_level);
}
// static
void PageZoom::Zoom(content::WebContents* web_contents,
content::PageZoom zoom) {
zoom::ZoomController* zoom_controller =
zoom::ZoomController::FromWebContents(web_contents);
if (!zoom_controller)
return;
double current_zoom_level = zoom_controller->GetZoomLevel();
double default_zoom_level = zoom_controller->GetDefaultZoomLevel();
if (zoom == content::PAGE_ZOOM_RESET) {
zoom_controller->SetZoomLevel(default_zoom_level);
web_contents->SetPageScale(1.f);
base::RecordAction(UserMetricsAction("ZoomNormal"));
return;
}
// Generate a vector of zoom levels from an array of known presets along with
// the default level added if necessary.
std::vector<double> zoom_levels = PresetZoomLevels(default_zoom_level);
if (zoom == content::PAGE_ZOOM_OUT) {
// Iterate through the zoom levels in reverse order to find the next
// lower level based on the current zoom level for this page.
for (std::vector<double>::reverse_iterator i = zoom_levels.rbegin();
i != zoom_levels.rend(); ++i) {
double zoom_level = *i;
if (content::ZoomValuesEqual(zoom_level, current_zoom_level))
continue;
if (zoom_level < current_zoom_level) {
zoom_controller->SetZoomLevel(zoom_level);
base::RecordAction(UserMetricsAction("ZoomMinus"));
return;
}
}
base::RecordAction(UserMetricsAction("ZoomMinus_AtMinimum"));
} else {
// Iterate through the zoom levels in normal order to find the next
// higher level based on the current zoom level for this page.
for (std::vector<double>::const_iterator i = zoom_levels.begin();
i != zoom_levels.end(); ++i) {
double zoom_level = *i;
if (content::ZoomValuesEqual(zoom_level, current_zoom_level))
continue;
if (zoom_level > current_zoom_level) {
zoom_controller->SetZoomLevel(zoom_level);
base::RecordAction(UserMetricsAction("ZoomPlus"));
return;
}
}
base::RecordAction(UserMetricsAction("ZoomPlus_AtMaximum"));
}
}
} // namespace zoom
|