15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/startup/startup_browser_creator.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <algorithm>   // For max().
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <set>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "apps/app_load_service.h"
11868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "apps/switches.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/bind.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/bind_helpers.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/command_line.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/compiler_specific.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/environment.h"
172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/files/file_path.h"
181320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "base/files/file_util.h"
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/lazy_instance.h"
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/logging.h"
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/metrics/histogram.h"
23f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "base/metrics/statistics_recorder.h"
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/path_service.h"
252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/prefs/pref_service.h"
262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/strings/string_number_conversions.h"
272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/strings/string_split.h"
28868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/utf_string_conversions.h"
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/threading/thread_restrictions.h"
302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/app_mode/app_mode_utils.h"
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/auto_launch_trial.h"
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/browser_process.h"
337dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "chrome/browser/chrome_notification_types.h"
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/custom_handlers/protocol_handler_registry.h"
352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/extensions/startup_helper.h"
362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/extensions/unpacked_installer.h"
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/first_run/first_run.h"
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/notifications/desktop_notification_service.h"
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/prefs/incognito_mode_prefs.h"
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/prefs/session_startup_pref.h"
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/profiles/profile.h"
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/profiles/profile_manager.h"
43f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "chrome/browser/profiles/profiles_state.h"
44116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "chrome/browser/search_engines/template_url_service_factory.h"
455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/ui/app_list/app_list_service.h"
462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/browser.h"
47f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "chrome/browser/ui/browser_dialogs.h"
482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/browser_finder.h"
492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/browser_window.h"
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/startup/startup_browser_creator_impl.h"
511320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "chrome/browser/ui/user_manager.h"
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/chrome_constants.h"
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/chrome_paths.h"
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/chrome_result_codes.h"
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/chrome_switches.h"
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/chrome_version_info.h"
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/pref_names.h"
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/url_constants.h"
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/installer/util/browser_distribution.h"
606d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)#include "components/google/core/browser/google_util.h"
61116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "components/search_engines/util.h"
620529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "components/signin/core/common/profile_management_switches.h"
63f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "components/url_fixer/url_fixer.h"
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/browser_thread.h"
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/child_process_security_policy.h"
6690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "content/public/browser/navigation_controller.h"
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/net_util.h"
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
69a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#if defined(USE_ASH)
70a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "ash/shell.h"
71a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#endif
72a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_CHROMEOS)
743551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#include "chrome/browser/chromeos/app_mode/app_launch_utils.h"
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/chromeos/kiosk_mode/kiosk_mode_settings.h"
765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/chromeos/login/demo_mode/demo_app_launcher.h"
77c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/browser/chromeos/profiles/profile_helper.h"
785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/lifetime/application_lifetime.h"
79c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chromeos/chromeos_switches.h"
806e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "components/user_manager/user_manager.h"
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(TOOLKIT_VIEWS) && defined(OS_LINUX)
8468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)#include "ui/events/x/touch_factory_x11.h"
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
87f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#if defined(OS_MACOSX)
88f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "chrome/browser/web_applications/web_app_mac.h"
89f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#endif
90f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
91c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch#if defined(ENABLE_FULL_PRINTING)
92c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch#include "chrome/browser/printing/cloud_print/cloud_print_proxy_service.h"
93c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch#include "chrome/browser/printing/cloud_print/cloud_print_proxy_service_factory.h"
94c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch#include "chrome/browser/printing/print_dialog_cloud.h"
95c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch#endif
96c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using content::BrowserThread;
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using content::ChildProcessSecurityPolicy;
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace {
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Keeps track on which profiles have been launched.
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class ProfileLaunchObserver : public content::NotificationObserver {
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
1052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ProfileLaunchObserver()
1062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      : profile_to_activate_(NULL),
1072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        activated_profile_(false) {
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    registrar_.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED,
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                   content::NotificationService::AllSources());
1102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    registrar_.Add(this, chrome::NOTIFICATION_BROWSER_WINDOW_READY,
1112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                   content::NotificationService::AllSources());
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~ProfileLaunchObserver() {}
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void Observe(int type,
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       const content::NotificationSource& source,
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       const content::NotificationDetails& details) OVERRIDE {
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    switch (type) {
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      case chrome::NOTIFICATION_PROFILE_DESTROYED: {
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        Profile* profile = content::Source<Profile>(source).ptr();
1212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        launched_profiles_.erase(profile);
1222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        opened_profiles_.erase(profile);
1232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        if (profile == profile_to_activate_)
1242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          profile_to_activate_ = NULL;
1252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        // If this profile was the last launched one without an opened window,
1262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        // then we may be ready to activate |profile_to_activate_|.
1272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        MaybeActivateProfile();
1282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        break;
1292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      }
1302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      case chrome::NOTIFICATION_BROWSER_WINDOW_READY: {
1312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        Browser* browser = content::Source<Browser>(source).ptr();
1322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        DCHECK(browser);
1332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        opened_profiles_.insert(browser->profile());
1342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        MaybeActivateProfile();
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        break;
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      default:
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        NOTREACHED();
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool HasBeenLaunched(const Profile* profile) const {
1432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return launched_profiles_.find(profile) != launched_profiles_.end();
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void AddLaunched(Profile* profile) {
1472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    launched_profiles_.insert(profile);
1482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Since the startup code only executes for browsers launched in
1492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // desktop mode, i.e., HOST_DESKTOP_TYPE_NATIVE. Ash should never get here.
1502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (chrome::FindBrowserWithProfile(profile,
1512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                       chrome::HOST_DESKTOP_TYPE_NATIVE)) {
1522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // A browser may get opened before we get initialized (e.g., in tests),
1532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // so we never see the NOTIFICATION_BROWSER_WINDOW_READY for it.
1542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      opened_profiles_.insert(profile);
1552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
1562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
1572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void Clear() {
1592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    launched_profiles_.clear();
1602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    opened_profiles_.clear();
1612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
1622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool activated_profile() { return activated_profile_; }
1642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void set_profile_to_activate(Profile* profile) {
1662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    profile_to_activate_ = profile;
1672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    MaybeActivateProfile();
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
1712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void MaybeActivateProfile() {
1722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (!profile_to_activate_)
1732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return;
1742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Check that browsers have been opened for all the launched profiles.
1752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Note that browsers opened for profiles that were not added as launched
1762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // profiles are simply ignored.
1772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    std::set<const Profile*>::const_iterator i = launched_profiles_.begin();
1782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    for (; i != launched_profiles_.end(); ++i) {
1792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      if (opened_profiles_.find(*i) == opened_profiles_.end())
1802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        return;
1812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
1822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Asynchronous post to give a chance to the last window to completely
1832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // open and activate before trying to activate |profile_to_activate_|.
1842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    BrowserThread::PostTask(
1852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        BrowserThread::UI, FROM_HERE,
1862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        base::Bind(&ProfileLaunchObserver::ActivateProfile,
1872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                   base::Unretained(this)));
188a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    // Avoid posting more than once before ActivateProfile gets called.
1892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    registrar_.Remove(this, chrome::NOTIFICATION_BROWSER_WINDOW_READY,
1902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                      content::NotificationService::AllSources());
191a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    registrar_.Remove(this, chrome::NOTIFICATION_PROFILE_DESTROYED,
192a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)                      content::NotificationService::AllSources());
1932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
1942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void ActivateProfile() {
1962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // We need to test again, in case the profile got deleted in the mean time.
1972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (profile_to_activate_) {
1982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      Browser* browser = chrome::FindBrowserWithProfile(
1992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          profile_to_activate_, chrome::HOST_DESKTOP_TYPE_NATIVE);
2002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // |profile| may never get launched, e.g., if it only had
2012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // incognito Windows and one of them was used to exit Chrome.
2022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // So it won't have a browser in that case.
2032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      if (browser)
2042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        browser->window()->Activate();
2052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // No need try to activate this profile again.
2062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      profile_to_activate_ = NULL;
2072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
2082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Assign true here, even if no browser was actually activated, so that
2092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // the test can stop waiting, and fail gracefully when needed.
2102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    activated_profile_ = true;
2112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
2122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // These are the profiles that get launched by
2142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // StartupBrowserCreator::LaunchBrowser.
2152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::set<const Profile*> launched_profiles_;
2162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // These are the profiles for which at least one browser window has been
2172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // opened. This is needed to know when it is safe to activate
2182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // |profile_to_activate_|, otherwise, new browser windows being opened will
2192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // be activated on top of it.
2202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::set<const Profile*> opened_profiles_;
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  content::NotificationRegistrar registrar_;
2222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // This is NULL until the profile to activate has been chosen. This value,
2232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // should only be set once all profiles have been launched, otherwise,
2242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // activation may not happen after the launch of newer profiles.
2252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  Profile* profile_to_activate_;
2262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Set once we attempted to activate a profile. We only get one shot at this.
2272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool activated_profile_;
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(ProfileLaunchObserver);
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)base::LazyInstance<ProfileLaunchObserver> profile_launch_observer =
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LAZY_INSTANCE_INITIALIZER;
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
235f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// Dumps the current set of the browser process's histograms to |output_file|.
236f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// The file is overwritten if it exists. This function should only be called in
237f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// the blocking pool.
238f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void DumpBrowserHistograms(const base::FilePath& output_file) {
239f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  base::ThreadRestrictions::AssertIOAllowed();
240f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
241f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  std::string output_string(base::StatisticsRecorder::ToJSON(std::string()));
242a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  base::WriteFile(output_file, output_string.data(),
243a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                  static_cast<int>(output_string.size()));
244f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
245f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)StartupBrowserCreator::StartupBrowserCreator()
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    : is_default_browser_dialog_suppressed_(false),
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      show_main_browser_window_(true) {
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)StartupBrowserCreator::~StartupBrowserCreator() {}
2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool StartupBrowserCreator::was_restarted_read_ = false;
2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// static
2592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool StartupBrowserCreator::in_synchronous_profile_launch_ = false;
2602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void StartupBrowserCreator::AddFirstRunTab(const GURL& url) {
2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  first_run_tabs_.push_back(url);
2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool StartupBrowserCreator::InSynchronousProfileLaunch() {
2672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return in_synchronous_profile_launch_;
2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool StartupBrowserCreator::LaunchBrowser(
2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const CommandLine& command_line,
2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    Profile* profile,
2732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const base::FilePath& cur_dir,
2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    chrome::startup::IsProcessStartup process_startup,
2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    chrome::startup::IsFirstRun is_first_run,
2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int* return_code) {
27790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
2782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  in_synchronous_profile_launch_ =
2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      process_startup == chrome::startup::IS_PROCESS_STARTUP;
2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(profile);
2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Continue with the incognito profile from here on if Incognito mode
2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // is forced.
2845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (IncognitoModePrefs::ShouldLaunchIncognito(command_line,
2855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                                profile->GetPrefs())) {
2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    profile = profile->GetOffTheRecordProfile();
2875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else if (command_line.HasSwitch(switches::kIncognito)) {
2885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LOG(WARNING) << "Incognito mode disabled by policy, launching a normal "
2895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                 << "browser session.";
2905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
29290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Note: This check should have been done in ProcessCmdLineImpl()
29390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // before calling this function. However chromeos/login/login_utils.cc
29490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // calls this function directly (see comments there) so it has to be checked
29590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // again.
29690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  const bool silent_launch = command_line.HasSwitch(switches::kSilentLaunch);
29790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
29890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (!silent_launch) {
29990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    StartupBrowserCreatorImpl lwp(cur_dir, command_line, this, is_first_run);
30090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    const std::vector<GURL> urls_to_launch =
30190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        GetURLsFromCommandLine(command_line, cur_dir, profile);
302a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    chrome::HostDesktopType host_desktop_type =
303a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)        chrome::HOST_DESKTOP_TYPE_NATIVE;
304a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
305cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#if defined(USE_ASH) && !defined(OS_CHROMEOS)
306a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    // We want to maintain only one type of instance for now, either ASH
307a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    // or desktop.
308a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    // TODO(shrikant): Remove this code once we decide on running both desktop
309a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    // and ASH instances side by side.
310a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    if (ash::Shell::HasInstance())
311a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      host_desktop_type = chrome::HOST_DESKTOP_TYPE_ASH;
312a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#endif
313a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
31490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    const bool launched = lwp.Launch(profile, urls_to_launch,
31590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                               in_synchronous_profile_launch_,
316a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                               host_desktop_type);
31790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    in_synchronous_profile_launch_ = false;
31890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    if (!launched) {
31990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      LOG(ERROR) << "launch error";
32090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      if (return_code)
32190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        *return_code = chrome::RESULT_CODE_INVALID_CMDLINE_URL;
32290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      return false;
32390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    }
32490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  } else {
32590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    in_synchronous_profile_launch_ = false;
3265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
32790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
3285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  profile_launch_observer.Get().AddLaunched(profile);
3295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_CHROMEOS)
331116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  chromeos::ProfileHelper::Get()->ProfileStartup(profile, process_startup);
3325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
3335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return true;
3345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
3375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool StartupBrowserCreator::WasRestarted() {
3385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Stores the value of the preference kWasRestarted had when it was read.
3395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static bool was_restarted = false;
3405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!was_restarted_read_) {
3425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    PrefService* pref_service = g_browser_process->local_state();
3435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    was_restarted = pref_service->GetBoolean(prefs::kWasRestarted);
3445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    pref_service->SetBoolean(prefs::kWasRestarted, false);
3455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    was_restarted_read_ = true;
3465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return was_restarted;
3485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
3515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)SessionStartupPref StartupBrowserCreator::GetSessionStartupPref(
3525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const CommandLine& command_line,
3535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    Profile* profile) {
3545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(profile);
3555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PrefService* prefs = profile->GetPrefs();
3565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SessionStartupPref pref = SessionStartupPref::GetStartupPref(prefs);
3575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // IsChromeFirstRun() looks for a sentinel file to determine whether the user
3592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // is starting Chrome for the first time. On Chrome OS, the sentinel is stored
3602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // in a location shared by all users and the check is meaningless. Query the
3612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // UserManager instead to determine whether the user is new.
3622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#if defined(OS_CHROMEOS)
3636e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  const bool is_first_run =
3646e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      user_manager::UserManager::Get()->IsCurrentUserNew();
3652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#else
3662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const bool is_first_run = first_run::IsChromeFirstRun();
3672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif
3682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The pref has an OS-dependent default value. For the first run only, this
3705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // default is overridden with SessionStartupPref::DEFAULT so that first run
3715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // behavior (sync promo, welcome page) is consistently invoked.
3725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This applies only if the pref is still at its default and has not been
3735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // set by the user, managed prefs or policy.
3742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (is_first_run && SessionStartupPref::TypeIsDefault(prefs))
3755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    pref.type = SessionStartupPref::DEFAULT;
3765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
37790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // The switches::kRestoreLastSession command line switch is used to restore
37890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // sessions after a browser self restart (e.g. after a Chrome upgrade).
37990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // However, new profiles can be created from a browser process that has this
38090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // switch so do not set the session pref to SessionStartupPref::LAST for
38190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // those as there is nothing to restore.
38290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if ((command_line.HasSwitch(switches::kRestoreLastSession) ||
38390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)       StartupBrowserCreator::WasRestarted()) &&
38490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      !profile->IsNewProfile()) {
3855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    pref.type = SessionStartupPref::LAST;
3865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (pref.type == SessionStartupPref::LAST &&
3885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      IncognitoModePrefs::ShouldLaunchIncognito(command_line, prefs)) {
3895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // We don't store session information when incognito. If the user has
3905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // chosen to restore last session and launched incognito, fallback to
3915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // default launch behavior.
3925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    pref.type = SessionStartupPref::DEFAULT;
3935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return pref;
3965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// static
3992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void StartupBrowserCreator::ClearLaunchedProfilesForTesting() {
4002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  profile_launch_observer.Get().Clear();
4012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
4022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// static
4045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)std::vector<GURL> StartupBrowserCreator::GetURLsFromCommandLine(
4055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const CommandLine& command_line,
4062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const base::FilePath& cur_dir,
4075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    Profile* profile) {
4085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::vector<GURL> urls;
4095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
410eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  const CommandLine::StringVector& params = command_line.GetArgs();
4115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (size_t i = 0; i < params.size(); ++i) {
4122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    base::FilePath param = base::FilePath(params[i]);
4135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Handle Vista way of searching - "? <search-term>"
414eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    if ((param.value().size() > 2) && (param.value()[0] == '?') &&
415eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        (param.value()[1] == ' ')) {
416eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      GURL url(GetDefaultSearchURLForSearchTerms(
417116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch          TemplateURLServiceFactory::GetForProfile(profile),
418116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch          param.LossyDisplayName().substr(2)));
419eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      if (url.is_valid()) {
420eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        urls.push_back(url);
4215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        continue;
4225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
4235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
4245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Otherwise, fall through to treating it as a URL.
4265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // This will create a file URL or a regular URL.
4285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // This call can (in rare circumstances) block the UI thread.
4295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Allow it until this bug is fixed.
4305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    //  http://code.google.com/p/chromium/issues/detail?id=60641
431010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    GURL url = GURL(param.MaybeAsASCII());
432010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    // http://crbug.com/371030: Only use URLFixerUpper if we don't have a valid
433010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    // URL, otherwise we will look in the current directory for a file named
434010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    // 'about' if the browser was started with a about:foo argument.
435010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    if (!url.is_valid()) {
4365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      base::ThreadRestrictions::ScopedAllowIO allow_io;
437f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      url = url_fixer::FixupRelativeFile(cur_dir, param);
4385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
4395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Exclude dangerous schemes.
4405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (url.is_valid()) {
4412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      ChildProcessSecurityPolicy* policy =
4425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          ChildProcessSecurityPolicy::GetInstance();
4435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (policy->IsWebSafeScheme(url.scheme()) ||
444cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          url.SchemeIs(url::kFileScheme) ||
4455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_CHROMEOS)
4465f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)          // In ChromeOS, allow any settings page to be specified on the command
4475f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)          // line. See ExistingUserController::OnLoginSuccess.
4485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          (url.spec().find(chrome::kChromeUISettingsURL) == 0) ||
4495f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#else
4505f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)          ((url.spec().find(std::string(chrome::kChromeUISettingsURL) +
4515f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                            chrome::kResetProfileSettingsSubPage) == 0)) ||
4525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
453f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)          (url.spec().compare(url::kAboutBlankURL) == 0)) {
4545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        urls.push_back(url);
4555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
4565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
4575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return urls;
4595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
4625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool StartupBrowserCreator::ProcessCmdLineImpl(
4635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const CommandLine& command_line,
4642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const base::FilePath& cur_dir,
4655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool process_startup,
4665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    Profile* last_used_profile,
4675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const Profiles& last_opened_profiles,
4685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int* return_code,
4695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    StartupBrowserCreator* browser_creator) {
4705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(last_used_profile);
4715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (process_startup) {
4725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (command_line.HasSwitch(switches::kDisablePromptOnRepost))
4735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      content::NavigationController::DisablePromptOnRepost();
4745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool silent_launch = false;
4775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
478c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch#if defined(ENABLE_FULL_PRINTING)
4795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If we are just displaying a print dialog we shouldn't open browser
4805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // windows.
4815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (command_line.HasSwitch(switches::kCloudPrintFile) &&
4825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      print_dialog_cloud::CreatePrintDialogFromCommandLine(last_used_profile,
4835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                                           command_line)) {
4845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    silent_launch = true;
4855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If we are checking the proxy enabled policy, don't open any windows.
4885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (command_line.HasSwitch(switches::kCheckCloudPrintConnectorPolicy)) {
4895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    silent_launch = true;
4905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (CloudPrintProxyServiceFactory::GetForProfile(last_used_profile)->
4915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        EnforceCloudPrintConnectorPolicyAndQuit())
4925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // Success, nothing more needs to be done, so return false to stop
4935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // launching and quit.
4945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return false;
4955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
496c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch#endif  // defined(ENABLE_FULL_PRINTING)
4975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (command_line.HasSwitch(switches::kExplicitlyAllowedPorts)) {
4995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    std::string allowed_ports =
5005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        command_line.GetSwitchValueASCII(switches::kExplicitlyAllowedPorts);
5015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    net::SetExplicitlyAllowedPorts(allowed_ports);
5025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
5035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
50434680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)  if (command_line.HasSwitch(switches::kInstallEphemeralAppFromWebstore)) {
5052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    extensions::StartupHelper helper;
50634680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)    helper.InstallEphemeralApp(command_line, last_used_profile);
5072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Nothing more needs to be done, so return false to stop launching and
5082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // quit.
5092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return false;
5102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
5112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
512c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (command_line.HasSwitch(switches::kValidateCrx)) {
513c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (!process_startup) {
514c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      LOG(ERROR) << "chrome is already running; you must close all running "
515c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                 << "instances before running with the --"
516c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                 << switches::kValidateCrx << " flag";
517c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return false;
518c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
519c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    extensions::StartupHelper helper;
520c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    std::string message;
521c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    std::string error;
522c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (helper.ValidateCrx(command_line, &error))
523c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      message = std::string("ValidateCrx Success");
524c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    else
525c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      message = std::string("ValidateCrx Failure: ") + error;
526c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    printf("%s\n", message.c_str());
527c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return false;
528c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
529c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
5305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_CHROMEOS)
5311320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
5321320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#if defined(USE_ATHENA)
5331320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // Athena will never launch browser.
5341320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  silent_launch = true;
5351320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#endif
5361320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
5375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The browser will be launched after the user logs in.
538116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (command_line.HasSwitch(chromeos::switches::kLoginManager))
5395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    silent_launch = true;
5402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (chrome::IsRunningInAppMode() &&
5422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      command_line.HasSwitch(switches::kAppId)) {
5433551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    chromeos::LaunchAppOrDie(
5442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        last_used_profile,
5453551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)        command_line.GetSwitchValueASCII(switches::kAppId));
5462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Skip browser launch since app mode launches its app window.
5482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    silent_launch = true;
5492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
5505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
5515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // If we are a demo app session and we crashed, there is no safe recovery
5525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // possible. We should instead cleanly exit and go back to the OOBE screen,
5535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // where we will launch again after the timeout has expired.
5545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (chromeos::DemoAppLauncher::IsDemoAppSession(
5555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      command_line.GetSwitchValueASCII(chromeos::switches::kLoginUser))) {
5565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    chrome::AttemptUserExit();
5575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return false;
5585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
5591320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#endif  // OS_CHROMEOS
5605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
561c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#if defined(TOOLKIT_VIEWS) && defined(USE_X11)
5625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ui::TouchFactory::SetTouchDeviceListFromCommandLine();
5635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
5645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
565f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#if defined(OS_MACOSX)
566f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  if (web_app::MaybeRebuildShortcut(command_line))
567f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    return true;
568f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#endif
569f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
570f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (!process_startup &&
571f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      command_line.HasSwitch(switches::kDumpBrowserHistograms)) {
572f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // Only handle --dump-browser-histograms from a rendezvous. In this case, do
573f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // not open a new browser window even if no output file was given.
574f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    base::FilePath output_file(
575f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        command_line.GetSwitchValuePath(switches::kDumpBrowserHistograms));
576f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    if (!output_file.empty()) {
577f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      BrowserThread::PostBlockingPoolTask(
578f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)          FROM_HERE,
579f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)          base::Bind(&DumpBrowserHistograms, output_file));
580f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    }
581f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    silent_launch = true;
582f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
583f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
584e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  // If we don't want to launch a new browser window or tab we are done here.
5852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (silent_launch)
5862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return true;
5872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Check for --load-and-launch-app.
589868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (command_line.HasSwitch(apps::kLoadAndLaunchApp) &&
5902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      !IncognitoModePrefs::ShouldLaunchIncognito(
5912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          command_line, last_used_profile->GetPrefs())) {
5922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    CommandLine::StringType path = command_line.GetSwitchValueNative(
593868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        apps::kLoadAndLaunchApp);
5945e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)
5955e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)    if (!apps::AppLoadService::Get(last_used_profile)->LoadAndLaunch(
5965e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)            base::FilePath(path), command_line, cur_dir)) {
597868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      return false;
598868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    }
599868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
6002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Return early here since we don't want to open a browser window.
6012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // The exception is when there are no browser windows, since we don't want
6022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // chrome to shut down.
6032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // TODO(jackhou): Do this properly once keep-alive is handled by the
6042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // background page of apps. Tracked at http://crbug.com/175381
6052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (chrome::GetTotalBrowserCountForProfile(last_used_profile) != 0)
6062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return true;
6072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
6082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  chrome::startup::IsProcessStartup is_process_startup = process_startup ?
6102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      chrome::startup::IS_PROCESS_STARTUP :
6112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      chrome::startup::IS_NOT_PROCESS_STARTUP;
6122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  chrome::startup::IsFirstRun is_first_run = first_run::IsChromeFirstRun() ?
6132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      chrome::startup::IS_FIRST_RUN : chrome::startup::IS_NOT_FIRST_RUN;
6142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // |last_opened_profiles| will be empty in the following circumstances:
6152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // - This is the first launch. |last_used_profile| is the initial profile.
6162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // - The user exited the browser by closing all windows for all
6172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // profiles. |last_used_profile| is the profile which owned the last open
6182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // window.
6192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // - Only incognito windows were open when the browser exited.
6202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // |last_used_profile| is the last used incognito profile. Restoring it will
6212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // create a browser window for the corresponding original profile.
6222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (last_opened_profiles.empty()) {
623e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch    // If the last used profile is locked or was a guest, show the user manager.
6245f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    if (switches::IsNewAvatarMenu()) {
625e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch      ProfileInfoCache& profile_info =
626e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch          g_browser_process->profile_manager()->GetProfileInfoCache();
627e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch      size_t profile_index = profile_info.GetIndexOfProfileWithPath(
628e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch          last_used_profile->GetPath());
629e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch      bool signin_required = profile_index != std::string::npos &&
630e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch          profile_info.ProfileIsSigninRequiredAtIndex(profile_index);
631e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch      if (signin_required || last_used_profile->IsGuestSession()) {
6321320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        UserManager::Show(base::FilePath(),
6331320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                          profiles::USER_MANAGER_NO_TUTORIAL,
6341320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                          profiles::USER_MANAGER_SELECT_PROFILE_NO_ACTION);
635e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch        return true;
636e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch      }
637f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    }
6382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (!browser_creator->LaunchBrowser(command_line, last_used_profile,
6392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                        cur_dir, is_process_startup,
6402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                        is_first_run, return_code)) {
6412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return false;
6422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
6432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  } else {
6442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Launch the last used profile with the full command line, and the other
6452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // opened profiles without the URLs to launch.
6462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    CommandLine command_line_without_urls(command_line.GetProgram());
6472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const CommandLine::SwitchMap& switches = command_line.GetSwitches();
6482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    for (CommandLine::SwitchMap::const_iterator switch_it = switches.begin();
6492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)         switch_it != switches.end(); ++switch_it) {
6502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      command_line_without_urls.AppendSwitchNative(switch_it->first,
6512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                                   switch_it->second);
6522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
6532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Launch the profiles in the order they became active.
6542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    for (Profiles::const_iterator it = last_opened_profiles.begin();
6552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)         it != last_opened_profiles.end(); ++it) {
6562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // Don't launch additional profiles which would only open a new tab
6572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // page. When restarting after an update, all profiles will reopen last
6582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // open pages.
6592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      SessionStartupPref startup_pref =
6602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          GetSessionStartupPref(command_line, *it);
6612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      if (*it != last_used_profile &&
6622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          startup_pref.type == SessionStartupPref::DEFAULT &&
6632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          !HasPendingUncleanExit(*it))
6642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        continue;
665f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
666f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      // Don't re-open a browser window for the guest profile.
6675f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      if (switches::IsNewAvatarMenu() &&
668f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)          (*it)->IsGuestSession())
669f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        continue;
670f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
6712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      if (!browser_creator->LaunchBrowser((*it == last_used_profile) ?
6722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          command_line : command_line_without_urls, *it, cur_dir,
6732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          is_process_startup, is_first_run, return_code))
6745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        return false;
6752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // We've launched at least one browser.
6762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      is_process_startup = chrome::startup::IS_NOT_PROCESS_STARTUP;
6775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
6782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // This must be done after all profiles have been launched so the observer
6792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // knows about all profiles to wait for before activating this one.
680f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
681f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // If the last used profile was the guest one, we didn't open it so
682f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // we don't need to activate it either.
6835f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    if (!switches::IsNewAvatarMenu() &&
684f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        !last_used_profile->IsGuestSession())
685f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      profile_launch_observer.Get().set_profile_to_activate(last_used_profile);
6865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
6875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return true;
6885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
6915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void StartupBrowserCreator::ProcessCommandLineOnProfileCreated(
6922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const CommandLine& command_line,
6932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const base::FilePath& cur_dir,
6945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    Profile* profile,
6955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    Profile::CreateStatus status) {
6965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (status == Profile::CREATE_STATUS_INITIALIZED)
6972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ProcessCmdLineImpl(command_line, cur_dir, false, profile, Profiles(), NULL,
6985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       NULL);
6995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
7005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
7025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void StartupBrowserCreator::ProcessCommandLineAlreadyRunning(
7032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const CommandLine& command_line,
7042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const base::FilePath& cur_dir,
7052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const base::FilePath& profile_path) {
7062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ProfileManager* profile_manager = g_browser_process->profile_manager();
7072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  Profile* profile = profile_manager->GetProfileByPath(profile_path);
7085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // The profile isn't loaded yet and so needs to be loaded asynchronously.
7105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!profile) {
7112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    profile_manager->CreateProfileAsync(profile_path,
7122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        base::Bind(&StartupBrowserCreator::ProcessCommandLineOnProfileCreated,
713a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                   command_line, cur_dir), base::string16(), base::string16(),
7142385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch                   std::string());
7155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
7165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
7172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
7182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ProcessCmdLineImpl(command_line, cur_dir, false, profile, Profiles(), NULL,
7192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                     NULL);
7202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
7212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
7222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// static
7232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool StartupBrowserCreator::ActivatedProfile() {
7242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return profile_launch_observer.Get().activated_profile();
7255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
7265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool HasPendingUncleanExit(Profile* profile) {
7285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return profile->GetLastSessionExitType() == Profile::EXIT_CRASHED &&
7295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      !profile_launch_observer.Get().HasBeenLaunched(profile);
7305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
7315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
7325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)base::FilePath GetStartupProfilePath(const base::FilePath& user_data_dir,
7335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                     const CommandLine& command_line) {
7341320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (command_line.HasSwitch(switches::kProfileDirectory)) {
7351320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    return user_data_dir.Append(
7361320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        command_line.GetSwitchValuePath(switches::kProfileDirectory));
7371320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  }
7381320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
7395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // If we are showing the app list then chrome isn't shown so load the app
7405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // list's profile rather than chrome's.
7415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (command_line.HasSwitch(switches::kShowAppList)) {
7425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return AppListService::Get(chrome::HOST_DESKTOP_TYPE_NATIVE)->
7435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        GetProfilePath(user_data_dir);
7445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
7455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
7465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return g_browser_process->profile_manager()->GetLastUsedProfileDir(
7475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      user_data_dir);
7485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
749