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 #include "components/zoom/zoom_controller.h" | 5 #include "components/zoom/zoom_controller.h" |
6 | 6 |
7 #include "components/zoom/zoom_event_manager.h" | 7 #include "components/zoom/zoom_event_manager.h" |
8 #include "components/zoom/zoom_observer.h" | 8 #include "components/zoom/zoom_observer.h" |
9 #include "content/public/browser/browser_context.h" | |
9 #include "content/public/browser/host_zoom_map.h" | 10 #include "content/public/browser/host_zoom_map.h" |
10 #include "content/public/browser/navigation_details.h" | 11 #include "content/public/browser/navigation_details.h" |
11 #include "content/public/browser/navigation_entry.h" | 12 #include "content/public/browser/navigation_entry.h" |
12 #include "content/public/browser/navigation_handle.h" | 13 #include "content/public/browser/navigation_handle.h" |
14 #include "content/public/browser/render_frame_host.h" | |
13 #include "content/public/browser/render_process_host.h" | 15 #include "content/public/browser/render_process_host.h" |
14 #include "content/public/browser/render_view_host.h" | 16 #include "content/public/browser/render_view_host.h" |
17 #include "content/public/browser/storage_partition.h" | |
15 #include "content/public/browser/web_contents.h" | 18 #include "content/public/browser/web_contents.h" |
16 #include "content/public/common/page_type.h" | 19 #include "content/public/common/page_type.h" |
17 #include "content/public/common/page_zoom.h" | 20 #include "content/public/common/page_zoom.h" |
18 #include "net/base/url_util.h" | 21 #include "net/base/url_util.h" |
19 | 22 |
20 DEFINE_WEB_CONTENTS_USER_DATA_KEY(zoom::ZoomController); | 23 DEFINE_WEB_CONTENTS_USER_DATA_KEY(zoom::ZoomController); |
21 | 24 |
22 namespace zoom { | 25 namespace zoom { |
23 | 26 |
24 double ZoomController::GetZoomLevelForWebContents( | 27 double ZoomController::GetZoomLevelForWebContents( |
25 const content::WebContents* web_contents) { | 28 const content::WebContents* web_contents) { |
26 if (!web_contents) | 29 if (!web_contents) |
27 return 0.0; | 30 return 0.0; |
28 | 31 |
29 auto* zoom_controller = FromWebContents(web_contents); | 32 auto* zoom_controller = FromWebContents(web_contents); |
30 if (zoom_controller) | 33 if (zoom_controller) |
31 return zoom_controller->GetZoomLevel(); | 34 return zoom_controller->GetZoomLevel(); |
32 | 35 |
33 return content::HostZoomMap::GetZoomLevel(web_contents); | 36 return content::HostZoomMap::GetZoomLevel(web_contents); |
34 } | 37 } |
35 | 38 |
36 ZoomController::ZoomController(content::WebContents* web_contents) | 39 ZoomController::ZoomController(content::WebContents* web_contents) |
37 : content::WebContentsObserver(web_contents), | 40 : content::WebContentsObserver(web_contents), |
38 can_show_bubble_(true), | 41 can_show_bubble_(true), |
39 zoom_mode_(ZOOM_MODE_DEFAULT), | 42 default_scope_is_per_origin_(ZoomPrefsDelegate::kZoomScopeSettingDefault), |
40 zoom_level_(1.0), | 43 zoom_level_(1.0), |
41 browser_context_(web_contents->GetBrowserContext()) { | 44 browser_context_(web_contents->GetBrowserContext()) { |
42 host_zoom_map_ = content::HostZoomMap::GetForWebContents(web_contents); | 45 host_zoom_map_ = content::HostZoomMap::GetForWebContents(web_contents); |
43 zoom_level_ = host_zoom_map_->GetDefaultZoomLevel(); | 46 zoom_level_ = host_zoom_map_->GetDefaultZoomLevel(); |
44 | 47 |
48 ZoomPrefsDelegate* zoom_prefs_delegate = static_cast<ZoomPrefsDelegate*>( | |
49 content::BrowserContext::GetStoragePartition( | |
50 browser_context_, web_contents->GetSiteInstance()) | |
51 ->GetZoomLevelDelegate()); | |
52 if (zoom_prefs_delegate) { | |
53 default_scope_is_per_origin_ = | |
54 zoom_prefs_delegate->GetZoomScopeIsPerOriginPref(); | |
55 default_zoom_scope_subscription_ = | |
56 zoom_prefs_delegate->RegisterDefaultZoomScopeCallback( | |
57 base::Bind(&ZoomController::OnDefaultZoomScopeChanged, | |
58 base::Unretained(this))); | |
59 } | |
60 zoom_mode_ = | |
61 default_scope_is_per_origin_ ? ZOOM_MODE_DEFAULT : ZOOM_MODE_ISOLATED; | |
62 | |
63 // When in isolated zoom, a temporary zoom level needs to be set to override | |
64 // any existing per-host zoom levels. | |
65 // If this WebContents is a clone of another, don't overwrite the temporary | |
66 // level we've inherited. | |
67 int render_process_id = web_contents->GetRenderProcessHost()->GetID(); | |
68 int render_view_id = web_contents->GetRenderViewHost()->GetRoutingID(); | |
69 if (zoom_mode_ == ZOOM_MODE_ISOLATED && | |
70 !host_zoom_map_->UsesTemporaryZoomLevel(render_process_id, | |
71 render_view_id)) { | |
72 host_zoom_map_->SetTemporaryZoomLevel(render_process_id, render_view_id, | |
73 zoom_level_); | |
74 } | |
75 | |
45 zoom_subscription_ = host_zoom_map_->AddZoomLevelChangedCallback( | 76 zoom_subscription_ = host_zoom_map_->AddZoomLevelChangedCallback( |
46 base::Bind(&ZoomController::OnZoomLevelChanged, base::Unretained(this))); | 77 base::Bind(&ZoomController::OnZoomLevelChanged, base::Unretained(this))); |
47 | 78 |
48 UpdateState(std::string()); | 79 UpdateState(std::string()); |
49 } | 80 } |
50 | 81 |
51 ZoomController::~ZoomController() {} | 82 ZoomController::~ZoomController() {} |
52 | 83 |
53 bool ZoomController::IsAtDefaultZoom() const { | 84 bool ZoomController::IsAtDefaultZoom() const { |
54 return content::ZoomValuesEqual(GetZoomLevel(), GetDefaultZoomLevel()); | 85 return content::ZoomValuesEqual(GetZoomLevel(), GetDefaultZoomLevel()); |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
173 content::HostZoomMap* zoom_map = | 204 content::HostZoomMap* zoom_map = |
174 content::HostZoomMap::GetForWebContents(web_contents()); | 205 content::HostZoomMap::GetForWebContents(web_contents()); |
175 DCHECK(zoom_map); | 206 DCHECK(zoom_map); |
176 int render_process_id = web_contents()->GetRenderProcessHost()->GetID(); | 207 int render_process_id = web_contents()->GetRenderProcessHost()->GetID(); |
177 int render_view_id = web_contents()->GetRenderViewHost()->GetRoutingID(); | 208 int render_view_id = web_contents()->GetRenderViewHost()->GetRoutingID(); |
178 double original_zoom_level = GetZoomLevel(); | 209 double original_zoom_level = GetZoomLevel(); |
179 | 210 |
180 DCHECK(!event_data_); | 211 DCHECK(!event_data_); |
181 event_data_.reset(new ZoomChangedEventData( | 212 event_data_.reset(new ZoomChangedEventData( |
182 web_contents(), original_zoom_level, original_zoom_level, new_mode, | 213 web_contents(), original_zoom_level, original_zoom_level, new_mode, |
183 new_mode != ZOOM_MODE_DEFAULT)); | 214 new_mode != |
215 (default_scope_is_per_origin_ | |
216 ? ZOOM_MODE_DEFAULT : ZOOM_MODE_ISOLATED))); | |
184 | 217 |
185 switch (new_mode) { | 218 switch (new_mode) { |
186 case ZOOM_MODE_DEFAULT: { | 219 case ZOOM_MODE_DEFAULT: { |
187 content::NavigationEntry* entry = | 220 content::NavigationEntry* entry = |
188 web_contents()->GetController().GetLastCommittedEntry(); | 221 web_contents()->GetController().GetLastCommittedEntry(); |
189 | 222 |
190 if (entry) { | 223 if (entry) { |
191 GURL url = content::HostZoomMap::GetURLFromEntry(entry); | 224 GURL url = content::HostZoomMap::GetURLFromEntry(entry); |
192 std::string host = net::GetHostOrSpecFromURL(url); | 225 std::string host = net::GetHostOrSpecFromURL(url); |
193 | 226 |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
254 break; | 287 break; |
255 } | 288 } |
256 } | 289 } |
257 // Any event data we've stored should have been consumed by this point. | 290 // Any event data we've stored should have been consumed by this point. |
258 DCHECK(!event_data_); | 291 DCHECK(!event_data_); |
259 | 292 |
260 zoom_mode_ = new_mode; | 293 zoom_mode_ = new_mode; |
261 } | 294 } |
262 | 295 |
263 void ZoomController::ResetZoomModeOnNavigationIfNeeded(const GURL& url) { | 296 void ZoomController::ResetZoomModeOnNavigationIfNeeded(const GURL& url) { |
264 if (zoom_mode_ != ZOOM_MODE_ISOLATED && zoom_mode_ != ZOOM_MODE_MANUAL) | 297 if ((!default_scope_is_per_origin_ || zoom_mode_ != ZOOM_MODE_ISOLATED) && |
298 zoom_mode_ != ZOOM_MODE_MANUAL) | |
265 return; | 299 return; |
266 | 300 |
267 int render_process_id = web_contents()->GetRenderProcessHost()->GetID(); | 301 int render_process_id = web_contents()->GetRenderProcessHost()->GetID(); |
268 int render_view_id = web_contents()->GetRenderViewHost()->GetRoutingID(); | 302 int render_view_id = web_contents()->GetRenderViewHost()->GetRoutingID(); |
269 content::HostZoomMap* zoom_map = | 303 content::HostZoomMap* zoom_map = |
270 content::HostZoomMap::GetForWebContents(web_contents()); | 304 content::HostZoomMap::GetForWebContents(web_contents()); |
305 double old_zoom_level = GetZoomLevel(); | |
306 // Set |zoom_level_| after calling GetZoomLevel() so we get the correct old | |
307 // zoom level if we're resetting from manual. | |
271 zoom_level_ = zoom_map->GetDefaultZoomLevel(); | 308 zoom_level_ = zoom_map->GetDefaultZoomLevel(); |
272 double old_zoom_level = zoom_map->GetZoomLevel(web_contents()); | 309 ZoomMode new_mode; |
273 double new_zoom_level = zoom_map->GetZoomLevelForHostAndScheme( | 310 double new_zoom_level; |
274 url.scheme(), net::GetHostOrSpecFromURL(url)); | 311 if (default_scope_is_per_origin_) { |
312 new_mode = ZOOM_MODE_DEFAULT; | |
313 new_zoom_level = zoom_map->GetZoomLevelForHostAndScheme( | |
314 url.scheme(), net::GetHostOrSpecFromURL(url)); | |
315 } else { | |
316 new_mode = ZOOM_MODE_ISOLATED; | |
317 new_zoom_level = zoom_level_; | |
318 } | |
275 event_data_.reset(new ZoomChangedEventData(web_contents(), old_zoom_level, | 319 event_data_.reset(new ZoomChangedEventData(web_contents(), old_zoom_level, |
276 new_zoom_level, ZOOM_MODE_DEFAULT, | 320 new_zoom_level, new_mode, |
277 false /* can_show_bubble */)); | 321 false /* can_show_bubble */)); |
278 // The call to ClearTemporaryZoomLevel() doesn't generate any events from | 322 if (default_scope_is_per_origin_) { |
279 // HostZoomMap, but the call to UpdateState() at the end of this function | 323 // The call to ClearTemporaryZoomLevel() doesn't generate any events from |
280 // will notify our observers. | 324 // HostZoomMap, but the call to UpdateState() at the end of this function |
281 // Note: it's possible the render_process/view ids have disappeared (e.g. | 325 // will notify our observers. |
282 // if we navigated to a new origin), but this won't cause a problem in the | 326 // Note: it's possible the render_process/view ids have disappeared (e.g. |
283 // call below. | 327 // if we navigated to a new origin), but this won't cause a problem in the |
284 zoom_map->ClearTemporaryZoomLevel(render_process_id, render_view_id); | 328 // call below. |
285 zoom_mode_ = ZOOM_MODE_DEFAULT; | 329 zoom_map->ClearTemporaryZoomLevel(render_process_id, render_view_id); |
330 } else { | |
331 // If we're resetting to isolated zoom, a temporary zoom level needs to | |
332 // be set to override any existing per-host zoom levels. | |
333 zoom_map->SetTemporaryZoomLevel(render_process_id, render_view_id, | |
334 new_zoom_level); | |
335 } | |
336 zoom_mode_ = new_mode; | |
337 } | |
338 | |
339 void ZoomController::UpdateZoomModeOnScopeChangeIfNeeded() { | |
340 if (zoom_mode_ != ZOOM_MODE_DEFAULT && zoom_mode_ != ZOOM_MODE_ISOLATED) | |
341 return; | |
342 | |
343 // When going from per-tab to per-host, if there isn't an existing per-host | |
344 // zoom level set, the first ZoomController (that has set a non-default | |
345 // temporary zoom level) to be notified of the scope change will set the | |
346 // per-host zoom level. | |
Kevin McNee
2017/02/03 23:17:20
Is this acceptable behaviour?
wjmaclean
2017/02/06 16:09:07
I don't think I understand the comment. I would ha
Kevin McNee
2017/02/06 18:26:12
The problem with deferring the change in zoom mode
wjmaclean
2017/02/06 18:48:38
Ok. In that case I'd clarify the comment, since it
| |
347 SetZoomMode( | |
348 default_scope_is_per_origin_ ? ZOOM_MODE_DEFAULT : ZOOM_MODE_ISOLATED); | |
286 } | 349 } |
287 | 350 |
288 void ZoomController::DidFinishNavigation( | 351 void ZoomController::DidFinishNavigation( |
289 content::NavigationHandle* navigation_handle) { | 352 content::NavigationHandle* navigation_handle) { |
290 if (!navigation_handle->IsInMainFrame() || !navigation_handle->HasCommitted()) | 353 if (!navigation_handle->IsInMainFrame() || !navigation_handle->HasCommitted()) |
291 return; | 354 return; |
292 | 355 |
293 if (navigation_handle->IsErrorPage()) | 356 if (navigation_handle->IsErrorPage()) |
294 content::HostZoomMap::SendErrorPageZoomLevelRefresh(web_contents()); | 357 content::HostZoomMap::SendErrorPageZoomLevelRefresh(web_contents()); |
295 | 358 |
296 if (!navigation_handle->IsSamePage()) | 359 if (!navigation_handle->IsSamePage()) |
297 ResetZoomModeOnNavigationIfNeeded(navigation_handle->GetURL()); | 360 ResetZoomModeOnNavigationIfNeeded(navigation_handle->GetURL()); |
298 | 361 |
299 // If the main frame's content has changed, the new page may have a different | 362 // If the main frame's content has changed, the new page may have a different |
300 // zoom level from the old one. | 363 // zoom level from the old one. |
301 UpdateState(std::string()); | 364 UpdateState(std::string()); |
302 DCHECK(!event_data_); | 365 DCHECK(!event_data_); |
303 } | 366 } |
304 | 367 |
305 void ZoomController::WebContentsDestroyed() { | 368 void ZoomController::WebContentsDestroyed() { |
369 // Once our WebContents is destroyed, we can no longer respond to zoom scope | |
370 // changes. | |
371 default_zoom_scope_subscription_.reset(); | |
306 // At this point we should no longer be sending any zoom events with this | 372 // At this point we should no longer be sending any zoom events with this |
307 // WebContents. | 373 // WebContents. |
308 observers_.Clear(); | 374 observers_.Clear(); |
309 } | 375 } |
310 | 376 |
311 void ZoomController::RenderFrameHostChanged( | 377 void ZoomController::RenderFrameHostChanged( |
312 content::RenderFrameHost* old_host, | 378 content::RenderFrameHost* old_host, |
313 content::RenderFrameHost* new_host) { | 379 content::RenderFrameHost* new_host) { |
314 // If our associated HostZoomMap changes, update our event subscription. | |
315 content::HostZoomMap* new_host_zoom_map = | 380 content::HostZoomMap* new_host_zoom_map = |
316 content::HostZoomMap::GetForWebContents(web_contents()); | 381 content::HostZoomMap::GetForWebContents(web_contents()); |
382 | |
383 // The HostZoomMap records temporary zoom levels per RenderView. When zooming | |
384 // is per-tab and our RenderView changes, we need to copy the level for our | |
385 // new RenderView so that the tab's zoom level is preserved. | |
386 if (old_host && !default_scope_is_per_origin_ && | |
387 zoom_mode_ == ZOOM_MODE_ISOLATED) { | |
388 PreserveTemporaryZoomLevel( | |
389 host_zoom_map_, new_host_zoom_map, old_host, new_host); | |
390 } | |
391 | |
392 // If our associated HostZoomMap changes, update our event subscription. | |
317 if (new_host_zoom_map == host_zoom_map_) | 393 if (new_host_zoom_map == host_zoom_map_) |
318 return; | 394 return; |
319 | 395 |
320 host_zoom_map_ = new_host_zoom_map; | 396 host_zoom_map_ = new_host_zoom_map; |
321 zoom_subscription_ = host_zoom_map_->AddZoomLevelChangedCallback( | 397 zoom_subscription_ = host_zoom_map_->AddZoomLevelChangedCallback( |
322 base::Bind(&ZoomController::OnZoomLevelChanged, base::Unretained(this))); | 398 base::Bind(&ZoomController::OnZoomLevelChanged, base::Unretained(this))); |
323 } | 399 } |
324 | 400 |
401 void ZoomController::PreserveTemporaryZoomLevel( | |
402 const content::HostZoomMap* old_zoom_map, | |
403 content::HostZoomMap* new_zoom_map, | |
404 content::RenderFrameHost* old_host, | |
405 content::RenderFrameHost* new_host) const { | |
406 const content::RenderViewHost* old_view_host = old_host->GetRenderViewHost(); | |
407 const content::RenderViewHost* new_view_host = new_host->GetRenderViewHost(); | |
408 | |
409 DCHECK(old_zoom_map->UsesTemporaryZoomLevel( | |
410 old_view_host->GetProcess()->GetID(), old_view_host->GetRoutingID())); | |
411 | |
412 const double level = old_zoom_map->GetTemporaryZoomLevel( | |
413 old_view_host->GetProcess()->GetID(), old_view_host->GetRoutingID()); | |
414 new_zoom_map->SetTemporaryZoomLevel( | |
415 new_view_host->GetProcess()->GetID(), new_view_host->GetRoutingID(), | |
416 level); | |
417 } | |
418 | |
419 void ZoomController::PreserveTemporaryZoomLevel( | |
420 content::WebContents* old_web_contents, | |
421 content::WebContents* new_web_contents) const { | |
422 if (default_scope_is_per_origin_ || zoom_mode_ != ZOOM_MODE_ISOLATED) | |
423 return; | |
424 | |
425 content::RenderFrameHost* old_host = old_web_contents->GetMainFrame(); | |
426 content::RenderFrameHost* new_host = new_web_contents->GetMainFrame(); | |
427 const content::HostZoomMap* old_host_zoom_map = | |
428 content::HostZoomMap::GetForWebContents(old_web_contents); | |
429 content::HostZoomMap* new_host_zoom_map = | |
430 content::HostZoomMap::GetForWebContents(new_web_contents); | |
431 | |
432 PreserveTemporaryZoomLevel( | |
433 old_host_zoom_map, new_host_zoom_map, old_host, new_host); | |
434 } | |
435 | |
436 void ZoomController::DidCloneToNewWebContents( | |
437 content::WebContents* old_web_contents, | |
438 content::WebContents* new_web_contents) { | |
439 // If a WebContents is cloned when zooming is per-tab, have the clone inherit | |
440 // the original's zoom level. | |
441 PreserveTemporaryZoomLevel(old_web_contents, new_web_contents); | |
442 } | |
443 | |
444 void ZoomController::WebContentsReplaced( | |
445 content::WebContents* new_web_contents) { | |
446 // A tab may replace its WebContents with another, but from the user's | |
447 // perspective, this is still the same tab, so we need to preserve the | |
448 // per-tab zoom. | |
449 PreserveTemporaryZoomLevel(web_contents(), new_web_contents); | |
450 } | |
451 | |
325 void ZoomController::OnZoomLevelChanged( | 452 void ZoomController::OnZoomLevelChanged( |
326 const content::HostZoomMap::ZoomLevelChange& change) { | 453 const content::HostZoomMap::ZoomLevelChange& change) { |
327 UpdateState(change.host); | 454 UpdateState(change.host); |
328 } | 455 } |
329 | 456 |
457 void ZoomController::OnDefaultZoomScopeChanged() { | |
458 default_scope_is_per_origin_ = !default_scope_is_per_origin_; | |
459 UpdateZoomModeOnScopeChangeIfNeeded(); | |
460 } | |
461 | |
330 void ZoomController::UpdateState(const std::string& host) { | 462 void ZoomController::UpdateState(const std::string& host) { |
331 // If |host| is empty, all observers should be updated. | 463 // If |host| is empty, all observers should be updated. |
332 if (!host.empty()) { | 464 if (!host.empty()) { |
333 // Use the navigation entry's URL instead of the WebContents' so virtual | 465 // Use the navigation entry's URL instead of the WebContents' so virtual |
334 // URLs work (e.g. chrome://settings). http://crbug.com/153950 | 466 // URLs work (e.g. chrome://settings). http://crbug.com/153950 |
335 content::NavigationEntry* entry = | 467 content::NavigationEntry* entry = |
336 web_contents()->GetController().GetLastCommittedEntry(); | 468 web_contents()->GetController().GetLastCommittedEntry(); |
337 if (!entry || | 469 if (!entry || |
338 host != net::GetHostOrSpecFromURL( | 470 host != net::GetHostOrSpecFromURL( |
339 content::HostZoomMap::GetURLFromEntry(entry))) { | 471 content::HostZoomMap::GetURLFromEntry(entry))) { |
(...skipping 29 matching lines...) Expand all Loading... | |
369 int render_view_id = web_contents()->GetRenderViewHost()->GetRoutingID(); | 501 int render_view_id = web_contents()->GetRenderViewHost()->GetRoutingID(); |
370 host_zoom_map_->SetPageScaleFactorIsOneForView(render_process_id, | 502 host_zoom_map_->SetPageScaleFactorIsOneForView(render_process_id, |
371 render_view_id, is_one); | 503 render_view_id, is_one); |
372 } | 504 } |
373 | 505 |
374 bool ZoomController::PageScaleFactorIsOne() const { | 506 bool ZoomController::PageScaleFactorIsOne() const { |
375 return content::HostZoomMap::PageScaleFactorIsOne(web_contents()); | 507 return content::HostZoomMap::PageScaleFactorIsOne(web_contents()); |
376 } | 508 } |
377 | 509 |
378 } // namespace zoom | 510 } // namespace zoom |
OLD | NEW |