Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(205)

Side by Side Diff: headless/app/headless_shell.cc

Issue 2810353003: Adds a command-line flag indicating an open and listening socket to (Closed)
Patch Set: Updated upstream dependency Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 (command_line.HasSwitch(switches::kRemoteDebuggingPort) &&
dvallet 2017/04/13 03:44:29 You could add a non POSIX check here to ensure tha
402 command_line.HasSwitch(switches::kRemoteDebuggingSocketFd)) {
403 LOG(ERROR) << "remote-debugging-port and remote-debugging-socket "
404 << "can't both be set.";
405 return false;
406 }
407 if (!command_line.HasSwitch(switches::kRemoteDebuggingPort) &&
408 !command_line.HasSwitch(switches::kRemoteDebuggingSocketFd)) {
400 if (command_line.GetArgs().size() <= 1) 409 if (command_line.GetArgs().size() <= 1)
401 return true; 410 return true;
402 LOG(ERROR) << "Open multiple tabs is only supported when the " 411 LOG(ERROR) << "Open multiple tabs is only supported when "
403 << "remote debug port is set."; 412 << "remote debugging is enabled.";
404 return false; 413 return false;
405 } 414 }
406 if (command_line.HasSwitch(switches::kDumpDom)) { 415 if (command_line.HasSwitch(switches::kDumpDom)) {
407 LOG(ERROR) << "Dump DOM is disabled when remote debugging is enabled."; 416 LOG(ERROR) << "Dump DOM is disabled when remote debugging is enabled.";
408 return false; 417 return false;
409 } 418 }
410 if (command_line.HasSwitch(switches::kRepl)) { 419 if (command_line.HasSwitch(switches::kRepl)) {
411 LOG(ERROR) << "Evaluate Javascript is disabled " 420 LOG(ERROR) << "Evaluate Javascript is disabled "
412 << "when remote debugging is enabled."; 421 << "when remote debugging is enabled.";
413 return false; 422 return false;
(...skipping 15 matching lines...) Expand all
429 } 438 }
430 return true; 439 return true;
431 } 440 }
432 441
433 int HeadlessShellMain(int argc, const char** argv) { 442 int HeadlessShellMain(int argc, const char** argv) {
434 base::CommandLine::Init(argc, argv); 443 base::CommandLine::Init(argc, argv);
435 RunChildProcessIfNeeded(argc, argv); 444 RunChildProcessIfNeeded(argc, argv);
436 HeadlessShell shell; 445 HeadlessShell shell;
437 HeadlessBrowser::Options::Builder builder(argc, argv); 446 HeadlessBrowser::Options::Builder builder(argc, argv);
438 447
439 // Enable devtools if requested.
440 const base::CommandLine& command_line( 448 const base::CommandLine& command_line(
441 *base::CommandLine::ForCurrentProcess()); 449 *base::CommandLine::ForCurrentProcess());
442 if (!ValidateCommandLine(command_line)) 450 if (!ValidateCommandLine(command_line))
443 return EXIT_FAILURE; 451 return EXIT_FAILURE;
444 452
445 if (command_line.HasSwitch(::switches::kEnableCrashReporter)) 453 if (command_line.HasSwitch(::switches::kEnableCrashReporter))
446 builder.SetCrashReporterEnabled(true); 454 builder.SetCrashReporterEnabled(true);
447 if (command_line.HasSwitch(switches::kCrashDumpsDir)) { 455 if (command_line.HasSwitch(switches::kCrashDumpsDir)) {
448 builder.SetCrashDumpsDir( 456 builder.SetCrashDumpsDir(
449 command_line.GetSwitchValuePath(switches::kCrashDumpsDir)); 457 command_line.GetSwitchValuePath(switches::kCrashDumpsDir));
450 } 458 }
451 459
460 // Enable devtools if requested, either by specifying a port (and optional
461 // address), or by specifying the fd of an already-open socket.
452 if (command_line.HasSwitch(::switches::kRemoteDebuggingPort)) { 462 if (command_line.HasSwitch(::switches::kRemoteDebuggingPort)) {
453 std::string address = kDevToolsHttpServerAddress; 463 std::string address = kDevToolsHttpServerAddress;
454 if (command_line.HasSwitch(switches::kRemoteDebuggingAddress)) { 464 if (command_line.HasSwitch(switches::kRemoteDebuggingAddress)) {
455 address = 465 address =
456 command_line.GetSwitchValueASCII(switches::kRemoteDebuggingAddress); 466 command_line.GetSwitchValueASCII(switches::kRemoteDebuggingAddress);
457 net::IPAddress parsed_address; 467 net::IPAddress parsed_address;
458 if (!net::ParseURLHostnameToAddress(address, &parsed_address)) { 468 if (!net::ParseURLHostnameToAddress(address, &parsed_address)) {
459 LOG(ERROR) << "Invalid devtools server address"; 469 LOG(ERROR) << "Invalid devtools server address";
460 return EXIT_FAILURE; 470 return EXIT_FAILURE;
461 } 471 }
462 } 472 }
463 int parsed_port; 473 int parsed_port;
464 std::string port_str = 474 std::string port_str =
465 command_line.GetSwitchValueASCII(::switches::kRemoteDebuggingPort); 475 command_line.GetSwitchValueASCII(::switches::kRemoteDebuggingPort);
466 if (!base::StringToInt(port_str, &parsed_port) || 476 if (!base::StringToInt(port_str, &parsed_port) ||
467 !base::IsValueInRangeForNumericType<uint16_t>(parsed_port)) { 477 !base::IsValueInRangeForNumericType<uint16_t>(parsed_port)) {
468 LOG(ERROR) << "Invalid devtools server port"; 478 LOG(ERROR) << "Invalid devtools server port";
469 return EXIT_FAILURE; 479 return EXIT_FAILURE;
470 } 480 }
471 net::IPAddress devtools_address; 481 net::IPAddress devtools_address;
472 bool result = devtools_address.AssignFromIPLiteral(address); 482 bool result = devtools_address.AssignFromIPLiteral(address);
473 DCHECK(result); 483 DCHECK(result);
474 builder.EnableDevToolsServer(net::IPEndPoint( 484 const net::IPEndPoint endpoint(devtools_address,
475 devtools_address, base::checked_cast<uint16_t>(parsed_port))); 485 base::checked_cast<uint16_t>(parsed_port));
486 builder.EnableDevToolsServer(endpoint);
487 } else if (command_line.HasSwitch(switches::kRemoteDebuggingSocketFd)) {
488 int parsed_fd;
489 std::string fd_str =
490 command_line.GetSwitchValueASCII(switches::kRemoteDebuggingSocketFd);
491 if (!base::StringToInt(fd_str, &parsed_fd) ||
492 !base::IsValueInRangeForNumericType<size_t>(parsed_fd)) {
493 LOG(ERROR) << "Invalid devtools server socket fd";
494 return EXIT_FAILURE;
495 }
496 builder.EnableDevToolsServer(base::checked_cast<uint16_t>(parsed_fd));
476 } 497 }
477 498
478 if (command_line.HasSwitch(switches::kProxyServer)) { 499 if (command_line.HasSwitch(switches::kProxyServer)) {
479 std::string proxy_server = 500 std::string proxy_server =
480 command_line.GetSwitchValueASCII(switches::kProxyServer); 501 command_line.GetSwitchValueASCII(switches::kProxyServer);
481 net::HostPortPair parsed_proxy_server = 502 net::HostPortPair parsed_proxy_server =
482 net::HostPortPair::FromString(proxy_server); 503 net::HostPortPair::FromString(proxy_server);
483 if (parsed_proxy_server.host().empty() || !parsed_proxy_server.port()) { 504 if (parsed_proxy_server.host().empty() || !parsed_proxy_server.port()) {
484 LOG(ERROR) << "Malformed proxy server url"; 505 LOG(ERROR) << "Malformed proxy server url";
485 return EXIT_FAILURE; 506 return EXIT_FAILURE;
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
518 builder.SetOverrideWebPreferencesCallback(base::Bind([]( 539 builder.SetOverrideWebPreferencesCallback(base::Bind([](
519 WebPreferences* preferences) { preferences->hide_scrollbars = true; })); 540 WebPreferences* preferences) { preferences->hide_scrollbars = true; }));
520 } 541 }
521 542
522 return HeadlessBrowserMain( 543 return HeadlessBrowserMain(
523 builder.Build(), 544 builder.Build(),
524 base::Bind(&HeadlessShell::OnStart, base::Unretained(&shell))); 545 base::Bind(&HeadlessShell::OnStart, base::Unretained(&shell)));
525 } 546 }
526 547
527 } // namespace headless 548 } // namespace headless
OLDNEW
« no previous file with comments | « no previous file | headless/app/headless_shell_switches.h » ('j') | headless/lib/browser/headless_devtools.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698