summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/browser_init.h
blob: 900ccd788bc11da8fb5b82d6e7327da0a46eaa8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_UI_BROWSER_INIT_H_
#define CHROME_BROWSER_UI_BROWSER_INIT_H_
#pragma once

#include <string>
#include <vector>

#include "base/basictypes.h"
#include "base/file_path.h"
#include "base/gtest_prod_util.h"
#include "googleurl/src/gurl.h"

class Browser;
class CommandLine;
class GURL;
class Profile;
class TabContents;

// class containing helpers for BrowserMain to spin up a new instance and
// initialize the profile.
class BrowserInit {
 public:
  BrowserInit();
  ~BrowserInit();

  // Adds a url to be opened during first run. This overrides the standard
  // tabs shown at first run.
  void AddFirstRunTab(const GURL& url);

  // This function is equivalent to ProcessCommandLine but should only be
  // called during actual process startup.
  bool Start(const CommandLine& cmd_line, const FilePath& cur_dir,
             Profile* profile, int* return_code) {
    return ProcessCmdLineImpl(cmd_line, cur_dir, true, profile, return_code,
                              this);
  }

  // This function performs command-line handling and is invoked when process
  // starts as well as when we get a start request from another process (via the
  // WM_COPYDATA message). |command_line| holds the command line we need to
  // process - either from this process or from some other one (if
  // |process_startup| is true and we are being called from
  // ProcessSingleton::OnCopyData).
  static bool ProcessCommandLine(const CommandLine& cmd_line,
                                 const FilePath& cur_dir, bool process_startup,
                                 Profile* profile, int* return_code) {
    return ProcessCmdLineImpl(cmd_line, cur_dir, process_startup, profile,
                              return_code, NULL);
  }

  template <class AutomationProviderClass>
  static bool CreateAutomationProvider(const std::string& channel_id,
                                       Profile* profile,
                                       size_t expected_tabs);

  // Returns true if the browser is coming up.
  static bool InProcessStartup();

  // Launches a browser window associated with |profile|. |command_line| should
  // be the command line passed to this process. |cur_dir| can be empty, which
  // implies that the directory of the executable should be used.
  // |process_startup| indicates whether this is the first browser.
  bool LaunchBrowser(const CommandLine& command_line, Profile* profile,
                     const FilePath& cur_dir, bool process_startup,
                     int* return_code);

  // LaunchWithProfile ---------------------------------------------------------
  //
  // Assists launching the application and appending the initial tabs for a
  // browser window.

  class LaunchWithProfile {
   public:
    // Used by OpenTabsInBrowser.
    struct Tab {
      Tab();
      ~Tab();

      // The url to load.
      GURL url;

      // If true, the tab corresponds to an app an |app_id| gives the id of the
      // app.
      bool is_app;

      // True if the is tab pinned.
      bool is_pinned;

      // Id of the app.
      std::string app_id;
    };

    // There are two ctors. The first one implies a NULL browser_init object
    // and thus no access to distribution-specific first-run behaviors. The
    // second one is always called when the browser starts even if it is not
    // the first run.
    LaunchWithProfile(const FilePath& cur_dir, const CommandLine& command_line);
    LaunchWithProfile(const FilePath& cur_dir, const CommandLine& command_line,
                      BrowserInit* browser_init);
    ~LaunchWithProfile();

    // Creates the necessary windows for startup. Returns true on success,
    // false on failure. process_startup is true if Chrome is just
    // starting up. If process_startup is false, it indicates Chrome was
    // already running and the user wants to launch another instance.
    bool Launch(Profile* profile,
                const std::vector<GURL>& urls_to_open,
                bool process_startup);

    // Convenience for OpenTabsInBrowser that converts |urls| into a set of
    // Tabs.
    Browser* OpenURLsInBrowser(Browser* browser,
                               bool process_startup,
                               const std::vector<GURL>& urls);

    // Creates a tab for each of the Tabs in |tabs|. If browser is non-null
    // and a tabbed browser, the tabs are added to it. Otherwise a new tabbed
    // browser is created and the tabs are added to it. The browser the tabs
    // are added to is returned, which is either |browser| or the newly created
    // browser.
    Browser* OpenTabsInBrowser(Browser* browser,
                               bool process_startup,
                               const std::vector<Tab>& tabs);

