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"
25 // class containing helpers for BrowserMain to spin up a new instance and
26 // initialize the profile.
27 class StartupBrowserCreator
{
29 typedef std::vector
<Profile
*> Profiles
;
31 StartupBrowserCreator();
32 ~StartupBrowserCreator();
34 // Adds a url to be opened during first run. This overrides the standard
35 // tabs shown at first run.
36 void AddFirstRunTab(const GURL
& url
);
38 // This function is equivalent to ProcessCommandLine but should only be
39 // called during actual process startup.
40 bool Start(const CommandLine
& cmd_line
,
41 const base::FilePath
& cur_dir
,
42 Profile
* last_used_profile
,
43 const Profiles
& last_opened_profiles
,
45 return ProcessCmdLineImpl(cmd_line
, cur_dir
, true, last_used_profile
,
46 last_opened_profiles
, return_code
, this);
49 // This function performs command-line handling and is invoked only after
50 // start up (for example when we get a start request for another process).
51 // |command_line| holds the command line we need to process.
52 // |cur_dir| is the current working directory that the original process was
54 // |startup_profile_dir| is the directory that contains the profile that the
55 // command line arguments will be executed under.
56 static void ProcessCommandLineAlreadyRunning(
57 const CommandLine
& command_line
,
58 const base::FilePath
& cur_dir
,
59 const base::FilePath
& startup_profile_dir
);
61 template <class AutomationProviderClass
>
62 static bool CreateAutomationProvider(const std::string
& channel_id
,
64 size_t expected_tabs
);
66 // Returns true if we're launching a profile synchronously. In that case, the
67 // opened window should not cause a session restore.
68 static bool InSynchronousProfileLaunch();
70 // Launches a browser window associated with |profile|. |command_line| should
71 // be the command line passed to this process. |cur_dir| can be empty, which
72 // implies that the directory of the executable should be used.
73 // |process_startup| indicates whether this is the first browser.
74 // |is_first_run| indicates that this is a new profile.
75 bool LaunchBrowser(const CommandLine
& command_line
,
77 const base::FilePath
& cur_dir
,
78 chrome::startup::IsProcessStartup is_process_startup
,
79 chrome::startup::IsFirstRun is_first_run
,
82 // When called the first time, reads the value of the preference kWasRestarted
83 // and resets it to false. Subsequent calls return the value which was read
85 static bool WasRestarted();
87 static SessionStartupPref
GetSessionStartupPref(
88 const CommandLine
& command_line
,
91 void set_is_default_browser_dialog_suppressed(bool new_value
) {
92 is_default_browser_dialog_suppressed_
= new_value
;
95 bool is_default_browser_dialog_suppressed() const {
96 return is_default_browser_dialog_suppressed_
;
99 void set_show_main_browser_window(bool show_main_browser_window
) {
100 show_main_browser_window_
= show_main_browser_window
;
103 bool show_main_browser_window() const {
104 return show_main_browser_window_
;
107 // For faking that no profiles have been launched yet.
108 static void ClearLaunchedProfilesForTesting();
111 friend class CloudPrintProxyPolicyTest
;
112 friend class CloudPrintProxyPolicyStartupTest
;
113 friend class StartupBrowserCreatorImpl
;
114 FRIEND_TEST_ALL_PREFIXES(StartupBrowserCreatorTest
,
115 ReadingWasRestartedAfterNormalStart
);
116 FRIEND_TEST_ALL_PREFIXES(StartupBrowserCreatorTest
,
117 ReadingWasRestartedAfterRestart
);
118 FRIEND_TEST_ALL_PREFIXES(StartupBrowserCreatorTest
, UpdateWithTwoProfiles
);
119 FRIEND_TEST_ALL_PREFIXES(StartupBrowserCreatorTest
, LastUsedProfileActivated
);
121 // Returns the list of URLs to open from the command line. The returned
122 // vector is empty if the user didn't specify any URLs on the command line.
123 static std::vector
<GURL
> GetURLsFromCommandLine(
124 const CommandLine
& command_line
,
125 const base::FilePath
& cur_dir
,
128 static bool ProcessCmdLineImpl(const CommandLine
& command_line
,
129 const base::FilePath
& cur_dir
,
130 bool process_startup
,
131 Profile
* last_used_profile
,
132 const Profiles
& last_opened_profiles
,
134 StartupBrowserCreator
* browser_creator
);
136 // Callback after a profile has been created.
137 static void ProcessCommandLineOnProfileCreated(
138 const CommandLine
& command_line
,
139 const base::FilePath
& cur_dir
,
141 Profile::CreateStatus status
);
143 // Returns true once a profile was activated. Used by the
144 // StartupBrowserCreatorTest.LastUsedProfileActivated test.
145 static bool ActivatedProfile();
147 // Additional tabs to open during first run.
148 std::vector
<GURL
> first_run_tabs_
;
150 // True if the set-as-default dialog has been explicitly suppressed.
151 // This information is used to allow the default browser prompt to show on
152 // first-run when the dialog has been suppressed.
153 bool is_default_browser_dialog_suppressed_
;
155 // Whether the browser window should be shown immediately after it has been
156 // created. Default is true.
157 bool show_main_browser_window_
;
159 // True if we have already read and reset the preference kWasRestarted. (A
160 // member variable instead of a static variable inside WasRestarted because
162 static bool was_restarted_read_
;
164 static bool in_synchronous_profile_launch_
;
166 DISALLOW_COPY_AND_ASSIGN(StartupBrowserCreator
);
169 // Returns true if |profile| has exited uncleanly and has not been launched
170 // after the unclean exit.
171 bool HasPendingUncleanExit(Profile
* profile
);
173 // Returns the path that contains the profile that should be loaded on process
175 base::FilePath
GetStartupProfilePath(const base::FilePath
& user_data_dir
,
176 const CommandLine
& command_line
);
178 #endif // CHROME_BROWSER_UI_STARTUP_STARTUP_BROWSER_CREATOR_H_