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 "content/browser/host_zoom_map_impl.h" | 5 #include "content/browser/host_zoom_map_impl.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <cmath> | 8 #include <cmath> |
9 | 9 |
10 #include "base/strings/string_piece.h" | 10 #include "base/strings/string_piece.h" |
(...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
271 zoom_level_changed_callbacks_.Notify(change); | 271 zoom_level_changed_callbacks_.Notify(change); |
272 } | 272 } |
273 | 273 |
274 double HostZoomMapImpl::GetDefaultZoomLevel() const { | 274 double HostZoomMapImpl::GetDefaultZoomLevel() const { |
275 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 275 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
276 return default_zoom_level_; | 276 return default_zoom_level_; |
277 } | 277 } |
278 | 278 |
279 void HostZoomMapImpl::SetDefaultZoomLevel(double level) { | 279 void HostZoomMapImpl::SetDefaultZoomLevel(double level) { |
280 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 280 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
281 | |
282 if (ZoomValuesEqual(level, default_zoom_level_)) | |
283 return; | |
284 | |
281 default_zoom_level_ = level; | 285 default_zoom_level_ = level; |
286 | |
287 // First, remove all entries that match the new default zoom level. | |
288 { | |
289 base::AutoLock auto_lock(lock_); | |
290 for (auto it = host_zoom_levels_.begin(); it != host_zoom_levels_.end(); ) { | |
291 if (ZoomValuesEqual(it->second, default_zoom_level_)) | |
292 it = host_zoom_levels_.erase(it); | |
293 else | |
294 it++; | |
295 } | |
296 } | |
297 | |
298 // Second, update zoom levels for all pages that do not have an overriding | |
299 // entry. | |
300 for (auto web_contents : WebContentsImpl::GetAllWebContents()) { | |
301 if (GetForWebContents(web_contents) != this) | |
302 continue; | |
303 | |
304 int render_process_id = web_contents->GetRenderProcessHost()->GetID(); | |
305 int render_view_id = web_contents->GetRenderViewHost()->GetRoutingID(); | |
306 | |
307 // Get the url from the navigation controller directly, as calling | |
308 // WebContentsImpl::GetLastCommittedURL() may give us a virtual url that | |
309 // is different than the one stored in the map. | |
310 GURL url; | |
311 std::string host; | |
312 std::string scheme; | |
313 | |
314 NavigationEntry* entry = | |
315 web_contents->GetController().GetLastCommittedEntry(); | |
316 // It is possible for a WebContent's zoom level to be queried before | |
317 // a navigation has occurred. | |
318 if (entry) { | |
319 url = GetURLFromEntry(entry); | |
320 scheme = url.scheme(); | |
321 host = net::GetHostOrSpecFromURL(url); | |
322 } | |
323 | |
324 bool uses_default_zoom = | |
325 !HasZoomLevel(scheme, host) && | |
326 !UsesTemporaryZoomLevel(render_process_id, render_view_id); | |
327 | |
328 // Only change zoom for WebContents tied to the StoragePartition this | |
329 // HostZoomMap serves. | |
alexmos
2016/04/27 23:39:51
Should this comment be moved up to the "GetForWebC
wjmaclean
2016/04/28 13:20:11
Done.
| |
330 if (uses_default_zoom) { | |
331 web_contents->UpdateZoom(level); | |
332 | |
333 HostZoomMap::ZoomLevelChange change; | |
334 change.mode = HostZoomMap::ZOOM_CHANGED_FOR_HOST; | |
335 change.host = host; | |
336 change.zoom_level = level; | |
337 | |
338 zoom_level_changed_callbacks_.Notify(change); | |
339 } | |
340 } | |
282 } | 341 } |
283 | 342 |
284 std::unique_ptr<HostZoomMap::Subscription> | 343 std::unique_ptr<HostZoomMap::Subscription> |
285 HostZoomMapImpl::AddZoomLevelChangedCallback( | 344 HostZoomMapImpl::AddZoomLevelChangedCallback( |
286 const ZoomLevelChangedCallback& callback) { | 345 const ZoomLevelChangedCallback& callback) { |
287 return zoom_level_changed_callbacks_.Add(callback); | 346 return zoom_level_changed_callbacks_.Add(callback); |
288 } | 347 } |
289 | 348 |
290 double HostZoomMapImpl::GetZoomLevelForWebContents( | 349 double HostZoomMapImpl::GetZoomLevelForWebContents( |
291 const WebContentsImpl& web_contents_impl) const { | 350 const WebContentsImpl& web_contents_impl) const { |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
398 int render_view_id, | 457 int render_view_id, |
399 double level) { | 458 double level) { |
400 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 459 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
401 | 460 |
402 { | 461 { |
403 RenderViewKey key(render_process_id, render_view_id); | 462 RenderViewKey key(render_process_id, render_view_id); |
404 base::AutoLock auto_lock(lock_); | 463 base::AutoLock auto_lock(lock_); |
405 temporary_zoom_levels_[key] = level; | 464 temporary_zoom_levels_[key] = level; |
406 } | 465 } |
407 | 466 |
408 RenderViewHost* host = | 467 WebContentsImpl* web_contents = |
409 RenderViewHost::FromID(render_process_id, render_view_id); | 468 static_cast<WebContentsImpl*>(WebContents::FromRenderViewHost( |
410 host->Send(new ViewMsg_SetZoomLevelForView(render_view_id, true, level)); | 469 RenderViewHost::FromID(render_process_id, render_view_id))); |
470 web_contents->SetTemporaryZoomLevel(level, true); | |
411 | 471 |
412 HostZoomMap::ZoomLevelChange change; | 472 HostZoomMap::ZoomLevelChange change; |
413 change.mode = HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM; | 473 change.mode = HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM; |
414 change.host = GetHostFromProcessView(render_process_id, render_view_id); | 474 change.host = GetHostFromProcessView(render_process_id, render_view_id); |
415 change.zoom_level = level; | 475 change.zoom_level = level; |
416 | 476 |
417 zoom_level_changed_callbacks_.Notify(change); | 477 zoom_level_changed_callbacks_.Notify(change); |
418 } | 478 } |
419 | 479 |
420 double HostZoomMapImpl::GetZoomLevelForView(const GURL& url, | 480 double HostZoomMapImpl::GetZoomLevelForView(const GURL& url, |
(...skipping 29 matching lines...) Expand all Loading... | |
450 void HostZoomMapImpl::ClearTemporaryZoomLevel(int render_process_id, | 510 void HostZoomMapImpl::ClearTemporaryZoomLevel(int render_process_id, |
451 int render_view_id) { | 511 int render_view_id) { |
452 { | 512 { |
453 base::AutoLock auto_lock(lock_); | 513 base::AutoLock auto_lock(lock_); |
454 RenderViewKey key(render_process_id, render_view_id); | 514 RenderViewKey key(render_process_id, render_view_id); |
455 TemporaryZoomLevels::iterator it = temporary_zoom_levels_.find(key); | 515 TemporaryZoomLevels::iterator it = temporary_zoom_levels_.find(key); |
456 if (it == temporary_zoom_levels_.end()) | 516 if (it == temporary_zoom_levels_.end()) |
457 return; | 517 return; |
458 temporary_zoom_levels_.erase(it); | 518 temporary_zoom_levels_.erase(it); |
459 } | 519 } |
460 RenderViewHost* host = | 520 WebContentsImpl* web_contents = |
461 RenderViewHost::FromID(render_process_id, render_view_id); | 521 static_cast<WebContentsImpl*>(WebContents::FromRenderViewHost( |
462 DCHECK(host); | 522 RenderViewHost::FromID(render_process_id, render_view_id))); |
463 // Send a new zoom level, host-specific if one exists. | 523 web_contents->SetTemporaryZoomLevel(GetZoomLevelForHost( |
464 host->Send(new ViewMsg_SetZoomLevelForView( | 524 GetHostFromProcessView(render_process_id, render_view_id)), false); |
465 render_view_id, | |
466 false, | |
467 GetZoomLevelForHost( | |
468 GetHostFromProcessView(render_process_id, render_view_id)))); | |
469 } | 525 } |
470 | 526 |
471 void HostZoomMapImpl::SendZoomLevelChange(const std::string& scheme, | 527 void HostZoomMapImpl::SendZoomLevelChange(const std::string& scheme, |
472 const std::string& host, | 528 const std::string& host, |
473 double level) { | 529 double level) { |
474 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator()); | 530 // We'll only send to WebContents not using temporary zoom levels. The one |
475 !i.IsAtEnd(); i.Advance()) { | 531 // other case of interest is where the renderer is hosting a plugin document; |
476 RenderProcessHost* render_process_host = i.GetCurrentValue(); | 532 // that should be reflected in our temporary zoom level map, but we will |
477 // TODO(wjmaclean) This will need to be cleaned up when | 533 // double check on the renderer side to avoid the possibility of any races. |
478 // RenderProcessHost::GetStoragePartition() goes away. Perhaps have | 534 for (auto web_contents : WebContentsImpl::GetAllWebContents()) { |
479 // RenderProcessHost expose a GetHostZoomMap() function? | 535 if (GetForWebContents(web_contents) != this) |
480 if (render_process_host->GetStoragePartition()->GetHostZoomMap() == this) { | 536 continue; |
481 render_process_host->Send( | 537 |
482 new ViewMsg_SetZoomLevelForCurrentURL(scheme, host, level)); | 538 // Only send zoom level changes to WebContents that are using this |
483 } | 539 // HostZoomMap. |
alexmos
2016/04/27 23:39:51
Should this comment also be on the GetForWebConten
wjmaclean
2016/04/28 13:20:11
Done.
| |
540 int render_process_id = web_contents->GetRenderProcessHost()->GetID(); | |
541 int render_view_id = web_contents->GetRenderViewHost()->GetRoutingID(); | |
542 | |
543 if (!UsesTemporaryZoomLevel(render_process_id, render_view_id)) | |
544 web_contents->UpdateZoomIfNecessary(scheme, host, level); | |
484 } | 545 } |
485 } | 546 } |
486 | 547 |
487 void HostZoomMapImpl::SendErrorPageZoomLevelRefresh() { | 548 void HostZoomMapImpl::SendErrorPageZoomLevelRefresh() { |
488 GURL error_url(kUnreachableWebDataURL); | 549 GURL error_url(kUnreachableWebDataURL); |
489 std::string host = net::GetHostOrSpecFromURL(error_url); | 550 std::string host = net::GetHostOrSpecFromURL(error_url); |
490 double error_page_zoom_level = GetZoomLevelForHost(host); | 551 double error_page_zoom_level = GetZoomLevelForHost(host); |
491 | 552 |
492 SendZoomLevelChange(std::string(), host, error_page_zoom_level); | 553 SendZoomLevelChange(std::string(), host, error_page_zoom_level); |
493 } | 554 } |
494 | 555 |
495 HostZoomMapImpl::~HostZoomMapImpl() { | 556 HostZoomMapImpl::~HostZoomMapImpl() { |
496 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 557 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
497 } | 558 } |
498 | 559 |
499 } // namespace content | 560 } // namespace content |
OLD | NEW |