Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / ui / zoom / page_zoom.cc
blob4d7811aa8aea944c7bd4aa471f36ab5fb5e4b639
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/ui/zoom/page_zoom.h"
7 #include <algorithm>
8 #include <cmath>
10 #include "base/prefs/pref_service.h"
11 #include "components/ui/zoom/page_zoom_constants.h"
12 #include "components/ui/zoom/zoom_controller.h"
13 #include "content/public/browser/host_zoom_map.h"
14 #include "content/public/browser/render_view_host.h"
15 #include "content/public/browser/user_metrics.h"
16 #include "content/public/browser/web_contents.h"
17 #include "content/public/common/page_zoom.h"
18 #include "content/public/common/renderer_preferences.h"
20 using base::UserMetricsAction;
22 namespace {
24 enum PageZoomValueType {
25 PAGE_ZOOM_VALUE_TYPE_FACTOR,
26 PAGE_ZOOM_VALUE_TYPE_LEVEL,
29 std::vector<double> PresetZoomValues(PageZoomValueType value_type,
30 double custom_value) {
31 // Generate a vector of zoom values from an array of known preset
32 // factors. The values in content::kPresetZoomFactors will already be in
33 // sorted order.
34 std::vector<double> zoom_values;
35 bool found_custom = false;
36 for (size_t i = 0; i < ui_zoom::kPresetZoomFactorsSize; i++) {
37 double zoom_value = ui_zoom::kPresetZoomFactors[i];
38 if (value_type == PAGE_ZOOM_VALUE_TYPE_LEVEL)
39 zoom_value = content::ZoomFactorToZoomLevel(zoom_value);
40 if (content::ZoomValuesEqual(zoom_value, custom_value))
41 found_custom = true;
42 zoom_values.push_back(zoom_value);
44 // If the preset array did not contain the custom value, append it to the
45 // vector and then sort.
46 double min = value_type == PAGE_ZOOM_VALUE_TYPE_LEVEL ?
47 content::ZoomFactorToZoomLevel(content::kMinimumZoomFactor) :
48 content::kMinimumZoomFactor;
49 double max = value_type == PAGE_ZOOM_VALUE_TYPE_LEVEL ?
50 content::ZoomFactorToZoomLevel(content::kMaximumZoomFactor) :
51 content::kMaximumZoomFactor;
52 if (!found_custom && custom_value > min && custom_value < max) {
53 zoom_values.push_back(custom_value);
54 std::sort(zoom_values.begin(), zoom_values.end());
56 return zoom_values;
59 } // namespace anonymous
61 namespace ui_zoom {
63 // static
64 std::vector<double> PageZoom::PresetZoomFactors(double custom_factor) {
65 return PresetZoomValues(PAGE_ZOOM_VALUE_TYPE_FACTOR, custom_factor);
68 // static
69 std::vector<double> PageZoom::PresetZoomLevels(double custom_level) {
70 return PresetZoomValues(PAGE_ZOOM_VALUE_TYPE_LEVEL, custom_level);
73 // static
74 void PageZoom::Zoom(content::WebContents* web_contents,
75 content::PageZoom zoom) {
76 ui_zoom::ZoomController* zoom_controller =
77 ui_zoom::ZoomController::FromWebContents(web_contents);
78 if (!zoom_controller)
79 return;
81 double current_zoom_level = zoom_controller->GetZoomLevel();
82 double default_zoom_level = zoom_controller->GetDefaultZoomLevel();
84 if (zoom == content::PAGE_ZOOM_RESET) {
85 zoom_controller->SetZoomLevel(default_zoom_level);
86 web_contents->ResetPageScale();
87 content::RecordAction(UserMetricsAction("ZoomNormal"));
88 return;
91 // Generate a vector of zoom levels from an array of known presets along with
92 // the default level added if necessary.
93 std::vector<double> zoom_levels = PresetZoomLevels(default_zoom_level);
95 if (zoom == content::PAGE_ZOOM_OUT) {
96 // Iterate through the zoom levels in reverse order to find the next
97 // lower level based on the current zoom level for this page.
98 for (std::vector<double>::reverse_iterator i = zoom_levels.rbegin();
99 i != zoom_levels.rend(); ++i) {
100 double zoom_level = *i;
101 if (content::ZoomValuesEqual(zoom_level, current_zoom_level))
102 continue;
103 if (zoom_level < current_zoom_level) {
104 zoom_controller->SetZoomLevel(zoom_level);
105 content::RecordAction(UserMetricsAction("ZoomMinus"));
106 return;
108 content::RecordAction(UserMetricsAction("ZoomMinus_AtMinimum"));
110 } else {
111 // Iterate through the zoom levels in normal order to find the next
112 // higher level based on the current zoom level for this page.
113 for (std::vector<double>::const_iterator i = zoom_levels.begin();
114 i != zoom_levels.end(); ++i) {
115 double zoom_level = *i;
116 if (content::ZoomValuesEqual(zoom_level, current_zoom_level))
117 continue;
118 if (zoom_level > current_zoom_level) {
119 zoom_controller->SetZoomLevel(zoom_level);
120 content::RecordAction(UserMetricsAction("ZoomPlus"));
121 return;
124 content::RecordAction(UserMetricsAction("ZoomPlus_AtMaximum"));
128 } // namespace ui_zoom