GoogleURLTrackerInfoBarDelegate: Initialize uninitialized member in constructor.
[chromium-blink-merge.git] / chrome / browser / chrome_page_zoom.cc
blob6e534d90ca5aea983d8e9af10e94aae5c0085014
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/common/pref_names.h"
14 #include "content/public/browser/host_zoom_map.h"
15 #include "content/public/browser/render_view_host.h"
16 #include "content/public/browser/user_metrics.h"
17 #include "content/public/browser/web_contents.h"
18 #include "content/public/common/page_zoom.h"
19 #include "content/public/common/renderer_preferences.h"
21 using base::UserMetricsAction;
23 namespace chrome_page_zoom {
25 enum PageZoomValueType {
26 PAGE_ZOOM_VALUE_TYPE_FACTOR,
27 PAGE_ZOOM_VALUE_TYPE_LEVEL,
30 std::vector<double> PresetZoomValues(PageZoomValueType value_type,
31 double custom_value) {
32 // Generate a vector of zoom values from an array of known preset
33 // factors. The values in content::kPresetZoomFactors will already be in
34 // sorted order.
35 std::vector<double> zoom_values;
36 bool found_custom = false;
37 for (size_t i = 0; i < kPresetZoomFactorsSize; i++) {
38 double zoom_value = kPresetZoomFactors[i];
39 if (value_type == PAGE_ZOOM_VALUE_TYPE_LEVEL)
40 zoom_value = content::ZoomFactorToZoomLevel(zoom_value);
41 if (content::ZoomValuesEqual(zoom_value, custom_value))
42 found_custom = true;
43 zoom_values.push_back(zoom_value);
45 // If the preset array did not contain the custom value, append it to the
46 // vector and then sort.
47 double min = value_type == PAGE_ZOOM_VALUE_TYPE_LEVEL ?
48 content::ZoomFactorToZoomLevel(content::kMinimumZoomFactor) :
49 content::kMinimumZoomFactor;
50 double max = value_type == PAGE_ZOOM_VALUE_TYPE_LEVEL ?
51 content::ZoomFactorToZoomLevel(content::kMaximumZoomFactor) :
52 content::kMaximumZoomFactor;
53 if (!found_custom && custom_value > min && custom_value < max) {
54 zoom_values.push_back(custom_value);
55 std::sort(zoom_values.begin(), zoom_values.end());
57 return zoom_values;
60 std::vector<double> PresetZoomFactors(double custom_factor) {
61 return PresetZoomValues(PAGE_ZOOM_VALUE_TYPE_FACTOR, custom_factor);
64 std::vector<double> PresetZoomLevels(double custom_level) {
65 return PresetZoomValues(PAGE_ZOOM_VALUE_TYPE_LEVEL, custom_level);
68 void Zoom(content::WebContents* web_contents, content::PageZoom zoom) {
69 double current_zoom_level = content::HostZoomMap::GetZoomLevel(web_contents);
70 double default_zoom_level =
71 Profile::FromBrowserContext(web_contents->GetBrowserContext())->
72 GetPrefs()->GetDouble(prefs::kDefaultZoomLevel);
74 if (zoom == content::PAGE_ZOOM_RESET) {
75 content::HostZoomMap::SetZoomLevel(web_contents, default_zoom_level);
76 content::RecordAction(UserMetricsAction("ZoomNormal"));
77 return;
80 // Generate a vector of zoom levels from an array of known presets along with
81 // the default level added if necessary.
82 std::vector<double> zoom_levels = PresetZoomLevels(default_zoom_level);
84 if (zoom == content::PAGE_ZOOM_OUT) {
85 // Iterate through the zoom levels in reverse order to find the next
86 // lower level based on the current zoom level for this page.
87 for (std::vector<double>::reverse_iterator i = zoom_levels.rbegin();
88 i != zoom_levels.rend(); ++i) {
89 double zoom_level = *i;
90 if (content::ZoomValuesEqual(zoom_level, current_zoom_level))
91 continue;
92 if (zoom_level < current_zoom_level) {
93 content::HostZoomMap::SetZoomLevel(web_contents, zoom_level);
94 content::RecordAction(UserMetricsAction("ZoomMinus"));
95 return;
97 content::RecordAction(UserMetricsAction("ZoomMinus_AtMinimum"));
99 } else {
100 // Iterate through the zoom levels in normal order to find the next
101 // higher level based on the current zoom level for this page.
102 for (std::vector<double>::const_iterator i = zoom_levels.begin();
103 i != zoom_levels.end(); ++i) {
104 double zoom_level = *i;
105 if (content::ZoomValuesEqual(zoom_level, current_zoom_level))
106 continue;
107 if (zoom_level > current_zoom_level) {
108 content::HostZoomMap::SetZoomLevel(web_contents, zoom_level);
109 content::RecordAction(UserMetricsAction("ZoomPlus"));
110 return;
113 content::RecordAction(UserMetricsAction("ZoomPlus_AtMaximum"));
117 } // namespace chrome_page_zoom