OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "headless/lib/headless_browser_impl.h" |
| 6 |
| 7 #include "base/thread_task_runner_handle.h" |
| 8 #include "content/public/app/content_main.h" |
| 9 #include "content/public/browser/browser_thread.h" |
| 10 #include "content/public/browser/web_contents.h" |
| 11 #include "headless/lib/browser/headless_browser_context.h" |
| 12 #include "headless/lib/browser/headless_browser_main_parts.h" |
| 13 #include "headless/lib/headless_content_main_delegate.h" |
| 14 #include "headless/lib/headless_web_contents_impl.h" |
| 15 #include "ui/gfx/geometry/size.h" |
| 16 |
| 17 namespace headless { |
| 18 |
| 19 HeadlessBrowserImpl::HeadlessBrowserImpl( |
| 20 const StartCallback& on_start_callback, |
| 21 const HeadlessBrowser::Options& options) |
| 22 : on_start_callback_(on_start_callback), |
| 23 options_(options), |
| 24 browser_main_parts_(nullptr) {} |
| 25 |
| 26 HeadlessBrowserImpl::~HeadlessBrowserImpl() {} |
| 27 |
| 28 scoped_ptr<HeadlessWebContents> HeadlessBrowserImpl::CreateWebContents( |
| 29 const gfx::Size& size) { |
| 30 DCHECK(BrowserMainThread()->BelongsToCurrentThread()); |
| 31 return make_scoped_ptr(new HeadlessWebContentsImpl(browser_context(), size)); |
| 32 } |
| 33 |
| 34 scoped_refptr<base::SingleThreadTaskRunner> |
| 35 HeadlessBrowserImpl::BrowserMainThread() { |
| 36 return content::BrowserThread::GetMessageLoopProxyForThread( |
| 37 content::BrowserThread::UI); |
| 38 } |
| 39 |
| 40 void HeadlessBrowserImpl::Stop() { |
| 41 DCHECK(BrowserMainThread()->BelongsToCurrentThread()); |
| 42 BrowserMainThread()->PostTask(FROM_HERE, |
| 43 base::MessageLoop::QuitWhenIdleClosure()); |
| 44 } |
| 45 |
| 46 HeadlessBrowserContext* HeadlessBrowserImpl::browser_context() { |
| 47 DCHECK(BrowserMainThread()->BelongsToCurrentThread()); |
| 48 DCHECK(browser_main_parts()); |
| 49 return browser_main_parts()->browser_context(); |
| 50 } |
| 51 |
| 52 HeadlessBrowserMainParts* HeadlessBrowserImpl::browser_main_parts() { |
| 53 DCHECK(BrowserMainThread()->BelongsToCurrentThread()); |
| 54 return browser_main_parts_; |
| 55 } |
| 56 |
| 57 void HeadlessBrowserImpl::set_browser_main_parts( |
| 58 HeadlessBrowserMainParts* browser_main_parts) { |
| 59 DCHECK(!browser_main_parts_); |
| 60 browser_main_parts_ = browser_main_parts; |
| 61 } |
| 62 |
| 63 void HeadlessBrowserImpl::RunOnStartCallback() { |
| 64 if (!on_start_callback_.is_null()) { |
| 65 on_start_callback_.Run(this); |
| 66 on_start_callback_ = StartCallback(); |
| 67 } |
| 68 } |
| 69 |
| 70 int HeadlessBrowser::Run(const Options& options, |
| 71 const StartCallback& on_browser_start_callback) { |
| 72 scoped_ptr<HeadlessBrowserImpl> browser( |
| 73 new HeadlessBrowserImpl(on_browser_start_callback, options)); |
| 74 |
| 75 // TODO(skyostil): Implement custom message pumps. |
| 76 DCHECK(!options.message_pump); |
| 77 |
| 78 headless::HeadlessContentMainDelegate delegate(std::move(browser)); |
| 79 content::ContentMainParams params(&delegate); |
| 80 params.argc = options.argc; |
| 81 params.argv = options.argv; |
| 82 return content::ContentMain(params); |
| 83 } |
| 84 |
| 85 } // namespace headless |
OLD | NEW |