Chromium Code Reviews| 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 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| 11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/numerics/safe_conversions.h" | 12 #include "base/numerics/safe_conversions.h" |
| 13 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
| 14 #include "content/public/common/content_switches.h" | 14 #include "content/public/common/content_switches.h" |
| 15 #include "headless/app/headless_shell_switches.h" | 15 #include "headless/app/headless_shell_switches.h" |
| 16 #include "headless/public/domains/page.h" | |
| 16 #include "headless/public/headless_browser.h" | 17 #include "headless/public/headless_browser.h" |
| 18 #include "headless/public/headless_devtools_client.h" | |
| 19 #include "headless/public/headless_devtools_target.h" | |
| 17 #include "headless/public/headless_web_contents.h" | 20 #include "headless/public/headless_web_contents.h" |
| 18 #include "net/base/ip_address.h" | 21 #include "net/base/ip_address.h" |
| 19 #include "ui/gfx/geometry/size.h" | 22 #include "ui/gfx/geometry/size.h" |
| 20 | 23 |
| 21 using headless::HeadlessBrowser; | 24 using headless::HeadlessBrowser; |
| 25 using headless::HeadlessDevToolsClient; | |
| 22 using headless::HeadlessWebContents; | 26 using headless::HeadlessWebContents; |
| 27 namespace page = headless::page; | |
| 23 | 28 |
| 24 namespace { | 29 namespace { |
| 25 // Address where to listen to incoming DevTools connections. | 30 // Address where to listen to incoming DevTools connections. |
| 26 const char kDevToolsHttpServerAddress[] = "127.0.0.1"; | 31 const char kDevToolsHttpServerAddress[] = "127.0.0.1"; |
| 27 } | 32 } |
| 28 | 33 |
| 29 // A sample application which demonstrates the use of the headless API. | 34 // A sample application which demonstrates the use of the headless API. |
| 30 class HeadlessShell : public HeadlessWebContents::Observer { | 35 class HeadlessShell : public HeadlessWebContents::Observer { |
| 31 public: | 36 public: |
| 32 HeadlessShell() : browser_(nullptr) {} | 37 HeadlessShell() |
| 38 : browser_(nullptr), devtools_client_(HeadlessDevToolsClient::Create()) {} | |
| 33 ~HeadlessShell() override { | 39 ~HeadlessShell() override { |
| 34 if (web_contents_) | 40 if (web_contents_) |
| 35 web_contents_->RemoveObserver(this); | 41 web_contents_->RemoveObserver(this); |
| 36 } | 42 } |
| 37 | 43 |
| 44 void DevToolsTargetReady() override { | |
| 45 web_contents_->GetDevToolsTarget()->AttachClient(devtools_client_.get()); | |
| 46 // TODO(skyostil): Implement shorthand overloads for required parameters. | |
| 47 devtools_client_->GetPage()->Navigate( | |
| 48 page::NavigateParams::Builder().SetUrl("http://hs.fi").Build()); | |
| 49 } | |
| 50 | |
| 38 void OnStart(HeadlessBrowser* browser) { | 51 void OnStart(HeadlessBrowser* browser) { |
| 39 browser_ = browser; | 52 browser_ = browser; |
| 40 | 53 |
| 41 base::CommandLine::StringVector args = | 54 base::CommandLine::StringVector args = |
| 42 base::CommandLine::ForCurrentProcess()->GetArgs(); | 55 base::CommandLine::ForCurrentProcess()->GetArgs(); |
| 43 | 56 |
| 44 const char kDefaultUrl[] = "about:blank"; | 57 const char kDefaultUrl[] = "about:blank"; |
| 45 GURL url; | 58 GURL url; |
| 46 if (args.empty() || args[0].empty()) { | 59 if (args.empty() || args[0].empty()) { |
| 47 url = GURL(kDefaultUrl); | 60 url = GURL(kDefaultUrl); |
| 48 } else { | 61 } else { |
| 49 url = GURL(args[0]); | 62 url = GURL(args[0]); |
| 50 } | 63 } |
| 51 web_contents_ = browser->CreateWebContents(url, gfx::Size(800, 600)); | 64 web_contents_ = browser->CreateWebContents(url, gfx::Size(800, 600)); |
| 52 if (!web_contents_) { | 65 if (!web_contents_) { |
| 53 LOG(ERROR) << "Navigation failed"; | 66 LOG(ERROR) << "Navigation failed"; |
| 54 browser_->Shutdown(); | 67 browser_->Shutdown(); |
| 55 return; | 68 return; |
| 56 } | 69 } |
| 57 web_contents_->AddObserver(this); | 70 web_contents_->AddObserver(this); |
| 58 } | 71 } |
| 59 | 72 |
| 60 void ShutdownIfNeeded() { | 73 void ShutdownIfNeeded() { |
| 74 return; | |
|
altimin
2016/04/14 12:54:11
Unremoved debugging?
Sami
2016/04/15 14:43:44
Argh, yes. Cleaned up.
| |
| 61 const base::CommandLine& command_line = | 75 const base::CommandLine& command_line = |
| 62 *base::CommandLine::ForCurrentProcess(); | 76 *base::CommandLine::ForCurrentProcess(); |
| 77 // Detach the devtools client so a remote debugger can connect. | |
| 78 if (devtools_client_) { | |
| 79 web_contents_->GetDevToolsTarget()->DetachClient(devtools_client_.get()); | |
| 80 devtools_client_ = nullptr; | |
| 81 } | |
| 63 if (!command_line.HasSwitch(switches::kRemoteDebuggingPort)) { | 82 if (!command_line.HasSwitch(switches::kRemoteDebuggingPort)) { |
| 64 web_contents_ = nullptr; | 83 web_contents_ = nullptr; |
| 65 browser_->Shutdown(); | 84 browser_->Shutdown(); |
| 66 } | 85 } |
| 67 } | 86 } |
| 68 | 87 |
| 69 // HeadlessWebContents::Observer implementation: | 88 // HeadlessWebContents::Observer implementation: |
| 70 void DocumentOnLoadCompletedInMainFrame() override { | 89 void DocumentOnLoadCompletedInMainFrame() override { |
| 71 ShutdownIfNeeded(); | 90 ShutdownIfNeeded(); |
| 72 } | 91 } |
| 73 | 92 |
| 74 private: | 93 private: |
| 75 HeadlessBrowser* browser_; // Not owned. | 94 HeadlessBrowser* browser_; // Not owned. |
| 95 std::unique_ptr<HeadlessDevToolsClient> devtools_client_; | |
| 76 std::unique_ptr<HeadlessWebContents> web_contents_; | 96 std::unique_ptr<HeadlessWebContents> web_contents_; |
| 77 | 97 |
| 78 DISALLOW_COPY_AND_ASSIGN(HeadlessShell); | 98 DISALLOW_COPY_AND_ASSIGN(HeadlessShell); |
| 79 }; | 99 }; |
| 80 | 100 |
| 81 int main(int argc, const char** argv) { | 101 int main(int argc, const char** argv) { |
| 82 HeadlessShell shell; | 102 HeadlessShell shell; |
| 83 HeadlessBrowser::Options::Builder builder(argc, argv); | 103 HeadlessBrowser::Options::Builder builder(argc, argv); |
| 84 | 104 |
| 85 // Enable devtools if requested. | 105 // Enable devtools if requested. |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 108 LOG(ERROR) << "Malformed proxy server url"; | 128 LOG(ERROR) << "Malformed proxy server url"; |
| 109 return EXIT_FAILURE; | 129 return EXIT_FAILURE; |
| 110 } | 130 } |
| 111 builder.SetProxyServer(parsed_proxy_server); | 131 builder.SetProxyServer(parsed_proxy_server); |
| 112 } | 132 } |
| 113 | 133 |
| 114 return HeadlessBrowserMain( | 134 return HeadlessBrowserMain( |
| 115 builder.Build(), | 135 builder.Build(), |
| 116 base::Bind(&HeadlessShell::OnStart, base::Unretained(&shell))); | 136 base::Bind(&HeadlessShell::OnStart, base::Unretained(&shell))); |
| 117 } | 137 } |
| OLD | NEW |