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