[email protected] | 9f76c1e | 2012-03-05 15:15:58 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 5 | #include "content/browser/host_zoom_map_impl.h" |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 6 | |
[email protected] | fce82322 | 2014-05-30 16:24:30 | [diff] [blame] | 7 | #include <algorithm> |
[email protected] | b9e7c479f | 2013-04-12 04:33:24 | [diff] [blame] | 8 | #include <cmath> |
| 9 | |
| 10 | #include "base/strings/string_piece.h" |
[email protected] | 74ebfb1 | 2013-06-07 20:48:00 | [diff] [blame] | 11 | #include "base/strings/utf_string_conversions.h" |
[email protected] | d407cc0 | 2011-09-13 16:01:46 | [diff] [blame] | 12 | #include "base/values.h" |
[email protected] | fce82322 | 2014-05-30 16:24:30 | [diff] [blame] | 13 | #include "content/browser/frame_host/navigation_entry_impl.h" |
[email protected] | f3b1a08 | 2011-11-18 00:34:30 | [diff] [blame] | 14 | #include "content/browser/renderer_host/render_process_host_impl.h" |
[email protected] | b3c41c0b | 2012-03-06 15:48:32 | [diff] [blame] | 15 | #include "content/browser/renderer_host/render_view_host_impl.h" |
[email protected] | fce82322 | 2014-05-30 16:24:30 | [diff] [blame] | 16 | #include "content/browser/web_contents/web_contents_impl.h" |
[email protected] | 4e2a25a | 2012-01-27 00:42:08 | [diff] [blame] | 17 | #include "content/common/view_messages.h" |
| 18 | #include "content/public/browser/browser_context.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 19 | #include "content/public/browser/browser_thread.h" |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 20 | #include "content/public/browser/notification_service.h" |
[email protected] | 0d6e9bd | 2011-10-18 04:29:16 | [diff] [blame] | 21 | #include "content/public/browser/notification_types.h" |
[email protected] | 5fe3713a | 2012-02-22 08:31:56 | [diff] [blame] | 22 | #include "content/public/browser/resource_context.h" |
wjmaclean | caa7d6d | 2014-11-12 16:42:11 | [diff] [blame] | 23 | #include "content/public/browser/site_instance.h" |
| 24 | #include "content/public/browser/storage_partition.h" |
[email protected] | 0f08340 | 2011-11-22 02:59:01 | [diff] [blame] | 25 | #include "content/public/common/page_zoom.h" |
wjmaclean | de29ed5 | 2014-10-28 21:09:06 | [diff] [blame] | 26 | #include "content/public/common/url_constants.h" |
[email protected] | 9d797f3 | 2010-04-23 07:17:54 | [diff] [blame] | 27 | #include "net/base/net_util.h" |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 28 | |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 29 | namespace content { |
| 30 | |
[email protected] | d42bf47 | 2014-06-14 01:49:38 | [diff] [blame] | 31 | namespace { |
| 32 | |
| 33 | std::string GetHostFromProcessView(int render_process_id, int render_view_id) { |
mostynb | 4c27d04 | 2015-03-18 21:47:47 | [diff] [blame] | 34 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | d42bf47 | 2014-06-14 01:49:38 | [diff] [blame] | 35 | RenderViewHost* render_view_host = |
| 36 | RenderViewHost::FromID(render_process_id, render_view_id); |
| 37 | if (!render_view_host) |
| 38 | return std::string(); |
| 39 | |
| 40 | WebContents* web_contents = WebContents::FromRenderViewHost(render_view_host); |
| 41 | |
| 42 | NavigationEntry* entry = |
| 43 | web_contents->GetController().GetLastCommittedEntry(); |
| 44 | if (!entry) |
| 45 | return std::string(); |
| 46 | |
wjmaclean | de29ed5 | 2014-10-28 21:09:06 | [diff] [blame] | 47 | return net::GetHostOrSpecFromURL(HostZoomMap::GetURLFromEntry(entry)); |
[email protected] | d42bf47 | 2014-06-14 01:49:38 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | } // namespace |
| 51 | |
wjmaclean | de29ed5 | 2014-10-28 21:09:06 | [diff] [blame] | 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(); |
| 60 | } |
| 61 | } |
| 62 | |
wjmaclean | f9b6ec8 | 2014-08-27 14:33:24 | [diff] [blame] | 63 | HostZoomMap* HostZoomMap::GetDefaultForBrowserContext(BrowserContext* context) { |
wjmaclean | caa7d6d | 2014-11-12 16:42:11 | [diff] [blame] | 64 | StoragePartition* partition = |
| 65 | BrowserContext::GetDefaultStoragePartition(context); |
| 66 | DCHECK(partition); |
| 67 | return partition->GetHostZoomMap(); |
| 68 | } |
| 69 | |
| 70 | HostZoomMap* HostZoomMap::Get(SiteInstance* instance) { |
| 71 | StoragePartition* partition = BrowserContext::GetStoragePartition( |
| 72 | instance->GetBrowserContext(), instance); |
| 73 | DCHECK(partition); |
| 74 | return partition->GetHostZoomMap(); |
| 75 | } |
| 76 | |
| 77 | HostZoomMap* HostZoomMap::GetForWebContents(const WebContents* contents) { |
mcnee | 432e47d | 2015-11-09 19:37:46 | [diff] [blame] | 78 | // TODO(wjmaclean): Update this behaviour to work with OOPIF. |
| 79 | // See crbug.com/528407. |
wjmaclean | caa7d6d | 2014-11-12 16:42:11 | [diff] [blame] | 80 | StoragePartition* partition = |
| 81 | BrowserContext::GetStoragePartition(contents->GetBrowserContext(), |
| 82 | contents->GetSiteInstance()); |
| 83 | DCHECK(partition); |
| 84 | return partition->GetHostZoomMap(); |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 85 | } |
| 86 | |
[email protected] | fce82322 | 2014-05-30 16:24:30 | [diff] [blame] | 87 | // Helper function for setting/getting zoom levels for WebContents without |
| 88 | // having to import HostZoomMapImpl everywhere. |
| 89 | double HostZoomMap::GetZoomLevel(const WebContents* web_contents) { |
wjmaclean | caa7d6d | 2014-11-12 16:42:11 | [diff] [blame] | 90 | HostZoomMapImpl* host_zoom_map = static_cast<HostZoomMapImpl*>( |
| 91 | HostZoomMap::GetForWebContents(web_contents)); |
[email protected] | fce82322 | 2014-05-30 16:24:30 | [diff] [blame] | 92 | return host_zoom_map->GetZoomLevelForWebContents( |
| 93 | *static_cast<const WebContentsImpl*>(web_contents)); |
| 94 | } |
| 95 | |
ccameron | b7c1d6c | 2015-03-09 17:08:24 | [diff] [blame] | 96 | bool HostZoomMap::PageScaleFactorIsOne(const WebContents* web_contents) { |
| 97 | HostZoomMapImpl* host_zoom_map = static_cast<HostZoomMapImpl*>( |
| 98 | HostZoomMap::GetForWebContents(web_contents)); |
| 99 | return host_zoom_map->PageScaleFactorIsOneForWebContents( |
| 100 | *static_cast<const WebContentsImpl*>(web_contents)); |
| 101 | } |
| 102 | |
[email protected] | fce82322 | 2014-05-30 16:24:30 | [diff] [blame] | 103 | void HostZoomMap::SetZoomLevel(const WebContents* web_contents, double level) { |
wjmaclean | caa7d6d | 2014-11-12 16:42:11 | [diff] [blame] | 104 | HostZoomMapImpl* host_zoom_map = static_cast<HostZoomMapImpl*>( |
| 105 | HostZoomMap::GetForWebContents(web_contents)); |
[email protected] | fce82322 | 2014-05-30 16:24:30 | [diff] [blame] | 106 | host_zoom_map->SetZoomLevelForWebContents( |
| 107 | *static_cast<const WebContentsImpl*>(web_contents), level); |
| 108 | } |
| 109 | |
wjmaclean | de29ed5 | 2014-10-28 21:09:06 | [diff] [blame] | 110 | void HostZoomMap::SendErrorPageZoomLevelRefresh( |
| 111 | const WebContents* web_contents) { |
| 112 | HostZoomMapImpl* host_zoom_map = |
| 113 | static_cast<HostZoomMapImpl*>(HostZoomMap::GetDefaultForBrowserContext( |
| 114 | web_contents->GetBrowserContext())); |
| 115 | host_zoom_map->SendErrorPageZoomLevelRefresh(); |
| 116 | } |
| 117 | |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 118 | HostZoomMapImpl::HostZoomMapImpl() |
[email protected] | 4e2a25a | 2012-01-27 00:42:08 | [diff] [blame] | 119 | : default_zoom_level_(0.0) { |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 120 | registrar_.Add( |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 121 | this, NOTIFICATION_RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW, |
| 122 | NotificationService::AllSources()); |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 123 | } |
| 124 | |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 125 | void HostZoomMapImpl::CopyFrom(HostZoomMap* copy_interface) { |
[email protected] | 4e2a25a | 2012-01-27 00:42:08 | [diff] [blame] | 126 | // This can only be called on the UI thread to avoid deadlocks, otherwise |
| 127 | // UI: a.CopyFrom(b); |
| 128 | // IO: b.CopyFrom(a); |
| 129 | // can deadlock. |
mostynb | 4c27d04 | 2015-03-18 21:47:47 | [diff] [blame] | 130 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 131 | HostZoomMapImpl* copy = static_cast<HostZoomMapImpl*>(copy_interface); |
[email protected] | 4e2a25a | 2012-01-27 00:42:08 | [diff] [blame] | 132 | base::AutoLock auto_lock(lock_); |
| 133 | base::AutoLock copy_auto_lock(copy->lock_); |
[email protected] | 367c5c1d | 2013-03-11 18:59:02 | [diff] [blame] | 134 | host_zoom_levels_. |
| 135 | insert(copy->host_zoom_levels_.begin(), copy->host_zoom_levels_.end()); |
| 136 | for (SchemeHostZoomLevels::const_iterator i(copy-> |
| 137 | scheme_host_zoom_levels_.begin()); |
| 138 | i != copy->scheme_host_zoom_levels_.end(); ++i) { |
| 139 | scheme_host_zoom_levels_[i->first] = HostZoomLevels(); |
| 140 | scheme_host_zoom_levels_[i->first]. |
| 141 | insert(i->second.begin(), i->second.end()); |
[email protected] | 4e2a25a | 2012-01-27 00:42:08 | [diff] [blame] | 142 | } |
[email protected] | 19d2669 | 2013-01-12 19:56:33 | [diff] [blame] | 143 | default_zoom_level_ = copy->default_zoom_level_; |
[email protected] | 4e2a25a | 2012-01-27 00:42:08 | [diff] [blame] | 144 | } |
| 145 | |
[email protected] | 367c5c1d | 2013-03-11 18:59:02 | [diff] [blame] | 146 | double HostZoomMapImpl::GetZoomLevelForHost(const std::string& host) const { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 147 | base::AutoLock auto_lock(lock_); |
wjmaclean | c6249049 | 2015-02-13 22:02:07 | [diff] [blame] | 148 | return GetZoomLevelForHostInternal(host); |
| 149 | } |
| 150 | |
| 151 | double HostZoomMapImpl::GetZoomLevelForHostInternal( |
| 152 | const std::string& host) const { |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 153 | HostZoomLevels::const_iterator i(host_zoom_levels_.find(host)); |
[email protected] | d0b8d09 | 2010-10-25 04:05:17 | [diff] [blame] | 154 | return (i == host_zoom_levels_.end()) ? default_zoom_level_ : i->second; |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 155 | } |
| 156 | |
[email protected] | d42bf47 | 2014-06-14 01:49:38 | [diff] [blame] | 157 | bool HostZoomMapImpl::HasZoomLevel(const std::string& scheme, |
| 158 | const std::string& host) const { |
| 159 | base::AutoLock auto_lock(lock_); |
| 160 | |
| 161 | SchemeHostZoomLevels::const_iterator scheme_iterator( |
| 162 | scheme_host_zoom_levels_.find(scheme)); |
| 163 | |
| 164 | const HostZoomLevels& zoom_levels = |
| 165 | (scheme_iterator != scheme_host_zoom_levels_.end()) |
| 166 | ? scheme_iterator->second |
| 167 | : host_zoom_levels_; |
| 168 | |
| 169 | HostZoomLevels::const_iterator i(zoom_levels.find(host)); |
| 170 | return i != zoom_levels.end(); |
| 171 | } |
| 172 | |
wjmaclean | c6249049 | 2015-02-13 22:02:07 | [diff] [blame] | 173 | double HostZoomMapImpl::GetZoomLevelForHostAndSchemeInternal( |
| 174 | const std::string& scheme, |
| 175 | const std::string& host) const { |
| 176 | SchemeHostZoomLevels::const_iterator scheme_iterator( |
| 177 | scheme_host_zoom_levels_.find(scheme)); |
| 178 | if (scheme_iterator != scheme_host_zoom_levels_.end()) { |
| 179 | HostZoomLevels::const_iterator i(scheme_iterator->second.find(host)); |
| 180 | if (i != scheme_iterator->second.end()) |
| 181 | return i->second; |
| 182 | } |
| 183 | |
| 184 | return GetZoomLevelForHostInternal(host); |
| 185 | } |
| 186 | |
[email protected] | 367c5c1d | 2013-03-11 18:59:02 | [diff] [blame] | 187 | double HostZoomMapImpl::GetZoomLevelForHostAndScheme( |
| 188 | const std::string& scheme, |
| 189 | const std::string& host) const { |
wjmaclean | c6249049 | 2015-02-13 22:02:07 | [diff] [blame] | 190 | base::AutoLock auto_lock(lock_); |
| 191 | return GetZoomLevelForHostAndSchemeInternal(scheme, host); |
[email protected] | 367c5c1d | 2013-03-11 18:59:02 | [diff] [blame] | 192 | } |
| 193 | |
[email protected] | 0f37405 | 2014-03-18 20:37:22 | [diff] [blame] | 194 | HostZoomMap::ZoomLevelVector HostZoomMapImpl::GetAllZoomLevels() const { |
| 195 | HostZoomMap::ZoomLevelVector result; |
| 196 | { |
| 197 | base::AutoLock auto_lock(lock_); |
| 198 | result.reserve(host_zoom_levels_.size() + scheme_host_zoom_levels_.size()); |
| 199 | for (HostZoomLevels::const_iterator i = host_zoom_levels_.begin(); |
| 200 | i != host_zoom_levels_.end(); |
| 201 | ++i) { |
| 202 | ZoomLevelChange change = {HostZoomMap::ZOOM_CHANGED_FOR_HOST, |
| 203 | i->first, // host |
| 204 | std::string(), // scheme |
| 205 | i->second // zoom level |
| 206 | }; |
| 207 | result.push_back(change); |
| 208 | } |
| 209 | for (SchemeHostZoomLevels::const_iterator i = |
| 210 | scheme_host_zoom_levels_.begin(); |
| 211 | i != scheme_host_zoom_levels_.end(); |
| 212 | ++i) { |
| 213 | const std::string& scheme = i->first; |
| 214 | const HostZoomLevels& host_zoom_levels = i->second; |
| 215 | for (HostZoomLevels::const_iterator j = host_zoom_levels.begin(); |
| 216 | j != host_zoom_levels.end(); |
| 217 | ++j) { |
| 218 | ZoomLevelChange change = {HostZoomMap::ZOOM_CHANGED_FOR_SCHEME_AND_HOST, |
| 219 | j->first, // host |
| 220 | scheme, // scheme |
| 221 | j->second // zoom level |
| 222 | }; |
| 223 | result.push_back(change); |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | return result; |
| 228 | } |
| 229 | |
[email protected] | 367c5c1d | 2013-03-11 18:59:02 | [diff] [blame] | 230 | void HostZoomMapImpl::SetZoomLevelForHost(const std::string& host, |
| 231 | double level) { |
mostynb | 4c27d04 | 2015-03-18 21:47:47 | [diff] [blame] | 232 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 3ef4bc63 | 2010-04-09 04:25:31 | [diff] [blame] | 233 | |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 234 | { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 235 | base::AutoLock auto_lock(lock_); |
[email protected] | 0f08340 | 2011-11-22 02:59:01 | [diff] [blame] | 236 | |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 237 | if (ZoomValuesEqual(level, default_zoom_level_)) |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 238 | host_zoom_levels_.erase(host); |
| 239 | else |
| 240 | host_zoom_levels_[host] = level; |
| 241 | } |
| 242 | |
[email protected] | d42bf47 | 2014-06-14 01:49:38 | [diff] [blame] | 243 | // TODO(wjmaclean) Should we use a GURL here? crbug.com/384486 |
| 244 | SendZoomLevelChange(std::string(), host, level); |
| 245 | |
[email protected] | 367c5c1d | 2013-03-11 18:59:02 | [diff] [blame] | 246 | HostZoomMap::ZoomLevelChange change; |
| 247 | change.mode = HostZoomMap::ZOOM_CHANGED_FOR_HOST; |
| 248 | change.host = host; |
| 249 | change.zoom_level = level; |
| 250 | |
[email protected] | 11783281 | 2013-10-02 07:06:02 | [diff] [blame] | 251 | zoom_level_changed_callbacks_.Notify(change); |
[email protected] | 367c5c1d | 2013-03-11 18:59:02 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | void HostZoomMapImpl::SetZoomLevelForHostAndScheme(const std::string& scheme, |
| 255 | const std::string& host, |
| 256 | double level) { |
mostynb | 4c27d04 | 2015-03-18 21:47:47 | [diff] [blame] | 257 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 367c5c1d | 2013-03-11 18:59:02 | [diff] [blame] | 258 | { |
| 259 | base::AutoLock auto_lock(lock_); |
| 260 | scheme_host_zoom_levels_[scheme][host] = level; |
| 261 | } |
| 262 | |
[email protected] | d42bf47 | 2014-06-14 01:49:38 | [diff] [blame] | 263 | SendZoomLevelChange(scheme, host, level); |
[email protected] | 4e2a25a | 2012-01-27 00:42:08 | [diff] [blame] | 264 | |
[email protected] | 367c5c1d | 2013-03-11 18:59:02 | [diff] [blame] | 265 | HostZoomMap::ZoomLevelChange change; |
| 266 | change.mode = HostZoomMap::ZOOM_CHANGED_FOR_SCHEME_AND_HOST; |
| 267 | change.host = host; |
| 268 | change.scheme = scheme; |
| 269 | change.zoom_level = level; |
| 270 | |
[email protected] | 11783281 | 2013-10-02 07:06:02 | [diff] [blame] | 271 | zoom_level_changed_callbacks_.Notify(change); |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 272 | } |
| 273 | |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 274 | double HostZoomMapImpl::GetDefaultZoomLevel() const { |
mostynb | 4c27d04 | 2015-03-18 21:47:47 | [diff] [blame] | 275 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 276 | return default_zoom_level_; |
| 277 | } |
| 278 | |
| 279 | void HostZoomMapImpl::SetDefaultZoomLevel(double level) { |
mostynb | 4c27d04 | 2015-03-18 21:47:47 | [diff] [blame] | 280 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 281 | default_zoom_level_ = level; |
| 282 | } |
| 283 | |
[email protected] | 11783281 | 2013-10-02 07:06:02 | [diff] [blame] | 284 | scoped_ptr<HostZoomMap::Subscription> |
| 285 | HostZoomMapImpl::AddZoomLevelChangedCallback( |
[email protected] | 89c9aca | 2013-02-07 15:08:42 | [diff] [blame] | 286 | const ZoomLevelChangedCallback& callback) { |
[email protected] | 11783281 | 2013-10-02 07:06:02 | [diff] [blame] | 287 | return zoom_level_changed_callbacks_.Add(callback); |
[email protected] | 89c9aca | 2013-02-07 15:08:42 | [diff] [blame] | 288 | } |
| 289 | |
[email protected] | fce82322 | 2014-05-30 16:24:30 | [diff] [blame] | 290 | double HostZoomMapImpl::GetZoomLevelForWebContents( |
| 291 | const WebContentsImpl& web_contents_impl) const { |
| 292 | int render_process_id = web_contents_impl.GetRenderProcessHost()->GetID(); |
| 293 | int routing_id = web_contents_impl.GetRenderViewHost()->GetRoutingID(); |
| 294 | |
| 295 | if (UsesTemporaryZoomLevel(render_process_id, routing_id)) |
| 296 | return GetTemporaryZoomLevel(render_process_id, routing_id); |
| 297 | |
[email protected] | acda0ef3 | 2014-06-05 23:13:30 | [diff] [blame] | 298 | // Get the url from the navigation controller directly, as calling |
| 299 | // WebContentsImpl::GetLastCommittedURL() may give us a virtual url that |
| 300 | // is different than is stored in the map. |
[email protected] | fce82322 | 2014-05-30 16:24:30 | [diff] [blame] | 301 | GURL url; |
| 302 | NavigationEntry* entry = |
| 303 | web_contents_impl.GetController().GetLastCommittedEntry(); |
[email protected] | acda0ef3 | 2014-06-05 23:13:30 | [diff] [blame] | 304 | // It is possible for a WebContent's zoom level to be queried before |
| 305 | // a navigation has occurred. |
[email protected] | fce82322 | 2014-05-30 16:24:30 | [diff] [blame] | 306 | if (entry) |
wjmaclean | de29ed5 | 2014-10-28 21:09:06 | [diff] [blame] | 307 | url = GetURLFromEntry(entry); |
[email protected] | fce82322 | 2014-05-30 16:24:30 | [diff] [blame] | 308 | return GetZoomLevelForHostAndScheme(url.scheme(), |
| 309 | net::GetHostOrSpecFromURL(url)); |
| 310 | } |
| 311 | |
| 312 | void HostZoomMapImpl::SetZoomLevelForWebContents( |
| 313 | const WebContentsImpl& web_contents_impl, |
| 314 | double level) { |
| 315 | int render_process_id = web_contents_impl.GetRenderProcessHost()->GetID(); |
| 316 | int render_view_id = web_contents_impl.GetRenderViewHost()->GetRoutingID(); |
| 317 | if (UsesTemporaryZoomLevel(render_process_id, render_view_id)) { |
[email protected] | fce82322 | 2014-05-30 16:24:30 | [diff] [blame] | 318 | SetTemporaryZoomLevel(render_process_id, render_view_id, level); |
| 319 | } else { |
[email protected] | acda0ef3 | 2014-06-05 23:13:30 | [diff] [blame] | 320 | // Get the url from the navigation controller directly, as calling |
| 321 | // WebContentsImpl::GetLastCommittedURL() may give us a virtual url that |
| 322 | // is different than what the render view is using. If the two don't match, |
| 323 | // the attempt to set the zoom will fail. |
[email protected] | acda0ef3 | 2014-06-05 23:13:30 | [diff] [blame] | 324 | NavigationEntry* entry = |
| 325 | web_contents_impl.GetController().GetLastCommittedEntry(); |
[email protected] | f3b3e5e | 2014-06-10 15:47:41 | [diff] [blame] | 326 | // Tests may invoke this function with a null entry, but we don't |
| 327 | // want to save zoom levels in this case. |
| 328 | if (!entry) |
| 329 | return; |
| 330 | |
wjmaclean | de29ed5 | 2014-10-28 21:09:06 | [diff] [blame] | 331 | GURL url = GetURLFromEntry(entry); |
[email protected] | acda0ef3 | 2014-06-05 23:13:30 | [diff] [blame] | 332 | SetZoomLevelForHost(net::GetHostOrSpecFromURL(url), level); |
[email protected] | fce82322 | 2014-05-30 16:24:30 | [diff] [blame] | 333 | } |
| 334 | } |
| 335 | |
| 336 | void HostZoomMapImpl::SetZoomLevelForView(int render_process_id, |
| 337 | int render_view_id, |
| 338 | double level, |
| 339 | const std::string& host) { |
| 340 | if (UsesTemporaryZoomLevel(render_process_id, render_view_id)) |
| 341 | SetTemporaryZoomLevel(render_process_id, render_view_id, level); |
| 342 | else |
| 343 | SetZoomLevelForHost(host, level); |
| 344 | } |
| 345 | |
ccameron | b7c1d6c | 2015-03-09 17:08:24 | [diff] [blame] | 346 | void HostZoomMapImpl::SetPageScaleFactorIsOneForView(int render_process_id, |
| 347 | int render_view_id, |
| 348 | bool is_one) { |
| 349 | { |
| 350 | base::AutoLock auto_lock(lock_); |
| 351 | view_page_scale_factors_are_one_[RenderViewKey(render_process_id, |
| 352 | render_view_id)] = is_one; |
| 353 | } |
| 354 | HostZoomMap::ZoomLevelChange change; |
| 355 | change.mode = HostZoomMap::PAGE_SCALE_IS_ONE_CHANGED; |
| 356 | zoom_level_changed_callbacks_.Notify(change); |
| 357 | } |
| 358 | |
| 359 | bool HostZoomMapImpl::PageScaleFactorIsOneForWebContents( |
| 360 | const WebContentsImpl& web_contents_impl) const { |
| 361 | if (!web_contents_impl.GetRenderProcessHost()) |
| 362 | return true; |
| 363 | base::AutoLock auto_lock(lock_); |
| 364 | auto found = view_page_scale_factors_are_one_.find( |
| 365 | RenderViewKey(web_contents_impl.GetRenderProcessHost()->GetID(), |
| 366 | web_contents_impl.GetRoutingID())); |
| 367 | if (found == view_page_scale_factors_are_one_.end()) |
| 368 | return true; |
| 369 | return found->second; |
| 370 | } |
| 371 | |
| 372 | void HostZoomMapImpl::ClearPageScaleFactorIsOneForView(int render_process_id, |
| 373 | int render_view_id) { |
| 374 | base::AutoLock auto_lock(lock_); |
| 375 | view_page_scale_factors_are_one_.erase( |
| 376 | RenderViewKey(render_process_id, render_view_id)); |
| 377 | } |
| 378 | |
[email protected] | fce82322 | 2014-05-30 16:24:30 | [diff] [blame] | 379 | bool HostZoomMapImpl::UsesTemporaryZoomLevel(int render_process_id, |
| 380 | int render_view_id) const { |
[email protected] | d42bf47 | 2014-06-14 01:49:38 | [diff] [blame] | 381 | RenderViewKey key(render_process_id, render_view_id); |
[email protected] | fce82322 | 2014-05-30 16:24:30 | [diff] [blame] | 382 | |
| 383 | base::AutoLock auto_lock(lock_); |
[email protected] | d42bf47 | 2014-06-14 01:49:38 | [diff] [blame] | 384 | return ContainsKey(temporary_zoom_levels_, key); |
[email protected] | fce82322 | 2014-05-30 16:24:30 | [diff] [blame] | 385 | } |
| 386 | |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 387 | double HostZoomMapImpl::GetTemporaryZoomLevel(int render_process_id, |
| 388 | int render_view_id) const { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 389 | base::AutoLock auto_lock(lock_); |
[email protected] | d42bf47 | 2014-06-14 01:49:38 | [diff] [blame] | 390 | RenderViewKey key(render_process_id, render_view_id); |
| 391 | if (!ContainsKey(temporary_zoom_levels_, key)) |
| 392 | return 0; |
[email protected] | fce82322 | 2014-05-30 16:24:30 | [diff] [blame] | 393 | |
[email protected] | d42bf47 | 2014-06-14 01:49:38 | [diff] [blame] | 394 | return temporary_zoom_levels_.find(key)->second; |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 395 | } |
| 396 | |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 397 | void HostZoomMapImpl::SetTemporaryZoomLevel(int render_process_id, |
| 398 | int render_view_id, |
| 399 | double level) { |
mostynb | 4c27d04 | 2015-03-18 21:47:47 | [diff] [blame] | 400 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 401 | |
| 402 | { |
[email protected] | d42bf47 | 2014-06-14 01:49:38 | [diff] [blame] | 403 | RenderViewKey key(render_process_id, render_view_id); |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 404 | base::AutoLock auto_lock(lock_); |
[email protected] | d42bf47 | 2014-06-14 01:49:38 | [diff] [blame] | 405 | temporary_zoom_levels_[key] = level; |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 406 | } |
| 407 | |
[email protected] | d42bf47 | 2014-06-14 01:49:38 | [diff] [blame] | 408 | RenderViewHost* host = |
| 409 | RenderViewHost::FromID(render_process_id, render_view_id); |
| 410 | host->Send(new ViewMsg_SetZoomLevelForView(render_view_id, true, level)); |
| 411 | |
[email protected] | 367c5c1d | 2013-03-11 18:59:02 | [diff] [blame] | 412 | HostZoomMap::ZoomLevelChange change; |
| 413 | change.mode = HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM; |
[email protected] | d42bf47 | 2014-06-14 01:49:38 | [diff] [blame] | 414 | change.host = GetHostFromProcessView(render_process_id, render_view_id); |
[email protected] | 367c5c1d | 2013-03-11 18:59:02 | [diff] [blame] | 415 | change.zoom_level = level; |
| 416 | |
[email protected] | 11783281 | 2013-10-02 07:06:02 | [diff] [blame] | 417 | zoom_level_changed_callbacks_.Notify(change); |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 418 | } |
| 419 | |
wjmaclean | c6249049 | 2015-02-13 22:02:07 | [diff] [blame] | 420 | double HostZoomMapImpl::GetZoomLevelForView(const GURL& url, |
| 421 | int render_process_id, |
| 422 | int render_view_id) const { |
| 423 | RenderViewKey key(render_process_id, render_view_id); |
| 424 | base::AutoLock auto_lock(lock_); |
| 425 | |
| 426 | if (ContainsKey(temporary_zoom_levels_, key)) |
| 427 | return temporary_zoom_levels_.find(key)->second; |
| 428 | |
| 429 | return GetZoomLevelForHostAndSchemeInternal(url.scheme(), |
| 430 | net::GetHostOrSpecFromURL(url)); |
| 431 | } |
| 432 | |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 433 | void HostZoomMapImpl::Observe(int type, |
| 434 | const NotificationSource& source, |
| 435 | const NotificationDetails& details) { |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 436 | switch (type) { |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 437 | case NOTIFICATION_RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW: { |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 438 | int render_view_id = Source<RenderViewHost>(source)->GetRoutingID(); |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 439 | int render_process_id = |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 440 | Source<RenderViewHost>(source)->GetProcess()->GetID(); |
[email protected] | d42bf47 | 2014-06-14 01:49:38 | [diff] [blame] | 441 | ClearTemporaryZoomLevel(render_process_id, render_view_id); |
ccameron | b7c1d6c | 2015-03-09 17:08:24 | [diff] [blame] | 442 | ClearPageScaleFactorIsOneForView(render_process_id, render_view_id); |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 443 | break; |
[email protected] | 3ef4bc63 | 2010-04-09 04:25:31 | [diff] [blame] | 444 | } |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 445 | default: |
| 446 | NOTREACHED() << "Unexpected preference observed."; |
[email protected] | 3ef4bc63 | 2010-04-09 04:25:31 | [diff] [blame] | 447 | } |
[email protected] | 779c336 | 2010-01-30 01:09:33 | [diff] [blame] | 448 | } |
| 449 | |
[email protected] | d42bf47 | 2014-06-14 01:49:38 | [diff] [blame] | 450 | void HostZoomMapImpl::ClearTemporaryZoomLevel(int render_process_id, |
| 451 | int render_view_id) { |
| 452 | { |
| 453 | base::AutoLock auto_lock(lock_); |
| 454 | RenderViewKey key(render_process_id, render_view_id); |
| 455 | TemporaryZoomLevels::iterator it = temporary_zoom_levels_.find(key); |
| 456 | if (it == temporary_zoom_levels_.end()) |
| 457 | return; |
| 458 | temporary_zoom_levels_.erase(it); |
| 459 | } |
| 460 | RenderViewHost* host = |
| 461 | RenderViewHost::FromID(render_process_id, render_view_id); |
| 462 | DCHECK(host); |
| 463 | // Send a new zoom level, host-specific if one exists. |
| 464 | host->Send(new ViewMsg_SetZoomLevelForView( |
| 465 | render_view_id, |
| 466 | false, |
| 467 | GetZoomLevelForHost( |
| 468 | GetHostFromProcessView(render_process_id, render_view_id)))); |
| 469 | } |
| 470 | |
| 471 | void HostZoomMapImpl::SendZoomLevelChange(const std::string& scheme, |
| 472 | const std::string& host, |
| 473 | double level) { |
| 474 | for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator()); |
| 475 | !i.IsAtEnd(); i.Advance()) { |
| 476 | RenderProcessHost* render_process_host = i.GetCurrentValue(); |
wjmaclean | caa7d6d | 2014-11-12 16:42:11 | [diff] [blame] | 477 | // TODO(wjmaclean) This will need to be cleaned up when |
| 478 | // RenderProcessHost::GetStoragePartition() goes away. Perhaps have |
| 479 | // RenderProcessHost expose a GetHostZoomMap() function? |
| 480 | if (render_process_host->GetStoragePartition()->GetHostZoomMap() == this) { |
[email protected] | d42bf47 | 2014-06-14 01:49:38 | [diff] [blame] | 481 | render_process_host->Send( |
| 482 | new ViewMsg_SetZoomLevelForCurrentURL(scheme, host, level)); |
| 483 | } |
| 484 | } |
| 485 | } |
| 486 | |
wjmaclean | de29ed5 | 2014-10-28 21:09:06 | [diff] [blame] | 487 | void HostZoomMapImpl::SendErrorPageZoomLevelRefresh() { |
| 488 | GURL error_url(kUnreachableWebDataURL); |
| 489 | std::string host = net::GetHostOrSpecFromURL(error_url); |
| 490 | double error_page_zoom_level = GetZoomLevelForHost(host); |
| 491 | |
| 492 | SendZoomLevelChange(std::string(), host, error_page_zoom_level); |
| 493 | } |
| 494 | |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 495 | HostZoomMapImpl::~HostZoomMapImpl() { |
mostynb | 4c27d04 | 2015-03-18 21:47:47 | [diff] [blame] | 496 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 497 | } |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 498 | |
| 499 | } // namespace content |