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 <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 // Helper function for setting/getting zoom levels for WebContents without | |
| 41 // having to import HostZoomMapImpl everywhere. | |
| 42 double HostZoomMap::GetZoomLevel(const WebContents* web_contents) { | |
| 43 HostZoomMapImpl* host_zoom_map = static_cast<HostZoomMapImpl*>( | |
| 44 HostZoomMap::GetForBrowserContext(web_contents->GetBrowserContext())); | |
| 45 return host_zoom_map->GetZoomLevelForWebContents(*web_contents); | |
| 46 } | |
| 47 | |
| 48 void HostZoomMap::SetZoomLevel(const WebContents* web_contents, double level) { | |
| 49 HostZoomMapImpl* host_zoom_map = static_cast<HostZoomMapImpl*>( | |
| 50 HostZoomMap::GetForBrowserContext(web_contents->GetBrowserContext())); | |
| 51 host_zoom_map->SetZoomLevelForWebContents(*web_contents, level); | |
| 52 } | |
| 53 | |
| 37 HostZoomMapImpl::HostZoomMapImpl() | 54 HostZoomMapImpl::HostZoomMapImpl() |
| 38 : default_zoom_level_(0.0) { | 55 : default_zoom_level_(0.0) { |
| 39 registrar_.Add( | 56 registrar_.Add( |
| 40 this, NOTIFICATION_RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW, | 57 this, NOTIFICATION_RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW, |
| 41 NotificationService::AllSources()); | 58 NotificationService::AllSources()); |
| 42 } | 59 } |
| 43 | 60 |
| 44 void HostZoomMapImpl::CopyFrom(HostZoomMap* copy_interface) { | 61 void HostZoomMapImpl::CopyFrom(HostZoomMap* copy_interface) { |
| 45 // This can only be called on the UI thread to avoid deadlocks, otherwise | 62 // This can only be called on the UI thread to avoid deadlocks, otherwise |
| 46 // UI: a.CopyFrom(b); | 63 // UI: a.CopyFrom(b); |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 187 void HostZoomMapImpl::SetDefaultZoomLevel(double level) { | 204 void HostZoomMapImpl::SetDefaultZoomLevel(double level) { |
| 188 default_zoom_level_ = level; | 205 default_zoom_level_ = level; |
| 189 } | 206 } |
| 190 | 207 |
| 191 scoped_ptr<HostZoomMap::Subscription> | 208 scoped_ptr<HostZoomMap::Subscription> |
| 192 HostZoomMapImpl::AddZoomLevelChangedCallback( | 209 HostZoomMapImpl::AddZoomLevelChangedCallback( |
| 193 const ZoomLevelChangedCallback& callback) { | 210 const ZoomLevelChangedCallback& callback) { |
| 194 return zoom_level_changed_callbacks_.Add(callback); | 211 return zoom_level_changed_callbacks_.Add(callback); |
| 195 } | 212 } |
| 196 | 213 |
| 214 double HostZoomMapImpl::GetZoomLevelForWebContents( | |
| 215 const WebContents& web_contents) const { | |
| 216 const WebContentsImpl& web_contents_impl = | |
| 217 static_cast<const WebContentsImpl&>(web_contents); | |
|
Peter Kasting
2014/05/28 22:49:58
These unconditional downcasts scare me.
The API s
wjmaclean
2014/05/29 14:49:45
I've moved the downcasts to the static accessor fu
Peter Kasting
2014/05/29 18:16:08
What do we really gain by making those callsites n
jam
2014/05/30 16:22:20
casting interfaces to objects that are only implem
| |
| 218 int render_process_id = web_contents_impl.GetRenderProcessHost()->GetID(); | |
| 219 int routing_id = web_contents_impl.GetRenderViewHost()->GetRoutingID(); | |
| 220 | |
| 221 if (UsesTemporaryZoomLevel(render_process_id, routing_id)) { | |
| 222 return GetTemporaryZoomLevel(render_process_id, routing_id); | |
| 223 } else { | |
|
Peter Kasting
2014/05/28 22:49:58
Nit: No else after return
wjmaclean
2014/05/29 14:49:45
Done.
| |
| 224 GURL url; | |
| 225 NavigationEntry* entry = | |
| 226 web_contents_impl.GetController().GetLastCommittedEntry(); | |
| 227 // Since zoom map is updated using rewritten URL, use rewritten URL to get | |
| 228 // the zoom level. | |
|
Peter Kasting
2014/05/28 22:49:58
(1) I don't know what this comment means. What's
wjmaclean
2014/05/29 14:49:45
Done.
In all honesty, I don't even recall writing
| |
| 229 url = entry ? entry->GetURL() : GURL::EmptyGURL(); | |
|
Peter Kasting
2014/05/28 22:49:58
This use of EmptyGURL() is incorrect, and |url| is
wjmaclean
2014/05/29 14:49:45
Done.
| |
| 230 return GetZoomLevelForHostAndScheme(url.scheme(), | |
| 231 net::GetHostOrSpecFromURL(url)); | |
| 232 } | |
| 233 } | |
| 234 | |
| 235 void HostZoomMapImpl::SetZoomLevelForWebContents( | |
| 236 const WebContents& web_contents, | |
| 237 double level) { | |
| 238 const WebContentsImpl& web_contents_impl = | |
| 239 static_cast<const WebContentsImpl&>(web_contents); | |
| 240 int render_process_id = web_contents_impl.GetRenderProcessHost()->GetID(); | |
| 241 int render_view_id = web_contents_impl.GetRenderViewHost()->GetRoutingID(); | |
| 242 if (UsesTemporaryZoomLevel(render_process_id, render_view_id)) { | |
| 243 | |
| 244 SetTemporaryZoomLevel(render_process_id, render_view_id, level); | |
| 245 } else { | |
| 246 SetZoomLevelForHost( | |
| 247 net::GetHostOrSpecFromURL(web_contents_impl.GetLastCommittedURL()), | |
| 248 level); | |
| 249 } | |
| 250 } | |
| 251 | |
| 252 void HostZoomMapImpl::SetZoomLevelForView(int render_process_id, | |
| 253 int render_view_id, | |
| 254 double level, | |
| 255 const std::string& host) { | |
| 256 if (UsesTemporaryZoomLevel(render_process_id, render_view_id)) | |
| 257 SetTemporaryZoomLevel(render_process_id, render_view_id, level); | |
| 258 else | |
| 259 SetZoomLevelForHost(host, level); | |
| 260 } | |
| 261 | |
| 262 bool HostZoomMapImpl::UsesTemporaryZoomLevel(int render_process_id, | |
| 263 int render_view_id) const { | |
| 264 base::AutoLock auto_lock(lock_); | |
| 265 TemporaryZoomLevel zoom_level(render_process_id, render_view_id); | |
|
Peter Kasting
2014/05/28 22:49:58
Tiny nit: You can move this line above the lock (j
wjmaclean
2014/05/29 14:49:45
Done.
| |
| 266 TemporaryZoomLevelList::const_iterator it = find( | |
| 267 temporary_zoom_levels_.begin(), temporary_zoom_levels_.end(), zoom_level); | |
| 268 return it != temporary_zoom_levels_.end(); | |
| 269 } | |
| 270 | |
| 271 void HostZoomMapImpl::SetUsesTemporaryZoomLevel( | |
| 272 int render_process_id, | |
| 273 int render_view_id, | |
| 274 bool uses_temporary_zoom_settings) { | |
| 275 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 276 | |
| 277 base::AutoLock auto_lock(lock_); | |
| 278 TemporaryZoomLevel zoom_level( | |
| 279 render_process_id, render_view_id, default_zoom_level_); | |
|
Peter Kasting
2014/05/28 22:49:58
Tiny nit: Again, this can go above the lock
wjmaclean
2014/05/29 14:49:45
Done.
| |
| 280 TemporaryZoomLevelList::iterator it = find( | |
| 281 temporary_zoom_levels_.begin(), temporary_zoom_levels_.end(), zoom_level); | |
| 282 if (uses_temporary_zoom_settings) { | |
| 283 if (it == temporary_zoom_levels_.end()) | |
| 284 temporary_zoom_levels_.push_back(zoom_level); | |
| 285 } else { | |
| 286 if (it != temporary_zoom_levels_.end()) | |
|
Peter Kasting
2014/05/28 22:49:58
Nit: Can be combined with the else
wjmaclean
2014/05/29 14:49:45
Done.
| |
| 287 temporary_zoom_levels_.erase(it); | |
| 288 } | |
| 289 } | |
| 290 | |
| 197 double HostZoomMapImpl::GetTemporaryZoomLevel(int render_process_id, | 291 double HostZoomMapImpl::GetTemporaryZoomLevel(int render_process_id, |
| 198 int render_view_id) const { | 292 int render_view_id) const { |
| 199 base::AutoLock auto_lock(lock_); | 293 base::AutoLock auto_lock(lock_); |
| 200 for (size_t i = 0; i < temporary_zoom_levels_.size(); ++i) { | 294 for (size_t i = 0; i < temporary_zoom_levels_.size(); ++i) { |
| 201 if (temporary_zoom_levels_[i].render_process_id == render_process_id && | 295 if (temporary_zoom_levels_[i].render_process_id == render_process_id && |
| 202 temporary_zoom_levels_[i].render_view_id == render_view_id) { | 296 temporary_zoom_levels_[i].render_view_id == render_view_id) { |
| 203 return temporary_zoom_levels_[i].zoom_level; | 297 return temporary_zoom_levels_[i].zoom_level; |
| 204 } | 298 } |
| 205 } | 299 } |
| 300 | |
| 206 return 0; | 301 return 0; |
| 207 } | 302 } |
| 208 | 303 |
| 209 void HostZoomMapImpl::SetTemporaryZoomLevel(int render_process_id, | 304 void HostZoomMapImpl::SetTemporaryZoomLevel(int render_process_id, |
| 210 int render_view_id, | 305 int render_view_id, |
| 211 double level) { | 306 double level) { |
| 212 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 307 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 213 | 308 |
| 214 { | 309 { |
| 215 base::AutoLock auto_lock(lock_); | 310 base::AutoLock auto_lock(lock_); |
| 216 size_t i; | 311 size_t i; |
| 217 for (i = 0; i < temporary_zoom_levels_.size(); ++i) { | 312 for (i = 0; i < temporary_zoom_levels_.size(); ++i) { |
| 218 if (temporary_zoom_levels_[i].render_process_id == render_process_id && | 313 if (temporary_zoom_levels_[i].render_process_id == render_process_id && |
| 219 temporary_zoom_levels_[i].render_view_id == render_view_id) { | 314 temporary_zoom_levels_[i].render_view_id == render_view_id) { |
| 220 if (level) { | 315 if (level) { |
| 221 temporary_zoom_levels_[i].zoom_level = level; | 316 temporary_zoom_levels_[i].zoom_level = level; |
| 222 } else { | 317 } else { |
| 223 temporary_zoom_levels_.erase(temporary_zoom_levels_.begin() + i); | 318 temporary_zoom_levels_.erase(temporary_zoom_levels_.begin() + i); |
| 224 } | 319 } |
| 225 break; | 320 break; |
| 226 } | 321 } |
| 227 } | 322 } |
| 228 | 323 |
| 229 if (level && i == temporary_zoom_levels_.size()) { | 324 if (level && i == temporary_zoom_levels_.size()) { |
| 230 TemporaryZoomLevel temp; | 325 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); | 326 temporary_zoom_levels_.push_back(temp); |
| 235 } | 327 } |
| 236 } | 328 } |
| 237 | 329 |
| 238 HostZoomMap::ZoomLevelChange change; | 330 HostZoomMap::ZoomLevelChange change; |
| 239 change.mode = HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM; | 331 change.mode = HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM; |
| 240 change.zoom_level = level; | 332 change.zoom_level = level; |
| 241 | 333 |
| 242 zoom_level_changed_callbacks_.Notify(change); | 334 zoom_level_changed_callbacks_.Notify(change); |
| 243 } | 335 } |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 263 } | 355 } |
| 264 default: | 356 default: |
| 265 NOTREACHED() << "Unexpected preference observed."; | 357 NOTREACHED() << "Unexpected preference observed."; |
| 266 } | 358 } |
| 267 } | 359 } |
| 268 | 360 |
| 269 HostZoomMapImpl::~HostZoomMapImpl() { | 361 HostZoomMapImpl::~HostZoomMapImpl() { |
| 270 } | 362 } |
| 271 | 363 |
| 272 } // namespace content | 364 } // namespace content |
| OLD | NEW |