Chromium Code Reviews| 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/strings/string_number_conversions.h" | |
| 11 #include "content/public/common/content_switches.h" | |
| 12 #include "headless/public/headless_browser.h" | |
| 13 #include "headless/public/headless_web_contents.h" | |
| 14 #include "ui/gfx/geometry/size.h" | |
| 15 | |
| 16 using headless::HeadlessBrowser; | |
| 17 using headless::HeadlessWebContents; | |
| 18 | |
| 19 // A sample application which demonstrates the use of the headless API. | |
| 20 class HeadlessShell : public HeadlessWebContents::Observer { | |
| 21 public: | |
| 22 HeadlessShell() : browser_(nullptr) {} | |
| 23 ~HeadlessShell() override {} | |
| 24 | |
| 25 void OnStart(HeadlessBrowser* browser) { | |
| 26 browser_ = browser; | |
| 27 web_contents_ = browser->CreateWebContents({800, 600}); | |
| 28 Observe(web_contents_.get()); | |
| 29 | |
| 30 base::CommandLine::StringVector args = | |
| 31 base::CommandLine::ForCurrentProcess()->GetArgs(); | |
| 32 | |
| 33 const char kDefaultUrl[] = "https://google.com"; | |
| 34 std::string url; | |
| 35 if (args.empty() || args[0].empty()) { | |
| 36 url = kDefaultUrl; | |
| 37 } else { | |
| 38 url = args[0]; | |
| 39 } | |
| 40 web_contents_->OpenURL(GURL(url)); | |
| 41 } | |
| 42 | |
| 43 void StopIfNeeded() { | |
| 44 const base::CommandLine& command_line = | |
| 45 *base::CommandLine::ForCurrentProcess(); | |
| 46 if (!command_line.HasSwitch(switches::kRemoteDebuggingPort)) | |
| 47 browser_->Stop(); | |
| 48 } | |
| 49 | |
| 50 // HeadlessWebContents::Observer implementation: | |
| 51 void DocumentOnLoadCompletedInMainFrame() override { | |
| 52 LOG(INFO) << "Document load completed"; | |
| 53 StopIfNeeded(); | |
| 54 } | |
| 55 | |
| 56 private: | |
| 57 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.
| |
| 58 scoped_ptr<HeadlessWebContents> web_contents_; | |
| 59 }; | |
| 60 | |
| 61 int main(int argc, const char** argv) { | |
| 62 HeadlessShell shell; | |
| 63 HeadlessBrowser::Options::Builder builder(argc, argv); | |
| 64 | |
| 65 // Enable devtools if requested. | |
| 66 base::CommandLine command_line(argc, argv); | |
| 67 if (command_line.HasSwitch(switches::kRemoteDebuggingPort)) { | |
| 68 int temp_port; | |
| 69 std::string port_str = | |
| 70 command_line.GetSwitchValueASCII(switches::kRemoteDebuggingPort); | |
| 71 if (base::StringToInt(port_str, &temp_port) && temp_port >= 0 && | |
| 72 temp_port < 65535) { | |
| 73 builder.EnableDevToolsServer(static_cast<uint16_t>(temp_port)); | |
| 74 } | |
| 75 } | |
| 76 | |
| 77 return HeadlessBrowser::Run( | |
| 78 builder.Build(), | |
| 79 base::Bind(&HeadlessShell::OnStart, base::Unretained(&shell))); | |
| 80 } | |
| OLD | NEW |