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