diff options
author | W. James MacLean <wjmaclean@chromium.org> | 2015-01-20 09:13:50 -0500 |
---|---|---|
committer | W. James MacLean <wjmaclean@chromium.org> | 2015-01-20 14:15:05 +0000 |
commit | 8e9c02da125a76c1fcc6171e11c9863cbf709d07 (patch) | |
tree | 17cd556420ca2b9b5324bd592efb89a4eadcd32d | |
parent | 86948ef332b3eb11aa1cf67a2ce3935f4b91e788 (diff) | |
download | chromium_src-8e9c02da125a76c1fcc6171e11c9863cbf709d07.zip chromium_src-8e9c02da125a76c1fcc6171e11c9863cbf709d07.tar.gz chromium_src-8e9c02da125a76c1fcc6171e11c9863cbf709d07.tar.bz2 |
Patch to merge.
Cr-Commit-Position: refs/branch-heads/2272@{#57}
Cr-Branched-From: 827a380cfdb31aa54c8d56e63ce2c3fd8c3ba4d4-refs/heads/master@{#310958}
12 files changed, 227 insertions, 15 deletions
diff --git a/chrome/browser/guest_view/mime_handler_view/chrome_mime_handler_view_guest_delegate.cc b/chrome/browser/guest_view/mime_handler_view/chrome_mime_handler_view_guest_delegate.cc index 00a3003..a959c49 100644 --- a/chrome/browser/guest_view/mime_handler_view/chrome_mime_handler_view_guest_delegate.cc +++ b/chrome/browser/guest_view/mime_handler_view/chrome_mime_handler_view_guest_delegate.cc @@ -47,14 +47,6 @@ void ChromeMimeHandlerViewGuestDelegate::AttachHelpers() { new ChromePDFWebContentsHelperClient())); } -void ChromeMimeHandlerViewGuestDelegate::ChangeZoom(bool zoom_in) { - // TODO(lazyboy): Move this to //extensions once ZoomController and friends - // move to //extensions. - chrome_page_zoom::Zoom( - guest_->embedder_web_contents(), - zoom_in ? content::PAGE_ZOOM_IN : content::PAGE_ZOOM_OUT); -} - bool ChromeMimeHandlerViewGuestDelegate::HandleContextMenu( content::WebContents* web_contents, const content::ContextMenuParams& params) { diff --git a/chrome/browser/guest_view/mime_handler_view/chrome_mime_handler_view_guest_delegate.h b/chrome/browser/guest_view/mime_handler_view/chrome_mime_handler_view_guest_delegate.h index eaa88bf..d29168a 100644 --- a/chrome/browser/guest_view/mime_handler_view/chrome_mime_handler_view_guest_delegate.h +++ b/chrome/browser/guest_view/mime_handler_view/chrome_mime_handler_view_guest_delegate.h @@ -23,7 +23,6 @@ class ChromeMimeHandlerViewGuestDelegate : public MimeHandlerViewGuestDelegate { void AttachHelpers() override; bool HandleContextMenu(content::WebContents* web_contents, const content::ContextMenuParams& params) override; - void ChangeZoom(bool zoom_in) override; private: MimeHandlerViewGuest* guest_; // Owns us. diff --git a/components/ui/zoom/BUILD.gn b/components/ui/zoom/BUILD.gn index 2cefb39..a3958b8 100644 --- a/components/ui/zoom/BUILD.gn +++ b/components/ui/zoom/BUILD.gn @@ -4,6 +4,10 @@ static_library("ui_zoom") { sources = [ + "page_zoom.cc", + "page_zoom.h", + "page_zoom_constants.cc", + "page_zoom_constants.h", "zoom_controller.cc", "zoom_controller.h", "zoom_event_manager.cc", diff --git a/components/ui/zoom/page_zoom.cc b/components/ui/zoom/page_zoom.cc new file mode 100644 index 0000000..487e306 --- /dev/null +++ b/components/ui/zoom/page_zoom.cc @@ -0,0 +1,127 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "components/ui/zoom/page_zoom.h" + +#include <algorithm> +#include <cmath> + +#include "base/prefs/pref_service.h" +#include "components/ui/zoom/page_zoom_constants.h" +#include "components/ui/zoom/zoom_controller.h" +#include "content/public/browser/host_zoom_map.h" +#include "content/public/browser/render_view_host.h" +#include "content/public/browser/user_metrics.h" +#include "content/public/browser/web_contents.h" +#include "content/public/common/page_zoom.h" +#include "content/public/common/renderer_preferences.h" + +using base::UserMetricsAction; + +namespace { + +enum PageZoomValueType { + PAGE_ZOOM_VALUE_TYPE_FACTOR, + PAGE_ZOOM_VALUE_TYPE_LEVEL, +}; + +std::vector<double> PresetZoomValues(PageZoomValueType value_type, + double custom_value) { + // Generate a vector of zoom values from an array of known preset + // factors. The values in content::kPresetZoomFactors will already be in + // sorted order. + std::vector<double> zoom_values; + bool found_custom = false; + for (size_t i = 0; i < ui_zoom::kPresetZoomFactorsSize; i++) { + double zoom_value = ui_zoom::kPresetZoomFactors[i]; + if (value_type == PAGE_ZOOM_VALUE_TYPE_LEVEL) + zoom_value = content::ZoomFactorToZoomLevel(zoom_value); + if (content::ZoomValuesEqual(zoom_value, custom_value)) + found_custom = true; + zoom_values.push_back(zoom_value); + } + // If the preset array did not contain the custom value, append it to the + // vector and then sort. + double min = value_type == PAGE_ZOOM_VALUE_TYPE_LEVEL ? + content::ZoomFactorToZoomLevel(content::kMinimumZoomFactor) : + content::kMinimumZoomFactor; + double max = value_type == PAGE_ZOOM_VALUE_TYPE_LEVEL ? + content::ZoomFactorToZoomLevel(content::kMaximumZoomFactor) : + content::kMaximumZoomFactor; + if (!found_custom && custom_value > min && custom_value < max) { + zoom_values.push_back(custom_value); + std::sort(zoom_values.begin(), zoom_values.end()); + } + return zoom_values; +} + +} // namespace anonymous + +namespace ui_zoom { + +// static +std::vector<double> PageZoom::PresetZoomFactors(double custom_factor) { + return PresetZoomValues(PAGE_ZOOM_VALUE_TYPE_FACTOR, custom_factor); +} + +// static +std::vector<double> PageZoom::PresetZoomLevels(double custom_level) { + return PresetZoomValues(PAGE_ZOOM_VALUE_TYPE_LEVEL, custom_level); +} + +// static +void PageZoom::Zoom(content::WebContents* web_contents, + content::PageZoom zoom) { + ui_zoom::ZoomController* zoom_controller = + ui_zoom::ZoomController::FromWebContents(web_contents); + if (!zoom_controller) + return; + + double current_zoom_level = zoom_controller->GetZoomLevel(); + double default_zoom_level = zoom_controller->GetDefaultZoomLevel(); + + if (zoom == content::PAGE_ZOOM_RESET) { + zoom_controller->SetZoomLevel(default_zoom_level); + content::RecordAction(UserMetricsAction("ZoomNormal")); + return; + } + + // Generate a vector of zoom levels from an array of known presets along with + // the default level added if necessary. + std::vector<double> zoom_levels = PresetZoomLevels(default_zoom_level); + + if (zoom == content::PAGE_ZOOM_OUT) { + // Iterate through the zoom levels in reverse order to find the next + // lower level based on the current zoom level for this page. + for (std::vector<double>::reverse_iterator i = zoom_levels.rbegin(); + i != zoom_levels.rend(); ++i) { + double zoom_level = *i; + if (content::ZoomValuesEqual(zoom_level, current_zoom_level)) + continue; + if (zoom_level < current_zoom_level) { + zoom_controller->SetZoomLevel(zoom_level); + content::RecordAction(UserMetricsAction("ZoomMinus")); + return; + } + content::RecordAction(UserMetricsAction("ZoomMinus_AtMinimum")); + } + } else { + // Iterate through the zoom levels in normal order to find the next + // higher level based on the current zoom level for this page. + for (std::vector<double>::const_iterator i = zoom_levels.begin(); + i != zoom_levels.end(); ++i) { + double zoom_level = *i; + if (content::ZoomValuesEqual(zoom_level, current_zoom_level)) + continue; + if (zoom_level > current_zoom_level) { + zoom_controller->SetZoomLevel(zoom_level); + content::RecordAction(UserMetricsAction("ZoomPlus")); + return; + } + } + content::RecordAction(UserMetricsAction("ZoomPlus_AtMaximum")); + } +} + +} // namespace ui_zoom diff --git a/components/ui/zoom/page_zoom.h b/components/ui/zoom/page_zoom.h new file mode 100644 index 0000000..1c4de10 --- /dev/null +++ b/components/ui/zoom/page_zoom.h @@ -0,0 +1,46 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef COMPONENTS_UI_ZOOM_PAGE_ZOOM_H_ +#define COMPONENTS_UI_ZOOM_PAGE_ZOOM_H_ + +#include <vector> + +#include "base/macros.h" +#include "content/public/common/page_zoom.h" + +namespace content { +class WebContents; +} + +namespace ui_zoom { + +// This class provides a means of zooming pages according to a predetermined +// set of zoom levels/factors. In future, the static methods in this class +// can be made non-static, with PresetZoomX() being virtual, to allow clients +// to create custom sets of zoom levels. +class PageZoom { + public: + // Return a sorted vector of zoom factors. The vector will consist of preset + // values along with a custom value (if the custom value is not already + // represented.) + static std::vector<double> PresetZoomFactors(double custom_factor); + + // Return a sorted vector of zoom levels. The vector will consist of preset + // values along with a custom value (if the custom value is not already + // represented.) + static std::vector<double> PresetZoomLevels(double custom_level); + + // Adjusts the zoom level of |web_contents|. + static void Zoom(content::WebContents* web_contents, content::PageZoom zoom); + + private: + // We don't expect (currently) to create instances of this class. + PageZoom() {} + DISALLOW_COPY_AND_ASSIGN(PageZoom); +}; + +} // namespace ui_zoom + +#endif // COMPONENTS_UI_ZOOM_PAGE_ZOOM_H_ diff --git a/components/ui/zoom/page_zoom_constants.cc b/components/ui/zoom/page_zoom_constants.cc new file mode 100644 index 0000000..06f62be --- /dev/null +++ b/components/ui/zoom/page_zoom_constants.cc @@ -0,0 +1,18 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "components/ui/zoom/page_zoom_constants.h" + +#include "base/macros.h" + +namespace ui_zoom { + +// This list is duplicated in chrome/browser/resources/pdf/viewport.js. Please +// make sure the two match. +const double kPresetZoomFactors[] = { 0.25, 0.333, 0.5, 0.666, 0.75, 0.9, 1.0, + 1.1, 1.25, 1.5, 1.75, 2.0, 2.5, 3.0, 4.0, + 5.0 }; +const std::size_t kPresetZoomFactorsSize = arraysize(kPresetZoomFactors); + +} // namespace ui_zoom diff --git a/components/ui/zoom/page_zoom_constants.h b/components/ui/zoom/page_zoom_constants.h new file mode 100644 index 0000000..50d9b4c --- /dev/null +++ b/components/ui/zoom/page_zoom_constants.h @@ -0,0 +1,20 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef COMPONENTS_UI_ZOOM_PAGE_ZOOM_CONSTANTS_H_ +#define COMPONENTS_UI_ZOOM_PAGE_ZOOM_CONSTANTS_H_ + +#include <vector> + +namespace ui_zoom { + +// Default zoom factors supported by ui_zoom. +extern const double kPresetZoomFactors[]; + +// Size of |kPresetZoomFactors|. +extern const std::size_t kPresetZoomFactorsSize; + +} // namespace ui_zoom + +#endif // COMPONENTS_UI_ZOOM_PAGE_ZOOM_CONSTANTS_H_ diff --git a/components/ui_zoom.gypi b/components/ui_zoom.gypi index 2b0e153..fc3ed76 100644 --- a/components/ui_zoom.gypi +++ b/components/ui_zoom.gypi @@ -19,6 +19,10 @@ '../url/url.gyp:url_lib', ], 'sources': [ + 'ui/zoom/page_zoom.cc', + 'ui/zoom/page_zoom.h', + 'ui/zoom/page_zoom_constants.cc', + 'ui/zoom/page_zoom_constants.h', 'ui/zoom/zoom_controller.cc', 'ui/zoom/zoom_controller.h', 'ui/zoom/zoom_event_manager.cc', diff --git a/extensions/browser/guest_view/guest_view_base.cc b/extensions/browser/guest_view/guest_view_base.cc index 59d75d9..9270669 100644 --- a/extensions/browser/guest_view/guest_view_base.cc +++ b/extensions/browser/guest_view/guest_view_base.cc @@ -6,6 +6,7 @@ #include "base/lazy_instance.h" #include "base/strings/utf_string_conversions.h" +#include "components/ui/zoom/page_zoom.h" #include "components/ui/zoom/zoom_controller.h" #include "content/public/browser/navigation_details.h" #include "content/public/browser/render_frame_host.h" @@ -493,6 +494,12 @@ void GuestViewBase::DeactivateContents(WebContents* web_contents) { embedder_web_contents()); } +void GuestViewBase::ContentsZoomChange(bool zoom_in) { + ui_zoom::PageZoom::Zoom( + embedder_web_contents(), + zoom_in ? content::PAGE_ZOOM_IN : content::PAGE_ZOOM_OUT); +} + void GuestViewBase::RunFileChooser(WebContents* web_contents, const content::FileChooserParams& params) { if (!attached() || !embedder_web_contents()->GetDelegate()) diff --git a/extensions/browser/guest_view/guest_view_base.h b/extensions/browser/guest_view/guest_view_base.h index e03a9e2..8bffb89 100644 --- a/extensions/browser/guest_view/guest_view_base.h +++ b/extensions/browser/guest_view/guest_view_base.h @@ -302,6 +302,7 @@ class GuestViewBase : public content::BrowserPluginGuestDelegate, // WebContentsDelegate implementation. void ActivateContents(content::WebContents* contents) final; void DeactivateContents(content::WebContents* contents) final; + void ContentsZoomChange(bool zoom_in) override; void RunFileChooser(content::WebContents* web_contents, const content::FileChooserParams& params) override; bool ShouldFocusPageAfterCrash() final; diff --git a/extensions/browser/guest_view/mime_handler_view/mime_handler_view_guest.cc b/extensions/browser/guest_view/mime_handler_view/mime_handler_view_guest.cc index 0ab7f84..3b2bd05 100644 --- a/extensions/browser/guest_view/mime_handler_view/mime_handler_view_guest.cc +++ b/extensions/browser/guest_view/mime_handler_view/mime_handler_view_guest.cc @@ -154,11 +154,6 @@ content::WebContents* MimeHandlerViewGuest::OpenURLFromTab( embedder_web_contents(), params); } -void MimeHandlerViewGuest::ContentsZoomChange(bool zoom_in) { - if (delegate_) - delegate_->ChangeZoom(zoom_in); -} - bool MimeHandlerViewGuest::HandleContextMenu( const content::ContextMenuParams& params) { if (delegate_) diff --git a/extensions/browser/guest_view/mime_handler_view/mime_handler_view_guest.h b/extensions/browser/guest_view/mime_handler_view/mime_handler_view_guest.h index 845c47c..9b78db7 100644 --- a/extensions/browser/guest_view/mime_handler_view/mime_handler_view_guest.h +++ b/extensions/browser/guest_view/mime_handler_view/mime_handler_view_guest.h @@ -48,7 +48,6 @@ class MimeHandlerViewGuest : public GuestView<MimeHandlerViewGuest>, content::WebContents* OpenURLFromTab( content::WebContents* source, const content::OpenURLParams& params) override; - void ContentsZoomChange(bool zoom_in) override; bool HandleContextMenu(const content::ContextMenuParams& params) override; void HandleKeyboardEvent( content::WebContents* source, |