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" |
11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
12 #include "base/numerics/safe_conversions.h" | 12 #include "base/numerics/safe_conversions.h" |
13 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
14 #include "content/public/common/content_switches.h" | 14 #include "content/public/common/content_switches.h" |
15 #include "headless/app/headless_shell_switches.h" | 15 #include "headless/app/headless_shell_switches.h" |
| 16 #include "headless/public/domains/page.h" |
16 #include "headless/public/headless_browser.h" | 17 #include "headless/public/headless_browser.h" |
| 18 #include "headless/public/headless_devtools_client.h" |
| 19 #include "headless/public/headless_devtools_target.h" |
17 #include "headless/public/headless_web_contents.h" | 20 #include "headless/public/headless_web_contents.h" |
18 #include "net/base/ip_address.h" | 21 #include "net/base/ip_address.h" |
19 #include "ui/gfx/geometry/size.h" | 22 #include "ui/gfx/geometry/size.h" |
20 | 23 |
21 using headless::HeadlessBrowser; | 24 using headless::HeadlessBrowser; |
| 25 using headless::HeadlessDevToolsClient; |
22 using headless::HeadlessWebContents; | 26 using headless::HeadlessWebContents; |
| 27 namespace page = headless::page; |
23 | 28 |
24 namespace { | 29 namespace { |
25 // Address where to listen to incoming DevTools connections. | 30 // Address where to listen to incoming DevTools connections. |
26 const char kDevToolsHttpServerAddress[] = "127.0.0.1"; | 31 const char kDevToolsHttpServerAddress[] = "127.0.0.1"; |
27 } | 32 } |
28 | 33 |
29 // A sample application which demonstrates the use of the headless API. | 34 // A sample application which demonstrates the use of the headless API. |
30 class HeadlessShell : public HeadlessWebContents::Observer { | 35 class HeadlessShell : public HeadlessWebContents::Observer { |
31 public: | 36 public: |
32 HeadlessShell() : browser_(nullptr) {} | 37 HeadlessShell() |
33 ~HeadlessShell() override { | 38 : browser_(nullptr), devtools_client_(HeadlessDevToolsClient::Create()) {} |
34 if (web_contents_) | 39 ~HeadlessShell() override {} |
35 web_contents_->RemoveObserver(this); | 40 |
| 41 void DevToolsTargetReady() override { |
| 42 if (!RemoteDebuggingEnabled()) |
| 43 web_contents_->GetDevToolsTarget()->AttachClient(devtools_client_.get()); |
| 44 // TODO(skyostil): Implement more features to demonstrate the devtools API. |
36 } | 45 } |
37 | 46 |
38 void OnStart(HeadlessBrowser* browser) { | 47 void OnStart(HeadlessBrowser* browser) { |
39 browser_ = browser; | 48 browser_ = browser; |
40 | 49 |
41 base::CommandLine::StringVector args = | 50 base::CommandLine::StringVector args = |
42 base::CommandLine::ForCurrentProcess()->GetArgs(); | 51 base::CommandLine::ForCurrentProcess()->GetArgs(); |
43 | 52 |
44 const char kDefaultUrl[] = "about:blank"; | 53 const char kDefaultUrl[] = "about:blank"; |
45 GURL url; | 54 GURL url; |
46 if (args.empty() || args[0].empty()) { | 55 if (args.empty() || args[0].empty()) { |
47 url = GURL(kDefaultUrl); | 56 url = GURL(kDefaultUrl); |
48 } else { | 57 } else { |
49 url = GURL(args[0]); | 58 url = GURL(args[0]); |
50 } | 59 } |
51 web_contents_ = browser->CreateWebContents(url, gfx::Size(800, 600)); | 60 web_contents_ = browser->CreateWebContents(url, gfx::Size(800, 600)); |
52 if (!web_contents_) { | 61 if (!web_contents_) { |
53 LOG(ERROR) << "Navigation failed"; | 62 LOG(ERROR) << "Navigation failed"; |
54 browser_->Shutdown(); | 63 browser_->Shutdown(); |
55 return; | 64 return; |
56 } | 65 } |
57 web_contents_->AddObserver(this); | 66 web_contents_->AddObserver(this); |
58 } | 67 } |
59 | 68 |
60 void ShutdownIfNeeded() { | 69 void ShutdownIfNeeded() { |
61 const base::CommandLine& command_line = | 70 if (!RemoteDebuggingEnabled()) { |
62 *base::CommandLine::ForCurrentProcess(); | 71 web_contents_->GetDevToolsTarget()->DetachClient(devtools_client_.get()); |
63 if (!command_line.HasSwitch(switches::kRemoteDebuggingPort)) { | 72 web_contents_->RemoveObserver(this); |
64 web_contents_ = nullptr; | 73 web_contents_ = nullptr; |
65 browser_->Shutdown(); | 74 browser_->Shutdown(); |
66 } | 75 } |
67 } | 76 } |
68 | 77 |
69 // HeadlessWebContents::Observer implementation: | 78 // HeadlessWebContents::Observer implementation: |
70 void DocumentOnLoadCompletedInMainFrame() override { | 79 void DocumentOnLoadCompletedInMainFrame() override { |
71 ShutdownIfNeeded(); | 80 ShutdownIfNeeded(); |
72 } | 81 } |
73 | 82 |
| 83 bool RemoteDebuggingEnabled() const { |
| 84 const base::CommandLine& command_line = |
| 85 *base::CommandLine::ForCurrentProcess(); |
| 86 return command_line.HasSwitch(switches::kRemoteDebuggingPort); |
| 87 } |
| 88 |
74 private: | 89 private: |
75 HeadlessBrowser* browser_; // Not owned. | 90 HeadlessBrowser* browser_; // Not owned. |
| 91 std::unique_ptr<HeadlessDevToolsClient> devtools_client_; |
76 std::unique_ptr<HeadlessWebContents> web_contents_; | 92 std::unique_ptr<HeadlessWebContents> web_contents_; |
77 | 93 |
78 DISALLOW_COPY_AND_ASSIGN(HeadlessShell); | 94 DISALLOW_COPY_AND_ASSIGN(HeadlessShell); |
79 }; | 95 }; |
80 | 96 |
81 int main(int argc, const char** argv) { | 97 int main(int argc, const char** argv) { |
82 HeadlessShell shell; | 98 HeadlessShell shell; |
83 HeadlessBrowser::Options::Builder builder(argc, argv); | 99 HeadlessBrowser::Options::Builder builder(argc, argv); |
84 | 100 |
85 // Enable devtools if requested. | 101 // Enable devtools if requested. |
(...skipping 22 matching lines...) Expand all Loading... |
108 LOG(ERROR) << "Malformed proxy server url"; | 124 LOG(ERROR) << "Malformed proxy server url"; |
109 return EXIT_FAILURE; | 125 return EXIT_FAILURE; |
110 } | 126 } |
111 builder.SetProxyServer(parsed_proxy_server); | 127 builder.SetProxyServer(parsed_proxy_server); |
112 } | 128 } |
113 | 129 |
114 return HeadlessBrowserMain( | 130 return HeadlessBrowserMain( |
115 builder.Build(), | 131 builder.Build(), |
116 base::Bind(&HeadlessShell::OnStart, base::Unretained(&shell))); | 132 base::Bind(&HeadlessShell::OnStart, base::Unretained(&shell))); |
117 } | 133 } |
OLD | NEW |