| 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/common/page_zoom.h" | 23 #include "content/public/common/page_zoom.h" |
| 24 #include "content/public/common/url_constants.h" |
| 24 #include "net/base/net_util.h" | 25 #include "net/base/net_util.h" |
| 25 | 26 |
| 26 namespace content { | 27 namespace content { |
| 27 | 28 |
| 28 namespace { | 29 namespace { |
| 29 | 30 |
| 30 const char kHostZoomMapKeyName[] = "content_host_zoom_map"; | 31 const char kHostZoomMapKeyName[] = "content_host_zoom_map"; |
| 31 | 32 |
| 32 std::string GetHostFromProcessView(int render_process_id, int render_view_id) { | 33 std::string GetHostFromProcessView(int render_process_id, int render_view_id) { |
| 33 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 34 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 34 RenderViewHost* render_view_host = | 35 RenderViewHost* render_view_host = |
| 35 RenderViewHost::FromID(render_process_id, render_view_id); | 36 RenderViewHost::FromID(render_process_id, render_view_id); |
| 36 if (!render_view_host) | 37 if (!render_view_host) |
| 37 return std::string(); | 38 return std::string(); |
| 38 | 39 |
| 39 WebContents* web_contents = WebContents::FromRenderViewHost(render_view_host); | 40 WebContents* web_contents = WebContents::FromRenderViewHost(render_view_host); |
| 40 | 41 |
| 41 NavigationEntry* entry = | 42 NavigationEntry* entry = |
| 42 web_contents->GetController().GetLastCommittedEntry(); | 43 web_contents->GetController().GetLastCommittedEntry(); |
| 43 if (!entry) | 44 if (!entry) |
| 44 return std::string(); | 45 return std::string(); |
| 45 | 46 |
| 46 return net::GetHostOrSpecFromURL(entry->GetURL()); | 47 return net::GetHostOrSpecFromURL(HostZoomMap::GetURLFromEntry(entry)); |
| 47 } | 48 } |
| 48 | 49 |
| 49 } // namespace | 50 } // namespace |
| 50 | 51 |
| 52 GURL HostZoomMap::GetURLFromEntry(const NavigationEntry* entry) { |
| 53 switch (entry->GetPageType()) { |
| 54 case PAGE_TYPE_ERROR: |
| 55 return GURL(kUnreachableWebDataURL); |
| 56 // TODO(wjmaclean): In future, give interstitial pages special treatment as |
| 57 // well. |
| 58 default: |
| 59 return entry->GetURL(); |
| 60 } |
| 61 } |
| 62 |
| 51 HostZoomMap* HostZoomMap::GetDefaultForBrowserContext(BrowserContext* context) { | 63 HostZoomMap* HostZoomMap::GetDefaultForBrowserContext(BrowserContext* context) { |
| 52 HostZoomMapImpl* rv = static_cast<HostZoomMapImpl*>( | 64 HostZoomMapImpl* rv = static_cast<HostZoomMapImpl*>( |
| 53 context->GetUserData(kHostZoomMapKeyName)); | 65 context->GetUserData(kHostZoomMapKeyName)); |
| 54 if (!rv) { | 66 if (!rv) { |
| 55 rv = new HostZoomMapImpl(); | 67 rv = new HostZoomMapImpl(); |
| 56 context->SetUserData(kHostZoomMapKeyName, rv); | 68 context->SetUserData(kHostZoomMapKeyName, rv); |
| 57 } | 69 } |
| 58 return rv; | 70 return rv; |
| 59 } | 71 } |
| 60 | 72 |
| 61 // Helper function for setting/getting zoom levels for WebContents without | 73 // Helper function for setting/getting zoom levels for WebContents without |
| 62 // having to import HostZoomMapImpl everywhere. | 74 // having to import HostZoomMapImpl everywhere. |
| 63 double HostZoomMap::GetZoomLevel(const WebContents* web_contents) { | 75 double HostZoomMap::GetZoomLevel(const WebContents* web_contents) { |
| 64 HostZoomMapImpl* host_zoom_map = | 76 HostZoomMapImpl* host_zoom_map = |
| 65 static_cast<HostZoomMapImpl*>(HostZoomMap::GetDefaultForBrowserContext( | 77 static_cast<HostZoomMapImpl*>(HostZoomMap::GetDefaultForBrowserContext( |
| 66 web_contents->GetBrowserContext())); | 78 web_contents->GetBrowserContext())); |
| 67 return host_zoom_map->GetZoomLevelForWebContents( | 79 return host_zoom_map->GetZoomLevelForWebContents( |
| 68 *static_cast<const WebContentsImpl*>(web_contents)); | 80 *static_cast<const WebContentsImpl*>(web_contents)); |
| 69 } | 81 } |
| 70 | 82 |
| 71 void HostZoomMap::SetZoomLevel(const WebContents* web_contents, double level) { | 83 void HostZoomMap::SetZoomLevel(const WebContents* web_contents, double level) { |
| 72 HostZoomMapImpl* host_zoom_map = | 84 HostZoomMapImpl* host_zoom_map = |
| 73 static_cast<HostZoomMapImpl*>(HostZoomMap::GetDefaultForBrowserContext( | 85 static_cast<HostZoomMapImpl*>(HostZoomMap::GetDefaultForBrowserContext( |
| 74 web_contents->GetBrowserContext())); | 86 web_contents->GetBrowserContext())); |
| 75 host_zoom_map->SetZoomLevelForWebContents( | 87 host_zoom_map->SetZoomLevelForWebContents( |
| 76 *static_cast<const WebContentsImpl*>(web_contents), level); | 88 *static_cast<const WebContentsImpl*>(web_contents), level); |
| 77 } | 89 } |
| 78 | 90 |
| 91 void HostZoomMap::SendErrorPageZoomLevelRefresh( |
| 92 const WebContents* web_contents) { |
| 93 HostZoomMapImpl* host_zoom_map = |
| 94 static_cast<HostZoomMapImpl*>(HostZoomMap::GetDefaultForBrowserContext( |
| 95 web_contents->GetBrowserContext())); |
| 96 host_zoom_map->SendErrorPageZoomLevelRefresh(); |
| 97 } |
| 98 |
| 79 HostZoomMapImpl::HostZoomMapImpl() | 99 HostZoomMapImpl::HostZoomMapImpl() |
| 80 : default_zoom_level_(0.0) { | 100 : default_zoom_level_(0.0) { |
| 81 registrar_.Add( | 101 registrar_.Add( |
| 82 this, NOTIFICATION_RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW, | 102 this, NOTIFICATION_RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW, |
| 83 NotificationService::AllSources()); | 103 NotificationService::AllSources()); |
| 84 } | 104 } |
| 85 | 105 |
| 86 void HostZoomMapImpl::CopyFrom(HostZoomMap* copy_interface) { | 106 void HostZoomMapImpl::CopyFrom(HostZoomMap* copy_interface) { |
| 87 // This can only be called on the UI thread to avoid deadlocks, otherwise | 107 // This can only be called on the UI thread to avoid deadlocks, otherwise |
| 88 // UI: a.CopyFrom(b); | 108 // UI: a.CopyFrom(b); |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 246 | 266 |
| 247 // Get the url from the navigation controller directly, as calling | 267 // Get the url from the navigation controller directly, as calling |
| 248 // WebContentsImpl::GetLastCommittedURL() may give us a virtual url that | 268 // WebContentsImpl::GetLastCommittedURL() may give us a virtual url that |
| 249 // is different than is stored in the map. | 269 // is different than is stored in the map. |
| 250 GURL url; | 270 GURL url; |
| 251 NavigationEntry* entry = | 271 NavigationEntry* entry = |
| 252 web_contents_impl.GetController().GetLastCommittedEntry(); | 272 web_contents_impl.GetController().GetLastCommittedEntry(); |
| 253 // It is possible for a WebContent's zoom level to be queried before | 273 // It is possible for a WebContent's zoom level to be queried before |
| 254 // a navigation has occurred. | 274 // a navigation has occurred. |
| 255 if (entry) | 275 if (entry) |
| 256 url = entry->GetURL(); | 276 url = GetURLFromEntry(entry); |
| 257 return GetZoomLevelForHostAndScheme(url.scheme(), | 277 return GetZoomLevelForHostAndScheme(url.scheme(), |
| 258 net::GetHostOrSpecFromURL(url)); | 278 net::GetHostOrSpecFromURL(url)); |
| 259 } | 279 } |
| 260 | 280 |
| 261 void HostZoomMapImpl::SetZoomLevelForWebContents( | 281 void HostZoomMapImpl::SetZoomLevelForWebContents( |
| 262 const WebContentsImpl& web_contents_impl, | 282 const WebContentsImpl& web_contents_impl, |
| 263 double level) { | 283 double level) { |
| 264 int render_process_id = web_contents_impl.GetRenderProcessHost()->GetID(); | 284 int render_process_id = web_contents_impl.GetRenderProcessHost()->GetID(); |
| 265 int render_view_id = web_contents_impl.GetRenderViewHost()->GetRoutingID(); | 285 int render_view_id = web_contents_impl.GetRenderViewHost()->GetRoutingID(); |
| 266 if (UsesTemporaryZoomLevel(render_process_id, render_view_id)) { | 286 if (UsesTemporaryZoomLevel(render_process_id, render_view_id)) { |
| 267 SetTemporaryZoomLevel(render_process_id, render_view_id, level); | 287 SetTemporaryZoomLevel(render_process_id, render_view_id, level); |
| 268 } else { | 288 } else { |
| 269 // Get the url from the navigation controller directly, as calling | 289 // Get the url from the navigation controller directly, as calling |
| 270 // WebContentsImpl::GetLastCommittedURL() may give us a virtual url that | 290 // WebContentsImpl::GetLastCommittedURL() may give us a virtual url that |
| 271 // is different than what the render view is using. If the two don't match, | 291 // is different than what the render view is using. If the two don't match, |
| 272 // the attempt to set the zoom will fail. | 292 // the attempt to set the zoom will fail. |
| 273 NavigationEntry* entry = | 293 NavigationEntry* entry = |
| 274 web_contents_impl.GetController().GetLastCommittedEntry(); | 294 web_contents_impl.GetController().GetLastCommittedEntry(); |
| 275 // Tests may invoke this function with a null entry, but we don't | 295 // Tests may invoke this function with a null entry, but we don't |
| 276 // want to save zoom levels in this case. | 296 // want to save zoom levels in this case. |
| 277 if (!entry) | 297 if (!entry) |
| 278 return; | 298 return; |
| 279 | 299 |
| 280 GURL url = entry->GetURL(); | 300 GURL url = GetURLFromEntry(entry); |
| 281 SetZoomLevelForHost(net::GetHostOrSpecFromURL(url), level); | 301 SetZoomLevelForHost(net::GetHostOrSpecFromURL(url), level); |
| 282 } | 302 } |
| 283 } | 303 } |
| 284 | 304 |
| 285 void HostZoomMapImpl::SetZoomLevelForView(int render_process_id, | 305 void HostZoomMapImpl::SetZoomLevelForView(int render_process_id, |
| 286 int render_view_id, | 306 int render_view_id, |
| 287 double level, | 307 double level, |
| 288 const std::string& host) { | 308 const std::string& host) { |
| 289 if (UsesTemporaryZoomLevel(render_process_id, render_view_id)) | 309 if (UsesTemporaryZoomLevel(render_process_id, render_view_id)) |
| 290 SetTemporaryZoomLevel(render_process_id, render_view_id, level); | 310 SetTemporaryZoomLevel(render_process_id, render_view_id, level); |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 377 !i.IsAtEnd(); i.Advance()) { | 397 !i.IsAtEnd(); i.Advance()) { |
| 378 RenderProcessHost* render_process_host = i.GetCurrentValue(); | 398 RenderProcessHost* render_process_host = i.GetCurrentValue(); |
| 379 if (HostZoomMap::GetDefaultForBrowserContext( | 399 if (HostZoomMap::GetDefaultForBrowserContext( |
| 380 render_process_host->GetBrowserContext()) == this) { | 400 render_process_host->GetBrowserContext()) == this) { |
| 381 render_process_host->Send( | 401 render_process_host->Send( |
| 382 new ViewMsg_SetZoomLevelForCurrentURL(scheme, host, level)); | 402 new ViewMsg_SetZoomLevelForCurrentURL(scheme, host, level)); |
| 383 } | 403 } |
| 384 } | 404 } |
| 385 } | 405 } |
| 386 | 406 |
| 407 void HostZoomMapImpl::SendErrorPageZoomLevelRefresh() { |
| 408 GURL error_url(kUnreachableWebDataURL); |
| 409 std::string host = net::GetHostOrSpecFromURL(error_url); |
| 410 double error_page_zoom_level = GetZoomLevelForHost(host); |
| 411 |
| 412 SendZoomLevelChange(std::string(), host, error_page_zoom_level); |
| 413 } |
| 414 |
| 387 HostZoomMapImpl::~HostZoomMapImpl() { | 415 HostZoomMapImpl::~HostZoomMapImpl() { |
| 388 } | 416 } |
| 389 | 417 |
| 390 } // namespace content | 418 } // namespace content |
| OLD | NEW |