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
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/startup/startup_browser_creator.h"
7 #include <algorithm> // For max().
10 #include "apps/app_load_service.h"
11 #include "apps/switches.h"
12 #include "base/bind.h"
13 #include "base/bind_helpers.h"
14 #include "base/command_line.h"
15 #include "base/compiler_specific.h"
16 #include "base/environment.h"
17 #include "base/file_util.h"
18 #include "base/files/file_path.h"
19 #include "base/lazy_instance.h"
20 #include "base/logging.h"
21 #include "base/memory/scoped_ptr.h"
22 #include "base/metrics/histogram.h"
23 #include "base/metrics/statistics_recorder.h"
24 #include "base/path_service.h"
25 #include "base/prefs/pref_service.h"
26 #include "base/strings/string_number_conversions.h"
27 #include "base/strings/string_split.h"
28 #include "base/strings/utf_string_conversions.h"
29 #include "base/threading/thread_restrictions.h"
30 #include "chrome/browser/app_mode/app_mode_utils.h"
31 #include "chrome/browser/auto_launch_trial.h"
32 #include "chrome/browser/browser_process.h"
33 #include "chrome/browser/chrome_notification_types.h"
34 #include "chrome/browser/custom_handlers/protocol_handler_registry.h"
35 #include "chrome/browser/extensions/startup_helper.h"
36 #include "chrome/browser/extensions/unpacked_installer.h"
37 #include "chrome/browser/first_run/first_run.h"
38 #include "chrome/browser/google/google_util.h"
39 #include "chrome/browser/notifications/desktop_notification_service.h"
40 #include "chrome/browser/prefs/incognito_mode_prefs.h"
41 #include "chrome/browser/prefs/session_startup_pref.h"
42 #include "chrome/browser/profiles/profile.h"
43 #include "chrome/browser/profiles/profile_manager.h"
44 #include "chrome/browser/profiles/profiles_state.h"
45 #include "chrome/browser/search_engines/util.h"
46 #include "chrome/browser/ui/app_list/app_list_service.h"
47 #include "chrome/browser/ui/browser.h"
48 #include "chrome/browser/ui/browser_dialogs.h"
49 #include "chrome/browser/ui/browser_finder.h"
50 #include "chrome/browser/ui/browser_window.h"
51 #include "chrome/browser/ui/startup/startup_browser_creator_impl.h"
52 #include "chrome/common/chrome_constants.h"
53 #include "chrome/common/chrome_paths.h"
54 #include "chrome/common/chrome_result_codes.h"
55 #include "chrome/common/chrome_switches.h"
56 #include "chrome/common/chrome_version_info.h"
57 #include "chrome/common/net/url_fixer_upper.h"
58 #include "chrome/common/pref_names.h"
59 #include "chrome/common/url_constants.h"
60 #include "chrome/installer/util/browser_distribution.h"
61 #include "components/signin/core/common/profile_management_switches.h"
62 #include "content/public/browser/browser_thread.h"
63 #include "content/public/browser/child_process_security_policy.h"
64 #include "content/public/browser/navigation_controller.h"
65 #include "grit/locale_settings.h"
66 #include "net/base/net_util.h"
67 #include "ui/base/l10n/l10n_util.h"
68 #include "ui/base/resource/resource_bundle.h"
71 #include "ash/shell.h"
74 #if defined(OS_CHROMEOS)
75 #include "chrome/browser/chromeos/app_mode/app_launch_utils.h"
76 #include "chrome/browser/chromeos/kiosk_mode/kiosk_mode_settings.h"
77 #include "chrome/browser/chromeos/login/demo_mode/demo_app_launcher.h"
78 #include "chrome/browser/chromeos/login/user_manager.h"
79 #include "chrome/browser/chromeos/profiles/profile_helper.h"
80 #include "chrome/browser/lifetime/application_lifetime.h"
81 #include "chromeos/chromeos_switches.h"
84 #if defined(TOOLKIT_VIEWS) && defined(OS_LINUX)
85 #include "ui/events/x/touch_factory_x11.h"
88 #if defined(ENABLE_FULL_PRINTING)
89 #include "chrome/browser/printing/cloud_print/cloud_print_proxy_service.h"
90 #include "chrome/browser/printing/cloud_print/cloud_print_proxy_service_factory.h"
91 #include "chrome/browser/printing/print_dialog_cloud.h"
94 using content::BrowserThread;
95 using content::ChildProcessSecurityPolicy;
99 // Keeps track on which profiles have been launched.
100 class ProfileLaunchObserver : public content::NotificationObserver {
102 ProfileLaunchObserver()
103 : profile_to_activate_(NULL),
104 activated_profile_(false) {
105 registrar_.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED,
106 content::NotificationService::AllSources());
107 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_WINDOW_READY,
108 content::NotificationService::AllSources());
110 virtual ~ProfileLaunchObserver() {}
112 virtual void Observe(int type,
113 const content::NotificationSource& source,
114 const content::NotificationDetails& details) OVERRIDE {
116 case chrome::NOTIFICATION_PROFILE_DESTROYED: {
117 Profile* profile = content::Source<Profile>(source).ptr();
118 launched_profiles_.erase(profile);
119 opened_profiles_.erase(profile);
120 if (profile == profile_to_activate_)
121 profile_to_activate_ = NULL;
122 // If this profile was the last launched one without an opened window,
123 // then we may be ready to activate |profile_to_activate_|.
124 MaybeActivateProfile();
127 case chrome::NOTIFICATION_BROWSER_WINDOW_READY: {
128 Browser* browser = content::Source<Browser>(source).ptr();
130 opened_profiles_.insert(browser->profile());
131 MaybeActivateProfile();
139 bool HasBeenLaunched(const Profile* profile) const {
140 return launched_profiles_.find(profile) != launched_profiles_.end();
143 void AddLaunched(Profile* profile) {
144 launched_profiles_.insert(profile);
145 // Since the startup code only executes for browsers launched in
146 // desktop mode, i.e., HOST_DESKTOP_TYPE_NATIVE. Ash should never get here.
147 if (chrome::FindBrowserWithProfile(profile,
148 chrome::HOST_DESKTOP_TYPE_NATIVE)) {
149 // A browser may get opened before we get initialized (e.g., in tests),
150 // so we never see the NOTIFICATION_BROWSER_WINDOW_READY for it.
151 opened_profiles_.insert(profile);
156 launched_profiles_.clear();
157 opened_profiles_.clear();
160 bool activated_profile() { return activated_profile_; }
162 void set_profile_to_activate(Profile* profile) {
163 profile_to_activate_ = profile;
164 MaybeActivateProfile();
168 void MaybeActivateProfile() {
169 if (!profile_to_activate_)
171 // Check that browsers have been opened for all the launched profiles.
172 // Note that browsers opened for profiles that were not added as launched
173 // profiles are simply ignored.
174 std::set<const Profile*>::const_iterator i = launched_profiles_.begin();
175 for (; i != launched_profiles_.end(); ++i) {
176 if (opened_profiles_.find(*i) == opened_profiles_.end())
179 // Asynchronous post to give a chance to the last window to completely
180 // open and activate before trying to activate |profile_to_activate_|.
181 BrowserThread::PostTask(
182 BrowserThread::UI, FROM_HERE,
183 base::Bind(&ProfileLaunchObserver::ActivateProfile,
184 base::Unretained(this)));
185 // Avoid posting more than once before ActivateProfile gets called.
186 registrar_.Remove(this, chrome::NOTIFICATION_BROWSER_WINDOW_READY,
187 content::NotificationService::AllSources());
188 registrar_.Remove(this, chrome::NOTIFICATION_PROFILE_DESTROYED,
189 content::NotificationService::AllSources());
192 void ActivateProfile() {
193 // We need to test again, in case the profile got deleted in the mean time.
194 if (profile_to_activate_) {
195 Browser* browser = chrome::FindBrowserWithProfile(
196 profile_to_activate_, chrome::HOST_DESKTOP_TYPE_NATIVE);
197 // |profile| may never get launched, e.g., if it only had
198 // incognito Windows and one of them was used to exit Chrome.
199 // So it won't have a browser in that case.
201 browser->window()->Activate();
202 // No need try to activate this profile again.
203 profile_to_activate_ = NULL;
205 // Assign true here, even if no browser was actually activated, so that
206 // the test can stop waiting, and fail gracefully when needed.
207 activated_profile_ = true;
210 // These are the profiles that get launched by
211 // StartupBrowserCreator::LaunchBrowser.
212 std::set<const Profile*> launched_profiles_;
213 // These are the profiles for which at least one browser window has been
214 // opened. This is needed to know when it is safe to activate
215 // |profile_to_activate_|, otherwise, new browser windows being opened will
216 // be activated on top of it.
217 std::set<const Profile*> opened_profiles_;
218 content::NotificationRegistrar registrar_;
219 // This is NULL until the profile to activate has been chosen. This value,
220 // should only be set once all profiles have been launched, otherwise,
221 // activation may not happen after the launch of newer profiles.
222 Profile* profile_to_activate_;
223 // Set once we attempted to activate a profile. We only get one shot at this.
224 bool activated_profile_;
226 DISALLOW_COPY_AND_ASSIGN(ProfileLaunchObserver);
229 base::LazyInstance<ProfileLaunchObserver> profile_launch_observer =
230 LAZY_INSTANCE_INITIALIZER;
232 // Dumps the current set of the browser process's histograms to |output_file|.
233 // The file is overwritten if it exists. This function should only be called in
234 // the blocking pool.
235 void DumpBrowserHistograms(const base::FilePath& output_file) {
236 base::ThreadRestrictions::AssertIOAllowed();
238 std::string output_string(base::StatisticsRecorder::ToJSON(std::string()));
239 base::WriteFile(output_file, output_string.data(),
240 static_cast<int>(output_string.size()));
245 StartupBrowserCreator::StartupBrowserCreator()
246 : is_default_browser_dialog_suppressed_(false),
247 show_main_browser_window_(true) {
250 StartupBrowserCreator::~StartupBrowserCreator() {}
253 bool StartupBrowserCreator::was_restarted_read_ = false;
256 bool StartupBrowserCreator::in_synchronous_profile_launch_ = false;
258 void StartupBrowserCreator::AddFirstRunTab(const GURL& url) {
259 first_run_tabs_.push_back(url);
263 bool StartupBrowserCreator::InSynchronousProfileLaunch() {
264 return in_synchronous_profile_launch_;
267 bool StartupBrowserCreator::LaunchBrowser(
268 const CommandLine& command_line,
270 const base::FilePath& cur_dir,
271 chrome::startup::IsProcessStartup process_startup,
272 chrome::startup::IsFirstRun is_first_run,
275 in_synchronous_profile_launch_ =
276 process_startup == chrome::startup::IS_PROCESS_STARTUP;
279 // Continue with the incognito profile from here on if Incognito mode
281 if (IncognitoModePrefs::ShouldLaunchIncognito(command_line,
282 profile->GetPrefs())) {
283 profile = profile->GetOffTheRecordProfile();
284 } else if (command_line.HasSwitch(switches::kIncognito)) {
285 LOG(WARNING) << "Incognito mode disabled by policy, launching a normal "
286 << "browser session.";
289 // Note: This check should have been done in ProcessCmdLineImpl()
290 // before calling this function. However chromeos/login/login_utils.cc
291 // calls this function directly (see comments there) so it has to be checked
293 const bool silent_launch = command_line.HasSwitch(switches::kSilentLaunch);
295 if (!silent_launch) {
296 StartupBrowserCreatorImpl lwp(cur_dir, command_line, this, is_first_run);
297 const std::vector<GURL> urls_to_launch =
298 GetURLsFromCommandLine(command_line, cur_dir, profile);
299 chrome::HostDesktopType host_desktop_type =
300 chrome::HOST_DESKTOP_TYPE_NATIVE;
302 #if defined(OS_WIN) && defined(USE_ASH)
303 // We want to maintain only one type of instance for now, either ASH
305 // TODO(shrikant): Remove this code once we decide on running both desktop
306 // and ASH instances side by side.
307 if (ash::Shell::HasInstance())
308 host_desktop_type = chrome::HOST_DESKTOP_TYPE_ASH;
311 const bool launched = lwp.Launch(profile, urls_to_launch,
312 in_synchronous_profile_launch_,
314 in_synchronous_profile_launch_ = false;
316 LOG(ERROR) << "launch error";
318 *return_code = chrome::RESULT_CODE_INVALID_CMDLINE_URL;
322 in_synchronous_profile_launch_ = false;
325 profile_launch_observer.Get().AddLaunched(profile);
327 #if defined(OS_CHROMEOS)
328 g_browser_process->platform_part()->profile_helper()->ProfileStartup(
336 bool StartupBrowserCreator::WasRestarted() {
337 // Stores the value of the preference kWasRestarted had when it was read.
338 static bool was_restarted = false;
340 if (!was_restarted_read_) {
341 PrefService* pref_service = g_browser_process->local_state();
342 was_restarted = pref_service->GetBoolean(prefs::kWasRestarted);
343 pref_service->SetBoolean(prefs::kWasRestarted, false);
344 was_restarted_read_ = true;
346 return was_restarted;
350 SessionStartupPref StartupBrowserCreator::GetSessionStartupPref(
351 const CommandLine& command_line,
354 PrefService* prefs = profile->GetPrefs();
355 SessionStartupPref pref = SessionStartupPref::GetStartupPref(prefs);
357 // IsChromeFirstRun() looks for a sentinel file to determine whether the user
358 // is starting Chrome for the first time. On Chrome OS, the sentinel is stored
359 // in a location shared by all users and the check is meaningless. Query the
360 // UserManager instead to determine whether the user is new.
361 #if defined(OS_CHROMEOS)
362 const bool is_first_run = chromeos::UserManager::Get()->IsCurrentUserNew();
364 const bool is_first_run = first_run::IsChromeFirstRun();
367 // The pref has an OS-dependent default value. For the first run only, this
368 // default is overridden with SessionStartupPref::DEFAULT so that first run
369 // behavior (sync promo, welcome page) is consistently invoked.
370 // This applies only if the pref is still at its default and has not been
371 // set by the user, managed prefs or policy.
372 if (is_first_run && SessionStartupPref::TypeIsDefault(prefs))
373 pref.type = SessionStartupPref::DEFAULT;
375 // The switches::kRestoreLastSession command line switch is used to restore
376 // sessions after a browser self restart (e.g. after a Chrome upgrade).
377 // However, new profiles can be created from a browser process that has this
378 // switch so do not set the session pref to SessionStartupPref::LAST for
379 // those as there is nothing to restore.
380 if ((command_line.HasSwitch(switches::kRestoreLastSession) ||
381 StartupBrowserCreator::WasRestarted()) &&
382 !profile->IsNewProfile()) {
383 pref.type = SessionStartupPref::LAST;
385 if (pref.type == SessionStartupPref::LAST &&
386 IncognitoModePrefs::ShouldLaunchIncognito(command_line, prefs)) {
387 // We don't store session information when incognito. If the user has
388 // chosen to restore last session and launched incognito, fallback to
389 // default launch behavior.
390 pref.type = SessionStartupPref::DEFAULT;
397 void StartupBrowserCreator::ClearLaunchedProfilesForTesting() {
398 profile_launch_observer.Get().Clear();
402 std::vector<GURL> StartupBrowserCreator::GetURLsFromCommandLine(
403 const CommandLine& command_line,
404 const base::FilePath& cur_dir,
406 std::vector<GURL> urls;
408 const CommandLine::StringVector& params = command_line.GetArgs();
409 for (size_t i = 0; i < params.size(); ++i) {
410 base::FilePath param = base::FilePath(params[i]);
411 // Handle Vista way of searching - "? <search-term>"
412 if ((param.value().size() > 2) && (param.value()[0] == '?') &&
413 (param.value()[1] == ' ')) {
414 GURL url(GetDefaultSearchURLForSearchTerms(
415 profile, param.LossyDisplayName().substr(2)));
416 if (url.is_valid()) {
422 // Otherwise, fall through to treating it as a URL.
424 // This will create a file URL or a regular URL.
425 // This call can (in rare circumstances) block the UI thread.
426 // Allow it until this bug is fixed.
427 // http://code.google.com/p/chromium/issues/detail?id=60641
428 GURL url = GURL(param.MaybeAsASCII());
429 // http://crbug.com/371030: Only use URLFixerUpper if we don't have a valid
430 // URL, otherwise we will look in the current directory for a file named
431 // 'about' if the browser was started with a about:foo argument.
432 if (!url.is_valid()) {
433 base::ThreadRestrictions::ScopedAllowIO allow_io;
434 url = URLFixerUpper::FixupRelativeFile(cur_dir, param);
436 // Exclude dangerous schemes.
437 if (url.is_valid()) {
438 ChildProcessSecurityPolicy* policy =
439 ChildProcessSecurityPolicy::GetInstance();
440 if (policy->IsWebSafeScheme(url.scheme()) ||
441 url.SchemeIs(content::kFileScheme) ||
442 #if defined(OS_CHROMEOS)
443 // In ChromeOS, allow a settings page to be specified on the
444 // command line. See ExistingUserController::OnLoginSuccess.
445 (url.spec().find(chrome::kChromeUISettingsURL) == 0) ||
447 (url.spec().compare(content::kAboutBlankURL) == 0)) {
456 bool StartupBrowserCreator::ProcessCmdLineImpl(
457 const CommandLine& command_line,
458 const base::FilePath& cur_dir,
459 bool process_startup,
460 Profile* last_used_profile,
461 const Profiles& last_opened_profiles,
463 StartupBrowserCreator* browser_creator) {
464 DCHECK(last_used_profile);
465 if (process_startup) {
466 if (command_line.HasSwitch(switches::kDisablePromptOnRepost))
467 content::NavigationController::DisablePromptOnRepost();
470 bool silent_launch = false;
472 #if defined(ENABLE_FULL_PRINTING)
473 // If we are just displaying a print dialog we shouldn't open browser
475 if (command_line.HasSwitch(switches::kCloudPrintFile) &&
476 print_dialog_cloud::CreatePrintDialogFromCommandLine(last_used_profile,
478 silent_launch = true;
481 // If we are checking the proxy enabled policy, don't open any windows.
482 if (command_line.HasSwitch(switches::kCheckCloudPrintConnectorPolicy)) {
483 silent_launch = true;
484 if (CloudPrintProxyServiceFactory::GetForProfile(last_used_profile)->
485 EnforceCloudPrintConnectorPolicyAndQuit())
486 // Success, nothing more needs to be done, so return false to stop
487 // launching and quit.
490 #endif // defined(ENABLE_FULL_PRINTING)
492 if (command_line.HasSwitch(switches::kExplicitlyAllowedPorts)) {
493 std::string allowed_ports =
494 command_line.GetSwitchValueASCII(switches::kExplicitlyAllowedPorts);
495 net::SetExplicitlyAllowedPorts(allowed_ports);
498 if (command_line.HasSwitch(switches::kInstallFromWebstore)) {
499 extensions::StartupHelper helper;
500 helper.InstallFromWebstore(command_line, last_used_profile);
501 // Nothing more needs to be done, so return false to stop launching and
506 if (command_line.HasSwitch(switches::kValidateCrx)) {
507 if (!process_startup) {
508 LOG(ERROR) << "chrome is already running; you must close all running "
509 << "instances before running with the --"
510 << switches::kValidateCrx << " flag";
513 extensions::StartupHelper helper;
516 if (helper.ValidateCrx(command_line, &error))
517 message = std::string("ValidateCrx Success");
519 message = std::string("ValidateCrx Failure: ") + error;
520 printf("%s\n", message.c_str());
524 if (command_line.HasSwitch(switches::kLimitedInstallFromWebstore)) {
525 extensions::StartupHelper helper;
526 helper.LimitedInstallFromWebstore(command_line, last_used_profile,
527 base::Bind(&base::DoNothing));
530 #if defined(OS_CHROMEOS)
531 // The browser will be launched after the user logs in.
532 if (command_line.HasSwitch(chromeos::switches::kLoginManager) ||
533 command_line.HasSwitch(chromeos::switches::kLoginPassword)) {
534 silent_launch = true;
537 if (chrome::IsRunningInAppMode() &&
538 command_line.HasSwitch(switches::kAppId)) {
539 chromeos::LaunchAppOrDie(
541 command_line.GetSwitchValueASCII(switches::kAppId));
543 // Skip browser launch since app mode launches its app window.
544 silent_launch = true;
547 // If we are a demo app session and we crashed, there is no safe recovery
548 // possible. We should instead cleanly exit and go back to the OOBE screen,
549 // where we will launch again after the timeout has expired.
550 if (chromeos::DemoAppLauncher::IsDemoAppSession(
551 command_line.GetSwitchValueASCII(chromeos::switches::kLoginUser))) {
552 chrome::AttemptUserExit();
557 #if defined(TOOLKIT_VIEWS) && defined(USE_X11)
558 ui::TouchFactory::SetTouchDeviceListFromCommandLine();
561 if (!process_startup &&
562 command_line.HasSwitch(switches::kDumpBrowserHistograms)) {
563 // Only handle --dump-browser-histograms from a rendezvous. In this case, do
564 // not open a new browser window even if no output file was given.
565 base::FilePath output_file(
566 command_line.GetSwitchValuePath(switches::kDumpBrowserHistograms));
567 if (!output_file.empty()) {
568 BrowserThread::PostBlockingPoolTask(
570 base::Bind(&DumpBrowserHistograms, output_file));
572 silent_launch = true;
575 // If we don't want to launch a new browser window or tab we are done here.
579 // Check for --load-and-launch-app.
580 if (command_line.HasSwitch(apps::kLoadAndLaunchApp) &&
581 !IncognitoModePrefs::ShouldLaunchIncognito(
582 command_line, last_used_profile->GetPrefs())) {
583 CommandLine::StringType path = command_line.GetSwitchValueNative(
584 apps::kLoadAndLaunchApp);
586 if (!apps::AppLoadService::Get(last_used_profile)->LoadAndLaunch(
587 base::FilePath(path), command_line, cur_dir)) {
591 // Return early here since we don't want to open a browser window.
592 // The exception is when there are no browser windows, since we don't want
593 // chrome to shut down.
594 // TODO(jackhou): Do this properly once keep-alive is handled by the
595 // background page of apps. Tracked at http://crbug.com/175381
596 if (chrome::GetTotalBrowserCountForProfile(last_used_profile) != 0)
600 chrome::startup::IsProcessStartup is_process_startup = process_startup ?
601 chrome::startup::IS_PROCESS_STARTUP :
602 chrome::startup::IS_NOT_PROCESS_STARTUP;
603 chrome::startup::IsFirstRun is_first_run = first_run::IsChromeFirstRun() ?
604 chrome::startup::IS_FIRST_RUN : chrome::startup::IS_NOT_FIRST_RUN;
605 // |last_opened_profiles| will be empty in the following circumstances:
606 // - This is the first launch. |last_used_profile| is the initial profile.
607 // - The user exited the browser by closing all windows for all
608 // profiles. |last_used_profile| is the profile which owned the last open
610 // - Only incognito windows were open when the browser exited.
611 // |last_used_profile| is the last used incognito profile. Restoring it will
612 // create a browser window for the corresponding original profile.
613 if (last_opened_profiles.empty()) {
614 // If the last used profile is locked or was a guest, show the user manager.
615 if (switches::IsNewProfileManagement()) {
616 ProfileInfoCache& profile_info =
617 g_browser_process->profile_manager()->GetProfileInfoCache();
618 size_t profile_index = profile_info.GetIndexOfProfileWithPath(
619 last_used_profile->GetPath());
620 bool signin_required = profile_index != std::string::npos &&
621 profile_info.ProfileIsSigninRequiredAtIndex(profile_index);
622 if (signin_required || last_used_profile->IsGuestSession()) {
623 chrome::ShowUserManager(base::FilePath());
627 if (!browser_creator->LaunchBrowser(command_line, last_used_profile,
628 cur_dir, is_process_startup,
629 is_first_run, return_code)) {
633 // Launch the last used profile with the full command line, and the other
634 // opened profiles without the URLs to launch.
635 CommandLine command_line_without_urls(command_line.GetProgram());
636 const CommandLine::SwitchMap& switches = command_line.GetSwitches();
637 for (CommandLine::SwitchMap::const_iterator switch_it = switches.begin();
638 switch_it != switches.end(); ++switch_it) {
639 command_line_without_urls.AppendSwitchNative(switch_it->first,
642 // Launch the profiles in the order they became active.
643 for (Profiles::const_iterator it = last_opened_profiles.begin();
644 it != last_opened_profiles.end(); ++it) {
645 // Don't launch additional profiles which would only open a new tab
646 // page. When restarting after an update, all profiles will reopen last
648 SessionStartupPref startup_pref =
649 GetSessionStartupPref(command_line, *it);
650 if (*it != last_used_profile &&
651 startup_pref.type == SessionStartupPref::DEFAULT &&
652 !HasPendingUncleanExit(*it))
655 // Don't re-open a browser window for the guest profile.
656 if (switches::IsNewProfileManagement() &&
657 (*it)->IsGuestSession())
660 if (!browser_creator->LaunchBrowser((*it == last_used_profile) ?
661 command_line : command_line_without_urls, *it, cur_dir,
662 is_process_startup, is_first_run, return_code))
664 // We've launched at least one browser.
665 is_process_startup = chrome::startup::IS_NOT_PROCESS_STARTUP;
667 // This must be done after all profiles have been launched so the observer
668 // knows about all profiles to wait for before activating this one.
670 // If the last used profile was the guest one, we didn't open it so
671 // we don't need to activate it either.
672 if (!switches::IsNewProfileManagement() &&
673 !last_used_profile->IsGuestSession())
674 profile_launch_observer.Get().set_profile_to_activate(last_used_profile);
680 void StartupBrowserCreator::ProcessCommandLineOnProfileCreated(
681 const CommandLine& command_line,
682 const base::FilePath& cur_dir,
684 Profile::CreateStatus status) {
685 if (status == Profile::CREATE_STATUS_INITIALIZED)
686 ProcessCmdLineImpl(command_line, cur_dir, false, profile, Profiles(), NULL,
691 void StartupBrowserCreator::ProcessCommandLineAlreadyRunning(
692 const CommandLine& command_line,
693 const base::FilePath& cur_dir,
694 const base::FilePath& profile_path) {
695 ProfileManager* profile_manager = g_browser_process->profile_manager();
696 Profile* profile = profile_manager->GetProfileByPath(profile_path);
698 // The profile isn't loaded yet and so needs to be loaded asynchronously.
700 profile_manager->CreateProfileAsync(profile_path,
701 base::Bind(&StartupBrowserCreator::ProcessCommandLineOnProfileCreated,
702 command_line, cur_dir), base::string16(), base::string16(),
707 ProcessCmdLineImpl(command_line, cur_dir, false, profile, Profiles(), NULL,
712 bool StartupBrowserCreator::ActivatedProfile() {
713 return profile_launch_observer.Get().activated_profile();
716 bool HasPendingUncleanExit(Profile* profile) {
717 return profile->GetLastSessionExitType() == Profile::EXIT_CRASHED &&
718 !profile_launch_observer.Get().HasBeenLaunched(profile);
721 base::FilePath GetStartupProfilePath(const base::FilePath& user_data_dir,
722 const CommandLine& command_line) {
723 // If we are showing the app list then chrome isn't shown so load the app
724 // list's profile rather than chrome's.
725 if (command_line.HasSwitch(switches::kShowAppList)) {
726 return AppListService::Get(chrome::HOST_DESKTOP_TYPE_NATIVE)->
727 GetProfilePath(user_data_dir);
730 if (command_line.HasSwitch(switches::kProfileDirectory)) {
731 return user_data_dir.Append(
732 command_line.GetSwitchValuePath(switches::kProfileDirectory));
735 return g_browser_process->profile_manager()->GetLastUsedProfileDir(