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