| 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> |
| 8 #include <utility> |
| 7 #include <vector> | 9 #include <vector> |
| 8 | 10 |
| 9 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 10 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 11 #include "base/threading/thread_task_runner_handle.h" | 13 #include "base/threading/thread_task_runner_handle.h" |
| 12 #include "content/public/app/content_main.h" | 14 #include "content/public/app/content_main.h" |
| 13 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
| 14 #include "content/public/browser/web_contents.h" | 16 #include "content/public/browser/web_contents.h" |
| 15 #include "content/public/common/content_switches.h" | 17 #include "content/public/common/content_switches.h" |
| 16 #include "headless/lib/browser/headless_browser_context_impl.h" | 18 #include "headless/lib/browser/headless_browser_context_impl.h" |
| (...skipping 25 matching lines...) Expand all Loading... |
| 42 return content::ContentMain(params); | 44 return content::ContentMain(params); |
| 43 } | 45 } |
| 44 | 46 |
| 45 } // namespace | 47 } // namespace |
| 46 | 48 |
| 47 HeadlessBrowserImpl::HeadlessBrowserImpl( | 49 HeadlessBrowserImpl::HeadlessBrowserImpl( |
| 48 const base::Callback<void(HeadlessBrowser*)>& on_start_callback, | 50 const base::Callback<void(HeadlessBrowser*)>& on_start_callback, |
| 49 HeadlessBrowser::Options options) | 51 HeadlessBrowser::Options options) |
| 50 : on_start_callback_(on_start_callback), | 52 : on_start_callback_(on_start_callback), |
| 51 options_(std::move(options)), | 53 options_(std::move(options)), |
| 52 browser_main_parts_(nullptr) { | 54 browser_main_parts_(nullptr) {} |
| 53 } | |
| 54 | 55 |
| 55 HeadlessBrowserImpl::~HeadlessBrowserImpl() {} | 56 HeadlessBrowserImpl::~HeadlessBrowserImpl() {} |
| 56 | 57 |
| 57 HeadlessWebContents::Builder HeadlessBrowserImpl::CreateWebContentsBuilder() { | |
| 58 DCHECK(BrowserMainThread()->BelongsToCurrentThread()); | |
| 59 return HeadlessWebContents::Builder(this); | |
| 60 } | |
| 61 | |
| 62 HeadlessBrowserContext::Builder | 58 HeadlessBrowserContext::Builder |
| 63 HeadlessBrowserImpl::CreateBrowserContextBuilder() { | 59 HeadlessBrowserImpl::CreateBrowserContextBuilder() { |
| 64 DCHECK(BrowserMainThread()->BelongsToCurrentThread()); | 60 DCHECK(BrowserMainThread()->BelongsToCurrentThread()); |
| 65 return HeadlessBrowserContext::Builder(this); | 61 return HeadlessBrowserContext::Builder(this); |
| 66 } | 62 } |
| 67 | 63 |
| 68 HeadlessWebContents* HeadlessBrowserImpl::CreateWebContents( | |
| 69 HeadlessWebContents::Builder* builder) { | |
| 70 DCHECK(BrowserMainThread()->BelongsToCurrentThread()); | |
| 71 std::unique_ptr<HeadlessWebContentsImpl> headless_web_contents = | |
| 72 HeadlessWebContentsImpl::Create(builder, window_tree_host_->window(), | |
| 73 this); | |
| 74 if (!headless_web_contents) | |
| 75 return nullptr; | |
| 76 return RegisterWebContents(std::move(headless_web_contents)); | |
| 77 } | |
| 78 | |
| 79 HeadlessWebContents* HeadlessBrowserImpl::CreateWebContents( | |
| 80 const GURL& initial_url, | |
| 81 const gfx::Size& size) { | |
| 82 return CreateWebContentsBuilder() | |
| 83 .SetInitialURL(initial_url) | |
| 84 .SetWindowSize(size) | |
| 85 .Build(); | |
| 86 } | |
| 87 | |
| 88 scoped_refptr<base::SingleThreadTaskRunner> | 64 scoped_refptr<base::SingleThreadTaskRunner> |
| 89 HeadlessBrowserImpl::BrowserMainThread() const { | 65 HeadlessBrowserImpl::BrowserMainThread() const { |
| 90 return content::BrowserThread::GetTaskRunnerForThread( | 66 return content::BrowserThread::GetTaskRunnerForThread( |
| 91 content::BrowserThread::UI); | 67 content::BrowserThread::UI); |
| 92 } | 68 } |
| 93 | 69 |
| 94 scoped_refptr<base::SingleThreadTaskRunner> | 70 scoped_refptr<base::SingleThreadTaskRunner> |
| 95 HeadlessBrowserImpl::BrowserFileThread() const { | 71 HeadlessBrowserImpl::BrowserFileThread() const { |
| 96 return content::BrowserThread::GetTaskRunnerForThread( | 72 return content::BrowserThread::GetTaskRunnerForThread( |
| 97 content::BrowserThread::FILE); | 73 content::BrowserThread::FILE); |
| 98 } | 74 } |
| 99 | 75 |
| 100 void HeadlessBrowserImpl::Shutdown() { | 76 void HeadlessBrowserImpl::Shutdown() { |
| 101 DCHECK(BrowserMainThread()->BelongsToCurrentThread()); | 77 DCHECK(BrowserMainThread()->BelongsToCurrentThread()); |
| 78 |
| 79 // DevToolsManagerDelegate owns some BrowserContexts. Tell it to delete them. |
| 80 if (devtools_manager_delegate()) { |
| 81 devtools_manager_delegate()->Shutdown(); |
| 82 } |
| 83 |
| 84 // We need to close all WebContents here. |
| 85 std::vector<HeadlessWebContents*> all_web_contents = GetAllWebContents(); |
| 86 for (HeadlessWebContents* web_contents : all_web_contents) { |
| 87 web_contents->Close(); |
| 88 } |
| 89 DCHECK(web_contents_map_.empty()); |
| 90 |
| 102 BrowserMainThread()->PostTask(FROM_HERE, | 91 BrowserMainThread()->PostTask(FROM_HERE, |
| 103 base::MessageLoop::QuitWhenIdleClosure()); | 92 base::MessageLoop::QuitWhenIdleClosure()); |
| 104 } | 93 } |
| 105 | 94 |
| 106 std::vector<HeadlessWebContents*> HeadlessBrowserImpl::GetAllWebContents() { | 95 std::vector<HeadlessWebContents*> HeadlessBrowserImpl::GetAllWebContents() { |
| 107 std::vector<HeadlessWebContents*> result; | 96 std::vector<HeadlessWebContents*> result; |
| 108 result.reserve(web_contents_map_.size()); | 97 result.reserve(web_contents_map_.size()); |
| 109 | 98 |
| 110 for (const auto& web_contents_pair : web_contents_map_) { | 99 for (const auto& web_contents_pair : web_contents_map_) { |
| 111 result.push_back(web_contents_pair.second.get()); | 100 result.push_back(web_contents_pair.second.get()); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 131 aura::WindowTreeHost::Create(gfx::Rect(options()->window_size))); | 120 aura::WindowTreeHost::Create(gfx::Rect(options()->window_size))); |
| 132 window_tree_host_->InitHost(); | 121 window_tree_host_->InitHost(); |
| 133 | 122 |
| 134 window_tree_client_.reset( | 123 window_tree_client_.reset( |
| 135 new HeadlessWindowTreeClient(window_tree_host_->window())); | 124 new HeadlessWindowTreeClient(window_tree_host_->window())); |
| 136 | 125 |
| 137 on_start_callback_.Run(this); | 126 on_start_callback_.Run(this); |
| 138 on_start_callback_ = base::Callback<void(HeadlessBrowser*)>(); | 127 on_start_callback_ = base::Callback<void(HeadlessBrowser*)>(); |
| 139 } | 128 } |
| 140 | 129 |
| 130 HeadlessWebContents* HeadlessBrowserImpl::CreateWebContents( |
| 131 HeadlessWebContents::Builder* builder) { |
| 132 DCHECK(BrowserMainThread()->BelongsToCurrentThread()); |
| 133 std::unique_ptr<HeadlessWebContentsImpl> headless_web_contents = |
| 134 HeadlessWebContentsImpl::Create(builder, window_tree_host_->window()); |
| 135 if (!headless_web_contents) |
| 136 return nullptr; |
| 137 builder->browser_context_->RegisterWebContents(headless_web_contents.get()); |
| 138 return RegisterWebContents(std::move(headless_web_contents)); |
| 139 } |
| 140 |
| 141 HeadlessWebContentsImpl* HeadlessBrowserImpl::RegisterWebContents( | 141 HeadlessWebContentsImpl* HeadlessBrowserImpl::RegisterWebContents( |
| 142 std::unique_ptr<HeadlessWebContentsImpl> web_contents) { | 142 std::unique_ptr<HeadlessWebContentsImpl> web_contents) { |
| 143 DCHECK(web_contents); | 143 DCHECK(web_contents); |
| 144 HeadlessWebContentsImpl* unowned_web_contents = web_contents.get(); | 144 HeadlessWebContentsImpl* unowned_web_contents = web_contents.get(); |
| 145 web_contents_map_[unowned_web_contents->GetDevtoolsAgentHostId()] = | 145 web_contents_map_[unowned_web_contents->GetDevtoolsAgentHostId()] = |
| 146 std::move(web_contents); | 146 std::move(web_contents); |
| 147 return unowned_web_contents; | 147 return unowned_web_contents; |
| 148 } | 148 } |
| 149 | 149 |
| 150 void HeadlessBrowserImpl::DestroyWebContents( | 150 void HeadlessBrowserImpl::DestroyWebContents( |
| 151 HeadlessWebContentsImpl* web_contents) { | 151 HeadlessWebContentsImpl* web_contents) { |
| 152 auto it = web_contents_map_.find(web_contents->GetDevtoolsAgentHostId()); | 152 auto it = web_contents_map_.find(web_contents->GetDevtoolsAgentHostId()); |
| 153 DCHECK(it != web_contents_map_.end()); | 153 DCHECK(it != web_contents_map_.end()); |
| 154 web_contents_map_.erase(it); | 154 web_contents_map_.erase(it); |
| 155 } | 155 } |
| 156 | 156 |
| 157 HeadlessDevToolsManagerDelegate* |
| 158 HeadlessBrowserImpl::devtools_manager_delegate() const { |
| 159 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 160 return devtools_manager_delegate_.get(); |
| 161 } |
| 162 |
| 163 void HeadlessBrowserImpl::set_devtools_manager_delegate( |
| 164 base::WeakPtr<HeadlessDevToolsManagerDelegate> devtools_manager_delegate) { |
| 165 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 166 devtools_manager_delegate_ = devtools_manager_delegate; |
| 167 } |
| 168 |
| 157 HeadlessWebContents* HeadlessBrowserImpl::GetWebContentsForDevtoolsAgentHostId( | 169 HeadlessWebContents* HeadlessBrowserImpl::GetWebContentsForDevtoolsAgentHostId( |
| 158 const std::string& devtools_agent_host_id) { | 170 const std::string& devtools_agent_host_id) { |
| 159 auto it = web_contents_map_.find(devtools_agent_host_id); | 171 auto it = web_contents_map_.find(devtools_agent_host_id); |
| 160 if (it == web_contents_map_.end()) | 172 if (it == web_contents_map_.end()) |
| 161 return nullptr; | 173 return nullptr; |
| 162 return it->second.get(); | 174 return it->second.get(); |
| 163 } | 175 } |
| 164 | 176 |
| 165 void HeadlessBrowserImpl::SetOptionsForTesting( | |
| 166 HeadlessBrowser::Options options) { | |
| 167 options_ = std::move(options); | |
| 168 browser_main_parts()->default_browser_context()->SetOptionsForTesting( | |
| 169 &options_); | |
| 170 } | |
| 171 | |
| 172 void RunChildProcessIfNeeded(int argc, const char** argv) { | 177 void RunChildProcessIfNeeded(int argc, const char** argv) { |
| 173 base::CommandLine command_line(argc, argv); | 178 base::CommandLine command_line(argc, argv); |
| 174 if (!command_line.HasSwitch(switches::kProcessType)) | 179 if (!command_line.HasSwitch(switches::kProcessType)) |
| 175 return; | 180 return; |
| 176 | 181 |
| 177 HeadlessBrowser::Options::Builder builder(argc, argv); | 182 HeadlessBrowser::Options::Builder builder(argc, argv); |
| 178 exit(RunContentMain(builder.Build(), | 183 exit(RunContentMain(builder.Build(), |
| 179 base::Callback<void(HeadlessBrowser*)>())); | 184 base::Callback<void(HeadlessBrowser*)>())); |
| 180 } | 185 } |
| 181 | 186 |
| 182 int HeadlessBrowserMain( | 187 int HeadlessBrowserMain( |
| 183 HeadlessBrowser::Options options, | 188 HeadlessBrowser::Options options, |
| 184 const base::Callback<void(HeadlessBrowser*)>& on_browser_start_callback) { | 189 const base::Callback<void(HeadlessBrowser*)>& on_browser_start_callback) { |
| 185 DCHECK(!on_browser_start_callback.is_null()); | 190 DCHECK(!on_browser_start_callback.is_null()); |
| 186 #if DCHECK_IS_ON() | 191 #if DCHECK_IS_ON() |
| 187 // The browser can only be initialized once. | 192 // The browser can only be initialized once. |
| 188 static bool browser_was_initialized; | 193 static bool browser_was_initialized; |
| 189 DCHECK(!browser_was_initialized); | 194 DCHECK(!browser_was_initialized); |
| 190 browser_was_initialized = true; | 195 browser_was_initialized = true; |
| 191 | 196 |
| 192 // Child processes should not end up here. | 197 // Child processes should not end up here. |
| 193 base::CommandLine command_line(options.argc, options.argv); | 198 base::CommandLine command_line(options.argc, options.argv); |
| 194 DCHECK(!command_line.HasSwitch(switches::kProcessType)); | 199 DCHECK(!command_line.HasSwitch(switches::kProcessType)); |
| 195 #endif | 200 #endif |
| 196 return RunContentMain(std::move(options), | 201 return RunContentMain(std::move(options), |
| 197 std::move(on_browser_start_callback)); | 202 std::move(on_browser_start_callback)); |
| 198 } | 203 } |
| 199 | 204 |
| 200 } // namespace headless | 205 } // namespace headless |
| OLD | NEW |