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

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

Issue 2164033002: Refactoring startup logic for upcoming FRE changes (non-Win 10). (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Adding first unit tests Created 4 years, 4 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>
11 #include <memory> 11 #include <memory>
12 #include <vector> 12 #include <vector>
13 13
14 #include "apps/app_restore_service.h" 14 #include "apps/app_restore_service.h"
15 #include "apps/app_restore_service_factory.h" 15 #include "apps/app_restore_service_factory.h"
16 #include "base/bind.h" 16 #include "base/bind.h"
17 #include "base/bind_helpers.h" 17 #include "base/bind_helpers.h"
18 #include "base/command_line.h" 18 #include "base/command_line.h"
19 #include "base/compiler_specific.h" 19 #include "base/compiler_specific.h"
20 #include "base/environment.h" 20 #include "base/environment.h"
21 #include "base/feature_list.h"
21 #include "base/lazy_instance.h" 22 #include "base/lazy_instance.h"
22 #include "base/metrics/histogram_macros.h" 23 #include "base/metrics/histogram_macros.h"
23 #include "base/metrics/statistics_recorder.h" 24 #include "base/metrics/statistics_recorder.h"
24 #include "base/strings/string_number_conversions.h" 25 #include "base/strings/string_number_conversions.h"
25 #include "base/strings/string_split.h" 26 #include "base/strings/string_split.h"
26 #include "base/strings/string_util.h" 27 #include "base/strings/string_util.h"
27 #include "base/strings/stringprintf.h" 28 #include "base/strings/stringprintf.h"
28 #include "base/strings/utf_string_conversions.h" 29 #include "base/strings/utf_string_conversions.h"
29 #include "base/threading/thread_restrictions.h" 30 #include "base/threading/thread_restrictions.h"
30 #include "build/build_config.h" 31 #include "build/build_config.h"
31 #include "chrome/browser/apps/install_chrome_app.h" 32 #include "chrome/browser/apps/install_chrome_app.h"
32 #include "chrome/browser/browser_process.h" 33 #include "chrome/browser/browser_process.h"
33 #include "chrome/browser/chrome_notification_types.h" 34 #include "chrome/browser/chrome_notification_types.h"
34 #include "chrome/browser/custom_handlers/protocol_handler_registry.h" 35 #include "chrome/browser/custom_handlers/protocol_handler_registry.h"
35 #include "chrome/browser/custom_handlers/protocol_handler_registry_factory.h" 36 #include "chrome/browser/custom_handlers/protocol_handler_registry_factory.h"
36 #include "chrome/browser/defaults.h" 37 #include "chrome/browser/defaults.h"
37 #include "chrome/browser/extensions/extension_creator.h" 38 #include "chrome/browser/extensions/extension_creator.h"
38 #include "chrome/browser/extensions/extension_util.h" 39 #include "chrome/browser/extensions/extension_util.h"
39 #include "chrome/browser/extensions/launch_util.h" 40 #include "chrome/browser/extensions/launch_util.h"
40 #include "chrome/browser/extensions/pack_extension_job.h" 41 #include "chrome/browser/extensions/pack_extension_job.h"
41 #include "chrome/browser/first_run/first_run.h" 42 #include "chrome/browser/first_run/first_run.h"
43 #include "chrome/browser/first_run/first_run_features.h"
42 #include "chrome/browser/infobars/infobar_service.h" 44 #include "chrome/browser/infobars/infobar_service.h"
43 #include "chrome/browser/prefs/incognito_mode_prefs.h" 45 #include "chrome/browser/prefs/incognito_mode_prefs.h"
44 #include "chrome/browser/prefs/session_startup_pref.h" 46 #include "chrome/browser/prefs/session_startup_pref.h"
45 #include "chrome/browser/profile_resetter/triggered_profile_resetter.h" 47 #include "chrome/browser/profile_resetter/triggered_profile_resetter.h"
46 #include "chrome/browser/profile_resetter/triggered_profile_resetter_factory.h" 48 #include "chrome/browser/profile_resetter/triggered_profile_resetter_factory.h"
47 #include "chrome/browser/profiles/profile.h" 49 #include "chrome/browser/profiles/profile.h"
48 #include "chrome/browser/profiles/profile_io_data.h" 50 #include "chrome/browser/profiles/profile_io_data.h"
49 #include "chrome/browser/sessions/session_restore.h" 51 #include "chrome/browser/sessions/session_restore.h"
50 #include "chrome/browser/sessions/session_service.h" 52 #include "chrome/browser/sessions/session_service.h"
51 #include "chrome/browser/sessions/session_service_factory.h" 53 #include "chrome/browser/sessions/session_service_factory.h"
(...skipping 440 matching lines...) Expand 10 before | Expand all | Expand 10 after
492 WebContents* app_tab = ::OpenAppShortcutWindow(profile, url); 494 WebContents* app_tab = ::OpenAppShortcutWindow(profile, url);
493 return (app_tab != NULL); 495 return (app_tab != NULL);
494 } 496 }
495 } 497 }
496 return false; 498 return false;
497 } 499 }
498 500
499 void StartupBrowserCreatorImpl::ProcessLaunchURLs( 501 void StartupBrowserCreatorImpl::ProcessLaunchURLs(
500 bool process_startup, 502 bool process_startup,
501 const std::vector<GURL>& urls_to_open) { 503 const std::vector<GURL>& urls_to_open) {
504 if (base::FeatureList::IsEnabled(features::kUseConsolidatedFirstRun)) {
505 ProcessLaunchURLsUsingConsolidatedFlow(process_startup, urls_to_open);
506 return;
507 }
508 // TODO(tmartino): Remainder of this function is deprecated. Remove when
509 // kUseConsolidatedFirstRun is on by default.
510
502 // Don't open any browser windows if we're starting up in "background mode". 511 // Don't open any browser windows if we're starting up in "background mode".
503 if (process_startup && command_line_.HasSwitch(switches::kNoStartupWindow)) 512 if (process_startup && command_line_.HasSwitch(switches::kNoStartupWindow))
504 return; 513 return;
505 514
506 // Determine whether or not this launch must include the welcome page. 515 // Determine whether or not this launch must include the welcome page.
507 InitializeWelcomeRunType(urls_to_open); 516 InitializeWelcomeRunType(urls_to_open);
508 517
509 // TODO(tapted): Move this to startup_browser_creator_win.cc after refactor.
510 #if defined(OS_WIN)
511 if (base::win::GetVersion() >= base::win::VERSION_WIN8) {
512 // See if there are apps for this profile that should be launched on startup
513 // due to a switch from Metro mode.
514 app_metro_launch::HandleAppLaunchForMetroRestart(profile_);
515 }
516 #endif
517
518 if (process_startup && ProcessStartupURLs(urls_to_open)) { 518 if (process_startup && ProcessStartupURLs(urls_to_open)) {
519 // ProcessStartupURLs processed the urls, nothing else to do. 519 // ProcessStartupURLs processed the urls, nothing else to do.
520 return; 520 return;
521 } 521 }
522 522
523 chrome::startup::IsProcessStartup is_process_startup = process_startup ? 523 chrome::startup::IsProcessStartup is_process_startup = process_startup ?
524 chrome::startup::IS_PROCESS_STARTUP : 524 chrome::startup::IS_PROCESS_STARTUP :
525 chrome::startup::IS_NOT_PROCESS_STARTUP; 525 chrome::startup::IS_NOT_PROCESS_STARTUP;
526 if (!process_startup) { 526 if (!process_startup) {
527 // Even if we're not starting a new process, this may conceptually be 527 // Even if we're not starting a new process, this may conceptually be
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
559 // Always open a list of urls in a window on the native desktop. 559 // Always open a list of urls in a window on the native desktop.
560 browser = chrome::FindTabbedBrowser(profile_, false); 560 browser = chrome::FindTabbedBrowser(profile_, false);
561 } 561 }
562 // This will launch a browser; prevent session restore. 562 // This will launch a browser; prevent session restore.
563 StartupBrowserCreator::in_synchronous_profile_launch_ = true; 563 StartupBrowserCreator::in_synchronous_profile_launch_ = true;
564 browser = OpenURLsInBrowser(browser, process_startup, adjusted_urls); 564 browser = OpenURLsInBrowser(browser, process_startup, adjusted_urls);
565 StartupBrowserCreator::in_synchronous_profile_launch_ = false; 565 StartupBrowserCreator::in_synchronous_profile_launch_ = false;
566 AddInfoBarsIfNecessary(browser, is_process_startup); 566 AddInfoBarsIfNecessary(browser, is_process_startup);
567 } 567 }
568 568
569 void StartupBrowserCreatorImpl::ProcessLaunchURLsUsingConsolidatedFlow(
570 bool process_startup,
571 const std::vector<GURL>& urls_to_open) {
572 std::vector<GURL> adjusted_urls(urls_to_open);
573
574 // Don't open any browser windows if we're starting up in "background mode".
grt (UTC plus 2) 2016/08/23 10:36:02 nit: i'm in the "no personal pronouns" camp (https
tmartino 2016/09/08 21:33:17 Done.
575 if (process_startup && command_line_.HasSwitch(switches::kNoStartupWindow))
576 return;
577
578 // If Master Prefs contains any first run tabs, use only those.
grt (UTC plus 2) 2016/08/23 10:36:02 in the old logic, URLs on the command-line suppres
tmartino 2016/09/08 21:33:16 Good catch; done.
579 std::vector<GURL> tabs_to_insert;
580 if (browser_creator_) {
grt (UTC plus 2) 2016/08/23 10:36:02 && !browser_creator_->first_run_tabs_.empty()
tmartino 2016/09/08 21:33:16 Ack, see thread about ProcessMasterPrefsTabs in fi
581 tabs_to_insert =
grt (UTC plus 2) 2016/08/23 10:36:02 if you're assuming that first_run_tabs_ will never
grt (UTC plus 2) 2016/08/23 10:36:02 also add: DCHECK(process_startup); to document
tmartino 2016/09/08 21:33:16 Note that this logic has been moved to startup_tab
grt (UTC plus 2) 2016/09/09 11:40:29 when i wrote that, i must have been thinking "how
tmartino 2016/09/10 00:19:40 Two things of note: 1. IsChromeFirstRun uses a cac
582 first_run::ProcessMasterPrefsTabs(browser_creator_->first_run_tabs_);
583 browser_creator_->first_run_tabs_.clear();
584 }
585 // Otherwise, use the standard Onboarding logic to determine which, if any,
586 // tabs need to be surfaced.
587 if (tabs_to_insert.empty()) {
588 first_run::FirstRunSystemStatus status =
589 first_run::GetFirstRunSystemStatus();
590 tabs_to_insert = first_run::GetOnboardingTabs(status);
591 }
592
grt (UTC plus 2) 2016/08/23 10:36:02 is it intentional that the NTP is no longer opened
tmartino 2016/09/08 21:33:16 No, good catch. Thanks.
593 // If this profile is marked for a reset prompt, put this before all other
594 // tabs.
595 if (ProfileHasResetTrigger()) {
596 tabs_to_insert.insert(tabs_to_insert.begin(),
597 internals::GetTriggeredResetSettingsURL());
598 }
599
600 // TODO(tmartino): Function for processing pinned tabs.
601
602 // Prepend the resulting list of tabs to those passed in.
603 if (!tabs_to_insert.empty()) {
604 adjusted_urls.insert(adjusted_urls.begin(), tabs_to_insert.begin(),
605 tabs_to_insert.end());
606 }
607
608 // TODO(tmartino): If this is not process startup, we need to restore
609 // asynchronously and return here.
610
611 // See if we can or should restore an old session synchronously.
612 Browser* browser = MaybeRestoreSession(process_startup, adjusted_urls);
grt (UTC plus 2) 2016/08/23 10:36:02 can you break this function into two: one that com
613
614 // Otherwise, open.
615 if (!browser) {
grt (UTC plus 2) 2016/08/23 10:36:02 nit: omit braces
tmartino 2016/09/08 21:33:16 N/A
616 browser = OpenURLsInBrowser(browser, process_startup, adjusted_urls);
617 }
618
619 // Finally, add info bars.
620 chrome::startup::IsProcessStartup is_process_startup =
621 process_startup ? chrome::startup::IS_PROCESS_STARTUP
622 : chrome::startup::IS_NOT_PROCESS_STARTUP;
623 AddInfoBarsIfNecessary(browser, is_process_startup);
624 }
625
626 Browser* StartupBrowserCreatorImpl::MaybeRestoreSession(
627 bool process_startup,
628 const std::vector<GURL>& urls) {
629 // TODO(tmartino): Move session restore logic into this function.
630 return NULL;
grt (UTC plus 2) 2016/08/23 10:36:02 nit: nullptr
tmartino 2016/09/08 21:33:17 Done (throughout)
631 }
632
569 bool StartupBrowserCreatorImpl::ProcessStartupURLs( 633 bool StartupBrowserCreatorImpl::ProcessStartupURLs(
570 const std::vector<GURL>& urls_to_open) { 634 const std::vector<GURL>& urls_to_open) {
571 VLOG(1) << "StartupBrowserCreatorImpl::ProcessStartupURLs"; 635 VLOG(1) << "StartupBrowserCreatorImpl::ProcessStartupURLs";
572 SessionStartupPref pref = 636 SessionStartupPref pref =
573 StartupBrowserCreator::GetSessionStartupPref(command_line_, profile_); 637 StartupBrowserCreator::GetSessionStartupPref(command_line_, profile_);
574 if (pref.type == SessionStartupPref::LAST) 638 if (pref.type == SessionStartupPref::LAST)
575 VLOG(1) << "Pref: last"; 639 VLOG(1) << "Pref: last";
576 else if (pref.type == SessionStartupPref::URLS) 640 else if (pref.type == SessionStartupPref::URLS)
577 VLOG(1) << "Pref: urls"; 641 VLOG(1) << "Pref: urls";
578 else if (pref.type == SessionStartupPref::DEFAULT) 642 else if (pref.type == SessionStartupPref::DEFAULT)
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
641 !HasPendingUncleanExit(profile_)) { 705 !HasPendingUncleanExit(profile_)) {
642 content::BrowserContext::GetDefaultStoragePartition(profile_)-> 706 content::BrowserContext::GetDefaultStoragePartition(profile_)->
643 GetDOMStorageContext()->StartScavengingUnusedSessionStorage(); 707 GetDOMStorageContext()->StartScavengingUnusedSessionStorage();
644 } 708 }
645 709
646 return true; 710 return true;
647 } 711 }
648 712
649 Browser* StartupBrowserCreatorImpl::ProcessSpecifiedURLs( 713 Browser* StartupBrowserCreatorImpl::ProcessSpecifiedURLs(
650 const std::vector<GURL>& urls_to_open) { 714 const std::vector<GURL>& urls_to_open) {
715 // TODO(tmartino): Deprecated, remove this once UseConsolidatedFirstRun is
716 // enabled.
717
651 SessionStartupPref pref = 718 SessionStartupPref pref =
652 StartupBrowserCreator::GetSessionStartupPref(command_line_, profile_); 719 StartupBrowserCreator::GetSessionStartupPref(command_line_, profile_);
653 StartupTabs tabs; 720 StartupTabs tabs;
654 // Pinned tabs should not be displayed when chrome is launched in incognito 721 // Pinned tabs should not be displayed when chrome is launched in incognito
655 // mode. Also, no pages should be opened automatically if the session 722 // mode. Also, no pages should be opened automatically if the session
656 // crashed. Otherwise it might trigger another crash, locking the user out of 723 // crashed. Otherwise it might trigger another crash, locking the user out of
657 // chrome. The crash infobar is shown in this case. 724 // chrome. The crash infobar is shown in this case.
658 if (!IncognitoModePrefs::ShouldLaunchIncognito(command_line_, 725 if (!IncognitoModePrefs::ShouldLaunchIncognito(command_line_,
659 profile_->GetPrefs()) && 726 profile_->GetPrefs()) &&
660 !HasPendingUncleanExit(profile_)) { 727 !HasPendingUncleanExit(profile_)) {
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after
819 browser_creator_->is_default_browser_dialog_suppressed())) { 886 browser_creator_->is_default_browser_dialog_suppressed())) {
820 chrome::ShowDefaultBrowserPrompt(profile_); 887 chrome::ShowDefaultBrowserPrompt(profile_);
821 } 888 }
822 } 889 }
823 #endif 890 #endif
824 } 891 }
825 } 892 }
826 893
827 void StartupBrowserCreatorImpl::AddStartupURLs( 894 void StartupBrowserCreatorImpl::AddStartupURLs(
828 std::vector<GURL>* startup_urls) const { 895 std::vector<GURL>* startup_urls) const {
829 // TODO(atwilson): Simplify the logic that decides which tabs to open on 896 // TODO(tmartino): Deprecated, remove this once UseConsolidatedFirstRun is
830 // start-up and make it more consistent. http://crbug.com/248883 897 // enabled.
831 898
832 // If we have urls specified by the first run master preferences use them 899 // If we have urls specified by the first run master preferences use them
833 // and nothing else. 900 // and nothing else.
834 if (browser_creator_ && startup_urls->empty()) { 901 if (browser_creator_ && startup_urls->empty()) {
835 if (!browser_creator_->first_run_tabs_.empty()) { 902 if (!browser_creator_->first_run_tabs_.empty()) {
836 std::vector<GURL>::iterator it = 903 std::vector<GURL>::iterator it =
837 browser_creator_->first_run_tabs_.begin(); 904 browser_creator_->first_run_tabs_.begin();
838 while (it != browser_creator_->first_run_tabs_.end()) { 905 while (it != browser_creator_->first_run_tabs_.end()) {
839 // Replace magic names for the actual urls. 906 // Replace magic names for the actual urls.
840 if (it->host() == "new_tab_page") { 907 if (it->host() == "new_tab_page") {
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
886 startup_urls->at(0) == GURL(chrome::kChromeUINewTabURL)) 953 startup_urls->at(0) == GURL(chrome::kChromeUINewTabURL))
887 startup_urls->at(0) = sync_promo_url; 954 startup_urls->at(0) = sync_promo_url;
888 else 955 else
889 startup_urls->insert(startup_urls->begin(), sync_promo_url); 956 startup_urls->insert(startup_urls->begin(), sync_promo_url);
890 } 957 }
891 } 958 }
892 } 959 }
893 960
894 void StartupBrowserCreatorImpl::AddSpecialURLs( 961 void StartupBrowserCreatorImpl::AddSpecialURLs(
895 std::vector<GURL>* url_list) const { 962 std::vector<GURL>* url_list) const {
963 // TODO(tmartino): Deprecated, remove this once UseConsolidatedFirstRun is
964 // enabled.
965
896 // Optionally include the welcome page. 966 // Optionally include the welcome page.
897 if (welcome_run_type_ == WelcomeRunType::FIRST_TAB) 967 if (welcome_run_type_ == WelcomeRunType::FIRST_TAB)
898 url_list->insert(url_list->begin(), internals::GetWelcomePageURL()); 968 url_list->insert(url_list->begin(), internals::GetWelcomePageURL());
899 969
900 // If this Profile is marked for a reset prompt, ensure the reset 970 // If this Profile is marked for a reset prompt, ensure the reset
901 // settings dialog appears. 971 // settings dialog appears.
902 if (ProfileHasResetTrigger()) { 972 if (ProfileHasResetTrigger()) {
903 url_list->insert(url_list->begin(), 973 url_list->insert(url_list->begin(),
904 internals::GetTriggeredResetSettingsURL()); 974 internals::GetTriggeredResetSettingsURL());
905 } 975 }
906 } 976 }
907 977
908 // For first-run, the type will be FIRST_RUN_LAST for all systems except for 978 // For first-run, the type will be FIRST_RUN_LAST for all systems except for
909 // Windows 10+, where it will be FIRST_RUN_FIRST. For non-first run, the type 979 // Windows 10+, where it will be FIRST_RUN_FIRST. For non-first run, the type
910 // will be NONE for all systems except for Windows 10+, where it will be 980 // will be NONE for all systems except for Windows 10+, where it will be
911 // ANY_RUN_FIRST if this is the first somewhat normal launch since an OS 981 // ANY_RUN_FIRST if this is the first somewhat normal launch since an OS
912 // upgrade. 982 // upgrade.
983
984 // TODO(tmartino): Deprecated, remove this once UseConsolidatedFirstRun is
985 // enabled.
913 void StartupBrowserCreatorImpl::InitializeWelcomeRunType( 986 void StartupBrowserCreatorImpl::InitializeWelcomeRunType(
914 const std::vector<GURL>& urls_to_open) { 987 const std::vector<GURL>& urls_to_open) {
915 DCHECK_EQ(static_cast<int>(WelcomeRunType::NONE), 988 DCHECK_EQ(static_cast<int>(WelcomeRunType::NONE),
916 static_cast<int>(welcome_run_type_)); 989 static_cast<int>(welcome_run_type_));
917 #if defined(OS_WIN) 990 #if defined(OS_WIN)
918 // Do not welcome if there are any URLs to open. 991 // Do not welcome if there are any URLs to open.
919 if (!urls_to_open.empty()) 992 if (!urls_to_open.empty())
920 return; 993 return;
921 994
922 base::win::OSInfo* const os_info = base::win::OSInfo::GetInstance(); 995 base::win::OSInfo* const os_info = base::win::OSInfo::GetInstance();
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
989 #if defined(OS_WIN) 1062 #if defined(OS_WIN)
990 TriggeredProfileResetter* triggered_profile_resetter = 1063 TriggeredProfileResetter* triggered_profile_resetter =
991 TriggeredProfileResetterFactory::GetForBrowserContext(profile_); 1064 TriggeredProfileResetterFactory::GetForBrowserContext(profile_);
992 // TriggeredProfileResetter instance will be nullptr for incognito profiles. 1065 // TriggeredProfileResetter instance will be nullptr for incognito profiles.
993 if (triggered_profile_resetter) { 1066 if (triggered_profile_resetter) {
994 has_reset_trigger = triggered_profile_resetter->HasResetTrigger(); 1067 has_reset_trigger = triggered_profile_resetter->HasResetTrigger();
995 } 1068 }
996 #endif // defined(OS_WIN) 1069 #endif // defined(OS_WIN)
997 return has_reset_trigger; 1070 return has_reset_trigger;
998 } 1071 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698