OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_UI_ZOOM_ZOOM_CONTROLLER_H_ | 5 #ifndef COMPONENTS_UI_ZOOM_ZOOM_CONTROLLER_H_ |
6 #define CHROME_BROWSER_UI_ZOOM_ZOOM_CONTROLLER_H_ | 6 #define COMPONENTS_UI_ZOOM_ZOOM_CONTROLLER_H_ |
7 | 7 |
8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 10 #include "base/memory/ref_counted.h" |
10 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
11 #include "base/observer_list.h" | 12 #include "base/observer_list.h" |
12 #include "base/prefs/pref_member.h" | 13 #include "base/prefs/pref_member.h" |
13 #include "content/public/browser/host_zoom_map.h" | 14 #include "content/public/browser/host_zoom_map.h" |
14 #include "content/public/browser/web_contents_observer.h" | 15 #include "content/public/browser/web_contents_observer.h" |
15 #include "content/public/browser/web_contents_user_data.h" | 16 #include "content/public/browser/web_contents_user_data.h" |
16 | 17 |
17 class ZoomObserver; | 18 class ZoomControllerTest; |
18 | 19 |
19 namespace content { | 20 namespace content { |
20 class WebContents; | 21 class WebContents; |
21 } | 22 } |
22 | 23 |
23 namespace extensions { | 24 namespace ui_zoom { |
24 class Extension; | 25 class ZoomObserver; |
25 } // namespace extensions | 26 |
| 27 class ZoomRequestClient : public base::RefCounted<ZoomRequestClient> { |
| 28 public: |
| 29 ZoomRequestClient() {} |
| 30 |
| 31 protected: |
| 32 virtual ~ZoomRequestClient() {} |
| 33 |
| 34 private: |
| 35 friend class base::RefCounted<ZoomRequestClient>; |
| 36 |
| 37 DISALLOW_COPY_AND_ASSIGN(ZoomRequestClient); |
| 38 }; |
26 | 39 |
27 // Per-tab class to manage zoom changes and the Omnibox zoom icon. | 40 // Per-tab class to manage zoom changes and the Omnibox zoom icon. |
28 class ZoomController : public content::WebContentsObserver, | 41 class ZoomController : public content::WebContentsObserver, |
29 public content::WebContentsUserData<ZoomController> { | 42 public content::WebContentsUserData<ZoomController> { |
30 public: | 43 public: |
31 // Defines how zoom changes are handled. | 44 // Defines how zoom changes are handled. |
32 enum ZoomMode { | 45 enum ZoomMode { |
33 // Results in default zoom behavior, i.e. zoom changes are handled | 46 // Results in default zoom behavior, i.e. zoom changes are handled |
34 // automatically and on a per-origin basis, meaning that other tabs | 47 // automatically and on a per-origin basis, meaning that other tabs |
35 // navigated to the same origin will also zoom. | 48 // navigated to the same origin will also zoom. |
36 ZOOM_MODE_DEFAULT, | 49 ZOOM_MODE_DEFAULT, |
37 // Results in zoom changes being handled automatically, but on a per-tab | 50 // 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 | 51 // basis. Tabs in this zoom mode will not be affected by zoom changes in |
39 // other tabs, and vice versa. | 52 // other tabs, and vice versa. |
40 ZOOM_MODE_ISOLATED, | 53 ZOOM_MODE_ISOLATED, |
41 // Overrides the automatic handling of zoom changes. The |onZoomChange| | 54 // Overrides the automatic handling of zoom changes. The |onZoomChange| |
42 // event will still be dispatched, but the page will not actually be zoomed. | 55 // 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 | 56 // 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. | 57 // |onZoomChange| event. Zooming in this mode is also on a per-tab basis. |
45 ZOOM_MODE_MANUAL, | 58 ZOOM_MODE_MANUAL, |
46 // Disables all zooming in this tab. The tab will revert to default (100%) | 59 // Disables all zooming in this tab. The tab will revert to default (100%) |
47 // zoom, and all attempted zoom changes will be ignored. | 60 // zoom, and all attempted zoom changes will be ignored. |
48 ZOOM_MODE_DISABLED, | 61 ZOOM_MODE_DISABLED, |
49 }; | 62 }; |
50 | 63 |
| 64 enum RelativeZoom { |
| 65 ZOOM_BELOW_DEFAULT_ZOOM, |
| 66 ZOOM_AT_DEFAULT_ZOOM, |
| 67 ZOOM_ABOVE_DEFAULT_ZOOM |
| 68 }; |
| 69 |
51 struct ZoomChangedEventData { | 70 struct ZoomChangedEventData { |
52 ZoomChangedEventData(content::WebContents* web_contents, | 71 ZoomChangedEventData(content::WebContents* web_contents, |
53 double old_zoom_level, | 72 double old_zoom_level, |
54 double new_zoom_level, | 73 double new_zoom_level, |
55 ZoomController::ZoomMode zoom_mode, | 74 ZoomController::ZoomMode zoom_mode, |
56 bool can_show_bubble) | 75 bool can_show_bubble) |
57 : web_contents(web_contents), | 76 : web_contents(web_contents), |
58 old_zoom_level(old_zoom_level), | 77 old_zoom_level(old_zoom_level), |
59 new_zoom_level(new_zoom_level), | 78 new_zoom_level(new_zoom_level), |
60 zoom_mode(zoom_mode), | 79 zoom_mode(zoom_mode), |
(...skipping 13 matching lines...) Expand all Loading... |
74 // inlining. | 93 // inlining. |
75 double GetDefaultZoomLevel() const { | 94 double GetDefaultZoomLevel() const { |
76 return content::HostZoomMap::GetForWebContents(web_contents()) | 95 return content::HostZoomMap::GetForWebContents(web_contents()) |
77 ->GetDefaultZoomLevel(); | 96 ->GetDefaultZoomLevel(); |
78 } | 97 } |
79 | 98 |
80 // Convenience method to quickly check if the tab's at default zoom. | 99 // Convenience method to quickly check if the tab's at default zoom. |
81 bool IsAtDefaultZoom() const; | 100 bool IsAtDefaultZoom() const; |
82 | 101 |
83 // Returns which image should be loaded for the current zoom level. | 102 // Returns which image should be loaded for the current zoom level. |
84 int GetResourceForZoomLevel() const; | 103 RelativeZoom GetZoomRelativeToDefault() const; |
85 | 104 |
86 const extensions::Extension* last_extension() const { | 105 const ZoomRequestClient* last_client() const { return last_client_.get(); } |
87 return last_extension_.get(); | |
88 } | |
89 | 106 |
90 void AddObserver(ZoomObserver* observer); | 107 void AddObserver(ZoomObserver* observer); |
91 void RemoveObserver(ZoomObserver* observer); | 108 void RemoveObserver(ZoomObserver* observer); |
92 | 109 |
93 // Used to set whether the zoom notification bubble can be shown when the | 110 // 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 | 111 // zoom level is changed for this controller. Default behavior is to show |
95 // the bubble. | 112 // the bubble. |
96 void SetShowsNotificationBubble(bool can_show_bubble) { | 113 void SetShowsNotificationBubble(bool can_show_bubble) { |
97 can_show_bubble_ = can_show_bubble; | 114 can_show_bubble_ = can_show_bubble; |
98 } | 115 } |
99 | 116 |
100 // Gets the current zoom level by querying HostZoomMap (if not in manual zoom | 117 // Gets the current zoom level by querying HostZoomMap (if not in manual zoom |
101 // mode) or from the ZoomController local value otherwise. | 118 // mode) or from the ZoomController local value otherwise. |
102 double GetZoomLevel() const; | 119 double GetZoomLevel() const; |
103 // Calls GetZoomLevel() then converts the returned value to a percentage | 120 // Calls GetZoomLevel() then converts the returned value to a percentage |
104 // zoom factor. | 121 // zoom factor. |
105 // Virtual for testing. | 122 // Virtual for testing. |
106 virtual int GetZoomPercent() const; | 123 virtual int GetZoomPercent() const; |
107 | 124 |
108 // Sets the zoom level through HostZoomMap. | 125 // Sets the zoom level through HostZoomMap. |
109 // Returns true on success. | 126 // Returns true on success. |
110 bool SetZoomLevel(double zoom_level); | 127 bool SetZoomLevel(double zoom_level); |
111 | 128 |
112 // Sets the zoom level via HostZoomMap (or stores it locally if in manual zoom | 129 // 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. | 130 // mode), and attributes the zoom to |client|. Returns true on success. |
114 bool SetZoomLevelByExtension( | 131 bool SetZoomLevelByClient( |
115 double zoom_level, | 132 double zoom_level, |
116 const scoped_refptr<const extensions::Extension>& extension); | 133 const scoped_refptr<const ZoomRequestClient>& client); |
117 | 134 |
118 // Sets the zoom mode, which defines zoom behavior (see enum ZoomMode). | 135 // Sets the zoom mode, which defines zoom behavior (see enum ZoomMode). |
119 void SetZoomMode(ZoomMode zoom_mode); | 136 void SetZoomMode(ZoomMode zoom_mode); |
120 | 137 |
121 // content::WebContentsObserver overrides: | 138 // content::WebContentsObserver overrides: |
122 void DidNavigateMainFrame( | 139 void DidNavigateMainFrame( |
123 const content::LoadCommittedDetails& details, | 140 const content::LoadCommittedDetails& details, |
124 const content::FrameNavigateParams& params) override; | 141 const content::FrameNavigateParams& params) override; |
125 void WebContentsDestroyed() override; | 142 void WebContentsDestroyed() override; |
126 | 143 |
127 protected: | 144 protected: |
128 // Protected for testing. | 145 // Protected for testing. |
129 explicit ZoomController(content::WebContents* web_contents); | 146 explicit ZoomController(content::WebContents* web_contents); |
130 | 147 |
131 private: | 148 private: |
132 friend class content::WebContentsUserData<ZoomController>; | 149 friend class content::WebContentsUserData<ZoomController>; |
133 friend class ZoomControllerTest; | 150 friend class ::ZoomControllerTest; |
134 | 151 |
135 void OnZoomLevelChanged(const content::HostZoomMap::ZoomLevelChange& change); | 152 void OnZoomLevelChanged(const content::HostZoomMap::ZoomLevelChange& change); |
136 | 153 |
137 // Updates the zoom icon and zoom percentage based on current values and | 154 // Updates the zoom icon and zoom percentage based on current values and |
138 // notifies the observer if changes have occurred. |host| may be empty, | 155 // 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 | 156 // meaning the change should apply to ~all sites. If it is not empty, the |
140 // change only affects sites with the given host. | 157 // change only affects sites with the given host. |
141 void UpdateState(const std::string& host); | 158 void UpdateState(const std::string& host); |
142 | 159 |
143 // True if changes to zoom level can trigger the zoom notification bubble. | 160 // True if changes to zoom level can trigger the zoom notification bubble. |
144 bool can_show_bubble_; | 161 bool can_show_bubble_; |
145 | 162 |
146 // The current zoom mode. | 163 // The current zoom mode. |
147 ZoomMode zoom_mode_; | 164 ZoomMode zoom_mode_; |
148 | 165 |
149 // Current zoom level. | 166 // Current zoom level. |
150 double zoom_level_; | 167 double zoom_level_; |
151 | 168 |
152 scoped_ptr<ZoomChangedEventData> event_data_; | 169 scoped_ptr<ZoomChangedEventData> event_data_; |
153 | 170 |
154 // Keeps track of the extension (if any) that initiated the last zoom change | 171 // Keeps track of the extension (if any) that initiated the last zoom change |
155 // that took effect. | 172 // that took effect. |
156 scoped_refptr<const extensions::Extension> last_extension_; | 173 scoped_refptr<const ZoomRequestClient> last_client_; |
157 | 174 |
158 // Observer receiving notifications on state changes. | 175 // Observer receiving notifications on state changes. |
159 ObserverList<ZoomObserver> observers_; | 176 ObserverList<ZoomObserver> observers_; |
160 | 177 |
161 content::BrowserContext* browser_context_; | 178 content::BrowserContext* browser_context_; |
162 | 179 |
163 scoped_ptr<content::HostZoomMap::Subscription> zoom_subscription_; | 180 scoped_ptr<content::HostZoomMap::Subscription> zoom_subscription_; |
164 | 181 |
165 DISALLOW_COPY_AND_ASSIGN(ZoomController); | 182 DISALLOW_COPY_AND_ASSIGN(ZoomController); |
166 }; | 183 }; |
167 | 184 |
168 #endif // CHROME_BROWSER_UI_ZOOM_ZOOM_CONTROLLER_H_ | 185 } // namespace ui_zoom |
| 186 |
| 187 #endif // COMPONENTS_UI_ZOOM_ZOOM_CONTROLLER_H_ |
OLD | NEW |