Chromium Code Reviews| 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/session_restore.h" | 5 #include "chrome/browser/sessions/session_restore.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <list> | 10 #include <list> |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 24 #include "base/metrics/histogram_macros.h" | 24 #include "base/metrics/histogram_macros.h" |
| 25 #include "base/run_loop.h" | 25 #include "base/run_loop.h" |
| 26 #include "base/single_thread_task_runner.h" | 26 #include "base/single_thread_task_runner.h" |
| 27 #include "base/task/cancelable_task_tracker.h" | 27 #include "base/task/cancelable_task_tracker.h" |
| 28 #include "base/threading/thread_task_runner_handle.h" | 28 #include "base/threading/thread_task_runner_handle.h" |
| 29 #include "build/build_config.h" | 29 #include "build/build_config.h" |
| 30 #include "chrome/browser/browser_process.h" | 30 #include "chrome/browser/browser_process.h" |
| 31 #include "chrome/browser/chrome_notification_types.h" | 31 #include "chrome/browser/chrome_notification_types.h" |
| 32 #include "chrome/browser/lifetime/keep_alive_types.h" | 32 #include "chrome/browser/lifetime/keep_alive_types.h" |
| 33 #include "chrome/browser/lifetime/scoped_keep_alive.h" | 33 #include "chrome/browser/lifetime/scoped_keep_alive.h" |
| 34 #include "chrome/browser/prefs/session_startup_pref.h" | |
| 34 #include "chrome/browser/profiles/profile.h" | 35 #include "chrome/browser/profiles/profile.h" |
| 35 #include "chrome/browser/search/search.h" | 36 #include "chrome/browser/search/search.h" |
| 36 #include "chrome/browser/sessions/session_restore_delegate.h" | 37 #include "chrome/browser/sessions/session_restore_delegate.h" |
| 37 #include "chrome/browser/sessions/session_service.h" | 38 #include "chrome/browser/sessions/session_service.h" |
| 38 #include "chrome/browser/sessions/session_service_factory.h" | 39 #include "chrome/browser/sessions/session_service_factory.h" |
| 39 #include "chrome/browser/sessions/session_service_utils.h" | 40 #include "chrome/browser/sessions/session_service_utils.h" |
| 40 #include "chrome/browser/sessions/tab_loader.h" | 41 #include "chrome/browser/sessions/tab_loader.h" |
| 41 #include "chrome/browser/ui/browser.h" | 42 #include "chrome/browser/ui/browser.h" |
| 42 #include "chrome/browser/ui/browser_finder.h" | 43 #include "chrome/browser/ui/browser_finder.h" |
| 43 #include "chrome/browser/ui/browser_navigator.h" | 44 #include "chrome/browser/ui/browser_navigator.h" |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 68 #include "chrome/browser/chromeos/boot_times_recorder.h" | 69 #include "chrome/browser/chromeos/boot_times_recorder.h" |
| 69 #endif | 70 #endif |
| 70 | 71 |
| 71 using content::NavigationController; | 72 using content::NavigationController; |
| 72 using content::RenderWidgetHost; | 73 using content::RenderWidgetHost; |
| 73 using content::WebContents; | 74 using content::WebContents; |
| 74 using RestoredTab = SessionRestoreDelegate::RestoredTab; | 75 using RestoredTab = SessionRestoreDelegate::RestoredTab; |
| 75 | 76 |
| 76 namespace { | 77 namespace { |
| 77 | 78 |
| 79 // Appends the urls in |urls| to |browser|. | |
| 80 void AppendURLsToBrowser(Browser* browser, const std::vector<GURL>& urls) { | |
| 81 for (size_t i = 0; i < urls.size(); ++i) { | |
| 82 int add_types = TabStripModel::ADD_FORCE_INDEX; | |
| 83 if (i == 0) | |
| 84 add_types |= TabStripModel::ADD_ACTIVE; | |
| 85 chrome::NavigateParams params(browser, urls[i], | |
| 86 ui::PAGE_TRANSITION_AUTO_TOPLEVEL); | |
| 87 params.disposition = i == 0 ? WindowOpenDisposition::NEW_FOREGROUND_TAB | |
| 88 : WindowOpenDisposition::NEW_BACKGROUND_TAB; | |
| 89 params.tabstrip_add_types = add_types; | |
| 90 chrome::Navigate(¶ms); | |
| 91 } | |
| 92 } | |
| 93 | |
| 94 bool HasSingleNewTabPage(Browser* browser) { | |
| 95 if (browser->tab_strip_model()->count() != 1) | |
| 96 return false; | |
| 97 const content::WebContents* active_tab = | |
| 98 browser->tab_strip_model()->GetWebContentsAt(0); | |
| 99 return active_tab->GetURL() == chrome::kChromeUINewTabURL || | |
| 100 search::IsInstantNTP(active_tab); | |
| 101 } | |
| 102 | |
| 78 class SessionRestoreImpl; | 103 class SessionRestoreImpl; |
| 79 | 104 |
| 80 // Pointers to SessionRestoreImpls which are currently restoring the session. | 105 // Pointers to SessionRestoreImpls which are currently restoring the session. |
| 81 std::set<SessionRestoreImpl*>* active_session_restorers = nullptr; | 106 std::set<SessionRestoreImpl*>* active_session_restorers = nullptr; |
| 82 | 107 |
| 83 // SessionRestoreImpl --------------------------------------------------------- | 108 // SessionRestoreImpl --------------------------------------------------------- |
| 84 | 109 |
| 85 // SessionRestoreImpl is responsible for fetching the set of tabs to create | 110 // SessionRestoreImpl is responsible for fetching the set of tabs to create |
| 86 // from SessionService. SessionRestoreImpl deletes itself when done. | 111 // from SessionService. SessionRestoreImpl deletes itself when done. |
| 87 | 112 |
| (...skipping 554 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 642 return; | 667 return; |
| 643 | 668 |
| 644 browser->window()->Show(); | 669 browser->window()->Show(); |
| 645 browser->set_is_session_restore(false); | 670 browser->set_is_session_restore(false); |
| 646 | 671 |
| 647 // TODO(jcampan): http://crbug.com/8123 we should not need to set the | 672 // TODO(jcampan): http://crbug.com/8123 we should not need to set the |
| 648 // initial focus explicitly. | 673 // initial focus explicitly. |
| 649 browser->tab_strip_model()->GetActiveWebContents()->SetInitialFocus(); | 674 browser->tab_strip_model()->GetActiveWebContents()->SetInitialFocus(); |
| 650 } | 675 } |
| 651 | 676 |
| 652 // Appends the urls in |urls| to |browser|. | |
| 653 void AppendURLsToBrowser(Browser* browser, const std::vector<GURL>& urls) { | |
| 654 for (size_t i = 0; i < urls.size(); ++i) { | |
| 655 int add_types = TabStripModel::ADD_FORCE_INDEX; | |
| 656 if (i == 0) | |
| 657 add_types |= TabStripModel::ADD_ACTIVE; | |
| 658 chrome::NavigateParams params(browser, urls[i], | |
| 659 ui::PAGE_TRANSITION_AUTO_TOPLEVEL); | |
| 660 params.disposition = i == 0 ? WindowOpenDisposition::NEW_FOREGROUND_TAB | |
| 661 : WindowOpenDisposition::NEW_BACKGROUND_TAB; | |
| 662 params.tabstrip_add_types = add_types; | |
| 663 chrome::Navigate(¶ms); | |
| 664 } | |
| 665 } | |
| 666 | |
| 667 // Invokes TabRestored on the SessionService for all tabs in browser after | 677 // Invokes TabRestored on the SessionService for all tabs in browser after |
| 668 // initial_count. | 678 // initial_count. |
| 669 void NotifySessionServiceOfRestoredTabs(Browser* browser, int initial_count) { | 679 void NotifySessionServiceOfRestoredTabs(Browser* browser, int initial_count) { |
| 670 SessionService* session_service = | 680 SessionService* session_service = |
| 671 SessionServiceFactory::GetForProfile(profile_); | 681 SessionServiceFactory::GetForProfile(profile_); |
| 672 if (!session_service) | 682 if (!session_service) |
| 673 return; | 683 return; |
| 674 TabStripModel* tab_strip = browser->tab_strip_model(); | 684 TabStripModel* tab_strip = browser->tab_strip_model(); |
| 675 for (int i = initial_count; i < tab_strip->count(); ++i) | 685 for (int i = initial_count; i < tab_strip->count(); ++i) |
| 676 session_service->TabRestored(tab_strip->GetWebContentsAt(i), | 686 session_service->TabRestored(tab_strip->GetWebContentsAt(i), |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 757 SessionRestoreImpl* restorer = new SessionRestoreImpl( | 767 SessionRestoreImpl* restorer = new SessionRestoreImpl( |
| 758 profile, browser, (behavior & SYNCHRONOUS) != 0, | 768 profile, browser, (behavior & SYNCHRONOUS) != 0, |
| 759 (behavior & CLOBBER_CURRENT_TAB) != 0, | 769 (behavior & CLOBBER_CURRENT_TAB) != 0, |
| 760 (behavior & ALWAYS_CREATE_TABBED_BROWSER) != 0, urls_to_open, | 770 (behavior & ALWAYS_CREATE_TABBED_BROWSER) != 0, urls_to_open, |
| 761 SessionRestore::on_session_restored_callbacks()); | 771 SessionRestore::on_session_restored_callbacks()); |
| 762 return restorer->Restore(); | 772 return restorer->Restore(); |
| 763 } | 773 } |
| 764 | 774 |
| 765 // static | 775 // static |
| 766 void SessionRestore::RestoreSessionAfterCrash(Browser* browser) { | 776 void SessionRestore::RestoreSessionAfterCrash(Browser* browser) { |
| 767 uint32_t behavior = 0; | 777 uint32_t behavior = |
| 768 if (browser->tab_strip_model()->count() == 1) { | 778 HasSingleNewTabPage(browser) ? SessionRestore::CLOBBER_CURRENT_TAB : 0; |
| 769 const content::WebContents* active_tab = | |
| 770 browser->tab_strip_model()->GetWebContentsAt(0); | |
| 771 if (active_tab->GetURL() == chrome::kChromeUINewTabURL || | |
| 772 search::IsInstantNTP(active_tab)) { | |
| 773 // There is only one tab and its the new tab page, make session restore | |
| 774 // clobber it. | |
| 775 behavior = SessionRestore::CLOBBER_CURRENT_TAB; | |
| 776 } | |
| 777 } | |
| 778 SessionRestore::RestoreSession(browser->profile(), browser, behavior, | 779 SessionRestore::RestoreSession(browser->profile(), browser, behavior, |
| 779 std::vector<GURL>()); | 780 std::vector<GURL>()); |
| 780 } | 781 } |
| 781 | 782 |
| 782 // static | 783 // static |
| 784 void SessionRestore::OpenStartupPagesAfterCrash(Browser* browser) { | |
| 785 WebContents* tab_to_clobber = nullptr; | |
| 786 if (HasSingleNewTabPage(browser)) | |
| 787 tab_to_clobber = browser->tab_strip_model()->GetActiveWebContents(); | |
| 788 | |
| 789 AppendURLsToBrowser( | |
|
sky
2017/01/24 23:00:39
I'm mildly concerned this isn't the same path we u
MAD
2017/01/25 19:48:53
I saw that, but I thought it would be too much tro
| |
| 790 browser, SessionStartupPref::GetStartupPref(browser->profile()).urls); | |
| 791 if (tab_to_clobber) | |
|
sky
2017/01/24 23:00:39
You should make sure a tab was actually added.
MAD
2017/01/25 19:48:53
Done.
| |
| 792 chrome::CloseWebContents(browser, tab_to_clobber, true); | |
| 793 } | |
| 794 | |
| 795 // static | |
| 783 std::vector<Browser*> SessionRestore::RestoreForeignSessionWindows( | 796 std::vector<Browser*> SessionRestore::RestoreForeignSessionWindows( |
| 784 Profile* profile, | 797 Profile* profile, |
| 785 std::vector<const sessions::SessionWindow*>::const_iterator begin, | 798 std::vector<const sessions::SessionWindow*>::const_iterator begin, |
| 786 std::vector<const sessions::SessionWindow*>::const_iterator end) { | 799 std::vector<const sessions::SessionWindow*>::const_iterator end) { |
| 787 std::vector<GURL> gurls; | 800 std::vector<GURL> gurls; |
| 788 SessionRestoreImpl restorer(profile, static_cast<Browser*>(nullptr), true, | 801 SessionRestoreImpl restorer(profile, static_cast<Browser*>(nullptr), true, |
| 789 false, true, gurls, | 802 false, true, gurls, |
| 790 on_session_restored_callbacks()); | 803 on_session_restored_callbacks()); |
| 791 return restorer.RestoreForeignSession(begin, end); | 804 return restorer.RestoreForeignSession(begin, end); |
| 792 } | 805 } |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 833 // static | 846 // static |
| 834 SessionRestore::CallbackSubscription | 847 SessionRestore::CallbackSubscription |
| 835 SessionRestore::RegisterOnSessionRestoredCallback( | 848 SessionRestore::RegisterOnSessionRestoredCallback( |
| 836 const base::Callback<void(int)>& callback) { | 849 const base::Callback<void(int)>& callback) { |
| 837 return on_session_restored_callbacks()->Add(callback); | 850 return on_session_restored_callbacks()->Add(callback); |
| 838 } | 851 } |
| 839 | 852 |
| 840 // static | 853 // static |
| 841 base::CallbackList<void(int)>* | 854 base::CallbackList<void(int)>* |
| 842 SessionRestore::on_session_restored_callbacks_ = nullptr; | 855 SessionRestore::on_session_restored_callbacks_ = nullptr; |
| OLD | NEW |