OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_UI_STARTUP_STARTUP_BROWSER_CREATOR_H_ | 5 #ifndef CHROME_BROWSER_UI_STARTUP_STARTUP_BROWSER_CREATOR_H_ |
6 #define CHROME_BROWSER_UI_STARTUP_STARTUP_BROWSER_CREATOR_H_ | 6 #define CHROME_BROWSER_UI_STARTUP_STARTUP_BROWSER_CREATOR_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
13 #include "base/file_path.h" | 13 #include "base/file_path.h" |
14 #include "base/gtest_prod_util.h" | 14 #include "base/gtest_prod_util.h" |
15 #include "chrome/browser/prefs/session_startup_pref.h" | 15 #include "chrome/browser/prefs/session_startup_pref.h" |
16 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
| 17 #include "chrome/browser/ui/startup/startup_types.h" |
| 18 #include "chrome/browser/ui/startup/startup_tab.h" |
17 #include "googleurl/src/gurl.h" | 19 #include "googleurl/src/gurl.h" |
18 | 20 |
19 class Browser; | 21 class Browser; |
20 class CommandLine; | 22 class CommandLine; |
21 class GURL; | 23 class GURL; |
22 class PrefService; | 24 class PrefService; |
23 class TabContentsWrapper; | 25 class TabContentsWrapper; |
24 | 26 |
25 // class containing helpers for BrowserMain to spin up a new instance and | 27 // class containing helpers for BrowserMain to spin up a new instance and |
26 // initialize the profile. | 28 // initialize the profile. |
27 class StartupBrowserCreator { | 29 class StartupBrowserCreator { |
28 public: | 30 public: |
29 typedef std::vector<Profile*> Profiles; | 31 typedef std::vector<Profile*> Profiles; |
30 | 32 |
31 enum IsProcessStartup { | |
32 IS_NOT_PROCESS_STARTUP, | |
33 IS_PROCESS_STARTUP | |
34 }; | |
35 enum IsFirstRun { | |
36 IS_NOT_FIRST_RUN, | |
37 IS_FIRST_RUN | |
38 }; | |
39 | |
40 StartupBrowserCreator(); | 33 StartupBrowserCreator(); |
41 ~StartupBrowserCreator(); | 34 ~StartupBrowserCreator(); |
42 | 35 |
43 // Adds a url to be opened during first run. This overrides the standard | 36 // Adds a url to be opened during first run. This overrides the standard |
44 // tabs shown at first run. | 37 // tabs shown at first run. |
45 void AddFirstRunTab(const GURL& url); | 38 void AddFirstRunTab(const GURL& url); |
46 | 39 |
47 // This function is equivalent to ProcessCommandLine but should only be | 40 // This function is equivalent to ProcessCommandLine but should only be |
48 // called during actual process startup. | 41 // called during actual process startup. |
49 bool Start(const CommandLine& cmd_line, | 42 bool Start(const CommandLine& cmd_line, |
(...skipping 21 matching lines...) Expand all Loading... |
71 static bool InSynchronousProfileLaunch(); | 64 static bool InSynchronousProfileLaunch(); |
72 | 65 |
73 // Launches a browser window associated with |profile|. |command_line| should | 66 // 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 | 67 // 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. | 68 // implies that the directory of the executable should be used. |
76 // |process_startup| indicates whether this is the first browser. | 69 // |process_startup| indicates whether this is the first browser. |
77 // |is_first_run| indicates that this is a new profile. | 70 // |is_first_run| indicates that this is a new profile. |
78 bool LaunchBrowser(const CommandLine& command_line, | 71 bool LaunchBrowser(const CommandLine& command_line, |
79 Profile* profile, | 72 Profile* profile, |
80 const FilePath& cur_dir, | 73 const FilePath& cur_dir, |
81 IsProcessStartup is_process_startup, | 74 browser::startup::IsProcessStartup is_process_startup, |
82 IsFirstRun is_first_run, | 75 browser::startup::IsFirstRun is_first_run, |
83 int* return_code); | 76 int* return_code); |
84 | 77 |
85 // When called the first time, reads the value of the preference kWasRestarted | 78 // 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 | 79 // and resets it to false. Subsequent calls return the value which was read |
87 // the first time. | 80 // the first time. |
88 static bool WasRestarted(); | 81 static bool WasRestarted(); |
89 | 82 |
90 static SessionStartupPref GetSessionStartupPref( | 83 static SessionStartupPref GetSessionStartupPref( |
91 const CommandLine& command_line, | 84 const CommandLine& command_line, |
92 Profile* profile); | 85 Profile* profile); |
93 | 86 |
94 // LaunchWithProfile --------------------------------------------------------- | |
95 // | |
96 // Assists launching the application and appending the initial tabs for a | |
97 // browser window. | |
98 | |
99 class LaunchWithProfile { | |
100 public: | |
101 // Used by OpenTabsInBrowser. | |
102 struct Tab { | |
103 Tab(); | |
104 ~Tab(); | |
105 | |
106 // The url to load. | |
107 GURL url; | |
108 | |
109 // If true, the tab corresponds to an app an |app_id| gives the id of the | |
110 // app. | |
111 bool is_app; | |
112 | |
113 // True if the is tab pinned. | |
114 bool is_pinned; | |
115 | |
116 // Id of the app. | |
117 std::string app_id; | |
118 }; | |
119 | |
120 // There are two ctors. The first one implies a NULL browser_creator object | |
121 // and thus no access to distribution-specific first-run behaviors. The | |
122 // second one is always called when the browser starts even if it is not | |
123 // the first run. |is_first_run| indicates that this is a new profile. | |
124 LaunchWithProfile(const FilePath& cur_dir, | |
125 const CommandLine& command_line, | |
126 IsFirstRun is_first_run); | |
127 LaunchWithProfile(const FilePath& cur_dir, | |
128 const CommandLine& command_line, | |
129 StartupBrowserCreator* browser_creator, | |
130 IsFirstRun is_first_run); | |
131 ~LaunchWithProfile(); | |
132 | |
133 // Creates the necessary windows for startup. Returns true on success, | |
134 // false on failure. process_startup is true if Chrome is just | |
135 // starting up. If process_startup is false, it indicates Chrome was | |
136 // already running and the user wants to launch another instance. | |
137 bool Launch(Profile* profile, | |
138 const std::vector<GURL>& urls_to_open, | |
139 bool process_startup); | |
140 | |
141 // Convenience for OpenTabsInBrowser that converts |urls| into a set of | |
142 // Tabs. | |
143 Browser* OpenURLsInBrowser(Browser* browser, | |
144 bool process_startup, | |
145 const std::vector<GURL>& urls); | |
146 | |
147 // Creates a tab for each of the Tabs in |tabs|. If browser is non-null | |
148 // and a tabbed browser, the tabs are added to it. Otherwise a new tabbed | |
149 // browser is created and the tabs are added to it. The browser the tabs | |
150 // are added to is returned, which is either |browser| or the newly created | |
151 // browser. | |
152 Browser* OpenTabsInBrowser(Browser* browser, | |
153 bool process_startup, | |
154 const std::vector<Tab>& tabs); | |
155 | |
156 private: | |
157 FRIEND_TEST_ALL_PREFIXES(BrowserTest, RestorePinnedTabs); | |
158 FRIEND_TEST_ALL_PREFIXES(BrowserTest, AppIdSwitch); | |
159 | |
160 // If the process was launched with the web application command line flags, | |
161 // e.g. --app=http://www.google.com/ or --app_id=... return true. | |
162 // In this case |app_url| or |app_id| are populated if they're non-null. | |
163 bool IsAppLaunch(std::string* app_url, std::string* app_id); | |
164 | |
165 // If IsAppLaunch is true, tries to open an application window. | |
166 // If the app is specified to start in a tab, or IsAppLaunch is false, | |
167 // returns false to specify default processing. | |
168 bool OpenApplicationWindow(Profile* profile); | |
169 | |
170 // If IsAppLaunch is true and the user set a pref indicating that the app | |
171 // should open in a tab, do so. | |
172 bool OpenApplicationTab(Profile* profile); | |
173 | |
174 // Invoked from Launch to handle processing of urls. This may do any of the | |
175 // following: | |
176 // . Invoke ProcessStartupURLs if |process_startup| is true. | |
177 // . If |process_startup| is false, restore the last session if necessary, | |
178 // or invoke ProcessSpecifiedURLs. | |
179 // . Open the urls directly. | |
180 void ProcessLaunchURLs(bool process_startup, | |
181 const std::vector<GURL>& urls_to_open); | |
182 | |
183 // Does the following: | |
184 // . If the user's startup pref is to restore the last session (or the | |
185 // command line flag is present to force using last session), it is | |
186 // restored. | |
187 // . Otherwise invoke ProcessSpecifiedURLs | |
188 // If a browser was created, true is returned. Otherwise returns false and | |
189 // the caller must create a new browser. | |
190 bool ProcessStartupURLs(const std::vector<GURL>& urls_to_open); | |
191 | |
192 // Invoked from either ProcessLaunchURLs or ProcessStartupURLs to handle | |
193 // processing of URLs where the behavior is common between process startup | |
194 // and launch via an existing process (i.e. those explicitly specified by | |
195 // the user somehow). Does the following: | |
196 // . Attempts to restore any pinned tabs from last run of chrome. | |
197 // . If urls_to_open is non-empty, they are opened. | |
198 // . If the user's startup pref is to launch a specific set of URLs they | |
199 // are opened. | |
200 // | |
201 // If any tabs were opened, the Browser which was created is returned. | |
202 // Otherwise null is returned and the caller must create a new browser. | |
203 Browser* ProcessSpecifiedURLs(const std::vector<GURL>& urls_to_open); | |
204 | |
205 // Adds a Tab to |tabs| for each url in |urls| that doesn't already exist | |
206 // in |tabs|. | |
207 void AddUniqueURLs(const std::vector<GURL>& urls, | |
208 std::vector<Tab>* tabs); | |
209 | |
210 // Adds any startup infobars to the selected tab of the given browser. | |
211 void AddInfoBarsIfNecessary(Browser* browser, | |
212 IsProcessStartup is_process_startup); | |
213 | |
214 // Adds additional startup URLs to the specified vector. | |
215 void AddStartupURLs(std::vector<GURL>* startup_urls) const; | |
216 | |
217 // Checks whether the Preferences backup is invalid and notifies user in | |
218 // that case. | |
219 void CheckPreferencesBackup(Profile* profile); | |
220 | |
221 const FilePath cur_dir_; | |
222 const CommandLine& command_line_; | |
223 Profile* profile_; | |
224 StartupBrowserCreator* browser_creator_; | |
225 bool is_first_run_; | |
226 DISALLOW_COPY_AND_ASSIGN(LaunchWithProfile); | |
227 }; | |
228 | |
229 private: | 87 private: |
230 friend class CloudPrintProxyPolicyTest; | 88 friend class CloudPrintProxyPolicyTest; |
231 friend class CloudPrintProxyPolicyStartupTest; | 89 friend class CloudPrintProxyPolicyStartupTest; |
| 90 friend class StartupBrowserCreatorImpl; |
232 FRIEND_TEST_ALL_PREFIXES(StartupBrowserCreatorTest, | 91 FRIEND_TEST_ALL_PREFIXES(StartupBrowserCreatorTest, |
233 ReadingWasRestartedAfterNormalStart); | 92 ReadingWasRestartedAfterNormalStart); |
234 FRIEND_TEST_ALL_PREFIXES(StartupBrowserCreatorTest, | 93 FRIEND_TEST_ALL_PREFIXES(StartupBrowserCreatorTest, |
235 ReadingWasRestartedAfterRestart); | 94 ReadingWasRestartedAfterRestart); |
236 FRIEND_TEST_ALL_PREFIXES(StartupBrowserCreatorTest, UpdateWithTwoProfiles); | 95 FRIEND_TEST_ALL_PREFIXES(StartupBrowserCreatorTest, UpdateWithTwoProfiles); |
237 | 96 |
238 // Returns the list of URLs to open from the command line. The returned | 97 // Returns the list of URLs to open from the command line. The returned |
239 // vector is empty if the user didn't specify any URLs on the command line. | 98 // vector is empty if the user didn't specify any URLs on the command line. |
240 static std::vector<GURL> GetURLsFromCommandLine( | 99 static std::vector<GURL> GetURLsFromCommandLine( |
241 const CommandLine& command_line, | 100 const CommandLine& command_line, |
(...skipping 20 matching lines...) Expand all Loading... |
262 | 121 |
263 // True if we have already read and reset the preference kWasRestarted. (A | 122 // True if we have already read and reset the preference kWasRestarted. (A |
264 // member variable instead of a static variable inside WasRestarted because | 123 // member variable instead of a static variable inside WasRestarted because |
265 // of testing.) | 124 // of testing.) |
266 static bool was_restarted_read_; | 125 static bool was_restarted_read_; |
267 | 126 |
268 DISALLOW_COPY_AND_ASSIGN(StartupBrowserCreator); | 127 DISALLOW_COPY_AND_ASSIGN(StartupBrowserCreator); |
269 }; | 128 }; |
270 | 129 |
271 #endif // CHROME_BROWSER_UI_STARTUP_STARTUP_BROWSER_CREATOR_H_ | 130 #endif // CHROME_BROWSER_UI_STARTUP_STARTUP_BROWSER_CREATOR_H_ |
OLD | NEW |