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