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