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_web_contents_impl.h" | 5 #include "headless/lib/browser/headless_web_contents_impl.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/json/json_writer.h" | |
8 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
9 #include "base/memory/weak_ptr.h" | 10 #include "base/memory/weak_ptr.h" |
11 #include "base/strings/utf_string_conversions.h" | |
10 #include "base/trace_event/trace_event.h" | 12 #include "base/trace_event/trace_event.h" |
11 #include "content/public/browser/devtools_agent_host.h" | 13 #include "content/public/browser/devtools_agent_host.h" |
12 #include "content/public/browser/navigation_handle.h" | 14 #include "content/public/browser/navigation_handle.h" |
13 #include "content/public/browser/render_frame_host.h" | 15 #include "content/public/browser/render_frame_host.h" |
14 #include "content/public/browser/render_process_host.h" | 16 #include "content/public/browser/render_process_host.h" |
15 #include "content/public/browser/render_view_host.h" | 17 #include "content/public/browser/render_view_host.h" |
16 #include "content/public/browser/render_widget_host_view.h" | 18 #include "content/public/browser/render_widget_host_view.h" |
17 #include "content/public/browser/web_contents.h" | 19 #include "content/public/browser/web_contents.h" |
18 #include "content/public/browser/web_contents_delegate.h" | 20 #include "content/public/browser/web_contents_delegate.h" |
19 #include "content/public/browser/web_contents_observer.h" | |
20 #include "content/public/common/bindings_policy.h" | 21 #include "content/public/common/bindings_policy.h" |
21 #include "content/public/common/service_registry.h" | 22 #include "content/public/common/service_registry.h" |
22 #include "content/public/renderer/render_frame.h" | 23 #include "content/public/renderer/render_frame.h" |
23 #include "headless/lib/browser/headless_browser_context_impl.h" | 24 #include "headless/lib/browser/headless_browser_context_impl.h" |
24 #include "headless/lib/browser/headless_browser_impl.h" | 25 #include "headless/lib/browser/headless_browser_impl.h" |
25 #include "headless/lib/browser/headless_browser_main_parts.h" | 26 #include "headless/lib/browser/headless_browser_main_parts.h" |
26 #include "headless/lib/browser/headless_devtools_client_impl.h" | 27 #include "headless/lib/browser/headless_devtools_client_impl.h" |
27 #include "ui/aura/window.h" | 28 #include "ui/aura/window.h" |
28 | 29 |
29 namespace headless { | 30 namespace headless { |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
72 HeadlessBrowserImpl* browser) { | 73 HeadlessBrowserImpl* browser) { |
73 content::BrowserContext* context = | 74 content::BrowserContext* context = |
74 HeadlessBrowserContextImpl::From(builder->browser_context_); | 75 HeadlessBrowserContextImpl::From(builder->browser_context_); |
75 content::WebContents::CreateParams create_params(context, nullptr); | 76 content::WebContents::CreateParams create_params(context, nullptr); |
76 create_params.initial_size = builder->window_size_; | 77 create_params.initial_size = builder->window_size_; |
77 | 78 |
78 std::unique_ptr<HeadlessWebContentsImpl> headless_web_contents = | 79 std::unique_ptr<HeadlessWebContentsImpl> headless_web_contents = |
79 base::WrapUnique(new HeadlessWebContentsImpl( | 80 base::WrapUnique(new HeadlessWebContentsImpl( |
80 content::WebContents::Create(create_params), browser)); | 81 content::WebContents::Create(create_params), browser)); |
81 | 82 |
83 headless_web_contents->InstallMojoServices( | |
84 std::move(builder->mojo_services_)); | |
85 headless_web_contents->InstallMojoBindings( | |
86 std::move(builder->mojo_bindings_)); | |
82 headless_web_contents->InitializeScreen(parent_window, builder->window_size_); | 87 headless_web_contents->InitializeScreen(parent_window, builder->window_size_); |
83 if (!headless_web_contents->OpenURL(builder->initial_url_)) | 88 if (!headless_web_contents->OpenURL(builder->initial_url_)) |
84 return nullptr; | 89 return nullptr; |
85 return headless_web_contents; | 90 return headless_web_contents; |
86 } | 91 } |
87 | 92 |
88 // static | 93 // static |
89 std::unique_ptr<HeadlessWebContentsImpl> | 94 std::unique_ptr<HeadlessWebContentsImpl> |
90 HeadlessWebContentsImpl::CreateFromWebContents( | 95 HeadlessWebContentsImpl::CreateFromWebContents( |
91 content::WebContents* web_contents, | 96 content::WebContents* web_contents, |
(...skipping 11 matching lines...) Expand all Loading... | |
103 parent_window->AddChild(contents); | 108 parent_window->AddChild(contents); |
104 contents->Show(); | 109 contents->Show(); |
105 | 110 |
106 contents->SetBounds(gfx::Rect(initial_size)); | 111 contents->SetBounds(gfx::Rect(initial_size)); |
107 content::RenderWidgetHostView* host_view = | 112 content::RenderWidgetHostView* host_view = |
108 web_contents_->GetRenderWidgetHostView(); | 113 web_contents_->GetRenderWidgetHostView(); |
109 if (host_view) | 114 if (host_view) |
110 host_view->SetSize(initial_size); | 115 host_view->SetSize(initial_size); |
111 } | 116 } |
112 | 117 |
118 void HeadlessWebContentsImpl::InstallMojoServices( | |
119 std::list<MojoService> mojo_services) { | |
120 if (mojo_services.empty()) | |
121 return; | |
122 | |
123 content::ServiceRegistry* service_registry = | |
124 web_contents_->GetRenderViewHost()->GetMainFrame()->GetServiceRegistry(); | |
125 | |
126 for (const MojoService& service : mojo_services) { | |
127 service_registry->AddService(service.service_name, | |
128 std::move(service.service_factory), | |
129 browser_->BrowserMainThread()); | |
130 } | |
131 } | |
132 | |
133 void HeadlessWebContentsImpl::InstallMojoBindings( | |
134 std::list<MojoBindings> mojo_bindings) { | |
135 mojo_bindings_ = std::move(mojo_bindings); | |
136 | |
137 if (mojo_bindings_.empty()) | |
138 return; | |
139 | |
140 web_contents_->GetRenderViewHost()->AllowBindings( | |
141 content::BINDINGS_POLICY_MOJO); | |
Charlie Reis
2016/06/21 18:23:49
This seems like a problem. If you do a cross-proc
alex clarke (OOO till 29th)
2016/06/21 19:52:44
Is there a better way of installing them?
It's ha
Charlie Reis
2016/06/21 20:05:48
WebContentsObserver::RenderFrameCreated is a bette
alex clarke (OOO till 29th)
2016/06/21 20:39:23
OK. I'll have a go at using that and write a brows
alex clarke (OOO till 29th)
2016/06/21 21:56:34
Done.
Charlie Reis
2016/06/21 23:08:31
I don't know what this means. Won't you still hit
Sam McNally
2016/06/22 01:09:11
Yes, BINDINGS_POLICY_MOJO is only suitable for lay
alex clarke (OOO till 29th)
2016/06/22 10:04:00
Right. That actually isn't a problem for us since
alex clarke (OOO till 29th)
2016/06/22 10:04:00
Agreed that BINDINGS_POLICY_MOJO doesn't quite fit
Charlie Reis
2016/06/22 19:30:59
Sorry, I don't know what you're referring to with
alex clarke (OOO till 29th)
2016/06/22 20:54:22
Happy to set up a VC if that would help. But I'll
Charlie Reis
2016/06/22 23:18:48
Maybe a VC would be useful, or I could defer to so
| |
142 } | |
143 | |
113 HeadlessWebContentsImpl::HeadlessWebContentsImpl( | 144 HeadlessWebContentsImpl::HeadlessWebContentsImpl( |
114 content::WebContents* web_contents, | 145 content::WebContents* web_contents, |
115 HeadlessBrowserImpl* browser) | 146 HeadlessBrowserImpl* browser) |
116 : web_contents_delegate_(new HeadlessWebContentsImpl::Delegate(browser)), | 147 : content::WebContentsObserver(web_contents), |
148 web_contents_delegate_(new HeadlessWebContentsImpl::Delegate(browser)), | |
117 web_contents_(web_contents), | 149 web_contents_(web_contents), |
118 browser_(browser) { | 150 browser_(browser) { |
119 web_contents_->SetDelegate(web_contents_delegate_.get()); | 151 web_contents_->SetDelegate(web_contents_delegate_.get()); |
120 } | 152 } |
121 | 153 |
122 HeadlessWebContentsImpl::~HeadlessWebContentsImpl() { | 154 HeadlessWebContentsImpl::~HeadlessWebContentsImpl() { |
123 web_contents_->Close(); | 155 web_contents_->Close(); |
124 } | 156 } |
125 | 157 |
158 void HeadlessWebContentsImpl::DocumentOnLoadCompletedInMainFrame() { | |
159 // Install mojo bindings by executing the bindings js. Note this has to be | |
160 // done here since this is the earliest callback the browser has after the | |
161 // mojo module service is created at document start. | |
162 content::RenderFrameHost* render_frame_host = web_contents_->GetMainFrame(); | |
163 base::ListValue module_list; | |
164 for (const MojoBindings& binding : mojo_bindings_) { | |
165 // Note RenderFrameHostImpl is unaware of headless chrome and we have to | |
166 // work around it's limits on when it's safe to call JS by using the | |
167 // ForTests version. | |
168 // TODO(alexclarke): Consider fixing this. | |
169 render_frame_host->ExecuteJavaScriptForTests( | |
170 base::UTF8ToUTF16(binding.js_bindings)); | |
171 module_list.AppendString(binding.mojom_name); | |
172 } | |
173 std::string json_module_list; | |
174 CHECK(base::JSONWriter::Write(module_list, &json_module_list)); | |
175 render_frame_host->ExecuteJavaScriptForTests(base::UTF8ToUTF16( | |
176 "mojo.resolvePromisesForServices_(" + json_module_list + ");")); | |
177 } | |
178 | |
126 bool HeadlessWebContentsImpl::OpenURL(const GURL& url) { | 179 bool HeadlessWebContentsImpl::OpenURL(const GURL& url) { |
127 if (!url.is_valid()) | 180 if (!url.is_valid()) |
128 return false; | 181 return false; |
129 content::NavigationController::LoadURLParams params(url); | 182 content::NavigationController::LoadURLParams params(url); |
130 params.transition_type = ui::PageTransitionFromInt( | 183 params.transition_type = ui::PageTransitionFromInt( |
131 ui::PAGE_TRANSITION_TYPED | ui::PAGE_TRANSITION_FROM_ADDRESS_BAR); | 184 ui::PAGE_TRANSITION_TYPED | ui::PAGE_TRANSITION_FROM_ADDRESS_BAR); |
132 web_contents_->GetController().LoadURLWithParams(params); | 185 web_contents_->GetController().LoadURLWithParams(params); |
133 web_contents_->Focus(); | 186 web_contents_->Focus(); |
134 return true; | 187 return true; |
135 } | 188 } |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
189 window_size_ = size; | 242 window_size_ = size; |
190 return *this; | 243 return *this; |
191 } | 244 } |
192 | 245 |
193 HeadlessWebContents::Builder& HeadlessWebContents::Builder::SetBrowserContext( | 246 HeadlessWebContents::Builder& HeadlessWebContents::Builder::SetBrowserContext( |
194 HeadlessBrowserContext* browser_context) { | 247 HeadlessBrowserContext* browser_context) { |
195 browser_context_ = browser_context; | 248 browser_context_ = browser_context; |
196 return *this; | 249 return *this; |
197 } | 250 } |
198 | 251 |
252 HeadlessWebContents::Builder& HeadlessWebContents::Builder::AddMojoService( | |
253 const std::string& service_name, | |
254 const base::Callback<void(mojo::ScopedMessagePipeHandle)>& | |
255 service_factory) { | |
256 mojo_services_.emplace_back(service_name, service_factory); | |
257 return *this; | |
258 } | |
259 | |
260 HeadlessWebContents::Builder& HeadlessWebContents::Builder::AddJsMojoBindings( | |
261 const std::string& mojom_name, | |
262 const std::string& js_bindings) { | |
263 mojo_bindings_.emplace_back(mojom_name, js_bindings); | |
264 return *this; | |
265 } | |
266 | |
199 HeadlessWebContents* HeadlessWebContents::Builder::Build() { | 267 HeadlessWebContents* HeadlessWebContents::Builder::Build() { |
200 return browser_->CreateWebContents(this); | 268 return browser_->CreateWebContents(this); |
201 } | 269 } |
202 | 270 |
271 HeadlessWebContents::Builder::MojoService::MojoService() {} | |
272 | |
273 HeadlessWebContents::Builder::MojoService::MojoService( | |
274 const std::string& service_name, | |
275 const base::Callback<void(mojo::ScopedMessagePipeHandle)>& service_factory) | |
276 : service_name(service_name), service_factory(service_factory) {} | |
277 | |
278 HeadlessWebContents::Builder::MojoService::~MojoService() {} | |
279 | |
280 HeadlessWebContents::Builder::MojoBindings::MojoBindings() {} | |
281 | |
282 HeadlessWebContents::Builder::MojoBindings::MojoBindings( | |
283 const std::string& mojom_name, | |
284 const std::string& js_bindings) | |
285 : mojom_name(mojom_name), js_bindings(js_bindings) {} | |
286 | |
287 HeadlessWebContents::Builder::MojoBindings::~MojoBindings() {} | |
288 | |
203 } // namespace headless | 289 } // namespace headless |
OLD | NEW |