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}); | |
|
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.
| |
| 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)); | |
|
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.
| |
| 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_; // Not owned. | |
| 58 scoped_ptr<HeadlessWebContents> web_contents_; | |
| 59 | |
| 60 DISALLOW_COPY_AND_ASSIGN(HeadlessShell); | |
| 61 }; | |
| 62 | |
| 63 int main(int argc, const char** argv) { | |
| 64 HeadlessShell shell; | |
| 65 HeadlessBrowser::Options::Builder builder(argc, argv); | |
| 66 | |
| 67 // Enable devtools if requested. | |
| 68 base::CommandLine command_line(argc, argv); | |
| 69 if (command_line.HasSwitch(switches::kRemoteDebuggingPort)) { | |
| 70 int temp_port; | |
| 71 std::string port_str = | |
| 72 command_line.GetSwitchValueASCII(switches::kRemoteDebuggingPort); | |
| 73 if (base::StringToInt(port_str, &temp_port) && temp_port >= 0 && | |
| 74 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.
| |
| 75 builder.EnableDevToolsServer(static_cast<uint16_t>(temp_port)); | |
| 76 } | |
| 77 } | |
| 78 | |
| 79 return HeadlessBrowser::Run( | |
| 80 builder.Build(), | |
| 81 base::Bind(&HeadlessShell::OnStart, base::Unretained(&shell))); | |
| 82 } | |
| OLD | NEW |