[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" |
[email protected] | 0f08340 | 2011-11-22 02:59:01 | [diff] [blame] | 23 | #include "content/public/common/page_zoom.h" |
wjmaclean | de29ed5 | 2014-10-28 21:09:06 | [diff] [blame^] | 24 | #include "content/public/common/url_constants.h" |
[email protected] | 9d797f3 | 2010-04-23 07:17:54 | [diff] [blame] | 25 | #include "net/base/net_util.h" |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 26 | |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 27 | namespace content { |
| 28 | |
[email protected] | d42bf47 | 2014-06-14 01:49:38 | [diff] [blame] | 29 | namespace { |
| 30 | |
[email protected] | e2a8b0e | 2014-08-21 08:49:53 | [diff] [blame] | 31 | const char kHostZoomMapKeyName[] = "content_host_zoom_map"; |
| 32 | |
[email protected] | d42bf47 | 2014-06-14 01:49:38 | [diff] [blame] | 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(); |
| 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) { |
[email protected] | 5fe3713a | 2012-02-22 08:31:56 | [diff] [blame] | 64 | HostZoomMapImpl* rv = static_cast<HostZoomMapImpl*>( |
| 65 | context->GetUserData(kHostZoomMapKeyName)); |
| 66 | if (!rv) { |
| 67 | rv = new HostZoomMapImpl(); |
| 68 | context->SetUserData(kHostZoomMapKeyName, rv); |
| 69 | } |
| 70 | return rv; |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 71 | } |
| 72 | |
[email protected] | fce82322 | 2014-05-30 16:24:30 | [diff] [blame] | 73 | // Helper function for setting/getting zoom levels for WebContents without |
| 74 | // having to import HostZoomMapImpl everywhere. |
| 75 | double HostZoomMap::GetZoomLevel(const WebContents* web_contents) { |
wjmaclean | f9b6ec8 | 2014-08-27 14:33:24 | [diff] [blame] | 76 | HostZoomMapImpl* host_zoom_map = |
| 77 | static_cast<HostZoomMapImpl*>(HostZoomMap::GetDefaultForBrowserContext( |
| 78 | web_contents->GetBrowserContext())); |
[email protected] | fce82322 | 2014-05-30 16:24:30 | [diff] [blame] | 79 | return host_zoom_map->GetZoomLevelForWebContents( |
| 80 | *static_cast<const WebContentsImpl*>(web_contents)); |
| 81 | } |
| 82 | |
| 83 | void HostZoomMap::SetZoomLevel(const WebContents* web_contents, double level) { |
wjmaclean | f9b6ec8 | 2014-08-27 14:33:24 | [diff] [blame] | 84 | HostZoomMapImpl* host_zoom_map = |
| 85 | static_cast<HostZoomMapImpl*>(HostZoomMap::GetDefaultForBrowserContext( |
| 86 | web_contents->GetBrowserContext())); |
[email protected] | fce82322 | 2014-05-30 16:24:30 | [diff] [blame] | 87 | host_zoom_map->SetZoomLevelForWebContents( |
| 88 | *static_cast<const WebContentsImpl*>(web_contents), level); |
| 89 | } |
| 90 | |
wjmaclean | de29ed5 | 2014-10-28 21:09:06 | [diff] [blame^] | 91 | void HostZoomMap::SendErrorPageZoomLevelRefresh( |
| 92 | const WebContents* web_contents) { |
| 93 | HostZoomMapImpl* host_zoom_map = |
| 94 | static_cast<HostZoomMapImpl*>(HostZoomMap::GetDefaultForBrowserContext( |
| 95 | web_contents->GetBrowserContext())); |
| 96 | host_zoom_map->SendErrorPageZoomLevelRefresh(); |
| 97 | } |
| 98 | |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 99 | HostZoomMapImpl::HostZoomMapImpl() |
[email protected] | 4e2a25a | 2012-01-27 00:42:08 | [diff] [blame] | 100 | : default_zoom_level_(0.0) { |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 101 | registrar_.Add( |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 102 | this, NOTIFICATION_RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW, |
| 103 | NotificationService::AllSources()); |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 104 | } |
| 105 | |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 106 | void HostZoomMapImpl::CopyFrom(HostZoomMap* copy_interface) { |
[email protected] | 4e2a25a | 2012-01-27 00:42:08 | [diff] [blame] | 107 | // This can only be called on the UI thread to avoid deadlocks, otherwise |
| 108 | // UI: a.CopyFrom(b); |
| 109 | // IO: b.CopyFrom(a); |
| 110 | // can deadlock. |
| 111 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 112 | HostZoomMapImpl* copy = static_cast<HostZoomMapImpl*>(copy_interface); |
[email protected] | 4e2a25a | 2012-01-27 00:42:08 | [diff] [blame] | 113 | base::AutoLock auto_lock(lock_); |
| 114 | base::AutoLock copy_auto_lock(copy->lock_); |
[email protected] | 367c5c1d | 2013-03-11 18:59:02 | [diff] [blame] | 115 | host_zoom_levels_. |
| 116 | insert(copy->host_zoom_levels_.begin(), copy->host_zoom_levels_.end()); |
| 117 | for (SchemeHostZoomLevels::const_iterator i(copy-> |
| 118 | scheme_host_zoom_levels_.begin()); |
| 119 | i != copy->scheme_host_zoom_levels_.end(); ++i) { |
| 120 | scheme_host_zoom_levels_[i->first] = HostZoomLevels(); |
| 121 | scheme_host_zoom_levels_[i->first]. |
| 122 | insert(i->second.begin(), i->second.end()); |
[email protected] | 4e2a25a | 2012-01-27 00:42:08 | [diff] [blame] | 123 | } |
[email protected] | 19d2669 | 2013-01-12 19:56:33 | [diff] [blame] | 124 | default_zoom_level_ = copy->default_zoom_level_; |
[email protected] | 4e2a25a | 2012-01-27 00:42:08 | [diff] [blame] | 125 | } |
| 126 | |
[email protected] | 367c5c1d | 2013-03-11 18:59:02 | [diff] [blame] | 127 | double HostZoomMapImpl::GetZoomLevelForHost(const std::string& host) const { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 128 | base::AutoLock auto_lock(lock_); |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 129 | HostZoomLevels::const_iterator i(host_zoom_levels_.find(host)); |
[email protected] | d0b8d09 | 2010-10-25 04:05:17 | [diff] [blame] | 130 | return (i == host_zoom_levels_.end()) ? default_zoom_level_ : i->second; |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 131 | } |
| 132 | |
[email protected] | d42bf47 | 2014-06-14 01:49:38 | [diff] [blame] | 133 | bool HostZoomMapImpl::HasZoomLevel(const std::string& scheme, |
| 134 | const std::string& host) const { |
| 135 | base::AutoLock auto_lock(lock_); |
| 136 | |
| 137 | SchemeHostZoomLevels::const_iterator scheme_iterator( |
| 138 | scheme_host_zoom_levels_.find(scheme)); |
| 139 | |
| 140 | const HostZoomLevels& zoom_levels = |
| 141 | (scheme_iterator != scheme_host_zoom_levels_.end()) |
| 142 | ? scheme_iterator->second |
| 143 | : host_zoom_levels_; |
| 144 | |
| 145 | HostZoomLevels::const_iterator i(zoom_levels.find(host)); |
| 146 | return i != zoom_levels.end(); |
| 147 | } |
| 148 | |
[email protected] | 367c5c1d | 2013-03-11 18:59:02 | [diff] [blame] | 149 | double HostZoomMapImpl::GetZoomLevelForHostAndScheme( |
| 150 | const std::string& scheme, |
| 151 | const std::string& host) const { |
| 152 | { |
| 153 | base::AutoLock auto_lock(lock_); |
| 154 | SchemeHostZoomLevels::const_iterator scheme_iterator( |
| 155 | scheme_host_zoom_levels_.find(scheme)); |
| 156 | if (scheme_iterator != scheme_host_zoom_levels_.end()) { |
| 157 | HostZoomLevels::const_iterator i(scheme_iterator->second.find(host)); |
| 158 | if (i != scheme_iterator->second.end()) |
| 159 | return i->second; |
| 160 | } |
| 161 | } |
| 162 | return GetZoomLevelForHost(host); |
| 163 | } |
| 164 | |
[email protected] | 0f37405 | 2014-03-18 20:37:22 | [diff] [blame] | 165 | HostZoomMap::ZoomLevelVector HostZoomMapImpl::GetAllZoomLevels() const { |
| 166 | HostZoomMap::ZoomLevelVector result; |
| 167 | { |
| 168 | base::AutoLock auto_lock(lock_); |
| 169 | result.reserve(host_zoom_levels_.size() + scheme_host_zoom_levels_.size()); |
| 170 | for (HostZoomLevels::const_iterator i = host_zoom_levels_.begin(); |
| 171 | i != host_zoom_levels_.end(); |
| 172 | ++i) { |
| 173 | ZoomLevelChange change = {HostZoomMap::ZOOM_CHANGED_FOR_HOST, |
| 174 | i->first, // host |
| 175 | std::string(), // scheme |
| 176 | i->second // zoom level |
| 177 | }; |
| 178 | result.push_back(change); |
| 179 | } |
| 180 | for (SchemeHostZoomLevels::const_iterator i = |
| 181 | scheme_host_zoom_levels_.begin(); |
| 182 | i != scheme_host_zoom_levels_.end(); |
| 183 | ++i) { |
| 184 | const std::string& scheme = i->first; |
| 185 | const HostZoomLevels& host_zoom_levels = i->second; |
| 186 | for (HostZoomLevels::const_iterator j = host_zoom_levels.begin(); |
| 187 | j != host_zoom_levels.end(); |
| 188 | ++j) { |
| 189 | ZoomLevelChange change = {HostZoomMap::ZOOM_CHANGED_FOR_SCHEME_AND_HOST, |
| 190 | j->first, // host |
| 191 | scheme, // scheme |
| 192 | j->second // zoom level |
| 193 | }; |
| 194 | result.push_back(change); |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | return result; |
| 199 | } |
| 200 | |
[email protected] | 367c5c1d | 2013-03-11 18:59:02 | [diff] [blame] | 201 | void HostZoomMapImpl::SetZoomLevelForHost(const std::string& host, |
| 202 | double level) { |
[email protected] | f8b3ef8 | 2010-10-11 02:45:52 | [diff] [blame] | 203 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 3ef4bc63 | 2010-04-09 04:25:31 | [diff] [blame] | 204 | |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 205 | { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 206 | base::AutoLock auto_lock(lock_); |
[email protected] | 0f08340 | 2011-11-22 02:59:01 | [diff] [blame] | 207 | |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 208 | if (ZoomValuesEqual(level, default_zoom_level_)) |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 209 | host_zoom_levels_.erase(host); |
| 210 | else |
| 211 | host_zoom_levels_[host] = level; |
| 212 | } |
| 213 | |
[email protected] | d42bf47 | 2014-06-14 01:49:38 | [diff] [blame] | 214 | // TODO(wjmaclean) Should we use a GURL here? crbug.com/384486 |
| 215 | SendZoomLevelChange(std::string(), host, level); |
| 216 | |
[email protected] | 367c5c1d | 2013-03-11 18:59:02 | [diff] [blame] | 217 | HostZoomMap::ZoomLevelChange change; |
| 218 | change.mode = HostZoomMap::ZOOM_CHANGED_FOR_HOST; |
| 219 | change.host = host; |
| 220 | change.zoom_level = level; |
| 221 | |
[email protected] | 11783281 | 2013-10-02 07:06:02 | [diff] [blame] | 222 | zoom_level_changed_callbacks_.Notify(change); |
[email protected] | 367c5c1d | 2013-03-11 18:59:02 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | void HostZoomMapImpl::SetZoomLevelForHostAndScheme(const std::string& scheme, |
| 226 | const std::string& host, |
| 227 | double level) { |
| 228 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 229 | { |
| 230 | base::AutoLock auto_lock(lock_); |
| 231 | scheme_host_zoom_levels_[scheme][host] = level; |
| 232 | } |
| 233 | |
[email protected] | d42bf47 | 2014-06-14 01:49:38 | [diff] [blame] | 234 | SendZoomLevelChange(scheme, host, level); |
[email protected] | 4e2a25a | 2012-01-27 00:42:08 | [diff] [blame] | 235 | |
[email protected] | 367c5c1d | 2013-03-11 18:59:02 | [diff] [blame] | 236 | HostZoomMap::ZoomLevelChange change; |
| 237 | change.mode = HostZoomMap::ZOOM_CHANGED_FOR_SCHEME_AND_HOST; |
| 238 | change.host = host; |
| 239 | change.scheme = scheme; |
| 240 | change.zoom_level = level; |
| 241 | |
[email protected] | 11783281 | 2013-10-02 07:06:02 | [diff] [blame] | 242 | zoom_level_changed_callbacks_.Notify(change); |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 243 | } |
| 244 | |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 245 | double HostZoomMapImpl::GetDefaultZoomLevel() const { |
| 246 | return default_zoom_level_; |
| 247 | } |
| 248 | |
| 249 | void HostZoomMapImpl::SetDefaultZoomLevel(double level) { |
| 250 | default_zoom_level_ = level; |
| 251 | } |
| 252 | |
[email protected] | 11783281 | 2013-10-02 07:06:02 | [diff] [blame] | 253 | scoped_ptr<HostZoomMap::Subscription> |
| 254 | HostZoomMapImpl::AddZoomLevelChangedCallback( |
[email protected] | 89c9aca | 2013-02-07 15:08:42 | [diff] [blame] | 255 | const ZoomLevelChangedCallback& callback) { |
[email protected] | 11783281 | 2013-10-02 07:06:02 | [diff] [blame] | 256 | return zoom_level_changed_callbacks_.Add(callback); |
[email protected] | 89c9aca | 2013-02-07 15:08:42 | [diff] [blame] | 257 | } |
| 258 | |
[email protected] | fce82322 | 2014-05-30 16:24:30 | [diff] [blame] | 259 | double HostZoomMapImpl::GetZoomLevelForWebContents( |
| 260 | const WebContentsImpl& web_contents_impl) const { |
| 261 | int render_process_id = web_contents_impl.GetRenderProcessHost()->GetID(); |
| 262 | int routing_id = web_contents_impl.GetRenderViewHost()->GetRoutingID(); |
| 263 | |
| 264 | if (UsesTemporaryZoomLevel(render_process_id, routing_id)) |
| 265 | return GetTemporaryZoomLevel(render_process_id, routing_id); |
| 266 | |
[email protected] | acda0ef3 | 2014-06-05 23:13:30 | [diff] [blame] | 267 | // Get the url from the navigation controller directly, as calling |
| 268 | // WebContentsImpl::GetLastCommittedURL() may give us a virtual url that |
| 269 | // is different than is stored in the map. |
[email protected] | fce82322 | 2014-05-30 16:24:30 | [diff] [blame] | 270 | GURL url; |
| 271 | NavigationEntry* entry = |
| 272 | web_contents_impl.GetController().GetLastCommittedEntry(); |
[email protected] | acda0ef3 | 2014-06-05 23:13:30 | [diff] [blame] | 273 | // It is possible for a WebContent's zoom level to be queried before |
| 274 | // a navigation has occurred. |
[email protected] | fce82322 | 2014-05-30 16:24:30 | [diff] [blame] | 275 | if (entry) |
wjmaclean | de29ed5 | 2014-10-28 21:09:06 | [diff] [blame^] | 276 | url = GetURLFromEntry(entry); |
[email protected] | fce82322 | 2014-05-30 16:24:30 | [diff] [blame] | 277 | return GetZoomLevelForHostAndScheme(url.scheme(), |
| 278 | net::GetHostOrSpecFromURL(url)); |
| 279 | } |
| 280 | |
| 281 | void HostZoomMapImpl::SetZoomLevelForWebContents( |
| 282 | const WebContentsImpl& web_contents_impl, |
| 283 | double level) { |
| 284 | int render_process_id = web_contents_impl.GetRenderProcessHost()->GetID(); |
| 285 | int render_view_id = web_contents_impl.GetRenderViewHost()->GetRoutingID(); |
| 286 | if (UsesTemporaryZoomLevel(render_process_id, render_view_id)) { |
[email protected] | fce82322 | 2014-05-30 16:24:30 | [diff] [blame] | 287 | SetTemporaryZoomLevel(render_process_id, render_view_id, level); |
| 288 | } else { |
[email protected] | acda0ef3 | 2014-06-05 23:13:30 | [diff] [blame] | 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 what the render view is using. If the two don't match, |
| 292 | // the attempt to set the zoom will fail. |
[email protected] | acda0ef3 | 2014-06-05 23:13:30 | [diff] [blame] | 293 | NavigationEntry* entry = |
| 294 | web_contents_impl.GetController().GetLastCommittedEntry(); |
[email protected] | f3b3e5e | 2014-06-10 15:47:41 | [diff] [blame] | 295 | // Tests may invoke this function with a null entry, but we don't |
| 296 | // want to save zoom levels in this case. |
| 297 | if (!entry) |
| 298 | return; |
| 299 | |
wjmaclean | de29ed5 | 2014-10-28 21:09:06 | [diff] [blame^] | 300 | GURL url = GetURLFromEntry(entry); |
[email protected] | acda0ef3 | 2014-06-05 23:13:30 | [diff] [blame] | 301 | SetZoomLevelForHost(net::GetHostOrSpecFromURL(url), level); |
[email protected] | fce82322 | 2014-05-30 16:24:30 | [diff] [blame] | 302 | } |
| 303 | } |
| 304 | |
| 305 | void HostZoomMapImpl::SetZoomLevelForView(int render_process_id, |
| 306 | int render_view_id, |
| 307 | double level, |
| 308 | const std::string& host) { |
| 309 | if (UsesTemporaryZoomLevel(render_process_id, render_view_id)) |
| 310 | SetTemporaryZoomLevel(render_process_id, render_view_id, level); |
| 311 | else |
| 312 | SetZoomLevelForHost(host, level); |
| 313 | } |
| 314 | |
| 315 | bool HostZoomMapImpl::UsesTemporaryZoomLevel(int render_process_id, |
| 316 | int render_view_id) const { |
[email protected] | d42bf47 | 2014-06-14 01:49:38 | [diff] [blame] | 317 | RenderViewKey key(render_process_id, render_view_id); |
[email protected] | fce82322 | 2014-05-30 16:24:30 | [diff] [blame] | 318 | |
| 319 | base::AutoLock auto_lock(lock_); |
[email protected] | d42bf47 | 2014-06-14 01:49:38 | [diff] [blame] | 320 | return ContainsKey(temporary_zoom_levels_, key); |
[email protected] | fce82322 | 2014-05-30 16:24:30 | [diff] [blame] | 321 | } |
| 322 | |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 323 | double HostZoomMapImpl::GetTemporaryZoomLevel(int render_process_id, |
| 324 | int render_view_id) const { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 325 | base::AutoLock auto_lock(lock_); |
[email protected] | d42bf47 | 2014-06-14 01:49:38 | [diff] [blame] | 326 | RenderViewKey key(render_process_id, render_view_id); |
| 327 | if (!ContainsKey(temporary_zoom_levels_, key)) |
| 328 | return 0; |
[email protected] | fce82322 | 2014-05-30 16:24:30 | [diff] [blame] | 329 | |
[email protected] | d42bf47 | 2014-06-14 01:49:38 | [diff] [blame] | 330 | return temporary_zoom_levels_.find(key)->second; |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 331 | } |
| 332 | |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 333 | void HostZoomMapImpl::SetTemporaryZoomLevel(int render_process_id, |
| 334 | int render_view_id, |
| 335 | double level) { |
[email protected] | f8b3ef8 | 2010-10-11 02:45:52 | [diff] [blame] | 336 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 337 | |
| 338 | { |
[email protected] | d42bf47 | 2014-06-14 01:49:38 | [diff] [blame] | 339 | RenderViewKey key(render_process_id, render_view_id); |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 340 | base::AutoLock auto_lock(lock_); |
[email protected] | d42bf47 | 2014-06-14 01:49:38 | [diff] [blame] | 341 | temporary_zoom_levels_[key] = level; |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 342 | } |
| 343 | |
[email protected] | d42bf47 | 2014-06-14 01:49:38 | [diff] [blame] | 344 | RenderViewHost* host = |
| 345 | RenderViewHost::FromID(render_process_id, render_view_id); |
| 346 | host->Send(new ViewMsg_SetZoomLevelForView(render_view_id, true, level)); |
| 347 | |
[email protected] | 367c5c1d | 2013-03-11 18:59:02 | [diff] [blame] | 348 | HostZoomMap::ZoomLevelChange change; |
| 349 | change.mode = HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM; |
[email protected] | d42bf47 | 2014-06-14 01:49:38 | [diff] [blame] | 350 | change.host = GetHostFromProcessView(render_process_id, render_view_id); |
[email protected] | 367c5c1d | 2013-03-11 18:59:02 | [diff] [blame] | 351 | change.zoom_level = level; |
| 352 | |
[email protected] | 11783281 | 2013-10-02 07:06:02 | [diff] [blame] | 353 | zoom_level_changed_callbacks_.Notify(change); |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 354 | } |
| 355 | |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 356 | void HostZoomMapImpl::Observe(int type, |
| 357 | const NotificationSource& source, |
| 358 | const NotificationDetails& details) { |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 359 | switch (type) { |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 360 | case NOTIFICATION_RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW: { |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 361 | int render_view_id = Source<RenderViewHost>(source)->GetRoutingID(); |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 362 | int render_process_id = |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 363 | Source<RenderViewHost>(source)->GetProcess()->GetID(); |
[email protected] | d42bf47 | 2014-06-14 01:49:38 | [diff] [blame] | 364 | ClearTemporaryZoomLevel(render_process_id, render_view_id); |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 365 | break; |
[email protected] | 3ef4bc63 | 2010-04-09 04:25:31 | [diff] [blame] | 366 | } |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 367 | default: |
| 368 | NOTREACHED() << "Unexpected preference observed."; |
[email protected] | 3ef4bc63 | 2010-04-09 04:25:31 | [diff] [blame] | 369 | } |
[email protected] | 779c336 | 2010-01-30 01:09:33 | [diff] [blame] | 370 | } |
| 371 | |
[email protected] | d42bf47 | 2014-06-14 01:49:38 | [diff] [blame] | 372 | void HostZoomMapImpl::ClearTemporaryZoomLevel(int render_process_id, |
| 373 | int render_view_id) { |
| 374 | { |
| 375 | base::AutoLock auto_lock(lock_); |
| 376 | RenderViewKey key(render_process_id, render_view_id); |
| 377 | TemporaryZoomLevels::iterator it = temporary_zoom_levels_.find(key); |
| 378 | if (it == temporary_zoom_levels_.end()) |
| 379 | return; |
| 380 | temporary_zoom_levels_.erase(it); |
| 381 | } |
| 382 | RenderViewHost* host = |
| 383 | RenderViewHost::FromID(render_process_id, render_view_id); |
| 384 | DCHECK(host); |
| 385 | // Send a new zoom level, host-specific if one exists. |
| 386 | host->Send(new ViewMsg_SetZoomLevelForView( |
| 387 | render_view_id, |
| 388 | false, |
| 389 | GetZoomLevelForHost( |
| 390 | GetHostFromProcessView(render_process_id, render_view_id)))); |
| 391 | } |
| 392 | |
| 393 | void HostZoomMapImpl::SendZoomLevelChange(const std::string& scheme, |
| 394 | const std::string& host, |
| 395 | double level) { |
| 396 | for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator()); |
| 397 | !i.IsAtEnd(); i.Advance()) { |
| 398 | RenderProcessHost* render_process_host = i.GetCurrentValue(); |
wjmaclean | f9b6ec8 | 2014-08-27 14:33:24 | [diff] [blame] | 399 | if (HostZoomMap::GetDefaultForBrowserContext( |
[email protected] | d42bf47 | 2014-06-14 01:49:38 | [diff] [blame] | 400 | render_process_host->GetBrowserContext()) == this) { |
| 401 | render_process_host->Send( |
| 402 | new ViewMsg_SetZoomLevelForCurrentURL(scheme, host, level)); |
| 403 | } |
| 404 | } |
| 405 | } |
| 406 | |
wjmaclean | de29ed5 | 2014-10-28 21:09:06 | [diff] [blame^] | 407 | void HostZoomMapImpl::SendErrorPageZoomLevelRefresh() { |
| 408 | GURL error_url(kUnreachableWebDataURL); |
| 409 | std::string host = net::GetHostOrSpecFromURL(error_url); |
| 410 | double error_page_zoom_level = GetZoomLevelForHost(host); |
| 411 | |
| 412 | SendZoomLevelChange(std::string(), host, error_page_zoom_level); |
| 413 | } |
| 414 | |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 415 | HostZoomMapImpl::~HostZoomMapImpl() { |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 416 | } |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 417 | |
| 418 | } // namespace content |