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 <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <memory> | |
10 #include <utility> | 11 #include <utility> |
11 #include <vector> | 12 #include <vector> |
12 | 13 |
13 #include "base/base64.h" | 14 #include "base/base64.h" |
14 #include "base/bind.h" | 15 #include "base/bind.h" |
15 #include "base/command_line.h" | 16 #include "base/command_line.h" |
16 #include "base/files/file_path.h" | 17 #include "base/files/file_path.h" |
17 #include "base/files/file_util.h" | 18 #include "base/files/file_util.h" |
18 #include "base/files/scoped_file.h" | 19 #include "base/files/scoped_file.h" |
19 #include "base/format_macros.h" | 20 #include "base/format_macros.h" |
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
237 } | 238 } |
238 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(50)); | 239 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(50)); |
239 } | 240 } |
240 return Status(kUnknownError, "unable to discover open pages"); | 241 return Status(kUnknownError, "unable to discover open pages"); |
241 } | 242 } |
242 | 243 |
243 Status CreateBrowserwideDevToolsClientAndConnect( | 244 Status CreateBrowserwideDevToolsClientAndConnect( |
244 const NetAddress& address, | 245 const NetAddress& address, |
245 const PerfLoggingPrefs& perf_logging_prefs, | 246 const PerfLoggingPrefs& perf_logging_prefs, |
246 const SyncWebSocketFactory& socket_factory, | 247 const SyncWebSocketFactory& socket_factory, |
247 const ScopedVector<DevToolsEventListener>& devtools_event_listeners, | 248 const std::vector<std::unique_ptr<DevToolsEventListener>>& |
249 devtools_event_listeners, | |
248 std::unique_ptr<DevToolsClient>* browser_client) { | 250 std::unique_ptr<DevToolsClient>* browser_client) { |
249 std::unique_ptr<DevToolsClient> client(new DevToolsClientImpl( | 251 std::unique_ptr<DevToolsClient> client(new DevToolsClientImpl( |
250 socket_factory, base::StringPrintf("ws://%s/devtools/browser/", | 252 socket_factory, base::StringPrintf("ws://%s/devtools/browser/", |
251 address.ToString().c_str()), | 253 address.ToString().c_str()), |
252 DevToolsClientImpl::kBrowserwideDevToolsClientId)); | 254 DevToolsClientImpl::kBrowserwideDevToolsClientId)); |
253 for (ScopedVector<DevToolsEventListener>::const_iterator it = | 255 for (const auto& listener : devtools_event_listeners) { |
254 devtools_event_listeners.begin(); | |
255 it != devtools_event_listeners.end(); | |
256 ++it) { | |
257 // Only add listeners that subscribe to the browser-wide |DevToolsClient|. | 256 // Only add listeners that subscribe to the browser-wide |DevToolsClient|. |
258 // Otherwise, listeners will think this client is associated with a webview, | 257 // Otherwise, listeners will think this client is associated with a webview, |
259 // and will send unrecognized commands to it. | 258 // and will send unrecognized commands to it. |
260 if ((*it)->subscribes_to_browser()) | 259 if (listener->subscribes_to_browser()) |
261 client->AddListener(*it); | 260 client->AddListener(listener.get()); |
262 } | 261 } |
263 // Provide the client regardless of whether it connects, so that Chrome always | 262 // Provide the client regardless of whether it connects, so that Chrome always |
264 // has a valid |devtools_websocket_client_|. If not connected, no listeners | 263 // has a valid |devtools_websocket_client_|. If not connected, no listeners |
265 // will be notified, and client will just return kDisconnected errors if used. | 264 // will be notified, and client will just return kDisconnected errors if used. |
266 *browser_client = std::move(client); | 265 *browser_client = std::move(client); |
267 // To avoid unnecessary overhead, only connect if tracing is enabled, since | 266 // To avoid unnecessary overhead, only connect if tracing is enabled, since |
268 // the browser-wide client is currently only used for tracing. | 267 // the browser-wide client is currently only used for tracing. |
269 if (!perf_logging_prefs.trace_categories.empty()) { | 268 if (!perf_logging_prefs.trace_categories.empty()) { |
270 Status status = (*browser_client)->ConnectIfNecessary(); | 269 Status status = (*browser_client)->ConnectIfNecessary(); |
271 if (status.IsError()) | 270 if (status.IsError()) |
272 return status; | 271 return status; |
273 } | 272 } |
274 return Status(kOk); | 273 return Status(kOk); |
275 } | 274 } |
276 | 275 |
277 Status LaunchRemoteChromeSession( | 276 Status LaunchRemoteChromeSession( |
278 URLRequestContextGetter* context_getter, | 277 URLRequestContextGetter* context_getter, |
279 const SyncWebSocketFactory& socket_factory, | 278 const SyncWebSocketFactory& socket_factory, |
280 const Capabilities& capabilities, | 279 const Capabilities& capabilities, |
281 ScopedVector<DevToolsEventListener>* devtools_event_listeners, | 280 std::vector<std::unique_ptr<DevToolsEventListener>>* |
281 devtools_event_listeners, | |
Avi (use Gerrit)
2017/03/29 03:26:55
This is passed by pointer, yes, but down on line 2
leonhsl(Using Gerrit)
2017/03/29 09:22:30
Done.
| |
282 std::unique_ptr<Chrome>* chrome) { | 282 std::unique_ptr<Chrome>* chrome) { |
283 Status status(kOk); | 283 Status status(kOk); |
284 std::unique_ptr<DevToolsHttpClient> devtools_http_client; | 284 std::unique_ptr<DevToolsHttpClient> devtools_http_client; |
285 status = WaitForDevToolsAndCheckVersion( | 285 status = WaitForDevToolsAndCheckVersion( |
286 capabilities.debugger_address, context_getter, socket_factory, | 286 capabilities.debugger_address, context_getter, socket_factory, |
287 &capabilities, &devtools_http_client); | 287 &capabilities, &devtools_http_client); |
288 if (status.IsError()) { | 288 if (status.IsError()) { |
289 return Status(kUnknownError, "cannot connect to chrome at " + | 289 return Status(kUnknownError, "cannot connect to chrome at " + |
290 capabilities.debugger_address.ToString(), | 290 capabilities.debugger_address.ToString(), |
291 status); | 291 status); |
292 } | 292 } |
293 | 293 |
294 std::unique_ptr<DevToolsClient> devtools_websocket_client; | 294 std::unique_ptr<DevToolsClient> devtools_websocket_client; |
295 status = CreateBrowserwideDevToolsClientAndConnect( | 295 status = CreateBrowserwideDevToolsClientAndConnect( |
296 capabilities.debugger_address, capabilities.perf_logging_prefs, | 296 capabilities.debugger_address, capabilities.perf_logging_prefs, |
297 socket_factory, *devtools_event_listeners, &devtools_websocket_client); | 297 socket_factory, *devtools_event_listeners, &devtools_websocket_client); |
298 if (status.IsError()) { | 298 if (status.IsError()) { |
299 LOG(WARNING) << "Browser-wide DevTools client failed to connect: " | 299 LOG(WARNING) << "Browser-wide DevTools client failed to connect: " |
300 << status.message(); | 300 << status.message(); |
301 } | 301 } |
302 | 302 |
303 chrome->reset(new ChromeRemoteImpl(std::move(devtools_http_client), | 303 chrome->reset(new ChromeRemoteImpl(std::move(devtools_http_client), |
304 std::move(devtools_websocket_client), | 304 std::move(devtools_websocket_client), |
305 *devtools_event_listeners, | 305 *devtools_event_listeners, |
306 capabilities.page_load_strategy)); | 306 capabilities.page_load_strategy)); |
307 return Status(kOk); | 307 return Status(kOk); |
308 } | 308 } |
309 | 309 |
310 Status LaunchDesktopChrome( | 310 Status LaunchDesktopChrome(URLRequestContextGetter* context_getter, |
311 URLRequestContextGetter* context_getter, | 311 uint16_t port, |
312 uint16_t port, | 312 std::unique_ptr<PortReservation> port_reservation, |
313 std::unique_ptr<PortReservation> port_reservation, | 313 const SyncWebSocketFactory& socket_factory, |
314 const SyncWebSocketFactory& socket_factory, | 314 const Capabilities& capabilities, |
315 const Capabilities& capabilities, | 315 std::vector<std::unique_ptr<DevToolsEventListener>>* |
316 ScopedVector<DevToolsEventListener>* devtools_event_listeners, | 316 devtools_event_listeners, |
317 std::unique_ptr<Chrome>* chrome, | 317 std::unique_ptr<Chrome>* chrome, |
318 bool w3c_compliant) { | 318 bool w3c_compliant) { |
319 base::CommandLine command(base::CommandLine::NO_PROGRAM); | 319 base::CommandLine command(base::CommandLine::NO_PROGRAM); |
320 base::ScopedTempDir user_data_dir; | 320 base::ScopedTempDir user_data_dir; |
321 base::ScopedTempDir extension_dir; | 321 base::ScopedTempDir extension_dir; |
322 std::vector<std::string> extension_bg_pages; | 322 std::vector<std::string> extension_bg_pages; |
323 Status status = PrepareCommandLine(port, | 323 Status status = PrepareCommandLine(port, |
324 capabilities, | 324 capabilities, |
325 &command, | 325 &command, |
326 &user_data_dir, | 326 &user_data_dir, |
327 &extension_dir, | 327 &extension_dir, |
328 &extension_bg_pages); | 328 &extension_bg_pages); |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
459 return Status(kUnknownError, | 459 return Status(kUnknownError, |
460 "failed to wait for extension background page to load: " + | 460 "failed to wait for extension background page to load: " + |
461 extension_bg_pages[i], | 461 extension_bg_pages[i], |
462 status); | 462 status); |
463 } | 463 } |
464 } | 464 } |
465 *chrome = std::move(chrome_desktop); | 465 *chrome = std::move(chrome_desktop); |
466 return Status(kOk); | 466 return Status(kOk); |
467 } | 467 } |
468 | 468 |
469 Status LaunchAndroidChrome( | 469 Status LaunchAndroidChrome(URLRequestContextGetter* context_getter, |
470 URLRequestContextGetter* context_getter, | 470 uint16_t port, |
471 uint16_t port, | 471 std::unique_ptr<PortReservation> port_reservation, |
472 std::unique_ptr<PortReservation> port_reservation, | 472 const SyncWebSocketFactory& socket_factory, |
473 const SyncWebSocketFactory& socket_factory, | 473 const Capabilities& capabilities, |
474 const Capabilities& capabilities, | 474 std::vector<std::unique_ptr<DevToolsEventListener>>* |
475 ScopedVector<DevToolsEventListener>* devtools_event_listeners, | 475 devtools_event_listeners, |
476 DeviceManager* device_manager, | 476 DeviceManager* device_manager, |
477 std::unique_ptr<Chrome>* chrome) { | 477 std::unique_ptr<Chrome>* chrome) { |
478 Status status(kOk); | 478 Status status(kOk); |
479 std::unique_ptr<Device> device; | 479 std::unique_ptr<Device> device; |
480 if (capabilities.android_device_serial.empty()) { | 480 if (capabilities.android_device_serial.empty()) { |
481 status = device_manager->AcquireDevice(&device); | 481 status = device_manager->AcquireDevice(&device); |
482 } else { | 482 } else { |
483 status = device_manager->AcquireSpecificDevice( | 483 status = device_manager->AcquireSpecificDevice( |
484 capabilities.android_device_serial, &device); | 484 capabilities.android_device_serial, &device); |
485 } | 485 } |
486 if (status.IsError()) | 486 if (status.IsError()) |
487 return status; | 487 return status; |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
528 std::move(devtools_websocket_client), | 528 std::move(devtools_websocket_client), |
529 *devtools_event_listeners, | 529 *devtools_event_listeners, |
530 std::move(port_reservation), | 530 std::move(port_reservation), |
531 capabilities.page_load_strategy, | 531 capabilities.page_load_strategy, |
532 std::move(device))); | 532 std::move(device))); |
533 return Status(kOk); | 533 return Status(kOk); |
534 } | 534 } |
535 | 535 |
536 } // namespace | 536 } // namespace |
537 | 537 |
538 Status LaunchChrome( | 538 Status LaunchChrome(URLRequestContextGetter* context_getter, |
539 URLRequestContextGetter* context_getter, | 539 const SyncWebSocketFactory& socket_factory, |
540 const SyncWebSocketFactory& socket_factory, | 540 DeviceManager* device_manager, |
541 DeviceManager* device_manager, | 541 PortServer* port_server, |
542 PortServer* port_server, | 542 PortManager* port_manager, |
543 PortManager* port_manager, | 543 const Capabilities& capabilities, |
544 const Capabilities& capabilities, | 544 std::vector<std::unique_ptr<DevToolsEventListener>>* |
545 ScopedVector<DevToolsEventListener>* devtools_event_listeners, | 545 devtools_event_listeners, |
546 std::unique_ptr<Chrome>* chrome, | 546 std::unique_ptr<Chrome>* chrome, |
547 bool w3c_compliant) { | 547 bool w3c_compliant) { |
548 if (capabilities.IsRemoteBrowser()) { | 548 if (capabilities.IsRemoteBrowser()) { |
549 return LaunchRemoteChromeSession( | 549 return LaunchRemoteChromeSession( |
550 context_getter, socket_factory, | 550 context_getter, socket_factory, |
551 capabilities, devtools_event_listeners, chrome); | 551 capabilities, devtools_event_listeners, chrome); |
552 } | 552 } |
553 | 553 |
554 uint16_t port = 0; | 554 uint16_t port = 0; |
555 std::unique_ptr<PortReservation> port_reservation; | 555 std::unique_ptr<PortReservation> port_reservation; |
556 Status port_status(kOk); | 556 Status port_status(kOk); |
557 | 557 |
(...skipping 323 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
881 // Write empty "First Run" file, otherwise Chrome will wipe the default | 881 // Write empty "First Run" file, otherwise Chrome will wipe the default |
882 // profile that was written. | 882 // profile that was written. |
883 if (base::WriteFile( | 883 if (base::WriteFile( |
884 user_data_dir.Append(chrome::kFirstRunSentinel), "", 0) != 0) { | 884 user_data_dir.Append(chrome::kFirstRunSentinel), "", 0) != 0) { |
885 return Status(kUnknownError, "failed to write first run file"); | 885 return Status(kUnknownError, "failed to write first run file"); |
886 } | 886 } |
887 return Status(kOk); | 887 return Status(kOk); |
888 } | 888 } |
889 | 889 |
890 } // namespace internal | 890 } // namespace internal |
OLD | NEW |