| 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" |
| 11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 12 #include "base/values.h" | 12 #include "base/values.h" |
| 13 #include "content/browser/frame_host/navigation_entry_impl.h" | 13 #include "content/browser/frame_host/navigation_entry_impl.h" |
| 14 #include "content/browser/renderer_host/render_process_host_impl.h" | 14 #include "content/browser/renderer_host/render_process_host_impl.h" |
| 15 #include "content/browser/renderer_host/render_view_host_impl.h" | 15 #include "content/browser/renderer_host/render_view_host_impl.h" |
| 16 #include "content/browser/web_contents/web_contents_impl.h" | 16 #include "content/browser/web_contents/web_contents_impl.h" |
| 17 #include "content/common/view_messages.h" | 17 #include "content/common/view_messages.h" |
| 18 #include "content/public/browser/browser_context.h" | 18 #include "content/public/browser/browser_context.h" |
| 19 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
| 20 #include "content/public/browser/notification_service.h" | 20 #include "content/public/browser/notification_service.h" |
| 21 #include "content/public/browser/notification_types.h" | 21 #include "content/public/browser/notification_types.h" |
| 22 #include "content/public/browser/resource_context.h" | 22 #include "content/public/browser/resource_context.h" |
| 23 #include "content/public/browser/site_instance.h" |
| 24 #include "content/public/browser/storage_partition.h" |
| 23 #include "content/public/common/page_zoom.h" | 25 #include "content/public/common/page_zoom.h" |
| 24 #include "content/public/common/url_constants.h" | 26 #include "content/public/common/url_constants.h" |
| 25 #include "net/base/net_util.h" | 27 #include "net/base/net_util.h" |
| 26 | 28 |
| 27 namespace content { | 29 namespace content { |
| 28 | 30 |
| 29 namespace { | 31 namespace { |
| 30 | 32 |
| 31 const char kHostZoomMapKeyName[] = "content_host_zoom_map"; | |
| 32 | |
| 33 std::string GetHostFromProcessView(int render_process_id, int render_view_id) { | 33 std::string GetHostFromProcessView(int render_process_id, int render_view_id) { |
| 34 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 34 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 35 RenderViewHost* render_view_host = | 35 RenderViewHost* render_view_host = |
| 36 RenderViewHost::FromID(render_process_id, render_view_id); | 36 RenderViewHost::FromID(render_process_id, render_view_id); |
| 37 if (!render_view_host) | 37 if (!render_view_host) |
| 38 return std::string(); | 38 return std::string(); |
| 39 | 39 |
| 40 WebContents* web_contents = WebContents::FromRenderViewHost(render_view_host); | 40 WebContents* web_contents = WebContents::FromRenderViewHost(render_view_host); |
| 41 | 41 |
| 42 NavigationEntry* entry = | 42 NavigationEntry* entry = |
| (...skipping 11 matching lines...) Expand all Loading... |
| 54 case PAGE_TYPE_ERROR: | 54 case PAGE_TYPE_ERROR: |
| 55 return GURL(kUnreachableWebDataURL); | 55 return GURL(kUnreachableWebDataURL); |
| 56 // TODO(wjmaclean): In future, give interstitial pages special treatment as | 56 // TODO(wjmaclean): In future, give interstitial pages special treatment as |
| 57 // well. | 57 // well. |
| 58 default: | 58 default: |
| 59 return entry->GetURL(); | 59 return entry->GetURL(); |
| 60 } | 60 } |
| 61 } | 61 } |
| 62 | 62 |
| 63 HostZoomMap* HostZoomMap::GetDefaultForBrowserContext(BrowserContext* context) { | 63 HostZoomMap* HostZoomMap::GetDefaultForBrowserContext(BrowserContext* context) { |
| 64 HostZoomMapImpl* rv = static_cast<HostZoomMapImpl*>( | 64 StoragePartition* partition = |
| 65 context->GetUserData(kHostZoomMapKeyName)); | 65 BrowserContext::GetDefaultStoragePartition(context); |
| 66 if (!rv) { | 66 DCHECK(partition); |
| 67 rv = new HostZoomMapImpl(); | 67 return partition->GetHostZoomMap(); |
| 68 context->SetUserData(kHostZoomMapKeyName, rv); | 68 } |
| 69 } | 69 |
| 70 return rv; | 70 HostZoomMap* HostZoomMap::Get(SiteInstance* instance) { |
| 71 StoragePartition* partition = BrowserContext::GetStoragePartition( |
| 72 instance->GetBrowserContext(), instance); |
| 73 DCHECK(partition); |
| 74 return partition->GetHostZoomMap(); |
| 75 } |
| 76 |
| 77 HostZoomMap* HostZoomMap::GetForWebContents(const WebContents* contents) { |
| 78 StoragePartition* partition = |
| 79 BrowserContext::GetStoragePartition(contents->GetBrowserContext(), |
| 80 contents->GetSiteInstance()); |
| 81 DCHECK(partition); |
| 82 return partition->GetHostZoomMap(); |
| 71 } | 83 } |
| 72 | 84 |
| 73 // Helper function for setting/getting zoom levels for WebContents without | 85 // Helper function for setting/getting zoom levels for WebContents without |
| 74 // having to import HostZoomMapImpl everywhere. | 86 // having to import HostZoomMapImpl everywhere. |
| 75 double HostZoomMap::GetZoomLevel(const WebContents* web_contents) { | 87 double HostZoomMap::GetZoomLevel(const WebContents* web_contents) { |
| 76 HostZoomMapImpl* host_zoom_map = | 88 HostZoomMapImpl* host_zoom_map = static_cast<HostZoomMapImpl*>( |
| 77 static_cast<HostZoomMapImpl*>(HostZoomMap::GetDefaultForBrowserContext( | 89 HostZoomMap::GetForWebContents(web_contents)); |
| 78 web_contents->GetBrowserContext())); | |
| 79 return host_zoom_map->GetZoomLevelForWebContents( | 90 return host_zoom_map->GetZoomLevelForWebContents( |
| 80 *static_cast<const WebContentsImpl*>(web_contents)); | 91 *static_cast<const WebContentsImpl*>(web_contents)); |
| 81 } | 92 } |
| 82 | 93 |
| 83 void HostZoomMap::SetZoomLevel(const WebContents* web_contents, double level) { | 94 void HostZoomMap::SetZoomLevel(const WebContents* web_contents, double level) { |
| 84 HostZoomMapImpl* host_zoom_map = | 95 HostZoomMapImpl* host_zoom_map = static_cast<HostZoomMapImpl*>( |
| 85 static_cast<HostZoomMapImpl*>(HostZoomMap::GetDefaultForBrowserContext( | 96 HostZoomMap::GetForWebContents(web_contents)); |
| 86 web_contents->GetBrowserContext())); | |
| 87 host_zoom_map->SetZoomLevelForWebContents( | 97 host_zoom_map->SetZoomLevelForWebContents( |
| 88 *static_cast<const WebContentsImpl*>(web_contents), level); | 98 *static_cast<const WebContentsImpl*>(web_contents), level); |
| 89 } | 99 } |
| 90 | 100 |
| 91 void HostZoomMap::SendErrorPageZoomLevelRefresh( | 101 void HostZoomMap::SendErrorPageZoomLevelRefresh( |
| 92 const WebContents* web_contents) { | 102 const WebContents* web_contents) { |
| 93 HostZoomMapImpl* host_zoom_map = | 103 HostZoomMapImpl* host_zoom_map = |
| 94 static_cast<HostZoomMapImpl*>(HostZoomMap::GetDefaultForBrowserContext( | 104 static_cast<HostZoomMapImpl*>(HostZoomMap::GetDefaultForBrowserContext( |
| 95 web_contents->GetBrowserContext())); | 105 web_contents->GetBrowserContext())); |
| 96 host_zoom_map->SendErrorPageZoomLevelRefresh(); | 106 host_zoom_map->SendErrorPageZoomLevelRefresh(); |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 236 HostZoomMap::ZoomLevelChange change; | 246 HostZoomMap::ZoomLevelChange change; |
| 237 change.mode = HostZoomMap::ZOOM_CHANGED_FOR_SCHEME_AND_HOST; | 247 change.mode = HostZoomMap::ZOOM_CHANGED_FOR_SCHEME_AND_HOST; |
| 238 change.host = host; | 248 change.host = host; |
| 239 change.scheme = scheme; | 249 change.scheme = scheme; |
| 240 change.zoom_level = level; | 250 change.zoom_level = level; |
| 241 | 251 |
| 242 zoom_level_changed_callbacks_.Notify(change); | 252 zoom_level_changed_callbacks_.Notify(change); |
| 243 } | 253 } |
| 244 | 254 |
| 245 double HostZoomMapImpl::GetDefaultZoomLevel() const { | 255 double HostZoomMapImpl::GetDefaultZoomLevel() const { |
| 256 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 246 return default_zoom_level_; | 257 return default_zoom_level_; |
| 247 } | 258 } |
| 248 | 259 |
| 249 void HostZoomMapImpl::SetDefaultZoomLevel(double level) { | 260 void HostZoomMapImpl::SetDefaultZoomLevel(double level) { |
| 261 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 250 default_zoom_level_ = level; | 262 default_zoom_level_ = level; |
| 251 } | 263 } |
| 252 | 264 |
| 253 scoped_ptr<HostZoomMap::Subscription> | 265 scoped_ptr<HostZoomMap::Subscription> |
| 254 HostZoomMapImpl::AddZoomLevelChangedCallback( | 266 HostZoomMapImpl::AddZoomLevelChangedCallback( |
| 255 const ZoomLevelChangedCallback& callback) { | 267 const ZoomLevelChangedCallback& callback) { |
| 256 return zoom_level_changed_callbacks_.Add(callback); | 268 return zoom_level_changed_callbacks_.Add(callback); |
| 257 } | 269 } |
| 258 | 270 |
| 259 double HostZoomMapImpl::GetZoomLevelForWebContents( | 271 double HostZoomMapImpl::GetZoomLevelForWebContents( |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 389 GetZoomLevelForHost( | 401 GetZoomLevelForHost( |
| 390 GetHostFromProcessView(render_process_id, render_view_id)))); | 402 GetHostFromProcessView(render_process_id, render_view_id)))); |
| 391 } | 403 } |
| 392 | 404 |
| 393 void HostZoomMapImpl::SendZoomLevelChange(const std::string& scheme, | 405 void HostZoomMapImpl::SendZoomLevelChange(const std::string& scheme, |
| 394 const std::string& host, | 406 const std::string& host, |
| 395 double level) { | 407 double level) { |
| 396 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator()); | 408 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator()); |
| 397 !i.IsAtEnd(); i.Advance()) { | 409 !i.IsAtEnd(); i.Advance()) { |
| 398 RenderProcessHost* render_process_host = i.GetCurrentValue(); | 410 RenderProcessHost* render_process_host = i.GetCurrentValue(); |
| 399 if (HostZoomMap::GetDefaultForBrowserContext( | 411 // TODO(wjmaclean) This will need to be cleaned up when |
| 400 render_process_host->GetBrowserContext()) == this) { | 412 // RenderProcessHost::GetStoragePartition() goes away. Perhaps have |
| 413 // RenderProcessHost expose a GetHostZoomMap() function? |
| 414 if (render_process_host->GetStoragePartition()->GetHostZoomMap() == this) { |
| 401 render_process_host->Send( | 415 render_process_host->Send( |
| 402 new ViewMsg_SetZoomLevelForCurrentURL(scheme, host, level)); | 416 new ViewMsg_SetZoomLevelForCurrentURL(scheme, host, level)); |
| 403 } | 417 } |
| 404 } | 418 } |
| 405 } | 419 } |
| 406 | 420 |
| 407 void HostZoomMapImpl::SendErrorPageZoomLevelRefresh() { | 421 void HostZoomMapImpl::SendErrorPageZoomLevelRefresh() { |
| 408 GURL error_url(kUnreachableWebDataURL); | 422 GURL error_url(kUnreachableWebDataURL); |
| 409 std::string host = net::GetHostOrSpecFromURL(error_url); | 423 std::string host = net::GetHostOrSpecFromURL(error_url); |
| 410 double error_page_zoom_level = GetZoomLevelForHost(host); | 424 double error_page_zoom_level = GetZoomLevelForHost(host); |
| 411 | 425 |
| 412 SendZoomLevelChange(std::string(), host, error_page_zoom_level); | 426 SendZoomLevelChange(std::string(), host, error_page_zoom_level); |
| 413 } | 427 } |
| 414 | 428 |
| 415 HostZoomMapImpl::~HostZoomMapImpl() { | 429 HostZoomMapImpl::~HostZoomMapImpl() { |
| 430 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 416 } | 431 } |
| 417 | 432 |
| 418 } // namespace content | 433 } // namespace content |
| OLD | NEW |