| 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 <cmath> | 8 #include <cmath> |
| 8 | 9 |
| 9 #include "base/strings/string_piece.h" | 10 #include "base/strings/string_piece.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 11 #include "base/values.h" | 12 #include "base/values.h" |
| 13 #include "content/browser/frame_host/navigation_entry_impl.h" |
| 12 #include "content/browser/renderer_host/render_process_host_impl.h" | 14 #include "content/browser/renderer_host/render_process_host_impl.h" |
| 13 #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" |
| 14 #include "content/common/view_messages.h" | 17 #include "content/common/view_messages.h" |
| 15 #include "content/public/browser/browser_context.h" | 18 #include "content/public/browser/browser_context.h" |
| 16 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
| 17 #include "content/public/browser/notification_service.h" | 20 #include "content/public/browser/notification_service.h" |
| 18 #include "content/public/browser/notification_types.h" | 21 #include "content/public/browser/notification_types.h" |
| 19 #include "content/public/browser/resource_context.h" | 22 #include "content/public/browser/resource_context.h" |
| 20 #include "content/public/common/page_zoom.h" | 23 #include "content/public/common/page_zoom.h" |
| 21 #include "net/base/net_util.h" | 24 #include "net/base/net_util.h" |
| 22 | 25 |
| 23 static const char* kHostZoomMapKeyName = "content_host_zoom_map"; | 26 static const char* kHostZoomMapKeyName = "content_host_zoom_map"; |
| 24 | 27 |
| 25 namespace content { | 28 namespace content { |
| 26 | 29 |
| 27 HostZoomMap* HostZoomMap::GetForBrowserContext(BrowserContext* context) { | 30 HostZoomMap* HostZoomMap::GetForBrowserContext(BrowserContext* context) { |
| 28 HostZoomMapImpl* rv = static_cast<HostZoomMapImpl*>( | 31 HostZoomMapImpl* rv = static_cast<HostZoomMapImpl*>( |
| 29 context->GetUserData(kHostZoomMapKeyName)); | 32 context->GetUserData(kHostZoomMapKeyName)); |
| 30 if (!rv) { | 33 if (!rv) { |
| 31 rv = new HostZoomMapImpl(); | 34 rv = new HostZoomMapImpl(); |
| 32 context->SetUserData(kHostZoomMapKeyName, rv); | 35 context->SetUserData(kHostZoomMapKeyName, rv); |
| 33 } | 36 } |
| 34 return rv; | 37 return rv; |
| 35 } | 38 } |
| 36 | 39 |
| 40 double HostZoomMap::GetZoomLevel(const WebContents* web_contents) { |
| 41 HostZoomMapImpl* host_zoom_map = static_cast<HostZoomMapImpl*>( |
| 42 HostZoomMap::GetForBrowserContext(web_contents->GetBrowserContext())); |
| 43 return host_zoom_map->GetZoomLevelForWebContents(*web_contents); |
| 44 } |
| 45 |
| 46 void HostZoomMap::SetZoomLevel(const WebContents* web_contents, double level) { |
| 47 HostZoomMapImpl* host_zoom_map = static_cast<HostZoomMapImpl*>( |
| 48 HostZoomMap::GetForBrowserContext(web_contents->GetBrowserContext())); |
| 49 host_zoom_map->SetZoomLevelForWebContents(*web_contents, level); |
| 50 } |
| 51 |
| 37 HostZoomMapImpl::HostZoomMapImpl() | 52 HostZoomMapImpl::HostZoomMapImpl() |
| 38 : default_zoom_level_(0.0) { | 53 : default_zoom_level_(0.0) { |
| 39 registrar_.Add( | 54 registrar_.Add( |
| 40 this, NOTIFICATION_RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW, | 55 this, NOTIFICATION_RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW, |
| 41 NotificationService::AllSources()); | 56 NotificationService::AllSources()); |
| 42 } | 57 } |
| 43 | 58 |
| 44 void HostZoomMapImpl::CopyFrom(HostZoomMap* copy_interface) { | 59 void HostZoomMapImpl::CopyFrom(HostZoomMap* copy_interface) { |
| 45 // This can only be called on the UI thread to avoid deadlocks, otherwise | 60 // This can only be called on the UI thread to avoid deadlocks, otherwise |
| 46 // UI: a.CopyFrom(b); | 61 // UI: a.CopyFrom(b); |
| 47 // IO: b.CopyFrom(a); | 62 // IO: b.CopyFrom(a); |
| 48 // can deadlock. | 63 // can deadlock. |
| 49 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 64 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 50 HostZoomMapImpl* copy = static_cast<HostZoomMapImpl*>(copy_interface); | 65 HostZoomMapImpl* copy = static_cast<HostZoomMapImpl*>(copy_interface); |
| 51 base::AutoLock auto_lock(lock_); | 66 base::AutoLock auto_lock(lock_); |
| 52 base::AutoLock copy_auto_lock(copy->lock_); | 67 base::AutoLock copy_auto_lock(copy->lock_); |
| 53 host_zoom_levels_. | 68 host_zoom_levels_. |
| 54 insert(copy->host_zoom_levels_.begin(), copy->host_zoom_levels_.end()); | 69 insert(copy->host_zoom_levels_.begin(), copy->host_zoom_levels_.end()); |
| 55 for (SchemeHostZoomLevels::const_iterator i(copy-> | 70 for (SchemeHostZoomLevels::const_iterator i(copy-> |
| 56 scheme_host_zoom_levels_.begin()); | 71 scheme_host_zoom_levels_.begin()); |
| 57 i != copy->scheme_host_zoom_levels_.end(); ++i) { | 72 i != copy->scheme_host_zoom_levels_.end(); ++i) { |
| 58 scheme_host_zoom_levels_[i->first] = HostZoomLevels(); | 73 scheme_host_zoom_levels_[i->first] = HostZoomLevels(); |
| 59 scheme_host_zoom_levels_[i->first]. | 74 scheme_host_zoom_levels_[i->first]. |
| 60 insert(i->second.begin(), i->second.end()); | 75 insert(i->second.begin(), i->second.end()); |
| 61 } | 76 } |
| 62 default_zoom_level_ = copy->default_zoom_level_; | 77 default_zoom_level_ = copy->default_zoom_level_; |
| 63 } | 78 } |
| 64 | 79 |
| 80 double HostZoomMapImpl::GetZoomLevelForWebContents( |
| 81 const WebContents& web_contents) const { |
| 82 const WebContentsImpl& web_contents_impl = |
| 83 static_cast<const WebContentsImpl&>(web_contents); |
| 84 int render_process_id = web_contents_impl.GetRenderProcessHost()->GetID(); |
| 85 int routing_id = web_contents_impl.GetRenderViewHost()->GetRoutingID(); |
| 86 |
| 87 if (UsesTemporaryZoomLevel(render_process_id, routing_id)) { |
| 88 return GetTemporaryZoomLevel(render_process_id, routing_id); |
| 89 } else { |
| 90 GURL url; |
| 91 NavigationEntry* entry = |
| 92 web_contents_impl.GetController().GetLastCommittedEntry(); |
| 93 // Since zoom map is updated using rewritten URL, use rewritten URL to get |
| 94 // the zoom level. |
| 95 url = entry ? entry->GetURL() : GURL::EmptyGURL(); |
| 96 return GetZoomLevelForHostAndScheme(url.scheme(), |
| 97 net::GetHostOrSpecFromURL(url)); |
| 98 } |
| 99 } |
| 100 |
| 65 double HostZoomMapImpl::GetZoomLevelForHost(const std::string& host) const { | 101 double HostZoomMapImpl::GetZoomLevelForHost(const std::string& host) const { |
| 66 base::AutoLock auto_lock(lock_); | 102 base::AutoLock auto_lock(lock_); |
| 67 HostZoomLevels::const_iterator i(host_zoom_levels_.find(host)); | 103 HostZoomLevels::const_iterator i(host_zoom_levels_.find(host)); |
| 68 return (i == host_zoom_levels_.end()) ? default_zoom_level_ : i->second; | 104 return (i == host_zoom_levels_.end()) ? default_zoom_level_ : i->second; |
| 69 } | 105 } |
| 70 | 106 |
| 71 double HostZoomMapImpl::GetZoomLevelForHostAndScheme( | 107 double HostZoomMapImpl::GetZoomLevelForHostAndScheme( |
| 72 const std::string& scheme, | 108 const std::string& scheme, |
| 73 const std::string& host) const { | 109 const std::string& host) const { |
| 74 { | 110 { |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 113 scheme, // scheme | 149 scheme, // scheme |
| 114 j->second // zoom level | 150 j->second // zoom level |
| 115 }; | 151 }; |
| 116 result.push_back(change); | 152 result.push_back(change); |
| 117 } | 153 } |
| 118 } | 154 } |
| 119 } | 155 } |
| 120 return result; | 156 return result; |
| 121 } | 157 } |
| 122 | 158 |
| 159 void HostZoomMapImpl::SetZoomLevelForWebContents( |
| 160 const WebContents& web_contents, |
| 161 double level) { |
| 162 const WebContentsImpl& web_contents_impl = |
| 163 static_cast<const WebContentsImpl&>(web_contents); |
| 164 int render_process_id = web_contents_impl.GetRenderProcessHost()->GetID(); |
| 165 int render_view_id = web_contents_impl.GetRenderViewHost()->GetRoutingID(); |
| 166 if (UsesTemporaryZoomLevel(render_process_id, render_view_id)) { |
| 167 |
| 168 SetTemporaryZoomLevel(render_process_id, render_view_id, level); |
| 169 } else { |
| 170 SetZoomLevelForHost( |
| 171 net::GetHostOrSpecFromURL(web_contents_impl.GetLastCommittedURL()), |
| 172 level); |
| 173 } |
| 174 } |
| 175 |
| 176 void HostZoomMapImpl::SetZoomLevelForView(int render_process_id, |
| 177 int render_view_id, |
| 178 double level, |
| 179 const std::string& host) { |
| 180 if (UsesTemporaryZoomLevel(render_process_id, render_view_id)) |
| 181 SetTemporaryZoomLevel(render_process_id, render_view_id, level); |
| 182 else |
| 183 SetZoomLevelForHost(host, level); |
| 184 } |
| 185 |
| 123 void HostZoomMapImpl::SetZoomLevelForHost(const std::string& host, | 186 void HostZoomMapImpl::SetZoomLevelForHost(const std::string& host, |
| 124 double level) { | 187 double level) { |
| 125 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 188 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 126 | 189 |
| 127 { | 190 { |
| 128 base::AutoLock auto_lock(lock_); | 191 base::AutoLock auto_lock(lock_); |
| 129 | 192 |
| 130 if (ZoomValuesEqual(level, default_zoom_level_)) | 193 if (ZoomValuesEqual(level, default_zoom_level_)) |
| 131 host_zoom_levels_.erase(host); | 194 host_zoom_levels_.erase(host); |
| 132 else | 195 else |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 187 void HostZoomMapImpl::SetDefaultZoomLevel(double level) { | 250 void HostZoomMapImpl::SetDefaultZoomLevel(double level) { |
| 188 default_zoom_level_ = level; | 251 default_zoom_level_ = level; |
| 189 } | 252 } |
| 190 | 253 |
| 191 scoped_ptr<HostZoomMap::Subscription> | 254 scoped_ptr<HostZoomMap::Subscription> |
| 192 HostZoomMapImpl::AddZoomLevelChangedCallback( | 255 HostZoomMapImpl::AddZoomLevelChangedCallback( |
| 193 const ZoomLevelChangedCallback& callback) { | 256 const ZoomLevelChangedCallback& callback) { |
| 194 return zoom_level_changed_callbacks_.Add(callback); | 257 return zoom_level_changed_callbacks_.Add(callback); |
| 195 } | 258 } |
| 196 | 259 |
| 260 void HostZoomMapImpl::SetUsesTemporaryZoomLevel( |
| 261 int render_process_id, |
| 262 int render_view_id, |
| 263 bool uses_temporary_zoom_settings) { |
| 264 base::AutoLock auto_lock(lock_); |
| 265 TemporaryZoomLevel zoom_level( |
| 266 render_process_id, render_view_id, default_zoom_level_); |
| 267 TemporaryZoomLevelList::iterator it = find( |
| 268 temporary_zoom_levels_.begin(), temporary_zoom_levels_.end(), zoom_level); |
| 269 if (uses_temporary_zoom_settings) { |
| 270 if (it == temporary_zoom_levels_.end()) |
| 271 temporary_zoom_levels_.push_back(zoom_level); |
| 272 } else { |
| 273 if (it != temporary_zoom_levels_.end()) |
| 274 temporary_zoom_levels_.erase(it); |
| 275 } |
| 276 } |
| 277 |
| 278 bool HostZoomMapImpl::UsesTemporaryZoomLevel(int render_process_id, |
| 279 int render_view_id) const { |
| 280 base::AutoLock auto_lock(lock_); |
| 281 TemporaryZoomLevel zoom_level(render_process_id, render_view_id); |
| 282 TemporaryZoomLevelList::const_iterator it = find( |
| 283 temporary_zoom_levels_.begin(), temporary_zoom_levels_.end(), zoom_level); |
| 284 return it != temporary_zoom_levels_.end(); |
| 285 } |
| 286 |
| 197 double HostZoomMapImpl::GetTemporaryZoomLevel(int render_process_id, | 287 double HostZoomMapImpl::GetTemporaryZoomLevel(int render_process_id, |
| 198 int render_view_id) const { | 288 int render_view_id) const { |
| 199 base::AutoLock auto_lock(lock_); | 289 base::AutoLock auto_lock(lock_); |
| 200 for (size_t i = 0; i < temporary_zoom_levels_.size(); ++i) { | 290 for (size_t i = 0; i < temporary_zoom_levels_.size(); ++i) { |
| 201 if (temporary_zoom_levels_[i].render_process_id == render_process_id && | 291 if (temporary_zoom_levels_[i].render_process_id == render_process_id && |
| 202 temporary_zoom_levels_[i].render_view_id == render_view_id) { | 292 temporary_zoom_levels_[i].render_view_id == render_view_id) { |
| 203 return temporary_zoom_levels_[i].zoom_level; | 293 return temporary_zoom_levels_[i].zoom_level; |
| 204 } | 294 } |
| 205 } | 295 } |
| 206 return 0; | 296 return 0; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 220 if (level) { | 310 if (level) { |
| 221 temporary_zoom_levels_[i].zoom_level = level; | 311 temporary_zoom_levels_[i].zoom_level = level; |
| 222 } else { | 312 } else { |
| 223 temporary_zoom_levels_.erase(temporary_zoom_levels_.begin() + i); | 313 temporary_zoom_levels_.erase(temporary_zoom_levels_.begin() + i); |
| 224 } | 314 } |
| 225 break; | 315 break; |
| 226 } | 316 } |
| 227 } | 317 } |
| 228 | 318 |
| 229 if (level && i == temporary_zoom_levels_.size()) { | 319 if (level && i == temporary_zoom_levels_.size()) { |
| 230 TemporaryZoomLevel temp; | 320 TemporaryZoomLevel temp(render_process_id, render_view_id, level); |
| 231 temp.render_process_id = render_process_id; | |
| 232 temp.render_view_id = render_view_id; | |
| 233 temp.zoom_level = level; | |
| 234 temporary_zoom_levels_.push_back(temp); | 321 temporary_zoom_levels_.push_back(temp); |
| 235 } | 322 } |
| 236 } | 323 } |
| 237 | 324 |
| 238 HostZoomMap::ZoomLevelChange change; | 325 HostZoomMap::ZoomLevelChange change; |
| 239 change.mode = HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM; | 326 change.mode = HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM; |
| 240 change.zoom_level = level; | 327 change.zoom_level = level; |
| 241 | 328 |
| 242 zoom_level_changed_callbacks_.Notify(change); | 329 zoom_level_changed_callbacks_.Notify(change); |
| 243 } | 330 } |
| (...skipping 19 matching lines...) Expand all Loading... |
| 263 } | 350 } |
| 264 default: | 351 default: |
| 265 NOTREACHED() << "Unexpected preference observed."; | 352 NOTREACHED() << "Unexpected preference observed."; |
| 266 } | 353 } |
| 267 } | 354 } |
| 268 | 355 |
| 269 HostZoomMapImpl::~HostZoomMapImpl() { | 356 HostZoomMapImpl::~HostZoomMapImpl() { |
| 270 } | 357 } |
| 271 | 358 |
| 272 } // namespace content | 359 } // namespace content |
| OLD | NEW |