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
3 // found in the LICENSE file.
5 #include "content/browser/host_zoom_map_impl.h"
10 #include "base/strings/string_piece.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/values.h"
13 #include "content/browser/frame_host/navigation_entry_impl.h"
14 #include "content/browser/renderer_host/render_process_host_impl.h"
15 #include "content/browser/renderer_host/render_view_host_impl.h"
16 #include "content/browser/web_contents/web_contents_impl.h"
17 #include "content/common/view_messages.h"
18 #include "content/public/browser/browser_context.h"
19 #include "content/public/browser/browser_thread.h"
20 #include "content/public/browser/notification_service.h"
21 #include "content/public/browser/notification_types.h"
22 #include "content/public/browser/resource_context.h"
23 #include "content/public/common/page_zoom.h"
24 #include "content/public/common/url_constants.h"
25 #include "net/base/net_util.h"
31 const char kHostZoomMapKeyName
[] = "content_host_zoom_map";
33 std::string
GetHostFromProcessView(int render_process_id
, int render_view_id
) {
34 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
35 RenderViewHost
* render_view_host
=
36 RenderViewHost::FromID(render_process_id
, render_view_id
);
37 if (!render_view_host
)
40 WebContents
* web_contents
= WebContents::FromRenderViewHost(render_view_host
);
42 NavigationEntry
* entry
=
43 web_contents
->GetController().GetLastCommittedEntry();
47 return net::GetHostOrSpecFromURL(HostZoomMap::GetURLFromEntry(entry
));
52 GURL
HostZoomMap::GetURLFromEntry(const NavigationEntry
* entry
) {
53 switch (entry
->GetPageType()) {
55 return GURL(kUnreachableWebDataURL
);
56 // TODO(wjmaclean): In future, give interstitial pages special treatment as
59 return entry
->GetURL();
63 HostZoomMap
* HostZoomMap::GetDefaultForBrowserContext(BrowserContext
* context
) {
64 HostZoomMapImpl
* rv
= static_cast<HostZoomMapImpl
*>(
65 context
->GetUserData(kHostZoomMapKeyName
));
67 rv
= new HostZoomMapImpl();
68 context
->SetUserData(kHostZoomMapKeyName
, rv
);
73 // Helper function for setting/getting zoom levels for WebContents without
74 // having to import HostZoomMapImpl everywhere.
75 double HostZoomMap::GetZoomLevel(const WebContents
* web_contents
) {
76 HostZoomMapImpl
* host_zoom_map
=
77 static_cast<HostZoomMapImpl
*>(HostZoomMap::GetDefaultForBrowserContext(
78 web_contents
->GetBrowserContext()));
79 return host_zoom_map
->GetZoomLevelForWebContents(
80 *static_cast<const WebContentsImpl
*>(web_contents
));
83 void HostZoomMap::SetZoomLevel(const WebContents
* web_contents
, double level
) {
84 HostZoomMapImpl
* host_zoom_map
=
85 static_cast<HostZoomMapImpl
*>(HostZoomMap::GetDefaultForBrowserContext(
86 web_contents
->GetBrowserContext()));
87 host_zoom_map
->SetZoomLevelForWebContents(
88 *static_cast<const WebContentsImpl
*>(web_contents
), level
);
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();
99 HostZoomMapImpl::HostZoomMapImpl()
100 : default_zoom_level_(0.0) {
102 this, NOTIFICATION_RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW
,
103 NotificationService::AllSources());
106 void HostZoomMapImpl::CopyFrom(HostZoomMap
* copy_interface
) {
107 // This can only be called on the UI thread to avoid deadlocks, otherwise
108 // UI: a.CopyFrom(b);
109 // IO: b.CopyFrom(a);
111 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
112 HostZoomMapImpl
* copy
= static_cast<HostZoomMapImpl
*>(copy_interface
);
113 base::AutoLock
auto_lock(lock_
);
114 base::AutoLock
copy_auto_lock(copy
->lock_
);
116 insert(copy
->host_zoom_levels_
.begin(), copy
->host_zoom_levels_
.end());
117 for (SchemeHostZoomLevels::const_iterator
i(copy
->
118 scheme_host_zoom_levels_
.begin());
119 i
!= copy
->scheme_host_zoom_levels_
.end(); ++i
) {
120 scheme_host_zoom_levels_
[i
->first
] = HostZoomLevels();
121 scheme_host_zoom_levels_
[i
->first
].
122 insert(i
->second
.begin(), i
->second
.end());
124 default_zoom_level_
= copy
->default_zoom_level_
;
127 double HostZoomMapImpl::GetZoomLevelForHost(const std::string
& host
) const {
128 base::AutoLock
auto_lock(lock_
);
129 HostZoomLevels::const_iterator
i(host_zoom_levels_
.find(host
));
130 return (i
== host_zoom_levels_
.end()) ? default_zoom_level_
: i
->second
;
133 bool HostZoomMapImpl::HasZoomLevel(const std::string
& scheme
,
134 const std::string
& host
) const {
135 base::AutoLock
auto_lock(lock_
);
137 SchemeHostZoomLevels::const_iterator
scheme_iterator(
138 scheme_host_zoom_levels_
.find(scheme
));
140 const HostZoomLevels
& zoom_levels
=
141 (scheme_iterator
!= scheme_host_zoom_levels_
.end())
142 ? scheme_iterator
->second
145 HostZoomLevels::const_iterator
i(zoom_levels
.find(host
));
146 return i
!= zoom_levels
.end();
149 double HostZoomMapImpl::GetZoomLevelForHostAndScheme(
150 const std::string
& scheme
,
151 const std::string
& host
) const {
153 base::AutoLock
auto_lock(lock_
);
154 SchemeHostZoomLevels::const_iterator
scheme_iterator(
155 scheme_host_zoom_levels_
.find(scheme
));
156 if (scheme_iterator
!= scheme_host_zoom_levels_
.end()) {
157 HostZoomLevels::const_iterator
i(scheme_iterator
->second
.find(host
));
158 if (i
!= scheme_iterator
->second
.end())
162 return GetZoomLevelForHost(host
);
165 HostZoomMap::ZoomLevelVector
HostZoomMapImpl::GetAllZoomLevels() const {
166 HostZoomMap::ZoomLevelVector result
;
168 base::AutoLock
auto_lock(lock_
);
169 result
.reserve(host_zoom_levels_
.size() + scheme_host_zoom_levels_
.size());
170 for (HostZoomLevels::const_iterator i
= host_zoom_levels_
.begin();
171 i
!= host_zoom_levels_
.end();
173 ZoomLevelChange change
= {HostZoomMap::ZOOM_CHANGED_FOR_HOST
,
175 std::string(), // scheme
176 i
->second
// zoom level
178 result
.push_back(change
);
180 for (SchemeHostZoomLevels::const_iterator i
=
181 scheme_host_zoom_levels_
.begin();
182 i
!= scheme_host_zoom_levels_
.end();
184 const std::string
& scheme
= i
->first
;
185 const HostZoomLevels
& host_zoom_levels
= i
->second
;
186 for (HostZoomLevels::const_iterator j
= host_zoom_levels
.begin();
187 j
!= host_zoom_levels
.end();
189 ZoomLevelChange change
= {HostZoomMap::ZOOM_CHANGED_FOR_SCHEME_AND_HOST
,
192 j
->second
// zoom level
194 result
.push_back(change
);
201 void HostZoomMapImpl::SetZoomLevelForHost(const std::string
& host
,
203 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
206 base::AutoLock
auto_lock(lock_
);
208 if (ZoomValuesEqual(level
, default_zoom_level_
))
209 host_zoom_levels_
.erase(host
);
211 host_zoom_levels_
[host
] = level
;
214 // TODO(wjmaclean) Should we use a GURL here? crbug.com/384486
215 SendZoomLevelChange(std::string(), host
, level
);
217 HostZoomMap::ZoomLevelChange change
;
218 change
.mode
= HostZoomMap::ZOOM_CHANGED_FOR_HOST
;
220 change
.zoom_level
= level
;
222 zoom_level_changed_callbacks_
.Notify(change
);
225 void HostZoomMapImpl::SetZoomLevelForHostAndScheme(const std::string
& scheme
,
226 const std::string
& host
,
228 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
230 base::AutoLock
auto_lock(lock_
);
231 scheme_host_zoom_levels_
[scheme
][host
] = level
;
234 SendZoomLevelChange(scheme
, host
, level
);
236 HostZoomMap::ZoomLevelChange change
;
237 change
.mode
= HostZoomMap::ZOOM_CHANGED_FOR_SCHEME_AND_HOST
;
239 change
.scheme
= scheme
;
240 change
.zoom_level
= level
;
242 zoom_level_changed_callbacks_
.Notify(change
);
245 double HostZoomMapImpl::GetDefaultZoomLevel() const {
246 return default_zoom_level_
;
249 void HostZoomMapImpl::SetDefaultZoomLevel(double level
) {
250 default_zoom_level_
= level
;
253 scoped_ptr
<HostZoomMap::Subscription
>
254 HostZoomMapImpl::AddZoomLevelChangedCallback(
255 const ZoomLevelChangedCallback
& callback
) {
256 return zoom_level_changed_callbacks_
.Add(callback
);
259 double HostZoomMapImpl::GetZoomLevelForWebContents(
260 const WebContentsImpl
& web_contents_impl
) const {
261 int render_process_id
= web_contents_impl
.GetRenderProcessHost()->GetID();
262 int routing_id
= web_contents_impl
.GetRenderViewHost()->GetRoutingID();
264 if (UsesTemporaryZoomLevel(render_process_id
, routing_id
))
265 return GetTemporaryZoomLevel(render_process_id
, routing_id
);
267 // Get the url from the navigation controller directly, as calling
268 // WebContentsImpl::GetLastCommittedURL() may give us a virtual url that
269 // is different than is stored in the map.
271 NavigationEntry
* entry
=
272 web_contents_impl
.GetController().GetLastCommittedEntry();
273 // It is possible for a WebContent's zoom level to be queried before
274 // a navigation has occurred.
276 url
= GetURLFromEntry(entry
);
277 return GetZoomLevelForHostAndScheme(url
.scheme(),
278 net::GetHostOrSpecFromURL(url
));
281 void HostZoomMapImpl::SetZoomLevelForWebContents(
282 const WebContentsImpl
& web_contents_impl
,
284 int render_process_id
= web_contents_impl
.GetRenderProcessHost()->GetID();
285 int render_view_id
= web_contents_impl
.GetRenderViewHost()->GetRoutingID();
286 if (UsesTemporaryZoomLevel(render_process_id
, render_view_id
)) {
287 SetTemporaryZoomLevel(render_process_id
, render_view_id
, level
);
289 // Get the url from the navigation controller directly, as calling
290 // WebContentsImpl::GetLastCommittedURL() may give us a virtual url that
291 // is different than what the render view is using. If the two don't match,
292 // the attempt to set the zoom will fail.
293 NavigationEntry
* entry
=
294 web_contents_impl
.GetController().GetLastCommittedEntry();
295 // Tests may invoke this function with a null entry, but we don't
296 // want to save zoom levels in this case.
300 GURL url
= GetURLFromEntry(entry
);
301 SetZoomLevelForHost(net::GetHostOrSpecFromURL(url
), level
);
305 void HostZoomMapImpl::SetZoomLevelForView(int render_process_id
,
308 const std::string
& host
) {
309 if (UsesTemporaryZoomLevel(render_process_id
, render_view_id
))
310 SetTemporaryZoomLevel(render_process_id
, render_view_id
, level
);
312 SetZoomLevelForHost(host
, level
);
315 bool HostZoomMapImpl::UsesTemporaryZoomLevel(int render_process_id
,
316 int render_view_id
) const {
317 RenderViewKey
key(render_process_id
, render_view_id
);
319 base::AutoLock
auto_lock(lock_
);
320 return ContainsKey(temporary_zoom_levels_
, key
);
323 double HostZoomMapImpl::GetTemporaryZoomLevel(int render_process_id
,
324 int render_view_id
) const {
325 base::AutoLock
auto_lock(lock_
);
326 RenderViewKey
key(render_process_id
, render_view_id
);
327 if (!ContainsKey(temporary_zoom_levels_
, key
))
330 return temporary_zoom_levels_
.find(key
)->second
;
333 void HostZoomMapImpl::SetTemporaryZoomLevel(int render_process_id
,
336 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
339 RenderViewKey
key(render_process_id
, render_view_id
);
340 base::AutoLock
auto_lock(lock_
);
341 temporary_zoom_levels_
[key
] = level
;
344 RenderViewHost
* host
=
345 RenderViewHost::FromID(render_process_id
, render_view_id
);
346 host
->Send(new ViewMsg_SetZoomLevelForView(render_view_id
, true, level
));
348 HostZoomMap::ZoomLevelChange change
;
349 change
.mode
= HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM
;
350 change
.host
= GetHostFromProcessView(render_process_id
, render_view_id
);
351 change
.zoom_level
= level
;
353 zoom_level_changed_callbacks_
.Notify(change
);
356 void HostZoomMapImpl::Observe(int type
,
357 const NotificationSource
& source
,
358 const NotificationDetails
& details
) {
360 case NOTIFICATION_RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW
: {
361 int render_view_id
= Source
<RenderViewHost
>(source
)->GetRoutingID();
362 int render_process_id
=
363 Source
<RenderViewHost
>(source
)->GetProcess()->GetID();
364 ClearTemporaryZoomLevel(render_process_id
, render_view_id
);
368 NOTREACHED() << "Unexpected preference observed.";
372 void HostZoomMapImpl::ClearTemporaryZoomLevel(int render_process_id
,
373 int render_view_id
) {
375 base::AutoLock
auto_lock(lock_
);
376 RenderViewKey
key(render_process_id
, render_view_id
);
377 TemporaryZoomLevels::iterator it
= temporary_zoom_levels_
.find(key
);
378 if (it
== temporary_zoom_levels_
.end())
380 temporary_zoom_levels_
.erase(it
);
382 RenderViewHost
* host
=
383 RenderViewHost::FromID(render_process_id
, render_view_id
);
385 // Send a new zoom level, host-specific if one exists.
386 host
->Send(new ViewMsg_SetZoomLevelForView(
390 GetHostFromProcessView(render_process_id
, render_view_id
))));
393 void HostZoomMapImpl::SendZoomLevelChange(const std::string
& scheme
,
394 const std::string
& host
,
396 for (RenderProcessHost::iterator
i(RenderProcessHost::AllHostsIterator());
397 !i
.IsAtEnd(); i
.Advance()) {
398 RenderProcessHost
* render_process_host
= i
.GetCurrentValue();
399 if (HostZoomMap::GetDefaultForBrowserContext(
400 render_process_host
->GetBrowserContext()) == this) {
401 render_process_host
->Send(
402 new ViewMsg_SetZoomLevelForCurrentURL(scheme
, host
, level
));
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
);
412 SendZoomLevelChange(std::string(), host
, error_page_zoom_level
);
415 HostZoomMapImpl::~HostZoomMapImpl() {
418 } // namespace content