| 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 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 41 // 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 |
| 42 // ServiceManagerConnectionImpl. This includes Service and ServiceFactory | 42 // ServiceManagerConnectionImpl. This includes Service and ServiceFactory |
| 43 // bindings. | 43 // bindings. |
| 44 class ServiceManagerConnectionImpl::IOThreadContext | 44 class ServiceManagerConnectionImpl::IOThreadContext |
| 45 : public base::RefCountedThreadSafe<IOThreadContext>, | 45 : public base::RefCountedThreadSafe<IOThreadContext>, |
| 46 public service_manager::Service, | 46 public service_manager::Service, |
| 47 public service_manager::mojom::ServiceFactory, | 47 public service_manager::mojom::ServiceFactory, |
| 48 public mojom::Child { | 48 public mojom::Child { |
| 49 public: | 49 public: |
| 50 using InitializeCallback = | 50 using InitializeCallback = |
| 51 base::Callback<void(const service_manager::ServiceInfo&)>; | 51 base::Callback<void(const service_manager::BindSourceInfo&)>; |
| 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 child_binding_(this), |
| 63 weak_factory_(this) { | 63 weak_factory_(this) { |
| 64 // 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. |
| 65 io_thread_checker_.DetachFromThread(); | 65 io_thread_checker_.DetachFromThread(); |
| 66 } | 66 } |
| 67 | 67 |
| 68 // Safe to call from any thread. | 68 // Safe to call from any thread. |
| 69 void Start(const InitializeCallback& local_info_available_callback, | 69 void Start(const InitializeCallback& browser_info_available_callback, |
| 70 const InitializeCallback& browser_info_available_callback, | |
| 71 const base::Closure& stop_callback) { | 70 const base::Closure& stop_callback) { |
| 72 DCHECK(!started_); | 71 DCHECK(!started_); |
| 73 | 72 |
| 74 started_ = true; | 73 started_ = true; |
| 75 callback_task_runner_ = base::ThreadTaskRunnerHandle::Get(); | 74 callback_task_runner_ = base::ThreadTaskRunnerHandle::Get(); |
| 76 local_info_available_callback_ = local_info_available_callback; | |
| 77 browser_info_available_callback_ = browser_info_available_callback; | 75 browser_info_available_callback_ = browser_info_available_callback; |
| 78 stop_callback_ = stop_callback; | 76 stop_callback_ = stop_callback; |
| 79 io_task_runner_->PostTask( | 77 io_task_runner_->PostTask( |
| 80 FROM_HERE, base::Bind(&IOThreadContext::StartOnIOThread, this)); | 78 FROM_HERE, base::Bind(&IOThreadContext::StartOnIOThread, this)); |
| 81 } | 79 } |
| 82 | 80 |
| 83 // Safe to call from whichever thread called Start() (or may have called | 81 // Safe to call from whichever thread called Start() (or may have called |
| 84 // Start()). Must be called before IO thread shutdown. | 82 // Start()). Must be called before IO thread shutdown. |
| 85 void ShutDown() { | 83 void ShutDown() { |
| 86 if (!started_) | 84 if (!started_) |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 241 const std::string& name, | 239 const std::string& name, |
| 242 const ServiceRequestHandler& handler) { | 240 const ServiceRequestHandler& handler) { |
| 243 DCHECK(io_thread_checker_.CalledOnValidThread()); | 241 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 244 auto result = request_handlers_.insert(std::make_pair(name, handler)); | 242 auto result = request_handlers_.insert(std::make_pair(name, handler)); |
| 245 DCHECK(result.second); | 243 DCHECK(result.second); |
| 246 } | 244 } |
| 247 | 245 |
| 248 ///////////////////////////////////////////////////////////////////////////// | 246 ///////////////////////////////////////////////////////////////////////////// |
| 249 // service_manager::Service implementation | 247 // service_manager::Service implementation |
| 250 | 248 |
| 251 void OnStart() override { | 249 void OnBindInterface(const service_manager::BindSourceInfo& source_info, |
| 252 DCHECK(io_thread_checker_.CalledOnValidThread()); | |
| 253 DCHECK(!local_info_available_callback_.is_null()); | |
| 254 local_info_ = context()->local_info(); | |
| 255 | |
| 256 InitializeCallback handler = | |
| 257 base::ResetAndReturn(&local_info_available_callback_); | |
| 258 callback_task_runner_->PostTask(FROM_HERE, | |
| 259 base::Bind(handler, local_info_)); | |
| 260 } | |
| 261 | |
| 262 void OnBindInterface(const service_manager::ServiceInfo& source_info, | |
| 263 const std::string& interface_name, | 250 const std::string& interface_name, |
| 264 mojo::ScopedMessagePipeHandle interface_pipe) override { | 251 mojo::ScopedMessagePipeHandle interface_pipe) override { |
| 265 DCHECK(io_thread_checker_.CalledOnValidThread()); | 252 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 266 if (source_info.identity.name() == service_manager::mojom::kServiceName && | 253 if (source_info.identity.name() == service_manager::mojom::kServiceName && |
| 267 interface_name == service_manager::mojom::ServiceFactory::Name_) { | 254 interface_name == service_manager::mojom::ServiceFactory::Name_) { |
| 268 factory_bindings_.AddBinding( | 255 factory_bindings_.AddBinding( |
| 269 this, mojo::MakeRequest<service_manager::mojom::ServiceFactory>( | 256 this, mojo::MakeRequest<service_manager::mojom::ServiceFactory>( |
| 270 std::move(interface_pipe))); | 257 std::move(interface_pipe))); |
| 271 } else if (source_info.identity.name() == mojom::kBrowserServiceName && | 258 } else if (source_info.identity.name() == mojom::kBrowserServiceName && |
| 272 interface_name == mojom::Child::Name_) { | 259 interface_name == mojom::Child::Name_) { |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 319 service_manager::mojom::ServiceRequest pending_service_request_; | 306 service_manager::mojom::ServiceRequest pending_service_request_; |
| 320 scoped_refptr<base::SequencedTaskRunner> io_task_runner_; | 307 scoped_refptr<base::SequencedTaskRunner> io_task_runner_; |
| 321 std::unique_ptr<service_manager::Connector> io_thread_connector_; | 308 std::unique_ptr<service_manager::Connector> io_thread_connector_; |
| 322 service_manager::mojom::ConnectorRequest pending_connector_request_; | 309 service_manager::mojom::ConnectorRequest pending_connector_request_; |
| 323 | 310 |
| 324 // TaskRunner on which to run our owner's callbacks, i.e. the ones passed to | 311 // TaskRunner on which to run our owner's callbacks, i.e. the ones passed to |
| 325 // Start(). | 312 // Start(). |
| 326 scoped_refptr<base::SequencedTaskRunner> callback_task_runner_; | 313 scoped_refptr<base::SequencedTaskRunner> callback_task_runner_; |
| 327 | 314 |
| 328 // Callback to run once Service::OnStart is invoked. | 315 // Callback to run once Service::OnStart is invoked. |
| 329 InitializeCallback local_info_available_callback_; | |
| 330 InitializeCallback browser_info_available_callback_; | 316 InitializeCallback browser_info_available_callback_; |
| 331 | 317 |
| 332 // Callback to run if the service is stopped by the service manager. | 318 // Callback to run if the service is stopped by the service manager. |
| 333 base::Closure stop_callback_; | 319 base::Closure stop_callback_; |
| 334 | 320 |
| 335 service_manager::ServiceInfo local_info_; | 321 service_manager::BindSourceInfo local_info_; |
| 336 | 322 |
| 337 std::unique_ptr<service_manager::ServiceContext> service_context_; | 323 std::unique_ptr<service_manager::ServiceContext> service_context_; |
| 338 mojo::BindingSet<service_manager::mojom::ServiceFactory> factory_bindings_; | 324 mojo::BindingSet<service_manager::mojom::ServiceFactory> factory_bindings_; |
| 339 int next_filter_id_ = kInvalidConnectionFilterId; | 325 int next_filter_id_ = kInvalidConnectionFilterId; |
| 340 | 326 |
| 341 // Not owned. | 327 // Not owned. |
| 342 MessageLoopObserver* message_loop_observer_ = nullptr; | 328 MessageLoopObserver* message_loop_observer_ = nullptr; |
| 343 | 329 |
| 344 // Guards |connection_filters_|. | 330 // Guards |connection_filters_|. |
| 345 base::Lock lock_; | 331 base::Lock lock_; |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 414 | 400 |
| 415 ServiceManagerConnectionImpl::~ServiceManagerConnectionImpl() { | 401 ServiceManagerConnectionImpl::~ServiceManagerConnectionImpl() { |
| 416 context_->ShutDown(); | 402 context_->ShutDown(); |
| 417 } | 403 } |
| 418 | 404 |
| 419 //////////////////////////////////////////////////////////////////////////////// | 405 //////////////////////////////////////////////////////////////////////////////// |
| 420 // ServiceManagerConnectionImpl, ServiceManagerConnection implementation: | 406 // ServiceManagerConnectionImpl, ServiceManagerConnection implementation: |
| 421 | 407 |
| 422 void ServiceManagerConnectionImpl::Start() { | 408 void ServiceManagerConnectionImpl::Start() { |
| 423 context_->Start( | 409 context_->Start( |
| 424 base::Bind(&ServiceManagerConnectionImpl::OnLocalServiceInfoAvailable, | |
| 425 weak_factory_.GetWeakPtr()), | |
| 426 base::Bind(&ServiceManagerConnectionImpl::OnBrowserServiceInfoAvailable, | 410 base::Bind(&ServiceManagerConnectionImpl::OnBrowserServiceInfoAvailable, |
| 427 weak_factory_.GetWeakPtr()), | 411 weak_factory_.GetWeakPtr()), |
| 428 base::Bind(&ServiceManagerConnectionImpl::OnConnectionLost, | 412 base::Bind(&ServiceManagerConnectionImpl::OnConnectionLost, |
| 429 weak_factory_.GetWeakPtr())); | 413 weak_factory_.GetWeakPtr())); |
| 430 } | 414 } |
| 431 | 415 |
| 432 service_manager::Connector* ServiceManagerConnectionImpl::GetConnector() { | 416 service_manager::Connector* ServiceManagerConnectionImpl::GetConnector() { |
| 433 return connector_.get(); | 417 return connector_.get(); |
| 434 } | 418 } |
| 435 | 419 |
| 436 const service_manager::ServiceInfo& ServiceManagerConnectionImpl::GetLocalInfo() | 420 const service_manager::BindSourceInfo& |
| 437 const { | |
| 438 return local_info_; | |
| 439 } | |
| 440 | |
| 441 const service_manager::ServiceInfo& | |
| 442 ServiceManagerConnectionImpl::GetBrowserInfo() const { | 421 ServiceManagerConnectionImpl::GetBrowserInfo() const { |
| 443 return browser_info_; | 422 return browser_info_; |
| 444 } | 423 } |
| 445 | 424 |
| 446 void ServiceManagerConnectionImpl::SetConnectionLostClosure( | 425 void ServiceManagerConnectionImpl::SetConnectionLostClosure( |
| 447 const base::Closure& closure) { | 426 const base::Closure& closure) { |
| 448 connection_lost_handler_ = closure; | 427 connection_lost_handler_ = closure; |
| 449 } | 428 } |
| 450 | 429 |
| 451 int ServiceManagerConnectionImpl::AddConnectionFilter( | 430 int ServiceManagerConnectionImpl::AddConnectionFilter( |
| (...skipping 22 matching lines...) Expand all Loading... |
| 474 on_connect_handlers_[id] = handler; | 453 on_connect_handlers_[id] = handler; |
| 475 return id; | 454 return id; |
| 476 } | 455 } |
| 477 | 456 |
| 478 void ServiceManagerConnectionImpl::RemoveOnConnectHandler(int id) { | 457 void ServiceManagerConnectionImpl::RemoveOnConnectHandler(int id) { |
| 479 auto it = on_connect_handlers_.find(id); | 458 auto it = on_connect_handlers_.find(id); |
| 480 DCHECK(it != on_connect_handlers_.end()); | 459 DCHECK(it != on_connect_handlers_.end()); |
| 481 on_connect_handlers_.erase(it); | 460 on_connect_handlers_.erase(it); |
| 482 } | 461 } |
| 483 | 462 |
| 484 void ServiceManagerConnectionImpl::OnLocalServiceInfoAvailable( | |
| 485 const service_manager::ServiceInfo& local_info) { | |
| 486 local_info_ = local_info; | |
| 487 } | |
| 488 | |
| 489 void ServiceManagerConnectionImpl::OnBrowserServiceInfoAvailable( | 463 void ServiceManagerConnectionImpl::OnBrowserServiceInfoAvailable( |
| 490 const service_manager::ServiceInfo& browser_info) { | 464 const service_manager::BindSourceInfo& browser_info) { |
| 491 browser_info_ = browser_info; | 465 browser_info_ = browser_info; |
| 492 for (auto& handler : on_connect_handlers_) | 466 for (auto& handler : on_connect_handlers_) |
| 493 handler.second.Run(local_info_, browser_info_); | 467 handler.second.Run(browser_info_); |
| 494 } | 468 } |
| 495 | 469 |
| 496 void ServiceManagerConnectionImpl::OnConnectionLost() { | 470 void ServiceManagerConnectionImpl::OnConnectionLost() { |
| 497 if (!connection_lost_handler_.is_null()) | 471 if (!connection_lost_handler_.is_null()) |
| 498 connection_lost_handler_.Run(); | 472 connection_lost_handler_.Run(); |
| 499 } | 473 } |
| 500 | 474 |
| 501 void ServiceManagerConnectionImpl::GetInterface( | 475 void ServiceManagerConnectionImpl::GetInterface( |
| 502 service_manager::mojom::InterfaceProvider* provider, | 476 service_manager::mojom::InterfaceProvider* provider, |
| 503 const std::string& interface_name, | 477 const std::string& interface_name, |
| 504 mojo::ScopedMessagePipeHandle request_handle) { | 478 mojo::ScopedMessagePipeHandle request_handle) { |
| 505 provider->GetInterface(interface_name, std::move(request_handle)); | 479 provider->GetInterface(interface_name, std::move(request_handle)); |
| 506 } | 480 } |
| 507 | 481 |
| 508 } // namespace content | 482 } // namespace content |
| OLD | NEW |