blob: 1f67f545cf1e0f4928533dbc7e0c06782493d964 [file] [log] [blame]
[email protected]6f80e932012-06-04 19:00:071// 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_
[email protected]6f80e932012-06-04 19:00:077
8#include "base/basictypes.h"
9#include "base/compiler_specific.h"
[email protected]e5c636872014-07-08 12:07:4010#include "base/memory/scoped_ptr.h"
[email protected]63d1f9b2014-07-05 19:09:0311#include "base/observer_list.h"
[email protected]1ab137b2013-03-21 03:33:1812#include "base/prefs/pref_member.h"
[email protected]89c9aca2013-02-07 15:08:4213#include "content/public/browser/host_zoom_map.h"
[email protected]6f80e932012-06-04 19:00:0714#include "content/public/browser/web_contents_observer.h"
[email protected]46b3c982012-10-09 18:38:3015#include "content/public/browser/web_contents_user_data.h"
[email protected]6f80e932012-06-04 19:00:0716
[email protected]ff3d94e2012-12-18 07:16:3117class ZoomObserver;
18
[email protected]dea9cee82012-09-25 03:10:5219namespace content {
20class WebContents;
21}
22
[email protected]63d1f9b2014-07-05 19:09:0323namespace extensions {
24class Extension;
25} // namespace extensions
26
27// Per-tab class to manage zoom changes and the Omnibox zoom icon.
[email protected]89c9aca2013-02-07 15:08:4228class ZoomController : public content::WebContentsObserver,
[email protected]46b3c982012-10-09 18:38:3029 public content::WebContentsUserData<ZoomController> {
[email protected]6f80e932012-06-04 19:00:0730 public:
[email protected]63d1f9b2014-07-05 19:09:0331 // 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
[email protected]6f80e932012-06-04 19:00:0769 virtual ~ZoomController();
70
[email protected]63d1f9b2014-07-05 19:09:0371 ZoomMode zoom_mode() const { return zoom_mode_; }
[email protected]6f80e932012-06-04 19:00:0772
[email protected]5423c372012-08-22 05:50:1673 // Convenience method to quickly check if the tab's at default zoom.
74 bool IsAtDefaultZoom() const;
75
76 // Returns which image should be loaded for the current zoom level.
77 int GetResourceForZoomLevel() const;
78
[email protected]63d1f9b2014-07-05 19:09:0379 const extensions::Extension* last_extension() const {
80 return last_extension_.get();
81 }
82
83 void AddObserver(ZoomObserver* observer);
84 void RemoveObserver(ZoomObserver* observer);
85
[email protected]d27c65262014-07-10 02:51:5486 // Used to set whether the zoom notification bubble can be shown when the
87 // zoom level is changed for this controller. Default behavior is to show
88 // the bubble.
89 void SetShowsNotificationBubble(bool can_show_bubble) {
90 can_show_bubble_ = can_show_bubble;
91 }
92
[email protected]63d1f9b2014-07-05 19:09:0393 // Gets the current zoom level by querying HostZoomMap (if not in manual zoom
94 // mode) or from the ZoomController local value otherwise.
95 double GetZoomLevel() const;
96 // Calls GetZoomLevel() then converts the returned value to a percentage
97 // zoom factor.
98 int GetZoomPercent() const;
99
100 // Sets the zoom level through HostZoomMap.
101 // Returns true on success.
102 bool SetZoomLevel(double zoom_level);
103
104 // Sets the zoom level via HostZoomMap (or stores it locally if in manual zoom
105 // mode), and attributes the zoom to |extension|. Returns true on success.
106 bool SetZoomLevelByExtension(
107 double zoom_level,
108 const scoped_refptr<const extensions::Extension>& extension);
109
110 // Sets the zoom mode, which defines zoom behavior (see enum ZoomMode).
111 void SetZoomMode(ZoomMode zoom_mode);
[email protected]6f80e932012-06-04 19:00:07112
[email protected]6f80e932012-06-04 19:00:07113 // content::WebContentsObserver overrides:
114 virtual void DidNavigateMainFrame(
115 const content::LoadCommittedDetails& details,
116 const content::FrameNavigateParams& params) OVERRIDE;
117
[email protected]550fd3772012-11-20 01:06:02118 private:
119 explicit ZoomController(content::WebContents* web_contents);
120 friend class content::WebContentsUserData<ZoomController>;
121 friend class ZoomControllerTest;
122
[email protected]367c5c1d2013-03-11 18:59:02123 void OnZoomLevelChanged(const content::HostZoomMap::ZoomLevelChange& change);
[email protected]89c9aca2013-02-07 15:08:42124
[email protected]6f80e932012-06-04 19:00:07125 // Updates the zoom icon and zoom percentage based on current values and
[email protected]9a8408902012-09-26 16:17:59126 // notifies the observer if changes have occurred. |host| may be empty,
127 // meaning the change should apply to ~all sites. If it is not empty, the
[email protected]4ff2b5e2013-07-13 03:39:54128 // change only affects sites with the given host.
129 void UpdateState(const std::string& host);
[email protected]6f80e932012-06-04 19:00:07130
[email protected]d27c65262014-07-10 02:51:54131 // True if changes to zoom level can trigger the zoom notification bubble.
132 bool can_show_bubble_;
133
[email protected]63d1f9b2014-07-05 19:09:03134 // The current zoom mode.
135 ZoomMode zoom_mode_;
136
137 // Current zoom level.
138 double zoom_level_;
[email protected]6f80e932012-06-04 19:00:07139
[email protected]6f80e932012-06-04 19:00:07140 // Used to access the default zoom level preference.
141 DoublePrefMember default_zoom_level_;
142
[email protected]e5c636872014-07-08 12:07:40143 scoped_ptr<ZoomChangedEventData> event_data_;
[email protected]63d1f9b2014-07-05 19:09:03144
145 // Keeps track of the extension (if any) that initiated the last zoom change
146 // that took effect.
147 scoped_refptr<const extensions::Extension> last_extension_;
148
[email protected]6f80e932012-06-04 19:00:07149 // Observer receiving notifications on state changes.
[email protected]63d1f9b2014-07-05 19:09:03150 ObserverList<ZoomObserver> observers_;
[email protected]6f80e932012-06-04 19:00:07151
[email protected]89c9aca2013-02-07 15:08:42152 content::BrowserContext* browser_context_;
153
[email protected]117832812013-10-02 07:06:02154 scoped_ptr<content::HostZoomMap::Subscription> zoom_subscription_;
[email protected]89c9aca2013-02-07 15:08:42155
[email protected]6f80e932012-06-04 19:00:07156 DISALLOW_COPY_AND_ASSIGN(ZoomController);
157};
158
159#endif // CHROME_BROWSER_UI_ZOOM_ZOOM_CONTROLLER_H_