chromium/chrome/browser/ui/startup/startup_browser_creator.cc

// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/startup/startup_browser_creator.h"

#include <stddef.h>

#include <optional>
#include <set>
#include <string>
#include <utility>

#include "apps/switches.h"
#include "base/command_line.h"
#include "base/containers/contains.h"
#include "base/debug/dump_without_crashing.h"
#include "base/feature_list.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/lazy_instance.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "base/metrics/histogram_base.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros.h"
#include "base/metrics/statistics_recorder.h"
#include "base/scoped_multi_source_observation.h"
#include "base/strings/string_tokenizer.h"
#include "base/task/thread_pool.h"
#include "base/threading/scoped_blocking_call.h"
#include "base/trace_event/trace_event.h"
#include "build/branding_buildflags.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/app_mode/app_mode_utils.h"
#include "chrome/browser/apps/app_service/app_service_proxy.h"
#include "chrome/browser/apps/app_service/app_service_proxy_factory.h"
#include "chrome/browser/apps/platform_apps/app_load_service.h"
#include "chrome/browser/apps/platform_apps/platform_app_launch.h"
#include "chrome/browser/ash/crosapi/browser_data_migrator.h"
#include "chrome/browser/browser_features.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/custom_handlers/protocol_handler_registry_factory.h"
#include "chrome/browser/extensions/startup_helper.h"
#include "chrome/browser/first_run/first_run.h"
#include "chrome/browser/lifetime/browser_shutdown.h"
#include "chrome/browser/prefs/incognito_mode_prefs.h"
#include "chrome/browser/prefs/session_startup_pref.h"
#include "chrome/browser/profiles/nuke_profile_directory_utils.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_attributes_entry.h"
#include "chrome/browser/profiles/profile_attributes_storage.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/profiles/profile_observer.h"
#include "chrome/browser/profiles/profile_window.h"
#include "chrome/browser/profiles/profiles_state.h"
#include "chrome/browser/search_engines/template_url_service_factory.h"
#include "chrome/browser/sessions/exit_type_service.h"
#include "chrome/browser/signin/signin_util.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_list_observer.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/extensions/application_launch.h"
#include "chrome/browser/ui/startup/launch_mode_recorder.h"
#include "chrome/browser/ui/startup/startup_browser_creator_impl.h"
#include "chrome/browser/ui/startup/startup_tab_provider.h"
#include "chrome/browser/ui/startup/startup_types.h"
#include "chrome/browser/ui/startup/web_app_startup_utils.h"
#include "chrome/browser/ui/ui_features.h"
#include "chrome/browser/ui/web_applications/web_app_ui_manager_impl.h"
#include "chrome/browser/web_applications/web_app_ui_manager.h"
#include "chrome/browser/web_applications/web_app_utils.h"
#include "chrome/common/buildflags.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_features.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/url_constants.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "components/search_engines/util.h"
#include "components/services/app_service/public/cpp/app_launch_util.h"
#include "components/startup_metric_utils/browser/startup_metric_utils.h"
#include "components/url_formatter/url_fixer.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/child_process_security_policy.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/common/content_switches.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/common/switches.h"
#include "printing/buildflags/buildflags.h"

#if BUILDFLAG(IS_CHROMEOS_ASH)
#include "ash/constants/ash_switches.h"
#include "chrome/browser/ash/app_mode/app_launch_utils.h"
#include "chrome/browser/ash/app_mode/kiosk_app_types.h"
#include "chrome/browser/ash/app_mode/kiosk_controller.h"
#include "chrome/browser/ash/app_restore/full_restore_service.h"
#include "chrome/browser/ash/floating_workspace/floating_workspace_service.h"
#include "chrome/browser/ash/floating_workspace/floating_workspace_util.h"
#include "chrome/browser/ash/profiles/profile_helper.h"
#include "chrome/browser/lifetime/application_lifetime.h"
#include "chromeos/ash/components/browser_context_helper/browser_context_helper.h"
#include "chromeos/ash/components/cryptohome/cryptohome_parameters.h"
#include "components/user_manager/user_manager.h"
#else
#include "chrome/browser/extensions/api/messaging/native_messaging_launch_from_native.h"
#include "chrome/browser/ui/profiles/profile_picker.h"
#endif

#if BUILDFLAG(IS_CHROMEOS_LACROS)
#include "chromeos/startup/browser_params_proxy.h"
#endif

#if BUILDFLAG(IS_CHROMEOS_LACROS) || BUILDFLAG(ENABLE_DICE_SUPPORT)
#include "chrome/browser/ui/startup/first_run_service.h"
#endif

#if BUILDFLAG(IS_MAC)
#include "chrome/browser/web_applications/os_integration/mac/app_shim_registry.h"
#endif

