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 "components/zoom/zoom_controller.h" | 5 #include "components/zoom/zoom_controller.h" |
6 | 6 |
7 #include "components/zoom/zoom_event_manager.h" | 7 #include "components/zoom/zoom_event_manager.h" |
8 #include "components/zoom/zoom_observer.h" | 8 #include "components/zoom/zoom_observer.h" |
| 9 #include "content/public/browser/browser_context.h" |
9 #include "content/public/browser/host_zoom_map.h" | 10 #include "content/public/browser/host_zoom_map.h" |
10 #include "content/public/browser/navigation_details.h" | 11 #include "content/public/browser/navigation_details.h" |
11 #include "content/public/browser/navigation_entry.h" | 12 #include "content/public/browser/navigation_entry.h" |
12 #include "content/public/browser/navigation_handle.h" | 13 #include "content/public/browser/navigation_handle.h" |
| 14 #include "content/public/browser/render_frame_host.h" |
13 #include "content/public/browser/render_process_host.h" | 15 #include "content/public/browser/render_process_host.h" |
14 #include "content/public/browser/render_view_host.h" | 16 #include "content/public/browser/render_view_host.h" |
| 17 #include "content/public/browser/storage_partition.h" |
15 #include "content/public/browser/web_contents.h" | 18 #include "content/public/browser/web_contents.h" |
16 #include "content/public/common/page_type.h" | 19 #include "content/public/common/page_type.h" |
17 #include "content/public/common/page_zoom.h" | 20 #include "content/public/common/page_zoom.h" |
18 #include "net/base/url_util.h" | 21 #include "net/base/url_util.h" |
19 | 22 |
20 DEFINE_WEB_CONTENTS_USER_DATA_KEY(zoom::ZoomController); | 23 DEFINE_WEB_CONTENTS_USER_DATA_KEY(zoom::ZoomController); |
21 | 24 |
22 namespace zoom { | 25 namespace zoom { |
23 | 26 |
24 double ZoomController::GetZoomLevelForWebContents( | 27 double ZoomController::GetZoomLevelForWebContents( |
25 const content::WebContents* web_contents) { | 28 const content::WebContents* web_contents) { |
26 if (!web_contents) | 29 if (!web_contents) |
27 return 0.0; | 30 return 0.0; |
28 | 31 |
29 auto* zoom_controller = FromWebContents(web_contents); | 32 auto* zoom_controller = FromWebContents(web_contents); |
30 if (zoom_controller) | 33 if (zoom_controller) |
31 return zoom_controller->GetZoomLevel(); | 34 return zoom_controller->GetZoomLevel(); |
32 | 35 |
33 return content::HostZoomMap::GetZoomLevel(web_contents); | 36 return content::HostZoomMap::GetZoomLevel(web_contents); |
34 } | 37 } |
35 | 38 |
36 ZoomController::ZoomController(content::WebContents* web_contents) | 39 ZoomController::ZoomController(content::WebContents* web_contents) |
37 : content::WebContentsObserver(web_contents), | 40 : content::WebContentsObserver(web_contents), |
38 can_show_bubble_(true), | 41 can_show_bubble_(true), |
39 zoom_mode_(ZOOM_MODE_DEFAULT), | 42 default_scope_is_per_origin_(ZoomPrefsDelegate::kZoomScopeSettingDefault), |
40 zoom_level_(1.0), | 43 zoom_level_(1.0), |
41 browser_context_(web_contents->GetBrowserContext()) { | 44 browser_context_(web_contents->GetBrowserContext()) { |
42 host_zoom_map_ = content::HostZoomMap::GetForWebContents(web_contents); | 45 host_zoom_map_ = content::HostZoomMap::GetForWebContents(web_contents); |
43 zoom_level_ = host_zoom_map_->GetDefaultZoomLevel(); | 46 zoom_level_ = host_zoom_map_->GetDefaultZoomLevel(); |
44 | 47 |
| 48 ZoomPrefsDelegate* zoom_prefs_delegate = static_cast<ZoomPrefsDelegate*>( |
| 49 content::BrowserContext::GetStoragePartition( |
| 50 browser_context_, web_contents->GetSiteInstance()) |
| 51 ->GetZoomLevelDelegate()); |
| 52 if (zoom_prefs_delegate) { |
| 53 default_scope_is_per_origin_ = |
| 54 zoom_prefs_delegate->GetZoomScopeIsPerOriginPref(); |
| 55 // TODO(mcnee) If the need to navigate between StoragePartitions is |
| 56 // introduced by a new feature, we will need to add logic to update this |
| 57 // subscription. |
| 58 default_zoom_scope_subscription_ = |
| 59 zoom_prefs_delegate->RegisterDefaultZoomScopeCallback( |
| 60 base::Bind(&ZoomController::OnDefaultZoomScopeChanged, |
| 61 base::Unretained(this))); |
| 62 } |
| 63 zoom_mode_ = |
| 64 default_scope_is_per_origin_ ? ZOOM_MODE_DEFAULT : ZOOM_MODE_ISOLATED; |
| 65 |
| 66 // When in isolated zoom, a temporary zoom level needs to be set to override |
| 67 // any existing per-host zoom levels. |
| 68 // If this WebContents is a clone of another, don't overwrite the temporary |
| 69 // level we've inherited. |
| 70 int render_process_id = web_contents->GetRenderProcessHost()->GetID(); |
| 71 int render_view_id = web_contents->GetRenderViewHost()->GetRoutingID(); |
| 72 if (zoom_mode_ == ZOOM_MODE_ISOLATED && |
| 73 !host_zoom_map_->UsesTemporaryZoomLevel(render_process_id, |
| 74 render_view_id)) { |
| 75 host_zoom_map_->SetTemporaryZoomLevel(render_process_id, render_view_id, |
| 76 zoom_level_); |
| 77 } |
| 78 |
45 zoom_subscription_ = host_zoom_map_->AddZoomLevelChangedCallback( | 79 zoom_subscription_ = host_zoom_map_->AddZoomLevelChangedCallback( |
46 base::Bind(&ZoomController::OnZoomLevelChanged, base::Unretained(this))); | 80 base::Bind(&ZoomController::OnZoomLevelChanged, base::Unretained(this))); |
47 | 81 |
48 UpdateState(std::string()); | 82 UpdateState(std::string()); |
49 } | 83 } |
50 | 84 |
51 ZoomController::~ZoomController() {} | 85 ZoomController::~ZoomController() {} |
52 | 86 |
53 bool ZoomController::IsAtDefaultZoom() const { | 87 bool ZoomController::IsAtDefaultZoom() const { |
54 return content::ZoomValuesEqual(GetZoomLevel(), GetDefaultZoomLevel()); | 88 return content::ZoomValuesEqual(GetZoomLevel(), GetDefaultZoomLevel()); |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
173 content::HostZoomMap* zoom_map = | 207 content::HostZoomMap* zoom_map = |
174 content::HostZoomMap::GetForWebContents(web_contents()); | 208 content::HostZoomMap::GetForWebContents(web_contents()); |
175 DCHECK(zoom_map); | 209 DCHECK(zoom_map); |
176 int render_process_id = web_contents()->GetRenderProcessHost()->GetID(); | 210 int render_process_id = web_contents()->GetRenderProcessHost()->GetID(); |
177 int render_view_id = web_contents()->GetRenderViewHost()->GetRoutingID(); | 211 int render_view_id = web_contents()->GetRenderViewHost()->GetRoutingID(); |
178 double original_zoom_level = GetZoomLevel(); | 212 double original_zoom_level = GetZoomLevel(); |
179 | 213 |
180 DCHECK(!event_data_); | 214 DCHECK(!event_data_); |
181 event_data_.reset(new ZoomChangedEventData( | 215 event_data_.reset(new ZoomChangedEventData( |
182 web_contents(), original_zoom_level, original_zoom_level, new_mode, | 216 web_contents(), original_zoom_level, original_zoom_level, new_mode, |
183 new_mode != ZOOM_MODE_DEFAULT)); | 217 new_mode != (default_scope_is_per_origin_ ? ZOOM_MODE_DEFAULT |
| 218 : ZOOM_MODE_ISOLATED))); |
184 | 219 |
185 switch (new_mode) { | 220 switch (new_mode) { |
186 case ZOOM_MODE_DEFAULT: { | 221 case ZOOM_MODE_DEFAULT: { |
187 content::NavigationEntry* entry = | 222 content::NavigationEntry* entry = |
188 web_contents()->GetController().GetLastCommittedEntry(); | 223 web_contents()->GetController().GetLastCommittedEntry(); |
189 | 224 |
190 if (entry) { | 225 if (entry) { |
191 GURL url = content::HostZoomMap::GetURLFromEntry(entry); | 226 GURL url = content::HostZoomMap::GetURLFromEntry(entry); |
192 std::string host = net::GetHostOrSpecFromURL(url); | 227 std::string host = net::GetHostOrSpecFromURL(url); |
193 | 228 |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
254 break; | 289 break; |
255 } | 290 } |
256 } | 291 } |
257 // Any event data we've stored should have been consumed by this point. | 292 // Any event data we've stored should have been consumed by this point. |
258 DCHECK(!event_data_); | 293 DCHECK(!event_data_); |
259 | 294 |
260 zoom_mode_ = new_mode; | 295 zoom_mode_ = new_mode; |
261 } | 296 } |
262 | 297 |
263 void ZoomController::ResetZoomModeOnNavigationIfNeeded(const GURL& url) { | 298 void ZoomController::ResetZoomModeOnNavigationIfNeeded(const GURL& url) { |
264 if (zoom_mode_ != ZOOM_MODE_ISOLATED && zoom_mode_ != ZOOM_MODE_MANUAL) | 299 if (zoom_mode_ != (default_scope_is_per_origin_ ? ZOOM_MODE_ISOLATED |
| 300 : ZOOM_MODE_DEFAULT) && |
| 301 zoom_mode_ != ZOOM_MODE_MANUAL) |
265 return; | 302 return; |
266 | 303 |
267 int render_process_id = web_contents()->GetRenderProcessHost()->GetID(); | 304 int render_process_id = web_contents()->GetRenderProcessHost()->GetID(); |
268 int render_view_id = web_contents()->GetRenderViewHost()->GetRoutingID(); | 305 int render_view_id = web_contents()->GetRenderViewHost()->GetRoutingID(); |
269 content::HostZoomMap* zoom_map = | 306 content::HostZoomMap* zoom_map = |
270 content::HostZoomMap::GetForWebContents(web_contents()); | 307 content::HostZoomMap::GetForWebContents(web_contents()); |
| 308 double old_zoom_level = GetZoomLevel(); |
| 309 // Set |zoom_level_| after calling GetZoomLevel() so we get the correct old |
| 310 // zoom level if we're resetting from manual. |
271 zoom_level_ = zoom_map->GetDefaultZoomLevel(); | 311 zoom_level_ = zoom_map->GetDefaultZoomLevel(); |
272 double old_zoom_level = zoom_map->GetZoomLevel(web_contents()); | 312 ZoomMode new_mode; |
273 double new_zoom_level = zoom_map->GetZoomLevelForHostAndScheme( | 313 double new_zoom_level; |
274 url.scheme(), net::GetHostOrSpecFromURL(url)); | 314 if (default_scope_is_per_origin_) { |
| 315 new_mode = ZOOM_MODE_DEFAULT; |
| 316 new_zoom_level = zoom_map->GetZoomLevelForHostAndScheme( |
| 317 url.scheme(), net::GetHostOrSpecFromURL(url)); |
| 318 } else { |
| 319 new_mode = ZOOM_MODE_ISOLATED; |
| 320 new_zoom_level = zoom_level_; |
| 321 } |
275 event_data_.reset(new ZoomChangedEventData(web_contents(), old_zoom_level, | 322 event_data_.reset(new ZoomChangedEventData(web_contents(), old_zoom_level, |
276 new_zoom_level, ZOOM_MODE_DEFAULT, | 323 new_zoom_level, new_mode, |
277 false /* can_show_bubble */)); | 324 false /* can_show_bubble */)); |
278 // The call to ClearTemporaryZoomLevel() doesn't generate any events from | 325 if (default_scope_is_per_origin_) { |
279 // HostZoomMap, but the call to UpdateState() at the end of this function | 326 // The call to ClearTemporaryZoomLevel() doesn't generate any events from |
280 // will notify our observers. | 327 // HostZoomMap, but the call to UpdateState() at the end of this function |
281 // Note: it's possible the render_process/view ids have disappeared (e.g. | 328 // will notify our observers. |
282 // if we navigated to a new origin), but this won't cause a problem in the | 329 // Note: it's possible the render_process/view ids have disappeared (e.g. |
283 // call below. | 330 // if we navigated to a new origin), but this won't cause a problem in the |
284 zoom_map->ClearTemporaryZoomLevel(render_process_id, render_view_id); | 331 // call below. |
285 zoom_mode_ = ZOOM_MODE_DEFAULT; | 332 zoom_map->ClearTemporaryZoomLevel(render_process_id, render_view_id); |
| 333 } else { |
| 334 // If we're resetting to isolated zoom, a temporary zoom level needs to |
| 335 // be set to override any existing per-host zoom levels. |
| 336 zoom_map->SetTemporaryZoomLevel(render_process_id, render_view_id, |
| 337 new_zoom_level); |
| 338 } |
| 339 zoom_mode_ = new_mode; |
| 340 } |
| 341 |
| 342 void ZoomController::UpdateZoomModeOnScopeChangeIfNeeded() { |
| 343 if (zoom_mode_ != ZOOM_MODE_DEFAULT && zoom_mode_ != ZOOM_MODE_ISOLATED) |
| 344 return; |
| 345 |
| 346 // When going from per-tab to per-host, if there isn't an existing per-host |
| 347 // zoom level set and we have a non-default temporary zoom level, this |
| 348 // ZoomController will set the per-host zoom level. |
| 349 SetZoomMode(default_scope_is_per_origin_ ? ZOOM_MODE_DEFAULT |
| 350 : ZOOM_MODE_ISOLATED); |
286 } | 351 } |
287 | 352 |
288 void ZoomController::DidFinishNavigation( | 353 void ZoomController::DidFinishNavigation( |
289 content::NavigationHandle* navigation_handle) { | 354 content::NavigationHandle* navigation_handle) { |
290 if (!navigation_handle->IsInMainFrame() || !navigation_handle->HasCommitted()) | 355 if (!navigation_handle->IsInMainFrame() || !navigation_handle->HasCommitted()) |
291 return; | 356 return; |
292 | 357 |
293 if (navigation_handle->IsErrorPage()) | 358 if (navigation_handle->IsErrorPage()) |
294 content::HostZoomMap::SendErrorPageZoomLevelRefresh(web_contents()); | 359 content::HostZoomMap::SendErrorPageZoomLevelRefresh(web_contents()); |
295 | 360 |
296 if (!navigation_handle->IsSamePage()) | 361 if (!navigation_handle->IsSamePage()) |
297 ResetZoomModeOnNavigationIfNeeded(navigation_handle->GetURL()); | 362 ResetZoomModeOnNavigationIfNeeded(navigation_handle->GetURL()); |
298 | 363 |
299 // If the main frame's content has changed, the new page may have a different | 364 // If the main frame's content has changed, the new page may have a different |
300 // zoom level from the old one. | 365 // zoom level from the old one. |
301 UpdateState(std::string()); | 366 UpdateState(std::string()); |
302 DCHECK(!event_data_); | 367 DCHECK(!event_data_); |
303 } | 368 } |
304 | 369 |
305 void ZoomController::WebContentsDestroyed() { | 370 void ZoomController::WebContentsDestroyed() { |
| 371 // Once our WebContents is destroyed, we can no longer respond to zoom scope |
| 372 // changes. |
| 373 default_zoom_scope_subscription_.reset(); |
306 // At this point we should no longer be sending any zoom events with this | 374 // At this point we should no longer be sending any zoom events with this |
307 // WebContents. | 375 // WebContents. |
308 observers_.Clear(); | 376 observers_.Clear(); |
309 } | 377 } |
310 | 378 |
311 void ZoomController::RenderFrameHostChanged( | 379 void ZoomController::RenderFrameHostChanged( |
312 content::RenderFrameHost* old_host, | 380 content::RenderFrameHost* old_host, |
313 content::RenderFrameHost* new_host) { | 381 content::RenderFrameHost* new_host) { |
314 // If our associated HostZoomMap changes, update our event subscription. | |
315 content::HostZoomMap* new_host_zoom_map = | 382 content::HostZoomMap* new_host_zoom_map = |
316 content::HostZoomMap::GetForWebContents(web_contents()); | 383 content::HostZoomMap::GetForWebContents(web_contents()); |
| 384 |
| 385 // The HostZoomMap records temporary zoom levels per RenderView. When zooming |
| 386 // is per-tab and our RenderView changes, we need to copy the level for our |
| 387 // new RenderView so that the tab's zoom level is preserved. |
| 388 if (old_host && !default_scope_is_per_origin_ && |
| 389 zoom_mode_ == ZOOM_MODE_ISOLATED) { |
| 390 PreserveTemporaryZoomLevel(host_zoom_map_, new_host_zoom_map, old_host, |
| 391 new_host); |
| 392 } |
| 393 |
| 394 // If our associated HostZoomMap changes, update our event subscription. |
317 if (new_host_zoom_map == host_zoom_map_) | 395 if (new_host_zoom_map == host_zoom_map_) |
318 return; | 396 return; |
319 | 397 |
320 host_zoom_map_ = new_host_zoom_map; | 398 host_zoom_map_ = new_host_zoom_map; |
321 zoom_subscription_ = host_zoom_map_->AddZoomLevelChangedCallback( | 399 zoom_subscription_ = host_zoom_map_->AddZoomLevelChangedCallback( |
322 base::Bind(&ZoomController::OnZoomLevelChanged, base::Unretained(this))); | 400 base::Bind(&ZoomController::OnZoomLevelChanged, base::Unretained(this))); |
323 } | 401 } |
324 | 402 |
| 403 void ZoomController::PreserveTemporaryZoomLevel( |
| 404 const content::HostZoomMap* old_zoom_map, |
| 405 content::HostZoomMap* new_zoom_map, |
| 406 content::RenderFrameHost* old_host, |
| 407 content::RenderFrameHost* new_host) const { |
| 408 const content::RenderViewHost* old_view_host = old_host->GetRenderViewHost(); |
| 409 const content::RenderViewHost* new_view_host = new_host->GetRenderViewHost(); |
| 410 |
| 411 DCHECK(old_zoom_map->UsesTemporaryZoomLevel( |
| 412 old_view_host->GetProcess()->GetID(), old_view_host->GetRoutingID())); |
| 413 |
| 414 const double level = old_zoom_map->GetTemporaryZoomLevel( |
| 415 old_view_host->GetProcess()->GetID(), old_view_host->GetRoutingID()); |
| 416 new_zoom_map->SetTemporaryZoomLevel(new_view_host->GetProcess()->GetID(), |
| 417 new_view_host->GetRoutingID(), level); |
| 418 } |
| 419 |
| 420 void ZoomController::PreserveTemporaryZoomLevel( |
| 421 content::WebContents* old_web_contents, |
| 422 content::WebContents* new_web_contents) const { |
| 423 if (default_scope_is_per_origin_ || zoom_mode_ != ZOOM_MODE_ISOLATED) |
| 424 return; |
| 425 |
| 426 content::RenderFrameHost* old_host = old_web_contents->GetMainFrame(); |
| 427 content::RenderFrameHost* new_host = new_web_contents->GetMainFrame(); |
| 428 const content::HostZoomMap* old_host_zoom_map = |
| 429 content::HostZoomMap::GetForWebContents(old_web_contents); |
| 430 content::HostZoomMap* new_host_zoom_map = |
| 431 content::HostZoomMap::GetForWebContents(new_web_contents); |
| 432 |
| 433 PreserveTemporaryZoomLevel(old_host_zoom_map, new_host_zoom_map, old_host, |
| 434 new_host); |
| 435 } |
| 436 |
| 437 void ZoomController::DidCloneToNewWebContents( |
| 438 content::WebContents* old_web_contents, |
| 439 content::WebContents* new_web_contents) { |
| 440 // If a WebContents is cloned when zooming is per-tab, have the clone inherit |
| 441 // the original's zoom level. |
| 442 PreserveTemporaryZoomLevel(old_web_contents, new_web_contents); |
| 443 } |
| 444 |
| 445 void ZoomController::WebContentsReplaced( |
| 446 content::WebContents* new_web_contents) { |
| 447 // A tab may replace its WebContents with another, but from the user's |
| 448 // perspective, this is still the same tab, so we need to preserve the |
| 449 // per-tab zoom. |
| 450 PreserveTemporaryZoomLevel(web_contents(), new_web_contents); |
| 451 } |
| 452 |
325 void ZoomController::OnZoomLevelChanged( | 453 void ZoomController::OnZoomLevelChanged( |
326 const content::HostZoomMap::ZoomLevelChange& change) { | 454 const content::HostZoomMap::ZoomLevelChange& change) { |
327 UpdateState(change.host); | 455 UpdateState(change.host); |
328 } | 456 } |
329 | 457 |
| 458 void ZoomController::OnDefaultZoomScopeChanged() { |
| 459 default_scope_is_per_origin_ = !default_scope_is_per_origin_; |
| 460 UpdateZoomModeOnScopeChangeIfNeeded(); |
| 461 } |
| 462 |
330 void ZoomController::UpdateState(const std::string& host) { | 463 void ZoomController::UpdateState(const std::string& host) { |
331 // If |host| is empty, all observers should be updated. | 464 // If |host| is empty, all observers should be updated. |
332 if (!host.empty()) { | 465 if (!host.empty()) { |
333 // Use the navigation entry's URL instead of the WebContents' so virtual | 466 // Use the navigation entry's URL instead of the WebContents' so virtual |
334 // URLs work (e.g. chrome://settings). http://crbug.com/153950 | 467 // URLs work (e.g. chrome://settings). http://crbug.com/153950 |
335 content::NavigationEntry* entry = | 468 content::NavigationEntry* entry = |
336 web_contents()->GetController().GetLastCommittedEntry(); | 469 web_contents()->GetController().GetLastCommittedEntry(); |
337 if (!entry || | 470 if (!entry || |
338 host != net::GetHostOrSpecFromURL( | 471 host != net::GetHostOrSpecFromURL( |
339 content::HostZoomMap::GetURLFromEntry(entry))) { | 472 content::HostZoomMap::GetURLFromEntry(entry))) { |
(...skipping 29 matching lines...) Expand all Loading... |
369 int render_view_id = web_contents()->GetRenderViewHost()->GetRoutingID(); | 502 int render_view_id = web_contents()->GetRenderViewHost()->GetRoutingID(); |
370 host_zoom_map_->SetPageScaleFactorIsOneForView(render_process_id, | 503 host_zoom_map_->SetPageScaleFactorIsOneForView(render_process_id, |
371 render_view_id, is_one); | 504 render_view_id, is_one); |
372 } | 505 } |
373 | 506 |
374 bool ZoomController::PageScaleFactorIsOne() const { | 507 bool ZoomController::PageScaleFactorIsOne() const { |
375 return content::HostZoomMap::PageScaleFactorIsOne(web_contents()); | 508 return content::HostZoomMap::PageScaleFactorIsOne(web_contents()); |
376 } | 509 } |
377 | 510 |
378 } // namespace zoom | 511 } // namespace zoom |
OLD | NEW |