Home | History | Annotate | Download | only in ui
      1 // Copyright (c) 2011 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 
     17 class Browser;
     18 class CommandLine;
     19 class GURL;
     20 class Profile;
     21 class TabContents;
     22 
     23 // class containing helpers for BrowserMain to spin up a new instance and
     24 // initialize the profile.
     25 class 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 bool 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,
    111                 const std::vector<GURL>& urls_to_open,
    112                 bool process_startup);
    113 
    114     // Convenience for OpenTabsInBrowser that converts |urls| into a set of
    115     // Tabs.
    116     Browser* OpenURLsInBrowser(Browser* browser,
    117                                bool process_startup,
    118                                const std::vector<GURL>& urls);
    119 
    120     // Creates a tab for each of the Tabs in |tabs|. If browser is non-null
    121     // and a tabbed browser, the tabs are added to it. Otherwise a new tabbed
    122     // browser is created and the tabs are added to it. The browser the tabs
    123     // are added to is returned, which is either |browser| or the newly created
    124     // browser.
    125     Browser* OpenTabsInBrowser(Browser* browser,
    126                                bool process_startup,
    127                                const std::vector<Tab>& tabs);
    128 
    129    private:
    130     FRIEND_TEST_ALL_PREFIXES(BrowserTest, RestorePinnedTabs);
    131     FRIEND_TEST_ALL_PREFIXES(BrowserTest, AppIdSwitch);
    132 
    133     // If the process was launched with the web application command line flags,
    134     // e.g. --app=http://www.google.com/ or --app_id=... return true.
    135     // In this case |app_url| or |app_id| are populated if they're non-null.
    136     bool IsAppLaunch(std::string* app_url, std::string* app_id);
    137 
    138     // If IsAppLaunch is true, tries to open an application window.
    139     // If the app is specified to start in a tab, or IsAppLaunch is false,
    140     // returns false to specify default processing.
    141     bool OpenApplicationWindow(Profile* profile);
    142 
    143     // If IsAppLaunch is true and the user set a pref indicating that the app
    144     // should open in a tab, do so.
    145     bool OpenApplicationTab(Profile* profile);
    146 
    147     // Invoked from OpenURLsInBrowser to handle processing of urls. This may
    148     // do any of the following:
    149     // . Invoke ProcessStartupURLs if |process_startup| is true.
    150     // . Restore the last session if necessary.
    151     // . Open the urls directly.
    152     void ProcessLaunchURLs(bool process_startup,
    153                            const std::vector<GURL>& urls_to_open);
    154 
    155     // Does the following:
    156     // . If the user's startup pref is to restore the last session (or the
    157     //   command line flag is present to force using last session), it is
    158     //   restored, and true is returned.
    159     // . Attempts to restore any pinned tabs from last run of chrome and:
    160     //   . If urls_to_open is non-empty, they are opened and true is returned.
    161     //   . If the user's startup pref is to launch a specific set of URLs they
    162     //     are opened.
    163     //
    164     // Otherwise false is returned, which indicates the caller must create a
    165     // new browser.
    166     bool ProcessStartupURLs(const std::vector<GURL>& urls_to_open);
    167 
    168     // Adds a Tab to |tabs| for each url in |urls| that doesn't already exist
    169     // in |tabs|.
    170     void AddUniqueURLs(const std::vector<GURL>& urls,
    171                        std::vector<Tab>* tabs);
    172 
    173     // Adds any startup infobars to the selected tab of the given browser.
    174     void AddInfoBarsIfNecessary(Browser* browser);
    175 
    176     // If the last session didn't exit cleanly and tab is a web contents tab,
    177     // an infobar is added allowing the user to restore the last session.
    178     void AddCrashedInfoBarIfNecessary(TabContents* tab);
    179 
    180     // If we have been started with unsupported flags like --single-process,
    181     // politely nag the user about it.
    182     void AddBadFlagsInfoBarIfNecessary(TabContents* tab);
    183 
    184     // If DNS based certificate checking has been enabled then we show a
    185     // warning infobar.
    186     void AddDNSCertProvenanceCheckingWarningInfoBarIfNecessary(
    187         TabContents* tab);
    188 
    189     // If the user is using an operating system that we have deprecated
    190     // support for and will no longer provide updates, warn the user
    191     // about it.
    192     void AddObsoleteSystemInfoBarIfNecessary(TabContents* tab);
    193 
    194     // Adds additional startup URLs to the specified vector.
    195     void AddStartupURLs(std::vector<GURL>* startup_urls) const;
    196 
    197     // Checks whether Chrome is still the default browser (unless the user
    198     // previously instructed not to do so) and warns the user if it is not.
    199     void CheckDefaultBrowser(Profile* profile);
    200 
    201     const FilePath cur_dir_;
    202     const CommandLine& command_line_;
    203     Profile* profile_;
    204     BrowserInit* browser_init_;
    205     DISALLOW_COPY_AND_ASSIGN(LaunchWithProfile);
    206   };
    207 
    208  private:
    209   // Returns the list of URLs to open from the command line. The returned
    210   // vector is empty if the user didn't specify any URLs on the command line.
    211   static std::vector<GURL> GetURLsFromCommandLine(
    212       const CommandLine& command_line,
    213       const FilePath& cur_dir,
    214       Profile* profile);
    215 
    216   static bool ProcessCmdLineImpl(const CommandLine& command_line,
    217                                  const FilePath& cur_dir, bool process_startup,
    218                                  Profile* profile, int* return_code,
    219                                  BrowserInit* browser_init);
    220 
    221   // Additional tabs to open during first run.
    222   std::vector<GURL> first_run_tabs_;
    223 
    224   DISALLOW_COPY_AND_ASSIGN(BrowserInit);
    225 };
    226 
    227 #endif  // CHROME_BROWSER_UI_BROWSER_INIT_H_
    228