#if BUILDFLAG(IS_WIN)
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/notifications/notification_platform_bridge_win.h"
#include "chrome/browser/notifications/win/notification_launch_id.h"
#include "chrome/browser/ui/startup/credential_provider_signin_dialog_win.h"
#include "chrome/browser/ui/webui/settings/reset_settings_handler.h"
#include "chrome/browser/web_applications/web_app_provider.h"
#include "chrome/credential_provider/common/gcp_strings.h"
#endif  // BUILDFLAG(IS_WIN)

#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN)
#include "chrome/browser/headless/headless_mode_util.h"
#include "chrome/browser/ui/startup/web_app_info_recorder_utils.h"
#include "components/headless/policy/headless_mode_policy.h"
#endif

#if !BUILDFLAG(IS_CHROMEOS)
#include "chrome/browser/web_applications/isolated_web_apps/isolated_web_app_installation_manager.h"
#endif

BrowserThread;
ChildProcessSecurityPolicy;

namespace {

// Keeps track on which profiles have been launched.
class ProfileLaunchObserver : public ProfileObserver,
                              public BrowserListObserver {};

base::LazyInstance<ProfileLaunchObserver>::DestructorAtExit
    profile_launch_observer =;

// Dumps the current set of the browser process's histograms to |output_file|.
// The file is overwritten if it exists. This function should only be called in
// the blocking pool.
void DumpBrowserHistograms(const base::FilePath& output_file) {}

// Returns whether |profile_info.profile| can be opened during Chrome startup
// without explicit user action.
bool CanOpenProfileOnStartup(StartupProfileInfo profile_info) {}

#if !BUILDFLAG(IS_CHROMEOS_ASH)
StartupProfileModeReason ShouldShowProfilePickerAtProcessLaunch(
    ProfileManager* profile_manager,
    bool has_command_line_specified_profile_directory,
    const base::CommandLine& command_line) {}
#endif  // !BUILDFLAG(IS_CHROMEOS_ASH)

// If Incognito or Guest mode are requested by policy or command line returns
// the appropriate private browsing profile. Otherwise returns
// `profile_info.profile`.
Profile* GetPrivateProfileIfRequested(const base::CommandLine& command_line,
                                      StartupProfileInfo profile_info) {}

#if !BUILDFLAG(IS_CHROMEOS_ASH)
StartupProfileInfo GetProfilePickerStartupProfileInfo() {}
#endif  // !BUILDFLAG(IS_CHROMEOS_ASH)

bool IsSilentLaunchEnabled(const base::CommandLine& command_line,
                           const Profile* profile) {}

bool CanOpenWebApp(Profile* profile) {}

// Handles the --app switch.
bool MaybeLaunchAppShortcutWindow(const base::CommandLine& command_line,
                                  const base::FilePath& cur_dir,
                                  chrome::startup::IsFirstRun is_first_run,
                                  Profile* profile) {}

bool MaybeLaunchExtensionApp(const base::CommandLine& command_line,
                             const base::FilePath& cur_dir,
                             chrome::startup::IsFirstRun is_first_run,
                             Profile* profile) {}

// These values are persisted to logs. Entries should not be renumbered and
// numeric values should never be reused. Updates need to be reflected in
// enum IncognitoForcedStart in tools/metrics/histograms/enums.xml.
enum class IncognitoForcedStart {};

void RecordIncognitoForcedStart(bool should_launch_incognito,
                                bool has_incognito_switch) {}

#if BUILDFLAG(IS_CHROMEOS_LACROS) || BUILDFLAG(ENABLE_DICE_SUPPORT)
// Launches a browser by using a dedicated `StartupBrowserCreator`, to avoid
// having to rely on the current instance staying alive while this method is
// bound as a callback.
void OpenNewWindowForFirstRun(
    const base::CommandLine& command_line,
    Profile* profile,
    const base::FilePath& cur_dir,
    const std::vector<GURL>& first_run_urls,
    chrome::startup::IsProcessStartup process_startup,
    chrome::startup::IsFirstRun is_first_run,
    bool proceed) {}
#endif  // BUILDFLAG(IS_CHROMEOS_LACROS) || BUILDFLAG(ENABLE_DICE_SUPPORT)

#if BUILDFLAG(IS_CHROMEOS_ASH)
// Returns the app id of the kiosk app associated with the current user session.
// Returns nullopt for non-kiosk user sessions, since crash recovery is not
// supported there.
std::optional<ash::KioskAppId> GetAppId(const base::CommandLine& command_line,
                                        Profile* profile) {
  const user_manager::User* user =
      ash::ProfileHelper::Get()->GetUserByProfile(profile);
  if (user && user->GetType() == user_manager::UserType::kKioskApp) {
    return ash::KioskAppId::ForChromeApp(
        command_line.GetSwitchValueASCII(::switches::kAppId),
        user->GetAccountId());
  } else if (user && user->GetType() == user_manager::UserType::kWebKioskApp) {
    return ash::KioskAppId::ForWebApp(user->GetAccountId());
  } else {
    return std::nullopt;
  }
}
#endif  // BUILDFLAG(IS_CHROMEOS_ASH)

}  // namespace

