| 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/public/cpp/service_context.h" | 5 #include "services/service_manager/public/cpp/service_context.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/lazy_instance.h" |
| 10 #include "base/logging.h" | 11 #include "base/logging.h" |
| 11 #include "mojo/public/cpp/bindings/interface_request.h" | 12 #include "mojo/public/cpp/bindings/interface_request.h" |
| 12 #include "services/service_manager/public/cpp/connector.h" | 13 #include "services/service_manager/public/cpp/connector.h" |
| 13 #include "services/service_manager/public/cpp/service.h" | 14 #include "services/service_manager/public/cpp/service.h" |
| 14 | 15 |
| 15 namespace service_manager { | 16 namespace service_manager { |
| 16 | 17 |
| 18 namespace { |
| 19 |
| 20 using ServiceNameToBinderRegistryMap = std::map<std::string, BinderRegistry>; |
| 21 |
| 22 base::LazyInstance<std::unique_ptr<ServiceNameToBinderRegistryMap>>::Leaky |
| 23 g_overridden_binder_registries = LAZY_INSTANCE_INITIALIZER; |
| 24 |
| 25 // Returns the overridden binder registry which intercepts interface bind |
| 26 // requests to all |service_name| service instances, returns nullptr if no such |
| 27 // one. |
| 28 BinderRegistry* GetGlobalBinderRegistryForService( |
| 29 const std::string& service_name) { |
| 30 const auto& registries = g_overridden_binder_registries.Get(); |
| 31 if (registries) { |
| 32 auto it = registries->find(service_name); |
| 33 if (it != registries->end()) |
| 34 return &it->second; |
| 35 } |
| 36 |
| 37 return nullptr; |
| 38 } |
| 39 |
| 40 } // namespace |
| 41 |
| 17 //////////////////////////////////////////////////////////////////////////////// | 42 //////////////////////////////////////////////////////////////////////////////// |
| 18 // ServiceContext, public: | 43 // ServiceContext, public: |
| 19 | 44 |
| 45 // static |
| 46 void ServiceContext::SetGlobalBinderForTesting( |
| 47 const std::string& service_name, |
| 48 const std::string& interface_name, |
| 49 const BinderRegistry::Binder& binder, |
| 50 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) { |
| 51 if (!g_overridden_binder_registries.Get()) { |
| 52 g_overridden_binder_registries.Get() = |
| 53 base::MakeUnique<ServiceNameToBinderRegistryMap>(); |
| 54 } |
| 55 |
| 56 (*g_overridden_binder_registries.Get())[service_name].AddInterface( |
| 57 interface_name, binder, task_runner); |
| 58 } |
| 59 |
| 60 // static |
| 61 void ServiceContext::ClearGlobalBindersForTesting( |
| 62 const std::string& service_name) { |
| 63 if (!g_overridden_binder_registries.Get()) |
| 64 return; |
| 65 |
| 66 g_overridden_binder_registries.Get()->erase(service_name); |
| 67 } |
| 68 |
| 20 ServiceContext::ServiceContext( | 69 ServiceContext::ServiceContext( |
| 21 std::unique_ptr<service_manager::Service> service, | 70 std::unique_ptr<service_manager::Service> service, |
| 22 mojom::ServiceRequest request, | 71 mojom::ServiceRequest request, |
| 23 std::unique_ptr<Connector> connector, | 72 std::unique_ptr<Connector> connector, |
| 24 mojom::ConnectorRequest connector_request) | 73 mojom::ConnectorRequest connector_request) |
| 25 : pending_connector_request_(std::move(connector_request)), | 74 : pending_connector_request_(std::move(connector_request)), |
| 26 service_(std::move(service)), | 75 service_(std::move(service)), |
| 27 binding_(this, std::move(request)), | 76 binding_(this, std::move(request)), |
| 28 connector_(std::move(connector)), | 77 connector_(std::move(connector)), |
| 29 weak_factory_(this) { | 78 weak_factory_(this) { |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 } | 125 } |
| 77 | 126 |
| 78 void ServiceContext::OnBindInterface( | 127 void ServiceContext::OnBindInterface( |
| 79 const BindSourceInfo& source_info, | 128 const BindSourceInfo& source_info, |
| 80 const std::string& interface_name, | 129 const std::string& interface_name, |
| 81 mojo::ScopedMessagePipeHandle interface_pipe, | 130 mojo::ScopedMessagePipeHandle interface_pipe, |
| 82 const OnBindInterfaceCallback& callback) { | 131 const OnBindInterfaceCallback& callback) { |
| 83 // Acknowledge the request regardless of whether it's accepted. | 132 // Acknowledge the request regardless of whether it's accepted. |
| 84 callback.Run(); | 133 callback.Run(); |
| 85 | 134 |
| 135 BinderRegistry* global_registry = |
| 136 GetGlobalBinderRegistryForService(identity_.name()); |
| 137 if (global_registry && global_registry->CanBindInterface(interface_name)) { |
| 138 // Just use the binder overridden globally. |
| 139 global_registry->BindInterface(source_info, interface_name, |
| 140 std::move(interface_pipe)); |
| 141 return; |
| 142 } |
| 86 service_->OnBindInterface(source_info, interface_name, | 143 service_->OnBindInterface(source_info, interface_name, |
| 87 std::move(interface_pipe)); | 144 std::move(interface_pipe)); |
| 88 } | 145 } |
| 89 | 146 |
| 90 //////////////////////////////////////////////////////////////////////////////// | 147 //////////////////////////////////////////////////////////////////////////////// |
| 91 // ServiceContext, private: | 148 // ServiceContext, private: |
| 92 | 149 |
| 93 void ServiceContext::OnConnectionError() { | 150 void ServiceContext::OnConnectionError() { |
| 94 if (service_->OnServiceManagerConnectionLost()) { | 151 if (service_->OnServiceManagerConnectionLost()) { |
| 95 // CAUTION: May delete |this|. | 152 // CAUTION: May delete |this|. |
| 96 QuitNow(); | 153 QuitNow(); |
| 97 } | 154 } |
| 98 } | 155 } |
| 99 | 156 |
| 100 } // namespace service_manager | 157 } // namespace service_manager |
| OLD | NEW |