| 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/app/content_main.h" | |
| 22 #include "headless/app/headless_shell.h" | 21 #include "headless/app/headless_shell.h" |
| 23 #include "headless/app/headless_shell_switches.h" | 22 #include "headless/app/headless_shell_switches.h" |
| 24 #include "headless/public/headless_devtools_target.h" | 23 #include "headless/public/headless_devtools_target.h" |
| 25 #include "headless/public/util/deterministic_http_protocol_handler.h" | 24 #include "headless/public/util/deterministic_http_protocol_handler.h" |
| 26 #include "net/base/io_buffer.h" | 25 #include "net/base/io_buffer.h" |
| 27 #include "net/base/ip_address.h" | 26 #include "net/base/ip_address.h" |
| 28 #include "net/base/net_errors.h" | 27 #include "net/base/net_errors.h" |
| 29 #include "net/http/http_util.h" | 28 #include "net/http/http_util.h" |
| 30 #include "ui/gfx/geometry/size.h" | 29 #include "ui/gfx/geometry/size.h" |
| 31 | 30 |
| 32 #if defined(OS_WIN) | |
| 33 #include "sandbox/win/src/sandbox_types.h" | |
| 34 #endif | |
| 35 | |
| 36 namespace headless { | 31 namespace headless { |
| 37 namespace { | 32 namespace { |
| 38 // Address where to listen to incoming DevTools connections. | 33 // Address where to listen to incoming DevTools connections. |
| 39 const char kDevToolsHttpServerAddress[] = "127.0.0.1"; | 34 const char kDevToolsHttpServerAddress[] = "127.0.0.1"; |
| 40 // Default file name for screenshot. Can be overriden by "--screenshot" switch. | 35 // Default file name for screenshot. Can be overriden by "--screenshot" switch. |
| 41 const char kDefaultScreenshotFileName[] = "screenshot.png"; | 36 const char kDefaultScreenshotFileName[] = "screenshot.png"; |
| 42 // Default file name for pdf. Can be overriden by "--print-to-pdf" switch. | 37 // Default file name for pdf. Can be overriden by "--print-to-pdf" switch. |
| 43 const char kDefaultPDFFileName[] = "output.pdf"; | 38 const char kDefaultPDFFileName[] = "output.pdf"; |
| 44 | 39 |
| 45 bool ParseWindowSize(std::string window_size, gfx::Size* parsed_window_size) { | 40 bool ParseWindowSize(std::string window_size, gfx::Size* parsed_window_size) { |
| (...skipping 376 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 422 if (!file_proxy_->Write( | 417 if (!file_proxy_->Write( |
| 423 0, buf->data(), buf->size(), | 418 0, buf->data(), buf->size(), |
| 424 base::Bind(&HeadlessShell::OnFileWritten, weak_factory_.GetWeakPtr(), | 419 base::Bind(&HeadlessShell::OnFileWritten, weak_factory_.GetWeakPtr(), |
| 425 file_name, buf->size()))) { | 420 file_name, buf->size()))) { |
| 426 // Operation may have completed successfully or failed. | 421 // Operation may have completed successfully or failed. |
| 427 OnFileWritten(file_name, buf->size(), base::File::FILE_ERROR_FAILED, 0); | 422 OnFileWritten(file_name, buf->size(), base::File::FILE_ERROR_FAILED, 0); |
| 428 } | 423 } |
| 429 } | 424 } |
| 430 | 425 |
| 431 void HeadlessShell::OnFileWritten(const base::FilePath file_name, | 426 void HeadlessShell::OnFileWritten(const base::FilePath file_name, |
| 432 const size_t length, | 427 const int length, |
| 433 base::File::Error error_code, | 428 base::File::Error error_code, |
| 434 int write_result) { | 429 int write_result) { |
| 435 if (write_result < static_cast<int>(length)) { | 430 if (write_result < length) { |
| 436 // TODO(eseckler): Support recovering from partial writes. | 431 // TODO(eseckler): Support recovering from partial writes. |
| 437 LOG(ERROR) << "Writing to file " << file_name.value() | 432 LOG(ERROR) << "Writing to file " << file_name.value() |
| 438 << " was unsuccessful: " | 433 << " was unsuccessful: " |
| 439 << base::File::ErrorToString(error_code); | 434 << base::File::ErrorToString(error_code); |
| 440 } else { | 435 } else { |
| 441 LOG(INFO) << "Written to file " << file_name.value() << "."; | 436 LOG(INFO) << "Written to file " << file_name.value() << "."; |
| 442 } | 437 } |
| 443 if (!file_proxy_->Close(base::Bind(&HeadlessShell::OnFileClosed, | 438 if (!file_proxy_->Close(base::Bind(&HeadlessShell::OnFileClosed, |
| 444 weak_factory_.GetWeakPtr()))) { | 439 weak_factory_.GetWeakPtr()))) { |
| 445 // Operation could not be started. | 440 // Operation could not be started. |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 495 return false; | 490 return false; |
| 496 } | 491 } |
| 497 if (command_line.HasSwitch(switches::kVirtualTimeBudget)) { | 492 if (command_line.HasSwitch(switches::kVirtualTimeBudget)) { |
| 498 LOG(ERROR) << "Virtual time budget is disabled " | 493 LOG(ERROR) << "Virtual time budget is disabled " |
| 499 << "when remote debugging is enabled."; | 494 << "when remote debugging is enabled."; |
| 500 return false; | 495 return false; |
| 501 } | 496 } |
| 502 return true; | 497 return true; |
| 503 } | 498 } |
| 504 | 499 |
| 505 #if defined(OS_WIN) | |
| 506 int HeadlessShellMain(HINSTANCE instance, | |
| 507 sandbox::SandboxInterfaceInfo* sandbox_info) { | |
| 508 base::CommandLine::Init(0, nullptr); | |
| 509 HeadlessBrowser::Options::Builder builder(0, nullptr); | |
| 510 builder.SetInstance(instance); | |
| 511 builder.SetSandboxInfo(std::move(sandbox_info)); | |
| 512 #else | |
| 513 int HeadlessShellMain(int argc, const char** argv) { | 500 int HeadlessShellMain(int argc, const char** argv) { |
| 514 base::CommandLine::Init(argc, argv); | 501 base::CommandLine::Init(argc, argv); |
| 515 RunChildProcessIfNeeded(argc, argv); | 502 RunChildProcessIfNeeded(argc, argv); |
| 503 HeadlessShell shell; |
| 516 HeadlessBrowser::Options::Builder builder(argc, argv); | 504 HeadlessBrowser::Options::Builder builder(argc, argv); |
| 517 #endif // defined(OS_WIN) | |
| 518 HeadlessShell shell; | |
| 519 | 505 |
| 520 // Enable devtools if requested. | 506 // Enable devtools if requested. |
| 521 const base::CommandLine& command_line( | 507 const base::CommandLine& command_line( |
| 522 *base::CommandLine::ForCurrentProcess()); | 508 *base::CommandLine::ForCurrentProcess()); |
| 523 if (!ValidateCommandLine(command_line)) | 509 if (!ValidateCommandLine(command_line)) |
| 524 return EXIT_FAILURE; | 510 return EXIT_FAILURE; |
| 525 | 511 |
| 526 if (command_line.HasSwitch(switches::kEnableCrashReporter)) | 512 if (command_line.HasSwitch(::switches::kEnableCrashReporter)) |
| 527 builder.SetCrashReporterEnabled(true); | 513 builder.SetCrashReporterEnabled(true); |
| 528 if (command_line.HasSwitch(switches::kCrashDumpsDir)) { | 514 if (command_line.HasSwitch(switches::kCrashDumpsDir)) { |
| 529 builder.SetCrashDumpsDir( | 515 builder.SetCrashDumpsDir( |
| 530 command_line.GetSwitchValuePath(switches::kCrashDumpsDir)); | 516 command_line.GetSwitchValuePath(switches::kCrashDumpsDir)); |
| 531 } | 517 } |
| 532 | 518 |
| 533 if (command_line.HasSwitch(::switches::kRemoteDebuggingPort)) { | 519 if (command_line.HasSwitch(::switches::kRemoteDebuggingPort)) { |
| 534 std::string address = kDevToolsHttpServerAddress; | 520 std::string address = kDevToolsHttpServerAddress; |
| 535 if (command_line.HasSwitch(switches::kRemoteDebuggingAddress)) { | 521 if (command_line.HasSwitch(switches::kRemoteDebuggingAddress)) { |
| 536 address = | 522 address = |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 604 std::string ua = command_line.GetSwitchValueASCII(switches::kUserAgent); | 590 std::string ua = command_line.GetSwitchValueASCII(switches::kUserAgent); |
| 605 if (net::HttpUtil::IsValidHeaderValue(ua)) | 591 if (net::HttpUtil::IsValidHeaderValue(ua)) |
| 606 builder.SetUserAgent(ua); | 592 builder.SetUserAgent(ua); |
| 607 } | 593 } |
| 608 | 594 |
| 609 return HeadlessBrowserMain( | 595 return HeadlessBrowserMain( |
| 610 builder.Build(), | 596 builder.Build(), |
| 611 base::Bind(&HeadlessShell::OnStart, base::Unretained(&shell))); | 597 base::Bind(&HeadlessShell::OnStart, base::Unretained(&shell))); |
| 612 } | 598 } |
| 613 | 599 |
| 614 int HeadlessShellMain(const content::ContentMainParams& params) { | |
| 615 #if defined(OS_WIN) | |
| 616 return HeadlessShellMain(params.instance, params.sandbox_info); | |
| 617 #else | |
| 618 return HeadlessShellMain(params.argc, params.argv); | |
| 619 #endif | |
| 620 } | |
| 621 | |
| 622 } // namespace headless | 600 } // namespace headless |
| OLD | NEW |