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->mojo_services_ = std::move(builder->mojo_services_); | |
84 headless_web_contents->mojo_bindings_ = std::move(builder->mojo_bindings_); | |
82 headless_web_contents->InitializeScreen(parent_window, builder->window_size_); | 85 headless_web_contents->InitializeScreen(parent_window, builder->window_size_); |
83 if (!headless_web_contents->OpenURL(builder->initial_url_)) | 86 if (!headless_web_contents->OpenURL(builder->initial_url_)) |
84 return nullptr; | 87 return nullptr; |
85 return headless_web_contents; | 88 return headless_web_contents; |
86 } | 89 } |
87 | 90 |
88 // static | 91 // static |
89 std::unique_ptr<HeadlessWebContentsImpl> | 92 std::unique_ptr<HeadlessWebContentsImpl> |
90 HeadlessWebContentsImpl::CreateFromWebContents( | 93 HeadlessWebContentsImpl::CreateFromWebContents( |
91 content::WebContents* web_contents, | 94 content::WebContents* web_contents, |
(...skipping 14 matching lines...) Expand all Loading... | |
106 contents->SetBounds(gfx::Rect(initial_size)); | 109 contents->SetBounds(gfx::Rect(initial_size)); |
107 content::RenderWidgetHostView* host_view = | 110 content::RenderWidgetHostView* host_view = |
108 web_contents_->GetRenderWidgetHostView(); | 111 web_contents_->GetRenderWidgetHostView(); |
109 if (host_view) | 112 if (host_view) |
110 host_view->SetSize(initial_size); | 113 host_view->SetSize(initial_size); |
111 } | 114 } |
112 | 115 |
113 HeadlessWebContentsImpl::HeadlessWebContentsImpl( | 116 HeadlessWebContentsImpl::HeadlessWebContentsImpl( |
114 content::WebContents* web_contents, | 117 content::WebContents* web_contents, |
115 HeadlessBrowserImpl* browser) | 118 HeadlessBrowserImpl* browser) |
116 : web_contents_delegate_(new HeadlessWebContentsImpl::Delegate(browser)), | 119 : content::WebContentsObserver(web_contents), |
120 web_contents_delegate_(new HeadlessWebContentsImpl::Delegate(browser)), | |
117 web_contents_(web_contents), | 121 web_contents_(web_contents), |
118 browser_(browser) { | 122 browser_(browser) { |
119 web_contents_->SetDelegate(web_contents_delegate_.get()); | 123 web_contents_->SetDelegate(web_contents_delegate_.get()); |
120 } | 124 } |
121 | 125 |
122 HeadlessWebContentsImpl::~HeadlessWebContentsImpl() { | 126 HeadlessWebContentsImpl::~HeadlessWebContentsImpl() { |
123 web_contents_->Close(); | 127 web_contents_->Close(); |
124 } | 128 } |
125 | 129 |
130 void HeadlessWebContentsImpl::RenderFrameCreated( | |
Charlie Reis
2016/06/21 23:08:32
Yes, RenderFrameCreated should do what you want fo
| |
131 content::RenderFrameHost* render_frame_host) { | |
132 if (!mojo_bindings_.empty()) { | |
133 render_frame_host->GetRenderViewHost()->AllowBindings( | |
134 content::BINDINGS_POLICY_MOJO); | |
135 } | |
136 | |
137 content::ServiceRegistry* service_registry = | |
138 render_frame_host->GetServiceRegistry(); | |
139 | |
140 for (const MojoService& service : mojo_services_) { | |
141 service_registry->AddService(service.service_name, service.service_factory, | |
142 browser_->BrowserMainThread()); | |
143 } | |
144 } | |
145 | |
146 void HeadlessWebContentsImpl::DocumentOnLoadCompletedInMainFrame() { | |
147 // Install mojo bindings by executing the bindings js. Note this has to be | |
148 // done here since this is the earliest callback the browser has after the | |
149 // mojo module service is created at document start. | |
150 content::RenderFrameHost* render_frame_host = web_contents_->GetMainFrame(); | |
151 base::ListValue module_list; | |
152 for (const MojoBindings& binding : mojo_bindings_) { | |
153 // Note RenderFrameHostImpl is unaware of headless chrome and we have to | |
154 // work around it's limits on when it's safe to call JS by using the | |
155 // ForTests version. | |
156 // TODO(alexclarke): Consider fixing this. | |
157 render_frame_host->ExecuteJavaScriptForTests( | |
158 base::UTF8ToUTF16(binding.js_bindings)); | |
159 module_list.AppendString(binding.mojom_name); | |
160 } | |
161 std::string json_module_list; | |
162 CHECK(base::JSONWriter::Write(module_list, &json_module_list)); | |
163 render_frame_host->ExecuteJavaScriptForTests(base::UTF8ToUTF16( | |
164 "mojo.resolvePromisesForServices_(" + json_module_list + ");")); | |
165 } | |
166 | |
126 bool HeadlessWebContentsImpl::OpenURL(const GURL& url) { | 167 bool HeadlessWebContentsImpl::OpenURL(const GURL& url) { |
127 if (!url.is_valid()) | 168 if (!url.is_valid()) |
128 return false; | 169 return false; |
129 content::NavigationController::LoadURLParams params(url); | 170 content::NavigationController::LoadURLParams params(url); |
130 params.transition_type = ui::PageTransitionFromInt( | 171 params.transition_type = ui::PageTransitionFromInt( |
131 ui::PAGE_TRANSITION_TYPED | ui::PAGE_TRANSITION_FROM_ADDRESS_BAR); | 172 ui::PAGE_TRANSITION_TYPED | ui::PAGE_TRANSITION_FROM_ADDRESS_BAR); |
132 web_contents_->GetController().LoadURLWithParams(params); | 173 web_contents_->GetController().LoadURLWithParams(params); |
133 web_contents_->Focus(); | 174 web_contents_->Focus(); |
134 return true; | 175 return true; |
135 } | 176 } |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
189 window_size_ = size; | 230 window_size_ = size; |
190 return *this; | 231 return *this; |
191 } | 232 } |
192 | 233 |
193 HeadlessWebContents::Builder& HeadlessWebContents::Builder::SetBrowserContext( | 234 HeadlessWebContents::Builder& HeadlessWebContents::Builder::SetBrowserContext( |
194 HeadlessBrowserContext* browser_context) { | 235 HeadlessBrowserContext* browser_context) { |
195 browser_context_ = browser_context; | 236 browser_context_ = browser_context; |
196 return *this; | 237 return *this; |
197 } | 238 } |
198 | 239 |
240 HeadlessWebContents::Builder& HeadlessWebContents::Builder::AddMojoService( | |
241 const std::string& service_name, | |
242 const base::Callback<void(mojo::ScopedMessagePipeHandle)>& | |
243 service_factory) { | |
244 mojo_services_.emplace_back(service_name, service_factory); | |
245 return *this; | |
246 } | |
247 | |
248 HeadlessWebContents::Builder& HeadlessWebContents::Builder::AddJsMojoBindings( | |
249 const std::string& mojom_name, | |
250 const std::string& js_bindings) { | |
251 mojo_bindings_.emplace_back(mojom_name, js_bindings); | |
252 return *this; | |
253 } | |
254 | |
199 HeadlessWebContents* HeadlessWebContents::Builder::Build() { | 255 HeadlessWebContents* HeadlessWebContents::Builder::Build() { |
200 return browser_->CreateWebContents(this); | 256 return browser_->CreateWebContents(this); |
201 } | 257 } |
202 | 258 |
259 HeadlessWebContents::Builder::MojoService::MojoService() {} | |
260 | |
261 HeadlessWebContents::Builder::MojoService::MojoService( | |
262 const std::string& service_name, | |
263 const base::Callback<void(mojo::ScopedMessagePipeHandle)>& service_factory) | |
264 : service_name(service_name), service_factory(service_factory) {} | |
265 | |
266 HeadlessWebContents::Builder::MojoService::~MojoService() {} | |
267 | |
268 HeadlessWebContents::Builder::MojoBindings::MojoBindings() {} | |
269 | |
270 HeadlessWebContents::Builder::MojoBindings::MojoBindings( | |
271 const std::string& mojom_name, | |
272 const std::string& js_bindings) | |
273 : mojom_name(mojom_name), js_bindings(js_bindings) {} | |
274 | |
275 HeadlessWebContents::Builder::MojoBindings::~MojoBindings() {} | |
276 | |
203 } // namespace headless | 277 } // namespace headless |
OLD | NEW |