Chromium Code Reviews| 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/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
| 9 #include "base/memory/weak_ptr.h" | 9 #include "base/memory/weak_ptr.h" |
| 10 #include "base/strings/utf_string_conversions.h" | |
| 10 #include "base/trace_event/trace_event.h" | 11 #include "base/trace_event/trace_event.h" |
| 11 #include "content/public/browser/devtools_agent_host.h" | 12 #include "content/public/browser/devtools_agent_host.h" |
| 12 #include "content/public/browser/navigation_handle.h" | 13 #include "content/public/browser/navigation_handle.h" |
| 13 #include "content/public/browser/render_frame_host.h" | 14 #include "content/public/browser/render_frame_host.h" |
| 14 #include "content/public/browser/render_process_host.h" | 15 #include "content/public/browser/render_process_host.h" |
| 15 #include "content/public/browser/render_view_host.h" | 16 #include "content/public/browser/render_view_host.h" |
| 16 #include "content/public/browser/render_widget_host_view.h" | 17 #include "content/public/browser/render_widget_host_view.h" |
| 17 #include "content/public/browser/web_contents.h" | 18 #include "content/public/browser/web_contents.h" |
| 18 #include "content/public/browser/web_contents_delegate.h" | 19 #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" | 20 #include "content/public/common/bindings_policy.h" |
| 21 #include "content/public/common/service_registry.h" | 21 #include "content/public/common/service_registry.h" |
| 22 #include "content/public/renderer/render_frame.h" | 22 #include "content/public/renderer/render_frame.h" |
| 23 #include "headless/lib/browser/headless_browser_context_impl.h" | 23 #include "headless/lib/browser/headless_browser_context_impl.h" |
| 24 #include "headless/lib/browser/headless_browser_impl.h" | 24 #include "headless/lib/browser/headless_browser_impl.h" |
| 25 #include "headless/lib/browser/headless_browser_main_parts.h" | 25 #include "headless/lib/browser/headless_browser_main_parts.h" |
| 26 #include "headless/lib/browser/headless_devtools_client_impl.h" | 26 #include "headless/lib/browser/headless_devtools_client_impl.h" |
| 27 #include "ui/aura/window.h" | 27 #include "ui/aura/window.h" |
| 28 | 28 |
| 29 namespace headless { | 29 namespace headless { |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 72 HeadlessBrowserImpl* browser) { | 72 HeadlessBrowserImpl* browser) { |
| 73 content::BrowserContext* context = | 73 content::BrowserContext* context = |
| 74 HeadlessBrowserContextImpl::From(builder->browser_context_); | 74 HeadlessBrowserContextImpl::From(builder->browser_context_); |
| 75 content::WebContents::CreateParams create_params(context, nullptr); | 75 content::WebContents::CreateParams create_params(context, nullptr); |
| 76 create_params.initial_size = builder->window_size_; | 76 create_params.initial_size = builder->window_size_; |
| 77 | 77 |
| 78 std::unique_ptr<HeadlessWebContentsImpl> headless_web_contents = | 78 std::unique_ptr<HeadlessWebContentsImpl> headless_web_contents = |
| 79 base::WrapUnique(new HeadlessWebContentsImpl( | 79 base::WrapUnique(new HeadlessWebContentsImpl( |
| 80 content::WebContents::Create(create_params), browser)); | 80 content::WebContents::Create(create_params), browser)); |
| 81 | 81 |
| 82 headless_web_contents->InstallMojoServices( | |
| 83 std::move(builder->mojo_services_)); | |
| 84 headless_web_contents->InstallMojoBindings( | |
| 85 std::move(builder->mojo_bindings_)); | |
| 82 headless_web_contents->InitializeScreen(parent_window, builder->window_size_); | 86 headless_web_contents->InitializeScreen(parent_window, builder->window_size_); |
| 83 if (!headless_web_contents->OpenURL(builder->initial_url_)) | 87 if (!headless_web_contents->OpenURL(builder->initial_url_)) |
| 84 return nullptr; | 88 return nullptr; |
| 85 return headless_web_contents; | 89 return headless_web_contents; |
| 86 } | 90 } |
| 87 | 91 |
| 88 // static | 92 // static |
| 89 std::unique_ptr<HeadlessWebContentsImpl> | 93 std::unique_ptr<HeadlessWebContentsImpl> |
| 90 HeadlessWebContentsImpl::CreateFromWebContents( | 94 HeadlessWebContentsImpl::CreateFromWebContents( |
| 91 content::WebContents* web_contents, | 95 content::WebContents* web_contents, |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 103 parent_window->AddChild(contents); | 107 parent_window->AddChild(contents); |
| 104 contents->Show(); | 108 contents->Show(); |
| 105 | 109 |
| 106 contents->SetBounds(gfx::Rect(initial_size)); | 110 contents->SetBounds(gfx::Rect(initial_size)); |
| 107 content::RenderWidgetHostView* host_view = | 111 content::RenderWidgetHostView* host_view = |
| 108 web_contents_->GetRenderWidgetHostView(); | 112 web_contents_->GetRenderWidgetHostView(); |
| 109 if (host_view) | 113 if (host_view) |
| 110 host_view->SetSize(initial_size); | 114 host_view->SetSize(initial_size); |
| 111 } | 115 } |
| 112 | 116 |
| 117 void HeadlessWebContentsImpl::InstallMojoServices( | |
| 118 std::list<MojoService> mojo_services) { | |
| 119 if (mojo_services.empty()) | |
| 120 return; | |
| 121 | |
| 122 content::ServiceRegistry* service_registry = | |
| 123 web_contents_->GetRenderViewHost()->GetMainFrame()->GetServiceRegistry(); | |
| 124 | |
| 125 for (const MojoService& service : mojo_services) { | |
| 126 service_registry->AddService(service.service_name, | |
| 127 std::move(service.service_factory), | |
| 128 browser_->BrowserMainThread()); | |
| 129 } | |
| 130 } | |
| 131 | |
| 132 void HeadlessWebContentsImpl::InstallMojoBindings( | |
| 133 std::list<MojoBindings> mojo_bindings) { | |
| 134 mojo_bindings_ = std::move(mojo_bindings); | |
| 135 | |
| 136 if (mojo_bindings_.empty()) | |
| 137 return; | |
| 138 | |
| 139 web_contents_->GetRenderViewHost()->AllowBindings( | |
| 140 content::BINDINGS_POLICY_MOJO); | |
| 141 } | |
| 142 | |
| 113 HeadlessWebContentsImpl::HeadlessWebContentsImpl( | 143 HeadlessWebContentsImpl::HeadlessWebContentsImpl( |
| 114 content::WebContents* web_contents, | 144 content::WebContents* web_contents, |
| 115 HeadlessBrowserImpl* browser) | 145 HeadlessBrowserImpl* browser) |
| 116 : web_contents_delegate_(new HeadlessWebContentsImpl::Delegate(browser)), | 146 : content::WebContentsObserver(web_contents), |
| 147 web_contents_delegate_(new HeadlessWebContentsImpl::Delegate(browser)), | |
| 117 web_contents_(web_contents), | 148 web_contents_(web_contents), |
| 118 browser_(browser) { | 149 browser_(browser) { |
| 119 web_contents_->SetDelegate(web_contents_delegate_.get()); | 150 web_contents_->SetDelegate(web_contents_delegate_.get()); |
| 120 } | 151 } |
| 121 | 152 |
| 122 HeadlessWebContentsImpl::~HeadlessWebContentsImpl() { | 153 HeadlessWebContentsImpl::~HeadlessWebContentsImpl() { |
| 123 web_contents_->Close(); | 154 web_contents_->Close(); |
| 124 } | 155 } |
| 125 | 156 |
| 157 void HeadlessWebContentsImpl::DocumentOnLoadCompletedInMainFrame() { | |
| 158 content::RenderFrameHost* render_frame_host = web_contents_->GetMainFrame(); | |
|
Sami
2016/06/20 16:58:16
Could you add a short comment saying why we need t
alex clarke (OOO till 29th)
2016/06/20 23:19:13
Done.
| |
| 159 std::string module_names; | |
| 160 bool first_binding = true; | |
| 161 for (const MojoBindings& binding : mojo_bindings_) { | |
| 162 render_frame_host->ExecuteJavaScriptForTests( | |
| 163 base::UTF8ToUTF16(binding.js_bindings)); | |
| 164 if (first_binding) { | |
| 165 module_names = binding.mojom_name; | |
|
Sami
2016/06/20 16:58:16
We should probably use base::GetQuotedJSONString()
alex clarke (OOO till 29th)
2016/06/20 23:19:13
Why would that improve safety? These values come
Sami
2016/06/21 10:22:26
Sure, but they might be reading them from somewher
| |
| 166 } else { | |
| 167 module_names = module_names + "', '" + binding.mojom_name; | |
|
Sami
2016/06/20 16:58:16
nit: base::JoinString might be a little neater.
alex clarke (OOO till 29th)
2016/06/20 23:19:13
I ended up using json writer.
| |
| 168 } | |
| 169 first_binding = false; | |
| 170 } | |
| 171 // Note this function lets us run JS at anytime. | |
|
Sami
2016/06/20 16:58:16
nit: as opposed to some other function? Maybe ment
alex clarke (OOO till 29th)
2016/06/20 23:19:13
Done.
| |
| 172 render_frame_host->ExecuteJavaScriptForTests(base::UTF8ToUTF16( | |
| 173 "mojo.resolvePromisesForServices(['" + module_names + "']);")); | |
| 174 } | |
| 175 | |
| 126 bool HeadlessWebContentsImpl::OpenURL(const GURL& url) { | 176 bool HeadlessWebContentsImpl::OpenURL(const GURL& url) { |
| 127 if (!url.is_valid()) | 177 if (!url.is_valid()) |
| 128 return false; | 178 return false; |
| 129 content::NavigationController::LoadURLParams params(url); | 179 content::NavigationController::LoadURLParams params(url); |
| 130 params.transition_type = ui::PageTransitionFromInt( | 180 params.transition_type = ui::PageTransitionFromInt( |
| 131 ui::PAGE_TRANSITION_TYPED | ui::PAGE_TRANSITION_FROM_ADDRESS_BAR); | 181 ui::PAGE_TRANSITION_TYPED | ui::PAGE_TRANSITION_FROM_ADDRESS_BAR); |
| 132 web_contents_->GetController().LoadURLWithParams(params); | 182 web_contents_->GetController().LoadURLWithParams(params); |
| 133 web_contents_->Focus(); | 183 web_contents_->Focus(); |
| 134 return true; | 184 return true; |
| 135 } | 185 } |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 189 window_size_ = size; | 239 window_size_ = size; |
| 190 return *this; | 240 return *this; |
| 191 } | 241 } |
| 192 | 242 |
| 193 HeadlessWebContents::Builder& HeadlessWebContents::Builder::SetBrowserContext( | 243 HeadlessWebContents::Builder& HeadlessWebContents::Builder::SetBrowserContext( |
| 194 HeadlessBrowserContext* browser_context) { | 244 HeadlessBrowserContext* browser_context) { |
| 195 browser_context_ = browser_context; | 245 browser_context_ = browser_context; |
| 196 return *this; | 246 return *this; |
| 197 } | 247 } |
| 198 | 248 |
| 249 HeadlessWebContents::Builder& HeadlessWebContents::Builder::AddMojoService( | |
| 250 const std::string& service_name, | |
| 251 const base::Callback<void(mojo::ScopedMessagePipeHandle)>& | |
| 252 service_factory) { | |
| 253 mojo_services_.emplace_back(service_name, service_factory); | |
| 254 return *this; | |
| 255 } | |
| 256 | |
| 257 HeadlessWebContents::Builder& HeadlessWebContents::Builder::AddJsMojoBindings( | |
| 258 const std::string& mojom_name, | |
| 259 const std::string& js_bindings) { | |
| 260 mojo_bindings_.emplace_back(mojom_name, js_bindings); | |
| 261 return *this; | |
| 262 } | |
| 263 | |
| 199 HeadlessWebContents* HeadlessWebContents::Builder::Build() { | 264 HeadlessWebContents* HeadlessWebContents::Builder::Build() { |
| 200 return browser_->CreateWebContents(this); | 265 return browser_->CreateWebContents(this); |
| 201 } | 266 } |
| 202 | 267 |
| 268 HeadlessWebContents::Builder::MojoService::MojoService() {} | |
| 269 | |
| 270 HeadlessWebContents::Builder::MojoService::MojoService( | |
| 271 const std::string& service_name, | |
| 272 const base::Callback<void(mojo::ScopedMessagePipeHandle)>& service_factory) | |
| 273 : service_name(service_name), service_factory(service_factory) {} | |
| 274 | |
| 275 HeadlessWebContents::Builder::MojoService::~MojoService() {} | |
| 276 | |
| 277 HeadlessWebContents::Builder::MojoBindings::MojoBindings() {} | |
| 278 | |
| 279 HeadlessWebContents::Builder::MojoBindings::MojoBindings( | |
| 280 const std::string& mojom_name, | |
| 281 const std::string& js_bindings) | |
| 282 : mojom_name(mojom_name), js_bindings(js_bindings) {} | |
| 283 | |
| 284 HeadlessWebContents::Builder::MojoBindings::~MojoBindings() {} | |
| 285 | |
| 203 } // namespace headless | 286 } // namespace headless |
| OLD | NEW |