[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 | |
juncai | a64f769c2 | 2016-06-04 00:55:32 | [diff] [blame] | 5 | #ifndef COMPONENTS_ZOOM_ZOOM_CONTROLLER_H_ |
| 6 | #define COMPONENTS_ZOOM_ZOOM_CONTROLLER_H_ |
[email protected] | 6f80e93 | 2012-06-04 19:00:07 | [diff] [blame] | 7 | |
dcheng | 3f767dc3 | 2016-04-25 22:54:22 | [diff] [blame] | 8 | #include <memory> |
| 9 | |
[email protected] | 6f80e93 | 2012-06-04 19:00:07 | [diff] [blame] | 10 | #include "base/compiler_specific.h" |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame] | 11 | #include "base/macros.h" |
wjmaclean | 7f63c6b | 2014-12-09 14:59:55 | [diff] [blame] | 12 | #include "base/memory/ref_counted.h" |
[email protected] | 63d1f9b | 2014-07-05 19:09:03 | [diff] [blame] | 13 | #include "base/observer_list.h" |
brettw | f00b9b4 | 2016-02-01 22:11:38 | [diff] [blame] | 14 | #include "components/prefs/pref_member.h" |
[email protected] | 89c9aca | 2013-02-07 15:08:42 | [diff] [blame] | 15 | #include "content/public/browser/host_zoom_map.h" |
[email protected] | 6f80e93 | 2012-06-04 19:00:07 | [diff] [blame] | 16 | #include "content/public/browser/web_contents_observer.h" |
[email protected] | 46b3c98 | 2012-10-09 18:38:30 | [diff] [blame] | 17 | #include "content/public/browser/web_contents_user_data.h" |
[email protected] | 6f80e93 | 2012-06-04 19:00:07 | [diff] [blame] | 18 | |
wjmaclean | 7f63c6b | 2014-12-09 14:59:55 | [diff] [blame] | 19 | class ZoomControllerTest; |
[email protected] | ff3d94e | 2012-12-18 07:16:31 | [diff] [blame] | 20 | |
[email protected] | dea9cee8 | 2012-09-25 03:10:52 | [diff] [blame] | 21 | namespace content { |
| 22 | class WebContents; |
| 23 | } |
| 24 | |
juncai | a64f769c2 | 2016-06-04 00:55:32 | [diff] [blame] | 25 | namespace zoom { |
wjmaclean | 7f63c6b | 2014-12-09 14:59:55 | [diff] [blame] | 26 | class ZoomObserver; |
| 27 | |
| 28 | class ZoomRequestClient : public base::RefCounted<ZoomRequestClient> { |
| 29 | public: |
| 30 | ZoomRequestClient() {} |
wjmaclean | 70eb6d83 | 2015-10-29 21:10:09 | [diff] [blame] | 31 | virtual bool ShouldSuppressBubble() const = 0; |
wjmaclean | 7f63c6b | 2014-12-09 14:59:55 | [diff] [blame] | 32 | |
| 33 | protected: |
| 34 | virtual ~ZoomRequestClient() {} |
| 35 | |
| 36 | private: |
| 37 | friend class base::RefCounted<ZoomRequestClient>; |
| 38 | |
| 39 | DISALLOW_COPY_AND_ASSIGN(ZoomRequestClient); |
| 40 | }; |
[email protected] | 63d1f9b | 2014-07-05 19:09:03 | [diff] [blame] | 41 | |
Lei Zhang | 9d40e790 | 2017-11-20 19:54:36 | [diff] [blame] | 42 | // Per-tab class to manage zoom changes and the Omnibox zoom icon. Lives on the |
| 43 | // UI thread. |
[email protected] | 89c9aca | 2013-02-07 15:08:42 | [diff] [blame] | 44 | class ZoomController : public content::WebContentsObserver, |
[email protected] | 46b3c98 | 2012-10-09 18:38:30 | [diff] [blame] | 45 | public content::WebContentsUserData<ZoomController> { |
[email protected] | 6f80e93 | 2012-06-04 19:00:07 | [diff] [blame] | 46 | public: |
[email protected] | 63d1f9b | 2014-07-05 19:09:03 | [diff] [blame] | 47 | // Defines how zoom changes are handled. |
| 48 | enum ZoomMode { |
| 49 | // Results in default zoom behavior, i.e. zoom changes are handled |
| 50 | // automatically and on a per-origin basis, meaning that other tabs |
| 51 | // navigated to the same origin will also zoom. |
| 52 | ZOOM_MODE_DEFAULT, |
| 53 | // Results in zoom changes being handled automatically, but on a per-tab |
| 54 | // basis. Tabs in this zoom mode will not be affected by zoom changes in |
| 55 | // other tabs, and vice versa. |
| 56 | ZOOM_MODE_ISOLATED, |
| 57 | // Overrides the automatic handling of zoom changes. The |onZoomChange| |
| 58 | // event will still be dispatched, but the page will not actually be zoomed. |
| 59 | // These zoom changes can be handled manually by listening for the |
| 60 | // |onZoomChange| event. Zooming in this mode is also on a per-tab basis. |
| 61 | ZOOM_MODE_MANUAL, |
paulmeyer | 65703af | 2015-04-01 19:33:48 | [diff] [blame] | 62 | // Disables all zooming in this tab. The tab will revert to the default |
| 63 | // zoom level, and all attempted zoom changes will be ignored. |
[email protected] | 63d1f9b | 2014-07-05 19:09:03 | [diff] [blame] | 64 | ZOOM_MODE_DISABLED, |
| 65 | }; |
| 66 | |
wjmaclean | 7f63c6b | 2014-12-09 14:59:55 | [diff] [blame] | 67 | enum RelativeZoom { |
| 68 | ZOOM_BELOW_DEFAULT_ZOOM, |
| 69 | ZOOM_AT_DEFAULT_ZOOM, |
| 70 | ZOOM_ABOVE_DEFAULT_ZOOM |
| 71 | }; |
| 72 | |
[email protected] | 63d1f9b | 2014-07-05 19:09:03 | [diff] [blame] | 73 | struct ZoomChangedEventData { |
| 74 | ZoomChangedEventData(content::WebContents* web_contents, |
| 75 | double old_zoom_level, |
| 76 | double new_zoom_level, |
| 77 | ZoomController::ZoomMode zoom_mode, |
| 78 | bool can_show_bubble) |
| 79 | : web_contents(web_contents), |
| 80 | old_zoom_level(old_zoom_level), |
| 81 | new_zoom_level(new_zoom_level), |
| 82 | zoom_mode(zoom_mode), |
| 83 | can_show_bubble(can_show_bubble) {} |
| 84 | content::WebContents* web_contents; |
| 85 | double old_zoom_level; |
| 86 | double new_zoom_level; |
| 87 | ZoomController::ZoomMode zoom_mode; |
| 88 | bool can_show_bubble; |
| 89 | }; |
| 90 | |
wjmaclean | bd73117 | 2015-03-31 16:07:59 | [diff] [blame] | 91 | // Since it's possible for a WebContents to not have a ZoomController, provide |
| 92 | // a simple, safe and reliable method to find the current zoom level for a |
| 93 | // given WebContents*. |
Lucas Furukawa Gadani | e1c5dfda | 2018-11-29 17:57:41 | [diff] [blame] | 94 | static double GetZoomLevelForWebContents(content::WebContents* web_contents); |
wjmaclean | bd73117 | 2015-03-31 16:07:59 | [diff] [blame] | 95 | |
dcheng | 5dd5ff6 | 2014-10-21 12:42:38 | [diff] [blame] | 96 | ~ZoomController() override; |
[email protected] | 6f80e93 | 2012-06-04 19:00:07 | [diff] [blame] | 97 | |
[email protected] | 63d1f9b | 2014-07-05 19:09:03 | [diff] [blame] | 98 | ZoomMode zoom_mode() const { return zoom_mode_; } |
[email protected] | 6f80e93 | 2012-06-04 19:00:07 | [diff] [blame] | 99 | |
wjmaclean | 32a991c | 2014-09-02 14:13:47 | [diff] [blame] | 100 | // Convenience method to get default zoom level. Implemented here for |
| 101 | // inlining. |
| 102 | double GetDefaultZoomLevel() const { |
wjmaclean | caa7d6d | 2014-11-12 16:42:11 | [diff] [blame] | 103 | return content::HostZoomMap::GetForWebContents(web_contents()) |
| 104 | ->GetDefaultZoomLevel(); |
wjmaclean | 32a991c | 2014-09-02 14:13:47 | [diff] [blame] | 105 | } |
| 106 | |
[email protected] | 5423c37 | 2012-08-22 05:50:16 | [diff] [blame] | 107 | // Convenience method to quickly check if the tab's at default zoom. |
shrike | c12fa88b | 2016-10-14 18:21:59 | [diff] [blame] | 108 | // Virtual for testing. |
| 109 | virtual bool IsAtDefaultZoom() const; |
[email protected] | 5423c37 | 2012-08-22 05:50:16 | [diff] [blame] | 110 | |
| 111 | // Returns which image should be loaded for the current zoom level. |
wjmaclean | 7f63c6b | 2014-12-09 14:59:55 | [diff] [blame] | 112 | RelativeZoom GetZoomRelativeToDefault() const; |
[email protected] | 5423c37 | 2012-08-22 05:50:16 | [diff] [blame] | 113 | |
wjmaclean | 7f63c6b | 2014-12-09 14:59:55 | [diff] [blame] | 114 | const ZoomRequestClient* last_client() const { return last_client_.get(); } |
[email protected] | 63d1f9b | 2014-07-05 19:09:03 | [diff] [blame] | 115 | |
| 116 | void AddObserver(ZoomObserver* observer); |
| 117 | void RemoveObserver(ZoomObserver* observer); |
| 118 | |
[email protected] | d27c6526 | 2014-07-10 02:51:54 | [diff] [blame] | 119 | // Used to set whether the zoom notification bubble can be shown when the |
| 120 | // zoom level is changed for this controller. Default behavior is to show |
| 121 | // the bubble. |
| 122 | void SetShowsNotificationBubble(bool can_show_bubble) { |
| 123 | can_show_bubble_ = can_show_bubble; |
| 124 | } |
| 125 | |
[email protected] | 63d1f9b | 2014-07-05 19:09:03 | [diff] [blame] | 126 | // Gets the current zoom level by querying HostZoomMap (if not in manual zoom |
| 127 | // mode) or from the ZoomController local value otherwise. |
| 128 | double GetZoomLevel() const; |
| 129 | // Calls GetZoomLevel() then converts the returned value to a percentage |
| 130 | // zoom factor. |
[email protected] | 21267678 | 2014-08-11 17:50:18 | [diff] [blame] | 131 | // Virtual for testing. |
| 132 | virtual int GetZoomPercent() const; |
[email protected] | 63d1f9b | 2014-07-05 19:09:03 | [diff] [blame] | 133 | |
| 134 | // Sets the zoom level through HostZoomMap. |
| 135 | // Returns true on success. |
| 136 | bool SetZoomLevel(double zoom_level); |
| 137 | |
| 138 | // Sets the zoom level via HostZoomMap (or stores it locally if in manual zoom |
wjmaclean | 7f63c6b | 2014-12-09 14:59:55 | [diff] [blame] | 139 | // mode), and attributes the zoom to |client|. Returns true on success. |
| 140 | bool SetZoomLevelByClient( |
[email protected] | 63d1f9b | 2014-07-05 19:09:03 | [diff] [blame] | 141 | double zoom_level, |
wjmaclean | 7f63c6b | 2014-12-09 14:59:55 | [diff] [blame] | 142 | const scoped_refptr<const ZoomRequestClient>& client); |
[email protected] | 63d1f9b | 2014-07-05 19:09:03 | [diff] [blame] | 143 | |
| 144 | // Sets the zoom mode, which defines zoom behavior (see enum ZoomMode). |
| 145 | void SetZoomMode(ZoomMode zoom_mode); |
[email protected] | 6f80e93 | 2012-06-04 19:00:07 | [diff] [blame] | 146 | |
ccameron | b7c1d6c | 2015-03-09 17:08:24 | [diff] [blame] | 147 | // Set and query whether or not the page scale factor is one. |
| 148 | void SetPageScaleFactorIsOneForTesting(bool is_one); |
| 149 | bool PageScaleFactorIsOne() const; |
| 150 | |
[email protected] | 6f80e93 | 2012-06-04 19:00:07 | [diff] [blame] | 151 | // content::WebContentsObserver overrides: |
jam | e1f3984 | 2017-01-27 23:14:18 | [diff] [blame] | 152 | void DidFinishNavigation( |
| 153 | content::NavigationHandle* navigation_handle) override; |
dcheng | 5dd5ff6 | 2014-10-21 12:42:38 | [diff] [blame] | 154 | void WebContentsDestroyed() override; |
wjmaclean | c7bcb77d | 2014-12-16 15:05:38 | [diff] [blame] | 155 | void RenderFrameHostChanged(content::RenderFrameHost* old_host, |
| 156 | content::RenderFrameHost* new_host) override; |
[email protected] | 6f80e93 | 2012-06-04 19:00:07 | [diff] [blame] | 157 | |
[email protected] | 21267678 | 2014-08-11 17:50:18 | [diff] [blame] | 158 | protected: |
| 159 | // Protected for testing. |
[email protected] | 550fd377 | 2012-11-20 01:06:02 | [diff] [blame] | 160 | explicit ZoomController(content::WebContents* web_contents); |
[email protected] | 21267678 | 2014-08-11 17:50:18 | [diff] [blame] | 161 | |
| 162 | private: |
[email protected] | 550fd377 | 2012-11-20 01:06:02 | [diff] [blame] | 163 | friend class content::WebContentsUserData<ZoomController>; |
wjmaclean | 7f63c6b | 2014-12-09 14:59:55 | [diff] [blame] | 164 | friend class ::ZoomControllerTest; |
[email protected] | 550fd377 | 2012-11-20 01:06:02 | [diff] [blame] | 165 | |
wjmaclean | d96f864 | 2015-01-07 20:10:57 | [diff] [blame] | 166 | void ResetZoomModeOnNavigationIfNeeded(const GURL& url); |
[email protected] | 367c5c1d | 2013-03-11 18:59:02 | [diff] [blame] | 167 | void OnZoomLevelChanged(const content::HostZoomMap::ZoomLevelChange& change); |
[email protected] | 89c9aca | 2013-02-07 15:08:42 | [diff] [blame] | 168 | |
[email protected] | 6f80e93 | 2012-06-04 19:00:07 | [diff] [blame] | 169 | // Updates the zoom icon and zoom percentage based on current values and |
[email protected] | 9a840890 | 2012-09-26 16:17:59 | [diff] [blame] | 170 | // notifies the observer if changes have occurred. |host| may be empty, |
| 171 | // 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] | 172 | // change only affects sites with the given host. |
| 173 | void UpdateState(const std::string& host); |
[email protected] | 6f80e93 | 2012-06-04 19:00:07 | [diff] [blame] | 174 | |
[email protected] | d27c6526 | 2014-07-10 02:51:54 | [diff] [blame] | 175 | // True if changes to zoom level can trigger the zoom notification bubble. |
| 176 | bool can_show_bubble_; |
| 177 | |
[email protected] | 63d1f9b | 2014-07-05 19:09:03 | [diff] [blame] | 178 | // The current zoom mode. |
| 179 | ZoomMode zoom_mode_; |
| 180 | |
| 181 | // Current zoom level. |
| 182 | double zoom_level_; |
[email protected] | 6f80e93 | 2012-06-04 19:00:07 | [diff] [blame] | 183 | |
dcheng | 3f767dc3 | 2016-04-25 22:54:22 | [diff] [blame] | 184 | std::unique_ptr<ZoomChangedEventData> event_data_; |
[email protected] | 63d1f9b | 2014-07-05 19:09:03 | [diff] [blame] | 185 | |
| 186 | // Keeps track of the extension (if any) that initiated the last zoom change |
| 187 | // that took effect. |
wjmaclean | 7f63c6b | 2014-12-09 14:59:55 | [diff] [blame] | 188 | scoped_refptr<const ZoomRequestClient> last_client_; |
[email protected] | 63d1f9b | 2014-07-05 19:09:03 | [diff] [blame] | 189 | |
[email protected] | 6f80e93 | 2012-06-04 19:00:07 | [diff] [blame] | 190 | // Observer receiving notifications on state changes. |
Trent Apted | a250ec3ab | 2018-08-19 08:52:19 | [diff] [blame] | 191 | base::ObserverList<ZoomObserver>::Unchecked observers_; |
[email protected] | 6f80e93 | 2012-06-04 19:00:07 | [diff] [blame] | 192 | |
[email protected] | 89c9aca | 2013-02-07 15:08:42 | [diff] [blame] | 193 | content::BrowserContext* browser_context_; |
wjmaclean | c7bcb77d | 2014-12-16 15:05:38 | [diff] [blame] | 194 | // Keep track of the HostZoomMap we're currently subscribed to. |
| 195 | content::HostZoomMap* host_zoom_map_; |
[email protected] | 89c9aca | 2013-02-07 15:08:42 | [diff] [blame] | 196 | |
Peter Kasting | 7ba9440c | 2020-11-22 01:49:02 | [diff] [blame^] | 197 | base::CallbackListSubscription zoom_subscription_; |
[email protected] | 89c9aca | 2013-02-07 15:08:42 | [diff] [blame] | 198 | |
François Doray | 4f51d5d | 2018-12-03 22:26:24 | [diff] [blame] | 199 | WEB_CONTENTS_USER_DATA_KEY_DECL(); |
| 200 | |
[email protected] | 6f80e93 | 2012-06-04 19:00:07 | [diff] [blame] | 201 | DISALLOW_COPY_AND_ASSIGN(ZoomController); |
| 202 | }; |
| 203 | |
juncai | a64f769c2 | 2016-06-04 00:55:32 | [diff] [blame] | 204 | } // namespace zoom |
wjmaclean | 7f63c6b | 2014-12-09 14:59:55 | [diff] [blame] | 205 | |
juncai | a64f769c2 | 2016-06-04 00:55:32 | [diff] [blame] | 206 | #endif // COMPONENTS_ZOOM_ZOOM_CONTROLLER_H_ |