| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "headless/lib/browser/headless_browser_impl.h" | 5 #include "headless/lib/browser/headless_browser_impl.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/threading/thread_task_runner_handle.h" | 13 #include "base/threading/thread_task_runner_handle.h" |
| 14 #include "content/public/app/content_main.h" | 14 #include "content/public/app/content_main.h" |
| 15 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
| 16 #include "content/public/browser/devtools_agent_host.h" |
| 16 #include "content/public/browser/web_contents.h" | 17 #include "content/public/browser/web_contents.h" |
| 17 #include "content/public/common/content_switches.h" | 18 #include "content/public/common/content_switches.h" |
| 18 #include "headless/app/headless_shell_switches.h" | 19 #include "headless/app/headless_shell_switches.h" |
| 19 #include "headless/lib/browser/headless_browser_context_impl.h" | 20 #include "headless/lib/browser/headless_browser_context_impl.h" |
| 20 #include "headless/lib/browser/headless_browser_main_parts.h" | 21 #include "headless/lib/browser/headless_browser_main_parts.h" |
| 22 #include "headless/lib/browser/headless_devtools_client_impl.h" |
| 21 #include "headless/lib/browser/headless_web_contents_impl.h" | 23 #include "headless/lib/browser/headless_web_contents_impl.h" |
| 22 #include "headless/lib/headless_content_main_delegate.h" | 24 #include "headless/lib/headless_content_main_delegate.h" |
| 23 #include "net/http/http_util.h" | 25 #include "net/http/http_util.h" |
| 24 #include "ui/aura/client/focus_client.h" | 26 #include "ui/aura/client/focus_client.h" |
| 25 #include "ui/aura/env.h" | 27 #include "ui/aura/env.h" |
| 26 #include "ui/aura/window.h" | 28 #include "ui/aura/window.h" |
| 27 #include "ui/events/devices/device_data_manager.h" | 29 #include "ui/events/devices/device_data_manager.h" |
| 28 #include "ui/gfx/geometry/size.h" | 30 #include "ui/gfx/geometry/size.h" |
| 29 | 31 |
| 30 #if defined(OS_WIN) | 32 #if defined(OS_WIN) |
| 31 #include "content/public/app/sandbox_helper_win.h" | 33 #include "content/public/app/sandbox_helper_win.h" |
| 32 #include "sandbox/win/src/sandbox_types.h" | 34 #include "sandbox/win/src/sandbox_types.h" |
| 33 #endif | 35 #endif |
| 34 | 36 |
| 37 namespace content { |
| 38 class DevToolsAgentHost; |
| 39 } |
| 40 |
| 35 namespace headless { | 41 namespace headless { |
| 36 namespace { | 42 namespace { |
| 37 | 43 |
| 38 int RunContentMain( | 44 int RunContentMain( |
| 39 HeadlessBrowser::Options options, | 45 HeadlessBrowser::Options options, |
| 40 const base::Callback<void(HeadlessBrowser*)>& on_browser_start_callback) { | 46 const base::Callback<void(HeadlessBrowser*)>& on_browser_start_callback) { |
| 41 content::ContentMainParams params(nullptr); | 47 content::ContentMainParams params(nullptr); |
| 42 #if defined(OS_WIN) | 48 #if defined(OS_WIN) |
| 43 sandbox::SandboxInterfaceInfo sandbox_info = {0}; | 49 sandbox::SandboxInterfaceInfo sandbox_info = {0}; |
| 44 content::InitializeSandboxInfo(&sandbox_info); | 50 content::InitializeSandboxInfo(&sandbox_info); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 60 | 66 |
| 61 } // namespace | 67 } // namespace |
| 62 | 68 |
| 63 HeadlessBrowserImpl::HeadlessBrowserImpl( | 69 HeadlessBrowserImpl::HeadlessBrowserImpl( |
| 64 const base::Callback<void(HeadlessBrowser*)>& on_start_callback, | 70 const base::Callback<void(HeadlessBrowser*)>& on_start_callback, |
| 65 HeadlessBrowser::Options options) | 71 HeadlessBrowser::Options options) |
| 66 : on_start_callback_(on_start_callback), | 72 : on_start_callback_(on_start_callback), |
| 67 options_(std::move(options)), | 73 options_(std::move(options)), |
| 68 browser_main_parts_(nullptr), | 74 browser_main_parts_(nullptr), |
| 69 default_browser_context_(nullptr), | 75 default_browser_context_(nullptr), |
| 76 agent_host_(nullptr), |
| 70 weak_ptr_factory_(this) {} | 77 weak_ptr_factory_(this) {} |
| 71 | 78 |
| 72 HeadlessBrowserImpl::~HeadlessBrowserImpl() {} | 79 HeadlessBrowserImpl::~HeadlessBrowserImpl() {} |
| 73 | 80 |
| 74 HeadlessBrowserContext::Builder | 81 HeadlessBrowserContext::Builder |
| 75 HeadlessBrowserImpl::CreateBrowserContextBuilder() { | 82 HeadlessBrowserImpl::CreateBrowserContextBuilder() { |
| 76 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 83 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 77 return HeadlessBrowserContext::Builder(this); | 84 return HeadlessBrowserContext::Builder(this); |
| 78 } | 85 } |
| 79 | 86 |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 return browser_main_parts_; | 133 return browser_main_parts_; |
| 127 } | 134 } |
| 128 | 135 |
| 129 void HeadlessBrowserImpl::set_browser_main_parts( | 136 void HeadlessBrowserImpl::set_browser_main_parts( |
| 130 HeadlessBrowserMainParts* browser_main_parts) { | 137 HeadlessBrowserMainParts* browser_main_parts) { |
| 131 DCHECK(!browser_main_parts_); | 138 DCHECK(!browser_main_parts_); |
| 132 browser_main_parts_ = browser_main_parts; | 139 browser_main_parts_ = browser_main_parts; |
| 133 } | 140 } |
| 134 | 141 |
| 135 void HeadlessBrowserImpl::RunOnStartCallback() { | 142 void HeadlessBrowserImpl::RunOnStartCallback() { |
| 143 // We don't support the tethering domain on this agent host. |
| 144 agent_host_ = content::DevToolsAgentHost::CreateForBrowser( |
| 145 nullptr, content::DevToolsAgentHost::CreateServerSocketCallback()); |
| 146 |
| 136 PlatformCreateWindow(); | 147 PlatformCreateWindow(); |
| 137 on_start_callback_.Run(this); | 148 on_start_callback_.Run(this); |
| 138 on_start_callback_ = base::Callback<void(HeadlessBrowser*)>(); | 149 on_start_callback_ = base::Callback<void(HeadlessBrowser*)>(); |
| 139 } | 150 } |
| 140 | 151 |
| 141 HeadlessBrowserContext* HeadlessBrowserImpl::CreateBrowserContext( | 152 HeadlessBrowserContext* HeadlessBrowserImpl::CreateBrowserContext( |
| 142 HeadlessBrowserContext::Builder* builder) { | 153 HeadlessBrowserContext::Builder* builder) { |
| 143 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 154 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 144 | 155 |
| 145 std::unique_ptr<HeadlessBrowserContextImpl> browser_context = | 156 std::unique_ptr<HeadlessBrowserContextImpl> browser_context = |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 193 } | 204 } |
| 194 | 205 |
| 195 HeadlessBrowserContext* HeadlessBrowserImpl::GetBrowserContextForId( | 206 HeadlessBrowserContext* HeadlessBrowserImpl::GetBrowserContextForId( |
| 196 const std::string& id) { | 207 const std::string& id) { |
| 197 auto find_it = browser_contexts_.find(id); | 208 auto find_it = browser_contexts_.find(id); |
| 198 if (find_it == browser_contexts_.end()) | 209 if (find_it == browser_contexts_.end()) |
| 199 return nullptr; | 210 return nullptr; |
| 200 return find_it->second.get(); | 211 return find_it->second.get(); |
| 201 } | 212 } |
| 202 | 213 |
| 214 HeadlessDevToolsTarget* HeadlessBrowserImpl::GetDevToolsTarget() { |
| 215 return agent_host_ ? this : nullptr; |
| 216 } |
| 217 |
| 218 bool HeadlessBrowserImpl::AttachClient(HeadlessDevToolsClient* client) { |
| 219 DCHECK(agent_host_); |
| 220 return HeadlessDevToolsClientImpl::From(client)->AttachToHost( |
| 221 agent_host_.get()); |
| 222 } |
| 223 |
| 224 void HeadlessBrowserImpl::ForceAttachClient(HeadlessDevToolsClient* client) { |
| 225 DCHECK(agent_host_); |
| 226 HeadlessDevToolsClientImpl::From(client)->ForceAttachToHost( |
| 227 agent_host_.get()); |
| 228 } |
| 229 |
| 230 void HeadlessBrowserImpl::DetachClient(HeadlessDevToolsClient* client) { |
| 231 DCHECK(agent_host_); |
| 232 HeadlessDevToolsClientImpl::From(client)->DetachFromHost(agent_host_.get()); |
| 233 } |
| 234 |
| 235 bool HeadlessBrowserImpl::IsAttached() { |
| 236 DCHECK(agent_host_); |
| 237 return agent_host_->IsAttached(); |
| 238 } |
| 239 |
| 203 void RunChildProcessIfNeeded(int argc, const char** argv) { | 240 void RunChildProcessIfNeeded(int argc, const char** argv) { |
| 204 base::CommandLine::Init(argc, argv); | 241 base::CommandLine::Init(argc, argv); |
| 205 const base::CommandLine& command_line( | 242 const base::CommandLine& command_line( |
| 206 *base::CommandLine::ForCurrentProcess()); | 243 *base::CommandLine::ForCurrentProcess()); |
| 207 | 244 |
| 208 if (!command_line.HasSwitch(::switches::kProcessType)) | 245 if (!command_line.HasSwitch(::switches::kProcessType)) |
| 209 return; | 246 return; |
| 210 | 247 |
| 211 HeadlessBrowser::Options::Builder builder(argc, argv); | 248 HeadlessBrowser::Options::Builder builder(argc, argv); |
| 212 if (command_line.HasSwitch(switches::kUserAgent)) { | 249 if (command_line.HasSwitch(switches::kUserAgent)) { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 231 | 268 |
| 232 // Child processes should not end up here. | 269 // Child processes should not end up here. |
| 233 DCHECK(!base::CommandLine::ForCurrentProcess()->HasSwitch( | 270 DCHECK(!base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 234 ::switches::kProcessType)); | 271 ::switches::kProcessType)); |
| 235 #endif | 272 #endif |
| 236 return RunContentMain(std::move(options), | 273 return RunContentMain(std::move(options), |
| 237 std::move(on_browser_start_callback)); | 274 std::move(on_browser_start_callback)); |
| 238 } | 275 } |
| 239 | 276 |
| 240 } // namespace headless | 277 } // namespace headless |
| OLD | NEW |