| 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( |
| 46 *static_cast<const WebContentsImpl*>(web_contents)); |
| 47 } |
| 48 |
| 49 void HostZoomMap::SetZoomLevel(const WebContents* web_contents, double level) { |
| 50 HostZoomMapImpl* host_zoom_map = static_cast<HostZoomMapImpl*>( |
| 51 HostZoomMap::GetForBrowserContext(web_contents->GetBrowserContext())); |
| 52 host_zoom_map->SetZoomLevelForWebContents( |
| 53 *static_cast<const WebContentsImpl*>(web_contents), level); |
| 54 } |
| 55 |
| 37 HostZoomMapImpl::HostZoomMapImpl() | 56 HostZoomMapImpl::HostZoomMapImpl() |
| 38 : default_zoom_level_(0.0) { | 57 : default_zoom_level_(0.0) { |
| 39 registrar_.Add( | 58 registrar_.Add( |
| 40 this, NOTIFICATION_RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW, | 59 this, NOTIFICATION_RENDER_VIEW_HOST_WILL_CLOSE_RENDER_VIEW, |
| 41 NotificationService::AllSources()); | 60 NotificationService::AllSources()); |
| 42 } | 61 } |
| 43 | 62 |
| 44 void HostZoomMapImpl::CopyFrom(HostZoomMap* copy_interface) { | 63 void HostZoomMapImpl::CopyFrom(HostZoomMap* copy_interface) { |
| 45 // This can only be called on the UI thread to avoid deadlocks, otherwise | 64 // This can only be called on the UI thread to avoid deadlocks, otherwise |
| 46 // UI: a.CopyFrom(b); | 65 // UI: a.CopyFrom(b); |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 187 void HostZoomMapImpl::SetDefaultZoomLevel(double level) { | 206 void HostZoomMapImpl::SetDefaultZoomLevel(double level) { |
| 188 default_zoom_level_ = level; | 207 default_zoom_level_ = level; |
| 189 } | 208 } |
| 190 | 209 |
| 191 scoped_ptr<HostZoomMap::Subscription> | 210 scoped_ptr<HostZoomMap::Subscription> |
| 192 HostZoomMapImpl::AddZoomLevelChangedCallback( | 211 HostZoomMapImpl::AddZoomLevelChangedCallback( |
| 193 const ZoomLevelChangedCallback& callback) { | 212 const ZoomLevelChangedCallback& callback) { |
| 194 return zoom_level_changed_callbacks_.Add(callback); | 213 return zoom_level_changed_callbacks_.Add(callback); |
| 195 } | 214 } |
| 196 | 215 |
| 216 double HostZoomMapImpl::GetZoomLevelForWebContents( |
| 217 const WebContentsImpl& web_contents_impl) const { |
| 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 |
| 224 // Since zoom map is updated using the url as stored in the navigation |
| 225 // controller, we use that URL to get the zoom level. |
| 226 GURL url; |
| 227 NavigationEntry* entry = |
| 228 web_contents_impl.GetController().GetLastCommittedEntry(); |
| 229 if (entry) |
| 230 url = entry->GetURL(); |
| 231 return GetZoomLevelForHostAndScheme(url.scheme(), |
| 232 net::GetHostOrSpecFromURL(url)); |
| 233 } |
| 234 |
| 235 void HostZoomMapImpl::SetZoomLevelForWebContents( |
| 236 const WebContentsImpl& web_contents_impl, |
| 237 double level) { |
| 238 int render_process_id = web_contents_impl.GetRenderProcessHost()->GetID(); |
| 239 int render_view_id = web_contents_impl.GetRenderViewHost()->GetRoutingID(); |
| 240 if (UsesTemporaryZoomLevel(render_process_id, render_view_id)) { |
| 241 |
| 242 SetTemporaryZoomLevel(render_process_id, render_view_id, level); |
| 243 } else { |
| 244 SetZoomLevelForHost( |
| 245 net::GetHostOrSpecFromURL(web_contents_impl.GetLastCommittedURL()), |
| 246 level); |
| 247 } |
| 248 } |
| 249 |
| 250 void HostZoomMapImpl::SetZoomLevelForView(int render_process_id, |
| 251 int render_view_id, |
| 252 double level, |
| 253 const std::string& host) { |
| 254 if (UsesTemporaryZoomLevel(render_process_id, render_view_id)) |
| 255 SetTemporaryZoomLevel(render_process_id, render_view_id, level); |
| 256 else |
| 257 SetZoomLevelForHost(host, level); |
| 258 } |
| 259 |
| 260 bool HostZoomMapImpl::UsesTemporaryZoomLevel(int render_process_id, |
| 261 int render_view_id) const { |
| 262 TemporaryZoomLevel zoom_level(render_process_id, render_view_id); |
| 263 |
| 264 base::AutoLock auto_lock(lock_); |
| 265 TemporaryZoomLevels::const_iterator it = std::find( |
| 266 temporary_zoom_levels_.begin(), temporary_zoom_levels_.end(), zoom_level); |
| 267 return it != temporary_zoom_levels_.end(); |
| 268 } |
| 269 |
| 270 void HostZoomMapImpl::SetUsesTemporaryZoomLevel( |
| 271 int render_process_id, |
| 272 int render_view_id, |
| 273 bool uses_temporary_zoom_level) { |
| 274 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 275 |
| 276 TemporaryZoomLevel zoom_level( |
| 277 render_process_id, render_view_id, default_zoom_level_); |
| 278 |
| 279 base::AutoLock auto_lock(lock_); |
| 280 TemporaryZoomLevels::iterator it = std::find( |
| 281 temporary_zoom_levels_.begin(), temporary_zoom_levels_.end(), zoom_level); |
| 282 if (uses_temporary_zoom_level) { |
| 283 if (it == temporary_zoom_levels_.end()) |
| 284 temporary_zoom_levels_.push_back(zoom_level); |
| 285 } else if (it != temporary_zoom_levels_.end()) { |
| 286 temporary_zoom_levels_.erase(it); |
| 287 } |
| 288 } |
| 289 |
| 197 double HostZoomMapImpl::GetTemporaryZoomLevel(int render_process_id, | 290 double HostZoomMapImpl::GetTemporaryZoomLevel(int render_process_id, |
| 198 int render_view_id) const { | 291 int render_view_id) const { |
| 199 base::AutoLock auto_lock(lock_); | 292 base::AutoLock auto_lock(lock_); |
| 200 for (size_t i = 0; i < temporary_zoom_levels_.size(); ++i) { | 293 for (size_t i = 0; i < temporary_zoom_levels_.size(); ++i) { |
| 201 if (temporary_zoom_levels_[i].render_process_id == render_process_id && | 294 if (temporary_zoom_levels_[i].render_process_id == render_process_id && |
| 202 temporary_zoom_levels_[i].render_view_id == render_view_id) { | 295 temporary_zoom_levels_[i].render_view_id == render_view_id) { |
| 203 return temporary_zoom_levels_[i].zoom_level; | 296 return temporary_zoom_levels_[i].zoom_level; |
| 204 } | 297 } |
| 205 } | 298 } |
| 299 |
| 206 return 0; | 300 return 0; |
| 207 } | 301 } |
| 208 | 302 |
| 209 void HostZoomMapImpl::SetTemporaryZoomLevel(int render_process_id, | 303 void HostZoomMapImpl::SetTemporaryZoomLevel(int render_process_id, |
| 210 int render_view_id, | 304 int render_view_id, |
| 211 double level) { | 305 double level) { |
| 212 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 306 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 213 | 307 |
| 214 { | 308 { |
| 215 base::AutoLock auto_lock(lock_); | 309 base::AutoLock auto_lock(lock_); |
| 216 size_t i; | 310 size_t i; |
| 217 for (i = 0; i < temporary_zoom_levels_.size(); ++i) { | 311 for (i = 0; i < temporary_zoom_levels_.size(); ++i) { |
| 218 if (temporary_zoom_levels_[i].render_process_id == render_process_id && | 312 if (temporary_zoom_levels_[i].render_process_id == render_process_id && |
| 219 temporary_zoom_levels_[i].render_view_id == render_view_id) { | 313 temporary_zoom_levels_[i].render_view_id == render_view_id) { |
| 220 if (level) { | 314 if (level) { |
| 221 temporary_zoom_levels_[i].zoom_level = level; | 315 temporary_zoom_levels_[i].zoom_level = level; |
| 222 } else { | 316 } else { |
| 223 temporary_zoom_levels_.erase(temporary_zoom_levels_.begin() + i); | 317 temporary_zoom_levels_.erase(temporary_zoom_levels_.begin() + i); |
| 224 } | 318 } |
| 225 break; | 319 break; |
| 226 } | 320 } |
| 227 } | 321 } |
| 228 | 322 |
| 229 if (level && i == temporary_zoom_levels_.size()) { | 323 if (level && i == temporary_zoom_levels_.size()) { |
| 230 TemporaryZoomLevel temp; | 324 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); | 325 temporary_zoom_levels_.push_back(temp); |
| 235 } | 326 } |
| 236 } | 327 } |
| 237 | 328 |
| 238 HostZoomMap::ZoomLevelChange change; | 329 HostZoomMap::ZoomLevelChange change; |
| 239 change.mode = HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM; | 330 change.mode = HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM; |
| 240 change.zoom_level = level; | 331 change.zoom_level = level; |
| 241 | 332 |
| 242 zoom_level_changed_callbacks_.Notify(change); | 333 zoom_level_changed_callbacks_.Notify(change); |
| 243 } | 334 } |
| (...skipping 18 matching lines...) Expand all Loading... |
| 262 break; | 353 break; |
| 263 } | 354 } |
| 264 default: | 355 default: |
| 265 NOTREACHED() << "Unexpected preference observed."; | 356 NOTREACHED() << "Unexpected preference observed."; |
| 266 } | 357 } |
| 267 } | 358 } |
| 268 | 359 |
| 269 HostZoomMapImpl::~HostZoomMapImpl() { | 360 HostZoomMapImpl::~HostZoomMapImpl() { |
| 270 } | 361 } |
| 271 | 362 |
| 363 HostZoomMapImpl::TemporaryZoomLevel::TemporaryZoomLevel(int process_id, |
| 364 int view_id, |
| 365 double level) |
| 366 : render_process_id(process_id), |
| 367 render_view_id(view_id), |
| 368 zoom_level(level) { |
| 369 } |
| 370 |
| 371 HostZoomMapImpl::TemporaryZoomLevel::TemporaryZoomLevel(int process_id, |
| 372 int view_id) |
| 373 : render_process_id(process_id), |
| 374 render_view_id(view_id), |
| 375 zoom_level(0.0) { |
| 376 } |
| 377 |
| 378 bool HostZoomMapImpl::TemporaryZoomLevel::operator==( |
| 379 const TemporaryZoomLevel& other) const { |
| 380 return other.render_process_id == render_process_id && |
| 381 other.render_view_id == render_view_id; |
| 382 } |
| 383 |
| 272 } // namespace content | 384 } // namespace content |
| OLD | NEW |