Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(581)

Side by Side Diff: headless/app/headless_shell.cc

Issue 1674263002: headless: Initial headless embedder API implementation (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix shutdown memory leak. Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/numerics/safe_conversions.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "content/public/common/content_switches.h"
13 #include "headless/public/headless_browser.h"
14 #include "headless/public/headless_web_contents.h"
15 #include "ui/gfx/geometry/size.h"
16
17 using headless::HeadlessBrowser;
18 using headless::HeadlessWebContents;
19
20 namespace {
21 // Address where to listen to incoming DevTools connections.
22 const char kDevToolsHttpServerAddress[] = "127.0.0.1";
23 }
24
25 // A sample application which demonstrates the use of the headless API.
26 class HeadlessShell : public HeadlessWebContents::Observer {
27 public:
28 HeadlessShell() : browser_(nullptr) {}
29 ~HeadlessShell() override {
30 if (web_contents_)
31 web_contents_->RemoveObserver(this);
32 }
33
34 void OnStart(HeadlessBrowser* browser) {
35 browser_ = browser;
36 web_contents_ = browser->CreateWebContents(gfx::Size(800, 600));
37 web_contents_->AddObserver(this);
38
39 base::CommandLine::StringVector args =
40 base::CommandLine::ForCurrentProcess()->GetArgs();
41
42 const char kDefaultUrl[] = "https://google.com";
Ryan Sleevi 2016/02/25 22:04:36 Any reason this couldn't just be about:blank ? (R
Sami 2016/02/26 18:49:16 Good point, about:blank sgtm.
43 GURL url;
44 if (args.empty() || args[0].empty()) {
45 url = GURL(kDefaultUrl);
46 } else {
47 url = GURL(args[0]);
48 }
49 if (!web_contents_->OpenURL(url)) {
50 LOG(ERROR) << "Navigation failed";
51 web_contents_ = nullptr;
52 browser_->Shutdown();
53 }
54 }
55
56 void ShutdownIfNeeded() {
57 const base::CommandLine& command_line =
58 *base::CommandLine::ForCurrentProcess();
59 if (!command_line.HasSwitch(switches::kRemoteDebuggingPort)) {
60 web_contents_ = nullptr;
61 browser_->Shutdown();
62 }
63 }
64
65 // HeadlessWebContents::Observer implementation:
66 void DocumentOnLoadCompletedInMainFrame() override {
67 LOG(INFO) << "Document load completed";
68 ShutdownIfNeeded();
69 }
70
71 private:
72 HeadlessBrowser* browser_; // Not owned.
73 scoped_ptr<HeadlessWebContents> web_contents_;
74
75 DISALLOW_COPY_AND_ASSIGN(HeadlessShell);
76 };
77
78 int main(int argc, const char** argv) {
79 HeadlessShell shell;
80 HeadlessBrowser::Options::Builder builder(argc, argv);
81
82 // Enable devtools if requested.
83 base::CommandLine command_line(argc, argv);
84 if (command_line.HasSwitch(switches::kRemoteDebuggingPort)) {
85 int parsed_port;
86 std::string port_str =
87 command_line.GetSwitchValueASCII(switches::kRemoteDebuggingPort);
88 if (base::StringToInt(port_str, &parsed_port) &&
89 base::IsValueInRangeForNumericType<uint16_t>(parsed_port)) {
90 builder.EnableDevToolsServer(kDevToolsHttpServerAddress,
91 base::checked_cast<uint16_t>(parsed_port));
92 }
93 }
94
95 return HeadlessBrowserMain(
96 builder.Build(),
97 base::Bind(&HeadlessShell::OnStart, base::Unretained(&shell)));
98 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698