| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "components/ui/zoom/page_zoom.h" | |
| 6 | |
| 7 #include <stddef.h> | |
| 8 | |
| 9 #include <algorithm> | |
| 10 #include <cmath> | |
| 11 | |
| 12 #include "components/prefs/pref_service.h" | |
| 13 #include "components/ui/zoom/page_zoom_constants.h" | |
| 14 #include "components/ui/zoom/zoom_controller.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" | |
| 21 | |
| 22 using base::UserMetricsAction; | |
| 23 | |
| 24 namespace { | |
| 25 | |
| 26 enum PageZoomValueType { | |
| 27 PAGE_ZOOM_VALUE_TYPE_FACTOR, | |
| 28 PAGE_ZOOM_VALUE_TYPE_LEVEL, | |
| 29 }; | |
| 30 | |
| 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 < ui_zoom::kPresetZoomFactorsSize; i++) { | |
| 39 double zoom_value = ui_zoom::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); | |
| 45 } | |
| 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()); | |
| 57 } | |
| 58 return zoom_values; | |
| 59 } | |
| 60 | |
| 61 } // namespace anonymous | |
| 62 | |
| 63 namespace ui_zoom { | |
| 64 | |
| 65 // static | |
| 66 std::vector<double> PageZoom::PresetZoomFactors(double custom_factor) { | |
| 67 return PresetZoomValues(PAGE_ZOOM_VALUE_TYPE_FACTOR, custom_factor); | |
| 68 } | |
| 69 | |
| 70 // static | |
| 71 std::vector<double> PageZoom::PresetZoomLevels(double custom_level) { | |
| 72 return PresetZoomValues(PAGE_ZOOM_VALUE_TYPE_LEVEL, custom_level); | |
| 73 } | |
| 74 | |
| 75 // static | |
| 76 void PageZoom::Zoom(content::WebContents* web_contents, | |
| 77 content::PageZoom zoom) { | |
| 78 ui_zoom::ZoomController* zoom_controller = | |
| 79 ui_zoom::ZoomController::FromWebContents(web_contents); | |
| 80 if (!zoom_controller) | |
| 81 return; | |
| 82 | |
| 83 double current_zoom_level = zoom_controller->GetZoomLevel(); | |
| 84 double default_zoom_level = zoom_controller->GetDefaultZoomLevel(); | |
| 85 | |
| 86 if (zoom == content::PAGE_ZOOM_RESET) { | |
| 87 zoom_controller->SetZoomLevel(default_zoom_level); | |
| 88 web_contents->SetPageScale(1.f); | |
| 89 content::RecordAction(UserMetricsAction("ZoomNormal")); | |
| 90 return; | |
| 91 } | |
| 92 | |
| 93 // Generate a vector of zoom levels from an array of known presets along with | |
| 94 // the default level added if necessary. | |
| 95 std::vector<double> zoom_levels = PresetZoomLevels(default_zoom_level); | |
| 96 | |
| 97 if (zoom == content::PAGE_ZOOM_OUT) { | |
| 98 // Iterate through the zoom levels in reverse order to find the next | |
| 99 // lower level based on the current zoom level for this page. | |
| 100 for (std::vector<double>::reverse_iterator i = zoom_levels.rbegin(); | |
| 101 i != zoom_levels.rend(); ++i) { | |
| 102 double zoom_level = *i; | |
| 103 if (content::ZoomValuesEqual(zoom_level, current_zoom_level)) | |
| 104 continue; | |
| 105 if (zoom_level < current_zoom_level) { | |
| 106 zoom_controller->SetZoomLevel(zoom_level); | |
| 107 content::RecordAction(UserMetricsAction("ZoomMinus")); | |
| 108 return; | |
| 109 } | |
| 110 } | |
| 111 content::RecordAction(UserMetricsAction("ZoomMinus_AtMinimum")); | |
| 112 } else { | |
| 113 // Iterate through the zoom levels in normal order to find the next | |
| 114 // higher level based on the current zoom level for this page. | |
| 115 for (std::vector<double>::const_iterator i = zoom_levels.begin(); | |
| 116 i != zoom_levels.end(); ++i) { | |
| 117 double zoom_level = *i; | |
| 118 if (content::ZoomValuesEqual(zoom_level, current_zoom_level)) | |
| 119 continue; | |
| 120 if (zoom_level > current_zoom_level) { | |
| 121 zoom_controller->SetZoomLevel(zoom_level); | |
| 122 content::RecordAction(UserMetricsAction("ZoomPlus")); | |
| 123 return; | |
| 124 } | |
| 125 } | |
| 126 content::RecordAction(UserMetricsAction("ZoomPlus_AtMaximum")); | |
| 127 } | |
| 128 } | |
| 129 | |
| 130 } // namespace ui_zoom | |
| OLD | NEW |