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 <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/threading/thread_task_runner_handle.h" | 10 #include "base/threading/thread_task_runner_handle.h" |
| 11 #include "content/public/app/content_main.h" | 11 #include "content/public/app/content_main.h" |
| 12 #include "content/public/browser/browser_thread.h" | 12 #include "content/public/browser/browser_thread.h" |
| 13 #include "content/public/browser/web_contents.h" | 13 #include "content/public/browser/web_contents.h" |
| 14 #include "headless/lib/browser/headless_browser_context.h" | 14 #include "headless/lib/browser/headless_browser_context_impl.h" |
| 15 #include "headless/lib/browser/headless_browser_main_parts.h" | 15 #include "headless/lib/browser/headless_browser_main_parts.h" |
| 16 #include "headless/lib/browser/headless_web_contents_impl.h" | 16 #include "headless/lib/browser/headless_web_contents_impl.h" |
| 17 #include "headless/lib/browser/headless_window_tree_client.h" | 17 #include "headless/lib/browser/headless_window_tree_client.h" |
| 18 #include "headless/lib/headless_content_main_delegate.h" | 18 #include "headless/lib/headless_content_main_delegate.h" |
| 19 #include "ui/aura/env.h" | 19 #include "ui/aura/env.h" |
| 20 #include "ui/aura/window_tree_host.h" | 20 #include "ui/aura/window_tree_host.h" |
| 21 #include "ui/gfx/geometry/size.h" | 21 #include "ui/gfx/geometry/size.h" |
| 22 | 22 |
| 23 namespace headless { | 23 namespace headless { |
| 24 | 24 |
| 25 HeadlessBrowserImpl::HeadlessBrowserImpl( | 25 HeadlessBrowserImpl::HeadlessBrowserImpl( |
| 26 const base::Callback<void(HeadlessBrowser*)>& on_start_callback, | 26 const base::Callback<void(HeadlessBrowser*)>& on_start_callback, |
| 27 HeadlessBrowser::Options options) | 27 HeadlessBrowser::Options options) |
| 28 : on_start_callback_(on_start_callback), | 28 : on_start_callback_(on_start_callback), |
| 29 options_(std::move(options)), | 29 options_(std::move(options)), |
| 30 browser_main_parts_(nullptr) { | 30 browser_main_parts_(nullptr) { |
| 31 DCHECK(!on_start_callback_.is_null()); | 31 DCHECK(!on_start_callback_.is_null()); |
| 32 } | 32 } |
| 33 | 33 |
| 34 HeadlessBrowserImpl::~HeadlessBrowserImpl() {} | 34 HeadlessBrowserImpl::~HeadlessBrowserImpl() {} |
| 35 | 35 |
| 36 HeadlessWebContents::Builder HeadlessBrowserImpl::CreateWebContentsBuilder() { | |
| 37 DCHECK(BrowserMainThread()->BelongsToCurrentThread()); | |
| 38 return HeadlessWebContents::Builder(this); | |
| 39 } | |
| 40 | |
| 41 HeadlessBrowserContext::Builder | |
| 42 HeadlessBrowserImpl::CreateBrowserContextBuilder() { | |
| 43 DCHECK(BrowserMainThread()->BelongsToCurrentThread()); | |
| 44 return HeadlessBrowserContext::Builder(this); | |
| 45 } | |
| 46 | |
| 36 HeadlessWebContents* HeadlessBrowserImpl::CreateWebContents( | 47 HeadlessWebContents* HeadlessBrowserImpl::CreateWebContents( |
| 37 const GURL& initial_url, | 48 HeadlessWebContents::Builder* builder) { |
| 38 const gfx::Size& size) { | |
| 39 DCHECK(BrowserMainThread()->BelongsToCurrentThread()); | 49 DCHECK(BrowserMainThread()->BelongsToCurrentThread()); |
| 50 HeadlessBrowserContextImpl* browser_context_impl; | |
| 51 if (builder->browser_context_) { | |
| 52 browser_context_impl = reinterpret_cast<HeadlessBrowserContextImpl*>( | |
|
alex clarke (OOO till 29th)
2016/06/09 09:15:08
Nit: Elsewhere we've used the style HeadlessBrowse
Sami
2016/06/09 11:14:39
Thanks for the reminder, done.
| |
| 53 builder->browser_context_); | |
| 54 } else { | |
| 55 browser_context_impl = browser_main_parts_->default_browser_context(); | |
| 56 } | |
| 57 | |
| 40 std::unique_ptr<HeadlessWebContentsImpl> headless_web_contents = | 58 std::unique_ptr<HeadlessWebContentsImpl> headless_web_contents = |
| 41 HeadlessWebContentsImpl::Create(browser_context(), | 59 HeadlessWebContentsImpl::Create(browser_context_impl, |
|
alex clarke (OOO till 29th)
2016/06/09 09:15:08
I wonder if it would be cleaner to pass the builde
Sami
2016/06/09 11:14:39
Yeah, I think that will be more maintainable in th
| |
| 42 window_tree_host_->window(), size, this); | 60 window_tree_host_->window(), |
| 61 builder->window_size_, this); | |
| 43 | 62 |
| 44 if (!headless_web_contents->OpenURL(initial_url)) | 63 if (!headless_web_contents->OpenURL(builder->initial_url_)) |
| 45 return nullptr; | 64 return nullptr; |
| 46 | 65 |
| 47 return RegisterWebContents(std::move(headless_web_contents)); | 66 return RegisterWebContents(std::move(headless_web_contents)); |
| 48 } | 67 } |
| 49 | 68 |
| 69 HeadlessWebContents* HeadlessBrowserImpl::CreateWebContents( | |
| 70 const GURL& initial_url, | |
| 71 const gfx::Size& size) { | |
| 72 return CreateWebContentsBuilder() | |
| 73 .SetInitialURL(initial_url) | |
| 74 .SetWindowSize(size) | |
| 75 .Build(); | |
| 76 } | |
| 77 | |
| 50 scoped_refptr<base::SingleThreadTaskRunner> | 78 scoped_refptr<base::SingleThreadTaskRunner> |
| 51 HeadlessBrowserImpl::BrowserMainThread() const { | 79 HeadlessBrowserImpl::BrowserMainThread() const { |
| 52 return content::BrowserThread::GetMessageLoopProxyForThread( | 80 return content::BrowserThread::GetMessageLoopProxyForThread( |
| 53 content::BrowserThread::UI); | 81 content::BrowserThread::UI); |
| 54 } | 82 } |
| 55 | 83 |
| 56 scoped_refptr<base::SingleThreadTaskRunner> | 84 scoped_refptr<base::SingleThreadTaskRunner> |
| 57 HeadlessBrowserImpl::BrowserFileThread() const { | 85 HeadlessBrowserImpl::BrowserFileThread() const { |
| 58 return content::BrowserThread::GetMessageLoopProxyForThread( | 86 return content::BrowserThread::GetMessageLoopProxyForThread( |
| 59 content::BrowserThread::FILE); | 87 content::BrowserThread::FILE); |
| 60 } | 88 } |
| 61 | 89 |
| 62 void HeadlessBrowserImpl::Shutdown() { | 90 void HeadlessBrowserImpl::Shutdown() { |
| 63 DCHECK(BrowserMainThread()->BelongsToCurrentThread()); | 91 DCHECK(BrowserMainThread()->BelongsToCurrentThread()); |
| 64 BrowserMainThread()->PostTask(FROM_HERE, | 92 BrowserMainThread()->PostTask(FROM_HERE, |
| 65 base::MessageLoop::QuitWhenIdleClosure()); | 93 base::MessageLoop::QuitWhenIdleClosure()); |
| 66 } | 94 } |
| 67 | 95 |
| 68 std::vector<HeadlessWebContents*> HeadlessBrowserImpl::GetAllWebContents() { | 96 std::vector<HeadlessWebContents*> HeadlessBrowserImpl::GetAllWebContents() { |
| 69 std::vector<HeadlessWebContents*> result; | 97 std::vector<HeadlessWebContents*> result; |
| 70 result.reserve(web_contents_.size()); | 98 result.reserve(web_contents_.size()); |
| 71 | 99 |
| 72 for (const auto& web_contents_pair : web_contents_) { | 100 for (const auto& web_contents_pair : web_contents_) { |
| 73 result.push_back(web_contents_pair.first); | 101 result.push_back(web_contents_pair.first); |
| 74 } | 102 } |
| 75 | 103 |
| 76 return result; | 104 return result; |
| 77 } | 105 } |
| 78 | 106 |
| 79 HeadlessBrowserContext* HeadlessBrowserImpl::browser_context() const { | |
| 80 DCHECK(BrowserMainThread()->BelongsToCurrentThread()); | |
| 81 DCHECK(browser_main_parts()); | |
| 82 return browser_main_parts()->browser_context(); | |
| 83 } | |
| 84 | |
| 85 HeadlessBrowserMainParts* HeadlessBrowserImpl::browser_main_parts() const { | 107 HeadlessBrowserMainParts* HeadlessBrowserImpl::browser_main_parts() const { |
| 86 DCHECK(BrowserMainThread()->BelongsToCurrentThread()); | 108 DCHECK(BrowserMainThread()->BelongsToCurrentThread()); |
| 87 return browser_main_parts_; | 109 return browser_main_parts_; |
| 88 } | 110 } |
| 89 | 111 |
| 90 void HeadlessBrowserImpl::set_browser_main_parts( | 112 void HeadlessBrowserImpl::set_browser_main_parts( |
| 91 HeadlessBrowserMainParts* browser_main_parts) { | 113 HeadlessBrowserMainParts* browser_main_parts) { |
| 92 DCHECK(!browser_main_parts_); | 114 DCHECK(!browser_main_parts_); |
| 93 browser_main_parts_ = browser_main_parts; | 115 browser_main_parts_ = browser_main_parts; |
| 94 } | 116 } |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 118 void HeadlessBrowserImpl::DestroyWebContents( | 140 void HeadlessBrowserImpl::DestroyWebContents( |
| 119 HeadlessWebContentsImpl* web_contents) { | 141 HeadlessWebContentsImpl* web_contents) { |
| 120 auto it = web_contents_.find(web_contents); | 142 auto it = web_contents_.find(web_contents); |
| 121 DCHECK(it != web_contents_.end()); | 143 DCHECK(it != web_contents_.end()); |
| 122 web_contents_.erase(it); | 144 web_contents_.erase(it); |
| 123 } | 145 } |
| 124 | 146 |
| 125 void HeadlessBrowserImpl::SetOptionsForTesting( | 147 void HeadlessBrowserImpl::SetOptionsForTesting( |
| 126 HeadlessBrowser::Options options) { | 148 HeadlessBrowser::Options options) { |
| 127 options_ = std::move(options); | 149 options_ = std::move(options); |
| 128 browser_context()->SetOptionsForTesting(&options_); | 150 browser_main_parts()->default_browser_context()->SetOptionsForTesting( |
| 151 &options_); | |
| 129 } | 152 } |
| 130 | 153 |
| 131 int HeadlessBrowserMain( | 154 int HeadlessBrowserMain( |
| 132 HeadlessBrowser::Options options, | 155 HeadlessBrowser::Options options, |
| 133 const base::Callback<void(HeadlessBrowser*)>& on_browser_start_callback) { | 156 const base::Callback<void(HeadlessBrowser*)>& on_browser_start_callback) { |
| 134 content::ContentMainParams params(nullptr); | 157 content::ContentMainParams params(nullptr); |
| 135 params.argc = options.argc; | 158 params.argc = options.argc; |
| 136 params.argv = options.argv; | 159 params.argv = options.argv; |
| 137 | 160 |
| 138 // TODO(skyostil): Implement custom message pumps. | 161 // TODO(skyostil): Implement custom message pumps. |
| 139 DCHECK(!options.message_pump); | 162 DCHECK(!options.message_pump); |
| 140 | 163 |
| 141 std::unique_ptr<HeadlessBrowserImpl> browser( | 164 std::unique_ptr<HeadlessBrowserImpl> browser( |
| 142 new HeadlessBrowserImpl(on_browser_start_callback, std::move(options))); | 165 new HeadlessBrowserImpl(on_browser_start_callback, std::move(options))); |
| 143 headless::HeadlessContentMainDelegate delegate(std::move(browser)); | 166 headless::HeadlessContentMainDelegate delegate(std::move(browser)); |
| 144 params.delegate = &delegate; | 167 params.delegate = &delegate; |
| 145 return content::ContentMain(params); | 168 return content::ContentMain(params); |
| 146 } | 169 } |
| 147 | 170 |
| 148 } // namespace headless | 171 } // namespace headless |
| OLD | NEW |