| 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/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/callback.h" | 11 #include "base/callback.h" |
| 12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
| 13 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
| 14 #include "base/json/json_writer.h" | 14 #include "base/json/json_writer.h" |
| 15 #include "base/location.h" | 15 #include "base/location.h" |
| 16 #include "base/memory/ptr_util.h" | 16 #include "base/memory/ptr_util.h" |
| 17 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.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" | |
| 22 #include "headless/app/headless_shell_switches.h" | 21 #include "headless/app/headless_shell_switches.h" |
| 23 #include "headless/public/devtools/domains/emulation.h" | 22 #include "headless/public/devtools/domains/emulation.h" |
| 24 #include "headless/public/devtools/domains/inspector.h" | 23 #include "headless/public/devtools/domains/inspector.h" |
| 25 #include "headless/public/devtools/domains/page.h" | 24 #include "headless/public/devtools/domains/page.h" |
| 26 #include "headless/public/devtools/domains/runtime.h" | 25 #include "headless/public/devtools/domains/runtime.h" |
| 27 #include "headless/public/headless_browser.h" | 26 #include "headless/public/headless_browser.h" |
| 28 #include "headless/public/headless_devtools_client.h" | 27 #include "headless/public/headless_devtools_client.h" |
| 29 #include "headless/public/headless_devtools_target.h" | 28 #include "headless/public/headless_devtools_target.h" |
| 30 #include "headless/public/headless_web_contents.h" | 29 #include "headless/public/headless_web_contents.h" |
| 31 #include "headless/public/util/deterministic_dispatcher.h" | 30 #include "headless/public/util/deterministic_dispatcher.h" |
| (...skipping 345 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 377 // Operation could not be started. | 376 // Operation could not be started. |
| 378 OnScreenshotFileClosed(close_result); | 377 OnScreenshotFileClosed(close_result); |
| 379 } | 378 } |
| 380 } | 379 } |
| 381 | 380 |
| 382 void OnScreenshotFileClosed(const int close_result) { Shutdown(); } | 381 void OnScreenshotFileClosed(const int close_result) { Shutdown(); } |
| 383 | 382 |
| 384 bool RemoteDebuggingEnabled() const { | 383 bool RemoteDebuggingEnabled() const { |
| 385 const base::CommandLine& command_line = | 384 const base::CommandLine& command_line = |
| 386 *base::CommandLine::ForCurrentProcess(); | 385 *base::CommandLine::ForCurrentProcess(); |
| 387 return command_line.HasSwitch(::switches::kRemoteDebuggingPort); | 386 return command_line.HasSwitch(switches::kRemoteDebuggingPort); |
| 388 } | 387 } |
| 389 | 388 |
| 390 private: | 389 private: |
| 391 GURL url_; | 390 GURL url_; |
| 392 HeadlessBrowser* browser_; // Not owned. | 391 HeadlessBrowser* browser_; // Not owned. |
| 393 std::unique_ptr<HeadlessDevToolsClient> devtools_client_; | 392 std::unique_ptr<HeadlessDevToolsClient> devtools_client_; |
| 394 HeadlessWebContents* web_contents_; | 393 HeadlessWebContents* web_contents_; |
| 395 bool processed_page_ready_; | 394 bool processed_page_ready_; |
| 396 std::unique_ptr<net::FileStream> screenshot_file_stream_; | 395 std::unique_ptr<net::FileStream> screenshot_file_stream_; |
| 397 HeadlessBrowserContext* browser_context_; | 396 HeadlessBrowserContext* browser_context_; |
| 398 std::unique_ptr<DeterministicDispatcher> deterministic_dispatcher_; | 397 std::unique_ptr<DeterministicDispatcher> deterministic_dispatcher_; |
| 399 base::WeakPtrFactory<HeadlessShell> weak_factory_; | 398 base::WeakPtrFactory<HeadlessShell> weak_factory_; |
| 400 | 399 |
| 401 DISALLOW_COPY_AND_ASSIGN(HeadlessShell); | 400 DISALLOW_COPY_AND_ASSIGN(HeadlessShell); |
| 402 }; | 401 }; |
| 403 | 402 |
| 404 bool ValidateCommandLine(const base::CommandLine& command_line) { | 403 bool ValidateCommandLine(const base::CommandLine& command_line) { |
| 405 if (!command_line.HasSwitch(::switches::kRemoteDebuggingPort)) { | 404 if (!command_line.HasSwitch(switches::kRemoteDebuggingPort)) { |
| 406 if (command_line.GetArgs().size() <= 1) | 405 if (command_line.GetArgs().size() <= 1) |
| 407 return true; | 406 return true; |
| 408 LOG(ERROR) << "Open multiple tabs is only supported when the " | 407 LOG(ERROR) << "Open multiple tabs is only supported when the " |
| 409 << "remote debug port is set."; | 408 << "remote debug port is set."; |
| 410 return false; | 409 return false; |
| 411 } | 410 } |
| 412 if (command_line.HasSwitch(switches::kDumpDom)) { | 411 if (command_line.HasSwitch(switches::kDumpDom)) { |
| 413 LOG(ERROR) << "Dump DOM is disabled when remote debugging is enabled."; | 412 LOG(ERROR) << "Dump DOM is disabled when remote debugging is enabled."; |
| 414 return false; | 413 return false; |
| 415 } | 414 } |
| (...skipping 19 matching lines...) Expand all Loading... |
| 435 } | 434 } |
| 436 return true; | 435 return true; |
| 437 } | 436 } |
| 438 | 437 |
| 439 int HeadlessShellMain(int argc, const char** argv) { | 438 int HeadlessShellMain(int argc, const char** argv) { |
| 440 RunChildProcessIfNeeded(argc, argv); | 439 RunChildProcessIfNeeded(argc, argv); |
| 441 HeadlessShell shell; | 440 HeadlessShell shell; |
| 442 HeadlessBrowser::Options::Builder builder(argc, argv); | 441 HeadlessBrowser::Options::Builder builder(argc, argv); |
| 443 | 442 |
| 444 // Enable devtools if requested. | 443 // Enable devtools if requested. |
| 445 base::CommandLine command_line(argc, argv); | 444 base::CommandLine::Init(argc, argv); |
| 445 const base::CommandLine& command_line( |
| 446 *base::CommandLine::ForCurrentProcess()); |
| 446 if (!ValidateCommandLine(command_line)) | 447 if (!ValidateCommandLine(command_line)) |
| 447 return EXIT_FAILURE; | 448 return EXIT_FAILURE; |
| 448 | 449 |
| 449 if (command_line.HasSwitch(::switches::kRemoteDebuggingPort)) { | 450 if (command_line.HasSwitch(::switches::kRemoteDebuggingPort)) { |
| 450 std::string address = kDevToolsHttpServerAddress; | 451 std::string address = kDevToolsHttpServerAddress; |
| 451 if (command_line.HasSwitch(switches::kRemoteDebuggingAddress)) { | 452 if (command_line.HasSwitch(switches::kRemoteDebuggingAddress)) { |
| 452 address = | 453 address = |
| 453 command_line.GetSwitchValueASCII(switches::kRemoteDebuggingAddress); | 454 command_line.GetSwitchValueASCII(switches::kRemoteDebuggingAddress); |
| 454 net::IPAddress parsed_address; | 455 net::IPAddress parsed_address; |
| 455 if (!net::ParseURLHostnameToAddress(address, &parsed_address)) { | 456 if (!net::ParseURLHostnameToAddress(address, &parsed_address)) { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 477 command_line.GetSwitchValueASCII(switches::kProxyServer); | 478 command_line.GetSwitchValueASCII(switches::kProxyServer); |
| 478 net::HostPortPair parsed_proxy_server = | 479 net::HostPortPair parsed_proxy_server = |
| 479 net::HostPortPair::FromString(proxy_server); | 480 net::HostPortPair::FromString(proxy_server); |
| 480 if (parsed_proxy_server.host().empty() || !parsed_proxy_server.port()) { | 481 if (parsed_proxy_server.host().empty() || !parsed_proxy_server.port()) { |
| 481 LOG(ERROR) << "Malformed proxy server url"; | 482 LOG(ERROR) << "Malformed proxy server url"; |
| 482 return EXIT_FAILURE; | 483 return EXIT_FAILURE; |
| 483 } | 484 } |
| 484 builder.SetProxyServer(parsed_proxy_server); | 485 builder.SetProxyServer(parsed_proxy_server); |
| 485 } | 486 } |
| 486 | 487 |
| 487 if (command_line.HasSwitch(::switches::kHostResolverRules)) { | 488 if (command_line.HasSwitch(switches::kHostResolverRules)) { |
| 488 builder.SetHostResolverRules( | 489 builder.SetHostResolverRules( |
| 489 command_line.GetSwitchValueASCII(::switches::kHostResolverRules)); | 490 command_line.GetSwitchValueASCII(switches::kHostResolverRules)); |
| 490 } | 491 } |
| 491 | 492 |
| 492 if (command_line.HasSwitch(switches::kUseGL)) { | 493 if (command_line.HasSwitch(switches::kUseGL)) { |
| 493 builder.SetGLImplementation( | 494 builder.SetGLImplementation( |
| 494 command_line.GetSwitchValueASCII(switches::kUseGL)); | 495 command_line.GetSwitchValueASCII(switches::kUseGL)); |
| 495 } | 496 } |
| 496 | 497 |
| 497 if (command_line.HasSwitch(switches::kUserDataDir)) { | 498 if (command_line.HasSwitch(switches::kUserDataDir)) { |
| 498 builder.SetUserDataDir( | 499 builder.SetUserDataDir( |
| 499 command_line.GetSwitchValuePath(switches::kUserDataDir)); | 500 command_line.GetSwitchValuePath(switches::kUserDataDir)); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 515 builder.SetOverrideWebPreferencesCallback(base::Bind([]( | 516 builder.SetOverrideWebPreferencesCallback(base::Bind([]( |
| 516 WebPreferences* preferences) { preferences->hide_scrollbars = true; })); | 517 WebPreferences* preferences) { preferences->hide_scrollbars = true; })); |
| 517 } | 518 } |
| 518 | 519 |
| 519 return HeadlessBrowserMain( | 520 return HeadlessBrowserMain( |
| 520 builder.Build(), | 521 builder.Build(), |
| 521 base::Bind(&HeadlessShell::OnStart, base::Unretained(&shell))); | 522 base::Bind(&HeadlessShell::OnStart, base::Unretained(&shell))); |
| 522 } | 523 } |
| 523 | 524 |
| 524 } // namespace headless | 525 } // namespace headless |
| OLD | NEW |