StartupProfileMode StartupProfileModeFromReason(
    StartupProfileModeReason reason) {}

StartupBrowserCreator::StartupBrowserCreator() = default;

StartupBrowserCreator::~StartupBrowserCreator() {}

// static
bool StartupBrowserCreator::was_restarted_read_ =;

// static
bool StartupBrowserCreator::in_synchronous_profile_launch_ =;

void StartupBrowserCreator::AddFirstRunTabs(const std::vector<GURL>& urls) {}

bool StartupBrowserCreator::Start(const base::CommandLine& cmd_line,
                                  const base::FilePath& cur_dir,
                                  StartupProfileInfo profile_info,
                                  const Profiles& last_opened_profiles) {}

// static
bool StartupBrowserCreator::InSynchronousProfileLaunch() {}

void StartupBrowserCreator::LaunchBrowser(
    const base::CommandLine& command_line,
    Profile* profile,
    const base::FilePath& cur_dir,
    chrome::startup::IsProcessStartup process_startup,
    chrome::startup::IsFirstRun is_first_run,
    bool restore_tabbed_browser) {}

void StartupBrowserCreator::LaunchBrowserForLastProfiles(
    const base::CommandLine& command_line,
    const base::FilePath& cur_dir,
    chrome::startup::IsProcessStartup process_startup,
    chrome::startup::IsFirstRun is_first_run,
    StartupProfileInfo profile_info,
    const Profiles& last_opened_profiles,
    bool restore_tabbed_browser) {}

// static
bool StartupBrowserCreator::WasRestarted() {}

// static
SessionStartupPref StartupBrowserCreator::GetSessionStartupPref(
    const base::CommandLine& command_line,
    const Profile* profile) {}

// static
void StartupBrowserCreator::ClearLaunchedProfilesForTesting() {}

// static
void StartupBrowserCreator::RegisterLocalStatePrefs(
    PrefRegistrySimple* registry) {}

// static
void StartupBrowserCreator::RegisterProfilePrefs(PrefRegistrySimple* registry) {}

// static
bool StartupBrowserCreator::ShouldLoadProfileWithoutWindow(
    const base::CommandLine& command_line) {}

bool StartupBrowserCreator::ProcessCmdLineImpl(
    const base::CommandLine& command_line,
    const base::FilePath& cur_dir,
    chrome::startup::IsProcessStartup process_startup,
    StartupProfileInfo profile_info,
    const Profiles& last_opened_profiles) {}

void StartupBrowserCreator::ProcessLastOpenedProfiles(
    const base::CommandLine& command_line,
    const base::FilePath& cur_dir,
    chrome::startup::IsProcessStartup process_startup,
    chrome::startup::IsFirstRun is_first_run,
    Profile* last_used_profile,
    const Profiles& last_opened_profiles) {}

// static
bool StartupBrowserCreator::ProcessLoadApps(
    const base::CommandLine& command_line,
    const base::FilePath& cur_dir,
    Profile* profile) {}

// static
void StartupBrowserCreator::ProcessCommandLineWithProfile(
    const base::CommandLine& command_line,
    const base::FilePath& cur_dir,
    StartupProfileMode mode,
    Profile* profile) {}

// static
void StartupBrowserCreator::ProcessCommandLineAlreadyRunning(
    const base::CommandLine& command_line,
    const base::FilePath& cur_dir,
    const StartupProfilePathInfo& profile_path_info) {}

// static
void StartupBrowserCreator::OpenStartupPages(
    Browser* browser,
    chrome::startup::IsProcessStartup process_startup) {}

// static
bool StartupBrowserCreator::ActivatedProfile() {}

bool HasPendingUncleanExit(Profile* profile) {}

void AddLaunchedProfile(Profile* profile) {}

StartupProfilePathInfo GetStartupProfilePath(
    const base::FilePath& cur_dir,
    const base::CommandLine& command_line,
    bool ignore_profile_picker) {}

#if !BUILDFLAG(IS_CHROMEOS_ASH)
StartupProfileInfo GetStartupProfile(const base::FilePath& cur_dir,
                                     const base::CommandLine& command_line) {}

StartupProfileInfo GetFallbackStartupProfile() {}
#endif  // !BUILDFLAG(IS_CHROMEOS_ASH)