| 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/shell/service_manager.h" | 5 #include "services/shell/service_manager.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 274 if (!ValidateCapabilities(target, callback)) | 274 if (!ValidateCapabilities(target, callback)) |
| 275 return; | 275 return; |
| 276 | 276 |
| 277 std::unique_ptr<ConnectParams> params(new ConnectParams); | 277 std::unique_ptr<ConnectParams> params(new ConnectParams); |
| 278 params->set_source(identity_); | 278 params->set_source(identity_); |
| 279 params->set_target(target); | 279 params->set_target(target); |
| 280 params->set_remote_interfaces(std::move(remote_interfaces)); | 280 params->set_remote_interfaces(std::move(remote_interfaces)); |
| 281 params->set_local_interfaces(std::move(local_interfaces)); | 281 params->set_local_interfaces(std::move(local_interfaces)); |
| 282 params->set_client_process_connection(std::move(client_process_connection)); | 282 params->set_client_process_connection(std::move(client_process_connection)); |
| 283 params->set_connect_callback(callback); | 283 params->set_connect_callback(callback); |
| 284 service_manager_->Connect(std::move(params)); | 284 service_manager_->Connect( |
| 285 std::move(params), nullptr, weak_factory_.GetWeakPtr()); |
| 285 } | 286 } |
| 286 | 287 |
| 287 void Clone(mojom::ConnectorRequest request) override { | 288 void Clone(mojom::ConnectorRequest request) override { |
| 288 connectors_.AddBinding(this, std::move(request)); | 289 connectors_.AddBinding(this, std::move(request)); |
| 289 } | 290 } |
| 290 | 291 |
| 291 // mojom::PIDReceiver: | 292 // mojom::PIDReceiver: |
| 292 void SetPID(uint32_t pid) override { | 293 void SetPID(uint32_t pid) override { |
| 293 PIDAvailable(pid); | 294 PIDAvailable(pid); |
| 294 } | 295 } |
| (...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 510 OnInstanceError(identity_to_instance_.begin()->second); | 511 OnInstanceError(identity_to_instance_.begin()->second); |
| 511 identity_to_resolver_.clear(); | 512 identity_to_resolver_.clear(); |
| 512 } | 513 } |
| 513 | 514 |
| 514 void ServiceManager::SetInstanceQuitCallback( | 515 void ServiceManager::SetInstanceQuitCallback( |
| 515 base::Callback<void(const Identity&)> callback) { | 516 base::Callback<void(const Identity&)> callback) { |
| 516 instance_quit_callback_ = callback; | 517 instance_quit_callback_ = callback; |
| 517 } | 518 } |
| 518 | 519 |
| 519 void ServiceManager::Connect(std::unique_ptr<ConnectParams> params) { | 520 void ServiceManager::Connect(std::unique_ptr<ConnectParams> params) { |
| 520 Connect(std::move(params), nullptr); | 521 Connect(std::move(params), nullptr, nullptr); |
| 521 } | 522 } |
| 522 | 523 |
| 523 mojom::ServiceRequest ServiceManager::StartEmbedderService( | 524 mojom::ServiceRequest ServiceManager::StartEmbedderService( |
| 524 const std::string& name) { | 525 const std::string& name) { |
| 525 std::unique_ptr<ConnectParams> params(new ConnectParams); | 526 std::unique_ptr<ConnectParams> params(new ConnectParams); |
| 526 | 527 |
| 527 Identity embedder_identity(name, mojom::kRootUserID); | 528 Identity embedder_identity(name, mojom::kRootUserID); |
| 528 params->set_source(embedder_identity); | 529 params->set_source(embedder_identity); |
| 529 params->set_target(embedder_identity); | 530 params->set_target(embedder_identity); |
| 530 | 531 |
| 531 mojom::ServicePtr service; | 532 mojom::ServicePtr service; |
| 532 mojom::ServiceRequest request = mojo::GetProxy(&service); | 533 mojom::ServiceRequest request = mojo::GetProxy(&service); |
| 533 Connect(std::move(params), std::move(service)); | 534 Connect(std::move(params), std::move(service), nullptr); |
| 534 | 535 |
| 535 return request; | 536 return request; |
| 536 } | 537 } |
| 537 | 538 |
| 538 //////////////////////////////////////////////////////////////////////////////// | 539 //////////////////////////////////////////////////////////////////////////////// |
| 539 // ServiceManager, Service implementation: | 540 // ServiceManager, Service implementation: |
| 540 | 541 |
| 541 bool ServiceManager::OnConnect(Connection* connection) { | 542 bool ServiceManager::OnConnect(Connection* connection) { |
| 542 // The only interface we expose is mojom::ServiceManager, and access to this | 543 // The only interface we expose is mojom::ServiceManager, and access to this |
| 543 // interface is brokered by a policy specific to each caller, managed by the | 544 // interface is brokered by a policy specific to each caller, managed by the |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 598 listeners_.ForAllPtrs( | 599 listeners_.ForAllPtrs( |
| 599 [this, identity](mojom::ServiceManagerListener* listener) { | 600 [this, identity](mojom::ServiceManagerListener* listener) { |
| 600 listener->OnServiceStopped(mojom::Identity::From(identity)); | 601 listener->OnServiceStopped(mojom::Identity::From(identity)); |
| 601 }); | 602 }); |
| 602 delete instance; | 603 delete instance; |
| 603 if (!instance_quit_callback_.is_null()) | 604 if (!instance_quit_callback_.is_null()) |
| 604 instance_quit_callback_.Run(identity); | 605 instance_quit_callback_.Run(identity); |
| 605 } | 606 } |
| 606 | 607 |
| 607 void ServiceManager::Connect(std::unique_ptr<ConnectParams> params, | 608 void ServiceManager::Connect(std::unique_ptr<ConnectParams> params, |
| 608 mojom::ServicePtr service) { | 609 mojom::ServicePtr service, |
| 610 base::WeakPtr<Instance> source_instance) { |
| 609 TRACE_EVENT_INSTANT1("mojo_shell", "ServiceManager::Connect", | 611 TRACE_EVENT_INSTANT1("mojo_shell", "ServiceManager::Connect", |
| 610 TRACE_EVENT_SCOPE_THREAD, "original_name", | 612 TRACE_EVENT_SCOPE_THREAD, "original_name", |
| 611 params->target().name()); | 613 params->target().name()); |
| 612 DCHECK(IsValidName(params->target().name())); | 614 DCHECK(IsValidName(params->target().name())); |
| 613 DCHECK(base::IsValidGUID(params->target().user_id())); | 615 DCHECK(base::IsValidGUID(params->target().user_id())); |
| 614 DCHECK_NE(mojom::kInheritUserID, params->target().user_id()); | 616 DCHECK_NE(mojom::kInheritUserID, params->target().user_id()); |
| 615 DCHECK(!service.is_bound() || !identity_to_instance_.count(params->target())); | 617 DCHECK(!service.is_bound() || !identity_to_instance_.count(params->target())); |
| 616 | 618 |
| 617 // Connect to an existing matching instance, if possible. | 619 // Connect to an existing matching instance, if possible. |
| 618 if (!service.is_bound() && ConnectToExistingInstance(¶ms)) | 620 if (!service.is_bound() && ConnectToExistingInstance(¶ms)) |
| 619 return; | 621 return; |
| 620 | 622 |
| 621 // The catalog needs to see the source identity as that of the originating | 623 // The catalog needs to see the source identity as that of the originating |
| 622 // app so it loads the correct store. Since the catalog is itself run as root | 624 // app so it loads the correct store. Since the catalog is itself run as root |
| 623 // when this re-enters Connect() it'll be handled by | 625 // when this re-enters Connect() it'll be handled by |
| 624 // ConnectToExistingInstance(). | 626 // ConnectToExistingInstance(). |
| 625 mojom::Resolver* resolver = | 627 mojom::Resolver* resolver = |
| 626 GetResolver(Identity(kServiceManagerName, params->target().user_id())); | 628 GetResolver(Identity(kServiceManagerName, params->target().user_id())); |
| 627 | 629 |
| 628 std::string name = params->target().name(); | 630 std::string name = params->target().name(); |
| 629 resolver->ResolveMojoName( | 631 resolver->ResolveMojoName( |
| 630 name, base::Bind(&shell::ServiceManager::OnGotResolvedName, | 632 name, base::Bind(&shell::ServiceManager::OnGotResolvedName, |
| 631 weak_ptr_factory_.GetWeakPtr(), base::Passed(¶ms), | 633 weak_ptr_factory_.GetWeakPtr(), base::Passed(¶ms), |
| 632 base::Passed(&service))); | 634 base::Passed(&service), !!source_instance, |
| 635 source_instance)); |
| 633 } | 636 } |
| 634 | 637 |
| 635 ServiceManager::Instance* ServiceManager::GetExistingInstance( | 638 ServiceManager::Instance* ServiceManager::GetExistingInstance( |
| 636 const Identity& identity) const { | 639 const Identity& identity) const { |
| 637 const auto& it = identity_to_instance_.find(identity); | 640 const auto& it = identity_to_instance_.find(identity); |
| 638 Instance* instance = it != identity_to_instance_.end() ? it->second : nullptr; | 641 Instance* instance = it != identity_to_instance_.end() ? it->second : nullptr; |
| 639 if (instance) | 642 if (instance) |
| 640 return instance; | 643 return instance; |
| 641 | 644 |
| 642 if (singletons_.find(identity.name()) != singletons_.end()) { | 645 if (singletons_.find(identity.name()) != singletons_.end()) { |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 720 | 723 |
| 721 void ServiceManager::OnServiceFactoryLost(const Identity& which) { | 724 void ServiceManager::OnServiceFactoryLost(const Identity& which) { |
| 722 // Remove the mapping. | 725 // Remove the mapping. |
| 723 auto it = service_factories_.find(which); | 726 auto it = service_factories_.find(which); |
| 724 DCHECK(it != service_factories_.end()); | 727 DCHECK(it != service_factories_.end()); |
| 725 service_factories_.erase(it); | 728 service_factories_.erase(it); |
| 726 } | 729 } |
| 727 | 730 |
| 728 void ServiceManager::OnGotResolvedName(std::unique_ptr<ConnectParams> params, | 731 void ServiceManager::OnGotResolvedName(std::unique_ptr<ConnectParams> params, |
| 729 mojom::ServicePtr service, | 732 mojom::ServicePtr service, |
| 733 bool has_source_instance, |
| 734 base::WeakPtr<Instance> source_instance, |
| 730 mojom::ResolveResultPtr result) { | 735 mojom::ResolveResultPtr result) { |
| 736 // If this request was originated by a specific Instance and that Instance is |
| 737 // no longer around, we ignore this response. |
| 738 if (has_source_instance && !source_instance) |
| 739 return; |
| 740 |
| 731 std::string instance_name = params->target().instance(); | 741 std::string instance_name = params->target().instance(); |
| 732 if (instance_name == GetNamePath(params->target().name()) && | 742 if (instance_name == GetNamePath(params->target().name()) && |
| 733 result->qualifier != GetNamePath(result->resolved_name)) { | 743 result->qualifier != GetNamePath(result->resolved_name)) { |
| 734 instance_name = result->qualifier; | 744 instance_name = result->qualifier; |
| 735 } | 745 } |
| 736 Identity target(params->target().name(), params->target().user_id(), | 746 Identity target(params->target().name(), params->target().user_id(), |
| 737 instance_name); | 747 instance_name); |
| 738 params->set_target(target); | 748 params->set_target(target); |
| 739 | 749 |
| 740 // It's possible that when this manifest request was issued, another one was | 750 // It's possible that when this manifest request was issued, another one was |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 783 std::move(client_process_connection)); | 793 std::move(client_process_connection)); |
| 784 } else { | 794 } else { |
| 785 // Otherwise we create a new Service pipe. | 795 // Otherwise we create a new Service pipe. |
| 786 mojom::ServiceRequest request = GetProxy(&service); | 796 mojom::ServiceRequest request = GetProxy(&service); |
| 787 CHECK(!result->package_path.empty() && !result->capabilities.is_null()); | 797 CHECK(!result->package_path.empty() && !result->capabilities.is_null()); |
| 788 | 798 |
| 789 if (target.name() != result->resolved_name) { | 799 if (target.name() != result->resolved_name) { |
| 790 instance->StartWithService(std::move(service)); | 800 instance->StartWithService(std::move(service)); |
| 791 Identity factory(result->resolved_name, target.user_id(), | 801 Identity factory(result->resolved_name, target.user_id(), |
| 792 instance_name); | 802 instance_name); |
| 793 CreateServiceWithFactory(factory, target.name(), | 803 CreateServiceWithFactory(factory, target.name(), std::move(request)); |
| 794 std::move(request)); | |
| 795 } else { | 804 } else { |
| 796 instance->StartWithFilePath(result->package_path); | 805 instance->StartWithFilePath(result->package_path); |
| 797 } | 806 } |
| 798 } | 807 } |
| 799 | 808 |
| 800 // Now that the instance has a Service, we can connect to it. | 809 // Now that the instance has a Service, we can connect to it. |
| 801 bool connected = instance->ConnectToService(¶ms); | 810 bool connected = instance->ConnectToService(¶ms); |
| 802 DCHECK(connected); | 811 DCHECK(connected); |
| 803 } | 812 } |
| 804 | 813 |
| 805 base::WeakPtr<ServiceManager> ServiceManager::GetWeakPtr() { | 814 base::WeakPtr<ServiceManager> ServiceManager::GetWeakPtr() { |
| 806 return weak_ptr_factory_.GetWeakPtr(); | 815 return weak_ptr_factory_.GetWeakPtr(); |
| 807 } | 816 } |
| 808 | 817 |
| 809 } // namespace shell | 818 } // namespace shell |
| OLD | NEW |