| OLD | NEW |
| (Empty) |
| 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_ | |
| 7 | |
| 8 #include "base/basictypes.h" | |
| 9 #include "base/compiler_specific.h" | |
| 10 #include "base/memory/scoped_ptr.h" | |
| 11 #include "base/observer_list.h" | |
| 12 #include "base/prefs/pref_member.h" | |
| 13 #include "content/public/browser/host_zoom_map.h" | |
| 14 #include "content/public/browser/web_contents_observer.h" | |
| 15 #include "content/public/browser/web_contents_user_data.h" | |
| 16 | |
| 17 class ZoomObserver; | |
| 18 | |
| 19 namespace content { | |
| 20 class WebContents; | |
| 21 } | |
| 22 | |
| 23 namespace extensions { | |
| 24 class Extension; | |
| 25 } // namespace extensions | |
| 26 | |
| 27 // Per-tab class to manage zoom changes and the Omnibox zoom icon. | |
| 28 class ZoomController : public content::WebContentsObserver, | |
| 29 public content::WebContentsUserData<ZoomController> { | |
| 30 public: | |
| 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 | |
| 69 ~ZoomController() override; | |
| 70 | |
| 71 ZoomMode zoom_mode() const { return zoom_mode_; } | |
| 72 | |
| 73 // Convenience method to get default zoom level. Implemented here for | |
| 74 // inlining. | |
| 75 double GetDefaultZoomLevel() const { | |
| 76 return content::HostZoomMap::GetForWebContents(web_contents()) | |
| 77 ->GetDefaultZoomLevel(); | |
| 78 } | |
| 79 | |
| 80 // Convenience method to quickly check if the tab's at default zoom. | |
| 81 bool IsAtDefaultZoom() const; | |
| 82 | |
| 83 // Returns which image should be loaded for the current zoom level. | |
| 84 int GetResourceForZoomLevel() const; | |
| 85 | |
| 86 const extensions::Extension* last_extension() const { | |
| 87 return last_extension_.get(); | |
| 88 } | |
| 89 | |
| 90 void AddObserver(ZoomObserver* observer); | |
| 91 void RemoveObserver(ZoomObserver* observer); | |
| 92 | |
| 93 // Used to set whether the zoom notification bubble can be shown when the | |
| 94 // zoom level is changed for this controller. Default behavior is to show | |
| 95 // the bubble. | |
| 96 void SetShowsNotificationBubble(bool can_show_bubble) { | |
| 97 can_show_bubble_ = can_show_bubble; | |
| 98 } | |
| 99 | |
| 100 // Gets the current zoom level by querying HostZoomMap (if not in manual zoom | |
| 101 // mode) or from the ZoomController local value otherwise. | |
| 102 double GetZoomLevel() const; | |
| 103 // Calls GetZoomLevel() then converts the returned value to a percentage | |
| 104 // zoom factor. | |
| 105 // Virtual for testing. | |
| 106 virtual int GetZoomPercent() const; | |
| 107 | |
| 108 // Sets the zoom level through HostZoomMap. | |
| 109 // Returns true on success. | |
| 110 bool SetZoomLevel(double zoom_level); | |
| 111 | |
| 112 // Sets the zoom level via HostZoomMap (or stores it locally if in manual zoom | |
| 113 // mode), and attributes the zoom to |extension|. Returns true on success. | |
| 114 bool SetZoomLevelByExtension( | |
| 115 double zoom_level, | |
| 116 const scoped_refptr<const extensions::Extension>& extension); | |
| 117 | |
| 118 // Sets the zoom mode, which defines zoom behavior (see enum ZoomMode). | |
| 119 void SetZoomMode(ZoomMode zoom_mode); | |
| 120 | |
| 121 // content::WebContentsObserver overrides: | |
| 122 void DidNavigateMainFrame( | |
| 123 const content::LoadCommittedDetails& details, | |
| 124 const content::FrameNavigateParams& params) override; | |
| 125 void WebContentsDestroyed() override; | |
| 126 | |
| 127 protected: | |
| 128 // Protected for testing. | |
| 129 explicit ZoomController(content::WebContents* web_contents); | |
| 130 | |
| 131 private: | |
| 132 friend class content::WebContentsUserData<ZoomController>; | |
| 133 friend class ZoomControllerTest; | |
| 134 | |
| 135 void OnZoomLevelChanged(const content::HostZoomMap::ZoomLevelChange& change); | |
| 136 | |
| 137 // Updates the zoom icon and zoom percentage based on current values and | |
| 138 // notifies the observer if changes have occurred. |host| may be empty, | |
| 139 // meaning the change should apply to ~all sites. If it is not empty, the | |
| 140 // change only affects sites with the given host. | |
| 141 void UpdateState(const std::string& host); | |
| 142 | |
| 143 // True if changes to zoom level can trigger the zoom notification bubble. | |
| 144 bool can_show_bubble_; | |
| 145 | |
| 146 // The current zoom mode. | |
| 147 ZoomMode zoom_mode_; | |
| 148 | |
| 149 // Current zoom level. | |
| 150 double zoom_level_; | |
| 151 | |
| 152 scoped_ptr<ZoomChangedEventData> event_data_; | |
| 153 | |
| 154 // Keeps track of the extension (if any) that initiated the last zoom change | |
| 155 // that took effect. | |
| 156 scoped_refptr<const extensions::Extension> last_extension_; | |
| 157 | |
| 158 // Observer receiving notifications on state changes. | |
| 159 ObserverList<ZoomObserver> observers_; | |
| 160 | |
| 161 content::BrowserContext* browser_context_; | |
| 162 | |
| 163 scoped_ptr<content::HostZoomMap::Subscription> zoom_subscription_; | |
| 164 | |
| 165 DISALLOW_COPY_AND_ASSIGN(ZoomController); | |
| 166 }; | |
| 167 | |
| 168 #endif // CHROME_BROWSER_UI_ZOOM_ZOOM_CONTROLLER_H_ | |
| OLD | NEW |