Chromium Code Reviews| 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 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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 weak_factory_(this) { | 62 weak_factory_(this) { |
| 63 // This will be reattached by any of the IO thread functions on first call. | 63 // This will be reattached by any of the IO thread functions on first call. |
| 64 io_thread_checker_.DetachFromThread(); | 64 io_thread_checker_.DetachFromThread(); |
| 65 } | 65 } |
| 66 | 66 |
| 67 // Safe to call from any thread. | 67 // Safe to call from any thread. |
| 68 void Start(const InitializeCallback& initialize_callback, | 68 void Start( |
| 69 const ServiceFactoryCallback& create_service_callback, | 69 const InitializeCallback& initialize_callback, |
| 70 const base::Closure& stop_callback) { | 70 const ServiceManagerConnection::OnConnectHandler& on_connect_callback, |
| 71 const ServiceFactoryCallback& create_service_callback, | |
| 72 const base::Closure& stop_callback) { | |
| 71 DCHECK(!started_); | 73 DCHECK(!started_); |
| 72 | 74 |
| 73 started_ = true; | 75 started_ = true; |
| 74 callback_task_runner_ = base::ThreadTaskRunnerHandle::Get(); | 76 callback_task_runner_ = base::ThreadTaskRunnerHandle::Get(); |
| 75 initialize_handler_ = initialize_callback; | 77 initialize_handler_ = initialize_callback; |
| 78 on_connect_callback_ = on_connect_callback; | |
| 76 create_service_callback_ = create_service_callback; | 79 create_service_callback_ = create_service_callback; |
| 77 stop_callback_ = stop_callback; | 80 stop_callback_ = stop_callback; |
| 78 io_task_runner_->PostTask( | 81 io_task_runner_->PostTask( |
| 79 FROM_HERE, base::Bind(&IOThreadContext::StartOnIOThread, this)); | 82 FROM_HERE, base::Bind(&IOThreadContext::StartOnIOThread, this)); |
| 80 } | 83 } |
| 81 | 84 |
| 82 // Safe to call from whichever thread called Start() (or may have called | 85 // Safe to call from whichever thread called Start() (or may have called |
| 83 // Start()). Must be called before IO thread shutdown. | 86 // Start()). Must be called before IO thread shutdown. |
| 84 void ShutDown() { | 87 void ShutDown() { |
| 85 if (!started_) | 88 if (!started_) |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 214 DCHECK(io_thread_checker_.CalledOnValidThread()); | 217 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 215 has_browser_connection_ = false; | 218 has_browser_connection_ = false; |
| 216 } | 219 } |
| 217 | 220 |
| 218 ///////////////////////////////////////////////////////////////////////////// | 221 ///////////////////////////////////////////////////////////////////////////// |
| 219 // service_manager::Service implementation | 222 // service_manager::Service implementation |
| 220 | 223 |
| 221 void OnStart(const service_manager::ServiceInfo& info) override { | 224 void OnStart(const service_manager::ServiceInfo& info) override { |
| 222 DCHECK(io_thread_checker_.CalledOnValidThread()); | 225 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 223 DCHECK(!initialize_handler_.is_null()); | 226 DCHECK(!initialize_handler_.is_null()); |
| 224 id_ = info.identity; | 227 local_info_ = info; |
| 225 | 228 |
| 226 InitializeCallback handler = base::ResetAndReturn(&initialize_handler_); | 229 InitializeCallback handler = base::ResetAndReturn(&initialize_handler_); |
| 227 callback_task_runner_->PostTask(FROM_HERE, base::Bind(handler, id_)); | 230 callback_task_runner_->PostTask(FROM_HERE, |
| 231 base::Bind(handler, local_info_.identity)); | |
| 228 } | 232 } |
| 229 | 233 |
| 230 bool OnConnect(const service_manager::ServiceInfo& remote_info, | 234 bool OnConnect(const service_manager::ServiceInfo& remote_info, |
| 231 service_manager::InterfaceRegistry* registry) override { | 235 service_manager::InterfaceRegistry* registry) override { |
| 232 DCHECK(io_thread_checker_.CalledOnValidThread()); | 236 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 237 | |
| 238 callback_task_runner_->PostTask( | |
| 239 FROM_HERE, base::Bind(on_connect_callback_, local_info_, remote_info)); | |
| 240 | |
| 233 std::string remote_service = remote_info.identity.name(); | 241 std::string remote_service = remote_info.identity.name(); |
| 234 if (remote_service == "service:service_manager") { | 242 if (remote_service == "service:service_manager") { |
| 235 // Only expose the ServiceFactory interface to the Service Manager. | 243 // Only expose the ServiceFactory interface to the Service Manager. |
| 236 registry->AddInterface<service_manager::mojom::ServiceFactory>(this); | 244 registry->AddInterface<service_manager::mojom::ServiceFactory>(this); |
| 237 return true; | 245 return true; |
| 238 } | 246 } |
| 239 | 247 |
| 240 bool accept = false; | 248 bool accept = false; |
| 241 { | 249 { |
| 242 base::AutoLock lock(lock_); | 250 base::AutoLock lock(lock_); |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 305 std::unique_ptr<service_manager::Connector> io_thread_connector_; | 313 std::unique_ptr<service_manager::Connector> io_thread_connector_; |
| 306 service_manager::mojom::ConnectorRequest pending_connector_request_; | 314 service_manager::mojom::ConnectorRequest pending_connector_request_; |
| 307 | 315 |
| 308 // TaskRunner on which to run our owner's callbacks, i.e. the ones passed to | 316 // TaskRunner on which to run our owner's callbacks, i.e. the ones passed to |
| 309 // Start(). | 317 // Start(). |
| 310 scoped_refptr<base::SequencedTaskRunner> callback_task_runner_; | 318 scoped_refptr<base::SequencedTaskRunner> callback_task_runner_; |
| 311 | 319 |
| 312 // Callback to run once Service::OnStart is invoked. | 320 // Callback to run once Service::OnStart is invoked. |
| 313 InitializeCallback initialize_handler_; | 321 InitializeCallback initialize_handler_; |
| 314 | 322 |
| 323 // Callback to run when a connection request is received. | |
| 324 ServiceManagerConnection::OnConnectHandler on_connect_callback_; | |
| 325 | |
| 315 // Callback to run when a new Service request is received. | 326 // Callback to run when a new Service request is received. |
| 316 ServiceFactoryCallback create_service_callback_; | 327 ServiceFactoryCallback create_service_callback_; |
| 317 | 328 |
| 318 // Callback to run if the service is stopped by the service manager. | 329 // Callback to run if the service is stopped by the service manager. |
| 319 base::Closure stop_callback_; | 330 base::Closure stop_callback_; |
| 320 | 331 |
| 321 // Called once a connection has been received from the browser process & the | 332 // Called once a connection has been received from the browser process & the |
| 322 // default binder (below) has been set up. | 333 // default binder (below) has been set up. |
| 323 bool has_browser_connection_ = false; | 334 bool has_browser_connection_ = false; |
| 324 | 335 |
| 325 service_manager::Identity id_; | 336 service_manager::ServiceInfo local_info_; |
| 326 | 337 |
| 327 // Default binder callback used for the browser connection's | 338 // Default binder callback used for the browser connection's |
| 328 // InterfaceRegistry. | 339 // InterfaceRegistry. |
| 329 // | 340 // |
| 330 // TODO(rockot): Remove this once all interfaces exposed to the browser are | 341 // TODO(rockot): Remove this once all interfaces exposed to the browser are |
| 331 // exposed via a ConnectionFilter. | 342 // exposed via a ConnectionFilter. |
| 332 service_manager::InterfaceRegistry::Binder default_browser_binder_; | 343 service_manager::InterfaceRegistry::Binder default_browser_binder_; |
| 333 | 344 |
| 334 std::unique_ptr<service_manager::ServiceContext> service_context_; | 345 std::unique_ptr<service_manager::ServiceContext> service_context_; |
| 335 mojo::BindingSet<service_manager::mojom::ServiceFactory> factory_bindings_; | 346 mojo::BindingSet<service_manager::mojom::ServiceFactory> factory_bindings_; |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 407 context_->ShutDown(); | 418 context_->ShutDown(); |
| 408 } | 419 } |
| 409 | 420 |
| 410 //////////////////////////////////////////////////////////////////////////////// | 421 //////////////////////////////////////////////////////////////////////////////// |
| 411 // ServiceManagerConnectionImpl, ServiceManagerConnection implementation: | 422 // ServiceManagerConnectionImpl, ServiceManagerConnection implementation: |
| 412 | 423 |
| 413 void ServiceManagerConnectionImpl::Start() { | 424 void ServiceManagerConnectionImpl::Start() { |
| 414 context_->Start( | 425 context_->Start( |
| 415 base::Bind(&ServiceManagerConnectionImpl::OnContextInitialized, | 426 base::Bind(&ServiceManagerConnectionImpl::OnContextInitialized, |
| 416 weak_factory_.GetWeakPtr()), | 427 weak_factory_.GetWeakPtr()), |
| 428 base::Bind(&ServiceManagerConnectionImpl::OnConnect, | |
| 429 weak_factory_.GetWeakPtr()), | |
| 417 base::Bind(&ServiceManagerConnectionImpl::CreateService, | 430 base::Bind(&ServiceManagerConnectionImpl::CreateService, |
| 418 weak_factory_.GetWeakPtr()), | 431 weak_factory_.GetWeakPtr()), |
| 419 base::Bind(&ServiceManagerConnectionImpl::OnConnectionLost, | 432 base::Bind(&ServiceManagerConnectionImpl::OnConnectionLost, |
| 420 weak_factory_.GetWeakPtr())); | 433 weak_factory_.GetWeakPtr())); |
| 421 } | 434 } |
| 422 | 435 |
| 423 void ServiceManagerConnectionImpl::SetInitializeHandler( | 436 void ServiceManagerConnectionImpl::SetInitializeHandler( |
| 424 const base::Closure& handler) { | 437 const base::Closure& handler) { |
| 425 DCHECK(initialize_handler_.is_null()); | 438 DCHECK(initialize_handler_.is_null()); |
| 426 initialize_handler_ = handler; | 439 initialize_handler_ = handler; |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 473 DCHECK(result.second); | 486 DCHECK(result.second); |
| 474 } | 487 } |
| 475 | 488 |
| 476 void ServiceManagerConnectionImpl::AddServiceRequestHandler( | 489 void ServiceManagerConnectionImpl::AddServiceRequestHandler( |
| 477 const std::string& name, | 490 const std::string& name, |
| 478 const ServiceRequestHandler& handler) { | 491 const ServiceRequestHandler& handler) { |
| 479 auto result = request_handlers_.insert(std::make_pair(name, handler)); | 492 auto result = request_handlers_.insert(std::make_pair(name, handler)); |
| 480 DCHECK(result.second); | 493 DCHECK(result.second); |
| 481 } | 494 } |
| 482 | 495 |
| 496 int ServiceManagerConnectionImpl::AddOnConnectHandler( | |
| 497 const OnConnectHandler& handler) { | |
| 498 int id = ++next_on_connect_handler_id_; | |
| 499 on_connect_handlers_[id] = handler; | |
| 500 return id; | |
| 501 } | |
| 502 | |
| 503 void ServiceManagerConnectionImpl::RemoveOnConnectHandler(int id) { | |
| 504 auto it = on_connect_handlers_.find(id); | |
| 505 DCHECK(it != on_connect_handlers_.end()); | |
| 506 on_connect_handlers_.erase(it); | |
| 507 } | |
| 508 | |
| 483 void ServiceManagerConnectionImpl::CreateService( | 509 void ServiceManagerConnectionImpl::CreateService( |
| 484 service_manager::mojom::ServiceRequest request, | 510 service_manager::mojom::ServiceRequest request, |
| 485 const std::string& name) { | 511 const std::string& name) { |
| 486 auto it = request_handlers_.find(name); | 512 auto it = request_handlers_.find(name); |
| 487 if (it != request_handlers_.end()) | 513 if (it != request_handlers_.end()) |
| 488 it->second.Run(std::move(request)); | 514 it->second.Run(std::move(request)); |
| 489 } | 515 } |
| 490 | 516 |
| 491 void ServiceManagerConnectionImpl::OnContextInitialized( | 517 void ServiceManagerConnectionImpl::OnContextInitialized( |
| 492 const service_manager::Identity& identity) { | 518 const service_manager::Identity& identity) { |
| 493 identity_ = identity; | 519 identity_ = identity; |
| 494 if (!initialize_handler_.is_null()) | 520 if (!initialize_handler_.is_null()) |
| 495 base::ResetAndReturn(&initialize_handler_).Run(); | 521 base::ResetAndReturn(&initialize_handler_).Run(); |
| 496 } | 522 } |
| 497 | 523 |
| 498 void ServiceManagerConnectionImpl::OnConnectionLost() { | 524 void ServiceManagerConnectionImpl::OnConnectionLost() { |
| 499 if (!connection_lost_handler_.is_null()) | 525 if (!connection_lost_handler_.is_null()) |
| 500 connection_lost_handler_.Run(); | 526 connection_lost_handler_.Run(); |
| 501 } | 527 } |
| 502 | 528 |
| 529 void ServiceManagerConnectionImpl::OnConnect( | |
| 530 const service_manager::ServiceInfo& local_info, | |
| 531 const service_manager::ServiceInfo& remote_info) { | |
| 532 local_info_ = local_info; | |
| 533 last_remote_info_ = remote_info; | |
|
Ken Rockot(use gerrit already)
2016/10/27 05:12:52
last_remote_info_ is not used. remove?
| |
| 534 for (auto& handler : on_connect_handlers_) | |
| 535 handler.second.Run(local_info, remote_info); | |
| 536 } | |
| 537 | |
| 503 void ServiceManagerConnectionImpl::GetInterface( | 538 void ServiceManagerConnectionImpl::GetInterface( |
| 504 service_manager::mojom::InterfaceProvider* provider, | 539 service_manager::mojom::InterfaceProvider* provider, |
| 505 const std::string& interface_name, | 540 const std::string& interface_name, |
| 506 mojo::ScopedMessagePipeHandle request_handle) { | 541 mojo::ScopedMessagePipeHandle request_handle) { |
| 507 provider->GetInterface(interface_name, std::move(request_handle)); | 542 provider->GetInterface(interface_name, std::move(request_handle)); |
| 508 } | 543 } |
| 509 | 544 |
| 510 } // namespace content | 545 } // namespace content |
| 511 | 546 |
| OLD | NEW |