Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(142)

Side by Side Diff: chrome/browser/sessions/session_restore.cc

Issue 2644663003: Offer to open the startup pages after a crash. (Closed)
Patch Set: Typo fix Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 216 matching lines...) Expand 10 before | Expand all | Expand 10 after
260 return; 261 return;
261 262
262 default: 263 default:
263 NOTREACHED(); 264 NOTREACHED();
264 break; 265 break;
265 } 266 }
266 } 267 }
267 268
268 Profile* profile() { return profile_; } 269 Profile* profile() { return profile_; }
269 270
271 // Appends the urls in |urls| to |browser|.
272 static void AppendURLsToBrowser(Browser* browser,
Georges Khalil 2017/01/24 17:18:29 nit: I would simply move this outside of the class
MAD 2017/01/24 18:24:18 Done.
273 const std::vector<GURL>& urls) {
274 for (size_t i = 0; i < urls.size(); ++i) {
275 int add_types = TabStripModel::ADD_FORCE_INDEX;
276 if (i == 0)
277 add_types |= TabStripModel::ADD_ACTIVE;
278 chrome::NavigateParams params(browser, urls[i],
279 ui::PAGE_TRANSITION_AUTO_TOPLEVEL);
280 params.disposition = i == 0 ? WindowOpenDisposition::NEW_FOREGROUND_TAB
281 : WindowOpenDisposition::NEW_BACKGROUND_TAB;
282 params.tabstrip_add_types = add_types;
283 chrome::Navigate(&params);
284 }
285 }
286
270 private: 287 private:
271 // Invoked when done with creating all the tabs/browsers. 288 // Invoked when done with creating all the tabs/browsers.
272 // 289 //
273 // |created_tabbed_browser| indicates whether a tabbed browser was created, 290 // |created_tabbed_browser| indicates whether a tabbed browser was created,
274 // or we used an existing tabbed browser. 291 // or we used an existing tabbed browser.
275 // 292 //
276 // If successful, this begins loading tabs and deletes itself when all tabs 293 // If successful, this begins loading tabs and deletes itself when all tabs
277 // have been loaded. 294 // have been loaded.
278 // 295 //
279 // Returns the Browser that was created, if any. 296 // Returns the Browser that was created, if any.
(...skipping 362 matching lines...) Expand 10 before | Expand all | Expand 10 after
642 return; 659 return;
643 660
644 browser->window()->Show(); 661 browser->window()->Show();
645 browser->set_is_session_restore(false); 662 browser->set_is_session_restore(false);
646 663
647 // TODO(jcampan): http://crbug.com/8123 we should not need to set the 664 // TODO(jcampan): http://crbug.com/8123 we should not need to set the
648 // initial focus explicitly. 665 // initial focus explicitly.
649 browser->tab_strip_model()->GetActiveWebContents()->SetInitialFocus(); 666 browser->tab_strip_model()->GetActiveWebContents()->SetInitialFocus();
650 } 667 }
651 668
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(&params);
664 }
665 }
666
667 // Invokes TabRestored on the SessionService for all tabs in browser after 669 // Invokes TabRestored on the SessionService for all tabs in browser after
668 // initial_count. 670 // initial_count.
669 void NotifySessionServiceOfRestoredTabs(Browser* browser, int initial_count) { 671 void NotifySessionServiceOfRestoredTabs(Browser* browser, int initial_count) {
670 SessionService* session_service = 672 SessionService* session_service =
671 SessionServiceFactory::GetForProfile(profile_); 673 SessionServiceFactory::GetForProfile(profile_);
672 if (!session_service) 674 if (!session_service)
673 return; 675 return;
674 TabStripModel* tab_strip = browser->tab_strip_model(); 676 TabStripModel* tab_strip = browser->tab_strip_model();
675 for (int i = initial_count; i < tab_strip->count(); ++i) 677 for (int i = initial_count; i < tab_strip->count(); ++i)
676 session_service->TabRestored(tab_strip->GetWebContentsAt(i), 678 session_service->TabRestored(tab_strip->GetWebContentsAt(i),
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
723 725
724 // The time we started the restore. 726 // The time we started the restore.
725 base::TimeTicks restore_started_; 727 base::TimeTicks restore_started_;
726 728
727 // List of callbacks for session restore notification. 729 // List of callbacks for session restore notification.
728 SessionRestore::CallbackList* on_session_restored_callbacks_; 730 SessionRestore::CallbackList* on_session_restored_callbacks_;
729 731
730 DISALLOW_COPY_AND_ASSIGN(SessionRestoreImpl); 732 DISALLOW_COPY_AND_ASSIGN(SessionRestoreImpl);
731 }; 733 };
732 734
735 bool HasSingleNewTabPage(Browser* browser) {
736 if (browser->tab_strip_model()->count() == 1) {
Georges Khalil 2017/01/24 17:18:29 nit: I would invert logic to make this slightly cl
MAD 2017/01/24 18:24:18 Done.
737 const content::WebContents* active_tab =
738 browser->tab_strip_model()->GetWebContentsAt(0);
739 return active_tab->GetURL() == chrome::kChromeUINewTabURL ||
740 search::IsInstantNTP(active_tab);
741 }
742 return false;
743 }
744
733 } // namespace 745 } // namespace
734 746
735 // SessionRestore ------------------------------------------------------------- 747 // SessionRestore -------------------------------------------------------------
736 748
737 // static 749 // static
738 Browser* SessionRestore::RestoreSession( 750 Browser* SessionRestore::RestoreSession(
739 Profile* profile, 751 Profile* profile,
740 Browser* browser, 752 Browser* browser,
741 uint32_t behavior, 753 uint32_t behavior,
742 const std::vector<GURL>& urls_to_open) { 754 const std::vector<GURL>& urls_to_open) {
(...skipping 14 matching lines...) Expand all
757 SessionRestoreImpl* restorer = new SessionRestoreImpl( 769 SessionRestoreImpl* restorer = new SessionRestoreImpl(
758 profile, browser, (behavior & SYNCHRONOUS) != 0, 770 profile, browser, (behavior & SYNCHRONOUS) != 0,
759 (behavior & CLOBBER_CURRENT_TAB) != 0, 771 (behavior & CLOBBER_CURRENT_TAB) != 0,
760 (behavior & ALWAYS_CREATE_TABBED_BROWSER) != 0, urls_to_open, 772 (behavior & ALWAYS_CREATE_TABBED_BROWSER) != 0, urls_to_open,
761 SessionRestore::on_session_restored_callbacks()); 773 SessionRestore::on_session_restored_callbacks());
762 return restorer->Restore(); 774 return restorer->Restore();
763 } 775 }
764 776
765 // static 777 // static
766 void SessionRestore::RestoreSessionAfterCrash(Browser* browser) { 778 void SessionRestore::RestoreSessionAfterCrash(Browser* browser) {
767 uint32_t behavior = 0; 779 uint32_t behavior =
768 if (browser->tab_strip_model()->count() == 1) { 780 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, 781 SessionRestore::RestoreSession(browser->profile(), browser, behavior,
779 std::vector<GURL>()); 782 std::vector<GURL>());
780 } 783 }
781 784
782 // static 785 // static
786 void SessionRestore::OpenStartupPagesAfterCrash(Browser* browser) {
787 WebContents* tab_to_clobber = nullptr;
788 if (HasSingleNewTabPage(browser))
789 tab_to_clobber = browser->tab_strip_model()->GetActiveWebContents();
790
791 SessionRestoreImpl::AppendURLsToBrowser(
792 browser, SessionStartupPref::GetStartupPref(browser->profile()).urls);
793 if (tab_to_clobber)
794 chrome::CloseWebContents(browser, tab_to_clobber, true);
795 }
796
797 // static
783 std::vector<Browser*> SessionRestore::RestoreForeignSessionWindows( 798 std::vector<Browser*> SessionRestore::RestoreForeignSessionWindows(
784 Profile* profile, 799 Profile* profile,
785 std::vector<const sessions::SessionWindow*>::const_iterator begin, 800 std::vector<const sessions::SessionWindow*>::const_iterator begin,
786 std::vector<const sessions::SessionWindow*>::const_iterator end) { 801 std::vector<const sessions::SessionWindow*>::const_iterator end) {
787 std::vector<GURL> gurls; 802 std::vector<GURL> gurls;
788 SessionRestoreImpl restorer(profile, static_cast<Browser*>(nullptr), true, 803 SessionRestoreImpl restorer(profile, static_cast<Browser*>(nullptr), true,
789 false, true, gurls, 804 false, true, gurls,
790 on_session_restored_callbacks()); 805 on_session_restored_callbacks());
791 return restorer.RestoreForeignSession(begin, end); 806 return restorer.RestoreForeignSession(begin, end);
792 } 807 }
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
833 // static 848 // static
834 SessionRestore::CallbackSubscription 849 SessionRestore::CallbackSubscription
835 SessionRestore::RegisterOnSessionRestoredCallback( 850 SessionRestore::RegisterOnSessionRestoredCallback(
836 const base::Callback<void(int)>& callback) { 851 const base::Callback<void(int)>& callback) {
837 return on_session_restored_callbacks()->Add(callback); 852 return on_session_restored_callbacks()->Add(callback);
838 } 853 }
839 854
840 // static 855 // static
841 base::CallbackList<void(int)>* 856 base::CallbackList<void(int)>*
842 SessionRestore::on_session_restored_callbacks_ = nullptr; 857 SessionRestore::on_session_restored_callbacks_ = nullptr;
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698