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