Chromium Code Reviews| 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. | |
|
Sami
2016/08/03 11:20:30
Is it a problem if a HeadlessBrowserContext outliv
altimin
2016/08/03 11:54:10
We can have weak pointers to browser contexts and
Sami
2016/08/03 13:46:14
If the user needs to delete their contexts before
altimin
2016/08/03 14:18:51
Done.
| |
| 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 21 matching lines...) Expand all Loading... | |
| 133 aura::WindowTreeHost::Create(gfx::Rect(kDefaultSize))); | 122 aura::WindowTreeHost::Create(gfx::Rect(kDefaultSize))); |
| 134 window_tree_host_->InitHost(); | 123 window_tree_host_->InitHost(); |
| 135 | 124 |
| 136 window_tree_client_.reset( | 125 window_tree_client_.reset( |
| 137 new HeadlessWindowTreeClient(window_tree_host_->window())); | 126 new HeadlessWindowTreeClient(window_tree_host_->window())); |
| 138 | 127 |
| 139 on_start_callback_.Run(this); | 128 on_start_callback_.Run(this); |
| 140 on_start_callback_ = base::Callback<void(HeadlessBrowser*)>(); | 129 on_start_callback_ = base::Callback<void(HeadlessBrowser*)>(); |
| 141 } | 130 } |
| 142 | 131 |
| 132 HeadlessWebContents* HeadlessBrowserImpl::CreateWebContents( | |
| 133 HeadlessWebContents::Builder* builder) { | |
| 134 DCHECK(BrowserMainThread()->BelongsToCurrentThread()); | |
| 135 std::unique_ptr<HeadlessWebContentsImpl> headless_web_contents = | |
| 136 HeadlessWebContentsImpl::Create(builder, window_tree_host_->window()); | |
| 137 if (!headless_web_contents) | |
| 138 return nullptr; | |
| 139 builder->browser_context_->RegisterWebContents(headless_web_contents.get()); | |
| 140 return RegisterWebContents(std::move(headless_web_contents)); | |
| 141 } | |
| 142 | |
| 143 HeadlessWebContentsImpl* HeadlessBrowserImpl::RegisterWebContents( | 143 HeadlessWebContentsImpl* HeadlessBrowserImpl::RegisterWebContents( |
| 144 std::unique_ptr<HeadlessWebContentsImpl> web_contents) { | 144 std::unique_ptr<HeadlessWebContentsImpl> web_contents) { |
| 145 DCHECK(web_contents); | 145 DCHECK(web_contents); |
| 146 HeadlessWebContentsImpl* unowned_web_contents = web_contents.get(); | 146 HeadlessWebContentsImpl* unowned_web_contents = web_contents.get(); |
| 147 web_contents_map_[unowned_web_contents->GetDevtoolsAgentHostId()] = | 147 web_contents_map_[unowned_web_contents->GetDevtoolsAgentHostId()] = |
| 148 std::move(web_contents); | 148 std::move(web_contents); |
| 149 return unowned_web_contents; | 149 return unowned_web_contents; |
| 150 } | 150 } |
| 151 | 151 |
| 152 void HeadlessBrowserImpl::DestroyWebContents( | 152 void HeadlessBrowserImpl::DestroyWebContents( |
| 153 HeadlessWebContentsImpl* web_contents) { | 153 HeadlessWebContentsImpl* web_contents) { |
| 154 auto it = web_contents_map_.find(web_contents->GetDevtoolsAgentHostId()); | 154 auto it = web_contents_map_.find(web_contents->GetDevtoolsAgentHostId()); |
| 155 DCHECK(it != web_contents_map_.end()); | 155 DCHECK(it != web_contents_map_.end()); |
| 156 web_contents_map_.erase(it); | 156 web_contents_map_.erase(it); |
| 157 } | 157 } |
| 158 | 158 |
| 159 HeadlessDevToolsManagerDelegate* | |
| 160 HeadlessBrowserImpl::devtools_manager_delegate() const { | |
| 161 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 162 return devtools_manager_delegate_.get(); | |
| 163 } | |
| 164 | |
| 165 void HeadlessBrowserImpl::set_devtools_manager_delegate( | |
| 166 base::WeakPtr<HeadlessDevToolsManagerDelegate> devtools_manager_delegate) { | |
| 167 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 168 devtools_manager_delegate_ = devtools_manager_delegate; | |
| 169 } | |
| 170 | |
| 159 HeadlessWebContents* HeadlessBrowserImpl::GetWebContentsForDevtoolsAgentHostId( | 171 HeadlessWebContents* HeadlessBrowserImpl::GetWebContentsForDevtoolsAgentHostId( |
| 160 const std::string& devtools_agent_host_id) { | 172 const std::string& devtools_agent_host_id) { |
| 161 auto it = web_contents_map_.find(devtools_agent_host_id); | 173 auto it = web_contents_map_.find(devtools_agent_host_id); |
| 162 if (it == web_contents_map_.end()) | 174 if (it == web_contents_map_.end()) |
| 163 return nullptr; | 175 return nullptr; |
| 164 return it->second.get(); | 176 return it->second.get(); |
| 165 } | 177 } |
| 166 | 178 |
| 167 void HeadlessBrowserImpl::SetOptionsForTesting( | |
| 168 HeadlessBrowser::Options options) { | |
| 169 options_ = std::move(options); | |
| 170 browser_main_parts()->default_browser_context()->SetOptionsForTesting( | |
| 171 &options_); | |
| 172 } | |
| 173 | |
| 174 void RunChildProcessIfNeeded(int argc, const char** argv) { | 179 void RunChildProcessIfNeeded(int argc, const char** argv) { |
| 175 base::CommandLine command_line(argc, argv); | 180 base::CommandLine command_line(argc, argv); |
| 176 if (!command_line.HasSwitch(switches::kProcessType)) | 181 if (!command_line.HasSwitch(switches::kProcessType)) |
| 177 return; | 182 return; |
| 178 | 183 |
| 179 HeadlessBrowser::Options::Builder builder(argc, argv); | 184 HeadlessBrowser::Options::Builder builder(argc, argv); |
| 180 exit(RunContentMain(builder.Build(), | 185 exit(RunContentMain(builder.Build(), |
| 181 base::Callback<void(HeadlessBrowser*)>())); | 186 base::Callback<void(HeadlessBrowser*)>())); |
| 182 } | 187 } |
| 183 | 188 |
| 184 int HeadlessBrowserMain( | 189 int HeadlessBrowserMain( |
| 185 HeadlessBrowser::Options options, | 190 HeadlessBrowser::Options options, |
| 186 const base::Callback<void(HeadlessBrowser*)>& on_browser_start_callback) { | 191 const base::Callback<void(HeadlessBrowser*)>& on_browser_start_callback) { |
| 187 DCHECK(!on_browser_start_callback.is_null()); | 192 DCHECK(!on_browser_start_callback.is_null()); |
| 188 #if DCHECK_IS_ON() | 193 #if DCHECK_IS_ON() |
| 189 // The browser can only be initialized once. | 194 // The browser can only be initialized once. |
| 190 static bool browser_was_initialized; | 195 static bool browser_was_initialized; |
| 191 DCHECK(!browser_was_initialized); | 196 DCHECK(!browser_was_initialized); |
| 192 browser_was_initialized = true; | 197 browser_was_initialized = true; |
| 193 | 198 |
| 194 // Child processes should not end up here. | 199 // Child processes should not end up here. |
| 195 base::CommandLine command_line(options.argc, options.argv); | 200 base::CommandLine command_line(options.argc, options.argv); |
| 196 DCHECK(!command_line.HasSwitch(switches::kProcessType)); | 201 DCHECK(!command_line.HasSwitch(switches::kProcessType)); |
| 197 #endif | 202 #endif |
| 198 return RunContentMain(std::move(options), | 203 return RunContentMain(std::move(options), |
| 199 std::move(on_browser_start_callback)); | 204 std::move(on_browser_start_callback)); |
| 200 } | 205 } |
| 201 | 206 |
| 202 } // namespace headless | 207 } // namespace headless |
| OLD | NEW |