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" |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 80 } | 80 } |
| 81 default_zoom_level_ = copy->default_zoom_level_; | 81 default_zoom_level_ = copy->default_zoom_level_; |
| 82 } | 82 } |
| 83 | 83 |
| 84 double HostZoomMapImpl::GetZoomLevelForHost(const std::string& host) const { | 84 double HostZoomMapImpl::GetZoomLevelForHost(const std::string& host) const { |
| 85 base::AutoLock auto_lock(lock_); | 85 base::AutoLock auto_lock(lock_); |
| 86 HostZoomLevels::const_iterator i(host_zoom_levels_.find(host)); | 86 HostZoomLevels::const_iterator i(host_zoom_levels_.find(host)); |
| 87 return (i == host_zoom_levels_.end()) ? default_zoom_level_ : i->second; | 87 return (i == host_zoom_levels_.end()) ? default_zoom_level_ : i->second; |
| 88 } | 88 } |
| 89 | 89 |
| 90 bool HostZoomMapImpl::HasZoomLevel(const std::string& scheme, | |
| 91 const std::string& host) const { | |
| 92 base::AutoLock auto_lock(lock_); | |
| 93 | |
| 94 SchemeHostZoomLevels::const_iterator scheme_iterator( | |
| 95 scheme_host_zoom_levels_.find(scheme)); | |
| 96 | |
| 97 const HostZoomLevels& zoom_levels = | |
| 98 (scheme_iterator != scheme_host_zoom_levels_.end()) | |
| 99 ? scheme_iterator->second | |
| 100 : host_zoom_levels_; | |
| 101 | |
| 102 HostZoomLevels::const_iterator i(zoom_levels.find(host)); | |
| 103 return i != zoom_levels.end(); | |
| 104 } | |
| 105 | |
| 90 double HostZoomMapImpl::GetZoomLevelForHostAndScheme( | 106 double HostZoomMapImpl::GetZoomLevelForHostAndScheme( |
| 91 const std::string& scheme, | 107 const std::string& scheme, |
| 92 const std::string& host) const { | 108 const std::string& host) const { |
| 93 { | 109 { |
| 94 base::AutoLock auto_lock(lock_); | 110 base::AutoLock auto_lock(lock_); |
| 95 SchemeHostZoomLevels::const_iterator scheme_iterator( | 111 SchemeHostZoomLevels::const_iterator scheme_iterator( |
| 96 scheme_host_zoom_levels_.find(scheme)); | 112 scheme_host_zoom_levels_.find(scheme)); |
| 97 if (scheme_iterator != scheme_host_zoom_levels_.end()) { | 113 if (scheme_iterator != scheme_host_zoom_levels_.end()) { |
| 98 HostZoomLevels::const_iterator i(scheme_iterator->second.find(host)); | 114 HostZoomLevels::const_iterator i(scheme_iterator->second.find(host)); |
| 99 if (i != scheme_iterator->second.end()) | 115 if (i != scheme_iterator->second.end()) |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 145 | 161 |
| 146 { | 162 { |
| 147 base::AutoLock auto_lock(lock_); | 163 base::AutoLock auto_lock(lock_); |
| 148 | 164 |
| 149 if (ZoomValuesEqual(level, default_zoom_level_)) | 165 if (ZoomValuesEqual(level, default_zoom_level_)) |
| 150 host_zoom_levels_.erase(host); | 166 host_zoom_levels_.erase(host); |
| 151 else | 167 else |
| 152 host_zoom_levels_[host] = level; | 168 host_zoom_levels_[host] = level; |
| 153 } | 169 } |
| 154 | 170 |
| 155 // Notify renderers from this browser context. | 171 SendZoomLevelChange(std::string(), host, level); |
| 156 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator()); | 172 |
| 157 !i.IsAtEnd(); i.Advance()) { | |
| 158 RenderProcessHost* render_process_host = i.GetCurrentValue(); | |
| 159 if (HostZoomMap::GetForBrowserContext( | |
| 160 render_process_host->GetBrowserContext()) == this) { | |
| 161 render_process_host->Send( | |
| 162 new ViewMsg_SetZoomLevelForCurrentURL(std::string(), host, level)); | |
| 163 } | |
| 164 } | |
| 165 HostZoomMap::ZoomLevelChange change; | 173 HostZoomMap::ZoomLevelChange change; |
| 166 change.mode = HostZoomMap::ZOOM_CHANGED_FOR_HOST; | 174 change.mode = HostZoomMap::ZOOM_CHANGED_FOR_HOST; |
| 167 change.host = host; | 175 change.host = host; |
| 168 change.zoom_level = level; | 176 change.zoom_level = level; |
| 169 | 177 |
| 170 zoom_level_changed_callbacks_.Notify(change); | 178 zoom_level_changed_callbacks_.Notify(change); |
| 171 } | 179 } |
| 172 | 180 |
| 173 void HostZoomMapImpl::SetZoomLevelForHostAndScheme(const std::string& scheme, | 181 void HostZoomMapImpl::SetZoomLevelForHostAndScheme(const std::string& scheme, |
| 174 const std::string& host, | 182 const std::string& host, |
| 175 double level) { | 183 double level) { |
| 176 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 184 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 177 { | 185 { |
| 178 base::AutoLock auto_lock(lock_); | 186 base::AutoLock auto_lock(lock_); |
| 179 scheme_host_zoom_levels_[scheme][host] = level; | 187 scheme_host_zoom_levels_[scheme][host] = level; |
| 180 } | 188 } |
| 181 | 189 |
| 182 // Notify renderers from this browser context. | 190 SendZoomLevelChange(scheme, host, level); |
| 183 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator()); | |
| 184 !i.IsAtEnd(); i.Advance()) { | |
| 185 RenderProcessHost* render_process_host = i.GetCurrentValue(); | |
| 186 if (HostZoomMap::GetForBrowserContext( | |
| 187 render_process_host->GetBrowserContext()) == this) { | |
| 188 render_process_host->Send( | |
| 189 new ViewMsg_SetZoomLevelForCurrentURL(scheme, host, level)); | |
| 190 } | |
| 191 } | |
| 192 | 191 |
| 193 HostZoomMap::ZoomLevelChange change; | 192 HostZoomMap::ZoomLevelChange change; |
| 194 change.mode = HostZoomMap::ZOOM_CHANGED_FOR_SCHEME_AND_HOST; | 193 change.mode = HostZoomMap::ZOOM_CHANGED_FOR_SCHEME_AND_HOST; |
| 195 change.host = host; | 194 change.host = host; |
| 196 change.scheme = scheme; | 195 change.scheme = scheme; |
| 197 change.zoom_level = level; | 196 change.zoom_level = level; |
| 198 | 197 |
| 199 zoom_level_changed_callbacks_.Notify(change); | 198 zoom_level_changed_callbacks_.Notify(change); |
| 200 } | 199 } |
| 201 | 200 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 234 return GetZoomLevelForHostAndScheme(url.scheme(), | 233 return GetZoomLevelForHostAndScheme(url.scheme(), |
| 235 net::GetHostOrSpecFromURL(url)); | 234 net::GetHostOrSpecFromURL(url)); |
| 236 } | 235 } |
| 237 | 236 |
| 238 void HostZoomMapImpl::SetZoomLevelForWebContents( | 237 void HostZoomMapImpl::SetZoomLevelForWebContents( |
| 239 const WebContentsImpl& web_contents_impl, | 238 const WebContentsImpl& web_contents_impl, |
| 240 double level) { | 239 double level) { |
| 241 int render_process_id = web_contents_impl.GetRenderProcessHost()->GetID(); | 240 int render_process_id = web_contents_impl.GetRenderProcessHost()->GetID(); |
| 242 int render_view_id = web_contents_impl.GetRenderViewHost()->GetRoutingID(); | 241 int render_view_id = web_contents_impl.GetRenderViewHost()->GetRoutingID(); |
| 243 if (UsesTemporaryZoomLevel(render_process_id, render_view_id)) { | 242 if (UsesTemporaryZoomLevel(render_process_id, render_view_id)) { |
| 244 SetTemporaryZoomLevel(render_process_id, render_view_id, level); | 243 SetTemporaryZoomLevel(render_process_id, |
| 244 render_view_id, | |
| 245 level); | |
| 245 } else { | 246 } else { |
| 246 // Get the url from the navigation controller directly, as calling | 247 // Get the url from the navigation controller directly, as calling |
| 247 // WebContentsImpl::GetLastCommittedURL() may give us a virtual url that | 248 // WebContentsImpl::GetLastCommittedURL() may give us a virtual url that |
| 248 // is different than what the render view is using. If the two don't match, | 249 // is different than what the render view is using. If the two don't match, |
| 249 // the attempt to set the zoom will fail. | 250 // the attempt to set the zoom will fail. |
| 250 NavigationEntry* entry = | 251 NavigationEntry* entry = |
| 251 web_contents_impl.GetController().GetLastCommittedEntry(); | 252 web_contents_impl.GetController().GetLastCommittedEntry(); |
| 252 // Tests may invoke this function with a null entry, but we don't | 253 // Tests may invoke this function with a null entry, but we don't |
| 253 // want to save zoom levels in this case. | 254 // want to save zoom levels in this case. |
| 254 if (!entry) | 255 if (!entry) |
| 255 return; | 256 return; |
| 256 | 257 |
| 257 GURL url = entry->GetURL(); | 258 GURL url = entry->GetURL(); |
| 258 SetZoomLevelForHost(net::GetHostOrSpecFromURL(url), level); | 259 SetZoomLevelForHost(net::GetHostOrSpecFromURL(url), level); |
| 259 } | 260 } |
| 260 } | 261 } |
| 261 | 262 |
| 262 void HostZoomMapImpl::SetZoomLevelForView(int render_process_id, | 263 void HostZoomMapImpl::SetZoomLevelForView(int render_process_id, |
| 263 int render_view_id, | 264 int render_view_id, |
| 264 double level, | 265 double level, |
| 265 const std::string& host) { | 266 const std::string& host) { |
| 266 if (UsesTemporaryZoomLevel(render_process_id, render_view_id)) | 267 if (UsesTemporaryZoomLevel(render_process_id, render_view_id)) |
| 267 SetTemporaryZoomLevel(render_process_id, render_view_id, level); | 268 SetTemporaryZoomLevel(render_process_id, render_view_id, level); |
| 268 else | 269 else |
| 269 SetZoomLevelForHost(host, level); | 270 SetZoomLevelForHost(host, level); |
| 270 } | 271 } |
| 271 | 272 |
| 272 bool HostZoomMapImpl::UsesTemporaryZoomLevel(int render_process_id, | 273 bool HostZoomMapImpl::UsesTemporaryZoomLevel(int render_process_id, |
| 273 int render_view_id) const { | 274 int render_view_id) const { |
| 274 TemporaryZoomLevel zoom_level(render_process_id, render_view_id); | 275 RenderViewKey key(render_process_id, render_view_id); |
| 275 | 276 |
| 276 base::AutoLock auto_lock(lock_); | 277 base::AutoLock auto_lock(lock_); |
| 277 TemporaryZoomLevels::const_iterator it = std::find( | 278 return temporary_zoom_levels_.find(key) != temporary_zoom_levels_.end(); |
|
Fady Samuel
2014/06/12 18:49:48
Use ContainsKey: https://code.google.com/p/chromiu
wjmaclean
2014/06/12 19:35:27
Done.
| |
| 278 temporary_zoom_levels_.begin(), temporary_zoom_levels_.end(), zoom_level); | |
| 279 return it != temporary_zoom_levels_.end(); | |
| 280 } | |
| 281 | |
| 282 void HostZoomMapImpl::SetUsesTemporaryZoomLevel( | |
| 283 int render_process_id, | |
| 284 int render_view_id, | |
| 285 bool uses_temporary_zoom_level) { | |
| 286 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 287 | |
| 288 TemporaryZoomLevel zoom_level( | |
| 289 render_process_id, render_view_id, default_zoom_level_); | |
| 290 | |
| 291 base::AutoLock auto_lock(lock_); | |
| 292 TemporaryZoomLevels::iterator it = std::find( | |
| 293 temporary_zoom_levels_.begin(), temporary_zoom_levels_.end(), zoom_level); | |
| 294 if (uses_temporary_zoom_level) { | |
| 295 if (it == temporary_zoom_levels_.end()) | |
| 296 temporary_zoom_levels_.push_back(zoom_level); | |
| 297 } else if (it != temporary_zoom_levels_.end()) { | |
| 298 temporary_zoom_levels_.erase(it); | |
| 299 } | |
| 300 } | 279 } |
| 301 | 280 |
| 302 double HostZoomMapImpl::GetTemporaryZoomLevel(int render_process_id, | 281 double HostZoomMapImpl::GetTemporaryZoomLevel(int render_process_id, |
| 303 int render_view_id) const { | 282 int render_view_id) const { |
| 304 base::AutoLock auto_lock(lock_); | 283 base::AutoLock auto_lock(lock_); |
| 305 for (size_t i = 0; i < temporary_zoom_levels_.size(); ++i) { | 284 RenderViewKey key(render_process_id, render_view_id); |
| 306 if (temporary_zoom_levels_[i].render_process_id == render_process_id && | 285 TemporaryZoomLevels::const_iterator it = temporary_zoom_levels_.find(key); |
|
Fady Samuel
2014/06/12 18:49:48
for readability:
if (!ContainsKey(temporary_zoom_
wjmaclean
2014/06/12 19:35:27
Done.
We don't worry about the cost of two lookup
| |
| 307 temporary_zoom_levels_[i].render_view_id == render_view_id) { | 286 if (it != temporary_zoom_levels_.end()) |
| 308 return temporary_zoom_levels_[i].zoom_level; | 287 return it->second; |
| 309 } | |
| 310 } | |
| 311 | 288 |
| 312 return 0; | 289 return 0; |
| 313 } | 290 } |
| 314 | 291 |
| 292 namespace { | |
| 293 | |
| 294 std::string GetHostFromProcessView(int render_process_id, int render_view_id) { | |
| 295 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 296 RenderViewHost* render_view_host = | |
| 297 RenderViewHost::FromID(render_process_id, render_view_id); | |
| 298 if (!render_view_host) | |
| 299 return std::string(); | |
| 300 | |
| 301 WebContents* web_contents = WebContents::FromRenderViewHost(render_view_host); | |
| 302 DCHECK(web_contents); | |
| 303 | |
| 304 NavigationEntry* entry = | |
| 305 web_contents->GetController().GetLastCommittedEntry(); | |
| 306 if (!entry) | |
| 307 return std::string(); | |
| 308 | |
| 309 return net::GetHostOrSpecFromURL(entry->GetURL()); | |
| 310 } | |
| 311 | |
| 312 } // namespace | |
| 313 | |
| 315 void HostZoomMapImpl::SetTemporaryZoomLevel(int render_process_id, | 314 void HostZoomMapImpl::SetTemporaryZoomLevel(int render_process_id, |
| 316 int render_view_id, | 315 int render_view_id, |
| 317 double level) { | 316 double level) { |
| 318 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 317 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 319 | 318 |
| 320 { | 319 { |
| 320 RenderViewKey key(render_process_id, render_view_id); | |
| 321 base::AutoLock auto_lock(lock_); | 321 base::AutoLock auto_lock(lock_); |
| 322 size_t i; | |
| 323 for (i = 0; i < temporary_zoom_levels_.size(); ++i) { | |
| 324 if (temporary_zoom_levels_[i].render_process_id == render_process_id && | |
| 325 temporary_zoom_levels_[i].render_view_id == render_view_id) { | |
| 326 if (level) { | |
| 327 temporary_zoom_levels_[i].zoom_level = level; | |
| 328 } else { | |
| 329 temporary_zoom_levels_.erase(temporary_zoom_levels_.begin() + i); | |
| 330 } | |
| 331 break; | |
| 332 } | |
| 333 } | |
| 334 | 322 |
| 335 if (level && i == temporary_zoom_levels_.size()) { | 323 TemporaryZoomLevels::iterator it = temporary_zoom_levels_.find(key); |
| 336 TemporaryZoomLevel temp(render_process_id, render_view_id, level); | 324 if (it == temporary_zoom_levels_.end()) |
|
Fady Samuel
2014/06/12 18:49:48
Use ContainsKey.
wjmaclean
2014/06/12 19:35:27
Actually, we can just assign using [] since it doe
| |
| 337 temporary_zoom_levels_.push_back(temp); | 325 temporary_zoom_levels_.insert(std::make_pair(key, level)); |
| 338 } | 326 else |
| 327 it->second = level; | |
| 339 } | 328 } |
| 340 | 329 |
| 330 RenderViewHost* host = | |
| 331 RenderViewHost::FromID(render_process_id, render_view_id); | |
| 332 DCHECK(host); | |
| 333 host->Send(new ViewMsg_SetZoomLevelForView(render_view_id, true, level)); | |
| 334 | |
| 341 HostZoomMap::ZoomLevelChange change; | 335 HostZoomMap::ZoomLevelChange change; |
| 342 change.mode = HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM; | 336 change.mode = HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM; |
| 337 change.host = GetHostFromProcessView(render_process_id, render_view_id); | |
| 343 change.zoom_level = level; | 338 change.zoom_level = level; |
| 344 | 339 |
| 345 zoom_level_changed_callbacks_.Notify(change); | 340 zoom_level_changed_callbacks_.Notify(change); |
| 346 } | 341 } |
| 347 | 342 |
| 348 void HostZoomMapImpl::Observe(int type, | 343 void HostZoomMapImpl::Observe(int type, |
| 349 const NotificationSource& source, | 344 const NotificationSource& source, |
| 350 const NotificationDetails& details) { | 345 const NotificationDetails& details) { |
| 351 switch (type) { | 346 switch (type) { |
| 352 case NOTIFICATION_RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW: { | 347 case NOTIFICATION_RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW: { |
| 353 base::AutoLock auto_lock(lock_); | |
| 354 int render_view_id = Source<RenderViewHost>(source)->GetRoutingID(); | 348 int render_view_id = Source<RenderViewHost>(source)->GetRoutingID(); |
| 355 int render_process_id = | 349 int render_process_id = |
| 356 Source<RenderViewHost>(source)->GetProcess()->GetID(); | 350 Source<RenderViewHost>(source)->GetProcess()->GetID(); |
| 357 | 351 ClearTemporaryZoomLevel(render_process_id, render_view_id); |
| 358 for (size_t i = 0; i < temporary_zoom_levels_.size(); ++i) { | |
| 359 if (temporary_zoom_levels_[i].render_process_id == render_process_id && | |
| 360 temporary_zoom_levels_[i].render_view_id == render_view_id) { | |
| 361 temporary_zoom_levels_.erase(temporary_zoom_levels_.begin() + i); | |
| 362 break; | |
| 363 } | |
| 364 } | |
| 365 break; | 352 break; |
| 366 } | 353 } |
| 367 default: | 354 default: |
| 368 NOTREACHED() << "Unexpected preference observed."; | 355 NOTREACHED() << "Unexpected preference observed."; |
| 369 } | 356 } |
| 370 } | 357 } |
| 371 | 358 |
| 359 void HostZoomMapImpl::ClearTemporaryZoomLevel(int render_process_id, | |
| 360 int render_view_id) { | |
| 361 { | |
| 362 base::AutoLock auto_lock(lock_); | |
| 363 RenderViewKey key(render_process_id, render_view_id); | |
| 364 TemporaryZoomLevels::iterator it = temporary_zoom_levels_.find(key); | |
| 365 if (it == temporary_zoom_levels_.end()) | |
| 366 return; | |
| 367 // TODO(wjmaclean) Do we need to lookup the appropriate per-origin | |
|
Fady Samuel
2014/06/12 18:49:48
Do this now? This comment and the one below seem t
wjmaclean
2014/06/12 19:35:27
Done.
| |
| 368 // zoom level (if any) and send it to the render view? | |
| 369 temporary_zoom_levels_.erase(it); | |
| 370 } | |
| 371 RenderViewHost* host = | |
| 372 RenderViewHost::FromID(render_process_id, render_view_id); | |
| 373 DCHECK(host); | |
| 374 // TODO(wjmaclean) Need to see if there's a mapped host zoom level to use. | |
|
Fady Samuel
2014/06/12 18:49:48
Please do this now? Would calling GetZoomLevelForH
wjmaclean
2014/06/12 19:35:27
Yeah, probably ... I was still sort of deciding ab
| |
| 375 host->Send(new ViewMsg_SetZoomLevelForView( | |
| 376 render_view_id, false, GetDefaultZoomLevel())); | |
| 377 } | |
| 378 | |
| 379 void HostZoomMapImpl::SendZoomLevelChange(const std::string& scheme, | |
| 380 const std::string& host, | |
| 381 double level) { | |
| 382 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator()); | |
| 383 !i.IsAtEnd(); i.Advance()) { | |
| 384 RenderProcessHost* render_process_host = i.GetCurrentValue(); | |
| 385 if (HostZoomMap::GetForBrowserContext( | |
| 386 render_process_host->GetBrowserContext()) == this) { | |
| 387 render_process_host->Send( | |
| 388 new ViewMsg_SetZoomLevelForCurrentURL(scheme, host, level)); | |
| 389 } | |
| 390 } | |
| 391 } | |
| 392 | |
| 372 HostZoomMapImpl::~HostZoomMapImpl() { | 393 HostZoomMapImpl::~HostZoomMapImpl() { |
| 373 } | 394 } |
| 374 | 395 |
| 375 HostZoomMapImpl::TemporaryZoomLevel::TemporaryZoomLevel(int process_id, | |
| 376 int view_id, | |
| 377 double level) | |
| 378 : render_process_id(process_id), | |
| 379 render_view_id(view_id), | |
| 380 zoom_level(level) { | |
| 381 } | |
| 382 | |
| 383 HostZoomMapImpl::TemporaryZoomLevel::TemporaryZoomLevel(int process_id, | |
| 384 int view_id) | |
| 385 : render_process_id(process_id), | |
| 386 render_view_id(view_id), | |
| 387 zoom_level(0.0) { | |
| 388 } | |
| 389 | |
| 390 bool HostZoomMapImpl::TemporaryZoomLevel::operator==( | |
| 391 const TemporaryZoomLevel& other) const { | |
| 392 return other.render_process_id == render_process_id && | |
| 393 other.render_view_id == render_view_id; | |
| 394 } | |
| 395 | |
| 396 } // namespace content | 396 } // namespace content |
| OLD | NEW |