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

Side by Side Diff: chrome/browser/ui/startup/startup_browser_creator_impl.cc

Issue 2469363002: Tech Debt Repayment for StartupBrowserCreatorImpl Refactor (Closed)
Patch Set: Session restore formatting Created 3 years, 10 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 (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 "chrome/browser/ui/startup/startup_browser_creator_impl.h" 5 #include "chrome/browser/ui/startup/startup_browser_creator_impl.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <algorithm> 10 #include <algorithm>
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
42 #include "chrome/browser/extensions/launch_util.h" 42 #include "chrome/browser/extensions/launch_util.h"
43 #include "chrome/browser/extensions/pack_extension_job.h" 43 #include "chrome/browser/extensions/pack_extension_job.h"
44 #include "chrome/browser/first_run/first_run.h" 44 #include "chrome/browser/first_run/first_run.h"
45 #include "chrome/browser/infobars/infobar_service.h" 45 #include "chrome/browser/infobars/infobar_service.h"
46 #include "chrome/browser/prefs/incognito_mode_prefs.h" 46 #include "chrome/browser/prefs/incognito_mode_prefs.h"
47 #include "chrome/browser/prefs/session_startup_pref.h" 47 #include "chrome/browser/prefs/session_startup_pref.h"
48 #include "chrome/browser/profile_resetter/triggered_profile_resetter.h" 48 #include "chrome/browser/profile_resetter/triggered_profile_resetter.h"
49 #include "chrome/browser/profile_resetter/triggered_profile_resetter_factory.h" 49 #include "chrome/browser/profile_resetter/triggered_profile_resetter_factory.h"
50 #include "chrome/browser/profiles/profile.h" 50 #include "chrome/browser/profiles/profile.h"
51 #include "chrome/browser/profiles/profile_io_data.h" 51 #include "chrome/browser/profiles/profile_io_data.h"
52 #include "chrome/browser/sessions/session_restore.h"
53 #include "chrome/browser/sessions/session_service.h" 52 #include "chrome/browser/sessions/session_service.h"
54 #include "chrome/browser/sessions/session_service_factory.h" 53 #include "chrome/browser/sessions/session_service_factory.h"
55 #include "chrome/browser/shell_integration.h" 54 #include "chrome/browser/shell_integration.h"
56 #include "chrome/browser/signin/signin_promo.h" 55 #include "chrome/browser/signin/signin_promo.h"
57 #include "chrome/browser/ui/app_list/app_list_service.h" 56 #include "chrome/browser/ui/app_list/app_list_service.h"
58 #include "chrome/browser/ui/browser_commands.h" 57 #include "chrome/browser/ui/browser_commands.h"
59 #include "chrome/browser/ui/browser_finder.h" 58 #include "chrome/browser/ui/browser_finder.h"
60 #include "chrome/browser/ui/browser_list.h" 59 #include "chrome/browser/ui/browser_list.h"
61 #include "chrome/browser/ui/browser_navigator.h" 60 #include "chrome/browser/ui/browser_navigator.h"
62 #include "chrome/browser/ui/browser_navigator_params.h" 61 #include "chrome/browser/ui/browser_navigator_params.h"
(...skipping 554 matching lines...) Expand 10 before | Expand all | Expand 10 after
617 void StartupBrowserCreatorImpl::ProcessLaunchUrlsUsingConsolidatedFlow( 616 void StartupBrowserCreatorImpl::ProcessLaunchUrlsUsingConsolidatedFlow(
618 bool process_startup, 617 bool process_startup,
619 const std::vector<GURL>& cmd_line_urls) { 618 const std::vector<GURL>& cmd_line_urls) {
620 // Don't open any browser windows if starting up in "background mode". 619 // Don't open any browser windows if starting up in "background mode".
621 if (process_startup && command_line_.HasSwitch(switches::kNoStartupWindow)) 620 if (process_startup && command_line_.HasSwitch(switches::kNoStartupWindow))
622 return; 621 return;
623 622
624 StartupTabs cmd_line_tabs; 623 StartupTabs cmd_line_tabs;
625 UrlsToTabs(cmd_line_urls, &cmd_line_tabs); 624 UrlsToTabs(cmd_line_urls, &cmd_line_tabs);
626 625
627 bool is_ephemeral_profile = 626 bool is_incognito_or_guest =
628 profile_->GetProfileType() != Profile::ProfileType::REGULAR_PROFILE; 627 profile_->GetProfileType() != Profile::ProfileType::REGULAR_PROFILE;
629 bool is_post_crash_launch = HasPendingUncleanExit(profile_); 628 bool is_post_crash_launch = HasPendingUncleanExit(profile_);
630 StartupTabs tabs = 629 StartupTabs tabs =
631 DetermineStartupTabs(StartupTabProviderImpl(), cmd_line_tabs, 630 DetermineStartupTabs(StartupTabProviderImpl(), cmd_line_tabs,
632 is_ephemeral_profile, is_post_crash_launch); 631 is_incognito_or_guest, is_post_crash_launch);
633 632
634 // Return immediately if we start an async restore, since the remainder of 633 // Return immediately if we start an async restore, since the remainder of
635 // that process is self-contained. 634 // that process is self-contained.
636 if (MaybeAsyncRestore(tabs, process_startup, is_post_crash_launch)) 635 if (MaybeAsyncRestore(tabs, process_startup, is_post_crash_launch))
637 return; 636 return;
638 637
638 BrowserOpenBehaviorOptions behavior_options = 0;
639 if (process_startup)
640 behavior_options |= PROCESS_STARTUP;
641 if (is_post_crash_launch)
642 behavior_options |= IS_POST_CRASH_LAUNCH;
643 if (command_line_.HasSwitch(switches::kRestoreLastSession))
644 behavior_options |= HAS_RESTORE_SWITCH;
645 if (command_line_.HasSwitch(switches::kOpenInNewWindow))
646 behavior_options |= HAS_NEW_WINDOW_SWITCH;
647 if (!cmd_line_tabs.empty())
648 behavior_options |= HAS_CMD_LINE_TABS;
649
639 BrowserOpenBehavior behavior = DetermineBrowserOpenBehavior( 650 BrowserOpenBehavior behavior = DetermineBrowserOpenBehavior(
640 StartupBrowserCreator::GetSessionStartupPref(command_line_, profile_), 651 StartupBrowserCreator::GetSessionStartupPref(command_line_, profile_),
641 process_startup, is_post_crash_launch, 652 behavior_options);
642 command_line_.HasSwitch(switches::kRestoreLastSession),
643 command_line_.HasSwitch(switches::kOpenInNewWindow),
644 !cmd_line_tabs.empty());
645 653
646 uint32_t restore_options = 0; 654 SessionRestore::Behavior restore_options = 0;
647 if (behavior == BrowserOpenBehavior::SYNCHRONOUS_RESTORE) { 655 if (behavior == BrowserOpenBehavior::SYNCHRONOUS_RESTORE) {
648 #if defined(OS_MACOSX) 656 #if defined(OS_MACOSX)
649 bool was_mac_login_or_resume = base::mac::WasLaunchedAsLoginOrResumeItem(); 657 bool was_mac_login_or_resume = base::mac::WasLaunchedAsLoginOrResumeItem();
650 #else 658 #else
651 bool was_mac_login_or_resume = false; 659 bool was_mac_login_or_resume = false;
652 #endif 660 #endif
653 restore_options = DetermineSynchronousRestoreOptions( 661 restore_options = DetermineSynchronousRestoreOptions(
654 browser_defaults::kAlwaysCreateTabbedBrowserOnSessionRestore, 662 browser_defaults::kAlwaysCreateTabbedBrowserOnSessionRestore,
655 base::CommandLine::ForCurrentProcess()->HasSwitch( 663 base::CommandLine::ForCurrentProcess()->HasSwitch(
656 switches::kCreateBrowserOnStartupForTests), 664 switches::kCreateBrowserOnStartupForTests),
657 was_mac_login_or_resume); 665 was_mac_login_or_resume);
658 } 666 }
659 667
660 Browser* browser = RestoreOrCreateBrowser( 668 Browser* browser = RestoreOrCreateBrowser(
661 tabs, behavior, restore_options, process_startup, is_post_crash_launch); 669 tabs, behavior, restore_options, process_startup, is_post_crash_launch);
662 670
663 // Finally, add info bars. 671 // Finally, add info bars.
664 AddInfoBarsIfNecessary( 672 AddInfoBarsIfNecessary(
665 browser, process_startup ? chrome::startup::IS_PROCESS_STARTUP 673 browser, process_startup ? chrome::startup::IS_PROCESS_STARTUP
666 : chrome::startup::IS_NOT_PROCESS_STARTUP); 674 : chrome::startup::IS_NOT_PROCESS_STARTUP);
667 } 675 }
668 676
669 StartupTabs StartupBrowserCreatorImpl::DetermineStartupTabs( 677 StartupTabs StartupBrowserCreatorImpl::DetermineStartupTabs(
670 const StartupTabProvider& provider, 678 const StartupTabProvider& provider,
671 const StartupTabs& cmd_line_tabs, 679 const StartupTabs& cmd_line_tabs,
672 bool is_ephemeral_profile, 680 bool is_incognito_or_guest,
673 bool is_post_crash_launch) { 681 bool is_post_crash_launch) {
674 // Only the New Tab Page or command line URLs may be shown in incognito mode. 682 // Only the New Tab Page or command line URLs may be shown in incognito mode.
675 // A similar policy exists for crash recovery launches, to prevent getting the 683 // A similar policy exists for crash recovery launches, to prevent getting the
676 // user stuck in a crash loop. 684 // user stuck in a crash loop.
677 if (is_ephemeral_profile || is_post_crash_launch) { 685 if (is_incognito_or_guest || is_post_crash_launch) {
678 if (cmd_line_tabs.empty()) 686 if (cmd_line_tabs.empty())
679 return StartupTabs({StartupTab(GURL(chrome::kChromeUINewTabURL), false)}); 687 return StartupTabs({StartupTab(GURL(chrome::kChromeUINewTabURL), false)});
680 return cmd_line_tabs; 688 return cmd_line_tabs;
681 } 689 }
682 690
683 // A trigger on a profile may indicate that we should show a tab which 691 // A trigger on a profile may indicate that we should show a tab which
684 // offers to reset the user's settings. When this appears, it is first, and 692 // offers to reset the user's settings. When this appears, it is first, and
685 // may be shown alongside command-line tabs. 693 // may be shown alongside command-line tabs.
686 StartupTabs tabs = provider.GetResetTriggerTabs(profile_); 694 StartupTabs tabs = provider.GetResetTriggerTabs(profile_);
687 695
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
732 // Note: there's no session service in incognito or guest mode. 740 // Note: there's no session service in incognito or guest mode.
733 SessionService* service = 741 SessionService* service =
734 SessionServiceFactory::GetForProfileForSessionRestore(profile_); 742 SessionServiceFactory::GetForProfileForSessionRestore(profile_);
735 743
736 return service && service->RestoreIfNecessary(TabsToUrls(tabs)); 744 return service && service->RestoreIfNecessary(TabsToUrls(tabs));
737 } 745 }
738 746
739 Browser* StartupBrowserCreatorImpl::RestoreOrCreateBrowser( 747 Browser* StartupBrowserCreatorImpl::RestoreOrCreateBrowser(
740 const StartupTabs& tabs, 748 const StartupTabs& tabs,
741 BrowserOpenBehavior behavior, 749 BrowserOpenBehavior behavior,
742 uint32_t restore_options, 750 SessionRestore::Behavior restore_options,
743 bool process_startup, 751 bool process_startup,
744 bool is_post_crash_launch) { 752 bool is_post_crash_launch) {
745 Browser* browser = nullptr; 753 Browser* browser = nullptr;
746 if (behavior == BrowserOpenBehavior::SYNCHRONOUS_RESTORE) { 754 if (behavior == BrowserOpenBehavior::SYNCHRONOUS_RESTORE) {
747 browser = SessionRestore::RestoreSession(profile_, nullptr, restore_options, 755 browser = SessionRestore::RestoreSession(profile_, nullptr, restore_options,
748 TabsToUrls(tabs)); 756 TabsToUrls(tabs));
749 if (browser) 757 if (browser)
750 return browser; 758 return browser;
751 } else if (behavior == BrowserOpenBehavior::USE_EXISTING) { 759 } else if (behavior == BrowserOpenBehavior::USE_EXISTING) {
752 browser = chrome::FindTabbedBrowser(profile_, process_startup); 760 browser = chrome::FindTabbedBrowser(profile_, process_startup);
753 } 761 }
754 762
755 base::AutoReset<bool> synchronous_launch_resetter( 763 base::AutoReset<bool> synchronous_launch_resetter(
756 &StartupBrowserCreator::in_synchronous_profile_launch_, true); 764 &StartupBrowserCreator::in_synchronous_profile_launch_, true);
757 765
758 // OpenTabsInBrowser requires at least one tab be passed. As a fallback to 766 // OpenTabsInBrowser requires at least one tab be passed. As a fallback to
759 // prevent a crash, use the NTP if |tabs| is empty. 767 // prevent a crash, use the NTP if |tabs| is empty. This could happen if
768 // we expected a session restore to happen but it did not occur/succeed.
760 browser = OpenTabsInBrowser( 769 browser = OpenTabsInBrowser(
761 browser, process_startup, 770 browser, process_startup,
762 (tabs.empty() 771 (tabs.empty()
763 ? StartupTabs({StartupTab(GURL(chrome::kChromeUINewTabURL), false)}) 772 ? StartupTabs({StartupTab(GURL(chrome::kChromeUINewTabURL), false)})
764 : tabs)); 773 : tabs));
765 774
766 // Now that a restore is no longer possible, it is safe to clear DOM storage, 775 // Now that a restore is no longer possible, it is safe to clear DOM storage,
767 // unless this is a crash recovery. 776 // unless this is a crash recovery.
768 if (!is_post_crash_launch) { 777 if (!is_post_crash_launch) {
769 content::BrowserContext::GetDefaultStoragePartition(profile_) 778 content::BrowserContext::GetDefaultStoragePartition(profile_)
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
838 for (const GURL& url : urls_to_open) { 847 for (const GURL& url : urls_to_open) {
839 rappor::SampleDomainAndRegistryFromGURL(g_browser_process->rappor_service(), 848 rappor::SampleDomainAndRegistryFromGURL(g_browser_process->rappor_service(),
840 "Startup.BrowserLaunchURL", url); 849 "Startup.BrowserLaunchURL", url);
841 } 850 }
842 } 851 }
843 852
844 // static 853 // static
845 StartupBrowserCreatorImpl::BrowserOpenBehavior 854 StartupBrowserCreatorImpl::BrowserOpenBehavior
846 StartupBrowserCreatorImpl::DetermineBrowserOpenBehavior( 855 StartupBrowserCreatorImpl::DetermineBrowserOpenBehavior(
847 const SessionStartupPref& pref, 856 const SessionStartupPref& pref,
848 bool process_startup, 857 BrowserOpenBehaviorOptions options) {
849 bool is_post_crash_launch, 858 if (!(options & PROCESS_STARTUP)) {
850 bool has_restore_switch,
851 bool has_new_window_switch,
852 bool has_cmd_line_tabs) {
853 if (!process_startup) {
854 // For existing processes, restore would have happened before invoking this 859 // For existing processes, restore would have happened before invoking this
855 // function. If Chrome was launched with passed URLs, assume these should 860 // function. If Chrome was launched with passed URLs, assume these should
856 // be appended to an existing window if possible, unless overridden by a 861 // be appended to an existing window if possible, unless overridden by a
857 // switch. 862 // switch.
858 return (has_cmd_line_tabs && !has_new_window_switch) 863 return ((options & HAS_CMD_LINE_TABS) && !(options & HAS_NEW_WINDOW_SWITCH))
859 ? BrowserOpenBehavior::USE_EXISTING 864 ? BrowserOpenBehavior::USE_EXISTING
860 : BrowserOpenBehavior::NEW; 865 : BrowserOpenBehavior::NEW;
861 } 866 }
862 867
863 if (pref.type == SessionStartupPref::LAST) { 868 if (pref.type == SessionStartupPref::LAST) {
864 // Don't perform a session restore on a post-crash launch, as this could 869 // Don't perform a session restore on a post-crash launch, as this could
865 // cause a crash loop. These checks can be overridden by a switch. 870 // cause a crash loop. These checks can be overridden by a switch.
866 // TODO(crbug.com/647851): Group this check with the logic in 871 if (!(options & IS_POST_CRASH_LAUNCH) || (options & HAS_RESTORE_SWITCH))
867 // GetSessionStartupPref.
tmartino 2017/02/03 23:44:12 Note: Removing this TODO because I've closed the b
868 if (!is_post_crash_launch || has_restore_switch)
869 return BrowserOpenBehavior::SYNCHRONOUS_RESTORE; 872 return BrowserOpenBehavior::SYNCHRONOUS_RESTORE;
870 } 873 }
871 874
872 return BrowserOpenBehavior::NEW; 875 return BrowserOpenBehavior::NEW;
873 } 876 }
874 877
875 // static 878 // static
876 uint32_t StartupBrowserCreatorImpl::DetermineSynchronousRestoreOptions( 879 SessionRestore::Behavior
880 StartupBrowserCreatorImpl::DetermineSynchronousRestoreOptions(
877 bool has_create_browser_default, 881 bool has_create_browser_default,
878 bool has_create_browser_switch, 882 bool has_create_browser_switch,
879 bool was_mac_login_or_resume) { 883 bool was_mac_login_or_resume) {
880 uint32_t options = SessionRestore::SYNCHRONOUS; 884 SessionRestore::Behavior options = SessionRestore::SYNCHRONOUS;
881 885
882 // Suppress the creation of a new window on Mac when restoring with no windows 886 // Suppress the creation of a new window on Mac when restoring with no windows
883 // if launching Chrome via a login item or the resume feature in OS 10.7+. 887 // if launching Chrome via a login item or the resume feature in OS 10.7+.
884 if (!was_mac_login_or_resume && 888 if (!was_mac_login_or_resume &&
885 (has_create_browser_default || has_create_browser_switch)) 889 (has_create_browser_default || has_create_browser_switch))
886 options |= SessionRestore::ALWAYS_CREATE_TABBED_BROWSER; 890 options |= SessionRestore::ALWAYS_CREATE_TABBED_BROWSER;
887 891
888 return options; 892 return options;
889 } 893 }
890 894
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
975 if (profile_->GetLastSessionExitType() == Profile::EXIT_CRASHED && 979 if (profile_->GetLastSessionExitType() == Profile::EXIT_CRASHED &&
976 !command_line_.HasSwitch(switches::kRestoreLastSession)) { 980 !command_line_.HasSwitch(switches::kRestoreLastSession)) {
977 // The last session crashed. It's possible automatically loading the 981 // The last session crashed. It's possible automatically loading the
978 // page will trigger another crash, locking the user out of chrome. 982 // page will trigger another crash, locking the user out of chrome.
979 // To avoid this, don't restore on startup but instead show the crashed 983 // To avoid this, don't restore on startup but instead show the crashed
980 // infobar. 984 // infobar.
981 VLOG(1) << "Unclean exit; not processing"; 985 VLOG(1) << "Unclean exit; not processing";
982 return false; 986 return false;
983 } 987 }
984 988
985 uint32_t restore_behavior = SessionRestore::SYNCHRONOUS; 989 SessionRestore::Behavior restore_behavior = SessionRestore::SYNCHRONOUS;
986 if (browser_defaults::kAlwaysCreateTabbedBrowserOnSessionRestore || 990 if (browser_defaults::kAlwaysCreateTabbedBrowserOnSessionRestore ||
987 base::CommandLine::ForCurrentProcess()->HasSwitch( 991 base::CommandLine::ForCurrentProcess()->HasSwitch(
988 switches::kCreateBrowserOnStartupForTests)) { 992 switches::kCreateBrowserOnStartupForTests)) {
989 restore_behavior |= SessionRestore::ALWAYS_CREATE_TABBED_BROWSER; 993 restore_behavior |= SessionRestore::ALWAYS_CREATE_TABBED_BROWSER;
990 } 994 }
991 995
992 #if defined(OS_MACOSX) 996 #if defined(OS_MACOSX)
993 // On Mac, when restoring a session with no windows, suppress the creation 997 // On Mac, when restoring a session with no windows, suppress the creation
994 // of a new window in the case where the system is launching Chrome via a 998 // of a new window in the case where the system is launching Chrome via a
995 // login item or Lion's resume feature. 999 // login item or Lion's resume feature.
(...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after
1229 #if defined(OS_WIN) 1233 #if defined(OS_WIN)
1230 TriggeredProfileResetter* triggered_profile_resetter = 1234 TriggeredProfileResetter* triggered_profile_resetter =
1231 TriggeredProfileResetterFactory::GetForBrowserContext(profile_); 1235 TriggeredProfileResetterFactory::GetForBrowserContext(profile_);
1232 // TriggeredProfileResetter instance will be nullptr for incognito profiles. 1236 // TriggeredProfileResetter instance will be nullptr for incognito profiles.
1233 if (triggered_profile_resetter) { 1237 if (triggered_profile_resetter) {
1234 has_reset_trigger = triggered_profile_resetter->HasResetTrigger(); 1238 has_reset_trigger = triggered_profile_resetter->HasResetTrigger();
1235 } 1239 }
1236 #endif // defined(OS_WIN) 1240 #endif // defined(OS_WIN)
1237 return has_reset_trigger; 1241 return has_reset_trigger;
1238 } 1242 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698