Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(278)

Side by Side Diff: content/common/service_manager/service_manager_connection_impl.cc

Issue 2766263009: Convert content ConnectionFilter to OnBindInterface (Closed)
Patch Set: . Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/service_manager/embedded_service_runner.h" 20 #include "content/common/service_manager/embedded_service_runner.h"
20 #include "content/public/common/connection_filter.h" 21 #include "content/public/common/connection_filter.h"
21 #include "mojo/public/cpp/bindings/binding_set.h" 22 #include "mojo/public/cpp/bindings/binding_set.h"
22 #include "mojo/public/cpp/system/message_pipe.h" 23 #include "mojo/public/cpp/system/message_pipe.h"
23 #include "services/service_manager/public/cpp/interface_registry.h" 24 #include "services/service_manager/public/cpp/interface_registry.h"
24 #include "services/service_manager/public/cpp/service.h" 25 #include "services/service_manager/public/cpp/service.h"
25 #include "services/service_manager/public/cpp/service_context.h" 26 #include "services/service_manager/public/cpp/service_context.h"
26 #include "services/service_manager/public/interfaces/constants.mojom.h" 27 #include "services/service_manager/public/interfaces/constants.mojom.h"
27 #include "services/service_manager/public/interfaces/service_factory.mojom.h" 28 #include "services/service_manager/public/interfaces/service_factory.mojom.h"
28 #include "services/service_manager/runner/common/client_util.h" 29 #include "services/service_manager/runner/common/client_util.h"
29 30
30 namespace content { 31 namespace content {
31 namespace { 32 namespace {
32 33
33 base::LazyInstance<std::unique_ptr<ServiceManagerConnection>>::Leaky 34 base::LazyInstance<std::unique_ptr<ServiceManagerConnection>>::Leaky
34 g_connection_for_process = LAZY_INSTANCE_INITIALIZER; 35 g_connection_for_process = LAZY_INSTANCE_INITIALIZER;
35 36
36 ServiceManagerConnection::Factory* service_manager_connection_factory = nullptr; 37 ServiceManagerConnection::Factory* service_manager_connection_factory = nullptr;
37 38
38 } // namespace 39 } // namespace
39 40
40 // A ref-counted object which owns the IO thread state of a 41 // A ref-counted object which owns the IO thread state of a
41 // ServiceManagerConnectionImpl. This includes Service and ServiceFactory 42 // ServiceManagerConnectionImpl. This includes Service and ServiceFactory
42 // bindings. 43 // bindings.
43 class ServiceManagerConnectionImpl::IOThreadContext 44 class ServiceManagerConnectionImpl::IOThreadContext
44 : public base::RefCountedThreadSafe<IOThreadContext>, 45 : public base::RefCountedThreadSafe<IOThreadContext>,
45 public service_manager::Service, 46 public service_manager::Service,
46 public service_manager::InterfaceFactory< 47 public service_manager::mojom::ServiceFactory,
47 service_manager::mojom::ServiceFactory>, 48 public mojom::Child {
48 public service_manager::mojom::ServiceFactory {
49 public: 49 public:
50 using InitializeCallback = 50 using InitializeCallback =
51 base::Callback<void(const service_manager::Identity&)>; 51 base::Callback<void(const service_manager::ServiceInfo&)>;
52 52
53 IOThreadContext( 53 IOThreadContext(
54 service_manager::mojom::ServiceRequest service_request, 54 service_manager::mojom::ServiceRequest service_request,
55 scoped_refptr<base::SequencedTaskRunner> io_task_runner, 55 scoped_refptr<base::SequencedTaskRunner> io_task_runner,
56 std::unique_ptr<service_manager::Connector> io_thread_connector, 56 std::unique_ptr<service_manager::Connector> io_thread_connector,
57 service_manager::mojom::ConnectorRequest connector_request) 57 service_manager::mojom::ConnectorRequest connector_request)
58 : pending_service_request_(std::move(service_request)), 58 : pending_service_request_(std::move(service_request)),
59 io_task_runner_(io_task_runner), 59 io_task_runner_(io_task_runner),
60 io_thread_connector_(std::move(io_thread_connector)), 60 io_thread_connector_(std::move(io_thread_connector)),
61 pending_connector_request_(std::move(connector_request)), 61 pending_connector_request_(std::move(connector_request)),
62 child_binding_(this),
62 weak_factory_(this) { 63 weak_factory_(this) {
63 // This will be reattached by any of the IO thread functions on first call. 64 // This will be reattached by any of the IO thread functions on first call.
64 io_thread_checker_.DetachFromThread(); 65 io_thread_checker_.DetachFromThread();
65 } 66 }
66 67
67 // Safe to call from any thread. 68 // Safe to call from any thread.
68 void Start( 69 void Start(const InitializeCallback& local_info_available_callback,
69 const InitializeCallback& initialize_callback, 70 const InitializeCallback& browser_info_available_callback,
70 const ServiceManagerConnection::OnConnectHandler& on_connect_callback, 71 const base::Closure& stop_callback) {
71 const base::Closure& stop_callback) {
72 DCHECK(!started_); 72 DCHECK(!started_);
73 73
74 started_ = true; 74 started_ = true;
75 callback_task_runner_ = base::ThreadTaskRunnerHandle::Get(); 75 callback_task_runner_ = base::ThreadTaskRunnerHandle::Get();
76 initialize_handler_ = initialize_callback; 76 local_info_available_callback_ = local_info_available_callback;
77 on_connect_callback_ = on_connect_callback; 77 browser_info_available_callback_ = browser_info_available_callback;
78 stop_callback_ = stop_callback; 78 stop_callback_ = stop_callback;
79 io_task_runner_->PostTask( 79 io_task_runner_->PostTask(
80 FROM_HERE, base::Bind(&IOThreadContext::StartOnIOThread, this)); 80 FROM_HERE, base::Bind(&IOThreadContext::StartOnIOThread, this));
81 } 81 }
82 82
83 // Safe to call from whichever thread called Start() (or may have called 83 // Safe to call from whichever thread called Start() (or may have called
84 // Start()). Must be called before IO thread shutdown. 84 // Start()). Must be called before IO thread shutdown.
85 void ShutDown() { 85 void ShutDown() {
86 if (!started_) 86 if (!started_)
87 return; 87 return;
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after
256 DCHECK(io_thread_checker_.CalledOnValidThread()); 256 DCHECK(io_thread_checker_.CalledOnValidThread());
257 auto result = request_handlers_.insert(std::make_pair(name, handler)); 257 auto result = request_handlers_.insert(std::make_pair(name, handler));
258 DCHECK(result.second); 258 DCHECK(result.second);
259 } 259 }
260 260
261 ///////////////////////////////////////////////////////////////////////////// 261 /////////////////////////////////////////////////////////////////////////////
262 // service_manager::Service implementation 262 // service_manager::Service implementation
263 263
264 void OnStart() override { 264 void OnStart() override {
265 DCHECK(io_thread_checker_.CalledOnValidThread()); 265 DCHECK(io_thread_checker_.CalledOnValidThread());
266 DCHECK(!initialize_handler_.is_null()); 266 DCHECK(!local_info_available_callback_.is_null());
267 local_info_ = context()->local_info(); 267 local_info_ = context()->local_info();
268 268
269 InitializeCallback handler = base::ResetAndReturn(&initialize_handler_); 269 InitializeCallback handler =
270 base::ResetAndReturn(&local_info_available_callback_);
270 callback_task_runner_->PostTask(FROM_HERE, 271 callback_task_runner_->PostTask(FROM_HERE,
271 base::Bind(handler, local_info_.identity)); 272 base::Bind(handler, local_info_));
272 } 273 }
273 274
274 bool OnConnect(const service_manager::ServiceInfo& remote_info, 275 void OnBindInterface(const service_manager::ServiceInfo& source_info,
275 service_manager::InterfaceRegistry* registry) override { 276 const std::string& interface_name,
277 mojo::ScopedMessagePipeHandle interface_pipe) override {
276 DCHECK(io_thread_checker_.CalledOnValidThread()); 278 DCHECK(io_thread_checker_.CalledOnValidThread());
277 279
278 callback_task_runner_->PostTask( 280 std::string remote_service = source_info.identity.name();
279 FROM_HERE, base::Bind(on_connect_callback_, local_info_, remote_info)); 281 // Only expose the ServiceFactory interface to the Service Manager.
280 282 if (remote_service == service_manager::mojom::kServiceName &&
281 std::string remote_service = remote_info.identity.name(); 283 interface_name == service_manager::mojom::ServiceFactory::Name_) {
282 if (remote_service == service_manager::mojom::kServiceName) { 284 factory_bindings_.AddBinding(
283 // Only expose the ServiceFactory interface to the Service Manager. 285 this, mojo::MakeRequest<service_manager::mojom::ServiceFactory>(
284 registry->AddInterface<service_manager::mojom::ServiceFactory>(this); 286 std::move(interface_pipe)));
285 return true; 287 return;
286 } 288 }
287 289
288 bool accept = false;
289 { 290 {
290 base::AutoLock lock(lock_); 291 base::AutoLock lock(lock_);
291 for (auto& entry : connection_filters_) { 292 for (auto& entry : connection_filters_) {
292 accept |= entry.second->OnConnect(remote_info.identity, registry, 293 entry.second->OnBindInterface(source_info, interface_name,
293 service_context_->connector()); 294 &interface_pipe,
295 service_context_->connector());
296 // A filter may have bound the interface, claiming the pipe.
297 if (!interface_pipe.is_valid())
298 return;
294 } 299 }
295 } 300 }
296 301
297 if (remote_service == "content_browser" && 302 if (remote_service == "content_browser") {
298 !has_browser_connection_) { 303 if (interface_name == mojom::Child::Name_ && !has_browser_connection_) {
299 has_browser_connection_ = true; 304 has_browser_connection_ = true;
300 registry->set_default_binder(default_browser_binder_); 305 InitializeCallback handler =
301 registry->AddConnectionLostClosure( 306 base::ResetAndReturn(&browser_info_available_callback_);
302 base::Bind(&IOThreadContext::OnBrowserConnectionLost, this)); 307 callback_task_runner_->PostTask(FROM_HERE,
303 return true; 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 }
304 } 316 }
305
306 // If no filters were interested, reject the connection.
307 return accept;
308 } 317 }
309 318
310 bool OnServiceManagerConnectionLost() override { 319 bool OnServiceManagerConnectionLost() override {
311 ClearConnectionFiltersOnIOThread(); 320 ClearConnectionFiltersOnIOThread();
312 callback_task_runner_->PostTask(FROM_HERE, stop_callback_); 321 callback_task_runner_->PostTask(FROM_HERE, stop_callback_);
313 return true; 322 return true;
314 } 323 }
315 324
316 ///////////////////////////////////////////////////////////////////////////// 325 /////////////////////////////////////////////////////////////////////////////
317 // service_manager::InterfaceFactory<service_manager::mojom::ServiceFactory>
318 // implementation
319
320 void Create(const service_manager::Identity& remote_identity,
321 service_manager::mojom::ServiceFactoryRequest request) override {
322 DCHECK(io_thread_checker_.CalledOnValidThread());
323 factory_bindings_.AddBinding(this, std::move(request));
324 }
325
326 /////////////////////////////////////////////////////////////////////////////
327 // service_manager::mojom::ServiceFactory implementation 326 // service_manager::mojom::ServiceFactory implementation
328 327
329 void CreateService(service_manager::mojom::ServiceRequest request, 328 void CreateService(service_manager::mojom::ServiceRequest request,
330 const std::string& name) override { 329 const std::string& name) override {
331 DCHECK(io_thread_checker_.CalledOnValidThread()); 330 DCHECK(io_thread_checker_.CalledOnValidThread());
332 auto it = request_handlers_.find(name); 331 auto it = request_handlers_.find(name);
333 DCHECK(it != request_handlers_.end()) 332 DCHECK(it != request_handlers_.end())
334 << "Can't create service " << name << ". No handler found."; 333 << "Can't create service " << name << ". No handler found.";
335 it->second.Run(std::move(request)); 334 it->second.Run(std::move(request));
336 } 335 }
(...skipping 15 matching lines...) Expand all
352 service_manager::mojom::ServiceRequest pending_service_request_; 351 service_manager::mojom::ServiceRequest pending_service_request_;
353 scoped_refptr<base::SequencedTaskRunner> io_task_runner_; 352 scoped_refptr<base::SequencedTaskRunner> io_task_runner_;
354 std::unique_ptr<service_manager::Connector> io_thread_connector_; 353 std::unique_ptr<service_manager::Connector> io_thread_connector_;
355 service_manager::mojom::ConnectorRequest pending_connector_request_; 354 service_manager::mojom::ConnectorRequest pending_connector_request_;
356 355
357 // 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
358 // Start(). 357 // Start().
359 scoped_refptr<base::SequencedTaskRunner> callback_task_runner_; 358 scoped_refptr<base::SequencedTaskRunner> callback_task_runner_;
360 359
361 // Callback to run once Service::OnStart is invoked. 360 // Callback to run once Service::OnStart is invoked.
362 InitializeCallback initialize_handler_; 361 InitializeCallback local_info_available_callback_;
363 362 InitializeCallback browser_info_available_callback_;
364 // Callback to run when a connection request is received.
365 ServiceManagerConnection::OnConnectHandler on_connect_callback_;
366 363
367 // 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.
368 base::Closure stop_callback_; 365 base::Closure stop_callback_;
369 366
370 // Called once a connection has been received from the browser process & the 367 // Called once a connection has been received from the browser process & the
371 // default binder (below) has been set up. 368 // default binder (below) has been set up.
372 bool has_browser_connection_ = false; 369 bool has_browser_connection_ = false;
373 370
374 service_manager::ServiceInfo local_info_; 371 service_manager::ServiceInfo local_info_;
375 372
(...skipping 12 matching lines...) Expand all
388 MessageLoopObserver* message_loop_observer_ = nullptr; 385 MessageLoopObserver* message_loop_observer_ = nullptr;
389 386
390 // Guards |connection_filters_|. 387 // Guards |connection_filters_|.
391 base::Lock lock_; 388 base::Lock lock_;
392 std::map<int, std::unique_ptr<ConnectionFilter>> connection_filters_; 389 std::map<int, std::unique_ptr<ConnectionFilter>> connection_filters_;
393 390
394 std::unordered_map<std::string, std::unique_ptr<EmbeddedServiceRunner>> 391 std::unordered_map<std::string, std::unique_ptr<EmbeddedServiceRunner>>
395 embedded_services_; 392 embedded_services_;
396 std::unordered_map<std::string, ServiceRequestHandler> request_handlers_; 393 std::unordered_map<std::string, ServiceRequestHandler> request_handlers_;
397 394
395 mojo::Binding<mojom::Child> child_binding_;
396
398 base::WeakPtrFactory<IOThreadContext> weak_factory_; 397 base::WeakPtrFactory<IOThreadContext> weak_factory_;
399 398
400 DISALLOW_COPY_AND_ASSIGN(IOThreadContext); 399 DISALLOW_COPY_AND_ASSIGN(IOThreadContext);
401 }; 400 };
402 401
403 //////////////////////////////////////////////////////////////////////////////// 402 ////////////////////////////////////////////////////////////////////////////////
404 // ServiceManagerConnection, public: 403 // ServiceManagerConnection, public:
405 404
406 // static 405 // static
407 void ServiceManagerConnection::SetForProcess( 406 void ServiceManagerConnection::SetForProcess(
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
458 457
459 ServiceManagerConnectionImpl::~ServiceManagerConnectionImpl() { 458 ServiceManagerConnectionImpl::~ServiceManagerConnectionImpl() {
460 context_->ShutDown(); 459 context_->ShutDown();
461 } 460 }
462 461
463 //////////////////////////////////////////////////////////////////////////////// 462 ////////////////////////////////////////////////////////////////////////////////
464 // ServiceManagerConnectionImpl, ServiceManagerConnection implementation: 463 // ServiceManagerConnectionImpl, ServiceManagerConnection implementation:
465 464
466 void ServiceManagerConnectionImpl::Start() { 465 void ServiceManagerConnectionImpl::Start() {
467 context_->Start( 466 context_->Start(
468 base::Bind(&ServiceManagerConnectionImpl::OnContextInitialized, 467 base::Bind(&ServiceManagerConnectionImpl::OnLocalServiceInfoAvailable,
469 weak_factory_.GetWeakPtr()), 468 weak_factory_.GetWeakPtr()),
470 base::Bind(&ServiceManagerConnectionImpl::OnConnect, 469 base::Bind(&ServiceManagerConnectionImpl::OnBrowserServiceInfoAvailable,
471 weak_factory_.GetWeakPtr()), 470 weak_factory_.GetWeakPtr()),
472 base::Bind(&ServiceManagerConnectionImpl::OnConnectionLost, 471 base::Bind(&ServiceManagerConnectionImpl::OnConnectionLost,
473 weak_factory_.GetWeakPtr())); 472 weak_factory_.GetWeakPtr()));
474 } 473 }
475 474
476 service_manager::Connector* ServiceManagerConnectionImpl::GetConnector() { 475 service_manager::Connector* ServiceManagerConnectionImpl::GetConnector() {
477 return connector_.get(); 476 return connector_.get();
478 } 477 }
479 478
480 const service_manager::Identity& ServiceManagerConnectionImpl::GetIdentity() 479 const service_manager::ServiceInfo& ServiceManagerConnectionImpl::GetLocalInfo()
481 const { 480 const {
482 return identity_; 481 return local_info_;
482 }
483
484 const service_manager::ServiceInfo&
485 ServiceManagerConnectionImpl::GetBrowserInfo() const {
486 return browser_info_;
483 } 487 }
484 488
485 void ServiceManagerConnectionImpl::SetConnectionLostClosure( 489 void ServiceManagerConnectionImpl::SetConnectionLostClosure(
486 const base::Closure& closure) { 490 const base::Closure& closure) {
487 connection_lost_handler_ = closure; 491 connection_lost_handler_ = closure;
488 } 492 }
489 493
490 void ServiceManagerConnectionImpl::SetupInterfaceRequestProxies( 494 void ServiceManagerConnectionImpl::SetupInterfaceRequestProxies(
491 service_manager::InterfaceRegistry* registry, 495 service_manager::InterfaceRegistry* registry,
492 service_manager::InterfaceProvider* provider) { 496 service_manager::InterfaceProvider* provider) {
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
525 on_connect_handlers_[id] = handler; 529 on_connect_handlers_[id] = handler;
526 return id; 530 return id;
527 } 531 }
528 532
529 void ServiceManagerConnectionImpl::RemoveOnConnectHandler(int id) { 533 void ServiceManagerConnectionImpl::RemoveOnConnectHandler(int id) {
530 auto it = on_connect_handlers_.find(id); 534 auto it = on_connect_handlers_.find(id);
531 DCHECK(it != on_connect_handlers_.end()); 535 DCHECK(it != on_connect_handlers_.end());
532 on_connect_handlers_.erase(it); 536 on_connect_handlers_.erase(it);
533 } 537 }
534 538
535 void ServiceManagerConnectionImpl::OnContextInitialized( 539 void ServiceManagerConnectionImpl::OnLocalServiceInfoAvailable(
536 const service_manager::Identity& identity) { 540 const service_manager::ServiceInfo& local_info) {
537 identity_ = identity; 541 local_info_ = local_info;
542 }
543
544 void ServiceManagerConnectionImpl::OnBrowserServiceInfoAvailable(
545 const service_manager::ServiceInfo& browser_info) {
546 browser_info_ = browser_info;
547 for (auto& handler : on_connect_handlers_)
548 handler.second.Run(local_info_, browser_info_);
538 } 549 }
539 550
540 void ServiceManagerConnectionImpl::OnConnectionLost() { 551 void ServiceManagerConnectionImpl::OnConnectionLost() {
541 if (!connection_lost_handler_.is_null()) 552 if (!connection_lost_handler_.is_null())
542 connection_lost_handler_.Run(); 553 connection_lost_handler_.Run();
543 } 554 }
544 555
545 void ServiceManagerConnectionImpl::OnConnect(
546 const service_manager::ServiceInfo& local_info,
547 const service_manager::ServiceInfo& remote_info) {
548 local_info_ = local_info;
549 for (auto& handler : on_connect_handlers_)
550 handler.second.Run(local_info, remote_info);
551 }
552
553 void ServiceManagerConnectionImpl::GetInterface( 556 void ServiceManagerConnectionImpl::GetInterface(
554 service_manager::mojom::InterfaceProvider* provider, 557 service_manager::mojom::InterfaceProvider* provider,
555 const std::string& interface_name, 558 const std::string& interface_name,
556 mojo::ScopedMessagePipeHandle request_handle) { 559 mojo::ScopedMessagePipeHandle request_handle) {
557 provider->GetInterface(interface_name, std::move(request_handle)); 560 provider->GetInterface(interface_name, std::move(request_handle));
558 } 561 }
559 562
560 } // namespace content 563 } // namespace content
OLDNEW
« no previous file with comments | « content/common/service_manager/service_manager_connection_impl.h ('k') | content/public/app/mojo/content_gpu_manifest.json » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698