[email protected] | 6f80e93 | 2012-06-04 19:00:07 | [diff] [blame] | 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 | #ifndef CHROME_BROWSER_UI_ZOOM_ZOOM_CONTROLLER_H_ |
| 6 | #define CHROME_BROWSER_UI_ZOOM_ZOOM_CONTROLLER_H_ |
[email protected] | 6f80e93 | 2012-06-04 19:00:07 | [diff] [blame] | 7 | |
| 8 | #include "base/basictypes.h" |
| 9 | #include "base/compiler_specific.h" |
[email protected] | e5c63687 | 2014-07-08 12:07:40 | [diff] [blame] | 10 | #include "base/memory/scoped_ptr.h" |
[email protected] | 63d1f9b | 2014-07-05 19:09:03 | [diff] [blame] | 11 | #include "base/observer_list.h" |
[email protected] | 1ab137b | 2013-03-21 03:33:18 | [diff] [blame] | 12 | #include "base/prefs/pref_member.h" |
[email protected] | 89c9aca | 2013-02-07 15:08:42 | [diff] [blame] | 13 | #include "content/public/browser/host_zoom_map.h" |
[email protected] | 6f80e93 | 2012-06-04 19:00:07 | [diff] [blame] | 14 | #include "content/public/browser/web_contents_observer.h" |
[email protected] | 46b3c98 | 2012-10-09 18:38:30 | [diff] [blame] | 15 | #include "content/public/browser/web_contents_user_data.h" |
[email protected] | 6f80e93 | 2012-06-04 19:00:07 | [diff] [blame] | 16 | |
[email protected] | ff3d94e | 2012-12-18 07:16:31 | [diff] [blame] | 17 | class ZoomObserver; |
| 18 | |
[email protected] | dea9cee8 | 2012-09-25 03:10:52 | [diff] [blame] | 19 | namespace content { |
| 20 | class WebContents; |
| 21 | } |
| 22 | |
[email protected] | 63d1f9b | 2014-07-05 19:09:03 | [diff] [blame] | 23 | namespace extensions { |
| 24 | class Extension; |
| 25 | } // namespace extensions |
| 26 | |
| 27 | // Per-tab class to manage zoom changes and the Omnibox zoom icon. |
[email protected] | 89c9aca | 2013-02-07 15:08:42 | [diff] [blame] | 28 | class ZoomController : public content::WebContentsObserver, |
[email protected] | 46b3c98 | 2012-10-09 18:38:30 | [diff] [blame] | 29 | public content::WebContentsUserData<ZoomController> { |
[email protected] | 6f80e93 | 2012-06-04 19:00:07 | [diff] [blame] | 30 | public: |
[email protected] | 63d1f9b | 2014-07-05 19:09:03 | [diff] [blame] | 31 | // Defines how zoom changes are handled. |
| 32 | enum ZoomMode { |
| 33 | // Results in default zoom behavior, i.e. zoom changes are handled |
| 34 | // automatically and on a per-origin basis, meaning that other tabs |
| 35 | // navigated to the same origin will also zoom. |
| 36 | ZOOM_MODE_DEFAULT, |
| 37 | // Results in zoom changes being handled automatically, but on a per-tab |
| 38 | // basis. Tabs in this zoom mode will not be affected by zoom changes in |
| 39 | // other tabs, and vice versa. |
| 40 | ZOOM_MODE_ISOLATED, |
| 41 | // Overrides the automatic handling of zoom changes. The |onZoomChange| |
| 42 | // event will still be dispatched, but the page will not actually be zoomed. |
| 43 | // These zoom changes can be handled manually by listening for the |
| 44 | // |onZoomChange| event. Zooming in this mode is also on a per-tab basis. |
| 45 | ZOOM_MODE_MANUAL, |
| 46 | // Disables all zooming in this tab. The tab will revert to default (100%) |
| 47 | // zoom, and all attempted zoom changes will be ignored. |
| 48 | ZOOM_MODE_DISABLED, |
| 49 | }; |
| 50 | |
| 51 | struct ZoomChangedEventData { |
| 52 | ZoomChangedEventData(content::WebContents* web_contents, |
| 53 | double old_zoom_level, |
| 54 | double new_zoom_level, |
| 55 | ZoomController::ZoomMode zoom_mode, |
| 56 | bool can_show_bubble) |
| 57 | : web_contents(web_contents), |
| 58 | old_zoom_level(old_zoom_level), |
| 59 | new_zoom_level(new_zoom_level), |
| 60 | zoom_mode(zoom_mode), |
| 61 | can_show_bubble(can_show_bubble) {} |
| 62 | content::WebContents* web_contents; |
| 63 | double old_zoom_level; |
| 64 | double new_zoom_level; |
| 65 | ZoomController::ZoomMode zoom_mode; |
| 66 | bool can_show_bubble; |
| 67 | }; |
| 68 | |
[email protected] | 6f80e93 | 2012-06-04 19:00:07 | [diff] [blame] | 69 | virtual ~ZoomController(); |
| 70 | |
[email protected] | 63d1f9b | 2014-07-05 19:09:03 | [diff] [blame] | 71 | ZoomMode zoom_mode() const { return zoom_mode_; } |
[email protected] | 6f80e93 | 2012-06-04 19:00:07 | [diff] [blame] | 72 | |
[email protected] | 5423c37 | 2012-08-22 05:50:16 | [diff] [blame] | 73 | // Convenience method to quickly check if the tab's at default zoom. |
| 74 | bool IsAtDefaultZoom() const; |
| 75 | |
| 76 | // Returns which image should be loaded for the current zoom level. |
| 77 | int GetResourceForZoomLevel() const; |
| 78 | |
[email protected] | 63d1f9b | 2014-07-05 19:09:03 | [diff] [blame] | 79 | const extensions::Extension* last_extension() const { |
| 80 | return last_extension_.get(); |
| 81 | } |
| 82 | |
| 83 | void AddObserver(ZoomObserver* observer); |
| 84 | void RemoveObserver(ZoomObserver* observer); |
| 85 | |
[email protected] | d27c6526 | 2014-07-10 02:51:54 | [diff] [blame^] | 86 | // Used to set whether the zoom notification bubble can be shown when the |
| 87 | // zoom level is changed for this controller. Default behavior is to show |
| 88 | // the bubble. |
| 89 | void SetShowsNotificationBubble(bool can_show_bubble) { |
| 90 | can_show_bubble_ = can_show_bubble; |
| 91 | } |
| 92 | |
[email protected] | 63d1f9b | 2014-07-05 19:09:03 | [diff] [blame] | 93 | // Gets the current zoom level by querying HostZoomMap (if not in manual zoom |
| 94 | // mode) or from the ZoomController local value otherwise. |
| 95 | double GetZoomLevel() const; |
| 96 | // Calls GetZoomLevel() then converts the returned value to a percentage |
| 97 | // zoom factor. |
| 98 | int GetZoomPercent() const; |
| 99 | |
| 100 | // Sets the zoom level through HostZoomMap. |
| 101 | // Returns true on success. |
| 102 | bool SetZoomLevel(double zoom_level); |
| 103 | |
| 104 | // Sets the zoom level via HostZoomMap (or stores it locally if in manual zoom |
| 105 | // mode), and attributes the zoom to |extension|. Returns true on success. |
| 106 | bool SetZoomLevelByExtension( |
| 107 | double zoom_level, |
| 108 | const scoped_refptr<const extensions::Extension>& extension); |
| 109 | |
| 110 | // Sets the zoom mode, which defines zoom behavior (see enum ZoomMode). |
| 111 | void SetZoomMode(ZoomMode zoom_mode); |
[email protected] | 6f80e93 | 2012-06-04 19:00:07 | [diff] [blame] | 112 | |
[email protected] | 6f80e93 | 2012-06-04 19:00:07 | [diff] [blame] | 113 | // content::WebContentsObserver overrides: |
| 114 | virtual void DidNavigateMainFrame( |
| 115 | const content::LoadCommittedDetails& details, |
| 116 | const content::FrameNavigateParams& params) OVERRIDE; |
| 117 | |
[email protected] | 550fd377 | 2012-11-20 01:06:02 | [diff] [blame] | 118 | private: |
| 119 | explicit ZoomController(content::WebContents* web_contents); |
| 120 | friend class content::WebContentsUserData<ZoomController>; |
| 121 | friend class ZoomControllerTest; |
| 122 | |
[email protected] | 367c5c1d | 2013-03-11 18:59:02 | [diff] [blame] | 123 | void OnZoomLevelChanged(const content::HostZoomMap::ZoomLevelChange& change); |
[email protected] | 89c9aca | 2013-02-07 15:08:42 | [diff] [blame] | 124 | |
[email protected] | 6f80e93 | 2012-06-04 19:00:07 | [diff] [blame] | 125 | // Updates the zoom icon and zoom percentage based on current values and |
[email protected] | 9a840890 | 2012-09-26 16:17:59 | [diff] [blame] | 126 | // notifies the observer if changes have occurred. |host| may be empty, |
| 127 | // meaning the change should apply to ~all sites. If it is not empty, the |
[email protected] | 4ff2b5e | 2013-07-13 03:39:54 | [diff] [blame] | 128 | // change only affects sites with the given host. |
| 129 | void UpdateState(const std::string& host); |
[email protected] | 6f80e93 | 2012-06-04 19:00:07 | [diff] [blame] | 130 | |
[email protected] | d27c6526 | 2014-07-10 02:51:54 | [diff] [blame^] | 131 | // True if changes to zoom level can trigger the zoom notification bubble. |
| 132 | bool can_show_bubble_; |
| 133 | |
[email protected] | 63d1f9b | 2014-07-05 19:09:03 | [diff] [blame] | 134 | // The current zoom mode. |
| 135 | ZoomMode zoom_mode_; |
| 136 | |
| 137 | // Current zoom level. |
| 138 | double zoom_level_; |
[email protected] | 6f80e93 | 2012-06-04 19:00:07 | [diff] [blame] | 139 | |
[email protected] | 6f80e93 | 2012-06-04 19:00:07 | [diff] [blame] | 140 | // Used to access the default zoom level preference. |
| 141 | DoublePrefMember default_zoom_level_; |
| 142 | |
[email protected] | e5c63687 | 2014-07-08 12:07:40 | [diff] [blame] | 143 | scoped_ptr<ZoomChangedEventData> event_data_; |
[email protected] | 63d1f9b | 2014-07-05 19:09:03 | [diff] [blame] | 144 | |
| 145 | // Keeps track of the extension (if any) that initiated the last zoom change |
| 146 | // that took effect. |
| 147 | scoped_refptr<const extensions::Extension> last_extension_; |
| 148 | |
[email protected] | 6f80e93 | 2012-06-04 19:00:07 | [diff] [blame] | 149 | // Observer receiving notifications on state changes. |
[email protected] | 63d1f9b | 2014-07-05 19:09:03 | [diff] [blame] | 150 | ObserverList<ZoomObserver> observers_; |
[email protected] | 6f80e93 | 2012-06-04 19:00:07 | [diff] [blame] | 151 | |
[email protected] | 89c9aca | 2013-02-07 15:08:42 | [diff] [blame] | 152 | content::BrowserContext* browser_context_; |
| 153 | |
[email protected] | 11783281 | 2013-10-02 07:06:02 | [diff] [blame] | 154 | scoped_ptr<content::HostZoomMap::Subscription> zoom_subscription_; |
[email protected] | 89c9aca | 2013-02-07 15:08:42 | [diff] [blame] | 155 | |
[email protected] | 6f80e93 | 2012-06-04 19:00:07 | [diff] [blame] | 156 | DISALLOW_COPY_AND_ASSIGN(ZoomController); |
| 157 | }; |
| 158 | |
| 159 | #endif // CHROME_BROWSER_UI_ZOOM_ZOOM_CONTROLLER_H_ |