Chromium Code Reviews| 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::GetForBrowserContext(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(double default_zoom_level) |
|
Fady Samuel
2014/08/13 19:45:43
Why do we take in a default zoom level here?
wjmaclean
2014/08/14 18:18:21
Since we know the default zoom level at constructi
| |
| 78 : default_zoom_level_(0.0) { | 90 : default_zoom_level_(default_zoom_level) { |
| 79 registrar_.Add( | 91 registrar_.Add( |
| 80 this, NOTIFICATION_RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW, | 92 this, NOTIFICATION_RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW, |
| 81 NotificationService::AllSources()); | 93 NotificationService::AllSources()); |
| 82 } | 94 } |
| 83 | 95 |
| 84 void HostZoomMapImpl::CopyFrom(HostZoomMap* copy_interface) { | 96 void HostZoomMapImpl::CopyFrom(HostZoomMap* copy_interface) { |
| 85 // This can only be called on the UI thread to avoid deadlocks, otherwise | 97 // This can only be called on the UI thread to avoid deadlocks, otherwise |
| 86 // UI: a.CopyFrom(b); | 98 // UI: a.CopyFrom(b); |
| 87 // IO: b.CopyFrom(a); | 99 // IO: b.CopyFrom(a); |
| 88 // can deadlock. | 100 // can deadlock. |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 133 scheme_host_zoom_levels_.find(scheme)); | 145 scheme_host_zoom_levels_.find(scheme)); |
| 134 if (scheme_iterator != scheme_host_zoom_levels_.end()) { | 146 if (scheme_iterator != scheme_host_zoom_levels_.end()) { |
| 135 HostZoomLevels::const_iterator i(scheme_iterator->second.find(host)); | 147 HostZoomLevels::const_iterator i(scheme_iterator->second.find(host)); |
| 136 if (i != scheme_iterator->second.end()) | 148 if (i != scheme_iterator->second.end()) |
| 137 return i->second; | 149 return i->second; |
| 138 } | 150 } |
| 139 } | 151 } |
| 140 return GetZoomLevelForHost(host); | 152 return GetZoomLevelForHost(host); |
| 141 } | 153 } |
| 142 | 154 |
| 155 scoped_ptr<base::DictionaryValue> HostZoomMapImpl::ZoomLevelDictionary() const { | |
| 156 base::DictionaryValue* dictionary = new base::DictionaryValue(); | |
| 157 { | |
| 158 base::AutoLock auto_lock(lock_); | |
| 159 | |
| 160 for (HostZoomLevels::const_iterator i = host_zoom_levels_.begin(); | |
| 161 i != host_zoom_levels_.end(); | |
| 162 ++i) { | |
| 163 if (!ZoomValuesEqual(i->second, default_zoom_level_)) { | |
|
Fady Samuel
2014/08/13 19:45:42
if (ZoomValuesEqual(i->second, default_zoom_level_
wjmaclean
2014/08/14 18:18:21
Done.
| |
| 164 dictionary->SetDoubleWithoutPathExpansion(i->first /* host */, | |
| 165 i->second /* level */); | |
| 166 } | |
| 167 } | |
| 168 } | |
| 169 return scoped_ptr<base::DictionaryValue>(dictionary); | |
| 170 } | |
| 171 | |
| 172 // TODO(wjmaclean) The only non-test caller of this just throws away the non- | |
| 173 // host entries and makes a dictionary, and so could probably be converted to | |
| 174 // use ZoomLevelDictionary() instead. The test might also be amenable to | |
| 175 // something similar. | |
| 143 HostZoomMap::ZoomLevelVector HostZoomMapImpl::GetAllZoomLevels() const { | 176 HostZoomMap::ZoomLevelVector HostZoomMapImpl::GetAllZoomLevels() const { |
| 144 HostZoomMap::ZoomLevelVector result; | 177 HostZoomMap::ZoomLevelVector result; |
| 145 { | 178 { |
| 146 base::AutoLock auto_lock(lock_); | 179 base::AutoLock auto_lock(lock_); |
| 147 result.reserve(host_zoom_levels_.size() + scheme_host_zoom_levels_.size()); | 180 result.reserve(host_zoom_levels_.size() + scheme_host_zoom_levels_.size()); |
| 148 for (HostZoomLevels::const_iterator i = host_zoom_levels_.begin(); | 181 for (HostZoomLevels::const_iterator i = host_zoom_levels_.begin(); |
| 149 i != host_zoom_levels_.end(); | 182 i != host_zoom_levels_.end(); |
| 150 ++i) { | 183 ++i) { |
| 151 ZoomLevelChange change = {HostZoomMap::ZOOM_CHANGED_FOR_HOST, | 184 ZoomLevelChange change = {HostZoomMap::ZOOM_CHANGED_FOR_HOST, |
| 152 i->first, // host | 185 i->first, // host |
| (...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 367 GetZoomLevelForHost( | 400 GetZoomLevelForHost( |
| 368 GetHostFromProcessView(render_process_id, render_view_id)))); | 401 GetHostFromProcessView(render_process_id, render_view_id)))); |
| 369 } | 402 } |
| 370 | 403 |
| 371 void HostZoomMapImpl::SendZoomLevelChange(const std::string& scheme, | 404 void HostZoomMapImpl::SendZoomLevelChange(const std::string& scheme, |
| 372 const std::string& host, | 405 const std::string& host, |
| 373 double level) { | 406 double level) { |
| 374 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator()); | 407 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator()); |
| 375 !i.IsAtEnd(); i.Advance()) { | 408 !i.IsAtEnd(); i.Advance()) { |
| 376 RenderProcessHost* render_process_host = i.GetCurrentValue(); | 409 RenderProcessHost* render_process_host = i.GetCurrentValue(); |
| 377 if (HostZoomMap::GetForBrowserContext( | 410 // TODO(wjmaclean) This will need to be cleaned up when |
| 378 render_process_host->GetBrowserContext()) == this) { | 411 // RenderProcessHost::GetStoragePartition() goes away. Perhaps have |
| 412 // RenderProcessHost expose a GetHostZoomMap() function? | |
| 413 if (render_process_host->GetStoragePartition()->GetHostZoomMap() == this) { | |
| 379 render_process_host->Send( | 414 render_process_host->Send( |
| 380 new ViewMsg_SetZoomLevelForCurrentURL(scheme, host, level)); | 415 new ViewMsg_SetZoomLevelForCurrentURL(scheme, host, level)); |
| 381 } | 416 } |
| 382 } | 417 } |
| 383 } | 418 } |
| 384 | 419 |
| 385 HostZoomMapImpl::~HostZoomMapImpl() { | 420 HostZoomMapImpl::~HostZoomMapImpl() { |
| 421 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 386 } | 422 } |
| 387 | 423 |
| 424 | |
| 388 } // namespace content | 425 } // namespace content |
| OLD | NEW |