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); | |
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. | |
Sami
2016/06/21 10:22:26
TODO to fix this?
alex clarke (OOO till 29th)
2016/06/21 10:38:37
Done.
| |
168 render_frame_host->ExecuteJavaScriptForTests( | |
169 base::UTF8ToUTF16(binding.js_bindings)); | |
170 module_list.AppendString(binding.mojom_name); | |
171 } | |
172 std::string json_module_list; | |
173 CHECK(base::JSONWriter::Write(module_list, &json_module_list)); | |
174 render_frame_host->ExecuteJavaScriptForTests(base::UTF8ToUTF16( | |
175 "mojo.resolvePromisesForServices_(" + json_module_list + ");")); | |
176 } | |
177 | |
126 bool HeadlessWebContentsImpl::OpenURL(const GURL& url) { | 178 bool HeadlessWebContentsImpl::OpenURL(const GURL& url) { |
127 if (!url.is_valid()) | 179 if (!url.is_valid()) |
128 return false; | 180 return false; |
129 content::NavigationController::LoadURLParams params(url); | 181 content::NavigationController::LoadURLParams params(url); |
130 params.transition_type = ui::PageTransitionFromInt( | 182 params.transition_type = ui::PageTransitionFromInt( |
131 ui::PAGE_TRANSITION_TYPED | ui::PAGE_TRANSITION_FROM_ADDRESS_BAR); | 183 ui::PAGE_TRANSITION_TYPED | ui::PAGE_TRANSITION_FROM_ADDRESS_BAR); |
132 web_contents_->GetController().LoadURLWithParams(params); | 184 web_contents_->GetController().LoadURLWithParams(params); |
133 web_contents_->Focus(); | 185 web_contents_->Focus(); |
134 return true; | 186 return true; |
135 } | 187 } |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
189 window_size_ = size; | 241 window_size_ = size; |
190 return *this; | 242 return *this; |
191 } | 243 } |
192 | 244 |
193 HeadlessWebContents::Builder& HeadlessWebContents::Builder::SetBrowserContext( | 245 HeadlessWebContents::Builder& HeadlessWebContents::Builder::SetBrowserContext( |
194 HeadlessBrowserContext* browser_context) { | 246 HeadlessBrowserContext* browser_context) { |
195 browser_context_ = browser_context; | 247 browser_context_ = browser_context; |
196 return *this; | 248 return *this; |
197 } | 249 } |
198 | 250 |
251 HeadlessWebContents::Builder& HeadlessWebContents::Builder::AddMojoService( | |
252 const std::string& service_name, | |
253 const base::Callback<void(mojo::ScopedMessagePipeHandle)>& | |
254 service_factory) { | |
255 mojo_services_.emplace_back(service_name, service_factory); | |
256 return *this; | |
257 } | |
258 | |
259 HeadlessWebContents::Builder& HeadlessWebContents::Builder::AddJsMojoBindings( | |
260 const std::string& mojom_name, | |
261 const std::string& js_bindings) { | |
262 mojo_bindings_.emplace_back(mojom_name, js_bindings); | |
263 return *this; | |
264 } | |
265 | |
199 HeadlessWebContents* HeadlessWebContents::Builder::Build() { | 266 HeadlessWebContents* HeadlessWebContents::Builder::Build() { |
200 return browser_->CreateWebContents(this); | 267 return browser_->CreateWebContents(this); |
201 } | 268 } |
202 | 269 |
270 HeadlessWebContents::Builder::MojoService::MojoService() {} | |
271 | |
272 HeadlessWebContents::Builder::MojoService::MojoService( | |
273 const std::string& service_name, | |
274 const base::Callback<void(mojo::ScopedMessagePipeHandle)>& service_factory) | |
275 : service_name(service_name), service_factory(service_factory) {} | |
276 | |
277 HeadlessWebContents::Builder::MojoService::~MojoService() {} | |
278 | |
279 HeadlessWebContents::Builder::MojoBindings::MojoBindings() {} | |
280 | |
281 HeadlessWebContents::Builder::MojoBindings::MojoBindings( | |
282 const std::string& mojom_name, | |
283 const std::string& js_bindings) | |
284 : mojom_name(mojom_name), js_bindings(js_bindings) {} | |
285 | |
286 HeadlessWebContents::Builder::MojoBindings::~MojoBindings() {} | |
287 | |
203 } // namespace headless | 288 } // namespace headless |
OLD | NEW |