   private:
    FRIEND_TEST_ALL_PREFIXES(BrowserTest, RestorePinnedTabs);
    FRIEND_TEST_ALL_PREFIXES(BrowserTest, AppIdSwitch);

    // If the process was launched with the web application command line flags,
    // e.g. --app=http://www.google.com/ or --app_id=... return true.
    // In this case |app_url| or |app_id| are populated if they're non-null.
    bool IsAppLaunch(std::string* app_url, std::string* app_id);

    // If IsAppLaunch is true, tries to open an application window.
    // If the app is specified to start in a tab, or IsAppLaunch is false,
    // returns false to specify default processing.
    bool OpenApplicationWindow(Profile* profile);

    // If IsAppLaunch is true and the user set a pref indicating that the app
    // should open in a tab, do so.
    bool OpenApplicationTab(Profile* profile);

    // Invoked from OpenURLsInBrowser to handle processing of urls. This may
    // do any of the following:
    // . Invoke ProcessStartupURLs if |process_startup| is true.
    // . Restore the last session if necessary.
    // . Open the urls directly.
    void ProcessLaunchURLs(bool process_startup,
                           const std::vector<GURL>& urls_to_open);

    // Does the following:
    // . If the user's startup pref is to restore the last session (or the
    //   command line flag is present to force using last session), it is
    //   restored, and true is returned.
    // . Attempts to restore any pinned tabs from last run of chrome and:
    //   . If urls_to_open is non-empty, they are opened and true is returned.
    //   . If the user's startup pref is to launch a specific set of URLs they
    //     are opened.
    //
    // Otherwise false is returned, which indicates the caller must create a
    // new browser.
    bool ProcessStartupURLs(const std::vector<GURL>& urls_to_open);

    // Adds a Tab to |tabs| for each url in |urls| that doesn't already exist
    // in |tabs|.
    void AddUniqueURLs(const std::vector<GURL>& urls,
                       std::vector<Tab>* tabs);

    // Adds any startup infobars to the selected tab of the given browser.
    void AddInfoBarsIfNecessary(Browser* browser);

    // If the last session didn't exit cleanly and tab is a web contents tab,
    // an infobar is added allowing the user to restore the last session.
    void AddCrashedInfoBarIfNecessary(TabContents* tab);

    // If we have been started with unsupported flags like --single-process,
    // politely nag the user about it.
    void AddBadFlagsInfoBarIfNecessary(TabContents* tab);

    // If DNS based certificate checking has been enabled then we show a
    // warning infobar.
    void AddDNSCertProvenanceCheckingWarningInfoBarIfNecessary(
        TabContents* tab);

    // If the user is using an operating system that we have deprecated
    // support for and will no longer provide updates, warn the user
    // about it.
    void AddObsoleteSystemInfoBarIfNecessary(TabContents* tab);

    // Adds additional startup URLs to the specified vector.
    void AddStartupURLs(std::vector<GURL>* startup_urls) const;

    // Checks whether Chrome is still the default browser (unless the user
    // previously instructed not to do so) and warns the user if it is not.
    void CheckDefaultBrowser(Profile* profile);

    const FilePath cur_dir_;
    const CommandLine& command_line_;
    Profile* profile_;
    BrowserInit* browser_init_;
    DISALLOW_COPY_AND_ASSIGN(LaunchWithProfile);
  };

 private:
  // Returns the list of URLs to open from the command line. The returned
  // vector is empty if the user didn't specify any URLs on the command line.
  static std::vector<GURL> GetURLsFromCommandLine(
      const CommandLine& command_line,
      const FilePath& cur_dir,
      Profile* profile);

  static bool ProcessCmdLineImpl(const CommandLine& command_line,
                                 const FilePath& cur_dir, bool process_startup,
                                 Profile* profile, int* return_code,
                                 BrowserInit* browser_init);

  // Additional tabs to open during first run.
  std::vector<GURL> first_run_tabs_;

  DISALLOW_COPY_AND_ASSIGN(BrowserInit);
};

#endif  // CHROME_BROWSER_UI_BROWSER_INIT_H_