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/renderer/render_frame.h" | 22 #include "content/public/renderer/render_frame.h" |
22 #include "headless/lib/browser/headless_browser_context_impl.h" | 23 #include "headless/lib/browser/headless_browser_context_impl.h" |
23 #include "headless/lib/browser/headless_browser_impl.h" | 24 #include "headless/lib/browser/headless_browser_impl.h" |
24 #include "headless/lib/browser/headless_browser_main_parts.h" | 25 #include "headless/lib/browser/headless_browser_main_parts.h" |
25 #include "headless/lib/browser/headless_devtools_client_impl.h" | 26 #include "headless/lib/browser/headless_devtools_client_impl.h" |
| 27 #include "services/shell/public/cpp/interface_registry.h" |
26 #include "ui/aura/window.h" | 28 #include "ui/aura/window.h" |
27 | 29 |
28 namespace headless { | 30 namespace headless { |
29 | 31 |
30 // static | 32 // static |
31 HeadlessWebContentsImpl* HeadlessWebContentsImpl::From( | 33 HeadlessWebContentsImpl* HeadlessWebContentsImpl::From( |
32 HeadlessWebContents* web_contents) { | 34 HeadlessWebContents* web_contents) { |
33 // This downcast is safe because there is only one implementation of | 35 // This downcast is safe because there is only one implementation of |
34 // HeadlessWebContents. | 36 // HeadlessWebContents. |
35 return static_cast<HeadlessWebContentsImpl*>(web_contents); | 37 return static_cast<HeadlessWebContentsImpl*>(web_contents); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 HeadlessBrowserImpl* browser) { | 81 HeadlessBrowserImpl* browser) { |
80 content::BrowserContext* context = | 82 content::BrowserContext* context = |
81 HeadlessBrowserContextImpl::From(builder->browser_context_); | 83 HeadlessBrowserContextImpl::From(builder->browser_context_); |
82 content::WebContents::CreateParams create_params(context, nullptr); | 84 content::WebContents::CreateParams create_params(context, nullptr); |
83 create_params.initial_size = builder->window_size_; | 85 create_params.initial_size = builder->window_size_; |
84 | 86 |
85 std::unique_ptr<HeadlessWebContentsImpl> headless_web_contents = | 87 std::unique_ptr<HeadlessWebContentsImpl> headless_web_contents = |
86 base::WrapUnique(new HeadlessWebContentsImpl( | 88 base::WrapUnique(new HeadlessWebContentsImpl( |
87 content::WebContents::Create(create_params), browser)); | 89 content::WebContents::Create(create_params), browser)); |
88 | 90 |
| 91 headless_web_contents->mojo_services_ = std::move(builder->mojo_services_); |
89 headless_web_contents->InitializeScreen(parent_window, builder->window_size_); | 92 headless_web_contents->InitializeScreen(parent_window, builder->window_size_); |
90 if (!headless_web_contents->OpenURL(builder->initial_url_)) | 93 if (!headless_web_contents->OpenURL(builder->initial_url_)) |
91 return nullptr; | 94 return nullptr; |
92 return headless_web_contents; | 95 return headless_web_contents; |
93 } | 96 } |
94 | 97 |
95 // static | 98 // static |
96 std::unique_ptr<HeadlessWebContentsImpl> | 99 std::unique_ptr<HeadlessWebContentsImpl> |
97 HeadlessWebContentsImpl::CreateFromWebContents( | 100 HeadlessWebContentsImpl::CreateFromWebContents( |
98 content::WebContents* web_contents, | 101 content::WebContents* web_contents, |
(...skipping 14 matching lines...) Expand all Loading... |
113 contents->SetBounds(gfx::Rect(initial_size)); | 116 contents->SetBounds(gfx::Rect(initial_size)); |
114 content::RenderWidgetHostView* host_view = | 117 content::RenderWidgetHostView* host_view = |
115 web_contents_->GetRenderWidgetHostView(); | 118 web_contents_->GetRenderWidgetHostView(); |
116 if (host_view) | 119 if (host_view) |
117 host_view->SetSize(initial_size); | 120 host_view->SetSize(initial_size); |
118 } | 121 } |
119 | 122 |
120 HeadlessWebContentsImpl::HeadlessWebContentsImpl( | 123 HeadlessWebContentsImpl::HeadlessWebContentsImpl( |
121 content::WebContents* web_contents, | 124 content::WebContents* web_contents, |
122 HeadlessBrowserImpl* browser) | 125 HeadlessBrowserImpl* browser) |
123 : web_contents_delegate_(new HeadlessWebContentsImpl::Delegate(browser)), | 126 : content::WebContentsObserver(web_contents), |
| 127 web_contents_delegate_(new HeadlessWebContentsImpl::Delegate(browser)), |
124 web_contents_(web_contents), | 128 web_contents_(web_contents), |
125 agent_host_(content::DevToolsAgentHost::GetOrCreateFor(web_contents)), | 129 agent_host_(content::DevToolsAgentHost::GetOrCreateFor(web_contents)), |
126 browser_(browser) { | 130 browser_(browser) { |
127 web_contents_->SetDelegate(web_contents_delegate_.get()); | 131 web_contents_->SetDelegate(web_contents_delegate_.get()); |
128 } | 132 } |
129 | 133 |
130 HeadlessWebContentsImpl::~HeadlessWebContentsImpl() { | 134 HeadlessWebContentsImpl::~HeadlessWebContentsImpl() { |
131 web_contents_->Close(); | 135 web_contents_->Close(); |
132 } | 136 } |
133 | 137 |
| 138 void HeadlessWebContentsImpl::RenderFrameCreated( |
| 139 content::RenderFrameHost* render_frame_host) { |
| 140 if (!mojo_services_.empty()) { |
| 141 render_frame_host->GetRenderViewHost()->AllowBindings( |
| 142 content::BINDINGS_POLICY_HEADLESS); |
| 143 } |
| 144 |
| 145 shell::InterfaceRegistry* interface_registry = |
| 146 render_frame_host->GetInterfaceRegistry(); |
| 147 |
| 148 for (const MojoService& service : mojo_services_) { |
| 149 interface_registry->AddInterface(service.service_name, |
| 150 service.service_factory, |
| 151 browser_->BrowserMainThread()); |
| 152 } |
| 153 } |
| 154 |
134 bool HeadlessWebContentsImpl::OpenURL(const GURL& url) { | 155 bool HeadlessWebContentsImpl::OpenURL(const GURL& url) { |
135 if (!url.is_valid()) | 156 if (!url.is_valid()) |
136 return false; | 157 return false; |
137 content::NavigationController::LoadURLParams params(url); | 158 content::NavigationController::LoadURLParams params(url); |
138 params.transition_type = ui::PageTransitionFromInt( | 159 params.transition_type = ui::PageTransitionFromInt( |
139 ui::PAGE_TRANSITION_TYPED | ui::PAGE_TRANSITION_FROM_ADDRESS_BAR); | 160 ui::PAGE_TRANSITION_TYPED | ui::PAGE_TRANSITION_FROM_ADDRESS_BAR); |
140 web_contents_->GetController().LoadURLWithParams(params); | 161 web_contents_->GetController().LoadURLWithParams(params); |
141 web_contents_->Focus(); | 162 web_contents_->Focus(); |
142 return true; | 163 return true; |
143 } | 164 } |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
199 window_size_ = size; | 220 window_size_ = size; |
200 return *this; | 221 return *this; |
201 } | 222 } |
202 | 223 |
203 HeadlessWebContents::Builder& HeadlessWebContents::Builder::SetBrowserContext( | 224 HeadlessWebContents::Builder& HeadlessWebContents::Builder::SetBrowserContext( |
204 HeadlessBrowserContext* browser_context) { | 225 HeadlessBrowserContext* browser_context) { |
205 browser_context_ = browser_context; | 226 browser_context_ = browser_context; |
206 return *this; | 227 return *this; |
207 } | 228 } |
208 | 229 |
| 230 HeadlessWebContents::Builder& HeadlessWebContents::Builder::AddMojoService( |
| 231 const std::string& service_name, |
| 232 const base::Callback<void(mojo::ScopedMessagePipeHandle)>& |
| 233 service_factory) { |
| 234 mojo_services_.emplace_back(service_name, service_factory); |
| 235 return *this; |
| 236 } |
| 237 |
209 HeadlessWebContents* HeadlessWebContents::Builder::Build() { | 238 HeadlessWebContents* HeadlessWebContents::Builder::Build() { |
210 return browser_->CreateWebContents(this); | 239 return browser_->CreateWebContents(this); |
211 } | 240 } |
212 | 241 |
| 242 HeadlessWebContents::Builder::MojoService::MojoService() {} |
| 243 |
| 244 HeadlessWebContents::Builder::MojoService::MojoService( |
| 245 const std::string& service_name, |
| 246 const base::Callback<void(mojo::ScopedMessagePipeHandle)>& service_factory) |
| 247 : service_name(service_name), service_factory(service_factory) {} |
| 248 |
| 249 HeadlessWebContents::Builder::MojoService::~MojoService() {} |
| 250 |
213 } // namespace headless | 251 } // namespace headless |
OLD | NEW |