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/domains/page.h" |
| 17 #include "headless/public/domains/runtime.h" |
17 #include "headless/public/headless_browser.h" | 18 #include "headless/public/headless_browser.h" |
18 #include "headless/public/headless_devtools_client.h" | 19 #include "headless/public/headless_devtools_client.h" |
19 #include "headless/public/headless_devtools_target.h" | 20 #include "headless/public/headless_devtools_target.h" |
20 #include "headless/public/headless_web_contents.h" | 21 #include "headless/public/headless_web_contents.h" |
21 #include "net/base/ip_address.h" | 22 #include "net/base/ip_address.h" |
22 #include "ui/gfx/geometry/size.h" | 23 #include "ui/gfx/geometry/size.h" |
23 | 24 |
24 using headless::HeadlessBrowser; | 25 using headless::HeadlessBrowser; |
25 using headless::HeadlessDevToolsClient; | 26 using headless::HeadlessDevToolsClient; |
26 using headless::HeadlessWebContents; | 27 using headless::HeadlessWebContents; |
27 namespace page = headless::page; | 28 namespace page = headless::page; |
| 29 namespace runtime = headless::runtime; |
28 | 30 |
29 namespace { | 31 namespace { |
30 // Address where to listen to incoming DevTools connections. | 32 // Address where to listen to incoming DevTools connections. |
31 const char kDevToolsHttpServerAddress[] = "127.0.0.1"; | 33 const char kDevToolsHttpServerAddress[] = "127.0.0.1"; |
32 } | 34 } |
33 | 35 |
34 // A sample application which demonstrates the use of the headless API. | 36 // A sample application which demonstrates the use of the headless API. |
35 class HeadlessShell : public HeadlessWebContents::Observer { | 37 class HeadlessShell : public HeadlessWebContents::Observer, page::Observer { |
36 public: | 38 public: |
37 HeadlessShell() | 39 HeadlessShell() |
38 : browser_(nullptr), devtools_client_(HeadlessDevToolsClient::Create()) {} | 40 : browser_(nullptr), devtools_client_(HeadlessDevToolsClient::Create()) {} |
39 ~HeadlessShell() override {} | 41 ~HeadlessShell() override {} |
40 | 42 |
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. | |
45 } | |
46 | |
47 void OnStart(HeadlessBrowser* browser) { | 43 void OnStart(HeadlessBrowser* browser) { |
48 browser_ = browser; | 44 browser_ = browser; |
49 | 45 |
50 base::CommandLine::StringVector args = | 46 base::CommandLine::StringVector args = |
51 base::CommandLine::ForCurrentProcess()->GetArgs(); | 47 base::CommandLine::ForCurrentProcess()->GetArgs(); |
52 | 48 |
53 const char kDefaultUrl[] = "about:blank"; | 49 const char kDefaultUrl[] = "about:blank"; |
54 GURL url; | 50 GURL url; |
55 if (args.empty() || args[0].empty()) { | 51 if (args.empty() || args[0].empty()) { |
56 url = GURL(kDefaultUrl); | 52 url = GURL(kDefaultUrl); |
57 } else { | 53 } else { |
58 url = GURL(args[0]); | 54 url = GURL(args[0]); |
59 } | 55 } |
60 web_contents_ = browser->CreateWebContents(url, gfx::Size(800, 600)); | 56 web_contents_ = browser->CreateWebContents(url, gfx::Size(800, 600)); |
61 if (!web_contents_) { | 57 if (!web_contents_) { |
62 LOG(ERROR) << "Navigation failed"; | 58 LOG(ERROR) << "Navigation failed"; |
63 browser_->Shutdown(); | 59 browser_->Shutdown(); |
64 return; | 60 return; |
65 } | 61 } |
66 web_contents_->AddObserver(this); | 62 web_contents_->AddObserver(this); |
67 } | 63 } |
68 | 64 |
69 void ShutdownIfNeeded() { | 65 void Shutdown() { |
| 66 if (!web_contents_) |
| 67 return; |
70 if (!RemoteDebuggingEnabled()) { | 68 if (!RemoteDebuggingEnabled()) { |
| 69 devtools_client_->GetPage()->RemoveObserver(this); |
71 web_contents_->GetDevToolsTarget()->DetachClient(devtools_client_.get()); | 70 web_contents_->GetDevToolsTarget()->DetachClient(devtools_client_.get()); |
72 web_contents_->RemoveObserver(this); | |
73 web_contents_ = nullptr; | |
74 browser_->Shutdown(); | |
75 } | 71 } |
| 72 web_contents_->RemoveObserver(this); |
| 73 web_contents_ = nullptr; |
| 74 browser_->Shutdown(); |
76 } | 75 } |
77 | 76 |
78 // HeadlessWebContents::Observer implementation: | 77 // HeadlessWebContents::Observer implementation: |
79 void DocumentOnLoadCompletedInMainFrame() override { | 78 void DevToolsTargetReady() override { |
80 ShutdownIfNeeded(); | 79 if (RemoteDebuggingEnabled()) |
| 80 return; |
| 81 web_contents_->GetDevToolsTarget()->AttachClient(devtools_client_.get()); |
| 82 devtools_client_->GetPage()->AddObserver(this); |
| 83 devtools_client_->GetPage()->Enable(); |
| 84 // Check if the document had already finished loading by the time we |
| 85 // attached. |
| 86 devtools_client_->GetRuntime()->Evaluate( |
| 87 "document.readyState", |
| 88 base::Bind(&HeadlessShell::OnReadyState, base::Unretained(this))); |
| 89 // TODO(skyostil): Implement more features to demonstrate the devtools API. |
| 90 } |
| 91 |
| 92 void OnReadyState(std::unique_ptr<runtime::EvaluateResult> result) { |
| 93 std::string ready_state; |
| 94 if (result->GetResult()->GetValue()->GetAsString(&ready_state) && |
| 95 ready_state == "complete") |
| 96 Shutdown(); |
| 97 } |
| 98 |
| 99 // page::Observer implementation: |
| 100 void OnLoadEventFired(const page::LoadEventFiredParams& params) override { |
| 101 Shutdown(); |
81 } | 102 } |
82 | 103 |
83 bool RemoteDebuggingEnabled() const { | 104 bool RemoteDebuggingEnabled() const { |
84 const base::CommandLine& command_line = | 105 const base::CommandLine& command_line = |
85 *base::CommandLine::ForCurrentProcess(); | 106 *base::CommandLine::ForCurrentProcess(); |
86 return command_line.HasSwitch(switches::kRemoteDebuggingPort); | 107 return command_line.HasSwitch(switches::kRemoteDebuggingPort); |
87 } | 108 } |
88 | 109 |
89 private: | 110 private: |
90 HeadlessBrowser* browser_; // Not owned. | 111 HeadlessBrowser* browser_; // Not owned. |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 | 150 |
130 if (command_line.HasSwitch(switches::kHostResolverRules)) { | 151 if (command_line.HasSwitch(switches::kHostResolverRules)) { |
131 builder.SetHostResolverRules( | 152 builder.SetHostResolverRules( |
132 command_line.GetSwitchValueASCII(switches::kHostResolverRules)); | 153 command_line.GetSwitchValueASCII(switches::kHostResolverRules)); |
133 } | 154 } |
134 | 155 |
135 return HeadlessBrowserMain( | 156 return HeadlessBrowserMain( |
136 builder.Build(), | 157 builder.Build(), |
137 base::Bind(&HeadlessShell::OnStart, base::Unretained(&shell))); | 158 base::Bind(&HeadlessShell::OnStart, base::Unretained(&shell))); |
138 } | 159 } |
OLD | NEW |