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/browser/site_instance.h"
24 #include "content/public/browser/storage_partition.h"
25 #include "content/public/common/page_zoom.h"
26 #include "content/public/common/url_constants.h"
27 #include "net/base/net_util.h"
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 StoragePartition
* partition
=
65 BrowserContext::GetDefaultStoragePartition(context
);
67 return partition
->GetHostZoomMap();
70 HostZoomMap
* HostZoomMap::Get(SiteInstance
* instance
) {
71 StoragePartition
* partition
= BrowserContext::GetStoragePartition(
72 instance
->GetBrowserContext(), instance
);
74 return partition
->GetHostZoomMap();
77 HostZoomMap
* HostZoomMap::GetForWebContents(const WebContents
* contents
) {
78 StoragePartition
* partition
=
79 BrowserContext::GetStoragePartition(contents
->GetBrowserContext(),
80 contents
->GetSiteInstance());
82 return partition
->GetHostZoomMap();
85 // Helper function for setting/getting zoom levels for WebContents without
86 // having to import HostZoomMapImpl everywhere.
87 double HostZoomMap::GetZoomLevel(const WebContents
* web_contents
) {
88 HostZoomMapImpl
* host_zoom_map
= static_cast<HostZoomMapImpl
*>(
89 HostZoomMap::GetForWebContents(web_contents
));
90 return host_zoom_map
->GetZoomLevelForWebContents(
91 *static_cast<const WebContentsImpl
*>(web_contents
));
94 void HostZoomMap::SetZoomLevel(const WebContents
* web_contents
, double level
) {
95 HostZoomMapImpl
* host_zoom_map
= static_cast<HostZoomMapImpl
*>(
96 HostZoomMap::GetForWebContents(web_contents
));
97 host_zoom_map
->SetZoomLevelForWebContents(
98 *static_cast<const WebContentsImpl
*>(web_contents
), level
);
101 void HostZoomMap::SendErrorPageZoomLevelRefresh(
102 const WebContents
* web_contents
) {
103 HostZoomMapImpl
* host_zoom_map
=
104 static_cast<HostZoomMapImpl
*>(HostZoomMap::GetDefaultForBrowserContext(
105 web_contents
->GetBrowserContext()));
106 host_zoom_map
->SendErrorPageZoomLevelRefresh();
109 HostZoomMapImpl::HostZoomMapImpl()
110 : default_zoom_level_(0.0) {
112 this, NOTIFICATION_RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW
,
113 NotificationService::AllSources());
116 void HostZoomMapImpl::CopyFrom(HostZoomMap
* copy_interface
) {
117 // This can only be called on the UI thread to avoid deadlocks, otherwise
118 // UI: a.CopyFrom(b);
119 // IO: b.CopyFrom(a);
121 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
122 HostZoomMapImpl
* copy
= static_cast<HostZoomMapImpl
*>(copy_interface
);
123 base::AutoLock
auto_lock(lock_
);
124 base::AutoLock
copy_auto_lock(copy
->lock_
);
126 insert(copy
->host_zoom_levels_
.begin(), copy
->host_zoom_levels_
.end());
127 for (SchemeHostZoomLevels::const_iterator
i(copy
->
128 scheme_host_zoom_levels_
.begin());
129 i
!= copy
->scheme_host_zoom_levels_
.end(); ++i
) {
130 scheme_host_zoom_levels_
[i
->first
] = HostZoomLevels();
131 scheme_host_zoom_levels_
[i
->first
].
132 insert(i
->second
.begin(), i
->second
.end());
134 default_zoom_level_
= copy
->default_zoom_level_
;
137 double HostZoomMapImpl::GetZoomLevelForHost(const std::string
& host
) const {
138 base::AutoLock
auto_lock(lock_
);
139 HostZoomLevels::const_iterator
i(host_zoom_levels_
.find(host
));
140 return (i
== host_zoom_levels_
.end()) ? default_zoom_level_
: i
->second
;
143 bool HostZoomMapImpl::HasZoomLevel(const std::string
& scheme
,
144 const std::string
& host
) const {
145 base::AutoLock
auto_lock(lock_
);
147 SchemeHostZoomLevels::const_iterator
scheme_iterator(
148 scheme_host_zoom_levels_
.find(scheme
));
150 const HostZoomLevels
& zoom_levels
=
151 (scheme_iterator
!= scheme_host_zoom_levels_
.end())
152 ? scheme_iterator
->second
155 HostZoomLevels::const_iterator
i(zoom_levels
.find(host
));
156 return i
!= zoom_levels
.end();
159 double HostZoomMapImpl::GetZoomLevelForHostAndScheme(
160 const std::string
& scheme
,
161 const std::string
& host
) const {
163 base::AutoLock
auto_lock(lock_
);
164 SchemeHostZoomLevels::const_iterator
scheme_iterator(
165 scheme_host_zoom_levels_
.find(scheme
));
166 if (scheme_iterator
!= scheme_host_zoom_levels_
.end()) {
167 HostZoomLevels::const_iterator
i(scheme_iterator
->second
.find(host
));
168 if (i
!= scheme_iterator
->second
.end())
172 return GetZoomLevelForHost(host
);
175 HostZoomMap::ZoomLevelVector
HostZoomMapImpl::GetAllZoomLevels() const {
176 HostZoomMap::ZoomLevelVector result
;
178 base::AutoLock
auto_lock(lock_
);
179 result
.reserve(host_zoom_levels_
.size() + scheme_host_zoom_levels_
.size());
180 for (HostZoomLevels::const_iterator i
= host_zoom_levels_
.begin();
181 i
!= host_zoom_levels_
.end();
183 ZoomLevelChange change
= {HostZoomMap::ZOOM_CHANGED_FOR_HOST
,
185 std::string(), // scheme
186 i
->second
// zoom level
188 result
.push_back(change
);
190 for (SchemeHostZoomLevels::const_iterator i
=
191 scheme_host_zoom_levels_
.begin();
192 i
!= scheme_host_zoom_levels_
.end();
194 const std::string
& scheme
= i
->first
;
195 const HostZoomLevels
& host_zoom_levels
= i
->second
;
196 for (HostZoomLevels::const_iterator j
= host_zoom_levels
.begin();
197 j
!= host_zoom_levels
.end();
199 ZoomLevelChange change
= {HostZoomMap::ZOOM_CHANGED_FOR_SCHEME_AND_HOST
,
202 j
->second
// zoom level
204 result
.push_back(change
);
211 void HostZoomMapImpl::SetZoomLevelForHost(const std::string
& host
,
213 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
216 base::AutoLock
auto_lock(lock_
);
218 if (ZoomValuesEqual(level
, default_zoom_level_
))
219 host_zoom_levels_
.erase(host
);
221 host_zoom_levels_
[host
] = level
;
224 // TODO(wjmaclean) Should we use a GURL here? crbug.com/384486
225 SendZoomLevelChange(std::string(), host
, level
);
227 HostZoomMap::ZoomLevelChange change
;
228 change
.mode
= HostZoomMap::ZOOM_CHANGED_FOR_HOST
;
230 change
.zoom_level
= level
;
232 zoom_level_changed_callbacks_
.Notify(change
);
235 void HostZoomMapImpl::SetZoomLevelForHostAndScheme(const std::string
& scheme
,
236 const std::string
& host
,
238 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
240 base::AutoLock
auto_lock(lock_
);
241 scheme_host_zoom_levels_
[scheme
][host
] = level
;
244 SendZoomLevelChange(scheme
, host
, level
);
246 HostZoomMap::ZoomLevelChange change
;
247 change
.mode
= HostZoomMap::ZOOM_CHANGED_FOR_SCHEME_AND_HOST
;
249 change
.scheme
= scheme
;
250 change
.zoom_level
= level
;
252 zoom_level_changed_callbacks_
.Notify(change
);
255 double HostZoomMapImpl::GetDefaultZoomLevel() const {
256 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
257 return default_zoom_level_
;
260 void HostZoomMapImpl::SetDefaultZoomLevel(double level
) {
261 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
262 default_zoom_level_
= level
;
265 scoped_ptr
<HostZoomMap::Subscription
>
266 HostZoomMapImpl::AddZoomLevelChangedCallback(
267 const ZoomLevelChangedCallback
& callback
) {
268 return zoom_level_changed_callbacks_
.Add(callback
);
271 double HostZoomMapImpl::GetZoomLevelForWebContents(
272 const WebContentsImpl
& web_contents_impl
) const {
273 int render_process_id
= web_contents_impl
.GetRenderProcessHost()->GetID();
274 int routing_id
= web_contents_impl
.GetRenderViewHost()->GetRoutingID();
276 if (UsesTemporaryZoomLevel(render_process_id
, routing_id
))
277 return GetTemporaryZoomLevel(render_process_id
, routing_id
);
279 // Get the url from the navigation controller directly, as calling
280 // WebContentsImpl::GetLastCommittedURL() may give us a virtual url that
281 // is different than is stored in the map.
283 NavigationEntry
* entry
=
284 web_contents_impl
.GetController().GetLastCommittedEntry();
285 // It is possible for a WebContent's zoom level to be queried before
286 // a navigation has occurred.
288 url
= GetURLFromEntry(entry
);
289 return GetZoomLevelForHostAndScheme(url
.scheme(),
290 net::GetHostOrSpecFromURL(url
));
293 void HostZoomMapImpl::SetZoomLevelForWebContents(
294 const WebContentsImpl
& web_contents_impl
,
296 int render_process_id
= web_contents_impl
.GetRenderProcessHost()->GetID();
297 int render_view_id
= web_contents_impl
.GetRenderViewHost()->GetRoutingID();
298 if (UsesTemporaryZoomLevel(render_process_id
, render_view_id
)) {
299 SetTemporaryZoomLevel(render_process_id
, render_view_id
, level
);
301 // Get the url from the navigation controller directly, as calling
302 // WebContentsImpl::GetLastCommittedURL() may give us a virtual url that
303 // is different than what the render view is using. If the two don't match,
304 // the attempt to set the zoom will fail.
305 NavigationEntry
* entry
=
306 web_contents_impl
.GetController().GetLastCommittedEntry();
307 // Tests may invoke this function with a null entry, but we don't
308 // want to save zoom levels in this case.
312 GURL url
= GetURLFromEntry(entry
);
313 SetZoomLevelForHost(net::GetHostOrSpecFromURL(url
), level
);
317 void HostZoomMapImpl::SetZoomLevelForView(int render_process_id
,
320 const std::string
& host
) {
321 if (UsesTemporaryZoomLevel(render_process_id
, render_view_id
))
322 SetTemporaryZoomLevel(render_process_id
, render_view_id
, level
);
324 SetZoomLevelForHost(host
, level
);
327 bool HostZoomMapImpl::UsesTemporaryZoomLevel(int render_process_id
,
328 int render_view_id
) const {
329 RenderViewKey
key(render_process_id
, render_view_id
);
331 base::AutoLock
auto_lock(lock_
);
332 return ContainsKey(temporary_zoom_levels_
, key
);
335 double HostZoomMapImpl::GetTemporaryZoomLevel(int render_process_id
,
336 int render_view_id
) const {
337 base::AutoLock
auto_lock(lock_
);
338 RenderViewKey
key(render_process_id
, render_view_id
);
339 if (!ContainsKey(temporary_zoom_levels_
, key
))
342 return temporary_zoom_levels_
.find(key
)->second
;
345 void HostZoomMapImpl::SetTemporaryZoomLevel(int render_process_id
,
348 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
351 RenderViewKey
key(render_process_id
, render_view_id
);
352 base::AutoLock
auto_lock(lock_
);
353 temporary_zoom_levels_
[key
] = level
;
356 RenderViewHost
* host
=
357 RenderViewHost::FromID(render_process_id
, render_view_id
);
358 host
->Send(new ViewMsg_SetZoomLevelForView(render_view_id
, true, level
));
360 HostZoomMap::ZoomLevelChange change
;
361 change
.mode
= HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM
;
362 change
.host
= GetHostFromProcessView(render_process_id
, render_view_id
);
363 change
.zoom_level
= level
;
365 zoom_level_changed_callbacks_
.Notify(change
);
368 void HostZoomMapImpl::Observe(int type
,
369 const NotificationSource
& source
,
370 const NotificationDetails
& details
) {
372 case NOTIFICATION_RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW
: {
373 int render_view_id
= Source
<RenderViewHost
>(source
)->GetRoutingID();
374 int render_process_id
=
375 Source
<RenderViewHost
>(source
)->GetProcess()->GetID();
376 ClearTemporaryZoomLevel(render_process_id
, render_view_id
);
380 NOTREACHED() << "Unexpected preference observed.";
384 void HostZoomMapImpl::ClearTemporaryZoomLevel(int render_process_id
,
385 int render_view_id
) {
387 base::AutoLock
auto_lock(lock_
);
388 RenderViewKey
key(render_process_id
, render_view_id
);
389 TemporaryZoomLevels::iterator it
= temporary_zoom_levels_
.find(key
);
390 if (it
== temporary_zoom_levels_
.end())
392 temporary_zoom_levels_
.erase(it
);
394 RenderViewHost
* host
=
395 RenderViewHost::FromID(render_process_id
, render_view_id
);
397 // Send a new zoom level, host-specific if one exists.
398 host
->Send(new ViewMsg_SetZoomLevelForView(
402 GetHostFromProcessView(render_process_id
, render_view_id
))));
405 void HostZoomMapImpl::SendZoomLevelChange(const std::string
& scheme
,
406 const std::string
& host
,
408 for (RenderProcessHost::iterator
i(RenderProcessHost::AllHostsIterator());
409 !i
.IsAtEnd(); i
.Advance()) {
410 RenderProcessHost
* render_process_host
= i
.GetCurrentValue();
411 // TODO(wjmaclean) This will need to be cleaned up when
412 // RenderProcessHost::GetStoragePartition() goes away. Perhaps have
413 // RenderProcessHost expose a GetHostZoomMap() function?
414 if (render_process_host
->GetStoragePartition()->GetHostZoomMap() == this) {
415 render_process_host
->Send(
416 new ViewMsg_SetZoomLevelForCurrentURL(scheme
, host
, level
));
421 void HostZoomMapImpl::SendErrorPageZoomLevelRefresh() {
422 GURL
error_url(kUnreachableWebDataURL
);
423 std::string host
= net::GetHostOrSpecFromURL(error_url
);
424 double error_page_zoom_level
= GetZoomLevelForHost(host
);
426 SendZoomLevelChange(std::string(), host
, error_page_zoom_level
);
429 HostZoomMapImpl::~HostZoomMapImpl() {
430 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
433 } // namespace content