| 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 int render_process_id = web_contents->GetRenderProcessHost()->GetID(); |
| 302 int render_view_id = web_contents->GetRenderViewHost()->GetRoutingID(); |
| 303 |
| 304 // Get the url from the navigation controller directly, as calling |
| 305 // WebContentsImpl::GetLastCommittedURL() may give us a virtual url that |
| 306 // is different than the one stored in the map. |
| 307 GURL url; |
| 308 NavigationEntry* entry = |
| 309 web_contents->GetController().GetLastCommittedEntry(); |
| 310 // It is possible for a WebContent's zoom level to be queried before |
| 311 // a navigation has occurred. |
| 312 if (entry) |
| 313 url = GetURLFromEntry(entry); |
| 314 |
| 315 bool uses_default_zoom = |
| 316 !HasZoomLevel(url.scheme(), net::GetHostOrSpecFromURL(url)) && |
| 317 !UsesTemporaryZoomLevel(render_process_id, render_view_id); |
| 318 |
| 319 // Only change zoom for WebContents tied to the StoragePartition this |
| 320 // HostZoomMap serves. |
| 321 if (GetForWebContents(web_contents) == this && uses_default_zoom) { |
| 322 web_contents->UpdateZoom(level); |
| 323 |
| 324 HostZoomMap::ZoomLevelChange change; |
| 325 change.mode = HostZoomMap::ZOOM_CHANGED_FOR_HOST; |
| 326 change.host = |
| 327 entry ? net::GetHostOrSpecFromURL(HostZoomMap::GetURLFromEntry(entry)) |
| 328 : std::string(); |
| 329 change.zoom_level = level; |
| 330 |
| 331 zoom_level_changed_callbacks_.Notify(change); |
| 332 } |
| 333 } |
| 282 } | 334 } |
| 283 | 335 |
| 284 scoped_ptr<HostZoomMap::Subscription> | 336 scoped_ptr<HostZoomMap::Subscription> |
| 285 HostZoomMapImpl::AddZoomLevelChangedCallback( | 337 HostZoomMapImpl::AddZoomLevelChangedCallback( |
| 286 const ZoomLevelChangedCallback& callback) { | 338 const ZoomLevelChangedCallback& callback) { |
| 287 return zoom_level_changed_callbacks_.Add(callback); | 339 return zoom_level_changed_callbacks_.Add(callback); |
| 288 } | 340 } |
| 289 | 341 |
| 290 double HostZoomMapImpl::GetZoomLevelForWebContents( | 342 double HostZoomMapImpl::GetZoomLevelForWebContents( |
| 291 const WebContentsImpl& web_contents_impl) const { | 343 const WebContentsImpl& web_contents_impl) const { |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 398 int render_view_id, | 450 int render_view_id, |
| 399 double level) { | 451 double level) { |
| 400 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 452 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 401 | 453 |
| 402 { | 454 { |
| 403 RenderViewKey key(render_process_id, render_view_id); | 455 RenderViewKey key(render_process_id, render_view_id); |
| 404 base::AutoLock auto_lock(lock_); | 456 base::AutoLock auto_lock(lock_); |
| 405 temporary_zoom_levels_[key] = level; | 457 temporary_zoom_levels_[key] = level; |
| 406 } | 458 } |
| 407 | 459 |
| 408 RenderViewHost* host = | 460 WebContentsImpl* web_contents = |
| 409 RenderViewHost::FromID(render_process_id, render_view_id); | 461 static_cast<WebContentsImpl*>(WebContents::FromRenderViewHost( |
| 410 host->Send(new ViewMsg_SetZoomLevelForView(render_view_id, true, level)); | 462 RenderViewHost::FromID(render_process_id, render_view_id))); |
| 463 web_contents->SetTemporaryZoomLevel(level, true); |
| 411 | 464 |
| 412 HostZoomMap::ZoomLevelChange change; | 465 HostZoomMap::ZoomLevelChange change; |
| 413 change.mode = HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM; | 466 change.mode = HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM; |
| 414 change.host = GetHostFromProcessView(render_process_id, render_view_id); | 467 change.host = GetHostFromProcessView(render_process_id, render_view_id); |
| 415 change.zoom_level = level; | 468 change.zoom_level = level; |
| 416 | 469 |
| 417 zoom_level_changed_callbacks_.Notify(change); | 470 zoom_level_changed_callbacks_.Notify(change); |
| 418 } | 471 } |
| 419 | 472 |
| 420 double HostZoomMapImpl::GetZoomLevelForView(const GURL& url, | 473 double HostZoomMapImpl::GetZoomLevelForView(const GURL& url, |
| (...skipping 29 matching lines...) Expand all Loading... |
| 450 void HostZoomMapImpl::ClearTemporaryZoomLevel(int render_process_id, | 503 void HostZoomMapImpl::ClearTemporaryZoomLevel(int render_process_id, |
| 451 int render_view_id) { | 504 int render_view_id) { |
| 452 { | 505 { |
| 453 base::AutoLock auto_lock(lock_); | 506 base::AutoLock auto_lock(lock_); |
| 454 RenderViewKey key(render_process_id, render_view_id); | 507 RenderViewKey key(render_process_id, render_view_id); |
| 455 TemporaryZoomLevels::iterator it = temporary_zoom_levels_.find(key); | 508 TemporaryZoomLevels::iterator it = temporary_zoom_levels_.find(key); |
| 456 if (it == temporary_zoom_levels_.end()) | 509 if (it == temporary_zoom_levels_.end()) |
| 457 return; | 510 return; |
| 458 temporary_zoom_levels_.erase(it); | 511 temporary_zoom_levels_.erase(it); |
| 459 } | 512 } |
| 460 RenderViewHost* host = | 513 WebContentsImpl* web_contents = |
| 461 RenderViewHost::FromID(render_process_id, render_view_id); | 514 static_cast<WebContentsImpl*>(WebContents::FromRenderViewHost( |
| 462 DCHECK(host); | 515 RenderViewHost::FromID(render_process_id, render_view_id))); |
| 463 // Send a new zoom level, host-specific if one exists. | 516 web_contents->SetTemporaryZoomLevel(GetZoomLevelForHost( |
| 464 host->Send(new ViewMsg_SetZoomLevelForView( | 517 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 } | 518 } |
| 470 | 519 |
| 471 void HostZoomMapImpl::SendZoomLevelChange(const std::string& scheme, | 520 void HostZoomMapImpl::SendZoomLevelChange(const std::string& scheme, |
| 472 const std::string& host, | 521 const std::string& host, |
| 473 double level) { | 522 double level) { |
| 474 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator()); | 523 // We'll only send to WebContents not using temporary zoom levels. The one |
| 475 !i.IsAtEnd(); i.Advance()) { | 524 // other case of interest is where the renderer is hosting a plugin document; |
| 476 RenderProcessHost* render_process_host = i.GetCurrentValue(); | 525 // that should be reflected in our temporary zoom level map, but we will |
| 477 // TODO(wjmaclean) This will need to be cleaned up when | 526 // double check on the renderer side to avoid the possibility of any races. |
| 478 // RenderProcessHost::GetStoragePartition() goes away. Perhaps have | 527 for (auto web_contents : WebContentsImpl::GetAllWebContents()) { |
| 479 // RenderProcessHost expose a GetHostZoomMap() function? | 528 // Only send zoom level changes to WebContents that are using this |
| 480 if (render_process_host->GetStoragePartition()->GetHostZoomMap() == this) { | 529 // HostZoomMap. |
| 481 render_process_host->Send( | 530 int render_process_id = web_contents->GetRenderProcessHost()->GetID(); |
| 482 new ViewMsg_SetZoomLevelForCurrentURL(scheme, host, level)); | 531 int render_view_id = web_contents->GetRenderViewHost()->GetRoutingID(); |
| 532 |
| 533 if (GetForWebContents(web_contents) == this && |
| 534 !UsesTemporaryZoomLevel(render_process_id, render_view_id)) { |
| 535 web_contents->UpdateZoomIfNecessary(scheme, host, level); |
| 483 } | 536 } |
| 484 } | 537 } |
| 485 } | 538 } |
| 486 | 539 |
| 487 void HostZoomMapImpl::SendErrorPageZoomLevelRefresh() { | 540 void HostZoomMapImpl::SendErrorPageZoomLevelRefresh() { |
| 488 GURL error_url(kUnreachableWebDataURL); | 541 GURL error_url(kUnreachableWebDataURL); |
| 489 std::string host = net::GetHostOrSpecFromURL(error_url); | 542 std::string host = net::GetHostOrSpecFromURL(error_url); |
| 490 double error_page_zoom_level = GetZoomLevelForHost(host); | 543 double error_page_zoom_level = GetZoomLevelForHost(host); |
| 491 | 544 |
| 492 SendZoomLevelChange(std::string(), host, error_page_zoom_level); | 545 SendZoomLevelChange(std::string(), host, error_page_zoom_level); |
| 493 } | 546 } |
| 494 | 547 |
| 495 HostZoomMapImpl::~HostZoomMapImpl() { | 548 HostZoomMapImpl::~HostZoomMapImpl() { |
| 496 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 549 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 497 } | 550 } |
| 498 | 551 |
| 499 } // namespace content | 552 } // namespace content |
| OLD | NEW |