startup_browser_creator_impl.h revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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_STARTUP_STARTUP_BROWSER_CREATOR_IMPL_H_ 6#define CHROME_BROWSER_UI_STARTUP_STARTUP_BROWSER_CREATOR_IMPL_H_ 7 8#include <string> 9#include <vector> 10 11#include "base/files/file_path.h" 12#include "base/gtest_prod_util.h" 13#include "chrome/browser/ui/startup/startup_tab.h" 14#include "chrome/browser/ui/startup/startup_types.h" 15#include "googleurl/src/gurl.h" 16 17class Browser; 18class CommandLine; 19class Profile; 20class StartupBrowserCreator; 21 22namespace base { 23class FilePath; 24} 25 26namespace content { 27class WebContents; 28} 29 30namespace gfx { 31class Rect; 32} 33 34namespace internals { 35GURL GetWelcomePageURL(); 36} 37 38// Assists launching the application and appending the initial tabs for a 39// browser window. 40class StartupBrowserCreatorImpl { 41 public: 42 // There are two ctors. The first one implies a NULL browser_creator object 43 // and thus no access to distribution-specific first-run behaviors. The 44 // second one is always called when the browser starts even if it is not 45 // the first run. |is_first_run| indicates that this is a new profile. 46 StartupBrowserCreatorImpl(const base::FilePath& cur_dir, 47 const CommandLine& command_line, 48 chrome::startup::IsFirstRun is_first_run); 49 StartupBrowserCreatorImpl(const base::FilePath& cur_dir, 50 const CommandLine& command_line, 51 StartupBrowserCreator* browser_creator, 52 chrome::startup::IsFirstRun is_first_run); 53 ~StartupBrowserCreatorImpl(); 54 55 // Creates the necessary windows for startup. Returns true on success, 56 // false on failure. process_startup is true if Chrome is just 57 // starting up. If process_startup is false, it indicates Chrome was 58 // already running and the user wants to launch another instance. 59 bool Launch(Profile* profile, 60 const std::vector<GURL>& urls_to_open, 61 bool process_startup); 62 63 // Convenience for OpenTabsInBrowser that converts |urls| into a set of 64 // Tabs. 65 Browser* OpenURLsInBrowser(Browser* browser, 66 bool process_startup, 67 const std::vector<GURL>& urls); 68 69 // Creates a tab for each of the Tabs in |tabs|. If browser is non-null 70 // and a tabbed browser, the tabs are added to it. Otherwise a new tabbed 71 // browser is created and the tabs are added to it. The browser the tabs 72 // are added to is returned, which is either |browser| or the newly created 73 // browser. 74 Browser* OpenTabsInBrowser(Browser* browser, 75 bool process_startup, 76 const StartupTabs& tabs); 77 78 private: 79 FRIEND_TEST_ALL_PREFIXES(BrowserTest, RestorePinnedTabs); 80 FRIEND_TEST_ALL_PREFIXES(BrowserTest, AppIdSwitch); 81 82 // Extracts optional application window size passed in command line. 83 void ExtractOptionalAppWindowSize(gfx::Rect* bounds); 84 85 // If the process was launched with the web application command line flags, 86 // e.g. --app=http://www.google.com/ or --app_id=... return true. 87 // In this case |app_url| or |app_id| are populated if they're non-null. 88 bool IsAppLaunch(std::string* app_url, std::string* app_id); 89 90 // If IsAppLaunch is true, tries to open an application window. 91 // If the app is specified to start in a tab, or IsAppLaunch is false, 92 // returns false to specify default processing. |out_app_contents| is an 93 // optional argument to receive the created WebContents for the app. 94 bool OpenApplicationWindow(Profile* profile, 95 content::WebContents** out_app_contents); 96 97 // If IsAppLaunch is true and the user set a pref indicating that the app 98 // should open in a tab, do so. 99 bool OpenApplicationTab(Profile* profile); 100 101 // Invoked from Launch to handle processing of urls. This may do any of the 102 // following: 103 // . Invoke ProcessStartupURLs if |process_startup| is true. 104 // . If |process_startup| is false, restore the last session if necessary, 105 // or invoke ProcessSpecifiedURLs. 106 // . Open the urls directly. 107 void ProcessLaunchURLs(bool process_startup, 108 const std::vector<GURL>& urls_to_open); 109 110 // Does the following: 111 // . If the user's startup pref is to restore the last session (or the 112 // command line flag is present to force using last session), it is 113 // restored. 114 // . Otherwise invoke ProcessSpecifiedURLs 115 // If a browser was created, true is returned. Otherwise returns false and 116 // the caller must create a new browser. 117 bool ProcessStartupURLs(const std::vector<GURL>& urls_to_open); 118 119 // Invoked from either ProcessLaunchURLs or ProcessStartupURLs to handle 120 // processing of URLs where the behavior is common between process startup 121 // and launch via an existing process (i.e. those explicitly specified by 122 // the user somehow). Does the following: 123 // . Attempts to restore any pinned tabs from last run of chrome. 124 // . If urls_to_open is non-empty, they are opened. 125 // . If the user's startup pref is to launch a specific set of URLs they 126 // are opened. 127 // 128 // If any tabs were opened, the Browser which was created is returned. 129 // Otherwise null is returned and the caller must create a new browser. 130 Browser* ProcessSpecifiedURLs(const std::vector<GURL>& urls_to_open); 131 132 // Adds a Tab to |tabs| for each url in |urls| that doesn't already exist 133 // in |tabs|. 134 void AddUniqueURLs(const std::vector<GURL>& urls, StartupTabs* tabs); 135 136 // Adds any startup infobars to the selected tab of the given browser. 137 void AddInfoBarsIfNecessary( 138 Browser* browser, 139 chrome::startup::IsProcessStartup is_process_startup); 140 141 // Adds additional startup URLs to the specified vector. 142 void AddStartupURLs(std::vector<GURL>* startup_urls) const; 143 144 // Checks whether the Preferences backup is invalid and notifies user in 145 // that case. 146 void CheckPreferencesBackup(Profile* profile); 147 148 // Function to open startup urls in an existing Browser instance for the 149 // profile passed in. Returns true on success. 150 static bool OpenStartupURLsInExistingBrowser( 151 Profile* profile, 152 const std::vector<GURL>& startup_urls); 153 154 const base::FilePath cur_dir_; 155 const CommandLine& command_line_; 156 Profile* profile_; 157 StartupBrowserCreator* browser_creator_; 158 bool is_first_run_; 159 DISALLOW_COPY_AND_ASSIGN(StartupBrowserCreatorImpl); 160}; 161 162// Returns true if |profile| has exited uncleanly and has not been launched 163// after the unclean exit. 164bool HasPendingUncleanExit(Profile* profile); 165 166#endif // CHROME_BROWSER_UI_STARTUP_STARTUP_BROWSER_CREATOR_IMPL_H_ 167