Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(726)

Side by Side Diff: chrome/browser/ui/zoom/zoom_controller.cc

Issue 301733006: Zoom Extension API (chrome) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address comments: move ZoomObserver registration to TabsEventRouter. Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 #include "chrome/browser/ui/zoom/zoom_controller.h" 5 #include "chrome/browser/ui/zoom/zoom_controller.h"
6 6
7 #include "base/prefs/pref_service.h" 7 #include "base/prefs/pref_service.h"
8 #include "chrome/browser/chrome_notification_types.h" 8 #include "chrome/browser/chrome_notification_types.h"
9 #include "chrome/browser/extensions/api/tabs/tabs_event_router.h"
10 #include "chrome/browser/extensions/api/tabs/tabs_windows_api.h"
9 #include "chrome/browser/profiles/profile.h" 11 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/ui/browser_finder.h" 12 #include "chrome/browser/ui/browser_finder.h"
13 #include "chrome/browser/ui/zoom/zoom_event_manager.h"
11 #include "chrome/common/pref_names.h" 14 #include "chrome/common/pref_names.h"
12 #include "content/public/browser/host_zoom_map.h" 15 #include "content/public/browser/host_zoom_map.h"
13 #include "content/public/browser/navigation_entry.h" 16 #include "content/public/browser/navigation_entry.h"
14 #include "content/public/browser/notification_details.h" 17 #include "content/public/browser/notification_details.h"
15 #include "content/public/browser/notification_service.h" 18 #include "content/public/browser/notification_service.h"
16 #include "content/public/browser/notification_types.h" 19 #include "content/public/browser/notification_types.h"
20 #include "content/public/browser/render_process_host.h"
21 #include "content/public/browser/render_view_host.h"
17 #include "content/public/browser/web_contents.h" 22 #include "content/public/browser/web_contents.h"
18 #include "content/public/common/page_zoom.h" 23 #include "content/public/common/page_zoom.h"
24 #include "extensions/common/extension.h"
19 #include "grit/theme_resources.h" 25 #include "grit/theme_resources.h"
20 #include "net/base/net_util.h" 26 #include "net/base/net_util.h"
21 27
22 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ZoomController); 28 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ZoomController);
23 29
24 ZoomController::ZoomController(content::WebContents* web_contents) 30 ZoomController::ZoomController(content::WebContents* web_contents)
25 : content::WebContentsObserver(web_contents), 31 : content::WebContentsObserver(web_contents),
26 zoom_percent_(100), 32 zoom_mode_(ZOOM_MODE_DEFAULT),
27 observer_(NULL), 33 zoom_level_(1.0),
28 browser_context_(web_contents->GetBrowserContext()) { 34 browser_context_(web_contents->GetBrowserContext()) {
29 Profile* profile = 35 Profile* profile =
30 Profile::FromBrowserContext(web_contents->GetBrowserContext()); 36 Profile::FromBrowserContext(web_contents->GetBrowserContext());
31 default_zoom_level_.Init(prefs::kDefaultZoomLevel, profile->GetPrefs(), 37 default_zoom_level_.Init(
32 base::Bind(&ZoomController::UpdateState, 38 prefs::kDefaultZoomLevel,
33 base::Unretained(this), 39 profile->GetPrefs(),
34 std::string())); 40 base::Bind(
41 &ZoomController::UpdateState, base::Unretained(this), std::string()));
42 zoom_level_ = default_zoom_level_.GetValue();
35 43
36 zoom_subscription_ = content::HostZoomMap::GetForBrowserContext( 44 zoom_subscription_ = content::HostZoomMap::GetForBrowserContext(
37 browser_context_)->AddZoomLevelChangedCallback( 45 browser_context_)->AddZoomLevelChangedCallback(
38 base::Bind(&ZoomController::OnZoomLevelChanged, 46 base::Bind(&ZoomController::OnZoomLevelChanged,
39 base::Unretained(this))); 47 base::Unretained(this)));
40 48
41 UpdateState(std::string()); 49 UpdateState(std::string());
42 } 50 }
43 51
44 ZoomController::~ZoomController() {} 52 ZoomController::~ZoomController() {}
45 53
46 bool ZoomController::IsAtDefaultZoom() const { 54 bool ZoomController::IsAtDefaultZoom() const {
47 return content::ZoomValuesEqual( 55 return content::ZoomValuesEqual(GetZoomLevel(),
48 content::HostZoomMap::GetZoomLevel(web_contents()), 56 default_zoom_level_.GetValue());
49 default_zoom_level_.GetValue());
50 } 57 }
51 58
52 int ZoomController::GetResourceForZoomLevel() const { 59 int ZoomController::GetResourceForZoomLevel() const {
53 if (IsAtDefaultZoom()) 60 if (IsAtDefaultZoom())
54 return IDR_ZOOM_NORMAL; 61 return IDR_ZOOM_NORMAL;
55 double zoom = content::HostZoomMap::GetZoomLevel(web_contents()); 62 return GetZoomLevel() > default_zoom_level_.GetValue() ? IDR_ZOOM_PLUS
56 return zoom > default_zoom_level_.GetValue() ? IDR_ZOOM_PLUS : IDR_ZOOM_MINUS; 63 : IDR_ZOOM_MINUS;
64 }
65
66 void ZoomController::AddObserver(ZoomObserver* observer) {
67 observers_.AddObserver(observer);
68 }
69
70 void ZoomController::RemoveObserver(ZoomObserver* observer) {
71 observers_.RemoveObserver(observer);
72 }
73
74 double ZoomController::GetZoomLevel() const {
75 return zoom_mode_ == ZOOM_MODE_MANUAL
76 ? zoom_level_
77 : content::HostZoomMap::GetZoomLevel(web_contents());
Dan Beam 2014/06/26 03:40:02 return zoom_mode_ == ZOOM_MODE_MANUAL ? zoom_l
wjmaclean 2014/06/26 15:30:30 Dang Clang formatter ... done.
78 }
79
80 int ZoomController::GetZoomPercent() const {
81 double zoom_factor = content::ZoomLevelToZoomFactor(GetZoomLevel());
82 return static_cast<int>(zoom_factor * 100 + 0.5);
Dan Beam 2014/06/26 03:40:02 nit: mention in a comment that this is rounding
wjmaclean 2014/06/26 15:30:31 Done.
83 }
84
85 bool ZoomController::SetZoomLevel(double zoom_level) {
86 // An extension did not initiate this zoom change.
87 return SetZoomLevelByExtension(zoom_level,
88 scoped_refptr<const extensions::Extension>());
Dan Beam 2014/06/26 03:40:02 can you use NULL instead of scoped_refptr<const ex
wjmaclean 2014/06/26 15:30:30 Done. The current form was requested in a previous
89 }
90
91 bool ZoomController::SetZoomLevelByExtension(
92 double zoom_level,
93 scoped_refptr<const extensions::Extension> extension) {
Dan Beam 2014/06/26 03:40:02 nit: const-ref if possible/cheaper
wjmaclean 2014/06/26 15:30:31 Done.
94 // Cannot zoom in disabled mode.
95 if (zoom_mode_ == ZOOM_MODE_DISABLED)
96 return false;
97
98 // Store extension data so that |extension| can be attributed when the zoom
99 // change completes. We expect that by the time this function returns that
100 // any observers that require this information will have requested it.
101 last_extension_ = extension;
102
103 if (zoom_mode_ == ZOOM_MODE_MANUAL) {
104 double old_zoom_level = zoom_level_;
105 zoom_level_ = zoom_level;
106
107 // TODO(wjmaclean) Do we care about filling in host/scheme here?
108 content::HostZoomMap::ZoomLevelChange change;
109 change.mode = content::HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM;
110 change.zoom_level = zoom_level;
111 ZoomEventManager::GetForBrowserContext(browser_context_)
112 ->OnZoomLevelChanged(change);
113
114 FOR_EACH_OBSERVER(
115 ZoomObserver,
116 observers_,
117 OnZoomChanged(web_contents(), true /* can_show_bubble */));
118 FOR_EACH_OBSERVER(
119 ZoomObserver,
120 observers_,
121 OnZoomChangeInitiated(
122 web_contents(), old_zoom_level, zoom_level, zoom_mode_));
123
124 last_extension_ = NULL;
125 return true;
126 }
127
128 // Do not actually rescale the page in manual mode.
129 content::HostZoomMap* zoom_map =
130 content::HostZoomMap::GetForBrowserContext(browser_context_);
131 DCHECK(zoom_map);
132 double old_zoom_level = GetZoomLevel();
133 int render_process_id = web_contents()->GetRenderProcessHost()->GetID();
134 int render_view_id = web_contents()->GetRenderViewHost()->GetRoutingID();
135 if (zoom_mode_ == ZOOM_MODE_ISOLATED ||
136 zoom_map->UsesTemporaryZoomLevel(render_process_id, render_view_id)) {
137 zoom_map->SetTemporaryZoomLevel(
138 render_process_id, render_view_id, zoom_level);
139 } else {
140 content::NavigationEntry* entry =
141 web_contents()->GetController().GetLastCommittedEntry();
142 // We allow empty |host| values for data urls.
143 std::string host =
144 net::GetHostOrSpecFromURL(entry ? entry->GetURL() : GURL::EmptyGURL());
145 zoom_map->SetZoomLevelForHost(host, zoom_level);
146 }
147 FOR_EACH_OBSERVER(
148 ZoomObserver,
149 observers_,
150 OnZoomChangeInitiated(
151 web_contents(), old_zoom_level, zoom_level, zoom_mode_));
152
153 last_extension_ = NULL;
154 return true;
155 }
156
157 void ZoomController::SetZoomMode(ZoomMode new_mode) {
158 if (new_mode == zoom_mode_)
159 return;
160
161 content::HostZoomMap* zoom_map =
162 content::HostZoomMap::GetForBrowserContext(browser_context_);
163 DCHECK(zoom_map);
164 int render_process_id = web_contents()->GetRenderProcessHost()->GetID();
165 int render_view_id = web_contents()->GetRenderViewHost()->GetRoutingID();
166 double original_zoom_level = GetZoomLevel();
167
168 switch (new_mode) {
169 case ZOOM_MODE_DEFAULT: {
170 content::NavigationEntry* entry =
171 web_contents()->GetController().GetLastCommittedEntry();
172 GURL url;
173 if (entry)
174 url = entry->GetURL();
175 std::string host = net::GetHostOrSpecFromURL(url);
176
177 if (zoom_map->HasZoomLevel(url.scheme(), host)) {
178 // If there are other tabs with the same origin, then set this tab's
179 // zoom level to match theirs. The temporary zoom level will be cleared
180 // below, but this call will make sure this tab re-draws at the correct
181 // zoom level.
182 double origin_zoom_level =
183 zoom_map->GetZoomLevelForHostAndScheme(url.scheme(), host);
184 zoom_map->SetTemporaryZoomLevel(
185 render_process_id, render_view_id, origin_zoom_level);
186 } else {
187 // The host will need a level prior to removing the temporary level.
188 // We don't want the zoom level to change just because we entered
189 // default mode.
190 zoom_map->SetZoomLevelForHost(host, original_zoom_level);
191 }
192 // Remove per-tab zoom data for this tab.
193 zoom_map->ClearTemporaryZoomLevel(render_process_id, render_view_id);
Dan Beam 2014/06/26 03:40:02 i'm confused, why are you settings the temporary z
wjmaclean 2014/06/26 15:30:30 The call to SetTemporaryZoomLevel is required to f
194 break;
195 }
196 case ZOOM_MODE_ISOLATED: {
197 // Unless the zoom mode was |ZOOM_MODE_DISABLED| before this call, the
198 // page needs an initial isolated zoom back to the same level it was at
199 // in the other mode.
Dan Beam 2014/06/26 03:40:02 can you give an example of when this ^ would happe
wjmaclean 2014/06/26 15:30:30 This will happen when going from default mode to i
Dan Beam 2014/06/26 22:16:37 I mean, what can a user do to trigger this? Put i
200 if (zoom_mode_ != ZOOM_MODE_DISABLED) {
201 zoom_map->SetTemporaryZoomLevel(
202 render_process_id, render_view_id, original_zoom_level);
203 }
204 break;
205 }
206 case ZOOM_MODE_MANUAL: {
207 // Unless the zoom mode was |ZOOM_MODE_DISABLED| before this call, the
208 // page needs to be resized to the default zoom before calling
209 // SetZoomLevel() so that the page can be resized manually to the same
210 // zoom as before.
Dan Beam 2014/06/26 03:40:02 i'm confused, is there any other way you can put t
wjmaclean 2014/06/26 15:30:30 Done.
211 if (zoom_mode_ != ZOOM_MODE_DISABLED) {
212 zoom_map->SetTemporaryZoomLevel(render_process_id, render_view_id, 0);
213 zoom_level_ = original_zoom_level;
214 }
215 break;
216 }
217 case ZOOM_MODE_DISABLED: {
218 // The page needs to be zoomed back to default before disabling the zoom
219 zoom_map->SetTemporaryZoomLevel(render_process_id, render_view_id, 0);
220 break;
221 }
222 }
223
224 zoom_mode_ = new_mode;
57 } 225 }
58 226
59 void ZoomController::DidNavigateMainFrame( 227 void ZoomController::DidNavigateMainFrame(
60 const content::LoadCommittedDetails& details, 228 const content::LoadCommittedDetails& details,
61 const content::FrameNavigateParams& params) { 229 const content::FrameNavigateParams& params) {
62 // If the main frame's content has changed, the new page may have a different 230 // If the main frame's content has changed, the new page may have a different
63 // zoom level from the old one. 231 // zoom level from the old one.
64 UpdateState(std::string()); 232 UpdateState(std::string());
65 } 233 }
66 234
67 void ZoomController::OnZoomLevelChanged( 235 void ZoomController::OnZoomLevelChanged(
68 const content::HostZoomMap::ZoomLevelChange& change) { 236 const content::HostZoomMap::ZoomLevelChange& change) {
69 UpdateState(change.host); 237 UpdateStateIncludingTemporary(
238 change.host,
239 change.mode == content::HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM);
70 } 240 }
71 241
72 void ZoomController::UpdateState(const std::string& host) { 242 void ZoomController::UpdateState(const std::string& host) {
243 UpdateStateIncludingTemporary(host, false);
244 }
245
246 void ZoomController::UpdateStateIncludingTemporary(const std::string& host,
247 bool is_temporary_zoom) {
73 // If |host| is empty, all observers should be updated. 248 // If |host| is empty, all observers should be updated.
74 if (!host.empty()) { 249 if (!host.empty()) {
75 // Use the navigation entry's URL instead of the WebContents' so virtual 250 // Use the navigation entry's URL instead of the WebContents' so virtual
76 // URLs work (e.g. chrome://settings). http://crbug.com/153950 251 // URLs work (e.g. chrome://settings). http://crbug.com/153950
77 content::NavigationEntry* entry = 252 content::NavigationEntry* entry =
78 web_contents()->GetController().GetLastCommittedEntry(); 253 web_contents()->GetController().GetLastCommittedEntry();
79 if (!entry || 254 if (!entry ||
80 host != net::GetHostOrSpecFromURL(entry->GetURL())) { 255 host != net::GetHostOrSpecFromURL(entry->GetURL())) {
81 return; 256 return;
82 } 257 }
83 } 258 }
84 259
85 bool dummy; 260 // The zoom bubble can be shown for all normal, per-origin zoom changes
86 zoom_percent_ = web_contents()->GetZoomPercent(&dummy, &dummy); 261 // (where the host will not be empty and the zoom is not temporary), or any
87 262 // special zoom changes (where the zoom mode will not be "default").
88 if (observer_) 263 bool can_show_bubble =
89 observer_->OnZoomChanged(web_contents(), !host.empty()); 264 zoom_mode_ != ZOOM_MODE_DEFAULT || (!host.empty() && !is_temporary_zoom);
265 FOR_EACH_OBSERVER(ZoomObserver,
266 observers_,
267 OnZoomChanged(web_contents(), can_show_bubble));
90 } 268 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698