| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 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 "chrome/browser/sessions/tab_restore_service_helper.h" | 5 #include "chrome/browser/sessions/tab_restore_service_helper.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <iterator> | 8 #include <iterator> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
| 12 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
| 13 #include "chrome/browser/sessions/tab_restore_service_delegate.h" | 13 #include "chrome/browser/sessions/tab_restore_service_delegate.h" |
| 14 #include "chrome/browser/sessions/tab_restore_service_observer.h" | 14 #include "chrome/browser/sessions/tab_restore_service_observer.h" |
| 15 #include "components/sessions/content/content_serialized_navigation_builder.h" | 15 #include "components/sessions/content/content_serialized_navigation_builder.h" |
| 16 #include "components/sessions/core/tab_restore_service_client.h" | 16 #include "components/sessions/core/tab_restore_service_client.h" |
| 17 #include "components/sessions/session_types.h" | 17 #include "components/sessions/session_types.h" |
| 18 #include "content/public/browser/navigation_controller.h" | 18 #include "content/public/browser/navigation_controller.h" |
| 19 #include "content/public/browser/navigation_entry.h" | 19 #include "content/public/browser/navigation_entry.h" |
| 20 #include "content/public/browser/session_storage_namespace.h" | |
| 21 #include "content/public/browser/web_contents.h" | 20 #include "content/public/browser/web_contents.h" |
| 22 | 21 |
| 23 using content::NavigationController; | 22 using content::NavigationController; |
| 24 using content::NavigationEntry; | 23 using content::NavigationEntry; |
| 25 using content::WebContents; | 24 using content::WebContents; |
| 26 | 25 |
| 27 // TabRestoreServiceHelper::Observer ------------------------------------------- | 26 // TabRestoreServiceHelper::Observer ------------------------------------------- |
| 28 | 27 |
| 29 TabRestoreServiceHelper::Observer::~Observer() {} | 28 TabRestoreServiceHelper::Observer::~Observer() {} |
| 30 | 29 |
| (...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 210 | 209 |
| 211 // When restoring a window, either the entire window can be restored, or a | 210 // When restoring a window, either the entire window can be restored, or a |
| 212 // single tab within it. If the entry's ID matches the one to restore, then | 211 // single tab within it. If the entry's ID matches the one to restore, then |
| 213 // the entire window will be restored. | 212 // the entire window will be restored. |
| 214 if (!restoring_tab_in_window) { | 213 if (!restoring_tab_in_window) { |
| 215 delegate = client_->CreateTabRestoreServiceDelegate(host_desktop_type, | 214 delegate = client_->CreateTabRestoreServiceDelegate(host_desktop_type, |
| 216 window->app_name); | 215 window->app_name); |
| 217 for (size_t tab_i = 0; tab_i < window->tabs.size(); ++tab_i) { | 216 for (size_t tab_i = 0; tab_i < window->tabs.size(); ++tab_i) { |
| 218 const Tab& tab = window->tabs[tab_i]; | 217 const Tab& tab = window->tabs[tab_i]; |
| 219 WebContents* restored_tab = delegate->AddRestoredTab( | 218 WebContents* restored_tab = delegate->AddRestoredTab( |
| 220 tab.navigations, | 219 tab.navigations, delegate->GetTabCount(), |
| 221 delegate->GetTabCount(), | 220 tab.current_navigation_index, tab.extension_app_id, |
| 222 tab.current_navigation_index, | 221 static_cast<int>(tab_i) == window->selected_tab_index, tab.pinned, |
| 223 tab.extension_app_id, | 222 tab.from_last_session, tab.client_data.get(), |
| 224 static_cast<int>(tab_i) == window->selected_tab_index, | |
| 225 tab.pinned, | |
| 226 tab.from_last_session, | |
| 227 tab.session_storage_namespace.get(), | |
| 228 tab.user_agent_override); | 223 tab.user_agent_override); |
| 229 if (restored_tab) { | 224 if (restored_tab) { |
| 230 restored_tab->GetController().LoadIfNecessary(); | 225 restored_tab->GetController().LoadIfNecessary(); |
| 231 client_->OnTabRestored( | 226 client_->OnTabRestored( |
| 232 tab.navigations.at(tab.current_navigation_index).virtual_url()); | 227 tab.navigations.at(tab.current_navigation_index).virtual_url()); |
| 233 web_contents.push_back(restored_tab); | 228 web_contents.push_back(restored_tab); |
| 234 } | 229 } |
| 235 } | 230 } |
| 236 // All the window's tabs had the same former browser_id. | 231 // All the window's tabs had the same former browser_id. |
| 237 if (window->tabs[0].has_browser()) { | 232 if (window->tabs[0].has_browser()) { |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 394 if (tab->current_navigation_index == -1 && entry_count > 0) | 389 if (tab->current_navigation_index == -1 && entry_count > 0) |
| 395 tab->current_navigation_index = 0; | 390 tab->current_navigation_index = 0; |
| 396 tab->tabstrip_index = index; | 391 tab->tabstrip_index = index; |
| 397 | 392 |
| 398 tab->extension_app_id = | 393 tab->extension_app_id = |
| 399 client_->GetExtensionAppIDForWebContents(controller->GetWebContents()); | 394 client_->GetExtensionAppIDForWebContents(controller->GetWebContents()); |
| 400 | 395 |
| 401 tab->user_agent_override = | 396 tab->user_agent_override = |
| 402 controller->GetWebContents()->GetUserAgentOverride(); | 397 controller->GetWebContents()->GetUserAgentOverride(); |
| 403 | 398 |
| 404 // TODO(ajwong): This does not correctly handle storage for isolated apps. | 399 tab->client_data = |
| 405 tab->session_storage_namespace = | 400 client_->GetTabClientDataForWebContents(controller->GetWebContents()); |
| 406 controller->GetDefaultSessionStorageNamespace(); | |
| 407 | 401 |
| 408 // Delegate may be NULL during unit tests. | 402 // Delegate may be NULL during unit tests. |
| 409 if (delegate) { | 403 if (delegate) { |
| 410 tab->browser_id = delegate->GetSessionID().id(); | 404 tab->browser_id = delegate->GetSessionID().id(); |
| 411 tab->pinned = delegate->IsTabPinned(tab->tabstrip_index); | 405 tab->pinned = delegate->IsTabPinned(tab->tabstrip_index); |
| 412 } | 406 } |
| 413 } | 407 } |
| 414 | 408 |
| 415 TabRestoreServiceDelegate* TabRestoreServiceHelper::RestoreTab( | 409 TabRestoreServiceDelegate* TabRestoreServiceHelper::RestoreTab( |
| 416 const Tab& tab, | 410 const Tab& tab, |
| 417 TabRestoreServiceDelegate* delegate, | 411 TabRestoreServiceDelegate* delegate, |
| 418 int host_desktop_type, | 412 int host_desktop_type, |
| 419 WindowOpenDisposition disposition, | 413 WindowOpenDisposition disposition, |
| 420 WebContents** contents) { | 414 WebContents** contents) { |
| 421 WebContents* web_contents; | 415 WebContents* web_contents; |
| 422 if (disposition == CURRENT_TAB && delegate) { | 416 if (disposition == CURRENT_TAB && delegate) { |
| 423 web_contents = delegate->ReplaceRestoredTab( | 417 web_contents = delegate->ReplaceRestoredTab( |
| 424 tab.navigations, | 418 tab.navigations, tab.current_navigation_index, tab.from_last_session, |
| 425 tab.current_navigation_index, | 419 tab.extension_app_id, tab.client_data.get(), tab.user_agent_override); |
| 426 tab.from_last_session, | |
| 427 tab.extension_app_id, | |
| 428 tab.session_storage_namespace.get(), | |
| 429 tab.user_agent_override); | |
| 430 } else { | 420 } else { |
| 431 // We only respsect the tab's original browser if there's no disposition. | 421 // We only respsect the tab's original browser if there's no disposition. |
| 432 if (disposition == UNKNOWN && tab.has_browser()) { | 422 if (disposition == UNKNOWN && tab.has_browser()) { |
| 433 delegate = client_->FindTabRestoreServiceDelegateWithID( | 423 delegate = client_->FindTabRestoreServiceDelegateWithID( |
| 434 tab.browser_id, host_desktop_type); | 424 tab.browser_id, host_desktop_type); |
| 435 } | 425 } |
| 436 | 426 |
| 437 int tab_index = -1; | 427 int tab_index = -1; |
| 438 | 428 |
| 439 // |delegate| will be NULL in cases where one isn't already available (eg, | 429 // |delegate| will be NULL in cases where one isn't already available (eg, |
| 440 // when invoked on Mac OS X with no windows open). In this case, create a | 430 // when invoked on Mac OS X with no windows open). In this case, create a |
| 441 // new browser into which we restore the tabs. | 431 // new browser into which we restore the tabs. |
| 442 if (delegate && disposition != NEW_WINDOW) { | 432 if (delegate && disposition != NEW_WINDOW) { |
| 443 tab_index = tab.tabstrip_index; | 433 tab_index = tab.tabstrip_index; |
| 444 } else { | 434 } else { |
| 445 delegate = client_->CreateTabRestoreServiceDelegate(host_desktop_type, | 435 delegate = client_->CreateTabRestoreServiceDelegate(host_desktop_type, |
| 446 std::string()); | 436 std::string()); |
| 447 if (tab.has_browser()) | 437 if (tab.has_browser()) |
| 448 UpdateTabBrowserIDs(tab.browser_id, delegate->GetSessionID().id()); | 438 UpdateTabBrowserIDs(tab.browser_id, delegate->GetSessionID().id()); |
| 449 } | 439 } |
| 450 | 440 |
| 451 // Place the tab at the end if the tab index is no longer valid or | 441 // Place the tab at the end if the tab index is no longer valid or |
| 452 // we were passed a specific disposition. | 442 // we were passed a specific disposition. |
| 453 if (tab_index < 0 || tab_index > delegate->GetTabCount() || | 443 if (tab_index < 0 || tab_index > delegate->GetTabCount() || |
| 454 disposition != UNKNOWN) { | 444 disposition != UNKNOWN) { |
| 455 tab_index = delegate->GetTabCount(); | 445 tab_index = delegate->GetTabCount(); |
| 456 } | 446 } |
| 457 | 447 |
| 458 web_contents = delegate->AddRestoredTab(tab.navigations, | 448 web_contents = delegate->AddRestoredTab( |
| 459 tab_index, | 449 tab.navigations, tab_index, tab.current_navigation_index, |
| 460 tab.current_navigation_index, | 450 tab.extension_app_id, disposition != NEW_BACKGROUND_TAB, tab.pinned, |
| 461 tab.extension_app_id, | 451 tab.from_last_session, tab.client_data.get(), tab.user_agent_override); |
| 462 disposition != NEW_BACKGROUND_TAB, | |
| 463 tab.pinned, | |
| 464 tab.from_last_session, | |
| 465 tab.session_storage_namespace.get(), | |
| 466 tab.user_agent_override); | |
| 467 web_contents->GetController().LoadIfNecessary(); | 452 web_contents->GetController().LoadIfNecessary(); |
| 468 } | 453 } |
| 469 client_->OnTabRestored( | 454 client_->OnTabRestored( |
| 470 tab.navigations.at(tab.current_navigation_index).virtual_url()); | 455 tab.navigations.at(tab.current_navigation_index).virtual_url()); |
| 471 if (contents) | 456 if (contents) |
| 472 *contents = web_contents; | 457 *contents = web_contents; |
| 473 | 458 |
| 474 return delegate; | 459 return delegate; |
| 475 } | 460 } |
| 476 | 461 |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 554 Tab* tab = static_cast<Tab*>(entry); | 539 Tab* tab = static_cast<Tab*>(entry); |
| 555 if (tab->browser_id == old_id) | 540 if (tab->browser_id == old_id) |
| 556 tab->browser_id = new_id; | 541 tab->browser_id = new_id; |
| 557 } | 542 } |
| 558 } | 543 } |
| 559 } | 544 } |
| 560 | 545 |
| 561 base::Time TabRestoreServiceHelper::TimeNow() const { | 546 base::Time TabRestoreServiceHelper::TimeNow() const { |
| 562 return time_factory_ ? time_factory_->TimeNow() : base::Time::Now(); | 547 return time_factory_ ? time_factory_->TimeNow() : base::Time::Now(); |
| 563 } | 548 } |
| OLD | NEW |