Index: headless/app/headless_shell.cc |
diff --git a/headless/app/headless_shell.cc b/headless/app/headless_shell.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..ffa1ff174cdf28fb2863df924038df290f244d80 |
--- /dev/null |
+++ b/headless/app/headless_shell.cc |
@@ -0,0 +1,80 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "base/bind.h" |
+#include "base/callback.h" |
+#include "base/command_line.h" |
+#include "base/location.h" |
+#include "base/memory/ref_counted.h" |
+#include "base/strings/string_number_conversions.h" |
+#include "content/public/common/content_switches.h" |
+#include "headless/public/headless_browser.h" |
+#include "headless/public/headless_web_contents.h" |
+#include "ui/gfx/geometry/size.h" |
+ |
+using headless::HeadlessBrowser; |
+using headless::HeadlessWebContents; |
+ |
+// A sample application which demonstrates the use of the headless API. |
+class HeadlessShell : public HeadlessWebContents::Observer { |
+ public: |
+ HeadlessShell() : browser_(nullptr) {} |
+ ~HeadlessShell() override {} |
+ |
+ void OnStart(HeadlessBrowser* browser) { |
+ browser_ = browser; |
+ web_contents_ = browser->CreateWebContents({800, 600}); |
+ Observe(web_contents_.get()); |
+ |
+ base::CommandLine::StringVector args = |
+ base::CommandLine::ForCurrentProcess()->GetArgs(); |
+ |
+ const char kDefaultUrl[] = "https://google.com"; |
+ std::string url; |
+ if (args.empty() || args[0].empty()) { |
+ url = kDefaultUrl; |
+ } else { |
+ url = args[0]; |
+ } |
+ web_contents_->OpenURL(GURL(url)); |
+ } |
+ |
+ void StopIfNeeded() { |
+ const base::CommandLine& command_line = |
+ *base::CommandLine::ForCurrentProcess(); |
+ if (!command_line.HasSwitch(switches::kRemoteDebuggingPort)) |
+ browser_->Stop(); |
+ } |
+ |
+ // HeadlessWebContents::Observer implementation: |
+ void DocumentOnLoadCompletedInMainFrame() override { |
+ LOG(INFO) << "Document load completed"; |
+ StopIfNeeded(); |
+ } |
+ |
+ private: |
+ HeadlessBrowser* browser_; |
alex clarke (OOO till 29th)
2016/02/10 13:20:33
Please document ownership of this pointer.
Sami
2016/02/10 16:43:32
Done.
|
+ scoped_ptr<HeadlessWebContents> web_contents_; |
+}; |
+ |
+int main(int argc, const char** argv) { |
+ HeadlessShell shell; |
+ HeadlessBrowser::Options::Builder builder(argc, argv); |
+ |
+ // Enable devtools if requested. |
+ base::CommandLine command_line(argc, argv); |
+ if (command_line.HasSwitch(switches::kRemoteDebuggingPort)) { |
+ int temp_port; |
+ std::string port_str = |
+ command_line.GetSwitchValueASCII(switches::kRemoteDebuggingPort); |
+ if (base::StringToInt(port_str, &temp_port) && temp_port >= 0 && |
+ temp_port < 65535) { |
+ builder.EnableDevToolsServer(static_cast<uint16_t>(temp_port)); |
+ } |
+ } |
+ |
+ return HeadlessBrowser::Run( |
+ builder.Build(), |
+ base::Bind(&HeadlessShell::OnStart, base::Unretained(&shell))); |
+} |