OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "services/service_manager/service_manager.h" | 5 #include "services/service_manager/service_manager.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
(...skipping 11 matching lines...) Expand all Loading... |
22 #include "mojo/public/cpp/bindings/binding.h" | 22 #include "mojo/public/cpp/bindings/binding.h" |
23 #include "mojo/public/cpp/bindings/binding_set.h" | 23 #include "mojo/public/cpp/bindings/binding_set.h" |
24 #include "services/service_manager/connect_util.h" | 24 #include "services/service_manager/connect_util.h" |
25 #include "services/service_manager/public/cpp/connector.h" | 25 #include "services/service_manager/public/cpp/connector.h" |
26 #include "services/service_manager/public/cpp/names.h" | 26 #include "services/service_manager/public/cpp/names.h" |
27 #include "services/service_manager/public/cpp/service_context.h" | 27 #include "services/service_manager/public/cpp/service_context.h" |
28 #include "services/service_manager/public/interfaces/connector.mojom.h" | 28 #include "services/service_manager/public/interfaces/connector.mojom.h" |
29 #include "services/service_manager/public/interfaces/service.mojom.h" | 29 #include "services/service_manager/public/interfaces/service.mojom.h" |
30 #include "services/service_manager/public/interfaces/service_manager.mojom.h" | 30 #include "services/service_manager/public/interfaces/service_manager.mojom.h" |
31 | 31 |
32 namespace shell { | 32 namespace service_manager { |
33 | 33 |
34 namespace { | 34 namespace { |
35 | 35 |
36 const char kCatalogName[] = "service:catalog"; | 36 const char kCatalogName[] = "service:catalog"; |
37 const char kServiceManagerName[] = "service:shell"; | 37 const char kServiceManagerName[] = "service:shell"; |
38 const char kCapabilityClass_UserID[] = "shell:user_id"; | 38 const char kCapabilityClass_UserID[] = "shell:user_id"; |
39 const char kCapabilityClass_ClientProcess[] = "shell:client_process"; | 39 const char kCapabilityClass_ClientProcess[] = "shell:client_process"; |
40 const char kCapabilityClass_InstanceName[] = "shell:instance_name"; | 40 const char kCapabilityClass_InstanceName[] = "shell:instance_name"; |
41 const char kCapabilityClass_AllUsers[] = "shell:all_users"; | 41 const char kCapabilityClass_AllUsers[] = "shell:all_users"; |
42 const char kCapabilityClass_ExplicitClass[] = "shell:explicit_class"; | 42 const char kCapabilityClass_ExplicitClass[] = "shell:explicit_class"; |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 | 108 |
109 // Encapsulates a connection to an instance of a service, tracked by the | 109 // Encapsulates a connection to an instance of a service, tracked by the |
110 // Service Manager. | 110 // Service Manager. |
111 class ServiceManager::Instance | 111 class ServiceManager::Instance |
112 : public mojom::Connector, | 112 : public mojom::Connector, |
113 public mojom::PIDReceiver, | 113 public mojom::PIDReceiver, |
114 public Service, | 114 public Service, |
115 public InterfaceFactory<mojom::ServiceManager>, | 115 public InterfaceFactory<mojom::ServiceManager>, |
116 public mojom::ServiceManager { | 116 public mojom::ServiceManager { |
117 public: | 117 public: |
118 Instance(shell::ServiceManager* service_manager, | 118 Instance(service_manager::ServiceManager* service_manager, |
119 const Identity& identity, | 119 const Identity& identity, |
120 const CapabilitySpec& capability_spec) | 120 const CapabilitySpec& capability_spec) |
121 : service_manager_(service_manager), | 121 : service_manager_(service_manager), |
122 id_(GenerateUniqueID()), | 122 id_(GenerateUniqueID()), |
123 identity_(identity), | 123 identity_(identity), |
124 capability_spec_(capability_spec), | 124 capability_spec_(capability_spec), |
125 allow_any_application_(capability_spec.required.count("*") == 1), | 125 allow_any_application_(capability_spec.required.count("*") == 1), |
126 pid_receiver_binding_(this), | 126 pid_receiver_binding_(this), |
127 weak_factory_(this) { | 127 weak_factory_(this) { |
128 if (identity_.name() == kServiceManagerName || | 128 if (identity_.name() == kServiceManagerName || |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
252 DCHECK(source); | 252 DCHECK(source); |
253 if (HasClass(source->capability_spec_, kCapabilityClass_ServiceManager)) { | 253 if (HasClass(source->capability_spec_, kCapabilityClass_ServiceManager)) { |
254 registry->AddInterface<mojom::ServiceManager>(this); | 254 registry->AddInterface<mojom::ServiceManager>(this); |
255 return true; | 255 return true; |
256 } | 256 } |
257 return false; | 257 return false; |
258 } | 258 } |
259 | 259 |
260 private: | 260 private: |
261 // mojom::Connector implementation: | 261 // mojom::Connector implementation: |
262 void Connect(const shell::Identity& in_target, | 262 void Connect(const service_manager::Identity& in_target, |
263 mojom::InterfaceProviderRequest remote_interfaces, | 263 mojom::InterfaceProviderRequest remote_interfaces, |
264 mojom::ClientProcessConnectionPtr client_process_connection, | 264 mojom::ClientProcessConnectionPtr client_process_connection, |
265 const ConnectCallback& callback) override { | 265 const ConnectCallback& callback) override { |
266 Identity target = in_target; | 266 Identity target = in_target; |
267 if (target.user_id() == mojom::kInheritUserID) | 267 if (target.user_id() == mojom::kInheritUserID) |
268 target.set_user_id(identity_.user_id()); | 268 target.set_user_id(identity_.user_id()); |
269 | 269 |
270 if (!ValidateIdentity(target, callback)) | 270 if (!ValidateIdentity(target, callback)) |
271 return; | 271 return; |
272 if (!ValidateClientProcessConnection(&client_process_connection, target, | 272 if (!ValidateClientProcessConnection(&client_process_connection, target, |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
409 | 409 |
410 void PIDAvailable(base::ProcessId pid) { | 410 void PIDAvailable(base::ProcessId pid) { |
411 if (pid == base::kNullProcessId) { | 411 if (pid == base::kNullProcessId) { |
412 service_manager_->OnInstanceError(this); | 412 service_manager_->OnInstanceError(this); |
413 return; | 413 return; |
414 } | 414 } |
415 pid_ = pid; | 415 pid_ = pid; |
416 service_manager_->NotifyPIDAvailable(identity_, pid_); | 416 service_manager_->NotifyPIDAvailable(identity_, pid_); |
417 } | 417 } |
418 | 418 |
419 void OnServiceLost(base::WeakPtr<shell::ServiceManager> service_manager) { | 419 void OnServiceLost( |
| 420 base::WeakPtr<service_manager::ServiceManager> service_manager) { |
420 service_.reset(); | 421 service_.reset(); |
421 OnConnectionLost(service_manager); | 422 OnConnectionLost(service_manager); |
422 } | 423 } |
423 | 424 |
424 void OnConnectionLost(base::WeakPtr<shell::ServiceManager> service_manager) { | 425 void OnConnectionLost( |
| 426 base::WeakPtr<service_manager::ServiceManager> service_manager) { |
425 // Any time a Connector is lost or we lose the Service connection, it | 427 // Any time a Connector is lost or we lose the Service connection, it |
426 // may have been the last pipe using this Instance. If so, clean up. | 428 // may have been the last pipe using this Instance. If so, clean up. |
427 if (service_manager && !service_) { | 429 if (service_manager && !service_) { |
428 if (connectors_.empty()) | 430 if (connectors_.empty()) |
429 service_manager->OnInstanceError(this); | 431 service_manager->OnInstanceError(this); |
430 else | 432 else |
431 service_manager->OnInstanceUnreachable(this); | 433 service_manager->OnInstanceUnreachable(this); |
432 } | 434 } |
433 } | 435 } |
434 | 436 |
435 void OnInitializeResponse(mojom::ConnectorRequest connector_request) { | 437 void OnInitializeResponse(mojom::ConnectorRequest connector_request) { |
436 if (connector_request.is_pending()) { | 438 if (connector_request.is_pending()) { |
437 connectors_.AddBinding(this, std::move(connector_request)); | 439 connectors_.AddBinding(this, std::move(connector_request)); |
438 connectors_.set_connection_error_handler( | 440 connectors_.set_connection_error_handler( |
439 base::Bind(&Instance::OnConnectionLost, base::Unretained(this), | 441 base::Bind(&Instance::OnConnectionLost, base::Unretained(this), |
440 service_manager_->GetWeakPtr())); | 442 service_manager_->GetWeakPtr())); |
441 } | 443 } |
442 } | 444 } |
443 | 445 |
444 // Callback when NativeRunner completes. | 446 // Callback when NativeRunner completes. |
445 void OnRunnerCompleted() { | 447 void OnRunnerCompleted() { |
446 if (!runner_.get()) | 448 if (!runner_.get()) |
447 return; // We're in the destructor. | 449 return; // We're in the destructor. |
448 | 450 |
449 service_manager_->OnInstanceError(this); | 451 service_manager_->OnInstanceError(this); |
450 } | 452 } |
451 | 453 |
452 shell::ServiceManager* const service_manager_; | 454 service_manager::ServiceManager* const service_manager_; |
453 | 455 |
454 // An id that identifies this instance. Distinct from pid, as a single process | 456 // An id that identifies this instance. Distinct from pid, as a single process |
455 // may vend multiple application instances, and this object may exist before a | 457 // may vend multiple application instances, and this object may exist before a |
456 // process is launched. | 458 // process is launched. |
457 const uint32_t id_; | 459 const uint32_t id_; |
458 const Identity identity_; | 460 const Identity identity_; |
459 const CapabilitySpec capability_spec_; | 461 const CapabilitySpec capability_spec_; |
460 const bool allow_any_application_; | 462 const bool allow_any_application_; |
461 std::unique_ptr<NativeRunner> runner_; | 463 std::unique_ptr<NativeRunner> runner_; |
462 mojom::ServicePtr service_; | 464 mojom::ServicePtr service_; |
(...skipping 28 matching lines...) Expand all Loading... |
491 ServiceManager::ServiceManager( | 493 ServiceManager::ServiceManager( |
492 std::unique_ptr<NativeRunnerFactory> native_runner_factory, | 494 std::unique_ptr<NativeRunnerFactory> native_runner_factory, |
493 mojom::ServicePtr catalog) | 495 mojom::ServicePtr catalog) |
494 : native_runner_factory_(std::move(native_runner_factory)), | 496 : native_runner_factory_(std::move(native_runner_factory)), |
495 weak_ptr_factory_(this) { | 497 weak_ptr_factory_(this) { |
496 mojom::ServicePtr service; | 498 mojom::ServicePtr service; |
497 mojom::ServiceRequest request = mojo::GetProxy(&service); | 499 mojom::ServiceRequest request = mojo::GetProxy(&service); |
498 | 500 |
499 CapabilitySpec spec; | 501 CapabilitySpec spec; |
500 spec.provided[kCapabilityClass_ServiceManager].insert( | 502 spec.provided[kCapabilityClass_ServiceManager].insert( |
501 "shell::mojom::ServiceManager"); | 503 "service_manager::mojom::ServiceManager"); |
502 spec.required["*"].insert("shell:service_factory"); | 504 spec.required["*"].insert("shell:service_factory"); |
503 spec.required["service:catalog"].insert("shell:resolver"); | 505 spec.required["service:catalog"].insert("shell:resolver"); |
504 | 506 |
505 service_manager_instance_ = | 507 service_manager_instance_ = |
506 CreateInstance(Identity(), CreateServiceManagerIdentity(), spec); | 508 CreateInstance(Identity(), CreateServiceManagerIdentity(), spec); |
507 service_manager_instance_->StartWithService(std::move(service)); | 509 service_manager_instance_->StartWithService(std::move(service)); |
508 singletons_.insert(kServiceManagerName); | 510 singletons_.insert(kServiceManagerName); |
509 service_context_.reset(new ServiceContext(this, std::move(request))); | 511 service_context_.reset(new ServiceContext(this, std::move(request))); |
510 | 512 |
511 if (catalog) | 513 if (catalog) |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
575 | 577 |
576 //////////////////////////////////////////////////////////////////////////////// | 578 //////////////////////////////////////////////////////////////////////////////// |
577 // ServiceManager, private: | 579 // ServiceManager, private: |
578 | 580 |
579 void ServiceManager::InitCatalog(mojom::ServicePtr catalog) { | 581 void ServiceManager::InitCatalog(mojom::ServicePtr catalog) { |
580 // TODO(beng): It'd be great to build this from the manifest, however there's | 582 // TODO(beng): It'd be great to build this from the manifest, however there's |
581 // a bit of a chicken-and-egg problem. | 583 // a bit of a chicken-and-egg problem. |
582 CapabilitySpec spec; | 584 CapabilitySpec spec; |
583 spec.provided["app"].insert("filesystem::mojom::Directory"); | 585 spec.provided["app"].insert("filesystem::mojom::Directory"); |
584 spec.provided["catalog:catalog"].insert("catalog::mojom::Catalog"); | 586 spec.provided["catalog:catalog"].insert("catalog::mojom::Catalog"); |
585 spec.provided["shell:resolver"].insert("shell::mojom::Resolver"); | 587 spec.provided["shell:resolver"].insert("service_manager::mojom::Resolver"); |
586 spec.provided["control"].insert("catalog::mojom::CatalogControl"); | 588 spec.provided["control"].insert("catalog::mojom::CatalogControl"); |
587 Instance* instance = CreateInstance( | 589 Instance* instance = CreateInstance( |
588 CreateServiceManagerIdentity(), CreateCatalogIdentity(), spec); | 590 CreateServiceManagerIdentity(), CreateCatalogIdentity(), spec); |
589 singletons_.insert(kCatalogName); | 591 singletons_.insert(kCatalogName); |
590 instance->StartWithService(std::move(catalog)); | 592 instance->StartWithService(std::move(catalog)); |
591 } | 593 } |
592 | 594 |
593 mojom::Resolver* ServiceManager::GetResolver(const Identity& identity) { | 595 mojom::Resolver* ServiceManager::GetResolver(const Identity& identity) { |
594 auto iter = identity_to_resolver_.find(identity); | 596 auto iter = identity_to_resolver_.find(identity); |
595 if (iter != identity_to_resolver_.end()) | 597 if (iter != identity_to_resolver_.end()) |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
654 | 656 |
655 // The catalog needs to see the source identity as that of the originating | 657 // The catalog needs to see the source identity as that of the originating |
656 // app so it loads the correct store. Since the catalog is itself run as root | 658 // app so it loads the correct store. Since the catalog is itself run as root |
657 // when this re-enters Connect() it'll be handled by | 659 // when this re-enters Connect() it'll be handled by |
658 // ConnectToExistingInstance(). | 660 // ConnectToExistingInstance(). |
659 mojom::Resolver* resolver = | 661 mojom::Resolver* resolver = |
660 GetResolver(Identity(kServiceManagerName, params->target().user_id())); | 662 GetResolver(Identity(kServiceManagerName, params->target().user_id())); |
661 | 663 |
662 std::string name = params->target().name(); | 664 std::string name = params->target().name(); |
663 resolver->ResolveMojoName( | 665 resolver->ResolveMojoName( |
664 name, base::Bind(&shell::ServiceManager::OnGotResolvedName, | 666 name, |
665 weak_ptr_factory_.GetWeakPtr(), base::Passed(¶ms), | 667 base::Bind(&service_manager::ServiceManager::OnGotResolvedName, |
666 base::Passed(&service), !!source_instance, | 668 weak_ptr_factory_.GetWeakPtr(), base::Passed(¶ms), |
667 source_instance)); | 669 base::Passed(&service), !!source_instance, source_instance)); |
668 } | 670 } |
669 | 671 |
670 ServiceManager::Instance* ServiceManager::GetExistingInstance( | 672 ServiceManager::Instance* ServiceManager::GetExistingInstance( |
671 const Identity& identity) const { | 673 const Identity& identity) const { |
672 const auto& it = identity_to_instance_.find(identity); | 674 const auto& it = identity_to_instance_.find(identity); |
673 Instance* instance = it != identity_to_instance_.end() ? it->second : nullptr; | 675 Instance* instance = it != identity_to_instance_.end() ? it->second : nullptr; |
674 if (instance) | 676 if (instance) |
675 return instance; | 677 return instance; |
676 | 678 |
677 if (singletons_.find(identity.name()) != singletons_.end()) { | 679 if (singletons_.find(identity.name()) != singletons_.end()) { |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
751 const Identity& service_factory_identity) { | 753 const Identity& service_factory_identity) { |
752 auto it = service_factories_.find(service_factory_identity); | 754 auto it = service_factories_.find(service_factory_identity); |
753 if (it != service_factories_.end()) | 755 if (it != service_factories_.end()) |
754 return it->second.get(); | 756 return it->second.get(); |
755 | 757 |
756 Identity source_identity(kServiceManagerName, mojom::kInheritUserID); | 758 Identity source_identity(kServiceManagerName, mojom::kInheritUserID); |
757 mojom::ServiceFactoryPtr factory; | 759 mojom::ServiceFactoryPtr factory; |
758 ConnectToInterface(this, source_identity, service_factory_identity, | 760 ConnectToInterface(this, source_identity, service_factory_identity, |
759 &factory); | 761 &factory); |
760 mojom::ServiceFactory* factory_interface = factory.get(); | 762 mojom::ServiceFactory* factory_interface = factory.get(); |
761 factory.set_connection_error_handler(base::Bind( | 763 factory.set_connection_error_handler( |
762 &shell::ServiceManager::OnServiceFactoryLost, | 764 base::Bind(&service_manager::ServiceManager::OnServiceFactoryLost, |
763 weak_ptr_factory_.GetWeakPtr(), service_factory_identity)); | 765 weak_ptr_factory_.GetWeakPtr(), service_factory_identity)); |
764 service_factories_[service_factory_identity] = std::move(factory); | 766 service_factories_[service_factory_identity] = std::move(factory); |
765 return factory_interface; | 767 return factory_interface; |
766 } | 768 } |
767 | 769 |
768 void ServiceManager::OnServiceFactoryLost(const Identity& which) { | 770 void ServiceManager::OnServiceFactoryLost(const Identity& which) { |
769 // Remove the mapping. | 771 // Remove the mapping. |
770 auto it = service_factories_.find(which); | 772 auto it = service_factories_.find(which); |
771 DCHECK(it != service_factories_.end()); | 773 DCHECK(it != service_factories_.end()); |
772 service_factories_.erase(it); | 774 service_factories_.erase(it); |
773 } | 775 } |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
873 | 875 |
874 // Now that the instance has a Service, we can connect to it. | 876 // Now that the instance has a Service, we can connect to it. |
875 bool connected = instance->ConnectToService(¶ms); | 877 bool connected = instance->ConnectToService(¶ms); |
876 DCHECK(connected); | 878 DCHECK(connected); |
877 } | 879 } |
878 | 880 |
879 base::WeakPtr<ServiceManager> ServiceManager::GetWeakPtr() { | 881 base::WeakPtr<ServiceManager> ServiceManager::GetWeakPtr() { |
880 return weak_ptr_factory_.GetWeakPtr(); | 882 return weak_ptr_factory_.GetWeakPtr(); |
881 } | 883 } |
882 | 884 |
883 } // namespace shell | 885 } // namespace service_manager |
OLD | NEW |