OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "content/common/service_manager/service_manager_connection_impl.h" | 5 #include "content/common/service_manager/service_manager_connection_impl.h" |
6 | 6 |
7 #include <queue> | 7 #include <queue> |
8 #include <utility> | 8 #include <utility> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/callback_helpers.h" | 12 #include "base/callback_helpers.h" |
13 #include "base/lazy_instance.h" | 13 #include "base/lazy_instance.h" |
14 #include "base/macros.h" | 14 #include "base/macros.h" |
15 #include "base/memory/ptr_util.h" | 15 #include "base/memory/ptr_util.h" |
16 #include "base/message_loop/message_loop.h" | 16 #include "base/message_loop/message_loop.h" |
17 #include "base/threading/thread_checker.h" | 17 #include "base/threading/thread_checker.h" |
18 #include "base/threading/thread_task_runner_handle.h" | 18 #include "base/threading/thread_task_runner_handle.h" |
19 #include "content/common/child.mojom.h" | 19 #include "content/common/child.mojom.h" |
20 #include "content/common/service_manager/embedded_service_runner.h" | 20 #include "content/common/service_manager/embedded_service_runner.h" |
21 #include "content/public/common/connection_filter.h" | 21 #include "content/public/common/connection_filter.h" |
22 #include "content/public/common/service_names.mojom.h" | |
23 #include "mojo/public/cpp/bindings/binding_set.h" | 22 #include "mojo/public/cpp/bindings/binding_set.h" |
24 #include "mojo/public/cpp/system/message_pipe.h" | 23 #include "mojo/public/cpp/system/message_pipe.h" |
| 24 #include "services/service_manager/public/cpp/interface_registry.h" |
25 #include "services/service_manager/public/cpp/service.h" | 25 #include "services/service_manager/public/cpp/service.h" |
26 #include "services/service_manager/public/cpp/service_context.h" | 26 #include "services/service_manager/public/cpp/service_context.h" |
27 #include "services/service_manager/public/interfaces/constants.mojom.h" | 27 #include "services/service_manager/public/interfaces/constants.mojom.h" |
28 #include "services/service_manager/public/interfaces/service_factory.mojom.h" | 28 #include "services/service_manager/public/interfaces/service_factory.mojom.h" |
29 #include "services/service_manager/runner/common/client_util.h" | 29 #include "services/service_manager/runner/common/client_util.h" |
30 | 30 |
31 namespace content { | 31 namespace content { |
32 namespace { | 32 namespace { |
33 | 33 |
34 base::LazyInstance<std::unique_ptr<ServiceManagerConnection>>::Leaky | 34 base::LazyInstance<std::unique_ptr<ServiceManagerConnection>>::Leaky |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 return id; | 105 return id; |
106 } | 106 } |
107 | 107 |
108 void RemoveConnectionFilter(int filter_id) { | 108 void RemoveConnectionFilter(int filter_id) { |
109 io_task_runner_->PostTask( | 109 io_task_runner_->PostTask( |
110 FROM_HERE, | 110 FROM_HERE, |
111 base::Bind(&IOThreadContext::RemoveConnectionFilterOnIOThread, this, | 111 base::Bind(&IOThreadContext::RemoveConnectionFilterOnIOThread, this, |
112 filter_id)); | 112 filter_id)); |
113 } | 113 } |
114 | 114 |
| 115 // Safe to call any time before Start() is called. |
| 116 void SetDefaultBinderForBrowserConnection( |
| 117 const service_manager::InterfaceRegistry::Binder& binder) { |
| 118 DCHECK(!started_); |
| 119 default_browser_binder_ = base::Bind( |
| 120 &IOThreadContext::CallBinderOnTaskRunner, |
| 121 base::ThreadTaskRunnerHandle::Get(), binder); |
| 122 } |
| 123 |
115 void AddEmbeddedService(const std::string& name, const ServiceInfo& info) { | 124 void AddEmbeddedService(const std::string& name, const ServiceInfo& info) { |
116 io_task_runner_->PostTask( | 125 io_task_runner_->PostTask( |
117 FROM_HERE, base::Bind(&ServiceManagerConnectionImpl::IOThreadContext:: | 126 FROM_HERE, base::Bind(&ServiceManagerConnectionImpl::IOThreadContext:: |
118 AddEmbeddedServiceRequestHandlerOnIoThread, | 127 AddEmbeddedServiceRequestHandlerOnIoThread, |
119 this, name, info)); | 128 this, name, info)); |
120 } | 129 } |
121 | 130 |
122 void AddServiceRequestHandler(const std::string& name, | 131 void AddServiceRequestHandler(const std::string& name, |
123 const ServiceRequestHandler& handler) { | 132 const ServiceRequestHandler& handler) { |
124 io_task_runner_->PostTask( | 133 io_task_runner_->PostTask( |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 | 225 |
217 void RemoveConnectionFilterOnIOThread(int filter_id) { | 226 void RemoveConnectionFilterOnIOThread(int filter_id) { |
218 base::AutoLock lock(lock_); | 227 base::AutoLock lock(lock_); |
219 auto it = connection_filters_.find(filter_id); | 228 auto it = connection_filters_.find(filter_id); |
220 // During shutdown the connection filters might have been cleared already | 229 // During shutdown the connection filters might have been cleared already |
221 // by ClearConnectionFiltersOnIOThread() above, so this id might not exist. | 230 // by ClearConnectionFiltersOnIOThread() above, so this id might not exist. |
222 if (it != connection_filters_.end()) | 231 if (it != connection_filters_.end()) |
223 connection_filters_.erase(it); | 232 connection_filters_.erase(it); |
224 } | 233 } |
225 | 234 |
| 235 void OnBrowserConnectionLost() { |
| 236 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 237 has_browser_connection_ = false; |
| 238 } |
| 239 |
226 void AddEmbeddedServiceRequestHandlerOnIoThread(const std::string& name, | 240 void AddEmbeddedServiceRequestHandlerOnIoThread(const std::string& name, |
227 const ServiceInfo& info) { | 241 const ServiceInfo& info) { |
228 DCHECK(io_thread_checker_.CalledOnValidThread()); | 242 DCHECK(io_thread_checker_.CalledOnValidThread()); |
229 std::unique_ptr<EmbeddedServiceRunner> service( | 243 std::unique_ptr<EmbeddedServiceRunner> service( |
230 new EmbeddedServiceRunner(name, info)); | 244 new EmbeddedServiceRunner(name, info)); |
231 AddServiceRequestHandlerOnIoThread( | 245 AddServiceRequestHandlerOnIoThread( |
232 name, base::Bind(&EmbeddedServiceRunner::BindServiceRequest, | 246 name, base::Bind(&EmbeddedServiceRunner::BindServiceRequest, |
233 base::Unretained(service.get()))); | 247 base::Unretained(service.get()))); |
234 auto result = | 248 auto result = |
235 embedded_services_.insert(std::make_pair(name, std::move(service))); | 249 embedded_services_.insert(std::make_pair(name, std::move(service))); |
(...skipping 19 matching lines...) Expand all Loading... |
255 InitializeCallback handler = | 269 InitializeCallback handler = |
256 base::ResetAndReturn(&local_info_available_callback_); | 270 base::ResetAndReturn(&local_info_available_callback_); |
257 callback_task_runner_->PostTask(FROM_HERE, | 271 callback_task_runner_->PostTask(FROM_HERE, |
258 base::Bind(handler, local_info_)); | 272 base::Bind(handler, local_info_)); |
259 } | 273 } |
260 | 274 |
261 void OnBindInterface(const service_manager::ServiceInfo& source_info, | 275 void OnBindInterface(const service_manager::ServiceInfo& source_info, |
262 const std::string& interface_name, | 276 const std::string& interface_name, |
263 mojo::ScopedMessagePipeHandle interface_pipe) override { | 277 mojo::ScopedMessagePipeHandle interface_pipe) override { |
264 DCHECK(io_thread_checker_.CalledOnValidThread()); | 278 DCHECK(io_thread_checker_.CalledOnValidThread()); |
265 if (source_info.identity.name() == service_manager::mojom::kServiceName && | 279 |
| 280 std::string remote_service = source_info.identity.name(); |
| 281 // Only expose the ServiceFactory interface to the Service Manager. |
| 282 if (remote_service == service_manager::mojom::kServiceName && |
266 interface_name == service_manager::mojom::ServiceFactory::Name_) { | 283 interface_name == service_manager::mojom::ServiceFactory::Name_) { |
267 factory_bindings_.AddBinding( | 284 factory_bindings_.AddBinding( |
268 this, mojo::MakeRequest<service_manager::mojom::ServiceFactory>( | 285 this, mojo::MakeRequest<service_manager::mojom::ServiceFactory>( |
269 std::move(interface_pipe))); | 286 std::move(interface_pipe))); |
270 } else if (source_info.identity.name() == mojom::kBrowserServiceName && | 287 return; |
271 interface_name == mojom::Child::Name_) { | 288 } |
272 DCHECK(!child_binding_.is_bound()); | |
273 child_binding_.Bind( | |
274 mojo::MakeRequest<mojom::Child>(std::move(interface_pipe))); | |
275 | 289 |
276 InitializeCallback handler = | 290 { |
277 base::ResetAndReturn(&browser_info_available_callback_); | |
278 callback_task_runner_->PostTask(FROM_HERE, | |
279 base::Bind(handler, source_info)); | |
280 } else { | |
281 base::AutoLock lock(lock_); | 291 base::AutoLock lock(lock_); |
282 for (auto& entry : connection_filters_) { | 292 for (auto& entry : connection_filters_) { |
283 entry.second->OnBindInterface(source_info, interface_name, | 293 entry.second->OnBindInterface(source_info, interface_name, |
284 &interface_pipe, | 294 &interface_pipe, |
285 service_context_->connector()); | 295 service_context_->connector()); |
286 // A filter may have bound the interface, claiming the pipe. | 296 // A filter may have bound the interface, claiming the pipe. |
287 if (!interface_pipe.is_valid()) | 297 if (!interface_pipe.is_valid()) |
288 return; | 298 return; |
289 } | 299 } |
290 } | 300 } |
| 301 |
| 302 if (remote_service == "content_browser") { |
| 303 if (interface_name == mojom::Child::Name_ && !has_browser_connection_) { |
| 304 has_browser_connection_ = true; |
| 305 InitializeCallback handler = |
| 306 base::ResetAndReturn(&browser_info_available_callback_); |
| 307 callback_task_runner_->PostTask(FROM_HERE, |
| 308 base::Bind(handler, source_info)); |
| 309 |
| 310 child_binding_.Bind(std::move(interface_pipe)); |
| 311 child_binding_.set_connection_error_handler( |
| 312 base::Bind(&IOThreadContext::OnBrowserConnectionLost, this)); |
| 313 } else { |
| 314 default_browser_binder_.Run(interface_name, std::move(interface_pipe)); |
| 315 } |
| 316 } |
291 } | 317 } |
292 | 318 |
| 319 bool OnServiceManagerConnectionLost() override { |
| 320 ClearConnectionFiltersOnIOThread(); |
| 321 callback_task_runner_->PostTask(FROM_HERE, stop_callback_); |
| 322 return true; |
| 323 } |
| 324 |
293 ///////////////////////////////////////////////////////////////////////////// | 325 ///////////////////////////////////////////////////////////////////////////// |
294 // service_manager::mojom::ServiceFactory: | 326 // service_manager::mojom::ServiceFactory implementation |
295 | 327 |
296 void CreateService(service_manager::mojom::ServiceRequest request, | 328 void CreateService(service_manager::mojom::ServiceRequest request, |
297 const std::string& name) override { | 329 const std::string& name) override { |
298 DCHECK(io_thread_checker_.CalledOnValidThread()); | 330 DCHECK(io_thread_checker_.CalledOnValidThread()); |
299 auto it = request_handlers_.find(name); | 331 auto it = request_handlers_.find(name); |
300 DCHECK(it != request_handlers_.end()) | 332 DCHECK(it != request_handlers_.end()) |
301 << "Can't create service " << name << ". No handler found."; | 333 << "Can't create service " << name << ". No handler found."; |
302 it->second.Run(std::move(request)); | 334 it->second.Run(std::move(request)); |
303 } | 335 } |
304 | 336 |
| 337 static void CallBinderOnTaskRunner( |
| 338 scoped_refptr<base::SequencedTaskRunner> task_runner, |
| 339 const service_manager::InterfaceRegistry::Binder& binder, |
| 340 const std::string& interface_name, |
| 341 mojo::ScopedMessagePipeHandle request_handle) { |
| 342 task_runner->PostTask(FROM_HERE, base::Bind(binder, interface_name, |
| 343 base::Passed(&request_handle))); |
| 344 } |
| 345 |
305 base::ThreadChecker io_thread_checker_; | 346 base::ThreadChecker io_thread_checker_; |
306 bool started_ = false; | 347 bool started_ = false; |
307 | 348 |
308 // Temporary state established on construction and consumed on the IO thread | 349 // Temporary state established on construction and consumed on the IO thread |
309 // once the connection is started. | 350 // once the connection is started. |
310 service_manager::mojom::ServiceRequest pending_service_request_; | 351 service_manager::mojom::ServiceRequest pending_service_request_; |
311 scoped_refptr<base::SequencedTaskRunner> io_task_runner_; | 352 scoped_refptr<base::SequencedTaskRunner> io_task_runner_; |
312 std::unique_ptr<service_manager::Connector> io_thread_connector_; | 353 std::unique_ptr<service_manager::Connector> io_thread_connector_; |
313 service_manager::mojom::ConnectorRequest pending_connector_request_; | 354 service_manager::mojom::ConnectorRequest pending_connector_request_; |
314 | 355 |
315 // TaskRunner on which to run our owner's callbacks, i.e. the ones passed to | 356 // TaskRunner on which to run our owner's callbacks, i.e. the ones passed to |
316 // Start(). | 357 // Start(). |
317 scoped_refptr<base::SequencedTaskRunner> callback_task_runner_; | 358 scoped_refptr<base::SequencedTaskRunner> callback_task_runner_; |
318 | 359 |
319 // Callback to run once Service::OnStart is invoked. | 360 // Callback to run once Service::OnStart is invoked. |
320 InitializeCallback local_info_available_callback_; | 361 InitializeCallback local_info_available_callback_; |
321 InitializeCallback browser_info_available_callback_; | 362 InitializeCallback browser_info_available_callback_; |
322 | 363 |
323 // Callback to run if the service is stopped by the service manager. | 364 // Callback to run if the service is stopped by the service manager. |
324 base::Closure stop_callback_; | 365 base::Closure stop_callback_; |
325 | 366 |
| 367 // Called once a connection has been received from the browser process & the |
| 368 // default binder (below) has been set up. |
| 369 bool has_browser_connection_ = false; |
| 370 |
326 service_manager::ServiceInfo local_info_; | 371 service_manager::ServiceInfo local_info_; |
327 | 372 |
| 373 // Default binder callback used for the browser connection's |
| 374 // InterfaceRegistry. |
| 375 // |
| 376 // TODO(rockot): Remove this once all interfaces exposed to the browser are |
| 377 // exposed via a ConnectionFilter. |
| 378 service_manager::InterfaceRegistry::Binder default_browser_binder_; |
| 379 |
328 std::unique_ptr<service_manager::ServiceContext> service_context_; | 380 std::unique_ptr<service_manager::ServiceContext> service_context_; |
329 mojo::BindingSet<service_manager::mojom::ServiceFactory> factory_bindings_; | 381 mojo::BindingSet<service_manager::mojom::ServiceFactory> factory_bindings_; |
330 int next_filter_id_ = kInvalidConnectionFilterId; | 382 int next_filter_id_ = kInvalidConnectionFilterId; |
331 | 383 |
332 // Not owned. | 384 // Not owned. |
333 MessageLoopObserver* message_loop_observer_ = nullptr; | 385 MessageLoopObserver* message_loop_observer_ = nullptr; |
334 | 386 |
335 // Guards |connection_filters_|. | 387 // Guards |connection_filters_|. |
336 base::Lock lock_; | 388 base::Lock lock_; |
337 std::map<int, std::unique_ptr<ConnectionFilter>> connection_filters_; | 389 std::map<int, std::unique_ptr<ConnectionFilter>> connection_filters_; |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
432 const service_manager::ServiceInfo& | 484 const service_manager::ServiceInfo& |
433 ServiceManagerConnectionImpl::GetBrowserInfo() const { | 485 ServiceManagerConnectionImpl::GetBrowserInfo() const { |
434 return browser_info_; | 486 return browser_info_; |
435 } | 487 } |
436 | 488 |
437 void ServiceManagerConnectionImpl::SetConnectionLostClosure( | 489 void ServiceManagerConnectionImpl::SetConnectionLostClosure( |
438 const base::Closure& closure) { | 490 const base::Closure& closure) { |
439 connection_lost_handler_ = closure; | 491 connection_lost_handler_ = closure; |
440 } | 492 } |
441 | 493 |
| 494 void ServiceManagerConnectionImpl::SetupInterfaceRequestProxies( |
| 495 service_manager::InterfaceRegistry* registry, |
| 496 service_manager::InterfaceProvider* provider) { |
| 497 // It's safe to bind |registry| as a raw pointer because the caller must |
| 498 // guarantee that it outlives |this|, and |this| is bound as a weak ptr here. |
| 499 context_->SetDefaultBinderForBrowserConnection( |
| 500 base::Bind(&ServiceManagerConnectionImpl::GetInterface, |
| 501 weak_factory_.GetWeakPtr(), registry)); |
| 502 |
| 503 // TODO(beng): remove provider parameter. |
| 504 } |
| 505 |
442 int ServiceManagerConnectionImpl::AddConnectionFilter( | 506 int ServiceManagerConnectionImpl::AddConnectionFilter( |
443 std::unique_ptr<ConnectionFilter> filter) { | 507 std::unique_ptr<ConnectionFilter> filter) { |
444 return context_->AddConnectionFilter(std::move(filter)); | 508 return context_->AddConnectionFilter(std::move(filter)); |
445 } | 509 } |
446 | 510 |
447 void ServiceManagerConnectionImpl::RemoveConnectionFilter(int filter_id) { | 511 void ServiceManagerConnectionImpl::RemoveConnectionFilter(int filter_id) { |
448 context_->RemoveConnectionFilter(filter_id); | 512 context_->RemoveConnectionFilter(filter_id); |
449 } | 513 } |
450 | 514 |
451 void ServiceManagerConnectionImpl::AddEmbeddedService(const std::string& name, | 515 void ServiceManagerConnectionImpl::AddEmbeddedService(const std::string& name, |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
490 } | 554 } |
491 | 555 |
492 void ServiceManagerConnectionImpl::GetInterface( | 556 void ServiceManagerConnectionImpl::GetInterface( |
493 service_manager::mojom::InterfaceProvider* provider, | 557 service_manager::mojom::InterfaceProvider* provider, |
494 const std::string& interface_name, | 558 const std::string& interface_name, |
495 mojo::ScopedMessagePipeHandle request_handle) { | 559 mojo::ScopedMessagePipeHandle request_handle) { |
496 provider->GetInterface(interface_name, std::move(request_handle)); | 560 provider->GetInterface(interface_name, std::move(request_handle)); |
497 } | 561 } |
498 | 562 |
499 } // namespace content | 563 } // namespace content |
OLD | NEW |