| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #ifndef CHROME_BROWSER_UI_BROWSER_INIT_H_ | |
| 6 #define CHROME_BROWSER_UI_BROWSER_INIT_H_ | |
| 7 #pragma once | |
| 8 | |
| 9 #include <string> | |
| 10 #include <vector> | |
| 11 | |
| 12 #include "base/basictypes.h" | |
| 13 #include "base/file_path.h" | |
| 14 #include "base/gtest_prod_util.h" | |
| 15 #include "chrome/browser/prefs/session_startup_pref.h" | |
| 16 #include "chrome/browser/profiles/profile.h" | |
| 17 #include "googleurl/src/gurl.h" | |
| 18 | |
| 19 class Browser; | |
| 20 class CommandLine; | |
| 21 class GURL; | |
| 22 class PrefService; | |
| 23 class TabContentsWrapper; | |
| 24 | |
| 25 // class containing helpers for BrowserMain to spin up a new instance and | |
| 26 // initialize the profile. | |
| 27 class BrowserInit { | |
| 28 public: | |
| 29 typedef std::vector<Profile*> Profiles; | |
| 30 | |
| 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 BrowserInit(); | |
| 41 ~BrowserInit(); | |
| 42 | |
| 43 // Adds a url to be opened during first run. This overrides the standard | |
| 44 // tabs shown at first run. | |
| 45 void AddFirstRunTab(const GURL& url); | |
| 46 | |
| 47 // This function is equivalent to ProcessCommandLine but should only be | |
| 48 // called during actual process startup. | |
| 49 bool Start(const CommandLine& cmd_line, | |
| 50 const FilePath& cur_dir, | |
| 51 Profile* last_used_profile, | |
| 52 const Profiles& last_opened_profiles, | |
| 53 int* return_code) { | |
| 54 return ProcessCmdLineImpl(cmd_line, cur_dir, true, last_used_profile, | |
| 55 last_opened_profiles, return_code, this); | |
| 56 } | |
| 57 | |
| 58 // This function performs command-line handling and is invoked only after | |
| 59 // start up (for example when we get a start request for another process). | |
| 60 // |command_line| holds the command line we need to process | |
| 61 static void ProcessCommandLineAlreadyRunning(const CommandLine& cmd_line, | |
| 62 const FilePath& cur_dir); | |
| 63 | |
| 64 template <class AutomationProviderClass> | |
| 65 static bool CreateAutomationProvider(const std::string& channel_id, | |
| 66 Profile* profile, | |
| 67 size_t expected_tabs); | |
| 68 | |
| 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(); | |
| 72 | |
| 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 CommandLine& command_line, | |
| 79 Profile* profile, | |
| 80 const FilePath& cur_dir, | |
| 81 IsProcessStartup is_process_startup, | |
| 82 IsFirstRun is_first_run, | |
| 83 int* return_code); | |
| 84 | |
| 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 | |
| 87 // the first time. | |
| 88 static bool WasRestarted(); | |
| 89 | |
| 90 static SessionStartupPref GetSessionStartupPref( | |
| 91 const CommandLine& command_line, | |
| 92 Profile* profile); | |
| 93 | |
| 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_init 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 BrowserInit* browser_init, | |
| 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 BrowserInit* browser_init_; | |
| 225 bool is_first_run_; | |
| 226 DISALLOW_COPY_AND_ASSIGN(LaunchWithProfile); | |
| 227 }; | |
| 228 | |
| 229 private: | |
| 230 friend class CloudPrintProxyPolicyTest; | |
| 231 friend class CloudPrintProxyPolicyStartupTest; | |
| 232 FRIEND_TEST_ALL_PREFIXES(BrowserInitTest, | |
| 233 ReadingWasRestartedAfterNormalStart); | |
| 234 FRIEND_TEST_ALL_PREFIXES(BrowserInitTest, ReadingWasRestartedAfterRestart); | |
| 235 FRIEND_TEST_ALL_PREFIXES(BrowserInitTest, UpdateWithTwoProfiles); | |
| 236 | |
| 237 // Returns the list of URLs to open from the command line. The returned | |
| 238 // vector is empty if the user didn't specify any URLs on the command line. | |
| 239 static std::vector<GURL> GetURLsFromCommandLine( | |
| 240 const CommandLine& command_line, | |
| 241 const FilePath& cur_dir, | |
| 242 Profile* profile); | |
| 243 | |
| 244 static bool ProcessCmdLineImpl(const CommandLine& command_line, | |
| 245 const FilePath& cur_dir, | |
| 246 bool process_startup, | |
| 247 Profile* last_used_profile, | |
| 248 const Profiles& last_opened_profiles, | |
| 249 int* return_code, | |
| 250 BrowserInit* browser_init); | |
| 251 | |
| 252 // Callback after a profile has been created. | |
| 253 static void ProcessCommandLineOnProfileCreated( | |
| 254 const CommandLine& cmd_line, | |
| 255 const FilePath& cur_dir, | |
| 256 Profile* profile, | |
| 257 Profile::CreateStatus status); | |
| 258 | |
| 259 // Additional tabs to open during first run. | |
| 260 std::vector<GURL> first_run_tabs_; | |
| 261 | |
| 262 // True if we have already read and reset the preference kWasRestarted. (A | |
| 263 // member variable instead of a static variable inside WasRestarted because | |
| 264 // of testing.) | |
| 265 static bool was_restarted_read_; | |
| 266 | |
| 267 DISALLOW_COPY_AND_ASSIGN(BrowserInit); | |
| 268 }; | |
| 269 | |
| 270 #endif // CHROME_BROWSER_UI_BROWSER_INIT_H_ | |
| OLD | NEW |