browser_init.h revision 513209b27ff55e2841eac0e4120199c23acce758
1// Copyright (c) 2010 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 "googleurl/src/gurl.h" 16 17class Browser; 18class CommandLine; 19class GURL; 20class Profile; 21class TabContents; 22 23// class containing helpers for BrowserMain to spin up a new instance and 24// initialize the profile. 25class BrowserInit { 26 public: 27 BrowserInit(); 28 ~BrowserInit(); 29 30 // Adds a url to be opened during first run. This overrides the standard 31 // tabs shown at first run. 32 void AddFirstRunTab(const GURL& url); 33 34 // This function is equivalent to ProcessCommandLine but should only be 35 // called during actual process startup. 36 bool Start(const CommandLine& cmd_line, const FilePath& cur_dir, 37 Profile* profile, int* return_code) { 38 return ProcessCmdLineImpl(cmd_line, cur_dir, true, profile, return_code, 39 this); 40 } 41 42 // This function performs command-line handling and is invoked when process 43 // starts as well as when we get a start request from another process (via the 44 // WM_COPYDATA message). |command_line| holds the command line we need to 45 // process - either from this process or from some other one (if 46 // |process_startup| is true and we are being called from 47 // ProcessSingleton::OnCopyData). 48 static bool ProcessCommandLine(const CommandLine& cmd_line, 49 const FilePath& cur_dir, bool process_startup, 50 Profile* profile, int* return_code) { 51 return ProcessCmdLineImpl(cmd_line, cur_dir, process_startup, profile, 52 return_code, NULL); 53 } 54 55 template <class AutomationProviderClass> 56 static void CreateAutomationProvider(const std::string& channel_id, 57 Profile* profile, 58 size_t expected_tabs); 59 60 // Returns true if the browser is coming up. 61 static bool InProcessStartup(); 62 63 // Launches a browser window associated with |profile|. |command_line| should 64 // be the command line passed to this process. |cur_dir| can be empty, which 65 // implies that the directory of the executable should be used. 66 // |process_startup| indicates whether this is the first browser. 67 bool LaunchBrowser(const CommandLine& command_line, Profile* profile, 68 const FilePath& cur_dir, bool process_startup, 69 int* return_code); 70 71 // LaunchWithProfile --------------------------------------------------------- 72 // 73 // Assists launching the application and appending the initial tabs for a 74 // browser window. 75 76 class LaunchWithProfile { 77 public: 78 // Used by OpenTabsInBrowser. 79 struct Tab { 80 Tab(); 81 ~Tab(); 82 83 // The url to load. 84 GURL url; 85 86 // If true, the tab corresponds to an app an |app_id| gives the id of the 87 // app. 88 bool is_app; 89 90 // True if the is tab pinned. 91 bool is_pinned; 92 93 // Id of the app. 94 std::string app_id; 95 }; 96 97 // There are two ctors. The first one implies a NULL browser_init object 98 // and thus no access to distribution-specific first-run behaviors. The 99 // second one is always called when the browser starts even if it is not 100 // the first run. 101 LaunchWithProfile(const FilePath& cur_dir, const CommandLine& command_line); 102 LaunchWithProfile(const FilePath& cur_dir, const CommandLine& command_line, 103 BrowserInit* browser_init); 104 ~LaunchWithProfile(); 105 106 // Creates the necessary windows for startup. Returns true on success, 107 // false on failure. process_startup is true if Chrome is just 108 // starting up. If process_startup is false, it indicates Chrome was 109 // already running and the user wants to launch another instance. 110 bool Launch(Profile* profile, bool process_startup); 111 112 // Convenience for OpenTabsInBrowser that converts |urls| into a set of 113 // Tabs. 114 Browser* OpenURLsInBrowser(Browser* browser, 115 bool process_startup, 116 const std::vector<GURL>& urls); 117 118 // Creates a tab for each of the Tabs in |tabs|. If browser is non-null 119 // and a tabbed browser, the tabs are added to it. Otherwise a new tabbed 120 // browser is created and the tabs are added to it. The browser the tabs 121 // are added to is returned, which is either |browser| or the newly created 122 // browser. 123 Browser* OpenTabsInBrowser(Browser* browser, 124 bool process_startup, 125 const std::vector<Tab>& tabs); 126 127 private: 128 FRIEND_TEST_ALL_PREFIXES(BrowserTest, RestorePinnedTabs); 129 130 // If the process was launched with the web application command line flags, 131 // e.g. --app=http://www.google.com/ or --app_id=... return true. 132 // In this case |app_url| or |app_id| are populated if they're non-null. 133 bool IsAppLaunch(std::string* app_url, std::string* app_id); 134 135 // If IsAppLaunch is true, tries to open an application window. 136 // If the app is specified to start in a tab, or IsAppLaunch is false, 137 // returns false to specify default processing. 138 bool OpenApplicationWindow(Profile* profile); 139 140 // Invoked from OpenURLsInBrowser to handle processing of urls. This may 141 // do any of the following: 142 // . Invoke ProcessStartupURLs if |process_startup| is true. 143 // . Restore the last session if necessary. 144 // . Open the urls directly. 145 void ProcessLaunchURLs(bool process_startup, 146 const std::vector<GURL>& urls_to_open); 147 148 // Does the following: 149 // . If the user's startup pref is to restore the last session (or the 150 // command line flag is present to force using last session), it is 151 // restored, and true is returned. 152 // . Attempts to restore any pinned tabs from last run of chrome and: 153 // . If urls_to_open is non-empty, they are opened and true is returned. 154 // . If the user's startup pref is to launch a specific set of URLs they 155 // are opened. 156 // 157 // Otherwise false is returned, which indicates the caller must create a 158 // new browser. 159 bool ProcessStartupURLs(const std::vector<GURL>& urls_to_open); 160 161 // If the last session didn't exit cleanly and tab is a web contents tab, 162 // an infobar is added allowing the user to restore the last session. 163 void AddCrashedInfoBarIfNecessary(TabContents* tab); 164 165 // If we have been started with unsupported flags like --single-process, 166 // politely nag the user about it. 167 void AddBadFlagsInfoBarIfNecessary(TabContents* tab); 168 169 // Returns the list of URLs to open from the command line. The returned 170 // vector is empty if the user didn't specify any URLs on the command line. 171 std::vector<GURL> GetURLsFromCommandLine(Profile* profile); 172 173 // Adds additional startup URLs to the specified vector. 174 void AddStartupURLs(std::vector<GURL>* startup_urls) const; 175 176 // Checks whether Chrome is still the default browser (unless the user 177 // previously instructed not to do so) and warns the user if it is not. 178 void CheckDefaultBrowser(Profile* profile); 179 180 const FilePath cur_dir_; 181 const CommandLine& command_line_; 182 Profile* profile_; 183 BrowserInit* browser_init_; 184 DISALLOW_COPY_AND_ASSIGN(LaunchWithProfile); 185 }; 186 187 private: 188 static bool ProcessCmdLineImpl(const CommandLine& command_line, 189 const FilePath& cur_dir, bool process_startup, 190 Profile* profile, int* return_code, 191 BrowserInit* browser_init); 192 193 // Additional tabs to open during first run. 194 std::vector<GURL> first_run_tabs_; 195 196 DISALLOW_COPY_AND_ASSIGN(BrowserInit); 197}; 198 199#endif // CHROME_BROWSER_UI_BROWSER_INIT_H_ 200