| 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 <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/command_line.h" |
| 9 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| 10 #include "base/threading/thread_task_runner_handle.h" | 11 #include "base/threading/thread_task_runner_handle.h" |
| 11 #include "content/public/app/content_main.h" | 12 #include "content/public/app/content_main.h" |
| 12 #include "content/public/browser/browser_thread.h" | 13 #include "content/public/browser/browser_thread.h" |
| 13 #include "content/public/browser/web_contents.h" | 14 #include "content/public/browser/web_contents.h" |
| 15 #include "content/public/common/content_switches.h" |
| 14 #include "headless/lib/browser/headless_browser_context_impl.h" | 16 #include "headless/lib/browser/headless_browser_context_impl.h" |
| 15 #include "headless/lib/browser/headless_browser_main_parts.h" | 17 #include "headless/lib/browser/headless_browser_main_parts.h" |
| 16 #include "headless/lib/browser/headless_web_contents_impl.h" | 18 #include "headless/lib/browser/headless_web_contents_impl.h" |
| 17 #include "headless/lib/browser/headless_window_tree_client.h" | 19 #include "headless/lib/browser/headless_window_tree_client.h" |
| 18 #include "headless/lib/headless_content_main_delegate.h" | 20 #include "headless/lib/headless_content_main_delegate.h" |
| 19 #include "ui/aura/env.h" | 21 #include "ui/aura/env.h" |
| 20 #include "ui/aura/window_tree_host.h" | 22 #include "ui/aura/window_tree_host.h" |
| 21 #include "ui/gfx/geometry/size.h" | 23 #include "ui/gfx/geometry/size.h" |
| 22 | 24 |
| 23 namespace headless { | 25 namespace headless { |
| 26 namespace { |
| 27 |
| 28 int RunContentMain( |
| 29 HeadlessBrowser::Options options, |
| 30 const base::Callback<void(HeadlessBrowser*)>& on_browser_start_callback) { |
| 31 content::ContentMainParams params(nullptr); |
| 32 params.argc = options.argc; |
| 33 params.argv = options.argv; |
| 34 |
| 35 // TODO(skyostil): Implement custom message pumps. |
| 36 DCHECK(!options.message_pump); |
| 37 |
| 38 std::unique_ptr<HeadlessBrowserImpl> browser( |
| 39 new HeadlessBrowserImpl(on_browser_start_callback, std::move(options))); |
| 40 headless::HeadlessContentMainDelegate delegate(std::move(browser)); |
| 41 params.delegate = &delegate; |
| 42 return content::ContentMain(params); |
| 43 } |
| 44 |
| 45 } // namespace |
| 24 | 46 |
| 25 HeadlessBrowserImpl::HeadlessBrowserImpl( | 47 HeadlessBrowserImpl::HeadlessBrowserImpl( |
| 26 const base::Callback<void(HeadlessBrowser*)>& on_start_callback, | 48 const base::Callback<void(HeadlessBrowser*)>& on_start_callback, |
| 27 HeadlessBrowser::Options options) | 49 HeadlessBrowser::Options options) |
| 28 : on_start_callback_(on_start_callback), | 50 : on_start_callback_(on_start_callback), |
| 29 options_(std::move(options)), | 51 options_(std::move(options)), |
| 30 browser_main_parts_(nullptr) { | 52 browser_main_parts_(nullptr) { |
| 31 DCHECK(!on_start_callback_.is_null()); | |
| 32 } | 53 } |
| 33 | 54 |
| 34 HeadlessBrowserImpl::~HeadlessBrowserImpl() {} | 55 HeadlessBrowserImpl::~HeadlessBrowserImpl() {} |
| 35 | 56 |
| 36 HeadlessWebContents::Builder HeadlessBrowserImpl::CreateWebContentsBuilder() { | 57 HeadlessWebContents::Builder HeadlessBrowserImpl::CreateWebContentsBuilder() { |
| 37 DCHECK(BrowserMainThread()->BelongsToCurrentThread()); | 58 DCHECK(BrowserMainThread()->BelongsToCurrentThread()); |
| 38 return HeadlessWebContents::Builder(this); | 59 return HeadlessWebContents::Builder(this); |
| 39 } | 60 } |
| 40 | 61 |
| 41 HeadlessBrowserContext::Builder | 62 HeadlessBrowserContext::Builder |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 web_contents_.erase(it); | 155 web_contents_.erase(it); |
| 135 } | 156 } |
| 136 | 157 |
| 137 void HeadlessBrowserImpl::SetOptionsForTesting( | 158 void HeadlessBrowserImpl::SetOptionsForTesting( |
| 138 HeadlessBrowser::Options options) { | 159 HeadlessBrowser::Options options) { |
| 139 options_ = std::move(options); | 160 options_ = std::move(options); |
| 140 browser_main_parts()->default_browser_context()->SetOptionsForTesting( | 161 browser_main_parts()->default_browser_context()->SetOptionsForTesting( |
| 141 &options_); | 162 &options_); |
| 142 } | 163 } |
| 143 | 164 |
| 165 void RunChildProcessIfNeeded(int argc, const char** argv) { |
| 166 base::CommandLine command_line(argc, argv); |
| 167 if (!command_line.HasSwitch(switches::kProcessType)) |
| 168 return; |
| 169 |
| 170 HeadlessBrowser::Options::Builder builder(argc, argv); |
| 171 exit(RunContentMain(builder.Build(), |
| 172 base::Callback<void(HeadlessBrowser*)>())); |
| 173 } |
| 174 |
| 144 int HeadlessBrowserMain( | 175 int HeadlessBrowserMain( |
| 145 HeadlessBrowser::Options options, | 176 HeadlessBrowser::Options options, |
| 146 const base::Callback<void(HeadlessBrowser*)>& on_browser_start_callback) { | 177 const base::Callback<void(HeadlessBrowser*)>& on_browser_start_callback) { |
| 147 content::ContentMainParams params(nullptr); | 178 DCHECK(!on_browser_start_callback.is_null()); |
| 148 params.argc = options.argc; | 179 #if DCHECK_IS_ON() |
| 149 params.argv = options.argv; | 180 // The browser can only be initialized once. |
| 181 static bool browser_was_initialized; |
| 182 DCHECK(!browser_was_initialized); |
| 183 browser_was_initialized = true; |
| 150 | 184 |
| 151 // TODO(skyostil): Implement custom message pumps. | 185 // Child processes should not end up here. |
| 152 DCHECK(!options.message_pump); | 186 base::CommandLine command_line(options.argc, options.argv); |
| 153 | 187 DCHECK(!command_line.HasSwitch(switches::kProcessType)); |
| 154 std::unique_ptr<HeadlessBrowserImpl> browser( | 188 #endif |
| 155 new HeadlessBrowserImpl(on_browser_start_callback, std::move(options))); | 189 return RunContentMain(std::move(options), |
| 156 headless::HeadlessContentMainDelegate delegate(std::move(browser)); | 190 std::move(on_browser_start_callback)); |
| 157 params.delegate = &delegate; | |
| 158 return content::ContentMain(params); | |
| 159 } | 191 } |
| 160 | 192 |
| 161 } // namespace headless | 193 } // namespace headless |
| OLD | NEW |