Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / chrome_page_zoom.cc
blob5c6c7c34f8ee16d800c9d7cc8425bb943d446b2e
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/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 chrome_page_zoom {
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 < kPresetZoomFactorsSize; i++) {
37 double zoom_value = 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 std::vector<double> PresetZoomFactors(double custom_factor) {
60 return PresetZoomValues(PAGE_ZOOM_VALUE_TYPE_FACTOR, custom_factor);
63 std::vector<double> PresetZoomLevels(double custom_level) {
64 return PresetZoomValues(PAGE_ZOOM_VALUE_TYPE_LEVEL, custom_level);
67 void Zoom(content::WebContents* web_contents, content::PageZoom zoom) {
68 double current_zoom_level = web_contents->GetZoomLevel();
69 double default_zoom_level =
70 Profile::FromBrowserContext(web_contents->GetBrowserContext())->
71 GetPrefs()->GetDouble(prefs::kDefaultZoomLevel);
73 if (zoom == content::PAGE_ZOOM_RESET) {
74 web_contents->SetZoomLevel(default_zoom_level);
75 content::RecordAction(UserMetricsAction("ZoomNormal"));
76 return;
79 // Generate a vector of zoom levels from an array of known presets along with
80 // the default level added if necessary.
81 std::vector<double> zoom_levels = PresetZoomLevels(default_zoom_level);
83 if (zoom == content::PAGE_ZOOM_OUT) {
84 // Iterate through the zoom levels in reverse order to find the next
85 // lower level based on the current zoom level for this page.
86 for (std::vector<double>::reverse_iterator i = zoom_levels.rbegin();
87 i != zoom_levels.rend(); ++i) {
88 double zoom_level = *i;
89 if (content::ZoomValuesEqual(zoom_level, current_zoom_level))
90 continue;
91 if (zoom_level < current_zoom_level) {
92 web_contents->SetZoomLevel(zoom_level);
93 content::RecordAction(UserMetricsAction("ZoomMinus"));
94 return;
96 content::RecordAction(UserMetricsAction("ZoomMinus_AtMinimum"));
98 } else {
99 // Iterate through the zoom levels in normal order to find the next
100 // higher level based on the current zoom level for this page.
101 for (std::vector<double>::const_iterator i = zoom_levels.begin();
102 i != zoom_levels.end(); ++i) {
103 double zoom_level = *i;
104 if (content::ZoomValuesEqual(zoom_level, current_zoom_level))
105 continue;
106 if (zoom_level > current_zoom_level) {
107 web_contents->SetZoomLevel(zoom_level);
108 content::RecordAction(UserMetricsAction("ZoomPlus"));
109 return;
112 content::RecordAction(UserMetricsAction("ZoomPlus_AtMaximum"));
116 } // namespace chrome_page_zoom