Add ICU message format support
[chromium-blink-merge.git] / content / browser / host_zoom_map_impl.cc
blob2c1c5ec3e8d39d539714d9237d3a8d8ee746c665
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"
7 #include <algorithm>
8 #include <cmath>
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"
29 namespace content {
31 namespace {
33 std::string GetHostFromProcessView(int render_process_id, int render_view_id) {
34 DCHECK_CURRENTLY_ON(BrowserThread::UI);
35 RenderViewHost* render_view_host =
36 RenderViewHost::FromID(render_process_id, render_view_id);
37 if (!render_view_host)
38 return std::string();
40 WebContents* web_contents = WebContents::FromRenderViewHost(render_view_host);
42 NavigationEntry* entry =
43 web_contents->GetController().GetLastCommittedEntry();
44 if (!entry)
45 return std::string();
47 return net::GetHostOrSpecFromURL(HostZoomMap::GetURLFromEntry(entry));
50 } // namespace
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();
63 HostZoomMap* HostZoomMap::GetDefaultForBrowserContext(BrowserContext* context) {
64 StoragePartition* partition =
65 BrowserContext::GetDefaultStoragePartition(context);
66 DCHECK(partition);
67 return partition->GetHostZoomMap();
70 HostZoomMap* HostZoomMap::Get(SiteInstance* instance) {
71 StoragePartition* partition = BrowserContext::GetStoragePartition(
72 instance->GetBrowserContext(), instance);
73 DCHECK(partition);
74 return partition->GetHostZoomMap();
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();
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 bool HostZoomMap::PageScaleFactorIsOne(const WebContents* web_contents) {
95 HostZoomMapImpl* host_zoom_map = static_cast<HostZoomMapImpl*>(
96 HostZoomMap::GetForWebContents(web_contents));
97 return host_zoom_map->PageScaleFactorIsOneForWebContents(
98 *static_cast<const WebContentsImpl*>(web_contents));
101 void HostZoomMap::SetZoomLevel(const WebContents* web_contents, double level) {
102 HostZoomMapImpl* host_zoom_map = static_cast<HostZoomMapImpl*>(
103 HostZoomMap::GetForWebContents(web_contents));
104 host_zoom_map->SetZoomLevelForWebContents(
105 *static_cast<const WebContentsImpl*>(web_contents), level);
108 void HostZoomMap::SendErrorPageZoomLevelRefresh(
109 const WebContents* web_contents) {
110 HostZoomMapImpl* host_zoom_map =
111 static_cast<HostZoomMapImpl*>(HostZoomMap::GetDefaultForBrowserContext(
112 web_contents->GetBrowserContext()));
113 host_zoom_map->SendErrorPageZoomLevelRefresh();
116 HostZoomMapImpl::HostZoomMapImpl()
117 : default_zoom_level_(0.0) {
118 registrar_.Add(
119 this, NOTIFICATION_RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW,
120 NotificationService::AllSources());
123 void HostZoomMapImpl::CopyFrom(HostZoomMap* copy_interface) {
124 // This can only be called on the UI thread to avoid deadlocks, otherwise
125 // UI: a.CopyFrom(b);
126 // IO: b.CopyFrom(a);
127 // can deadlock.
128 DCHECK_CURRENTLY_ON(BrowserThread::UI);
129 HostZoomMapImpl* copy = static_cast<HostZoomMapImpl*>(copy_interface);
130 base::AutoLock auto_lock(lock_);
131 base::AutoLock copy_auto_lock(copy->lock_);
132 host_zoom_levels_.
133 insert(copy->host_zoom_levels_.begin(), copy->host_zoom_levels_.end());
134 for (SchemeHostZoomLevels::const_iterator i(copy->
135 scheme_host_zoom_levels_.begin());
136 i != copy->scheme_host_zoom_levels_.end(); ++i) {
137 scheme_host_zoom_levels_[i->first] = HostZoomLevels();
138 scheme_host_zoom_levels_[i->first].
139 insert(i->second.begin(), i->second.end());
141 default_zoom_level_ = copy->default_zoom_level_;
144 double HostZoomMapImpl::GetZoomLevelForHost(const std::string& host) const {
145 base::AutoLock auto_lock(lock_);
146 return GetZoomLevelForHostInternal(host);
149 double HostZoomMapImpl::GetZoomLevelForHostInternal(
150 const std::string& host) const {
151 HostZoomLevels::const_iterator i(host_zoom_levels_.find(host));
152 return (i == host_zoom_levels_.end()) ? default_zoom_level_ : i->second;
155 bool HostZoomMapImpl::HasZoomLevel(const std::string& scheme,
156 const std::string& host) const {
157 base::AutoLock auto_lock(lock_);
159 SchemeHostZoomLevels::const_iterator scheme_iterator(
160 scheme_host_zoom_levels_.find(scheme));
162 const HostZoomLevels& zoom_levels =
163 (scheme_iterator != scheme_host_zoom_levels_.end())
164 ? scheme_iterator->second
165 : host_zoom_levels_;
167 HostZoomLevels::const_iterator i(zoom_levels.find(host));
168 return i != zoom_levels.end();
171 double HostZoomMapImpl::GetZoomLevelForHostAndSchemeInternal(
172 const std::string& scheme,
173 const std::string& host) const {
174 SchemeHostZoomLevels::const_iterator scheme_iterator(
175 scheme_host_zoom_levels_.find(scheme));
176 if (scheme_iterator != scheme_host_zoom_levels_.end()) {
177 HostZoomLevels::const_iterator i(scheme_iterator->second.find(host));
178 if (i != scheme_iterator->second.end())
179 return i->second;
182 return GetZoomLevelForHostInternal(host);
185 double HostZoomMapImpl::GetZoomLevelForHostAndScheme(
186 const std::string& scheme,
187 const std::string& host) const {
188 base::AutoLock auto_lock(lock_);
189 return GetZoomLevelForHostAndSchemeInternal(scheme, host);
192 HostZoomMap::ZoomLevelVector HostZoomMapImpl::GetAllZoomLevels() const {
193 HostZoomMap::ZoomLevelVector result;
195 base::AutoLock auto_lock(lock_);
196 result.reserve(host_zoom_levels_.size() + scheme_host_zoom_levels_.size());
197 for (HostZoomLevels::const_iterator i = host_zoom_levels_.begin();
198 i != host_zoom_levels_.end();
199 ++i) {
200 ZoomLevelChange change = {HostZoomMap::ZOOM_CHANGED_FOR_HOST,
201 i->first, // host
202 std::string(), // scheme
203 i->second // zoom level
205 result.push_back(change);
207 for (SchemeHostZoomLevels::const_iterator i =
208 scheme_host_zoom_levels_.begin();
209 i != scheme_host_zoom_levels_.end();
210 ++i) {
211 const std::string& scheme = i->first;
212 const HostZoomLevels& host_zoom_levels = i->second;
213 for (HostZoomLevels::const_iterator j = host_zoom_levels.begin();
214 j != host_zoom_levels.end();
215 ++j) {
216 ZoomLevelChange change = {HostZoomMap::ZOOM_CHANGED_FOR_SCHEME_AND_HOST,
217 j->first, // host
218 scheme, // scheme
219 j->second // zoom level
221 result.push_back(change);
225 return result;
228 void HostZoomMapImpl::SetZoomLevelForHost(const std::string& host,
229 double level) {
230 DCHECK_CURRENTLY_ON(BrowserThread::UI);
233 base::AutoLock auto_lock(lock_);
235 if (ZoomValuesEqual(level, default_zoom_level_))
236 host_zoom_levels_.erase(host);
237 else
238 host_zoom_levels_[host] = level;
241 // TODO(wjmaclean) Should we use a GURL here? crbug.com/384486
242 SendZoomLevelChange(std::string(), host, level);
244 HostZoomMap::ZoomLevelChange change;
245 change.mode = HostZoomMap::ZOOM_CHANGED_FOR_HOST;
246 change.host = host;
247 change.zoom_level = level;
249 zoom_level_changed_callbacks_.Notify(change);
252 void HostZoomMapImpl::SetZoomLevelForHostAndScheme(const std::string& scheme,
253 const std::string& host,
254 double level) {
255 DCHECK_CURRENTLY_ON(BrowserThread::UI);
257 base::AutoLock auto_lock(lock_);
258 scheme_host_zoom_levels_[scheme][host] = level;
261 SendZoomLevelChange(scheme, host, level);
263 HostZoomMap::ZoomLevelChange change;
264 change.mode = HostZoomMap::ZOOM_CHANGED_FOR_SCHEME_AND_HOST;
265 change.host = host;
266 change.scheme = scheme;
267 change.zoom_level = level;
269 zoom_level_changed_callbacks_.Notify(change);
272 double HostZoomMapImpl::GetDefaultZoomLevel() const {
273 DCHECK_CURRENTLY_ON(BrowserThread::UI);
274 return default_zoom_level_;
277 void HostZoomMapImpl::SetDefaultZoomLevel(double level) {
278 DCHECK_CURRENTLY_ON(BrowserThread::UI);
279 default_zoom_level_ = level;
282 scoped_ptr<HostZoomMap::Subscription>
283 HostZoomMapImpl::AddZoomLevelChangedCallback(
284 const ZoomLevelChangedCallback& callback) {
285 return zoom_level_changed_callbacks_.Add(callback);
288 double HostZoomMapImpl::GetZoomLevelForWebContents(
289 const WebContentsImpl& web_contents_impl) const {
290 int render_process_id = web_contents_impl.GetRenderProcessHost()->GetID();
291 int routing_id = web_contents_impl.GetRenderViewHost()->GetRoutingID();
293 if (UsesTemporaryZoomLevel(render_process_id, routing_id))
294 return GetTemporaryZoomLevel(render_process_id, routing_id);
296 // Get the url from the navigation controller directly, as calling
297 // WebContentsImpl::GetLastCommittedURL() may give us a virtual url that
298 // is different than is stored in the map.
299 GURL url;
300 NavigationEntry* entry =
301 web_contents_impl.GetController().GetLastCommittedEntry();
302 // It is possible for a WebContent's zoom level to be queried before
303 // a navigation has occurred.
304 if (entry)
305 url = GetURLFromEntry(entry);
306 return GetZoomLevelForHostAndScheme(url.scheme(),
307 net::GetHostOrSpecFromURL(url));
310 void HostZoomMapImpl::SetZoomLevelForWebContents(
311 const WebContentsImpl& web_contents_impl,
312 double level) {
313 int render_process_id = web_contents_impl.GetRenderProcessHost()->GetID();
314 int render_view_id = web_contents_impl.GetRenderViewHost()->GetRoutingID();
315 if (UsesTemporaryZoomLevel(render_process_id, render_view_id)) {
316 SetTemporaryZoomLevel(render_process_id, render_view_id, level);
317 } else {
318 // Get the url from the navigation controller directly, as calling
319 // WebContentsImpl::GetLastCommittedURL() may give us a virtual url that
320 // is different than what the render view is using. If the two don't match,
321 // the attempt to set the zoom will fail.
322 NavigationEntry* entry =
323 web_contents_impl.GetController().GetLastCommittedEntry();
324 // Tests may invoke this function with a null entry, but we don't
325 // want to save zoom levels in this case.
326 if (!entry)
327 return;
329 GURL url = GetURLFromEntry(entry);
330 SetZoomLevelForHost(net::GetHostOrSpecFromURL(url), level);
334 void HostZoomMapImpl::SetZoomLevelForView(int render_process_id,
335 int render_view_id,
336 double level,
337 const std::string& host) {
338 if (UsesTemporaryZoomLevel(render_process_id, render_view_id))
339 SetTemporaryZoomLevel(render_process_id, render_view_id, level);
340 else
341 SetZoomLevelForHost(host, level);
344 void HostZoomMapImpl::SetPageScaleFactorIsOneForView(int render_process_id,
345 int render_view_id,
346 bool is_one) {
348 base::AutoLock auto_lock(lock_);
349 view_page_scale_factors_are_one_[RenderViewKey(render_process_id,
350 render_view_id)] = is_one;
352 HostZoomMap::ZoomLevelChange change;
353 change.mode = HostZoomMap::PAGE_SCALE_IS_ONE_CHANGED;
354 zoom_level_changed_callbacks_.Notify(change);
357 bool HostZoomMapImpl::PageScaleFactorIsOneForWebContents(
358 const WebContentsImpl& web_contents_impl) const {
359 if (!web_contents_impl.GetRenderProcessHost())
360 return true;
361 base::AutoLock auto_lock(lock_);
362 auto found = view_page_scale_factors_are_one_.find(
363 RenderViewKey(web_contents_impl.GetRenderProcessHost()->GetID(),
364 web_contents_impl.GetRoutingID()));
365 if (found == view_page_scale_factors_are_one_.end())
366 return true;
367 return found->second;
370 void HostZoomMapImpl::ClearPageScaleFactorIsOneForView(int render_process_id,
371 int render_view_id) {
372 base::AutoLock auto_lock(lock_);
373 view_page_scale_factors_are_one_.erase(
374 RenderViewKey(render_process_id, render_view_id));
377 bool HostZoomMapImpl::UsesTemporaryZoomLevel(int render_process_id,
378 int render_view_id) const {
379 RenderViewKey key(render_process_id, render_view_id);
381 base::AutoLock auto_lock(lock_);
382 return ContainsKey(temporary_zoom_levels_, key);
385 double HostZoomMapImpl::GetTemporaryZoomLevel(int render_process_id,
386 int render_view_id) const {
387 base::AutoLock auto_lock(lock_);
388 RenderViewKey key(render_process_id, render_view_id);
389 if (!ContainsKey(temporary_zoom_levels_, key))
390 return 0;
392 return temporary_zoom_levels_.find(key)->second;
395 void HostZoomMapImpl::SetTemporaryZoomLevel(int render_process_id,
396 int render_view_id,
397 double level) {
398 DCHECK_CURRENTLY_ON(BrowserThread::UI);
401 RenderViewKey key(render_process_id, render_view_id);
402 base::AutoLock auto_lock(lock_);
403 temporary_zoom_levels_[key] = level;
406 RenderViewHost* host =
407 RenderViewHost::FromID(render_process_id, render_view_id);
408 host->Send(new ViewMsg_SetZoomLevelForView(render_view_id, true, level));
410 HostZoomMap::ZoomLevelChange change;
411 change.mode = HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM;
412 change.host = GetHostFromProcessView(render_process_id, render_view_id);
413 change.zoom_level = level;
415 zoom_level_changed_callbacks_.Notify(change);
418 double HostZoomMapImpl::GetZoomLevelForView(const GURL& url,
419 int render_process_id,
420 int render_view_id) const {
421 RenderViewKey key(render_process_id, render_view_id);
422 base::AutoLock auto_lock(lock_);
424 if (ContainsKey(temporary_zoom_levels_, key))
425 return temporary_zoom_levels_.find(key)->second;
427 return GetZoomLevelForHostAndSchemeInternal(url.scheme(),
428 net::GetHostOrSpecFromURL(url));
431 void HostZoomMapImpl::Observe(int type,
432 const NotificationSource& source,
433 const NotificationDetails& details) {
434 switch (type) {
435 case NOTIFICATION_RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW: {
436 int render_view_id = Source<RenderViewHost>(source)->GetRoutingID();
437 int render_process_id =
438 Source<RenderViewHost>(source)->GetProcess()->GetID();
439 ClearTemporaryZoomLevel(render_process_id, render_view_id);
440 ClearPageScaleFactorIsOneForView(render_process_id, render_view_id);
441 break;
443 default:
444 NOTREACHED() << "Unexpected preference observed.";
448 void HostZoomMapImpl::ClearTemporaryZoomLevel(int render_process_id,
449 int render_view_id) {
451 base::AutoLock auto_lock(lock_);
452 RenderViewKey key(render_process_id, render_view_id);
453 TemporaryZoomLevels::iterator it = temporary_zoom_levels_.find(key);
454 if (it == temporary_zoom_levels_.end())
455 return;
456 temporary_zoom_levels_.erase(it);
458 RenderViewHost* host =
459 RenderViewHost::FromID(render_process_id, render_view_id);
460 DCHECK(host);
461 // Send a new zoom level, host-specific if one exists.
462 host->Send(new ViewMsg_SetZoomLevelForView(
463 render_view_id,
464 false,
465 GetZoomLevelForHost(
466 GetHostFromProcessView(render_process_id, render_view_id))));
469 void HostZoomMapImpl::SendZoomLevelChange(const std::string& scheme,
470 const std::string& host,
471 double level) {
472 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator());
473 !i.IsAtEnd(); i.Advance()) {
474 RenderProcessHost* render_process_host = i.GetCurrentValue();
475 // TODO(wjmaclean) This will need to be cleaned up when
476 // RenderProcessHost::GetStoragePartition() goes away. Perhaps have
477 // RenderProcessHost expose a GetHostZoomMap() function?
478 if (render_process_host->GetStoragePartition()->GetHostZoomMap() == this) {
479 render_process_host->Send(
480 new ViewMsg_SetZoomLevelForCurrentURL(scheme, host, level));
485 void HostZoomMapImpl::SendErrorPageZoomLevelRefresh() {
486 GURL error_url(kUnreachableWebDataURL);
487 std::string host = net::GetHostOrSpecFromURL(error_url);
488 double error_page_zoom_level = GetZoomLevelForHost(host);
490 SendZoomLevelChange(std::string(), host, error_page_zoom_level);
493 HostZoomMapImpl::~HostZoomMapImpl() {
494 DCHECK_CURRENTLY_ON(BrowserThread::UI);
497 } // namespace content