Chromium Code Reviews| 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..ea479ccfcfa13e25114aaf7d25fed3cd0cc74322 |
| --- /dev/null |
| +++ b/headless/app/headless_shell.cc |
| @@ -0,0 +1,82 @@ |
| +// 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}); |
|
Ryan Sleevi
2016/02/22 22:01:21
STYLE: This is still banned - https://chromium-cpp
Sami
2016/02/23 20:19:07
Agreed & done.
|
| + 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)); |
|
Ryan Sleevi
2016/02/22 22:01:21
DESIGN: You do nothing to validate the well-formed
Sami
2016/02/23 20:19:07
Added validation to OpenURL.
|
| + } |
| + |
| + 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_; // Not owned. |
| + scoped_ptr<HeadlessWebContents> web_contents_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(HeadlessShell); |
| +}; |
| + |
| +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) { |
|
Ryan Sleevi
2016/02/22 21:11:13
SECURITY/DESIGN: Please use base/numerics for conv
Sami
2016/02/23 20:19:07
Thanks for the tip -- done.
|
| + builder.EnableDevToolsServer(static_cast<uint16_t>(temp_port)); |
| + } |
| + } |
| + |
| + return HeadlessBrowser::Run( |
| + builder.Build(), |
| + base::Bind(&HeadlessShell::OnStart, base::Unretained(&shell))); |
| +} |