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 #ifndef CHROME_BROWSER_UI_STARTUP_STARTUP_BROWSER_CREATOR_H_
6 #define CHROME_BROWSER_UI_STARTUP_STARTUP_BROWSER_CREATOR_H_
11 #include "base/basictypes.h"
12 #include "base/files/file_path.h"
13 #include "base/gtest_prod_util.h"
14 #include "chrome/browser/prefs/session_startup_pref.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/ui/startup/startup_tab.h"
17 #include "chrome/browser/ui/startup/startup_types.h"
28 // class containing helpers for BrowserMain to spin up a new instance and
29 // initialize the profile.
30 class StartupBrowserCreator {
32 typedef std::vector<Profile*> Profiles;
34 StartupBrowserCreator();
35 ~StartupBrowserCreator();
37 // Adds a url to be opened during first run. This overrides the standard
38 // tabs shown at first run.
39 void AddFirstRunTab(const GURL& url);
41 // This function is equivalent to ProcessCommandLine but should only be
42 // called during actual process startup.
43 bool Start(const base::CommandLine& cmd_line,
44 const base::FilePath& cur_dir,
45 Profile* last_used_profile,
46 const Profiles& last_opened_profiles,
48 return ProcessCmdLineImpl(cmd_line, cur_dir, true, last_used_profile,
49 last_opened_profiles, return_code, this);
52 // This function performs command-line handling and is invoked only after
53 // start up (for example when we get a start request for another process).
54 // |command_line| holds the command line we need to process.
55 // |cur_dir| is the current working directory that the original process was
57 // |startup_profile_dir| is the directory that contains the profile that the
58 // command line arguments will be executed under.
59 static void ProcessCommandLineAlreadyRunning(
60 const base::CommandLine& command_line,
61 const base::FilePath& cur_dir,
62 const base::FilePath& startup_profile_dir);
64 template <class AutomationProviderClass>
65 static bool CreateAutomationProvider(const std::string& channel_id,
67 size_t expected_tabs);
69 // Returns true if we're launching a profile synchronously. In that case, the
70 // opened window should not cause a session restore.
71 static bool InSynchronousProfileLaunch();
73 // Launches a browser window associated with |profile|. |command_line| should
74 // be the command line passed to this process. |cur_dir| can be empty, which
75 // implies that the directory of the executable should be used.
76 // |process_startup| indicates whether this is the first browser.
77 // |is_first_run| indicates that this is a new profile.
78 bool LaunchBrowser(const base::CommandLine& command_line,
80 const base::FilePath& cur_dir,
81 chrome::startup::IsProcessStartup is_process_startup,
82 chrome::startup::IsFirstRun is_first_run,
85 // When called the first time, reads the value of the preference kWasRestarted
86 // and resets it to false. Subsequent calls return the value which was read
88 static bool WasRestarted();
90 static SessionStartupPref GetSessionStartupPref(
91 const base::CommandLine& command_line,
94 void set_is_default_browser_dialog_suppressed(bool new_value) {
95 is_default_browser_dialog_suppressed_ = new_value;
98 bool is_default_browser_dialog_suppressed() const {
99 return is_default_browser_dialog_suppressed_;
102 void set_show_main_browser_window(bool show_main_browser_window) {
103 show_main_browser_window_ = show_main_browser_window;
106 bool show_main_browser_window() const {
107 return show_main_browser_window_;
110 // For faking that no profiles have been launched yet.
111 static void ClearLaunchedProfilesForTesting();
114 friend class CloudPrintProxyPolicyTest;
115 friend class CloudPrintProxyPolicyStartupTest;
116 friend class StartupBrowserCreatorImpl;
117 FRIEND_TEST_ALL_PREFIXES(StartupBrowserCreatorTest,
118 ReadingWasRestartedAfterNormalStart);
119 FRIEND_TEST_ALL_PREFIXES(StartupBrowserCreatorTest,
120 ReadingWasRestartedAfterRestart);
121 FRIEND_TEST_ALL_PREFIXES(StartupBrowserCreatorTest, UpdateWithTwoProfiles);
122 FRIEND_TEST_ALL_PREFIXES(StartupBrowserCreatorTest, LastUsedProfileActivated);
124 // Returns the list of URLs to open from the command line. The returned
125 // vector is empty if the user didn't specify any URLs on the command line.
126 static std::vector<GURL> GetURLsFromCommandLine(
127 const base::CommandLine& command_line,
128 const base::FilePath& cur_dir,
131 static bool ProcessCmdLineImpl(const base::CommandLine& command_line,
132 const base::FilePath& cur_dir,
133 bool process_startup,
134 Profile* last_used_profile,
135 const Profiles& last_opened_profiles,
137 StartupBrowserCreator* browser_creator);
139 // Callback after a profile has been created.
140 static void ProcessCommandLineOnProfileCreated(
141 const base::CommandLine& command_line,
142 const base::FilePath& cur_dir,
144 Profile::CreateStatus status);
146 // Returns true once a profile was activated. Used by the
147 // StartupBrowserCreatorTest.LastUsedProfileActivated test.
148 static bool ActivatedProfile();
150 // Additional tabs to open during first run.
151 std::vector<GURL> first_run_tabs_;
153 // True if the set-as-default dialog has been explicitly suppressed.
154 // This information is used to allow the default browser prompt to show on
155 // first-run when the dialog has been suppressed.
156 bool is_default_browser_dialog_suppressed_;
158 // Whether the browser window should be shown immediately after it has been
159 // created. Default is true.
160 bool show_main_browser_window_;
162 // True if we have already read and reset the preference kWasRestarted. (A
163 // member variable instead of a static variable inside WasRestarted because
165 static bool was_restarted_read_;
167 static bool in_synchronous_profile_launch_;
169 DISALLOW_COPY_AND_ASSIGN(StartupBrowserCreator);
172 // Returns true if |profile| has exited uncleanly and has not been launched
173 // after the unclean exit.
174 bool HasPendingUncleanExit(Profile* profile);
176 // Returns the path that contains the profile that should be loaded on process
178 base::FilePath GetStartupProfilePath(const base::FilePath& user_data_dir,
179 const base::CommandLine& command_line);
181 #endif // CHROME_BROWSER_UI_STARTUP_STARTUP_BROWSER_CREATOR_H_