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.
5 #ifndef CHROME_TEST_AUTOMATION_PROXY_LAUNCHER_H_
6 #define CHROME_TEST_AUTOMATION_PROXY_LAUNCHER_H_
10 #include "base/basictypes.h"
11 #include "base/callback.h"
12 #include "base/command_line.h"
13 #include "base/compiler_specific.h"
14 #include "base/files/scoped_temp_dir.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/process/process.h"
17 #include "base/time/time.h"
19 class AutomationProxy
;
21 // Base class for all ProxyLauncher implementations. Contains functionality
22 // fo launching, terminating, and connecting tests to browser processes. This
23 // class determines which AutomationProxy implementation is used by a test.
24 // Command line arguments passed to the browser are set in this class.
26 // Subclass from this class to use a different AutomationProxy
27 // implementation or to override browser launching behavior.
30 // Default ID for named testing interface.
31 static const char kDefaultInterfaceId
[];
33 // Different ways to quit the browser.
40 // POD containing state variables that determine how to launch browser.
42 // If true the profile is cleared before launching.
45 // If set, the profiles in this path are copied
46 // into the user data directory for the test.
47 base::FilePath template_user_data
;
49 // Called just before starting the browser to allow any setup of the
50 // profile for the run time environment.
51 base::Closure setup_profile_callback
;
53 // Command line to launch the browser.
56 // Should we supply the testing channel id on the command line?
57 bool include_testing_id
;
59 // If true, the window is shown. Otherwise it is hidden.
65 virtual ~ProxyLauncher();
67 // Launches the browser if needed and establishes a connection with it.
68 // Returns true on success.
69 virtual bool InitializeConnection(
70 const LaunchState
& state
,
71 bool wait_for_initial_loads
) WARN_UNUSED_RESULT
= 0;
73 // Shuts down the browser if needed and destroys any
74 // connections established by InitalizeConnection.
75 virtual void TerminateConnection() = 0;
77 // Launches the browser and IPC testing connection in server mode.
78 // Returns true on success.
79 bool LaunchBrowserAndServer(const LaunchState
& state
,
80 bool wait_for_initial_loads
) WARN_UNUSED_RESULT
;
82 // Launches the IPC testing connection in client mode,
83 // which then attempts to connect to a browser.
84 // Returns true on success.
85 bool ConnectToRunningBrowser(bool wait_for_initial_loads
) WARN_UNUSED_RESULT
;
87 // Paired with LaunchBrowserAndServer().
88 // Closes the browser and IPC testing server.
89 void CloseBrowserAndServer();
91 // Launches the browser with the given command line. Returns true on success.
92 // TODO(phajdan.jr): Make LaunchBrowser private.
93 bool LaunchBrowser(const LaunchState
& state
) WARN_UNUSED_RESULT
;
95 // Exits out of browser instance.
98 // Terminates the browser, simulates end of session.
99 void TerminateBrowser();
101 // Check that no processes related to Chrome exist, displaying
102 // the given message if any do.
103 void AssertAppNotRunning(const std::string
& error_message
);
105 // Wait for the browser process to shut down on its own (i.e. as a result of
106 // some action that your test has taken). If it has exited within |timeout|,
107 // puts the exit code in |exit_code| and returns true.
108 bool WaitForBrowserProcessToQuit(base::TimeDelta timeout
, int* exit_code
);
110 AutomationProxy
* automation() const;
112 // Return the user data directory being used by the browser instance.
113 base::FilePath
user_data_dir() const;
115 // Get the handle of browser process connected to the automation. This
116 // function only returns a reference to the handle so the caller does not
117 // own the handle returned.
118 base::ProcessHandle
process() const;
120 // Return the process id of the browser process (-1 on error).
121 base::ProcessId
process_id() const;
123 // Return the time when the browser was run.
124 base::TimeTicks
browser_launch_time() const;
126 // Return how long the shutdown took.
127 base::TimeDelta
browser_quit_time() const;
129 // Sets the shutdown type, which defaults to WINDOW_CLOSE.
130 void set_shutdown_type(ShutdownType value
) {
131 shutdown_type_
= value
;
135 // Creates an automation proxy.
136 virtual AutomationProxy
* CreateAutomationProxy(
137 base::TimeDelta execution_timeout
) = 0;
139 // Returns the automation proxy's channel with any prefixes prepended,
140 // for passing as a command line parameter over to the browser.
141 virtual std::string
PrefixedChannelID() const = 0;
143 // Paired with ConnectToRunningBrowser().
144 // Disconnects the testing IPC from the browser.
145 void DisconnectFromRunningBrowser();
148 bool WaitForBrowserLaunch(bool wait_for_initial_loads
) WARN_UNUSED_RESULT
;
150 // Prepare command line that will be used to launch the child browser process.
151 void PrepareTestCommandline(CommandLine
* command_line
,
152 bool include_testing_id
);
154 bool LaunchBrowserHelper(const LaunchState
& state
,
157 base::ProcessHandle
* process
) WARN_UNUSED_RESULT
;
159 scoped_ptr
<AutomationProxy
> automation_proxy_
;
161 // We use a temporary directory for profile to avoid issues with being
162 // unable to delete some files because they're in use, etc.
163 base::ScopedTempDir temp_profile_dir_
;
165 // Handle to the first Chrome process.
166 base::ProcessHandle process_
;
168 // PID of |process_| (for debugging).
169 base::ProcessId process_id_
;
171 // Time when the browser was run.
172 base::TimeTicks browser_launch_time_
;
174 // How long the shutdown took.
175 base::TimeDelta browser_quit_time_
;
177 // The method for shutting down the browser. Used in ShutdownTest.
178 ShutdownType shutdown_type_
;
180 // If true, runs the renderer outside the sandbox.
183 // If true, write full memory dump during crash.
184 bool full_memory_dump_
;
186 // If true, a user is paying attention to the test, so show error dialogs.
187 bool show_error_dialogs_
;
189 // Enable dchecks in release mode.
192 // Dump process memory on dcheck without crashing.
193 bool silent_dump_on_dcheck_
;
195 // Disable breakpad on the browser.
196 bool disable_breakpad_
;
198 // Flags passed to the JS engine.
199 std::string js_flags_
;
202 std::string log_level_
;
204 DISALLOW_COPY_AND_ASSIGN(ProxyLauncher
);
207 // Uses an automation proxy that communicates over a named socket.
208 // This is used if you want to connect an AutomationProxy
209 // to a browser process that is already running.
210 // The channel id of the proxy is a constant specified by kInterfacePath.
211 class NamedProxyLauncher
: public ProxyLauncher
{
213 // If launch_browser is true, launches Chrome with named interface enabled.
214 // Otherwise, there should be an existing instance the proxy can connect to.
215 NamedProxyLauncher(const std::string
& channel_id
,
216 bool launch_browser
, bool disconnect_on_failure
);
218 virtual AutomationProxy
* CreateAutomationProxy(
219 base::TimeDelta execution_timeout
);
220 virtual bool InitializeConnection(
221 const LaunchState
& state
,
222 bool wait_for_initial_loads
) OVERRIDE WARN_UNUSED_RESULT
;
223 virtual void TerminateConnection();
224 virtual std::string
PrefixedChannelID() const;
227 std::string channel_id_
; // Channel id of automation proxy.
228 bool launch_browser_
; // True if we should launch the browser too.
229 bool disconnect_on_failure_
; // True if we disconnect on IPC channel failure.
232 DISALLOW_COPY_AND_ASSIGN(NamedProxyLauncher
);
235 // Uses an automation proxy that communicates over an anonymous socket.
236 class AnonymousProxyLauncher
: public ProxyLauncher
{
238 explicit AnonymousProxyLauncher(bool disconnect_on_failure
);
239 virtual AutomationProxy
* CreateAutomationProxy(
240 base::TimeDelta execution_timeout
);
241 virtual bool InitializeConnection(
242 const LaunchState
& state
,
243 bool wait_for_initial_loads
) OVERRIDE WARN_UNUSED_RESULT
;
244 virtual void TerminateConnection();
245 virtual std::string
PrefixedChannelID() const;
248 std::string channel_id_
; // Channel id of automation proxy.
249 bool disconnect_on_failure_
; // True if we disconnect on IPC channel failure.
252 DISALLOW_COPY_AND_ASSIGN(AnonymousProxyLauncher
);
255 #endif // CHROME_TEST_AUTOMATION_PROXY_LAUNCHER_H_