ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / content / browser / host_zoom_map_impl.cc
blob4260bbf2397444f1a79a2e64e840adaef273a6d3
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(BrowserThread::CurrentlyOn(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 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) {
111 registrar_.Add(
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);
120 // can deadlock.
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_);
125 host_zoom_levels_.
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 return GetZoomLevelForHostInternal(host);
142 double HostZoomMapImpl::GetZoomLevelForHostInternal(
143 const std::string& host) const {
144 HostZoomLevels::const_iterator i(host_zoom_levels_.find(host));
145 return (i == host_zoom_levels_.end()) ? default_zoom_level_ : i->second;
148 bool HostZoomMapImpl::HasZoomLevel(const std::string& scheme,
149 const std::string& host) const {
150 base::AutoLock auto_lock(lock_);
152 SchemeHostZoomLevels::const_iterator scheme_iterator(
153 scheme_host_zoom_levels_.find(scheme));
155 const HostZoomLevels& zoom_levels =
156 (scheme_iterator != scheme_host_zoom_levels_.end())
157 ? scheme_iterator->second
158 : host_zoom_levels_;
160 HostZoomLevels::const_iterator i(zoom_levels.find(host));
161 return i != zoom_levels.end();
164 double HostZoomMapImpl::GetZoomLevelForHostAndSchemeInternal(
165 const std::string& scheme,
166 const std::string& host) const {
167 SchemeHostZoomLevels::const_iterator scheme_iterator(
168 scheme_host_zoom_levels_.find(scheme));
169 if (scheme_iterator != scheme_host_zoom_levels_.end()) {
170 HostZoomLevels::const_iterator i(scheme_iterator->second.find(host));
171 if (i != scheme_iterator->second.end())
172 return i->second;
175 return GetZoomLevelForHostInternal(host);
178 double HostZoomMapImpl::GetZoomLevelForHostAndScheme(
179 const std::string& scheme,
180 const std::string& host) const {
181 base::AutoLock auto_lock(lock_);
182 return GetZoomLevelForHostAndSchemeInternal(scheme, host);
185 HostZoomMap::ZoomLevelVector HostZoomMapImpl::GetAllZoomLevels() const {
186 HostZoomMap::ZoomLevelVector result;
188 base::AutoLock auto_lock(lock_);
189 result.reserve(host_zoom_levels_.size() + scheme_host_zoom_levels_.size());
190 for (HostZoomLevels::const_iterator i = host_zoom_levels_.begin();
191 i != host_zoom_levels_.end();
192 ++i) {
193 ZoomLevelChange change = {HostZoomMap::ZOOM_CHANGED_FOR_HOST,
194 i->first, // host
195 std::string(), // scheme
196 i->second // zoom level
198 result.push_back(change);
200 for (SchemeHostZoomLevels::const_iterator i =
201 scheme_host_zoom_levels_.begin();
202 i != scheme_host_zoom_levels_.end();
203 ++i) {
204 const std::string& scheme = i->first;
205 const HostZoomLevels& host_zoom_levels = i->second;
206 for (HostZoomLevels::const_iterator j = host_zoom_levels.begin();
207 j != host_zoom_levels.end();
208 ++j) {
209 ZoomLevelChange change = {HostZoomMap::ZOOM_CHANGED_FOR_SCHEME_AND_HOST,
210 j->first, // host
211 scheme, // scheme
212 j->second // zoom level
214 result.push_back(change);
218 return result;
221 void HostZoomMapImpl::SetZoomLevelForHost(const std::string& host,
222 double level) {
223 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
226 base::AutoLock auto_lock(lock_);
228 if (ZoomValuesEqual(level, default_zoom_level_))
229 host_zoom_levels_.erase(host);
230 else
231 host_zoom_levels_[host] = level;
234 // TODO(wjmaclean) Should we use a GURL here? crbug.com/384486
235 SendZoomLevelChange(std::string(), host, level);
237 HostZoomMap::ZoomLevelChange change;
238 change.mode = HostZoomMap::ZOOM_CHANGED_FOR_HOST;
239 change.host = host;
240 change.zoom_level = level;
242 zoom_level_changed_callbacks_.Notify(change);
245 void HostZoomMapImpl::SetZoomLevelForHostAndScheme(const std::string& scheme,
246 const std::string& host,
247 double level) {
248 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
250 base::AutoLock auto_lock(lock_);
251 scheme_host_zoom_levels_[scheme][host] = level;
254 SendZoomLevelChange(scheme, host, level);
256 HostZoomMap::ZoomLevelChange change;
257 change.mode = HostZoomMap::ZOOM_CHANGED_FOR_SCHEME_AND_HOST;
258 change.host = host;
259 change.scheme = scheme;
260 change.zoom_level = level;
262 zoom_level_changed_callbacks_.Notify(change);
265 double HostZoomMapImpl::GetDefaultZoomLevel() const {
266 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
267 return default_zoom_level_;
270 void HostZoomMapImpl::SetDefaultZoomLevel(double level) {
271 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
272 default_zoom_level_ = level;
275 scoped_ptr<HostZoomMap::Subscription>
276 HostZoomMapImpl::AddZoomLevelChangedCallback(
277 const ZoomLevelChangedCallback& callback) {
278 return zoom_level_changed_callbacks_.Add(callback);
281 double HostZoomMapImpl::GetZoomLevelForWebContents(
282 const WebContentsImpl& web_contents_impl) const {
283 int render_process_id = web_contents_impl.GetRenderProcessHost()->GetID();
284 int routing_id = web_contents_impl.GetRenderViewHost()->GetRoutingID();
286 if (UsesTemporaryZoomLevel(render_process_id, routing_id))
287 return GetTemporaryZoomLevel(render_process_id, routing_id);
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 is stored in the map.
292 GURL url;
293 NavigationEntry* entry =
294 web_contents_impl.GetController().GetLastCommittedEntry();
295 // It is possible for a WebContent's zoom level to be queried before
296 // a navigation has occurred.
297 if (entry)
298 url = GetURLFromEntry(entry);
299 return GetZoomLevelForHostAndScheme(url.scheme(),
300 net::GetHostOrSpecFromURL(url));
303 void HostZoomMapImpl::SetZoomLevelForWebContents(
304 const WebContentsImpl& web_contents_impl,
305 double level) {
306 int render_process_id = web_contents_impl.GetRenderProcessHost()->GetID();
307 int render_view_id = web_contents_impl.GetRenderViewHost()->GetRoutingID();
308 if (UsesTemporaryZoomLevel(render_process_id, render_view_id)) {
309 SetTemporaryZoomLevel(render_process_id, render_view_id, level);
310 } else {
311 // Get the url from the navigation controller directly, as calling
312 // WebContentsImpl::GetLastCommittedURL() may give us a virtual url that
313 // is different than what the render view is using. If the two don't match,
314 // the attempt to set the zoom will fail.
315 NavigationEntry* entry =
316 web_contents_impl.GetController().GetLastCommittedEntry();
317 // Tests may invoke this function with a null entry, but we don't
318 // want to save zoom levels in this case.
319 if (!entry)
320 return;
322 GURL url = GetURLFromEntry(entry);
323 SetZoomLevelForHost(net::GetHostOrSpecFromURL(url), level);
327 void HostZoomMapImpl::SetZoomLevelForView(int render_process_id,
328 int render_view_id,
329 double level,
330 const std::string& host) {
331 if (UsesTemporaryZoomLevel(render_process_id, render_view_id))
332 SetTemporaryZoomLevel(render_process_id, render_view_id, level);
333 else
334 SetZoomLevelForHost(host, level);
337 bool HostZoomMapImpl::UsesTemporaryZoomLevel(int render_process_id,
338 int render_view_id) const {
339 RenderViewKey key(render_process_id, render_view_id);
341 base::AutoLock auto_lock(lock_);
342 return ContainsKey(temporary_zoom_levels_, key);
345 double HostZoomMapImpl::GetTemporaryZoomLevel(int render_process_id,
346 int render_view_id) const {
347 base::AutoLock auto_lock(lock_);
348 RenderViewKey key(render_process_id, render_view_id);
349 if (!ContainsKey(temporary_zoom_levels_, key))
350 return 0;
352 return temporary_zoom_levels_.find(key)->second;
355 void HostZoomMapImpl::SetTemporaryZoomLevel(int render_process_id,
356 int render_view_id,
357 double level) {
358 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
361 RenderViewKey key(render_process_id, render_view_id);
362 base::AutoLock auto_lock(lock_);
363 temporary_zoom_levels_[key] = level;
366 RenderViewHost* host =
367 RenderViewHost::FromID(render_process_id, render_view_id);
368 host->Send(new ViewMsg_SetZoomLevelForView(render_view_id, true, level));
370 HostZoomMap::ZoomLevelChange change;
371 change.mode = HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM;
372 change.host = GetHostFromProcessView(render_process_id, render_view_id);
373 change.zoom_level = level;
375 zoom_level_changed_callbacks_.Notify(change);
378 double HostZoomMapImpl::GetZoomLevelForView(const GURL& url,
379 int render_process_id,
380 int render_view_id) const {
381 RenderViewKey key(render_process_id, render_view_id);
382 base::AutoLock auto_lock(lock_);
384 if (ContainsKey(temporary_zoom_levels_, key))
385 return temporary_zoom_levels_.find(key)->second;
387 return GetZoomLevelForHostAndSchemeInternal(url.scheme(),
388 net::GetHostOrSpecFromURL(url));
391 void HostZoomMapImpl::Observe(int type,
392 const NotificationSource& source,
393 const NotificationDetails& details) {
394 switch (type) {
395 case NOTIFICATION_RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW: {
396 int render_view_id = Source<RenderViewHost>(source)->GetRoutingID();
397 int render_process_id =
398 Source<RenderViewHost>(source)->GetProcess()->GetID();
399 ClearTemporaryZoomLevel(render_process_id, render_view_id);
400 break;
402 default:
403 NOTREACHED() << "Unexpected preference observed.";
407 void HostZoomMapImpl::ClearTemporaryZoomLevel(int render_process_id,
408 int render_view_id) {
410 base::AutoLock auto_lock(lock_);
411 RenderViewKey key(render_process_id, render_view_id);
412 TemporaryZoomLevels::iterator it = temporary_zoom_levels_.find(key);
413 if (it == temporary_zoom_levels_.end())
414 return;
415 temporary_zoom_levels_.erase(it);
417 RenderViewHost* host =
418 RenderViewHost::FromID(render_process_id, render_view_id);
419 DCHECK(host);
420 // Send a new zoom level, host-specific if one exists.
421 host->Send(new ViewMsg_SetZoomLevelForView(
422 render_view_id,
423 false,
424 GetZoomLevelForHost(
425 GetHostFromProcessView(render_process_id, render_view_id))));
428 void HostZoomMapImpl::SendZoomLevelChange(const std::string& scheme,
429 const std::string& host,
430 double level) {
431 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator());
432 !i.IsAtEnd(); i.Advance()) {
433 RenderProcessHost* render_process_host = i.GetCurrentValue();
434 // TODO(wjmaclean) This will need to be cleaned up when
435 // RenderProcessHost::GetStoragePartition() goes away. Perhaps have
436 // RenderProcessHost expose a GetHostZoomMap() function?
437 if (render_process_host->GetStoragePartition()->GetHostZoomMap() == this) {
438 render_process_host->Send(
439 new ViewMsg_SetZoomLevelForCurrentURL(scheme, host, level));
444 void HostZoomMapImpl::SendErrorPageZoomLevelRefresh() {
445 GURL error_url(kUnreachableWebDataURL);
446 std::string host = net::GetHostOrSpecFromURL(error_url);
447 double error_page_zoom_level = GetZoomLevelForHost(host);
449 SendZoomLevelChange(std::string(), host, error_page_zoom_level);
452 HostZoomMapImpl::~HostZoomMapImpl() {
453 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
456 } // namespace content