OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <memory> | 5 #include <memory> |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/location.h" | 10 #include "base/location.h" |
(...skipping 19 matching lines...) Expand all Loading... |
30 class HeadlessShell : public HeadlessWebContents::Observer { | 30 class HeadlessShell : public HeadlessWebContents::Observer { |
31 public: | 31 public: |
32 HeadlessShell() : browser_(nullptr) {} | 32 HeadlessShell() : browser_(nullptr) {} |
33 ~HeadlessShell() override { | 33 ~HeadlessShell() override { |
34 if (web_contents_) | 34 if (web_contents_) |
35 web_contents_->RemoveObserver(this); | 35 web_contents_->RemoveObserver(this); |
36 } | 36 } |
37 | 37 |
38 void OnStart(HeadlessBrowser* browser) { | 38 void OnStart(HeadlessBrowser* browser) { |
39 browser_ = browser; | 39 browser_ = browser; |
40 web_contents_ = browser->CreateWebContents(gfx::Size(800, 600)); | |
41 web_contents_->AddObserver(this); | |
42 | 40 |
43 base::CommandLine::StringVector args = | 41 base::CommandLine::StringVector args = |
44 base::CommandLine::ForCurrentProcess()->GetArgs(); | 42 base::CommandLine::ForCurrentProcess()->GetArgs(); |
45 | 43 |
46 const char kDefaultUrl[] = "about:blank"; | 44 const char kDefaultUrl[] = "about:blank"; |
47 GURL url; | 45 GURL url; |
48 if (args.empty() || args[0].empty()) { | 46 if (args.empty() || args[0].empty()) { |
49 url = GURL(kDefaultUrl); | 47 url = GURL(kDefaultUrl); |
50 } else { | 48 } else { |
51 url = GURL(args[0]); | 49 url = GURL(args[0]); |
52 } | 50 } |
53 if (!web_contents_->OpenURL(url)) { | 51 web_contents_ = browser->CreateWebContents(url, gfx::Size(800, 600)); |
| 52 if (!web_contents_) { |
54 LOG(ERROR) << "Navigation failed"; | 53 LOG(ERROR) << "Navigation failed"; |
55 web_contents_ = nullptr; | |
56 browser_->Shutdown(); | 54 browser_->Shutdown(); |
| 55 return; |
57 } | 56 } |
| 57 web_contents_->AddObserver(this); |
58 } | 58 } |
59 | 59 |
60 void ShutdownIfNeeded() { | 60 void ShutdownIfNeeded() { |
61 const base::CommandLine& command_line = | 61 const base::CommandLine& command_line = |
62 *base::CommandLine::ForCurrentProcess(); | 62 *base::CommandLine::ForCurrentProcess(); |
63 if (!command_line.HasSwitch(switches::kRemoteDebuggingPort)) { | 63 if (!command_line.HasSwitch(switches::kRemoteDebuggingPort)) { |
64 web_contents_ = nullptr; | 64 web_contents_ = nullptr; |
65 browser_->Shutdown(); | 65 browser_->Shutdown(); |
66 } | 66 } |
67 } | 67 } |
68 | 68 |
69 // HeadlessWebContents::Observer implementation: | 69 // HeadlessWebContents::Observer implementation: |
70 void DocumentOnLoadCompletedInMainFrame() override { | 70 void DocumentOnLoadCompletedInMainFrame() override { |
71 ShutdownIfNeeded(); | 71 ShutdownIfNeeded(); |
72 } | 72 } |
73 | 73 |
74 void DidFinishNavigation(bool success) override {} | |
75 | |
76 private: | 74 private: |
77 HeadlessBrowser* browser_; // Not owned. | 75 HeadlessBrowser* browser_; // Not owned. |
78 std::unique_ptr<HeadlessWebContents> web_contents_; | 76 std::unique_ptr<HeadlessWebContents> web_contents_; |
79 | 77 |
80 DISALLOW_COPY_AND_ASSIGN(HeadlessShell); | 78 DISALLOW_COPY_AND_ASSIGN(HeadlessShell); |
81 }; | 79 }; |
82 | 80 |
83 int main(int argc, const char** argv) { | 81 int main(int argc, const char** argv) { |
84 HeadlessShell shell; | 82 HeadlessShell shell; |
85 HeadlessBrowser::Options::Builder builder(argc, argv); | 83 HeadlessBrowser::Options::Builder builder(argc, argv); |
(...skipping 24 matching lines...) Expand all Loading... |
110 LOG(ERROR) << "Malformed proxy server url"; | 108 LOG(ERROR) << "Malformed proxy server url"; |
111 return EXIT_FAILURE; | 109 return EXIT_FAILURE; |
112 } | 110 } |
113 builder.SetProxyServer(parsed_proxy_server); | 111 builder.SetProxyServer(parsed_proxy_server); |
114 } | 112 } |
115 | 113 |
116 return HeadlessBrowserMain( | 114 return HeadlessBrowserMain( |
117 builder.Build(), | 115 builder.Build(), |
118 base::Bind(&HeadlessShell::OnStart, base::Unretained(&shell))); | 116 base::Bind(&HeadlessShell::OnStart, base::Unretained(&shell))); |
119 } | 117 } |
OLD | NEW |