| 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" |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 99 } | 99 } |
| 100 browser_context_ = context_builder.Build(); | 100 browser_context_ = context_builder.Build(); |
| 101 | 101 |
| 102 HeadlessWebContents::Builder builder( | 102 HeadlessWebContents::Builder builder( |
| 103 browser_context_->CreateWebContentsBuilder()); | 103 browser_context_->CreateWebContentsBuilder()); |
| 104 base::CommandLine::StringVector args = | 104 base::CommandLine::StringVector args = |
| 105 base::CommandLine::ForCurrentProcess()->GetArgs(); | 105 base::CommandLine::ForCurrentProcess()->GetArgs(); |
| 106 | 106 |
| 107 // TODO(alexclarke): Should we navigate to about:blank first if using | 107 // TODO(alexclarke): Should we navigate to about:blank first if using |
| 108 // virtual time? | 108 // virtual time? |
| 109 if (!args.empty() && !args[0].empty()) | 109 if (args.empty()) |
| 110 builder.SetInitialURL(GURL(args[0])); | 110 args.push_back("about:blank"); |
| 111 | 111 for (auto it = args.rbegin(); it != args.rend(); ++it) { |
| 112 web_contents_ = builder.Build(); | 112 GURL url(*it); |
| 113 if (!web_contents_) { | 113 HeadlessWebContents* web_contents = builder.SetInitialURL(url).Build(); |
| 114 LOG(ERROR) << "Navigation failed"; | 114 if (!web_contents) { |
| 115 browser_->Shutdown(); | 115 LOG(ERROR) << "Navigation to " << url << " failed"; |
| 116 return; | 116 browser_->Shutdown(); |
| 117 return; |
| 118 } |
| 119 if (!web_contents_ && !RemoteDebuggingEnabled()) { |
| 120 // TODO(jzfeng): Support observing multiple targets. |
| 121 url_ = url; |
| 122 web_contents_ = web_contents; |
| 123 web_contents_->AddObserver(this); |
| 124 } |
| 117 } | 125 } |
| 118 web_contents_->AddObserver(this); | |
| 119 } | 126 } |
| 120 | 127 |
| 121 void Shutdown() { | 128 void Shutdown() { |
| 122 if (!web_contents_) | 129 if (!web_contents_) |
| 123 return; | 130 return; |
| 124 if (!RemoteDebuggingEnabled()) { | 131 if (!RemoteDebuggingEnabled()) { |
| 125 devtools_client_->GetEmulation()->GetExperimental()->RemoveObserver(this); | 132 devtools_client_->GetEmulation()->GetExperimental()->RemoveObserver(this); |
| 126 devtools_client_->GetInspector()->GetExperimental()->RemoveObserver(this); | 133 devtools_client_->GetInspector()->GetExperimental()->RemoveObserver(this); |
| 127 devtools_client_->GetPage()->RemoveObserver(this); | 134 devtools_client_->GetPage()->RemoveObserver(this); |
| 128 if (web_contents_->GetDevToolsTarget()) { | 135 if (web_contents_->GetDevToolsTarget()) { |
| 129 web_contents_->GetDevToolsTarget()->DetachClient( | 136 web_contents_->GetDevToolsTarget()->DetachClient( |
| 130 devtools_client_.get()); | 137 devtools_client_.get()); |
| 131 } | 138 } |
| 132 } | 139 } |
| 133 web_contents_->RemoveObserver(this); | 140 web_contents_->RemoveObserver(this); |
| 134 web_contents_ = nullptr; | 141 web_contents_ = nullptr; |
| 135 browser_context_->Close(); | 142 browser_context_->Close(); |
| 136 browser_->Shutdown(); | 143 browser_->Shutdown(); |
| 137 } | 144 } |
| 138 | 145 |
| 139 // HeadlessWebContents::Observer implementation: | 146 // HeadlessWebContents::Observer implementation: |
| 140 void DevToolsTargetReady() override { | 147 void DevToolsTargetReady() override { |
| 141 if (RemoteDebuggingEnabled()) | |
| 142 return; | |
| 143 web_contents_->GetDevToolsTarget()->AttachClient(devtools_client_.get()); | 148 web_contents_->GetDevToolsTarget()->AttachClient(devtools_client_.get()); |
| 144 devtools_client_->GetInspector()->GetExperimental()->AddObserver(this); | 149 devtools_client_->GetInspector()->GetExperimental()->AddObserver(this); |
| 145 devtools_client_->GetPage()->AddObserver(this); | 150 devtools_client_->GetPage()->AddObserver(this); |
| 146 devtools_client_->GetPage()->Enable(); | 151 devtools_client_->GetPage()->Enable(); |
| 147 // Check if the document had already finished loading by the time we | 152 // Check if the document had already finished loading by the time we |
| 148 // attached. | 153 // attached. |
| 149 | 154 |
| 150 devtools_client_->GetEmulation()->GetExperimental()->AddObserver(this); | 155 devtools_client_->GetEmulation()->GetExperimental()->AddObserver(this); |
| 151 | 156 |
| 152 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 157 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| (...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 393 HeadlessWebContents* web_contents_; | 398 HeadlessWebContents* web_contents_; |
| 394 bool processed_page_ready_; | 399 bool processed_page_ready_; |
| 395 std::unique_ptr<net::FileStream> screenshot_file_stream_; | 400 std::unique_ptr<net::FileStream> screenshot_file_stream_; |
| 396 HeadlessBrowserContext* browser_context_; | 401 HeadlessBrowserContext* browser_context_; |
| 397 std::unique_ptr<DeterministicDispatcher> deterministic_dispatcher_; | 402 std::unique_ptr<DeterministicDispatcher> deterministic_dispatcher_; |
| 398 base::WeakPtrFactory<HeadlessShell> weak_factory_; | 403 base::WeakPtrFactory<HeadlessShell> weak_factory_; |
| 399 | 404 |
| 400 DISALLOW_COPY_AND_ASSIGN(HeadlessShell); | 405 DISALLOW_COPY_AND_ASSIGN(HeadlessShell); |
| 401 }; | 406 }; |
| 402 | 407 |
| 408 bool ValidateCommandLine(const base::CommandLine& command_line) { |
| 409 if (!command_line.HasSwitch(::switches::kRemoteDebuggingPort)) { |
| 410 if (command_line.GetArgs().size() <= 1) |
| 411 return true; |
| 412 LOG(ERROR) << "Open multiple tabs is only supported when the " |
| 413 << "remote debug port is set."; |
| 414 return false; |
| 415 } |
| 416 if (command_line.HasSwitch(switches::kDumpDom)) { |
| 417 LOG(ERROR) << "Dump DOM is disabled when remote debugging is enabled."; |
| 418 return false; |
| 419 } |
| 420 if (command_line.HasSwitch(switches::kRepl)) { |
| 421 LOG(ERROR) << "Evaluate Javascript is disabled " |
| 422 << "when remote debugging is enabled."; |
| 423 return false; |
| 424 } |
| 425 if (command_line.HasSwitch(switches::kScreenshot)) { |
| 426 LOG(ERROR) << "Capture screenshot is disabled " |
| 427 << "when remote debugging is enabled."; |
| 428 return false; |
| 429 } |
| 430 if (command_line.HasSwitch(switches::kTimeout)) { |
| 431 LOG(ERROR) << "Navigation timeout is disabled " |
| 432 << "when remote debugging is enabled."; |
| 433 return false; |
| 434 } |
| 435 if (command_line.HasSwitch(switches::kVirtualTimeBudget)) { |
| 436 LOG(ERROR) << "Virtual time budget is disabled " |
| 437 << "when remote debugging is enabled."; |
| 438 return false; |
| 439 } |
| 440 return true; |
| 441 } |
| 442 |
| 403 int HeadlessShellMain(int argc, const char** argv) { | 443 int HeadlessShellMain(int argc, const char** argv) { |
| 404 RunChildProcessIfNeeded(argc, argv); | 444 RunChildProcessIfNeeded(argc, argv); |
| 405 HeadlessShell shell; | 445 HeadlessShell shell; |
| 406 HeadlessBrowser::Options::Builder builder(argc, argv); | 446 HeadlessBrowser::Options::Builder builder(argc, argv); |
| 407 | 447 |
| 408 // Enable devtools if requested. | 448 // Enable devtools if requested. |
| 409 base::CommandLine command_line(argc, argv); | 449 base::CommandLine command_line(argc, argv); |
| 450 if (!ValidateCommandLine(command_line)) |
| 451 return EXIT_FAILURE; |
| 452 |
| 410 if (command_line.HasSwitch(::switches::kRemoteDebuggingPort)) { | 453 if (command_line.HasSwitch(::switches::kRemoteDebuggingPort)) { |
| 411 std::string address = kDevToolsHttpServerAddress; | 454 std::string address = kDevToolsHttpServerAddress; |
| 412 if (command_line.HasSwitch(switches::kRemoteDebuggingAddress)) { | 455 if (command_line.HasSwitch(switches::kRemoteDebuggingAddress)) { |
| 413 address = | 456 address = |
| 414 command_line.GetSwitchValueASCII(switches::kRemoteDebuggingAddress); | 457 command_line.GetSwitchValueASCII(switches::kRemoteDebuggingAddress); |
| 415 net::IPAddress parsed_address; | 458 net::IPAddress parsed_address; |
| 416 if (!net::ParseURLHostnameToAddress(address, &parsed_address)) { | 459 if (!net::ParseURLHostnameToAddress(address, &parsed_address)) { |
| 417 LOG(ERROR) << "Invalid devtools server address"; | 460 LOG(ERROR) << "Invalid devtools server address"; |
| 418 return EXIT_FAILURE; | 461 return EXIT_FAILURE; |
| 419 } | 462 } |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 471 } | 514 } |
| 472 builder.SetWindowSize(parsed_window_size); | 515 builder.SetWindowSize(parsed_window_size); |
| 473 } | 516 } |
| 474 | 517 |
| 475 return HeadlessBrowserMain( | 518 return HeadlessBrowserMain( |
| 476 builder.Build(), | 519 builder.Build(), |
| 477 base::Bind(&HeadlessShell::OnStart, base::Unretained(&shell))); | 520 base::Bind(&HeadlessShell::OnStart, base::Unretained(&shell))); |
| 478 } | 521 } |
| 479 | 522 |
| 480 } // namespace headless | 523 } // namespace headless |
| OLD | NEW |