OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/test/chromedriver/chrome_launcher.h" | 5 #include "chrome/test/chromedriver/chrome_launcher.h" |
6 | 6 |
7 #include "base/base64.h" | 7 #include "base/base64.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
(...skipping 16 matching lines...) Expand all Loading... |
27 #include "chrome/test/chromedriver/chrome/devtools_http_client.h" | 27 #include "chrome/test/chromedriver/chrome/devtools_http_client.h" |
28 #include "chrome/test/chromedriver/chrome/status.h" | 28 #include "chrome/test/chromedriver/chrome/status.h" |
29 #include "chrome/test/chromedriver/chrome/user_data_dir.h" | 29 #include "chrome/test/chromedriver/chrome/user_data_dir.h" |
30 #include "chrome/test/chromedriver/chrome/version.h" | 30 #include "chrome/test/chromedriver/chrome/version.h" |
31 #include "chrome/test/chromedriver/chrome/zip.h" | 31 #include "chrome/test/chromedriver/chrome/zip.h" |
32 #include "chrome/test/chromedriver/net/url_request_context_getter.h" | 32 #include "chrome/test/chromedriver/net/url_request_context_getter.h" |
33 | 33 |
34 namespace { | 34 namespace { |
35 | 35 |
36 Status PrepareCommandLine(int port, | 36 Status PrepareCommandLine(int port, |
37 const base::FilePath& exe, | 37 const Capabilities& capabilities, |
38 const base::ListValue* args, | |
39 const base::ListValue* extensions, | |
40 const base::DictionaryValue* prefs, | |
41 const base::DictionaryValue* local_state, | |
42 CommandLine* prepared_command, | 38 CommandLine* prepared_command, |
43 base::ScopedTempDir* user_data_dir, | 39 base::ScopedTempDir* user_data_dir, |
44 base::ScopedTempDir* extension_dir) { | 40 base::ScopedTempDir* extension_dir) { |
45 base::FilePath program = exe; | 41 base::FilePath program = capabilities.chrome_exe; |
46 if (program.empty()) { | 42 if (program.empty()) { |
47 if (!FindChrome(&program)) | 43 if (!FindChrome(&program)) |
48 return Status(kUnknownError, "cannot find Chrome binary"); | 44 return Status(kUnknownError, "cannot find Chrome binary"); |
49 } | 45 } |
50 LOG(INFO) << "Using chrome from " << program.value(); | 46 LOG(INFO) << "Using chrome from " << program.value(); |
51 | 47 |
52 CommandLine command(program); | 48 CommandLine command(program); |
53 command.AppendSwitchASCII("remote-debugging-port", base::IntToString(port)); | 49 command.AppendSwitchASCII("remote-debugging-port", base::IntToString(port)); |
54 command.AppendSwitch("no-first-run"); | 50 command.AppendSwitch("no-first-run"); |
55 command.AppendSwitch("enable-logging"); | 51 command.AppendSwitch("enable-logging"); |
56 command.AppendSwitchASCII("logging-level", "1"); | 52 command.AppendSwitchASCII("logging-level", "1"); |
57 command.AppendArg("data:text/html;charset=utf-8,"); | 53 command.AppendArg("data:text/html;charset=utf-8,"); |
58 | 54 |
59 if (args) { | 55 if (!capabilities.args.empty()) { |
60 Status status = internal::ProcessCommandLineArgs(args, &command); | 56 Status status = internal::ProcessCommandLineArgs( |
| 57 &capabilities.args, &command); |
61 if (status.IsError()) | 58 if (status.IsError()) |
62 return status; | 59 return status; |
63 } | 60 } |
64 | 61 |
65 if (!command.HasSwitch("user-data-dir")) { | 62 if (!command.HasSwitch("user-data-dir")) { |
66 if (!user_data_dir->CreateUniqueTempDir()) | 63 if (!user_data_dir->CreateUniqueTempDir()) |
67 return Status(kUnknownError, "cannot create temp dir for user data dir"); | 64 return Status(kUnknownError, "cannot create temp dir for user data dir"); |
68 command.AppendSwitchPath("user-data-dir", user_data_dir->path()); | 65 command.AppendSwitchPath("user-data-dir", user_data_dir->path()); |
69 Status status = internal::PrepareUserDataDir( | 66 Status status = internal::PrepareUserDataDir( |
70 user_data_dir->path(), prefs, local_state); | 67 user_data_dir->path(), capabilities.prefs, capabilities.local_state); |
71 if (status.IsError()) | 68 if (status.IsError()) |
72 return status; | 69 return status; |
73 } | 70 } |
74 | 71 |
75 if (extensions) { | 72 if (capabilities.extensions) { |
76 if (!extension_dir->CreateUniqueTempDir()) | 73 if (!extension_dir->CreateUniqueTempDir()) |
77 return Status(kUnknownError, | 74 return Status(kUnknownError, |
78 "cannot create temp dir for unpacking extensions"); | 75 "cannot create temp dir for unpacking extensions"); |
79 Status status = internal::ProcessExtensions( | 76 Status status = internal::ProcessExtensions( |
80 extensions, extension_dir->path(), &command); | 77 capabilities.extensions, extension_dir->path(), &command); |
81 if (status.IsError()) | 78 if (status.IsError()) |
82 return status; | 79 return status; |
83 } | 80 } |
84 *prepared_command = command; | 81 *prepared_command = command; |
85 return Status(kOk); | 82 return Status(kOk); |
86 } | 83 } |
87 | 84 |
88 Status ParseAndCheckVersion(const std::string& devtools_version, | 85 Status ParseAndCheckVersion(const std::string& devtools_version, |
89 std::string* version, | 86 std::string* version, |
90 int* build_no) { | 87 int* build_no) { |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
152 client->GetWebViewsInfo(&views_info); | 149 client->GetWebViewsInfo(&views_info); |
153 if (views_info.GetSize()) { | 150 if (views_info.GetSize()) { |
154 *user_client = client.Pass(); | 151 *user_client = client.Pass(); |
155 return Status(kOk); | 152 return Status(kOk); |
156 } | 153 } |
157 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(100)); | 154 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(100)); |
158 } | 155 } |
159 return Status(kUnknownError, "unable to discover open pages"); | 156 return Status(kUnknownError, "unable to discover open pages"); |
160 } | 157 } |
161 | 158 |
162 } // namespace | |
163 | |
164 Status LaunchDesktopChrome(URLRequestContextGetter* context_getter, | 159 Status LaunchDesktopChrome(URLRequestContextGetter* context_getter, |
165 int port, | 160 int port, |
166 const SyncWebSocketFactory& socket_factory, | 161 const SyncWebSocketFactory& socket_factory, |
167 const base::FilePath& exe, | 162 const Capabilities& capabilities, |
168 const base::ListValue* args, | |
169 const base::ListValue* extensions, | |
170 const base::DictionaryValue* prefs, | |
171 const base::DictionaryValue* local_state, | |
172 const std::string& log_path, | |
173 scoped_ptr<Chrome>* chrome) { | 163 scoped_ptr<Chrome>* chrome) { |
174 CommandLine command(CommandLine::NO_PROGRAM); | 164 CommandLine command(CommandLine::NO_PROGRAM); |
175 base::ScopedTempDir user_data_dir; | 165 base::ScopedTempDir user_data_dir; |
176 base::ScopedTempDir extension_dir; | 166 base::ScopedTempDir extension_dir; |
177 PrepareCommandLine(port, exe, args, extensions, prefs, local_state, | 167 PrepareCommandLine(port, capabilities, |
178 &command, &user_data_dir, &extension_dir); | 168 &command, &user_data_dir, &extension_dir); |
179 base::LaunchOptions options; | 169 base::LaunchOptions options; |
180 | 170 |
181 #if !defined(OS_WIN) | 171 #if !defined(OS_WIN) |
182 base::EnvironmentVector environ; | 172 base::EnvironmentVector environ; |
183 if (!log_path.empty()) { | 173 if (!capabilities.log_path.empty()) { |
184 environ.push_back(base::EnvironmentVector::value_type("CHROME_LOG_FILE", | 174 environ.push_back( |
185 log_path)); | 175 base::EnvironmentVector::value_type("CHROME_LOG_FILE", |
| 176 capabilities.log_path)); |
186 options.environ = &environ; | 177 options.environ = &environ; |
187 } | 178 } |
188 #endif | 179 #endif |
189 | 180 |
190 base::ProcessHandle process; | 181 base::ProcessHandle process; |
191 if (!base::LaunchProcess(command, options, &process)) | 182 if (!base::LaunchProcess(command, options, &process)) |
192 return Status(kUnknownError, "chrome failed to start"); | 183 return Status(kUnknownError, "chrome failed to start"); |
193 | 184 |
194 scoped_ptr<DevToolsHttpClient> devtools_client; | 185 scoped_ptr<DevToolsHttpClient> devtools_client; |
195 std::string version; | 186 std::string version; |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
234 } | 225 } |
235 chrome->reset(new ChromeDesktopImpl( | 226 chrome->reset(new ChromeDesktopImpl( |
236 devtools_client.Pass(), version, build_no, process, &user_data_dir, | 227 devtools_client.Pass(), version, build_no, process, &user_data_dir, |
237 &extension_dir)); | 228 &extension_dir)); |
238 return Status(kOk); | 229 return Status(kOk); |
239 } | 230 } |
240 | 231 |
241 Status LaunchAndroidChrome(URLRequestContextGetter* context_getter, | 232 Status LaunchAndroidChrome(URLRequestContextGetter* context_getter, |
242 int port, | 233 int port, |
243 const SyncWebSocketFactory& socket_factory, | 234 const SyncWebSocketFactory& socket_factory, |
244 const std::string& package_name, | 235 const Capabilities& capabilities, |
245 scoped_ptr<Chrome>* chrome) { | 236 scoped_ptr<Chrome>* chrome) { |
246 // TODO(frankf): Figure out how this should be installed to | 237 // TODO(frankf): Figure out how this should be installed to |
247 // make this work for all platforms. | 238 // make this work for all platforms. |
248 base::FilePath adb_commands(FILE_PATH_LITERAL("adb_commands.py")); | 239 base::FilePath adb_commands(FILE_PATH_LITERAL("adb_commands.py")); |
249 CommandLine command(adb_commands); | 240 CommandLine command(adb_commands); |
250 command.AppendSwitchASCII("package", package_name); | 241 command.AppendSwitchASCII("package", capabilities.android_package); |
251 command.AppendSwitch("launch"); | 242 command.AppendSwitch("launch"); |
252 command.AppendSwitchASCII("port", base::IntToString(port)); | 243 command.AppendSwitchASCII("port", base::IntToString(port)); |
253 | 244 |
254 std::string output; | 245 std::string output; |
255 if (!base::GetAppOutput(command, &output)) { | 246 if (!base::GetAppOutput(command, &output)) { |
256 if (output.empty()) | 247 if (output.empty()) |
257 return Status( | 248 return Status( |
258 kUnknownError, | 249 kUnknownError, |
259 "failed to run adb_commands.py. Make sure it is set in PATH."); | 250 "failed to run adb_commands.py. Make sure it is set in PATH."); |
260 else | 251 else |
261 return Status(kUnknownError, "android app failed to start.\n" + output); | 252 return Status(kUnknownError, "android app failed to start.\n" + output); |
262 } | 253 } |
263 | 254 |
264 scoped_ptr<DevToolsHttpClient> devtools_client; | 255 scoped_ptr<DevToolsHttpClient> devtools_client; |
265 std::string version; | 256 std::string version; |
266 int build_no; | 257 int build_no; |
267 Status status = WaitForDevToolsAndCheckVersion( | 258 Status status = WaitForDevToolsAndCheckVersion( |
268 port, context_getter, socket_factory, &devtools_client, &version, | 259 port, context_getter, socket_factory, &devtools_client, &version, |
269 &build_no); | 260 &build_no); |
270 if (status.IsError()) | 261 if (status.IsError()) |
271 return status; | 262 return status; |
272 | 263 |
273 chrome->reset(new ChromeAndroidImpl( | 264 chrome->reset(new ChromeAndroidImpl( |
274 devtools_client.Pass(), version, build_no)); | 265 devtools_client.Pass(), version, build_no)); |
275 return Status(kOk); | 266 return Status(kOk); |
276 } | 267 } |
277 | 268 |
| 269 } // namespace |
| 270 |
| 271 Status LaunchChrome(URLRequestContextGetter* context_getter, |
| 272 int port, |
| 273 const SyncWebSocketFactory& socket_factory, |
| 274 const Capabilities& capabilities, |
| 275 scoped_ptr<Chrome>* chrome) { |
| 276 if (capabilities.HasAndroidPackage()) { |
| 277 return LaunchAndroidChrome( |
| 278 context_getter, port, socket_factory, capabilities, chrome); |
| 279 } else { |
| 280 return LaunchDesktopChrome( |
| 281 context_getter, port, socket_factory, capabilities, chrome); |
| 282 } |
| 283 } |
| 284 |
278 namespace internal { | 285 namespace internal { |
279 | 286 |
280 Status ProcessCommandLineArgs(const base::ListValue* args, | 287 Status ProcessCommandLineArgs(const base::ListValue* args, |
281 CommandLine* command) { | 288 CommandLine* command) { |
282 for (size_t i = 0; i < args->GetSize(); ++i) { | 289 for (size_t i = 0; i < args->GetSize(); ++i) { |
283 std::string arg_string; | 290 std::string arg_string; |
284 if (!args->GetString(i, &arg_string)) | 291 if (!args->GetString(i, &arg_string)) |
285 return Status(kUnknownError, "invalid chrome command line argument"); | 292 return Status(kUnknownError, "invalid chrome command line argument"); |
286 size_t separator_index = arg_string.find("="); | 293 size_t separator_index = arg_string.find("="); |
287 if (separator_index != std::string::npos) { | 294 if (separator_index != std::string::npos) { |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
398 // Write empty "First Run" file, otherwise Chrome will wipe the default | 405 // Write empty "First Run" file, otherwise Chrome will wipe the default |
399 // profile that was written. | 406 // profile that was written. |
400 if (file_util::WriteFile( | 407 if (file_util::WriteFile( |
401 user_data_dir.AppendASCII("First Run"), "", 0) != 0) { | 408 user_data_dir.AppendASCII("First Run"), "", 0) != 0) { |
402 return Status(kUnknownError, "failed to write first run file"); | 409 return Status(kUnknownError, "failed to write first run file"); |
403 } | 410 } |
404 return Status(kOk); | 411 return Status(kOk); |
405 } | 412 } |
406 | 413 |
407 } // namespace internal | 414 } // namespace internal |
OLD | NEW |