Update mojo surfaces bindings and mojo/cc/ glue
[chromium-blink-merge.git] / chrome / browser / chrome_page_zoom.cc
blob5e76cfff5aa70f1b5bb442be9132a79c12d234ee
1 // Copyright (c) 2012 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 "chrome/browser/chrome_page_zoom.h"
7 #include <algorithm>
8 #include <cmath>
10 #include "base/prefs/pref_service.h"
11 #include "chrome/browser/chrome_page_zoom_constants.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/ui/zoom/zoom_controller.h"
14 #include "chrome/common/pref_names.h"
15 #include "content/public/browser/host_zoom_map.h"
16 #include "content/public/browser/render_view_host.h"
17 #include "content/public/browser/user_metrics.h"
18 #include "content/public/browser/web_contents.h"
19 #include "content/public/common/page_zoom.h"
20 #include "content/public/common/renderer_preferences.h"
22 using base::UserMetricsAction;
24 namespace chrome_page_zoom {
26 enum PageZoomValueType {
27 PAGE_ZOOM_VALUE_TYPE_FACTOR,
28 PAGE_ZOOM_VALUE_TYPE_LEVEL,
31 std::vector<double> PresetZoomValues(PageZoomValueType value_type,
32 double custom_value) {
33 // Generate a vector of zoom values from an array of known preset
34 // factors. The values in content::kPresetZoomFactors will already be in
35 // sorted order.
36 std::vector<double> zoom_values;
37 bool found_custom = false;
38 for (size_t i = 0; i < kPresetZoomFactorsSize; i++) {
39 double zoom_value = kPresetZoomFactors[i];
40 if (value_type == PAGE_ZOOM_VALUE_TYPE_LEVEL)
41 zoom_value = content::ZoomFactorToZoomLevel(zoom_value);
42 if (content::ZoomValuesEqual(zoom_value, custom_value))
43 found_custom = true;
44 zoom_values.push_back(zoom_value);
46 // If the preset array did not contain the custom value, append it to the
47 // vector and then sort.
48 double min = value_type == PAGE_ZOOM_VALUE_TYPE_LEVEL ?
49 content::ZoomFactorToZoomLevel(content::kMinimumZoomFactor) :
50 content::kMinimumZoomFactor;
51 double max = value_type == PAGE_ZOOM_VALUE_TYPE_LEVEL ?
52 content::ZoomFactorToZoomLevel(content::kMaximumZoomFactor) :
53 content::kMaximumZoomFactor;
54 if (!found_custom && custom_value > min && custom_value < max) {
55 zoom_values.push_back(custom_value);
56 std::sort(zoom_values.begin(), zoom_values.end());
58 return zoom_values;
61 std::vector<double> PresetZoomFactors(double custom_factor) {
62 return PresetZoomValues(PAGE_ZOOM_VALUE_TYPE_FACTOR, custom_factor);
65 std::vector<double> PresetZoomLevels(double custom_level) {
66 return PresetZoomValues(PAGE_ZOOM_VALUE_TYPE_LEVEL, custom_level);
69 void Zoom(content::WebContents* web_contents, content::PageZoom zoom) {
70 ZoomController* zoom_controller =
71 ZoomController::FromWebContents(web_contents);
72 DCHECK(zoom_controller);
74 double current_zoom_level = zoom_controller->GetZoomLevel();
75 double default_zoom_level =
76 Profile::FromBrowserContext(web_contents->GetBrowserContext())->
77 GetPrefs()->GetDouble(prefs::kDefaultZoomLevel);
79 if (zoom == content::PAGE_ZOOM_RESET) {
80 zoom_controller->SetZoomLevel(default_zoom_level);
81 content::RecordAction(UserMetricsAction("ZoomNormal"));
82 return;
85 // Generate a vector of zoom levels from an array of known presets along with
86 // the default level added if necessary.
87 std::vector<double> zoom_levels = PresetZoomLevels(default_zoom_level);
89 if (zoom == content::PAGE_ZOOM_OUT) {
90 // Iterate through the zoom levels in reverse order to find the next
91 // lower level based on the current zoom level for this page.
92 for (std::vector<double>::reverse_iterator i = zoom_levels.rbegin();
93 i != zoom_levels.rend(); ++i) {
94 double zoom_level = *i;
95 if (content::ZoomValuesEqual(zoom_level, current_zoom_level))
96 continue;
97 if (zoom_level < current_zoom_level) {
98 zoom_controller->SetZoomLevel(zoom_level);
99 content::RecordAction(UserMetricsAction("ZoomMinus"));
100 return;
102 content::RecordAction(UserMetricsAction("ZoomMinus_AtMinimum"));
104 } else {
105 // Iterate through the zoom levels in normal order to find the next
106 // higher level based on the current zoom level for this page.
107 for (std::vector<double>::const_iterator i = zoom_levels.begin();
108 i != zoom_levels.end(); ++i) {
109 double zoom_level = *i;
110 if (content::ZoomValuesEqual(zoom_level, current_zoom_level))
111 continue;
112 if (zoom_level > current_zoom_level) {
113 zoom_controller->SetZoomLevel(zoom_level);
114 content::RecordAction(UserMetricsAction("ZoomPlus"));
115 return;
118 content::RecordAction(UserMetricsAction("ZoomPlus_AtMaximum"));
122 } // namespace chrome_page_zoom