Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 <memory> | 5 #include <memory> |
| 6 #include <sstream> | 6 #include <sstream> |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/base64.h" | 9 #include "base/base64.h" |
| 10 #include "base/base_switches.h" | 10 #include "base/base_switches.h" |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/callback.h" | 12 #include "base/callback.h" |
| 13 #include "base/command_line.h" | 13 #include "base/command_line.h" |
| 14 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
| 15 #include "base/json/json_writer.h" | 15 #include "base/json/json_writer.h" |
| 16 #include "base/location.h" | 16 #include "base/location.h" |
| 17 #include "base/memory/weak_ptr.h" | 17 #include "base/memory/weak_ptr.h" |
| 18 #include "base/numerics/safe_conversions.h" | 18 #include "base/numerics/safe_conversions.h" |
| 19 #include "base/strings/string_number_conversions.h" | 19 #include "base/strings/string_number_conversions.h" |
| 20 #include "headless/app/headless_shell.h" | 20 #include "headless/app/headless_shell.h" |
| 21 #include "headless/app/headless_shell_switches.h" | 21 #include "headless/app/headless_shell_switches.h" |
| 22 #include "headless/lib/browser/headless_devtools.h" | |
| 22 #include "headless/public/headless_devtools_target.h" | 23 #include "headless/public/headless_devtools_target.h" |
| 23 #include "headless/public/util/deterministic_http_protocol_handler.h" | 24 #include "headless/public/util/deterministic_http_protocol_handler.h" |
| 24 #include "net/base/io_buffer.h" | 25 #include "net/base/io_buffer.h" |
| 25 #include "net/base/ip_address.h" | 26 #include "net/base/ip_address.h" |
| 26 #include "net/base/net_errors.h" | 27 #include "net/base/net_errors.h" |
| 27 #include "ui/gfx/geometry/size.h" | 28 #include "ui/gfx/geometry/size.h" |
| 28 | 29 |
| 29 namespace headless { | 30 namespace headless { |
| 30 namespace { | 31 namespace { |
| 31 // Address where to listen to incoming DevTools connections. | 32 // Address where to listen to incoming DevTools connections. |
| (...skipping 353 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 385 } | 386 } |
| 386 } | 387 } |
| 387 | 388 |
| 388 void HeadlessShell::OnScreenshotFileClosed(base::File::Error error_code) { | 389 void HeadlessShell::OnScreenshotFileClosed(base::File::Error error_code) { |
| 389 Shutdown(); | 390 Shutdown(); |
| 390 } | 391 } |
| 391 | 392 |
| 392 bool HeadlessShell::RemoteDebuggingEnabled() const { | 393 bool HeadlessShell::RemoteDebuggingEnabled() const { |
| 393 const base::CommandLine& command_line = | 394 const base::CommandLine& command_line = |
| 394 *base::CommandLine::ForCurrentProcess(); | 395 *base::CommandLine::ForCurrentProcess(); |
| 395 return command_line.HasSwitch(switches::kRemoteDebuggingPort); | 396 return (command_line.HasSwitch(switches::kRemoteDebuggingPort) || |
| 397 command_line.HasSwitch(switches::kRemoteDebuggingSocketFd)); | |
| 396 } | 398 } |
| 397 | 399 |
| 398 bool ValidateCommandLine(const base::CommandLine& command_line) { | 400 bool ValidateCommandLine(const base::CommandLine& command_line) { |
| 399 if (!command_line.HasSwitch(switches::kRemoteDebuggingPort)) { | 401 #if !defined(OS_POSIX) |
| 402 if (command_line.HasSwitch(switches::kRemoteDebuggingSocketFd)) { | |
| 403 LOG(ERROR) << "remote-debugging-socket can't be set on non-Posix systems"; | |
|
Sami
2017/04/13 09:34:28
nit: Start with a capital letter please.
Raul Vera
2017/04/18 00:47:24
Done. I did this on purpose because the name of th
| |
| 404 return false; | |
| 405 } | |
| 406 #endif | |
| 407 if (command_line.HasSwitch(switches::kRemoteDebuggingPort) && | |
| 408 command_line.HasSwitch(switches::kRemoteDebuggingSocketFd)) { | |
| 409 LOG(ERROR) << "remote-debugging-port and remote-debugging-socket " | |
|
Sami
2017/04/13 09:34:28
Ditto.
Raul Vera
2017/04/18 00:47:24
Done.
| |
| 410 << "can't both be set."; | |
| 411 return false; | |
| 412 } | |
| 413 if (!command_line.HasSwitch(switches::kRemoteDebuggingPort) && | |
| 414 !command_line.HasSwitch(switches::kRemoteDebuggingSocketFd)) { | |
| 400 if (command_line.GetArgs().size() <= 1) | 415 if (command_line.GetArgs().size() <= 1) |
| 401 return true; | 416 return true; |
| 402 LOG(ERROR) << "Open multiple tabs is only supported when the " | 417 LOG(ERROR) << "Open multiple tabs is only supported when " |
| 403 << "remote debug port is set."; | 418 << "remote debugging is enabled."; |
| 404 return false; | 419 return false; |
| 405 } | 420 } |
| 406 if (command_line.HasSwitch(switches::kDumpDom)) { | 421 if (command_line.HasSwitch(switches::kDumpDom)) { |
| 407 LOG(ERROR) << "Dump DOM is disabled when remote debugging is enabled."; | 422 LOG(ERROR) << "Dump DOM is disabled when remote debugging is enabled."; |
| 408 return false; | 423 return false; |
| 409 } | 424 } |
| 410 if (command_line.HasSwitch(switches::kRepl)) { | 425 if (command_line.HasSwitch(switches::kRepl)) { |
| 411 LOG(ERROR) << "Evaluate Javascript is disabled " | 426 LOG(ERROR) << "Evaluate Javascript is disabled " |
| 412 << "when remote debugging is enabled."; | 427 << "when remote debugging is enabled."; |
| 413 return false; | 428 return false; |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 429 } | 444 } |
| 430 return true; | 445 return true; |
| 431 } | 446 } |
| 432 | 447 |
| 433 int HeadlessShellMain(int argc, const char** argv) { | 448 int HeadlessShellMain(int argc, const char** argv) { |
| 434 base::CommandLine::Init(argc, argv); | 449 base::CommandLine::Init(argc, argv); |
| 435 RunChildProcessIfNeeded(argc, argv); | 450 RunChildProcessIfNeeded(argc, argv); |
| 436 HeadlessShell shell; | 451 HeadlessShell shell; |
| 437 HeadlessBrowser::Options::Builder builder(argc, argv); | 452 HeadlessBrowser::Options::Builder builder(argc, argv); |
| 438 | 453 |
| 439 // Enable devtools if requested. | |
| 440 const base::CommandLine& command_line( | 454 const base::CommandLine& command_line( |
| 441 *base::CommandLine::ForCurrentProcess()); | 455 *base::CommandLine::ForCurrentProcess()); |
| 442 if (!ValidateCommandLine(command_line)) | 456 if (!ValidateCommandLine(command_line)) |
| 443 return EXIT_FAILURE; | 457 return EXIT_FAILURE; |
| 444 | 458 |
| 445 if (command_line.HasSwitch(::switches::kEnableCrashReporter)) | 459 if (command_line.HasSwitch(::switches::kEnableCrashReporter)) |
| 446 builder.SetCrashReporterEnabled(true); | 460 builder.SetCrashReporterEnabled(true); |
| 447 if (command_line.HasSwitch(switches::kCrashDumpsDir)) { | 461 if (command_line.HasSwitch(switches::kCrashDumpsDir)) { |
| 448 builder.SetCrashDumpsDir( | 462 builder.SetCrashDumpsDir( |
| 449 command_line.GetSwitchValuePath(switches::kCrashDumpsDir)); | 463 command_line.GetSwitchValuePath(switches::kCrashDumpsDir)); |
| 450 } | 464 } |
| 451 | 465 |
| 466 // Enable devtools if requested, either by specifying a port (and optional | |
| 467 // address), or by specifying the fd of an already-open socket. | |
| 452 if (command_line.HasSwitch(::switches::kRemoteDebuggingPort)) { | 468 if (command_line.HasSwitch(::switches::kRemoteDebuggingPort)) { |
| 453 std::string address = kDevToolsHttpServerAddress; | 469 std::string address = kDevToolsHttpServerAddress; |
| 454 if (command_line.HasSwitch(switches::kRemoteDebuggingAddress)) { | 470 if (command_line.HasSwitch(switches::kRemoteDebuggingAddress)) { |
| 455 address = | 471 address = |
| 456 command_line.GetSwitchValueASCII(switches::kRemoteDebuggingAddress); | 472 command_line.GetSwitchValueASCII(switches::kRemoteDebuggingAddress); |
| 457 net::IPAddress parsed_address; | 473 net::IPAddress parsed_address; |
| 458 if (!net::ParseURLHostnameToAddress(address, &parsed_address)) { | 474 if (!net::ParseURLHostnameToAddress(address, &parsed_address)) { |
| 459 LOG(ERROR) << "Invalid devtools server address"; | 475 LOG(ERROR) << "Invalid devtools server address"; |
| 460 return EXIT_FAILURE; | 476 return EXIT_FAILURE; |
| 461 } | 477 } |
| 462 } | 478 } |
| 463 int parsed_port; | 479 int parsed_port; |
| 464 std::string port_str = | 480 std::string port_str = |
| 465 command_line.GetSwitchValueASCII(::switches::kRemoteDebuggingPort); | 481 command_line.GetSwitchValueASCII(::switches::kRemoteDebuggingPort); |
| 466 if (!base::StringToInt(port_str, &parsed_port) || | 482 if (!base::StringToInt(port_str, &parsed_port) || |
| 467 !base::IsValueInRangeForNumericType<uint16_t>(parsed_port)) { | 483 !base::IsValueInRangeForNumericType<uint16_t>(parsed_port)) { |
| 468 LOG(ERROR) << "Invalid devtools server port"; | 484 LOG(ERROR) << "Invalid devtools server port"; |
| 469 return EXIT_FAILURE; | 485 return EXIT_FAILURE; |
| 470 } | 486 } |
| 471 net::IPAddress devtools_address; | 487 net::IPAddress devtools_address; |
| 472 bool result = devtools_address.AssignFromIPLiteral(address); | 488 bool result = devtools_address.AssignFromIPLiteral(address); |
| 473 DCHECK(result); | 489 DCHECK(result); |
| 474 builder.EnableDevToolsServer(net::IPEndPoint( | 490 const net::IPEndPoint endpoint(devtools_address, |
| 475 devtools_address, base::checked_cast<uint16_t>(parsed_port))); | 491 base::checked_cast<uint16_t>(parsed_port)); |
| 492 builder.EnableDevToolsServer(endpoint); | |
| 493 } else if (command_line.HasSwitch(switches::kRemoteDebuggingSocketFd)) { | |
| 494 int parsed_fd; | |
| 495 std::string fd_str = | |
| 496 command_line.GetSwitchValueASCII(switches::kRemoteDebuggingSocketFd); | |
| 497 if (!base::StringToInt(fd_str, &parsed_fd) || | |
| 498 !base::IsValueInRangeForNumericType<size_t>(parsed_fd)) { | |
| 499 LOG(ERROR) << "Invalid devtools server socket fd"; | |
| 500 return EXIT_FAILURE; | |
| 501 } | |
| 502 builder.EnableDevToolsServer(base::checked_cast<uint16_t>(parsed_fd)); | |
|
Sami
2017/04/13 09:34:28
Should we be casting to size_t instead since that'
Raul Vera
2017/04/18 00:47:25
Done. Converted from uint16_t to size_t and missed
| |
| 476 } | 503 } |
| 477 | 504 |
| 478 if (command_line.HasSwitch(switches::kProxyServer)) { | 505 if (command_line.HasSwitch(switches::kProxyServer)) { |
| 479 std::string proxy_server = | 506 std::string proxy_server = |
| 480 command_line.GetSwitchValueASCII(switches::kProxyServer); | 507 command_line.GetSwitchValueASCII(switches::kProxyServer); |
| 481 net::HostPortPair parsed_proxy_server = | 508 net::HostPortPair parsed_proxy_server = |
| 482 net::HostPortPair::FromString(proxy_server); | 509 net::HostPortPair::FromString(proxy_server); |
| 483 if (parsed_proxy_server.host().empty() || !parsed_proxy_server.port()) { | 510 if (parsed_proxy_server.host().empty() || !parsed_proxy_server.port()) { |
| 484 LOG(ERROR) << "Malformed proxy server url"; | 511 LOG(ERROR) << "Malformed proxy server url"; |
| 485 return EXIT_FAILURE; | 512 return EXIT_FAILURE; |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 518 builder.SetOverrideWebPreferencesCallback(base::Bind([]( | 545 builder.SetOverrideWebPreferencesCallback(base::Bind([]( |
| 519 WebPreferences* preferences) { preferences->hide_scrollbars = true; })); | 546 WebPreferences* preferences) { preferences->hide_scrollbars = true; })); |
| 520 } | 547 } |
| 521 | 548 |
| 522 return HeadlessBrowserMain( | 549 return HeadlessBrowserMain( |
| 523 builder.Build(), | 550 builder.Build(), |
| 524 base::Bind(&HeadlessShell::OnStart, base::Unretained(&shell))); | 551 base::Bind(&HeadlessShell::OnStart, base::Unretained(&shell))); |
| 525 } | 552 } |
| 526 | 553 |
| 527 } // namespace headless | 554 } // namespace headless |
| OLD | NEW |