Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chrome_page_zoom.cc
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.
4
5 #include "chrome/browser/chrome_page_zoom.h"
6
7 #include <algorithm>
8 #include <cmath>
9
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 "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"
20
21 using base::UserMetricsAction;
22
23 namespace chrome_page_zoom {
24
25 enum PageZoomValueType {
26   PAGE_ZOOM_VALUE_TYPE_FACTOR,
27   PAGE_ZOOM_VALUE_TYPE_LEVEL,
28 };
29
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);
44   }
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());
56   }
57   return zoom_values;
58 }
59
60 std::vector<double> PresetZoomFactors(double custom_factor) {
61   return PresetZoomValues(PAGE_ZOOM_VALUE_TYPE_FACTOR, custom_factor);
62 }
63
64 std::vector<double> PresetZoomLevels(double custom_level) {
65   return PresetZoomValues(PAGE_ZOOM_VALUE_TYPE_LEVEL, custom_level);
66 }
67
68 void Zoom(content::WebContents* web_contents, content::PageZoom zoom) {
69   ZoomController* zoom_controller =
70       ZoomController::FromWebContents(web_contents);
71   DCHECK(zoom_controller);
72
73   double current_zoom_level = zoom_controller->GetZoomLevel();
74   double default_zoom_level = zoom_controller->GetDefaultZoomLevel();
75
76   if (zoom == content::PAGE_ZOOM_RESET) {
77     zoom_controller->SetZoomLevel(default_zoom_level);
78     content::RecordAction(UserMetricsAction("ZoomNormal"));
79     return;
80   }
81
82   // Generate a vector of zoom levels from an array of known presets along with
83   // the default level added if necessary.
84   std::vector<double> zoom_levels = PresetZoomLevels(default_zoom_level);
85
86   if (zoom == content::PAGE_ZOOM_OUT) {
87     // Iterate through the zoom levels in reverse order to find the next
88     // lower level based on the current zoom level for this page.
89     for (std::vector<double>::reverse_iterator i = zoom_levels.rbegin();
90          i != zoom_levels.rend(); ++i) {
91       double zoom_level = *i;
92       if (content::ZoomValuesEqual(zoom_level, current_zoom_level))
93         continue;
94       if (zoom_level < current_zoom_level) {
95         zoom_controller->SetZoomLevel(zoom_level);
96         content::RecordAction(UserMetricsAction("ZoomMinus"));
97         return;
98       }
99       content::RecordAction(UserMetricsAction("ZoomMinus_AtMinimum"));
100     }
101   } else {
102     // Iterate through the zoom levels in normal order to find the next
103     // higher level based on the current zoom level for this page.
104     for (std::vector<double>::const_iterator i = zoom_levels.begin();
105          i != zoom_levels.end(); ++i) {
106       double zoom_level = *i;
107       if (content::ZoomValuesEqual(zoom_level, current_zoom_level))
108         continue;
109       if (zoom_level > current_zoom_level) {
110         zoom_controller->SetZoomLevel(zoom_level);
111         content::RecordAction(UserMetricsAction("ZoomPlus"));
112         return;
113       }
114     }
115     content::RecordAction(UserMetricsAction("ZoomPlus_AtMaximum"));
116   }
117 }
118
119 }  // namespace chrome_page_zoom