| 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/mojo/mojo_shell_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 "content/common/mojo/embedded_application_runner.h" | 18 #include "content/common/service_manager/embedded_service_runner.h" |
| 19 #include "content/public/common/connection_filter.h" | 19 #include "content/public/common/connection_filter.h" |
| 20 #include "mojo/public/cpp/bindings/binding_set.h" | 20 #include "mojo/public/cpp/bindings/binding_set.h" |
| 21 #include "mojo/public/cpp/system/message_pipe.h" | 21 #include "mojo/public/cpp/system/message_pipe.h" |
| 22 #include "services/shell/public/cpp/service.h" | 22 #include "services/shell/public/cpp/service.h" |
| 23 #include "services/shell/public/cpp/service_context.h" | 23 #include "services/shell/public/cpp/service_context.h" |
| 24 #include "services/shell/public/interfaces/service_factory.mojom.h" | 24 #include "services/shell/public/interfaces/service_factory.mojom.h" |
| 25 #include "services/shell/runner/common/client_util.h" | 25 #include "services/shell/runner/common/client_util.h" |
| 26 | 26 |
| 27 namespace content { | 27 namespace content { |
| 28 namespace { | 28 namespace { |
| 29 | 29 |
| 30 base::LazyInstance<std::unique_ptr<MojoShellConnection>>::Leaky | 30 base::LazyInstance<std::unique_ptr<ServiceManagerConnection>>::Leaky |
| 31 g_connection_for_process = LAZY_INSTANCE_INITIALIZER; | 31 g_connection_for_process = LAZY_INSTANCE_INITIALIZER; |
| 32 | 32 |
| 33 MojoShellConnection::Factory* mojo_shell_connection_factory = nullptr; | 33 ServiceManagerConnection::Factory* service_manager_connection_factory = nullptr; |
| 34 | 34 |
| 35 } // namespace | 35 } // namespace |
| 36 | 36 |
| 37 // A ref-counted object which owns the IO thread state of a | 37 // A ref-counted object which owns the IO thread state of a |
| 38 // MojoShellConnectionImpl. This includes Service and ServiceFactory | 38 // ServiceManagerConnectionImpl. This includes Service and ServiceFactory |
| 39 // bindings. | 39 // bindings. |
| 40 class MojoShellConnectionImpl::IOThreadContext | 40 class ServiceManagerConnectionImpl::IOThreadContext |
| 41 : public base::RefCountedThreadSafe<IOThreadContext>, | 41 : public base::RefCountedThreadSafe<IOThreadContext>, |
| 42 public shell::Service, | 42 public shell::Service, |
| 43 public shell::InterfaceFactory<shell::mojom::ServiceFactory>, | 43 public shell::InterfaceFactory<shell::mojom::ServiceFactory>, |
| 44 public shell::mojom::ServiceFactory { | 44 public shell::mojom::ServiceFactory { |
| 45 public: | 45 public: |
| 46 using InitializeCallback = base::Callback<void(const shell::Identity&)>; | 46 using InitializeCallback = base::Callback<void(const shell::Identity&)>; |
| 47 using ServiceFactoryCallback = | 47 using ServiceFactoryCallback = |
| 48 base::Callback<void(shell::mojom::ServiceRequest, const std::string&)>; | 48 base::Callback<void(shell::mojom::ServiceRequest, const std::string&)>; |
| 49 | 49 |
| 50 IOThreadContext(shell::mojom::ServiceRequest service_request, | 50 IOThreadContext(shell::mojom::ServiceRequest service_request, |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 DCHECK(!initialize_handler_.is_null()); | 219 DCHECK(!initialize_handler_.is_null()); |
| 220 id_ = identity; | 220 id_ = identity; |
| 221 | 221 |
| 222 InitializeCallback handler = base::ResetAndReturn(&initialize_handler_); | 222 InitializeCallback handler = base::ResetAndReturn(&initialize_handler_); |
| 223 callback_task_runner_->PostTask(FROM_HERE, base::Bind(handler, identity)); | 223 callback_task_runner_->PostTask(FROM_HERE, base::Bind(handler, identity)); |
| 224 } | 224 } |
| 225 | 225 |
| 226 bool OnConnect(const shell::Identity& remote_identity, | 226 bool OnConnect(const shell::Identity& remote_identity, |
| 227 shell::InterfaceRegistry* registry) override { | 227 shell::InterfaceRegistry* registry) override { |
| 228 DCHECK(io_thread_checker_.CalledOnValidThread()); | 228 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 229 std::string remote_app = remote_identity.name(); | 229 std::string remote_service = remote_identity.name(); |
| 230 if (remote_app == "service:shell") { | 230 if (remote_service == "service:shell") { |
| 231 // Only expose the SCF interface to the shell. | 231 // Only expose the SCF interface to the shell. |
| 232 registry->AddInterface<shell::mojom::ServiceFactory>(this); | 232 registry->AddInterface<shell::mojom::ServiceFactory>(this); |
| 233 return true; | 233 return true; |
| 234 } | 234 } |
| 235 | 235 |
| 236 bool accept = false; | 236 bool accept = false; |
| 237 { | 237 { |
| 238 base::AutoLock lock(lock_); | 238 base::AutoLock lock(lock_); |
| 239 for (auto& entry : connection_filters_) { | 239 for (auto& entry : connection_filters_) { |
| 240 accept |= entry.second->OnConnect(remote_identity, registry, | 240 accept |= entry.second->OnConnect(remote_identity, registry, |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 336 // Guards |connection_filters_|. | 336 // Guards |connection_filters_|. |
| 337 base::Lock lock_; | 337 base::Lock lock_; |
| 338 std::map<int, std::unique_ptr<ConnectionFilter>> connection_filters_; | 338 std::map<int, std::unique_ptr<ConnectionFilter>> connection_filters_; |
| 339 | 339 |
| 340 base::WeakPtrFactory<IOThreadContext> weak_factory_; | 340 base::WeakPtrFactory<IOThreadContext> weak_factory_; |
| 341 | 341 |
| 342 DISALLOW_COPY_AND_ASSIGN(IOThreadContext); | 342 DISALLOW_COPY_AND_ASSIGN(IOThreadContext); |
| 343 }; | 343 }; |
| 344 | 344 |
| 345 //////////////////////////////////////////////////////////////////////////////// | 345 //////////////////////////////////////////////////////////////////////////////// |
| 346 // MojoShellConnection, public: | 346 // ServiceManagerConnection, public: |
| 347 | 347 |
| 348 // static | 348 // static |
| 349 void MojoShellConnection::SetForProcess( | 349 void ServiceManagerConnection::SetForProcess( |
| 350 std::unique_ptr<MojoShellConnection> connection) { | 350 std::unique_ptr<ServiceManagerConnection> connection) { |
| 351 DCHECK(!g_connection_for_process.Get()); | 351 DCHECK(!g_connection_for_process.Get()); |
| 352 g_connection_for_process.Get() = std::move(connection); | 352 g_connection_for_process.Get() = std::move(connection); |
| 353 } | 353 } |
| 354 | 354 |
| 355 // static | 355 // static |
| 356 MojoShellConnection* MojoShellConnection::GetForProcess() { | 356 ServiceManagerConnection* ServiceManagerConnection::GetForProcess() { |
| 357 return g_connection_for_process.Get().get(); | 357 return g_connection_for_process.Get().get(); |
| 358 } | 358 } |
| 359 | 359 |
| 360 // static | 360 // static |
| 361 void MojoShellConnection::DestroyForProcess() { | 361 void ServiceManagerConnection::DestroyForProcess() { |
| 362 // This joins the shell controller thread. | 362 // This joins the service manager controller thread. |
| 363 g_connection_for_process.Get().reset(); | 363 g_connection_for_process.Get().reset(); |
| 364 } | 364 } |
| 365 | 365 |
| 366 // static | 366 // static |
| 367 void MojoShellConnection::SetFactoryForTest(Factory* factory) { | 367 void ServiceManagerConnection::SetFactoryForTest(Factory* factory) { |
| 368 DCHECK(!g_connection_for_process.Get()); | 368 DCHECK(!g_connection_for_process.Get()); |
| 369 mojo_shell_connection_factory = factory; | 369 service_manager_connection_factory = factory; |
| 370 } | 370 } |
| 371 | 371 |
| 372 // static | 372 // static |
| 373 std::unique_ptr<MojoShellConnection> MojoShellConnection::Create( | 373 std::unique_ptr<ServiceManagerConnection> ServiceManagerConnection::Create( |
| 374 shell::mojom::ServiceRequest request, | 374 shell::mojom::ServiceRequest request, |
| 375 scoped_refptr<base::SequencedTaskRunner> io_task_runner) { | 375 scoped_refptr<base::SequencedTaskRunner> io_task_runner) { |
| 376 if (mojo_shell_connection_factory) | 376 if (service_manager_connection_factory) |
| 377 return mojo_shell_connection_factory->Run(); | 377 return service_manager_connection_factory->Run(); |
| 378 return base::MakeUnique<MojoShellConnectionImpl>( | 378 return base::MakeUnique<ServiceManagerConnectionImpl>( |
| 379 std::move(request), io_task_runner); | 379 std::move(request), io_task_runner); |
| 380 } | 380 } |
| 381 | 381 |
| 382 MojoShellConnection::~MojoShellConnection() {} | 382 ServiceManagerConnection::~ServiceManagerConnection() {} |
| 383 | 383 |
| 384 //////////////////////////////////////////////////////////////////////////////// | 384 //////////////////////////////////////////////////////////////////////////////// |
| 385 // MojoShellConnectionImpl, public: | 385 // ServiceManagerConnectionImpl, public: |
| 386 | 386 |
| 387 MojoShellConnectionImpl::MojoShellConnectionImpl( | 387 ServiceManagerConnectionImpl::ServiceManagerConnectionImpl( |
| 388 shell::mojom::ServiceRequest request, | 388 shell::mojom::ServiceRequest request, |
| 389 scoped_refptr<base::SequencedTaskRunner> io_task_runner) | 389 scoped_refptr<base::SequencedTaskRunner> io_task_runner) |
| 390 : weak_factory_(this) { | 390 : weak_factory_(this) { |
| 391 shell::mojom::ConnectorRequest connector_request; | 391 shell::mojom::ConnectorRequest connector_request; |
| 392 connector_ = shell::Connector::Create(&connector_request); | 392 connector_ = shell::Connector::Create(&connector_request); |
| 393 | 393 |
| 394 std::unique_ptr<shell::Connector> io_thread_connector = connector_->Clone(); | 394 std::unique_ptr<shell::Connector> io_thread_connector = connector_->Clone(); |
| 395 context_ = new IOThreadContext( | 395 context_ = new IOThreadContext( |
| 396 std::move(request), io_task_runner, std::move(io_thread_connector), | 396 std::move(request), io_task_runner, std::move(io_thread_connector), |
| 397 std::move(connector_request)); | 397 std::move(connector_request)); |
| 398 } | 398 } |
| 399 | 399 |
| 400 MojoShellConnectionImpl::~MojoShellConnectionImpl() { | 400 ServiceManagerConnectionImpl::~ServiceManagerConnectionImpl() { |
| 401 context_->ShutDown(); | 401 context_->ShutDown(); |
| 402 } | 402 } |
| 403 | 403 |
| 404 //////////////////////////////////////////////////////////////////////////////// | 404 //////////////////////////////////////////////////////////////////////////////// |
| 405 // MojoShellConnectionImpl, MojoShellConnection implementation: | 405 // ServiceManagerConnectionImpl, ServiceManagerConnection implementation: |
| 406 | 406 |
| 407 void MojoShellConnectionImpl::Start() { | 407 void ServiceManagerConnectionImpl::Start() { |
| 408 context_->Start( | 408 context_->Start( |
| 409 base::Bind(&MojoShellConnectionImpl::OnContextInitialized, | 409 base::Bind(&ServiceManagerConnectionImpl::OnContextInitialized, |
| 410 weak_factory_.GetWeakPtr()), | 410 weak_factory_.GetWeakPtr()), |
| 411 base::Bind(&MojoShellConnectionImpl::CreateService, | 411 base::Bind(&ServiceManagerConnectionImpl::CreateService, |
| 412 weak_factory_.GetWeakPtr()), | 412 weak_factory_.GetWeakPtr()), |
| 413 base::Bind(&MojoShellConnectionImpl::OnConnectionLost, | 413 base::Bind(&ServiceManagerConnectionImpl::OnConnectionLost, |
| 414 weak_factory_.GetWeakPtr())); | 414 weak_factory_.GetWeakPtr())); |
| 415 } | 415 } |
| 416 | 416 |
| 417 void MojoShellConnectionImpl::SetInitializeHandler( | 417 void ServiceManagerConnectionImpl::SetInitializeHandler( |
| 418 const base::Closure& handler) { | 418 const base::Closure& handler) { |
| 419 DCHECK(initialize_handler_.is_null()); | 419 DCHECK(initialize_handler_.is_null()); |
| 420 initialize_handler_ = handler; | 420 initialize_handler_ = handler; |
| 421 } | 421 } |
| 422 | 422 |
| 423 shell::Connector* MojoShellConnectionImpl::GetConnector() { | 423 shell::Connector* ServiceManagerConnectionImpl::GetConnector() { |
| 424 return connector_.get(); | 424 return connector_.get(); |
| 425 } | 425 } |
| 426 | 426 |
| 427 const shell::Identity& MojoShellConnectionImpl::GetIdentity() const { | 427 const shell::Identity& ServiceManagerConnectionImpl::GetIdentity() const { |
| 428 return identity_; | 428 return identity_; |
| 429 } | 429 } |
| 430 | 430 |
| 431 void MojoShellConnectionImpl::SetConnectionLostClosure( | 431 void ServiceManagerConnectionImpl::SetConnectionLostClosure( |
| 432 const base::Closure& closure) { | 432 const base::Closure& closure) { |
| 433 connection_lost_handler_ = closure; | 433 connection_lost_handler_ = closure; |
| 434 } | 434 } |
| 435 | 435 |
| 436 void MojoShellConnectionImpl::SetupInterfaceRequestProxies( | 436 void ServiceManagerConnectionImpl::SetupInterfaceRequestProxies( |
| 437 shell::InterfaceRegistry* registry, | 437 shell::InterfaceRegistry* registry, |
| 438 shell::InterfaceProvider* provider) { | 438 shell::InterfaceProvider* provider) { |
| 439 // It's safe to bind |registry| as a raw pointer because the caller must | 439 // It's safe to bind |registry| as a raw pointer because the caller must |
| 440 // guarantee that it outlives |this|, and |this| is bound as a weak ptr here. | 440 // guarantee that it outlives |this|, and |this| is bound as a weak ptr here. |
| 441 context_->SetDefaultBinderForBrowserConnection( | 441 context_->SetDefaultBinderForBrowserConnection( |
| 442 base::Bind(&MojoShellConnectionImpl::GetInterface, | 442 base::Bind(&ServiceManagerConnectionImpl::GetInterface, |
| 443 weak_factory_.GetWeakPtr(), registry)); | 443 weak_factory_.GetWeakPtr(), registry)); |
| 444 | 444 |
| 445 // TODO(beng): remove provider parameter. | 445 // TODO(beng): remove provider parameter. |
| 446 } | 446 } |
| 447 | 447 |
| 448 int MojoShellConnectionImpl::AddConnectionFilter( | 448 int ServiceManagerConnectionImpl::AddConnectionFilter( |
| 449 std::unique_ptr<ConnectionFilter> filter) { | 449 std::unique_ptr<ConnectionFilter> filter) { |
| 450 return context_->AddConnectionFilter(std::move(filter)); | 450 return context_->AddConnectionFilter(std::move(filter)); |
| 451 } | 451 } |
| 452 | 452 |
| 453 void MojoShellConnectionImpl::RemoveConnectionFilter(int filter_id) { | 453 void ServiceManagerConnectionImpl::RemoveConnectionFilter(int filter_id) { |
| 454 context_->RemoveConnectionFilter(filter_id); | 454 context_->RemoveConnectionFilter(filter_id); |
| 455 } | 455 } |
| 456 | 456 |
| 457 void MojoShellConnectionImpl::AddEmbeddedService( | 457 void ServiceManagerConnectionImpl::AddEmbeddedService(const std::string& name, |
| 458 const std::string& name, | 458 const ServiceInfo& info) { |
| 459 const MojoApplicationInfo& info) { | 459 std::unique_ptr<EmbeddedServiceRunner> service( |
| 460 std::unique_ptr<EmbeddedApplicationRunner> app( | 460 new EmbeddedServiceRunner(name, info)); |
| 461 new EmbeddedApplicationRunner(name, info)); | |
| 462 AddServiceRequestHandler( | 461 AddServiceRequestHandler( |
| 463 name, base::Bind(&EmbeddedApplicationRunner::BindServiceRequest, | 462 name, base::Bind(&EmbeddedServiceRunner::BindServiceRequest, |
| 464 base::Unretained(app.get()))); | 463 base::Unretained(service.get()))); |
| 465 auto result = embedded_apps_.insert(std::make_pair(name, std::move(app))); | 464 auto result = |
| 465 embedded_services_.insert(std::make_pair(name, std::move(service))); |
| 466 DCHECK(result.second); | 466 DCHECK(result.second); |
| 467 } | 467 } |
| 468 | 468 |
| 469 void MojoShellConnectionImpl::AddServiceRequestHandler( | 469 void ServiceManagerConnectionImpl::AddServiceRequestHandler( |
| 470 const std::string& name, | 470 const std::string& name, |
| 471 const ServiceRequestHandler& handler) { | 471 const ServiceRequestHandler& handler) { |
| 472 auto result = request_handlers_.insert(std::make_pair(name, handler)); | 472 auto result = request_handlers_.insert(std::make_pair(name, handler)); |
| 473 DCHECK(result.second); | 473 DCHECK(result.second); |
| 474 } | 474 } |
| 475 | 475 |
| 476 void MojoShellConnectionImpl::CreateService( | 476 void ServiceManagerConnectionImpl::CreateService( |
| 477 shell::mojom::ServiceRequest request, | 477 shell::mojom::ServiceRequest request, |
| 478 const std::string& name) { | 478 const std::string& name) { |
| 479 auto it = request_handlers_.find(name); | 479 auto it = request_handlers_.find(name); |
| 480 if (it != request_handlers_.end()) | 480 if (it != request_handlers_.end()) |
| 481 it->second.Run(std::move(request)); | 481 it->second.Run(std::move(request)); |
| 482 } | 482 } |
| 483 | 483 |
| 484 void MojoShellConnectionImpl::OnContextInitialized( | 484 void ServiceManagerConnectionImpl::OnContextInitialized( |
| 485 const shell::Identity& identity) { | 485 const shell::Identity& identity) { |
| 486 identity_ = identity; | 486 identity_ = identity; |
| 487 if (!initialize_handler_.is_null()) | 487 if (!initialize_handler_.is_null()) |
| 488 base::ResetAndReturn(&initialize_handler_).Run(); | 488 base::ResetAndReturn(&initialize_handler_).Run(); |
| 489 } | 489 } |
| 490 | 490 |
| 491 void MojoShellConnectionImpl::OnConnectionLost() { | 491 void ServiceManagerConnectionImpl::OnConnectionLost() { |
| 492 if (!connection_lost_handler_.is_null()) | 492 if (!connection_lost_handler_.is_null()) |
| 493 connection_lost_handler_.Run(); | 493 connection_lost_handler_.Run(); |
| 494 } | 494 } |
| 495 | 495 |
| 496 void MojoShellConnectionImpl::GetInterface( | 496 void ServiceManagerConnectionImpl::GetInterface( |
| 497 shell::mojom::InterfaceProvider* provider, | 497 shell::mojom::InterfaceProvider* provider, |
| 498 const std::string& interface_name, | 498 const std::string& interface_name, |
| 499 mojo::ScopedMessagePipeHandle request_handle) { | 499 mojo::ScopedMessagePipeHandle request_handle) { |
| 500 provider->GetInterface(interface_name, std::move(request_handle)); | 500 provider->GetInterface(interface_name, std::move(request_handle)); |
| 501 } | 501 } |
| 502 | 502 |
| 503 } // namespace content | 503 } // namespace content |
| 504 | 504 |
| OLD | NEW |