| 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 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/base64.h" | 10 #include "base/base64.h" |
| 11 #include "base/base_switches.h" | 11 #include "base/base_switches.h" |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/callback.h" | 13 #include "base/callback.h" |
| 14 #include "base/command_line.h" | 14 #include "base/command_line.h" |
| 15 #include "base/files/file_path.h" | 15 #include "base/files/file_path.h" |
| 16 #include "base/json/json_writer.h" | 16 #include "base/json/json_writer.h" |
| 17 #include "base/location.h" | 17 #include "base/location.h" |
| 18 #include "base/memory/weak_ptr.h" | 18 #include "base/memory/weak_ptr.h" |
| 19 #include "base/numerics/safe_conversions.h" | 19 #include "base/numerics/safe_conversions.h" |
| 20 #include "base/strings/string_number_conversions.h" | 20 #include "base/strings/string_number_conversions.h" |
| 21 #include "content/public/common/content_switches.h" | 21 #include "content/public/common/content_switches.h" |
| 22 #include "headless/app/headless_shell.h" | 22 #include "headless/app/headless_shell.h" |
| 23 #include "headless/app/headless_shell_switches.h" | 23 #include "headless/app/headless_shell_switches.h" |
| 24 #include "headless/lib/browser/headless_devtools.h" |
| 24 #include "headless/public/headless_devtools_target.h" | 25 #include "headless/public/headless_devtools_target.h" |
| 25 #include "headless/public/util/deterministic_http_protocol_handler.h" | 26 #include "headless/public/util/deterministic_http_protocol_handler.h" |
| 26 #include "net/base/io_buffer.h" | 27 #include "net/base/io_buffer.h" |
| 27 #include "net/base/ip_address.h" | 28 #include "net/base/ip_address.h" |
| 28 #include "net/base/net_errors.h" | 29 #include "net/base/net_errors.h" |
| 29 #include "net/http/http_util.h" | 30 #include "net/http/http_util.h" |
| 30 #include "ui/gfx/geometry/size.h" | 31 #include "ui/gfx/geometry/size.h" |
| 31 | 32 |
| 32 namespace headless { | 33 namespace headless { |
| 33 namespace { | 34 namespace { |
| (...skipping 409 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 443 } | 444 } |
| 444 } | 445 } |
| 445 | 446 |
| 446 void HeadlessShell::OnFileClosed(base::File::Error error_code) { | 447 void HeadlessShell::OnFileClosed(base::File::Error error_code) { |
| 447 Shutdown(); | 448 Shutdown(); |
| 448 } | 449 } |
| 449 | 450 |
| 450 bool HeadlessShell::RemoteDebuggingEnabled() const { | 451 bool HeadlessShell::RemoteDebuggingEnabled() const { |
| 451 const base::CommandLine& command_line = | 452 const base::CommandLine& command_line = |
| 452 *base::CommandLine::ForCurrentProcess(); | 453 *base::CommandLine::ForCurrentProcess(); |
| 453 return command_line.HasSwitch(switches::kRemoteDebuggingPort); | 454 return (command_line.HasSwitch(switches::kRemoteDebuggingPort) || |
| 455 command_line.HasSwitch(switches::kRemoteDebuggingSocketFd)); |
| 454 } | 456 } |
| 455 | 457 |
| 456 bool ValidateCommandLine(const base::CommandLine& command_line) { | 458 bool ValidateCommandLine(const base::CommandLine& command_line) { |
| 457 if (!command_line.HasSwitch(switches::kRemoteDebuggingPort)) { | 459 #if !defined(OS_POSIX) |
| 460 if (command_line.HasSwitch(switches::kRemoteDebuggingSocketFd)) { |
| 461 LOG(ERROR) << "Remote-debugging-socket can't be set on non-Posix systems"; |
| 462 return false; |
| 463 } |
| 464 #endif |
| 465 if (command_line.HasSwitch(switches::kRemoteDebuggingPort) && |
| 466 command_line.HasSwitch(switches::kRemoteDebuggingSocketFd)) { |
| 467 LOG(ERROR) << "Remote-debugging-port and remote-debugging-socket " |
| 468 << "can't both be set."; |
| 469 return false; |
| 470 } |
| 471 if (!command_line.HasSwitch(switches::kRemoteDebuggingPort) && |
| 472 !command_line.HasSwitch(switches::kRemoteDebuggingSocketFd)) { |
| 458 if (command_line.GetArgs().size() <= 1) | 473 if (command_line.GetArgs().size() <= 1) |
| 459 return true; | 474 return true; |
| 460 LOG(ERROR) << "Open multiple tabs is only supported when the " | 475 LOG(ERROR) << "Open multiple tabs is only supported when " |
| 461 << "remote debug port is set."; | 476 << "remote debugging is enabled."; |
| 462 return false; | 477 return false; |
| 463 } | 478 } |
| 464 if (command_line.HasSwitch(switches::kDefaultBackgroundColor)) { | 479 if (command_line.HasSwitch(switches::kDefaultBackgroundColor)) { |
| 465 LOG(ERROR) << "Setting default background color is disabled " | 480 LOG(ERROR) << "Setting default background color is disabled " |
| 466 << "when remote debugging is enabled."; | 481 << "when remote debugging is enabled."; |
| 467 return false; | 482 return false; |
| 468 } | 483 } |
| 469 if (command_line.HasSwitch(switches::kDumpDom)) { | 484 if (command_line.HasSwitch(switches::kDumpDom)) { |
| 470 LOG(ERROR) << "Dump DOM is disabled when remote debugging is enabled."; | 485 LOG(ERROR) << "Dump DOM is disabled when remote debugging is enabled."; |
| 471 return false; | 486 return false; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 497 } | 512 } |
| 498 return true; | 513 return true; |
| 499 } | 514 } |
| 500 | 515 |
| 501 int HeadlessShellMain(int argc, const char** argv) { | 516 int HeadlessShellMain(int argc, const char** argv) { |
| 502 base::CommandLine::Init(argc, argv); | 517 base::CommandLine::Init(argc, argv); |
| 503 RunChildProcessIfNeeded(argc, argv); | 518 RunChildProcessIfNeeded(argc, argv); |
| 504 HeadlessShell shell; | 519 HeadlessShell shell; |
| 505 HeadlessBrowser::Options::Builder builder(argc, argv); | 520 HeadlessBrowser::Options::Builder builder(argc, argv); |
| 506 | 521 |
| 507 // Enable devtools if requested. | |
| 508 const base::CommandLine& command_line( | 522 const base::CommandLine& command_line( |
| 509 *base::CommandLine::ForCurrentProcess()); | 523 *base::CommandLine::ForCurrentProcess()); |
| 510 if (!ValidateCommandLine(command_line)) | 524 if (!ValidateCommandLine(command_line)) |
| 511 return EXIT_FAILURE; | 525 return EXIT_FAILURE; |
| 512 | 526 |
| 513 if (command_line.HasSwitch(::switches::kEnableCrashReporter)) | 527 if (command_line.HasSwitch(::switches::kEnableCrashReporter)) |
| 514 builder.SetCrashReporterEnabled(true); | 528 builder.SetCrashReporterEnabled(true); |
| 515 if (command_line.HasSwitch(switches::kCrashDumpsDir)) { | 529 if (command_line.HasSwitch(switches::kCrashDumpsDir)) { |
| 516 builder.SetCrashDumpsDir( | 530 builder.SetCrashDumpsDir( |
| 517 command_line.GetSwitchValuePath(switches::kCrashDumpsDir)); | 531 command_line.GetSwitchValuePath(switches::kCrashDumpsDir)); |
| 518 } | 532 } |
| 519 | 533 |
| 534 // Enable devtools if requested, either by specifying a port (and optional |
| 535 // address), or by specifying the fd of an already-open socket. |
| 520 if (command_line.HasSwitch(::switches::kRemoteDebuggingPort)) { | 536 if (command_line.HasSwitch(::switches::kRemoteDebuggingPort)) { |
| 521 std::string address = kDevToolsHttpServerAddress; | 537 std::string address = kDevToolsHttpServerAddress; |
| 522 if (command_line.HasSwitch(switches::kRemoteDebuggingAddress)) { | 538 if (command_line.HasSwitch(switches::kRemoteDebuggingAddress)) { |
| 523 address = | 539 address = |
| 524 command_line.GetSwitchValueASCII(switches::kRemoteDebuggingAddress); | 540 command_line.GetSwitchValueASCII(switches::kRemoteDebuggingAddress); |
| 525 net::IPAddress parsed_address; | 541 net::IPAddress parsed_address; |
| 526 if (!net::ParseURLHostnameToAddress(address, &parsed_address)) { | 542 if (!net::ParseURLHostnameToAddress(address, &parsed_address)) { |
| 527 LOG(ERROR) << "Invalid devtools server address"; | 543 LOG(ERROR) << "Invalid devtools server address"; |
| 528 return EXIT_FAILURE; | 544 return EXIT_FAILURE; |
| 529 } | 545 } |
| 530 } | 546 } |
| 531 int parsed_port; | 547 int parsed_port; |
| 532 std::string port_str = | 548 std::string port_str = |
| 533 command_line.GetSwitchValueASCII(::switches::kRemoteDebuggingPort); | 549 command_line.GetSwitchValueASCII(::switches::kRemoteDebuggingPort); |
| 534 if (!base::StringToInt(port_str, &parsed_port) || | 550 if (!base::StringToInt(port_str, &parsed_port) || |
| 535 !base::IsValueInRangeForNumericType<uint16_t>(parsed_port)) { | 551 !base::IsValueInRangeForNumericType<uint16_t>(parsed_port)) { |
| 536 LOG(ERROR) << "Invalid devtools server port"; | 552 LOG(ERROR) << "Invalid devtools server port"; |
| 537 return EXIT_FAILURE; | 553 return EXIT_FAILURE; |
| 538 } | 554 } |
| 539 net::IPAddress devtools_address; | 555 net::IPAddress devtools_address; |
| 540 bool result = devtools_address.AssignFromIPLiteral(address); | 556 bool result = devtools_address.AssignFromIPLiteral(address); |
| 541 DCHECK(result); | 557 DCHECK(result); |
| 542 builder.EnableDevToolsServer(net::IPEndPoint( | 558 const net::IPEndPoint endpoint(devtools_address, |
| 543 devtools_address, base::checked_cast<uint16_t>(parsed_port))); | 559 base::checked_cast<uint16_t>(parsed_port)); |
| 560 builder.EnableDevToolsServer(endpoint); |
| 561 } else if (command_line.HasSwitch(switches::kRemoteDebuggingSocketFd)) { |
| 562 int parsed_fd; |
| 563 std::string fd_str = |
| 564 command_line.GetSwitchValueASCII(switches::kRemoteDebuggingSocketFd); |
| 565 if (!base::StringToInt(fd_str, &parsed_fd) || |
| 566 !base::IsValueInRangeForNumericType<size_t>(parsed_fd)) { |
| 567 LOG(ERROR) << "Invalid devtools server socket fd"; |
| 568 return EXIT_FAILURE; |
| 569 } |
| 570 builder.EnableDevToolsServer(base::checked_cast<size_t>(parsed_fd)); |
| 544 } | 571 } |
| 545 | 572 |
| 546 if (command_line.HasSwitch(switches::kProxyServer)) { | 573 if (command_line.HasSwitch(switches::kProxyServer)) { |
| 547 std::string proxy_server = | 574 std::string proxy_server = |
| 548 command_line.GetSwitchValueASCII(switches::kProxyServer); | 575 command_line.GetSwitchValueASCII(switches::kProxyServer); |
| 549 net::HostPortPair parsed_proxy_server = | 576 net::HostPortPair parsed_proxy_server = |
| 550 net::HostPortPair::FromString(proxy_server); | 577 net::HostPortPair::FromString(proxy_server); |
| 551 if (parsed_proxy_server.host().empty() || !parsed_proxy_server.port()) { | 578 if (parsed_proxy_server.host().empty() || !parsed_proxy_server.port()) { |
| 552 LOG(ERROR) << "Malformed proxy server url"; | 579 LOG(ERROR) << "Malformed proxy server url"; |
| 553 return EXIT_FAILURE; | 580 return EXIT_FAILURE; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 592 if (net::HttpUtil::IsValidHeaderValue(ua)) | 619 if (net::HttpUtil::IsValidHeaderValue(ua)) |
| 593 builder.SetUserAgent(ua); | 620 builder.SetUserAgent(ua); |
| 594 } | 621 } |
| 595 | 622 |
| 596 return HeadlessBrowserMain( | 623 return HeadlessBrowserMain( |
| 597 builder.Build(), | 624 builder.Build(), |
| 598 base::Bind(&HeadlessShell::OnStart, base::Unretained(&shell))); | 625 base::Bind(&HeadlessShell::OnStart, base::Unretained(&shell))); |
| 599 } | 626 } |
| 600 | 627 |
| 601 } // namespace headless | 628 } // namespace headless |
| OLD | NEW |