OLD | NEW |
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/browser/site_instance.h" |
| 24 #include "content/public/browser/storage_partition.h" |
23 #include "content/public/common/page_zoom.h" | 25 #include "content/public/common/page_zoom.h" |
24 #include "net/base/net_util.h" | 26 #include "net/base/net_util.h" |
25 | 27 |
26 static const char* kHostZoomMapKeyName = "content_host_zoom_map"; | |
27 | |
28 namespace content { | 28 namespace content { |
29 | 29 |
30 namespace { | 30 namespace { |
31 | 31 |
32 std::string GetHostFromProcessView(int render_process_id, int render_view_id) { | 32 std::string GetHostFromProcessView(int render_process_id, int render_view_id) { |
33 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 33 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
34 RenderViewHost* render_view_host = | 34 RenderViewHost* render_view_host = |
35 RenderViewHost::FromID(render_process_id, render_view_id); | 35 RenderViewHost::FromID(render_process_id, render_view_id); |
36 if (!render_view_host) | 36 if (!render_view_host) |
37 return std::string(); | 37 return std::string(); |
38 | 38 |
39 WebContents* web_contents = WebContents::FromRenderViewHost(render_view_host); | 39 WebContents* web_contents = WebContents::FromRenderViewHost(render_view_host); |
40 | 40 |
41 NavigationEntry* entry = | 41 NavigationEntry* entry = |
42 web_contents->GetController().GetLastCommittedEntry(); | 42 web_contents->GetController().GetLastCommittedEntry(); |
43 if (!entry) | 43 if (!entry) |
44 return std::string(); | 44 return std::string(); |
45 | 45 |
46 return net::GetHostOrSpecFromURL(entry->GetURL()); | 46 return net::GetHostOrSpecFromURL(entry->GetURL()); |
47 } | 47 } |
48 | 48 |
49 } // namespace | 49 } // namespace |
50 | 50 |
51 HostZoomMap* HostZoomMap::GetForBrowserContext(BrowserContext* context) { | 51 HostZoomMap* HostZoomMap::GetDefaultForBrowserContext(BrowserContext* context) { |
52 HostZoomMapImpl* rv = static_cast<HostZoomMapImpl*>( | 52 StoragePartition* partition = |
53 context->GetUserData(kHostZoomMapKeyName)); | 53 BrowserContext::GetDefaultStoragePartition(context); |
54 if (!rv) { | 54 DCHECK(partition); |
55 rv = new HostZoomMapImpl(); | 55 return partition->GetHostZoomMap(); |
56 context->SetUserData(kHostZoomMapKeyName, rv); | 56 } |
57 } | 57 |
58 return rv; | 58 HostZoomMap* HostZoomMap::Get(SiteInstance* instance) { |
| 59 StoragePartition* partition = BrowserContext::GetStoragePartition( |
| 60 instance->GetBrowserContext(), instance); |
| 61 DCHECK(partition); |
| 62 return partition->GetHostZoomMap(); |
| 63 } |
| 64 |
| 65 HostZoomMap* HostZoomMap::GetForWebContents(const WebContents* contents) { |
| 66 StoragePartition* partition = |
| 67 BrowserContext::GetStoragePartition(contents->GetBrowserContext(), |
| 68 contents->GetSiteInstance()); |
| 69 DCHECK(partition); |
| 70 return partition->GetHostZoomMap(); |
59 } | 71 } |
60 | 72 |
61 // Helper function for setting/getting zoom levels for WebContents without | 73 // Helper function for setting/getting zoom levels for WebContents without |
62 // having to import HostZoomMapImpl everywhere. | 74 // having to import HostZoomMapImpl everywhere. |
63 double HostZoomMap::GetZoomLevel(const WebContents* web_contents) { | 75 double HostZoomMap::GetZoomLevel(const WebContents* web_contents) { |
64 HostZoomMapImpl* host_zoom_map = static_cast<HostZoomMapImpl*>( | 76 HostZoomMapImpl* host_zoom_map = static_cast<HostZoomMapImpl*>( |
65 HostZoomMap::GetForBrowserContext(web_contents->GetBrowserContext())); | 77 HostZoomMap::GetForWebContents(web_contents)); |
66 return host_zoom_map->GetZoomLevelForWebContents( | 78 return host_zoom_map->GetZoomLevelForWebContents( |
67 *static_cast<const WebContentsImpl*>(web_contents)); | 79 *static_cast<const WebContentsImpl*>(web_contents)); |
68 } | 80 } |
69 | 81 |
70 void HostZoomMap::SetZoomLevel(const WebContents* web_contents, double level) { | 82 void HostZoomMap::SetZoomLevel(const WebContents* web_contents, double level) { |
71 HostZoomMapImpl* host_zoom_map = static_cast<HostZoomMapImpl*>( | 83 HostZoomMapImpl* host_zoom_map = static_cast<HostZoomMapImpl*>( |
72 HostZoomMap::GetForBrowserContext(web_contents->GetBrowserContext())); | 84 HostZoomMap::GetForWebContents(web_contents)); |
73 host_zoom_map->SetZoomLevelForWebContents( | 85 host_zoom_map->SetZoomLevelForWebContents( |
74 *static_cast<const WebContentsImpl*>(web_contents), level); | 86 *static_cast<const WebContentsImpl*>(web_contents), level); |
75 } | 87 } |
76 | 88 |
77 HostZoomMapImpl::HostZoomMapImpl() | 89 HostZoomMapImpl::HostZoomMapImpl() : default_zoom_level_(0.0) { |
78 : default_zoom_level_(0.0) { | |
79 registrar_.Add( | 90 registrar_.Add( |
80 this, NOTIFICATION_RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW, | 91 this, NOTIFICATION_RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW, |
81 NotificationService::AllSources()); | 92 NotificationService::AllSources()); |
82 } | 93 } |
83 | 94 |
84 void HostZoomMapImpl::CopyFrom(HostZoomMap* copy_interface) { | 95 void HostZoomMapImpl::CopyFrom(HostZoomMap* copy_interface) { |
85 // This can only be called on the UI thread to avoid deadlocks, otherwise | 96 // This can only be called on the UI thread to avoid deadlocks, otherwise |
86 // UI: a.CopyFrom(b); | 97 // UI: a.CopyFrom(b); |
87 // IO: b.CopyFrom(a); | 98 // IO: b.CopyFrom(a); |
88 // can deadlock. | 99 // can deadlock. |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 scheme_host_zoom_levels_.find(scheme)); | 144 scheme_host_zoom_levels_.find(scheme)); |
134 if (scheme_iterator != scheme_host_zoom_levels_.end()) { | 145 if (scheme_iterator != scheme_host_zoom_levels_.end()) { |
135 HostZoomLevels::const_iterator i(scheme_iterator->second.find(host)); | 146 HostZoomLevels::const_iterator i(scheme_iterator->second.find(host)); |
136 if (i != scheme_iterator->second.end()) | 147 if (i != scheme_iterator->second.end()) |
137 return i->second; | 148 return i->second; |
138 } | 149 } |
139 } | 150 } |
140 return GetZoomLevelForHost(host); | 151 return GetZoomLevelForHost(host); |
141 } | 152 } |
142 | 153 |
| 154 scoped_ptr<base::DictionaryValue> HostZoomMapImpl::GetZoomLevelDictionary() |
| 155 const { |
| 156 scoped_ptr<base::DictionaryValue> zoom_level_dictionary( |
| 157 new base::DictionaryValue()); |
| 158 { |
| 159 base::AutoLock auto_lock(lock_); |
| 160 |
| 161 for (HostZoomLevels::const_iterator i = host_zoom_levels_.begin(); |
| 162 i != host_zoom_levels_.end(); |
| 163 ++i) { |
| 164 if (ZoomValuesEqual(i->second, default_zoom_level_)) continue; |
| 165 |
| 166 zoom_level_dictionary->SetDoubleWithoutPathExpansion( |
| 167 i->first /* host */, i->second /* level */); |
| 168 } |
| 169 } |
| 170 return zoom_level_dictionary.Pass(); |
| 171 } |
| 172 |
| 173 // TODO(wjmaclean) The only non-test caller of this just throws away the non- |
| 174 // host entries and makes a dictionary, and so could probably be converted to |
| 175 // use GetZoomLevelDictionary() instead. The test might also be amenable to |
| 176 // something similar. |
143 HostZoomMap::ZoomLevelVector HostZoomMapImpl::GetAllZoomLevels() const { | 177 HostZoomMap::ZoomLevelVector HostZoomMapImpl::GetAllZoomLevels() const { |
144 HostZoomMap::ZoomLevelVector result; | 178 HostZoomMap::ZoomLevelVector result; |
145 { | 179 { |
146 base::AutoLock auto_lock(lock_); | 180 base::AutoLock auto_lock(lock_); |
147 result.reserve(host_zoom_levels_.size() + scheme_host_zoom_levels_.size()); | 181 result.reserve(host_zoom_levels_.size() + scheme_host_zoom_levels_.size()); |
148 for (HostZoomLevels::const_iterator i = host_zoom_levels_.begin(); | 182 for (HostZoomLevels::const_iterator i = host_zoom_levels_.begin(); |
149 i != host_zoom_levels_.end(); | 183 i != host_zoom_levels_.end(); |
150 ++i) { | 184 ++i) { |
151 ZoomLevelChange change = {HostZoomMap::ZOOM_CHANGED_FOR_HOST, | 185 ZoomLevelChange change = {HostZoomMap::ZOOM_CHANGED_FOR_HOST, |
152 i->first, // host | 186 i->first, // host |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
218 change.zoom_level = level; | 252 change.zoom_level = level; |
219 | 253 |
220 zoom_level_changed_callbacks_.Notify(change); | 254 zoom_level_changed_callbacks_.Notify(change); |
221 } | 255 } |
222 | 256 |
223 double HostZoomMapImpl::GetDefaultZoomLevel() const { | 257 double HostZoomMapImpl::GetDefaultZoomLevel() const { |
224 return default_zoom_level_; | 258 return default_zoom_level_; |
225 } | 259 } |
226 | 260 |
227 void HostZoomMapImpl::SetDefaultZoomLevel(double level) { | 261 void HostZoomMapImpl::SetDefaultZoomLevel(double level) { |
| 262 if (level == default_zoom_level_) |
| 263 return; |
| 264 |
228 default_zoom_level_ = level; | 265 default_zoom_level_ = level; |
| 266 |
| 267 HostZoomMap::ZoomLevelChange change; |
| 268 change.mode = HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM; |
| 269 change.zoom_level = level; |
| 270 |
| 271 zoom_level_changed_callbacks_.Notify(change); |
229 } | 272 } |
230 | 273 |
231 scoped_ptr<HostZoomMap::Subscription> | 274 scoped_ptr<HostZoomMap::Subscription> |
232 HostZoomMapImpl::AddZoomLevelChangedCallback( | 275 HostZoomMapImpl::AddZoomLevelChangedCallback( |
233 const ZoomLevelChangedCallback& callback) { | 276 const ZoomLevelChangedCallback& callback) { |
234 return zoom_level_changed_callbacks_.Add(callback); | 277 return zoom_level_changed_callbacks_.Add(callback); |
235 } | 278 } |
236 | 279 |
237 double HostZoomMapImpl::GetZoomLevelForWebContents( | 280 double HostZoomMapImpl::GetZoomLevelForWebContents( |
238 const WebContentsImpl& web_contents_impl) const { | 281 const WebContentsImpl& web_contents_impl) const { |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
367 GetZoomLevelForHost( | 410 GetZoomLevelForHost( |
368 GetHostFromProcessView(render_process_id, render_view_id)))); | 411 GetHostFromProcessView(render_process_id, render_view_id)))); |
369 } | 412 } |
370 | 413 |
371 void HostZoomMapImpl::SendZoomLevelChange(const std::string& scheme, | 414 void HostZoomMapImpl::SendZoomLevelChange(const std::string& scheme, |
372 const std::string& host, | 415 const std::string& host, |
373 double level) { | 416 double level) { |
374 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator()); | 417 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator()); |
375 !i.IsAtEnd(); i.Advance()) { | 418 !i.IsAtEnd(); i.Advance()) { |
376 RenderProcessHost* render_process_host = i.GetCurrentValue(); | 419 RenderProcessHost* render_process_host = i.GetCurrentValue(); |
377 if (HostZoomMap::GetForBrowserContext( | 420 // TODO(wjmaclean) This will need to be cleaned up when |
378 render_process_host->GetBrowserContext()) == this) { | 421 // RenderProcessHost::GetStoragePartition() goes away. Perhaps have |
| 422 // RenderProcessHost expose a GetHostZoomMap() function? |
| 423 if (render_process_host->GetStoragePartition()->GetHostZoomMap() == this) { |
379 render_process_host->Send( | 424 render_process_host->Send( |
380 new ViewMsg_SetZoomLevelForCurrentURL(scheme, host, level)); | 425 new ViewMsg_SetZoomLevelForCurrentURL(scheme, host, level)); |
381 } | 426 } |
382 } | 427 } |
383 } | 428 } |
384 | 429 |
385 HostZoomMapImpl::~HostZoomMapImpl() { | 430 HostZoomMapImpl::~HostZoomMapImpl() { |
| 431 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
386 } | 432 } |
387 | 433 |
388 } // namespace content | 434 } // namespace content |
OLD | NEW |