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 // This function performs command-line handling and is invoked only after
49 // start up (for example when we get a start request for another process).
50 // |command_line| holds the command line we need to process.
51 // |cur_dir| is the current working directory that the original process was
53 // |startup_profile_dir| is the directory that contains the profile that the
54 // command line arguments will be executed under.
55 static void ProcessCommandLineAlreadyRunning(
56 const base::CommandLine
& command_line
,
57 const base::FilePath
& cur_dir
,
58 const base::FilePath
& startup_profile_dir
);
60 // Returns true if we're launching a profile synchronously. In that case, the
61 // opened window should not cause a session restore.
62 static bool InSynchronousProfileLaunch();
64 // Launches a browser window associated with |profile|. |command_line| should
65 // be the command line passed to this process. |cur_dir| can be empty, which
66 // implies that the directory of the executable should be used.
67 // |process_startup| indicates whether this is the first browser.
68 // |is_first_run| indicates that this is a new profile.
69 bool LaunchBrowser(const base::CommandLine
& command_line
,
71 const base::FilePath
& cur_dir
,
72 chrome::startup::IsProcessStartup is_process_startup
,
73 chrome::startup::IsFirstRun is_first_run
);
75 // When called the first time, reads the value of the preference kWasRestarted
76 // and resets it to false. Subsequent calls return the value which was read
78 static bool WasRestarted();
80 static SessionStartupPref
GetSessionStartupPref(
81 const base::CommandLine
& command_line
,
84 void set_is_default_browser_dialog_suppressed(bool new_value
) {
85 is_default_browser_dialog_suppressed_
= new_value
;
88 bool is_default_browser_dialog_suppressed() const {
89 return is_default_browser_dialog_suppressed_
;
92 void set_show_main_browser_window(bool show_main_browser_window
) {
93 show_main_browser_window_
= show_main_browser_window
;
96 bool show_main_browser_window() const {
97 return show_main_browser_window_
;
100 // For faking that no profiles have been launched yet.
101 static void ClearLaunchedProfilesForTesting();
104 friend class CloudPrintProxyPolicyTest
;
105 friend class CloudPrintProxyPolicyStartupTest
;
106 friend class StartupBrowserCreatorImpl
;
107 FRIEND_TEST_ALL_PREFIXES(StartupBrowserCreatorTest
,
108 ReadingWasRestartedAfterNormalStart
);
109 FRIEND_TEST_ALL_PREFIXES(StartupBrowserCreatorTest
,
110 ReadingWasRestartedAfterRestart
);
111 FRIEND_TEST_ALL_PREFIXES(StartupBrowserCreatorTest
, UpdateWithTwoProfiles
);
112 FRIEND_TEST_ALL_PREFIXES(StartupBrowserCreatorTest
, LastUsedProfileActivated
);
114 // Returns the list of URLs to open from the command line. The returned
115 // vector is empty if the user didn't specify any URLs on the command line.
116 static std::vector
<GURL
> GetURLsFromCommandLine(
117 const base::CommandLine
& command_line
,
118 const base::FilePath
& cur_dir
,
121 static bool ProcessCmdLineImpl(const base::CommandLine
& command_line
,
122 const base::FilePath
& cur_dir
,
123 bool process_startup
,
124 Profile
* last_used_profile
,
125 const Profiles
& last_opened_profiles
,
126 StartupBrowserCreator
* browser_creator
);
128 // This function performs command-line handling and is invoked only after
129 // start up (for example when we get a start request for another process).
130 // |command_line| holds the command line being processed.
131 // |cur_dir| is the current working directory that the original process was
133 // |profile| is the profile the apps will be launched in.
134 static bool ProcessLoadApps(const base::CommandLine
& command_line
,
135 const base::FilePath
& cur_dir
,
138 // Callback after a profile has been created.
139 static void ProcessCommandLineOnProfileCreated(
140 const base::CommandLine
& command_line
,
141 const base::FilePath
& cur_dir
,
143 Profile::CreateStatus status
);
145 // Returns true once a profile was activated. Used by the
146 // StartupBrowserCreatorTest.LastUsedProfileActivated test.
147 static bool ActivatedProfile();
149 // Additional tabs to open during first run.
150 std::vector
<GURL
> first_run_tabs_
;
152 // True if the set-as-default dialog has been explicitly suppressed.
153 // This information is used to allow the default browser prompt to show on
154 // first-run when the dialog has been suppressed.
155 bool is_default_browser_dialog_suppressed_
;
157 // Whether the browser window should be shown immediately after it has been
158 // created. Default is true.
159 bool show_main_browser_window_
;
161 // True if we have already read and reset the preference kWasRestarted. (A
162 // member variable instead of a static variable inside WasRestarted because
164 static bool was_restarted_read_
;
166 static bool in_synchronous_profile_launch_
;
168 DISALLOW_COPY_AND_ASSIGN(StartupBrowserCreator
);
171 // Returns true if |profile| has exited uncleanly and has not been launched
172 // after the unclean exit.
173 bool HasPendingUncleanExit(Profile
* profile
);
175 // Returns the path that contains the profile that should be loaded on process
177 base::FilePath
GetStartupProfilePath(const base::FilePath
& user_data_dir
,
178 const base::CommandLine
& command_line
);
180 #endif // CHROME_BROWSER_UI_STARTUP_STARTUP_BROWSER_CREATOR_H_