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 |
| 47 HeadlessWebContents* HeadlessBrowserImpl::CreateWebContents( |
| 48 HeadlessWebContents::Builder* builder) { |
| 49 DCHECK(BrowserMainThread()->BelongsToCurrentThread()); |
| 50 std::unique_ptr<HeadlessWebContentsImpl> headless_web_contents = |
| 51 HeadlessWebContentsImpl::Create(builder, window_tree_host_->window(), |
| 52 this); |
| 53 if (!headless_web_contents) |
| 54 return nullptr; |
| 55 return RegisterWebContents(std::move(headless_web_contents)); |
| 56 } |
| 57 |
36 HeadlessWebContents* HeadlessBrowserImpl::CreateWebContents( | 58 HeadlessWebContents* HeadlessBrowserImpl::CreateWebContents( |
37 const GURL& initial_url, | 59 const GURL& initial_url, |
38 const gfx::Size& size) { | 60 const gfx::Size& size) { |
39 DCHECK(BrowserMainThread()->BelongsToCurrentThread()); | 61 return CreateWebContentsBuilder() |
40 std::unique_ptr<HeadlessWebContentsImpl> headless_web_contents = | 62 .SetInitialURL(initial_url) |
41 HeadlessWebContentsImpl::Create(browser_context(), | 63 .SetWindowSize(size) |
42 window_tree_host_->window(), size, this); | 64 .Build(); |
43 | |
44 if (!headless_web_contents->OpenURL(initial_url)) | |
45 return nullptr; | |
46 | |
47 return RegisterWebContents(std::move(headless_web_contents)); | |
48 } | 65 } |
49 | 66 |
50 scoped_refptr<base::SingleThreadTaskRunner> | 67 scoped_refptr<base::SingleThreadTaskRunner> |
51 HeadlessBrowserImpl::BrowserMainThread() const { | 68 HeadlessBrowserImpl::BrowserMainThread() const { |
52 return content::BrowserThread::GetMessageLoopProxyForThread( | 69 return content::BrowserThread::GetMessageLoopProxyForThread( |
53 content::BrowserThread::UI); | 70 content::BrowserThread::UI); |
54 } | 71 } |
55 | 72 |
56 scoped_refptr<base::SingleThreadTaskRunner> | 73 scoped_refptr<base::SingleThreadTaskRunner> |
57 HeadlessBrowserImpl::BrowserFileThread() const { | 74 HeadlessBrowserImpl::BrowserFileThread() const { |
(...skipping 11 matching lines...) Expand all Loading... |
69 std::vector<HeadlessWebContents*> result; | 86 std::vector<HeadlessWebContents*> result; |
70 result.reserve(web_contents_.size()); | 87 result.reserve(web_contents_.size()); |
71 | 88 |
72 for (const auto& web_contents_pair : web_contents_) { | 89 for (const auto& web_contents_pair : web_contents_) { |
73 result.push_back(web_contents_pair.first); | 90 result.push_back(web_contents_pair.first); |
74 } | 91 } |
75 | 92 |
76 return result; | 93 return result; |
77 } | 94 } |
78 | 95 |
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 { | 96 HeadlessBrowserMainParts* HeadlessBrowserImpl::browser_main_parts() const { |
86 DCHECK(BrowserMainThread()->BelongsToCurrentThread()); | 97 DCHECK(BrowserMainThread()->BelongsToCurrentThread()); |
87 return browser_main_parts_; | 98 return browser_main_parts_; |
88 } | 99 } |
89 | 100 |
90 void HeadlessBrowserImpl::set_browser_main_parts( | 101 void HeadlessBrowserImpl::set_browser_main_parts( |
91 HeadlessBrowserMainParts* browser_main_parts) { | 102 HeadlessBrowserMainParts* browser_main_parts) { |
92 DCHECK(!browser_main_parts_); | 103 DCHECK(!browser_main_parts_); |
93 browser_main_parts_ = browser_main_parts; | 104 browser_main_parts_ = browser_main_parts; |
94 } | 105 } |
95 | 106 |
96 void HeadlessBrowserImpl::RunOnStartCallback() { | 107 void HeadlessBrowserImpl::RunOnStartCallback() { |
97 DCHECK(aura::Env::GetInstance()); | 108 DCHECK(aura::Env::GetInstance()); |
98 // TODO(eseckler): allow configuration of window (viewport) size by embedder. | 109 // TODO(eseckler): allow configuration of window (viewport) size by embedder. |
99 const gfx::Size kDefaultSize(800, 600); | 110 const gfx::Size kDefaultSize(800, 600); |
100 window_tree_host_.reset( | 111 window_tree_host_.reset( |
101 aura::WindowTreeHost::Create(gfx::Rect(kDefaultSize))); | 112 aura::WindowTreeHost::Create(gfx::Rect(kDefaultSize))); |
102 window_tree_host_->InitHost(); | 113 window_tree_host_->InitHost(); |
103 | 114 |
104 window_tree_client_.reset( | 115 window_tree_client_.reset( |
105 new HeadlessWindowTreeClient(window_tree_host_->window())); | 116 new HeadlessWindowTreeClient(window_tree_host_->window())); |
106 | 117 |
107 on_start_callback_.Run(this); | 118 on_start_callback_.Run(this); |
108 on_start_callback_ = base::Callback<void(HeadlessBrowser*)>(); | 119 on_start_callback_ = base::Callback<void(HeadlessBrowser*)>(); |
109 } | 120 } |
110 | 121 |
111 HeadlessWebContentsImpl* HeadlessBrowserImpl::RegisterWebContents( | 122 HeadlessWebContentsImpl* HeadlessBrowserImpl::RegisterWebContents( |
112 std::unique_ptr<HeadlessWebContentsImpl> web_contents) { | 123 std::unique_ptr<HeadlessWebContentsImpl> web_contents) { |
| 124 DCHECK(web_contents); |
113 HeadlessWebContentsImpl* unowned_web_contents = web_contents.get(); | 125 HeadlessWebContentsImpl* unowned_web_contents = web_contents.get(); |
114 web_contents_[unowned_web_contents] = std::move(web_contents); | 126 web_contents_[unowned_web_contents] = std::move(web_contents); |
115 return unowned_web_contents; | 127 return unowned_web_contents; |
116 } | 128 } |
117 | 129 |
118 void HeadlessBrowserImpl::DestroyWebContents( | 130 void HeadlessBrowserImpl::DestroyWebContents( |
119 HeadlessWebContentsImpl* web_contents) { | 131 HeadlessWebContentsImpl* web_contents) { |
120 auto it = web_contents_.find(web_contents); | 132 auto it = web_contents_.find(web_contents); |
121 DCHECK(it != web_contents_.end()); | 133 DCHECK(it != web_contents_.end()); |
122 web_contents_.erase(it); | 134 web_contents_.erase(it); |
123 } | 135 } |
124 | 136 |
125 void HeadlessBrowserImpl::SetOptionsForTesting( | 137 void HeadlessBrowserImpl::SetOptionsForTesting( |
126 HeadlessBrowser::Options options) { | 138 HeadlessBrowser::Options options) { |
127 options_ = std::move(options); | 139 options_ = std::move(options); |
128 browser_context()->SetOptionsForTesting(&options_); | 140 browser_main_parts()->default_browser_context()->SetOptionsForTesting( |
| 141 &options_); |
129 } | 142 } |
130 | 143 |
131 int HeadlessBrowserMain( | 144 int HeadlessBrowserMain( |
132 HeadlessBrowser::Options options, | 145 HeadlessBrowser::Options options, |
133 const base::Callback<void(HeadlessBrowser*)>& on_browser_start_callback) { | 146 const base::Callback<void(HeadlessBrowser*)>& on_browser_start_callback) { |
134 content::ContentMainParams params(nullptr); | 147 content::ContentMainParams params(nullptr); |
135 params.argc = options.argc; | 148 params.argc = options.argc; |
136 params.argv = options.argv; | 149 params.argv = options.argv; |
137 | 150 |
138 // TODO(skyostil): Implement custom message pumps. | 151 // TODO(skyostil): Implement custom message pumps. |
139 DCHECK(!options.message_pump); | 152 DCHECK(!options.message_pump); |
140 | 153 |
141 std::unique_ptr<HeadlessBrowserImpl> browser( | 154 std::unique_ptr<HeadlessBrowserImpl> browser( |
142 new HeadlessBrowserImpl(on_browser_start_callback, std::move(options))); | 155 new HeadlessBrowserImpl(on_browser_start_callback, std::move(options))); |
143 headless::HeadlessContentMainDelegate delegate(std::move(browser)); | 156 headless::HeadlessContentMainDelegate delegate(std::move(browser)); |
144 params.delegate = &delegate; | 157 params.delegate = &delegate; |
145 return content::ContentMain(params); | 158 return content::ContentMain(params); |
146 } | 159 } |
147 | 160 |
148 } // namespace headless | 161 } // namespace headless |
OLD | NEW |