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

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

Issue 2457653003: Completing refactor of startup_browser_creator_impl (Closed)
Patch Set: Created 4 years, 1 month 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 172 matching lines...) Expand 10 before | Expand all | Expand 10 after
183 183
184 void UrlsToTabs(const std::vector<GURL>& urls, StartupTabs* tabs) { 184 void UrlsToTabs(const std::vector<GURL>& urls, StartupTabs* tabs) {
185 for (size_t i = 0; i < urls.size(); ++i) { 185 for (size_t i = 0; i < urls.size(); ++i) {
186 StartupTab tab; 186 StartupTab tab;
187 tab.is_pinned = false; 187 tab.is_pinned = false;
188 tab.url = urls[i]; 188 tab.url = urls[i];
189 tabs->push_back(tab); 189 tabs->push_back(tab);
190 } 190 }
191 } 191 }
192 192
193 void TabsToUrls(const StartupTabs& tabs, std::vector<GURL>* urls) {
194 urls->resize(tabs.size());
195 std::transform(tabs.begin(), tabs.end(), urls->begin(),
196 [](StartupTab tab) { return tab.url; });
197 }
198
193 // Return true if the command line option --app-id is used. Set 199 // Return true if the command line option --app-id is used. Set
194 // |out_extension| to the app to open, and |out_launch_container| 200 // |out_extension| to the app to open, and |out_launch_container|
195 // to the type of window into which the app should be open. 201 // to the type of window into which the app should be open.
196 bool GetAppLaunchContainer( 202 bool GetAppLaunchContainer(
197 Profile* profile, 203 Profile* profile,
198 const std::string& app_id, 204 const std::string& app_id,
199 const Extension** out_extension, 205 const Extension** out_extension,
200 extensions::LaunchContainer* out_launch_container) { 206 extensions::LaunchContainer* out_launch_container) {
201 207
202 const Extension* extension = extensions::ExtensionRegistry::Get( 208 const Extension* extension = extensions::ExtensionRegistry::Get(
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
261 extension_id, extensions::ExtensionRegistry::EVERYTHING); 267 extension_id, extensions::ExtensionRegistry::EVERYTHING);
262 return extension && extension->is_platform_app() ? extension : NULL; 268 return extension && extension->is_platform_app() ? extension : NULL;
263 } 269 }
264 270
265 // Appends the contents of |from| to the end of |to|. 271 // Appends the contents of |from| to the end of |to|.
266 void AppendTabs(const StartupTabs& from, StartupTabs* to) { 272 void AppendTabs(const StartupTabs& from, StartupTabs* to) {
267 if (!from.empty()) 273 if (!from.empty())
268 to->insert(to->end(), from.begin(), from.end()); 274 to->insert(to->end(), from.begin(), from.end());
269 } 275 }
270 276
277 // Determines whether the Consolidated startup flow should be used, based on
278 // OS, OS version, and the kUseConsolidatedStartupFlow Feature.
279 bool UseConsolidatedFlow() {
280 #if defined(OS_WIN)
281 if (base::win::GetVersion() >= base::win::VERSION_WIN10)
282 return false;
283 #endif // defined(OS_WIN)
284 return base::FeatureList::IsEnabled(features::kUseConsolidatedStartupFlow);
285 }
286
271 } // namespace 287 } // namespace
272 288
273 namespace internals { 289 namespace internals {
274 290
275 GURL GetTriggeredResetSettingsURL() { 291 GURL GetTriggeredResetSettingsURL() {
276 return GURL( 292 return GURL(
277 chrome::GetSettingsUrl(chrome::kTriggeredResetProfileSettingsSubPage)); 293 chrome::GetSettingsUrl(chrome::kTriggeredResetProfileSettingsSubPage));
278 } 294 }
279 295
280 GURL GetWelcomePageURL() { 296 GURL GetWelcomePageURL() {
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
347 // URLs in that case. The user should see the window as an app, 363 // URLs in that case. The user should see the window as an app,
348 // not as chrome. 364 // not as chrome.
349 // Special case is when app switches are passed but we do want to restore 365 // Special case is when app switches are passed but we do want to restore
350 // session. In that case open app window + focus it after session is restored. 366 // session. In that case open app window + focus it after session is restored.
351 if (OpenApplicationWindow(profile)) { 367 if (OpenApplicationWindow(profile)) {
352 RecordLaunchModeHistogram(LM_AS_WEBAPP); 368 RecordLaunchModeHistogram(LM_AS_WEBAPP);
353 } else { 369 } else {
354 RecordLaunchModeHistogram(urls_to_open.empty() ? 370 RecordLaunchModeHistogram(urls_to_open.empty() ?
355 LM_TO_BE_DECIDED : LM_WITH_URLS); 371 LM_TO_BE_DECIDED : LM_WITH_URLS);
356 372
357 if (base::FeatureList::IsEnabled(features::kUseConsolidatedStartupFlow)) 373 if (UseConsolidatedFlow())
358 ProcessLaunchUrlsUsingConsolidatedFlow(process_startup, urls_to_open); 374 ProcessLaunchUrlsUsingConsolidatedFlow(process_startup, urls_to_open);
359 else 375 else
360 ProcessLaunchURLs(process_startup, urls_to_open); 376 ProcessLaunchURLs(process_startup, urls_to_open);
361 377
362 if (command_line_.HasSwitch(switches::kInstallChromeApp)) { 378 if (command_line_.HasSwitch(switches::kInstallChromeApp)) {
363 install_chrome_app::InstallChromeApp( 379 install_chrome_app::InstallChromeApp(
364 command_line_.GetSwitchValueASCII(switches::kInstallChromeApp)); 380 command_line_.GetSwitchValueASCII(switches::kInstallChromeApp));
365 } 381 }
366 382
367 // If this is an app launch, but we didn't open an app window, it may 383 // If this is an app launch, but we didn't open an app window, it may
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after
589 void StartupBrowserCreatorImpl::ProcessLaunchUrlsUsingConsolidatedFlow( 605 void StartupBrowserCreatorImpl::ProcessLaunchUrlsUsingConsolidatedFlow(
590 bool process_startup, 606 bool process_startup,
591 const std::vector<GURL>& cmd_line_urls) { 607 const std::vector<GURL>& cmd_line_urls) {
592 // Don't open any browser windows if starting up in "background mode". 608 // Don't open any browser windows if starting up in "background mode".
593 if (process_startup && command_line_.HasSwitch(switches::kNoStartupWindow)) 609 if (process_startup && command_line_.HasSwitch(switches::kNoStartupWindow))
594 return; 610 return;
595 611
596 StartupTabs cmd_line_tabs; 612 StartupTabs cmd_line_tabs;
597 UrlsToTabs(cmd_line_urls, &cmd_line_tabs); 613 UrlsToTabs(cmd_line_urls, &cmd_line_tabs);
598 614
599 bool is_incognito = IncognitoModePrefs::ShouldLaunchIncognito( 615 bool is_incognito_or_guest =
600 command_line_, profile_->GetPrefs()); 616 profile_->GetProfileType() != Profile::ProfileType::REGULAR_PROFILE;
601 bool is_post_crash_launch = HasPendingUncleanExit(profile_); 617 bool is_post_crash_launch = HasPendingUncleanExit(profile_);
602 StartupTabs tabs = 618 StartupTabs tabs =
603 DetermineStartupTabs(StartupTabProviderImpl(), cmd_line_tabs, 619 DetermineStartupTabs(StartupTabProviderImpl(), cmd_line_tabs,
604 is_incognito, is_post_crash_launch); 620 is_incognito_or_guest, is_post_crash_launch);
605 621
606 // TODO(tmartino): If this is not process startup, attempt to restore 622 // If an async restore is necessary and is successfully kicked off, the
607 // asynchronously and return here. This logic is self-contained in 623 // remainder of the process is self-contained, so return.
608 // SessionService and therefore can't be combined with the other Browser 624 if (MaybeAsyncRestore(tabs, process_startup, is_post_crash_launch))
grt (UTC plus 2) 2016/10/28 09:22:12 nit: i've had other reviewers ask me not to use "M
tmartino 2016/10/28 18:34:27 Hmm, I've tried three different names (Maybe..., B
609 // creation logic. 625 return;
610 626
611 // TODO(tmartino): Function which determines what behavior of session 627 BrowserOpenBehavior behavior = DetermineBrowserOpenBehavior(
tmartino 2016/10/27 23:00:43 One example of weird logic structure questions: do
612 // restore, if any, is necessary, and passes the result to a new function 628 StartupBrowserCreator::GetSessionStartupPref(command_line_, profile_),
613 // which opens tabs in a restored or newly-created Browser accordingly. 629 process_startup, is_post_crash_launch,
614 // Incorporates code from ProcessStartupUrls. 630 command_line_.HasSwitch(switches::kRestoreLastSession),
631 command_line_.HasSwitch(switches::kOpenInNewWindow),
632 !cmd_line_tabs.empty());
615 633
616 Browser* browser = OpenTabsInBrowser(nullptr, process_startup, tabs); 634 uint32_t restore_options = 0;
635 if (behavior == BrowserOpenBehavior::SYNCHRONOUS_RESTORE) {
636 #if defined(OS_MACOSX)
637 bool was_mac_login_or_resume = base::mac::WasLaunchedAsLoginOrResumeItem();
638 #else
639 bool was_mac_login_or_resume = false;
640 #endif
641 restore_options = DetermineSynchronousRestoreOptions(
642 browser_defaults::kAlwaysCreateTabbedBrowserOnSessionRestore,
643 base::CommandLine::ForCurrentProcess()->HasSwitch(
644 switches::kCreateBrowserOnStartupForTests),
645 was_mac_login_or_resume);
646 }
647
648 Browser* browser = RestoreOrCreateBrowser(
649 tabs, behavior, restore_options, process_startup, is_post_crash_launch);
617 650
618 // Finally, add info bars. 651 // Finally, add info bars.
619 AddInfoBarsIfNecessary( 652 AddInfoBarsIfNecessary(
620 browser, process_startup ? chrome::startup::IS_PROCESS_STARTUP 653 browser, process_startup ? chrome::startup::IS_PROCESS_STARTUP
621 : chrome::startup::IS_NOT_PROCESS_STARTUP); 654 : chrome::startup::IS_NOT_PROCESS_STARTUP);
622 } 655 }
623 656
624 StartupTabs StartupBrowserCreatorImpl::DetermineStartupTabs( 657 StartupTabs StartupBrowserCreatorImpl::DetermineStartupTabs(
625 const StartupTabProvider& provider, 658 const StartupTabProvider& provider,
626 const StartupTabs& cmd_line_tabs, 659 const StartupTabs& cmd_line_tabs,
627 bool is_incognito, 660 bool is_incognito_or_guest,
628 bool is_post_crash_launch) { 661 bool is_post_crash_launch) {
629 // Only the New Tab Page or command line URLs may be shown in incognito mode. 662 // Only the New Tab Page or command line URLs may be shown in incognito mode.
630 // A similar policy exists for crash recovery launches, to prevent getting the 663 // A similar policy exists for crash recovery launches, to prevent getting the
631 // user stuck in a crash loop. 664 // user stuck in a crash loop.
632 if (is_incognito || is_post_crash_launch) { 665 if (is_incognito_or_guest || is_post_crash_launch) {
633 if (cmd_line_tabs.empty()) 666 if (cmd_line_tabs.empty())
634 return StartupTabs({StartupTab(GURL(chrome::kChromeUINewTabURL), false)}); 667 return StartupTabs({StartupTab(GURL(chrome::kChromeUINewTabURL), false)});
635 return cmd_line_tabs; 668 return cmd_line_tabs;
636 } 669 }
637 670
638 // A trigger on a profile may indicate that we should show a tab which 671 // A trigger on a profile may indicate that we should show a tab which
639 // offers to reset the user's settings. When this appears, it is first, and 672 // offers to reset the user's settings. When this appears, it is first, and
640 // may be shown alongside command-line tabs. 673 // may be shown alongside command-line tabs.
641 StartupTabs tabs = provider.GetResetTriggerTabs(profile_); 674 StartupTabs tabs = provider.GetResetTriggerTabs(profile_);
642 675
(...skipping 15 matching lines...) Expand all
658 // including OS and whether or not this is First Run. 691 // including OS and whether or not this is First Run.
659 StartupTabs onboarding_tabs = provider.GetOnboardingTabs(); 692 StartupTabs onboarding_tabs = provider.GetOnboardingTabs();
660 AppendTabs(onboarding_tabs, &tabs); 693 AppendTabs(onboarding_tabs, &tabs);
661 694
662 // If the user has set the preference indicating URLs to show on opening, 695 // If the user has set the preference indicating URLs to show on opening,
663 // read and add those. 696 // read and add those.
664 StartupTabs prefs_tabs = provider.GetPreferencesTabs(command_line_, profile_); 697 StartupTabs prefs_tabs = provider.GetPreferencesTabs(command_line_, profile_);
665 AppendTabs(prefs_tabs, &tabs); 698 AppendTabs(prefs_tabs, &tabs);
666 699
667 // Potentially add the New Tab Page. Onboarding content is designed to 700 // Potentially add the New Tab Page. Onboarding content is designed to
668 // replace (and eventually funnel the user to) the NTP. Likewise, URLs read 701 // replace (and eventually funnel the user to) the NTP. Likewise, URLs
669 // from preferences are explicitly meant to override showing the NTP. 702 // from preferences are explicitly meant to override showing the NTP.
670 if (onboarding_tabs.empty() && prefs_tabs.empty()) 703 if (onboarding_tabs.empty() && prefs_tabs.empty())
671 tabs.emplace_back(GURL(chrome::kChromeUINewTabURL), false); 704 AppendTabs(provider.GetNewTabPageTabs(command_line_, profile_), &tabs);
672 705
673 // Add any tabs which the user has previously pinned. 706 // Maybe add any tabs which the user has previously pinned.
674 AppendTabs(provider.GetPinnedTabs(profile_), &tabs); 707 AppendTabs(provider.GetPinnedTabs(command_line_, profile_), &tabs);
675 708
676 return tabs; 709 return tabs;
677 } 710 }
678 711
712 bool StartupBrowserCreatorImpl::MaybeAsyncRestore(const StartupTabs& tabs,
713 bool process_startup,
714 bool is_post_crash_launch) {
715 // Restore is performed synchronously on startup, and is never performed when
716 // launching after crashing.
717 if (process_startup || is_post_crash_launch)
718 return false;
719
720 SessionService* service =
721 SessionServiceFactory::GetForProfileForSessionRestore(profile_);
722 std::vector<GURL> urls;
grt (UTC plus 2) 2016/10/28 09:22:12 i think this looks nicer as: return (service &&
tmartino 2016/10/28 18:34:27 Agreed, done.
723 TabsToUrls(tabs, &urls);
724 // Service will be null in incognito and guest mode.
725 return (service && service->RestoreIfNecessary(urls));
726 }
727
728 Browser* StartupBrowserCreatorImpl::RestoreOrCreateBrowser(
729 const StartupTabs& tabs,
730 BrowserOpenBehavior behavior,
731 uint32_t restore_options,
732 bool process_startup,
733 bool is_post_crash_launch) {
734 Browser* browser = nullptr;
735
736 if (behavior == BrowserOpenBehavior::SYNCHRONOUS_RESTORE) {
737 std::vector<GURL> urls;
738 TabsToUrls(tabs, &urls);
739 browser = SessionRestore::RestoreSession(profile_, nullptr, restore_options,
740 urls);
741 if (browser)
742 return browser;
743 } else if (behavior == BrowserOpenBehavior::USE_EXISTING) {
744 browser = chrome::FindTabbedBrowser(profile_, process_startup);
745 }
746
747 StartupBrowserCreator::in_synchronous_profile_launch_ = true;
748
749 // OpenTabsInBrowser requires at least one tab be passed. As a fallback to
750 // prevent a crash, use the NTP if |tabs| is empty.
751 browser = OpenTabsInBrowser(
tmartino 2016/10/27 23:00:43 This seems gross and unnecessary; however, I was a
grt (UTC plus 2) 2016/10/28 09:22:12 The doc comment on kRestoreLastSession says it's "
tmartino 2016/10/28 18:34:27 Done, and thank you for helping to keep docs up-to
752 browser, process_startup,
753 (tabs.empty()
754 ? StartupTabs({StartupTab(GURL(chrome::kChromeUINewTabURL), false)})
755 : tabs));
756
757 StartupBrowserCreator::in_synchronous_profile_launch_ = false;
758
759 // Now that a restore is no longer possible, it is safe to clear DOM storage,
760 // unless this is a crash recovery.
761 if (!is_post_crash_launch) {
762 content::BrowserContext::GetDefaultStoragePartition(profile_)
763 ->GetDOMStorageContext()
764 ->StartScavengingUnusedSessionStorage();
765 }
766
767 return browser;
768 }
769
770 // static
tmartino 2016/10/27 23:00:43 Will add unit tests for static code to this CL lat
771 StartupBrowserCreatorImpl::BrowserOpenBehavior
772 StartupBrowserCreatorImpl::DetermineBrowserOpenBehavior(
773 const SessionStartupPref& pref,
774 bool process_startup,
775 bool is_post_crash_launch,
776 bool has_restore_switch,
777 bool has_new_window_switch,
778 bool has_cmd_line_tabs) {
779 if (!process_startup) {
grt (UTC plus 2) 2016/10/28 09:22:12 i forget: is |process_startup| true or false for t
tmartino 2016/10/28 18:34:27 I haven't been able to completely trace that parti
780 // For existing processes, restore would have happened before invoking this
781 // function. If Chrome was launched with passed URLs, assume these should
782 // be appended to an existing window if possible, unless overridden by
783 // switch.
784 return (has_cmd_line_tabs && !has_new_window_switch)
785 ? BrowserOpenBehavior::USE_EXISTING
786 : BrowserOpenBehavior::NEW;
787 }
788
789 if (pref.type == SessionStartupPref::Type::LAST) {
grt (UTC plus 2) 2016/10/28 09:22:12 nit: omit "::Type" since it's a regular enum rathe
tmartino 2016/10/28 18:34:27 Done
790 // Don't perform a session restore on a post-crash launch, as this could
791 // cause a crash loop. These checks can be overridden by a switch.
792 // TODO(crbug.com/647851): Group this check with the logic in
793 // GetSessionStartupPref.
794 if (!is_post_crash_launch || has_restore_switch)
795 return BrowserOpenBehavior::SYNCHRONOUS_RESTORE;
796 }
797
798 return BrowserOpenBehavior::NEW;
799 }
800
801 // static
802 uint32_t StartupBrowserCreatorImpl::DetermineSynchronousRestoreOptions(
803 bool has_create_browser_default,
804 bool has_create_browser_switch,
805 bool was_mac_login_or_resume) {
806 uint32_t options = SessionRestore::SYNCHRONOUS;
807
808 if (has_create_browser_default || has_create_browser_switch)
grt (UTC plus 2) 2016/10/28 09:22:12 nit: rather than setting this bit and then clearin
tmartino 2016/10/28 18:34:27 Done
809 options |= SessionRestore::ALWAYS_CREATE_TABBED_BROWSER;
810
811 // On Mac, when restoring a session with no windows, suppress the creation
812 // of a new window in the case where the system is launching Chrome via a
813 // login item or Lion's resume feature.
grt (UTC plus 2) 2016/10/28 09:22:12 nit: while everyone new what "Lion" meant when it
tmartino 2016/10/28 18:34:27 Done
814 if (was_mac_login_or_resume)
815 options = options & ~SessionRestore::ALWAYS_CREATE_TABBED_BROWSER;
816
817 return options;
818 }
819
679 void StartupBrowserCreatorImpl::AddUniqueURLs(const std::vector<GURL>& urls, 820 void StartupBrowserCreatorImpl::AddUniqueURLs(const std::vector<GURL>& urls,
680 StartupTabs* tabs) { 821 StartupTabs* tabs) {
681 size_t num_existing_tabs = tabs->size(); 822 size_t num_existing_tabs = tabs->size();
682 for (size_t i = 0; i < urls.size(); ++i) { 823 for (size_t i = 0; i < urls.size(); ++i) {
683 bool in_tabs = false; 824 bool in_tabs = false;
684 for (size_t j = 0; j < num_existing_tabs; ++j) { 825 for (size_t j = 0; j < num_existing_tabs; ++j) {
685 if (urls[i] == (*tabs)[j].url) { 826 if (urls[i] == (*tabs)[j].url) {
686 in_tabs = true; 827 in_tabs = true;
687 break; 828 break;
688 } 829 }
(...skipping 393 matching lines...) Expand 10 before | Expand all | Expand 10 after
1082 #if defined(OS_WIN) 1223 #if defined(OS_WIN)
1083 TriggeredProfileResetter* triggered_profile_resetter = 1224 TriggeredProfileResetter* triggered_profile_resetter =
1084 TriggeredProfileResetterFactory::GetForBrowserContext(profile_); 1225 TriggeredProfileResetterFactory::GetForBrowserContext(profile_);
1085 // TriggeredProfileResetter instance will be nullptr for incognito profiles. 1226 // TriggeredProfileResetter instance will be nullptr for incognito profiles.
1086 if (triggered_profile_resetter) { 1227 if (triggered_profile_resetter) {
1087 has_reset_trigger = triggered_profile_resetter->HasResetTrigger(); 1228 has_reset_trigger = triggered_profile_resetter->HasResetTrigger();
1088 } 1229 }
1089 #endif // defined(OS_WIN) 1230 #endif // defined(OS_WIN)
1090 return has_reset_trigger; 1231 return has_reset_trigger;
1091 } 1232 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698