Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(241)

Side by Side Diff: content/browser/host_zoom_map_impl.cc

Issue 678963003: Allow zooming error pages. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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) {
Charlie Reis 2014/10/28 16:36:21 I'm confused why we're defining HostZoomMap method
wjmaclean 2014/10/28 17:37:04 I think the idea was to create convenience functio
Charlie Reis 2014/10/28 19:09:44 Thanks. I realized there's precedent for this in
53 switch (entry->GetPageType()) {
54 case PAGE_TYPE_ERROR:
55 return GURL(kUnreachableWebDataURL);
56 // In future, give interstitial pages special treatment as well.
Charlie Reis 2014/10/28 16:36:21 nit: In future -> TODO(wjmaclean):
wjmaclean 2014/10/28 17:37:04 Done.
57 default:
58 return entry->GetURL();
59 }
60 }
61
51 HostZoomMap* HostZoomMap::GetDefaultForBrowserContext(BrowserContext* context) { 62 HostZoomMap* HostZoomMap::GetDefaultForBrowserContext(BrowserContext* context) {
52 HostZoomMapImpl* rv = static_cast<HostZoomMapImpl*>( 63 HostZoomMapImpl* rv = static_cast<HostZoomMapImpl*>(
53 context->GetUserData(kHostZoomMapKeyName)); 64 context->GetUserData(kHostZoomMapKeyName));
54 if (!rv) { 65 if (!rv) {
55 rv = new HostZoomMapImpl(); 66 rv = new HostZoomMapImpl();
56 context->SetUserData(kHostZoomMapKeyName, rv); 67 context->SetUserData(kHostZoomMapKeyName, rv);
57 } 68 }
58 return rv; 69 return rv;
59 } 70 }
60 71
61 // Helper function for setting/getting zoom levels for WebContents without 72 // Helper function for setting/getting zoom levels for WebContents without
62 // having to import HostZoomMapImpl everywhere. 73 // having to import HostZoomMapImpl everywhere.
63 double HostZoomMap::GetZoomLevel(const WebContents* web_contents) { 74 double HostZoomMap::GetZoomLevel(const WebContents* web_contents) {
64 HostZoomMapImpl* host_zoom_map = 75 HostZoomMapImpl* host_zoom_map =
65 static_cast<HostZoomMapImpl*>(HostZoomMap::GetDefaultForBrowserContext( 76 static_cast<HostZoomMapImpl*>(HostZoomMap::GetDefaultForBrowserContext(
66 web_contents->GetBrowserContext())); 77 web_contents->GetBrowserContext()));
67 return host_zoom_map->GetZoomLevelForWebContents( 78 return host_zoom_map->GetZoomLevelForWebContents(
68 *static_cast<const WebContentsImpl*>(web_contents)); 79 *static_cast<const WebContentsImpl*>(web_contents));
69 } 80 }
70 81
71 void HostZoomMap::SetZoomLevel(const WebContents* web_contents, double level) { 82 void HostZoomMap::SetZoomLevel(const WebContents* web_contents, double level) {
72 HostZoomMapImpl* host_zoom_map = 83 HostZoomMapImpl* host_zoom_map =
73 static_cast<HostZoomMapImpl*>(HostZoomMap::GetDefaultForBrowserContext( 84 static_cast<HostZoomMapImpl*>(HostZoomMap::GetDefaultForBrowserContext(
74 web_contents->GetBrowserContext())); 85 web_contents->GetBrowserContext()));
75 host_zoom_map->SetZoomLevelForWebContents( 86 host_zoom_map->SetZoomLevelForWebContents(
76 *static_cast<const WebContentsImpl*>(web_contents), level); 87 *static_cast<const WebContentsImpl*>(web_contents), level);
77 } 88 }
78 89
90 void HostZoomMap::SendErrorPageZoomLevelRefresh(
91 const WebContents* web_contents) {
92 HostZoomMapImpl* host_zoom_map =
93 static_cast<HostZoomMapImpl*>(HostZoomMap::GetDefaultForBrowserContext(
94 web_contents->GetBrowserContext()));
95 host_zoom_map->SendErrorPageZoomLevelRefresh();
96 }
97
79 HostZoomMapImpl::HostZoomMapImpl() 98 HostZoomMapImpl::HostZoomMapImpl()
80 : default_zoom_level_(0.0) { 99 : default_zoom_level_(0.0) {
81 registrar_.Add( 100 registrar_.Add(
82 this, NOTIFICATION_RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW, 101 this, NOTIFICATION_RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW,
83 NotificationService::AllSources()); 102 NotificationService::AllSources());
84 } 103 }
85 104
86 void HostZoomMapImpl::CopyFrom(HostZoomMap* copy_interface) { 105 void HostZoomMapImpl::CopyFrom(HostZoomMap* copy_interface) {
87 // This can only be called on the UI thread to avoid deadlocks, otherwise 106 // This can only be called on the UI thread to avoid deadlocks, otherwise
88 // UI: a.CopyFrom(b); 107 // UI: a.CopyFrom(b);
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after
246 265
247 // Get the url from the navigation controller directly, as calling 266 // Get the url from the navigation controller directly, as calling
248 // WebContentsImpl::GetLastCommittedURL() may give us a virtual url that 267 // WebContentsImpl::GetLastCommittedURL() may give us a virtual url that
249 // is different than is stored in the map. 268 // is different than is stored in the map.
250 GURL url; 269 GURL url;
251 NavigationEntry* entry = 270 NavigationEntry* entry =
252 web_contents_impl.GetController().GetLastCommittedEntry(); 271 web_contents_impl.GetController().GetLastCommittedEntry();
253 // It is possible for a WebContent's zoom level to be queried before 272 // It is possible for a WebContent's zoom level to be queried before
254 // a navigation has occurred. 273 // a navigation has occurred.
255 if (entry) 274 if (entry)
256 url = entry->GetURL(); 275 url = GetURLFromEntry(entry);
257 return GetZoomLevelForHostAndScheme(url.scheme(), 276 return GetZoomLevelForHostAndScheme(url.scheme(),
258 net::GetHostOrSpecFromURL(url)); 277 net::GetHostOrSpecFromURL(url));
259 } 278 }
260 279
261 void HostZoomMapImpl::SetZoomLevelForWebContents( 280 void HostZoomMapImpl::SetZoomLevelForWebContents(
262 const WebContentsImpl& web_contents_impl, 281 const WebContentsImpl& web_contents_impl,
263 double level) { 282 double level) {
264 int render_process_id = web_contents_impl.GetRenderProcessHost()->GetID(); 283 int render_process_id = web_contents_impl.GetRenderProcessHost()->GetID();
265 int render_view_id = web_contents_impl.GetRenderViewHost()->GetRoutingID(); 284 int render_view_id = web_contents_impl.GetRenderViewHost()->GetRoutingID();
266 if (UsesTemporaryZoomLevel(render_process_id, render_view_id)) { 285 if (UsesTemporaryZoomLevel(render_process_id, render_view_id)) {
267 SetTemporaryZoomLevel(render_process_id, render_view_id, level); 286 SetTemporaryZoomLevel(render_process_id, render_view_id, level);
268 } else { 287 } else {
269 // Get the url from the navigation controller directly, as calling 288 // Get the url from the navigation controller directly, as calling
270 // WebContentsImpl::GetLastCommittedURL() may give us a virtual url that 289 // 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, 290 // is different than what the render view is using. If the two don't match,
272 // the attempt to set the zoom will fail. 291 // the attempt to set the zoom will fail.
273 NavigationEntry* entry = 292 NavigationEntry* entry =
274 web_contents_impl.GetController().GetLastCommittedEntry(); 293 web_contents_impl.GetController().GetLastCommittedEntry();
275 // Tests may invoke this function with a null entry, but we don't 294 // Tests may invoke this function with a null entry, but we don't
276 // want to save zoom levels in this case. 295 // want to save zoom levels in this case.
277 if (!entry) 296 if (!entry)
278 return; 297 return;
279 298
280 GURL url = entry->GetURL(); 299 GURL url = GetURLFromEntry(entry);
281 SetZoomLevelForHost(net::GetHostOrSpecFromURL(url), level); 300 SetZoomLevelForHost(net::GetHostOrSpecFromURL(url), level);
282 } 301 }
283 } 302 }
284 303
285 void HostZoomMapImpl::SetZoomLevelForView(int render_process_id, 304 void HostZoomMapImpl::SetZoomLevelForView(int render_process_id,
286 int render_view_id, 305 int render_view_id,
287 double level, 306 double level,
288 const std::string& host) { 307 const std::string& host) {
289 if (UsesTemporaryZoomLevel(render_process_id, render_view_id)) 308 if (UsesTemporaryZoomLevel(render_process_id, render_view_id))
290 SetTemporaryZoomLevel(render_process_id, render_view_id, level); 309 SetTemporaryZoomLevel(render_process_id, render_view_id, level);
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
377 !i.IsAtEnd(); i.Advance()) { 396 !i.IsAtEnd(); i.Advance()) {
378 RenderProcessHost* render_process_host = i.GetCurrentValue(); 397 RenderProcessHost* render_process_host = i.GetCurrentValue();
379 if (HostZoomMap::GetDefaultForBrowserContext( 398 if (HostZoomMap::GetDefaultForBrowserContext(
380 render_process_host->GetBrowserContext()) == this) { 399 render_process_host->GetBrowserContext()) == this) {
381 render_process_host->Send( 400 render_process_host->Send(
382 new ViewMsg_SetZoomLevelForCurrentURL(scheme, host, level)); 401 new ViewMsg_SetZoomLevelForCurrentURL(scheme, host, level));
383 } 402 }
384 } 403 }
385 } 404 }
386 405
406 void HostZoomMapImpl::SendErrorPageZoomLevelRefresh() {
407 GURL error_url(kUnreachableWebDataURL);
408 std::string host = net::GetHostOrSpecFromURL(error_url);
409 double error_page_zoom_level = GetZoomLevelForHost(host);
410 if (error_page_zoom_level == default_zoom_level_)
Charlie Reis 2014/10/28 16:36:21 What happens if the renderer had a different zoom
wjmaclean 2014/10/28 17:37:04 Good catch ... we should probably always just send
411 return;
412
413 SendZoomLevelChange("", host, error_page_zoom_level);
Bernhard Bauer 2014/10/28 14:55:22 Using an empty std::string() constructor instead o
wjmaclean 2014/10/28 17:37:04 I wondered if that might be better; I'll add it in
414 }
415
387 HostZoomMapImpl::~HostZoomMapImpl() { 416 HostZoomMapImpl::~HostZoomMapImpl() {
388 } 417 }
389 418
390 } // namespace content 419 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698