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_H_
      6 #define CHROME_BROWSER_UI_STARTUP_STARTUP_BROWSER_CREATOR_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "base/basictypes.h"
     12 #include "base/files/file_path.h"
     13 #include "base/gtest_prod_util.h"
     14 #include "chrome/browser/prefs/session_startup_pref.h"
     15 #include "chrome/browser/profiles/profile.h"
     16 #include "chrome/browser/ui/startup/startup_tab.h"
     17 #include "chrome/browser/ui/startup/startup_types.h"
     18 #include "url/gurl.h"
     19 
     20 class Browser;
     21 class CommandLine;
     22 class GURL;
     23 class PrefService;
     24 
     25 // class containing helpers for BrowserMain to spin up a new instance and
     26 // initialize the profile.
     27 class StartupBrowserCreator {
     28  public:
     29   typedef std::vector<Profile*> Profiles;
     30 
     31   StartupBrowserCreator();
     32   ~StartupBrowserCreator();
     33 
     34   // Adds a url to be opened during first run. This overrides the standard
     35   // tabs shown at first run.
     36   void AddFirstRunTab(const GURL& url);
     37 
     38   // This function is equivalent to ProcessCommandLine but should only be
     39   // called during actual process startup.
     40   bool Start(const CommandLine& cmd_line,
     41              const base::FilePath& cur_dir,
     42              Profile* last_used_profile,
     43              const Profiles& last_opened_profiles,
     44              int* return_code) {
     45     return ProcessCmdLineImpl(cmd_line, cur_dir, true, last_used_profile,
     46                               last_opened_profiles, return_code, this);
     47   }
     48 
     49   // This function performs command-line handling and is invoked only after
     50   // start up (for example when we get a start request for another process).
     51   // |command_line| holds the command line we need to process.
     52   // |cur_dir| is the current working directory that the original process was
     53   // invoked from.
     54   // |startup_profile_dir| is the directory that contains the profile that the
     55   // command line arguments will be executed under.
     56   static void ProcessCommandLineAlreadyRunning(
     57       const CommandLine& command_line,
     58       const base::FilePath& cur_dir,
     59       const base::FilePath& startup_profile_dir);
     60 
     61   template <class AutomationProviderClass>
     62   static bool CreateAutomationProvider(const std::string& channel_id,
     63                                        Profile* profile,
     64                                        size_t expected_tabs);
     65 
     66   // Returns true if we're launching a profile synchronously. In that case, the
     67   // opened window should not cause a session restore.
     68   static bool InSynchronousProfileLaunch();
     69 
     70   // Launches a browser window associated with |profile|. |command_line| should
     71   // be the command line passed to this process. |cur_dir| can be empty, which
     72   // implies that the directory of the executable should be used.
     73   // |process_startup| indicates whether this is the first browser.
     74   // |is_first_run| indicates that this is a new profile.
     75   bool LaunchBrowser(const CommandLine& command_line,
     76                      Profile* profile,
     77                      const base::FilePath& cur_dir,
     78                      chrome::startup::IsProcessStartup is_process_startup,
     79                      chrome::startup::IsFirstRun is_first_run,
     80                      int* return_code);
     81 
     82   // When called the first time, reads the value of the preference kWasRestarted
     83   // and resets it to false. Subsequent calls return the value which was read
     84   // the first time.
     85   static bool WasRestarted();
     86 
     87   static SessionStartupPref GetSessionStartupPref(
     88       const CommandLine& command_line,
     89       Profile* profile);
     90 
     91   void set_is_default_browser_dialog_suppressed(bool new_value) {
     92     is_default_browser_dialog_suppressed_ = new_value;
     93   }
     94 
     95   bool is_default_browser_dialog_suppressed() const {
     96     return is_default_browser_dialog_suppressed_;
     97   }
     98 
     99   void set_show_main_browser_window(bool show_main_browser_window) {
    100     show_main_browser_window_ = show_main_browser_window;
    101   }
    102 
    103   bool show_main_browser_window() const {
    104     return show_main_browser_window_;
    105   }
    106 
    107   // For faking that no profiles have been launched yet.
    108   static void ClearLaunchedProfilesForTesting();
    109 
    110  private:
    111   friend class CloudPrintProxyPolicyTest;
    112   friend class CloudPrintProxyPolicyStartupTest;
    113   friend class StartupBrowserCreatorImpl;
    114   FRIEND_TEST_ALL_PREFIXES(StartupBrowserCreatorTest,
    115                            ReadingWasRestartedAfterNormalStart);
    116   FRIEND_TEST_ALL_PREFIXES(StartupBrowserCreatorTest,
    117                            ReadingWasRestartedAfterRestart);
    118   FRIEND_TEST_ALL_PREFIXES(StartupBrowserCreatorTest, UpdateWithTwoProfiles);
    119   FRIEND_TEST_ALL_PREFIXES(StartupBrowserCreatorTest, LastUsedProfileActivated);
    120 
    121   // Returns the list of URLs to open from the command line. The returned
    122   // vector is empty if the user didn't specify any URLs on the command line.
    123   static std::vector<GURL> GetURLsFromCommandLine(
    124       const CommandLine& command_line,
    125       const base::FilePath& cur_dir,
    126       Profile* profile);
    127 
    128   static bool ProcessCmdLineImpl(const CommandLine& command_line,
    129                                  const base::FilePath& cur_dir,
    130                                  bool process_startup,
    131                                  Profile* last_used_profile,
    132                                  const Profiles& last_opened_profiles,
    133                                  int* return_code,
    134                                  StartupBrowserCreator* browser_creator);
    135 
    136   // Callback after a profile has been created.
    137   static void ProcessCommandLineOnProfileCreated(
    138       const CommandLine& command_line,
    139       const base::FilePath& cur_dir,
    140       Profile* profile,
    141       Profile::CreateStatus status);
    142 
    143   // Returns true once a profile was activated. Used by the
    144   // StartupBrowserCreatorTest.LastUsedProfileActivated test.
    145   static bool ActivatedProfile();
    146 
    147   // Additional tabs to open during first run.
    148   std::vector<GURL> first_run_tabs_;
    149 
    150   // True if the set-as-default dialog has been explicitly suppressed.
    151   // This information is used to allow the default browser prompt to show on
    152   // first-run when the dialog has been suppressed.
    153   bool is_default_browser_dialog_suppressed_;
    154 
    155   // Whether the browser window should be shown immediately after it has been
    156   // created. Default is true.
    157   bool show_main_browser_window_;
    158 
    159   // True if we have already read and reset the preference kWasRestarted. (A
    160   // member variable instead of a static variable inside WasRestarted because
    161   // of testing.)
    162   static bool was_restarted_read_;
    163 
    164   static bool in_synchronous_profile_launch_;
    165 
    166   DISALLOW_COPY_AND_ASSIGN(StartupBrowserCreator);
    167 };
    168 
    169 // Returns true if |profile| has exited uncleanly and has not been launched
    170 // after the unclean exit.
    171 bool HasPendingUncleanExit(Profile* profile);
    172 
    173 #endif  // CHROME_BROWSER_UI_STARTUP_STARTUP_BROWSER_CREATOR_H_
    174