OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/bind.h" |
| 6 #include "base/callback.h" |
| 7 #include "base/command_line.h" |
| 8 #include "base/location.h" |
| 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/numerics/safe_conversions.h" |
| 11 #include "base/strings/string_number_conversions.h" |
| 12 #include "content/public/common/content_switches.h" |
| 13 #include "headless/public/headless_browser.h" |
| 14 #include "headless/public/headless_web_contents.h" |
| 15 #include "net/base/ip_address.h" |
| 16 #include "ui/gfx/geometry/size.h" |
| 17 |
| 18 using headless::HeadlessBrowser; |
| 19 using headless::HeadlessWebContents; |
| 20 |
| 21 namespace { |
| 22 // Address where to listen to incoming DevTools connections. |
| 23 const char kDevToolsHttpServerAddress[] = "127.0.0.1"; |
| 24 } |
| 25 |
| 26 // A sample application which demonstrates the use of the headless API. |
| 27 class HeadlessShell : public HeadlessWebContents::Observer { |
| 28 public: |
| 29 HeadlessShell() : browser_(nullptr) {} |
| 30 ~HeadlessShell() override { |
| 31 if (web_contents_) |
| 32 web_contents_->RemoveObserver(this); |
| 33 } |
| 34 |
| 35 void OnStart(HeadlessBrowser* browser) { |
| 36 browser_ = browser; |
| 37 web_contents_ = browser->CreateWebContents(gfx::Size(800, 600)); |
| 38 web_contents_->AddObserver(this); |
| 39 |
| 40 base::CommandLine::StringVector args = |
| 41 base::CommandLine::ForCurrentProcess()->GetArgs(); |
| 42 |
| 43 const char kDefaultUrl[] = "about:blank"; |
| 44 GURL url; |
| 45 if (args.empty() || args[0].empty()) { |
| 46 url = GURL(kDefaultUrl); |
| 47 } else { |
| 48 url = GURL(args[0]); |
| 49 } |
| 50 if (!web_contents_->OpenURL(url)) { |
| 51 LOG(ERROR) << "Navigation failed"; |
| 52 web_contents_ = nullptr; |
| 53 browser_->Shutdown(); |
| 54 } |
| 55 } |
| 56 |
| 57 void ShutdownIfNeeded() { |
| 58 const base::CommandLine& command_line = |
| 59 *base::CommandLine::ForCurrentProcess(); |
| 60 if (!command_line.HasSwitch(switches::kRemoteDebuggingPort)) { |
| 61 web_contents_ = nullptr; |
| 62 browser_->Shutdown(); |
| 63 } |
| 64 } |
| 65 |
| 66 // HeadlessWebContents::Observer implementation: |
| 67 void DocumentOnLoadCompletedInMainFrame() override { |
| 68 LOG(DEBUG) << "Document load completed"; |
| 69 ShutdownIfNeeded(); |
| 70 } |
| 71 |
| 72 private: |
| 73 HeadlessBrowser* browser_; // Not owned. |
| 74 scoped_ptr<HeadlessWebContents> web_contents_; |
| 75 |
| 76 DISALLOW_COPY_AND_ASSIGN(HeadlessShell); |
| 77 }; |
| 78 |
| 79 int main(int argc, const char** argv) { |
| 80 HeadlessShell shell; |
| 81 HeadlessBrowser::Options::Builder builder(argc, argv); |
| 82 |
| 83 // Enable devtools if requested. |
| 84 base::CommandLine command_line(argc, argv); |
| 85 if (command_line.HasSwitch(switches::kRemoteDebuggingPort)) { |
| 86 int parsed_port; |
| 87 std::string port_str = |
| 88 command_line.GetSwitchValueASCII(switches::kRemoteDebuggingPort); |
| 89 if (base::StringToInt(port_str, &parsed_port) && |
| 90 base::IsValueInRangeForNumericType<uint16_t>(parsed_port)) { |
| 91 net::IPAddress devtools_address; |
| 92 bool result = |
| 93 devtools_address.AssignFromIPLiteral(kDevToolsHttpServerAddress); |
| 94 DCHECK(result); |
| 95 builder.EnableDevToolsServer(net::IPEndPoint( |
| 96 devtools_address, base::checked_cast<uint16_t>(parsed_port))); |
| 97 } |
| 98 } |
| 99 |
| 100 return HeadlessBrowserMain( |
| 101 builder.Build(), |
| 102 base::Bind(&HeadlessShell::OnStart, base::Unretained(&shell))); |
| 103 } |
OLD | NEW |