Chromium Code Reviews| 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()) { | |
|
Ken Rockot(use gerrit already)
2017/05/11 02:32:08
nit: no {} necessary
leonhsl(Using Gerrit)
2017/05/11 03:10:04
Done.
| |
| 64 return; | |
| 65 } | |
| 66 | |
| 67 g_overridden_binder_registries.Get()->erase(service_name); | |
| 68 } | |
| 69 | |
| 20 ServiceContext::ServiceContext( | 70 ServiceContext::ServiceContext( |
| 21 std::unique_ptr<service_manager::Service> service, | 71 std::unique_ptr<service_manager::Service> service, |
| 22 mojom::ServiceRequest request, | 72 mojom::ServiceRequest request, |
| 23 std::unique_ptr<Connector> connector, | 73 std::unique_ptr<Connector> connector, |
| 24 mojom::ConnectorRequest connector_request) | 74 mojom::ConnectorRequest connector_request) |
| 25 : pending_connector_request_(std::move(connector_request)), | 75 : pending_connector_request_(std::move(connector_request)), |
| 26 service_(std::move(service)), | 76 service_(std::move(service)), |
| 27 binding_(this, std::move(request)), | 77 binding_(this, std::move(request)), |
| 28 connector_(std::move(connector)), | 78 connector_(std::move(connector)), |
| 29 weak_factory_(this) { | 79 weak_factory_(this) { |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 76 } | 126 } |
| 77 | 127 |
| 78 void ServiceContext::OnBindInterface( | 128 void ServiceContext::OnBindInterface( |
| 79 const BindSourceInfo& source_info, | 129 const BindSourceInfo& source_info, |
| 80 const std::string& interface_name, | 130 const std::string& interface_name, |
| 81 mojo::ScopedMessagePipeHandle interface_pipe, | 131 mojo::ScopedMessagePipeHandle interface_pipe, |
| 82 const OnBindInterfaceCallback& callback) { | 132 const OnBindInterfaceCallback& callback) { |
| 83 // Acknowledge the request regardless of whether it's accepted. | 133 // Acknowledge the request regardless of whether it's accepted. |
| 84 callback.Run(); | 134 callback.Run(); |
| 85 | 135 |
| 136 BinderRegistry* global_registry = | |
| 137 GetGlobalBinderRegistryForService(identity_.name()); | |
| 138 if (global_registry && global_registry->CanBindInterface(interface_name)) { | |
| 139 // Just use the binder overridden globally. | |
| 140 global_registry->BindInterface(source_info, interface_name, | |
| 141 std::move(interface_pipe)); | |
| 142 return; | |
| 143 } | |
| 86 service_->OnBindInterface(source_info, interface_name, | 144 service_->OnBindInterface(source_info, interface_name, |
| 87 std::move(interface_pipe)); | 145 std::move(interface_pipe)); |
| 88 } | 146 } |
| 89 | 147 |
| 90 //////////////////////////////////////////////////////////////////////////////// | 148 //////////////////////////////////////////////////////////////////////////////// |
| 91 // ServiceContext, private: | 149 // ServiceContext, private: |
| 92 | 150 |
| 93 void ServiceContext::OnConnectionError() { | 151 void ServiceContext::OnConnectionError() { |
| 94 if (service_->OnServiceManagerConnectionLost()) { | 152 if (service_->OnServiceManagerConnectionLost()) { |
| 95 // CAUTION: May delete |this|. | 153 // CAUTION: May delete |this|. |
| 96 QuitNow(); | 154 QuitNow(); |
| 97 } | 155 } |
| 98 } | 156 } |
| 99 | 157 |
| 100 } // namespace service_manager | 158 } // namespace service_manager |
| OLD | NEW |