| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef SERVICES_SERVICE_MANAGER_PUBLIC_CPP_INTERFACE_REGISTRY_H_ | |
| 6 #define SERVICES_SERVICE_MANAGER_PUBLIC_CPP_INTERFACE_REGISTRY_H_ | |
| 7 | |
| 8 #include <list> | |
| 9 #include <memory> | |
| 10 #include <queue> | |
| 11 #include <set> | |
| 12 #include <utility> | |
| 13 | |
| 14 #include "base/callback.h" | |
| 15 #include "base/memory/ptr_util.h" | |
| 16 #include "mojo/public/cpp/bindings/binding.h" | |
| 17 #include "services/service_manager/public/cpp/identity.h" | |
| 18 #include "services/service_manager/public/cpp/interface_provider_spec.h" | |
| 19 #include "services/service_manager/public/cpp/lib/callback_binder.h" | |
| 20 #include "services/service_manager/public/cpp/lib/interface_factory_binder.h" | |
| 21 #include "services/service_manager/public/interfaces/interface_provider.mojom.h" | |
| 22 | |
| 23 namespace service_manager { | |
| 24 class Connection; | |
| 25 class InterfaceBinder; | |
| 26 | |
| 27 // Returns the set of capabilities required from the target. | |
| 28 CapabilitySet GetRequestedCapabilities(const InterfaceProviderSpec& source_spec, | |
| 29 const Identity& target); | |
| 30 | |
| 31 // Generates a single set of interfaces that is the union of all interfaces | |
| 32 // exposed by the target for the capabilities requested by the source. | |
| 33 InterfaceSet GetInterfacesToExpose(const InterfaceProviderSpec& source_spec, | |
| 34 const Identity& target, | |
| 35 const InterfaceProviderSpec& target_spec); | |
| 36 | |
| 37 // An implementation of mojom::InterfaceProvider that allows the user to | |
| 38 // register services to be exposed to another application. | |
| 39 // | |
| 40 // To use, define a class that implements your specific interface. Then | |
| 41 // implement an InterfaceFactory<Foo> that binds instances of FooImpl to | |
| 42 // InterfaceRequest<Foo>s and register that on the registry like this: | |
| 43 // | |
| 44 // registry.AddInterface(&factory); | |
| 45 // | |
| 46 // Or, if you have multiple factories implemented by the same type, explicitly | |
| 47 // specify the interface to register the factory for: | |
| 48 // | |
| 49 // registry.AddInterface<Foo>(&my_foo_and_bar_factory_); | |
| 50 // registry.AddInterface<Bar>(&my_foo_and_bar_factory_); | |
| 51 // | |
| 52 // The InterfaceFactory must outlive the InterfaceRegistry. | |
| 53 // | |
| 54 // Additionally you may specify a default InterfaceBinder to handle requests for | |
| 55 // interfaces unhandled by any registered InterfaceFactory. Just as with | |
| 56 // InterfaceFactory, the default InterfaceBinder supplied must outlive | |
| 57 // InterfaceRegistry. | |
| 58 // | |
| 59 class InterfaceRegistry : public mojom::InterfaceProvider { | |
| 60 public: | |
| 61 using Binder = base::Callback<void(const std::string&, | |
| 62 mojo::ScopedMessagePipeHandle)>; | |
| 63 | |
| 64 class TestApi { | |
| 65 public: | |
| 66 explicit TestApi(InterfaceRegistry* registry) : registry_(registry) {} | |
| 67 ~TestApi() {} | |
| 68 | |
| 69 void SetInterfaceBinderForName(InterfaceBinder* binder, | |
| 70 const std::string& interface_name) { | |
| 71 registry_->SetInterfaceBinderForName( | |
| 72 base::WrapUnique(binder), interface_name); | |
| 73 } | |
| 74 | |
| 75 template <typename Interface> | |
| 76 void GetLocalInterface(mojo::InterfaceRequest<Interface> request) { | |
| 77 GetLocalInterface(Interface::Name_, request.PassMessagePipe()); | |
| 78 } | |
| 79 | |
| 80 void GetLocalInterface(const std::string& name, | |
| 81 mojo::ScopedMessagePipeHandle handle) { | |
| 82 registry_->GetInterface(name, std::move(handle)); | |
| 83 } | |
| 84 | |
| 85 private: | |
| 86 InterfaceRegistry* registry_; | |
| 87 DISALLOW_COPY_AND_ASSIGN(TestApi); | |
| 88 }; | |
| 89 | |
| 90 // Construct an unbound InterfaceRegistry. This object will not bind requests | |
| 91 // for interfaces until Bind() is called. |name| is used for error reporting | |
| 92 // and should reflect the name of the InterfaceProviderSpec pair that controls | |
| 93 // which interfaces can be bound via this InterfaceRegistry. | |
| 94 explicit InterfaceRegistry(const std::string& name); | |
| 95 ~InterfaceRegistry() override; | |
| 96 | |
| 97 // Sets a default handler for incoming interface requests which are allowed by | |
| 98 // capability filters but have no registered handler in this registry. | |
| 99 void set_default_binder(const Binder& binder) { default_binder_ = binder; } | |
| 100 | |
| 101 // Binds a request for an InterfaceProvider from a remote source. | |
| 102 // |remote_info| contains the the identity of the remote, and the remote's | |
| 103 // InterfaceProviderSpec, which will be intersected with the local's exports | |
| 104 // to determine what interfaces may be bound. | |
| 105 void Bind(mojom::InterfaceProviderRequest request, | |
| 106 const Identity& local_identity, | |
| 107 const InterfaceProviderSpec& local_interface_provider_spec, | |
| 108 const Identity& remote_identity, | |
| 109 const InterfaceProviderSpec& remote_interface_provider_spec); | |
| 110 | |
| 111 // Serializes the contents of the registry (including the local and remote | |
| 112 // specs) to a stringstream. | |
| 113 void Serialize(std::stringstream* stream); | |
| 114 | |
| 115 base::WeakPtr<InterfaceRegistry> GetWeakPtr(); | |
| 116 | |
| 117 // Allows |Interface| to be exposed via this registry. Requests to bind will | |
| 118 // be handled by |factory|. Returns true if the interface was exposed, false | |
| 119 // if Connection policy prevented exposure. | |
| 120 template <typename Interface> | |
| 121 bool AddInterface(InterfaceFactory<Interface>* factory) { | |
| 122 return SetInterfaceBinderForName( | |
| 123 base::MakeUnique<internal::InterfaceFactoryBinder<Interface>>(factory), | |
| 124 Interface::Name_); | |
| 125 } | |
| 126 | |
| 127 // Like AddInterface above, except supplies a callback to bind the MP instead | |
| 128 // of an InterfaceFactory, and optionally provides a task runner where the | |
| 129 // callback will be run. | |
| 130 template <typename Interface> | |
| 131 bool AddInterface( | |
| 132 const base::Callback<void(mojo::InterfaceRequest<Interface>)>& callback, | |
| 133 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner = | |
| 134 nullptr) { | |
| 135 return SetInterfaceBinderForName( | |
| 136 base::MakeUnique<internal::CallbackBinder<Interface>>(callback, | |
| 137 task_runner), | |
| 138 Interface::Name_); | |
| 139 } | |
| 140 bool AddInterface( | |
| 141 const std::string& name, | |
| 142 const base::Callback<void(mojo::ScopedMessagePipeHandle)>& callback, | |
| 143 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner = | |
| 144 nullptr); | |
| 145 | |
| 146 template <typename Interface> | |
| 147 void RemoveInterface() { | |
| 148 RemoveInterface(Interface::Name_); | |
| 149 } | |
| 150 void RemoveInterface(const std::string& name); | |
| 151 | |
| 152 // Temporarily prevent incoming interface requests from being bound. Incoming | |
| 153 // requests will be queued internally and dispatched once ResumeBinding() is | |
| 154 // called. | |
| 155 void PauseBinding(); | |
| 156 | |
| 157 // Resumes incoming interface request binding. | |
| 158 void ResumeBinding(); | |
| 159 | |
| 160 // Populates a set with the interface names this registry can bind. | |
| 161 void GetInterfaceNames(std::set<std::string>* interface_names); | |
| 162 | |
| 163 // Sets a closure to be run when the InterfaceProvider pipe is closed. Note | |
| 164 // that by the time any added closure is invoked, the InterfaceRegistry may | |
| 165 // have been deleted. | |
| 166 void AddConnectionLostClosure(const base::Closure& connection_lost_closure); | |
| 167 | |
| 168 // Binds a local interface request. | |
| 169 void BindInterface(const std::string& name, | |
| 170 mojo::ScopedMessagePipeHandle handle); | |
| 171 | |
| 172 private: | |
| 173 using InterfaceNameToBinderMap = | |
| 174 std::map<std::string, std::unique_ptr<InterfaceBinder>>; | |
| 175 | |
| 176 // mojom::InterfaceProvider: | |
| 177 void GetInterface(const std::string& interface_name, | |
| 178 mojo::ScopedMessagePipeHandle handle) override; | |
| 179 | |
| 180 // Returns true if the binder was set, false if it was not set (e.g. by | |
| 181 // some filtering policy preventing this interface from being exposed). | |
| 182 bool SetInterfaceBinderForName(std::unique_ptr<InterfaceBinder> binder, | |
| 183 const std::string& name); | |
| 184 | |
| 185 // Returns true if |remote_identity_| is allowed to bind |interface_name|, | |
| 186 // according to capability policy. | |
| 187 bool CanBindRequestForInterface(const std::string& interface_name) const; | |
| 188 | |
| 189 // Called whenever |remote_interface_provider_spec_| changes to rebuild the | |
| 190 // contents of |exposed_interfaces_| and |expose_all_interfaces_|. | |
| 191 void RebuildExposedInterfaces(); | |
| 192 | |
| 193 void OnConnectionError(); | |
| 194 | |
| 195 mojom::InterfaceProviderRequest pending_request_; | |
| 196 | |
| 197 mojo::Binding<mojom::InterfaceProvider> binding_; | |
| 198 | |
| 199 std::string name_; | |
| 200 | |
| 201 // Initialized from static metadata in the host service's manifest. | |
| 202 Identity local_identity_; | |
| 203 InterfaceProviderSpec local_interface_provider_spec_; | |
| 204 | |
| 205 // Initialized from static metadata in the remote service's manifest. | |
| 206 Identity remote_identity_; | |
| 207 // Initialized from static metadata in the remote service's manifest. May be | |
| 208 // mutated after the fact when a capability is dynamically granted via a call | |
| 209 // to GrantCapability(). | |
| 210 InterfaceProviderSpec remote_interface_provider_spec_; | |
| 211 | |
| 212 // Metadata computed whenever |remote_interface_provider_spec_| changes. | |
| 213 InterfaceSet exposed_interfaces_; | |
| 214 bool expose_all_interfaces_ = false; | |
| 215 | |
| 216 // Contains every interface binder that has been registered with this | |
| 217 // InterfaceRegistry. Not all binders may be reachable depending on the | |
| 218 // capabilities requested by the remote. Only interfaces in | |
| 219 // exposed_interfaces_ may be bound. When |expose_all_interfaces_| is true, | |
| 220 // any interface may be bound. | |
| 221 InterfaceNameToBinderMap name_to_binder_; | |
| 222 Binder default_binder_; | |
| 223 | |
| 224 bool is_paused_ = false; | |
| 225 | |
| 226 // Pending interface requests which can accumulate if GetInterface() is called | |
| 227 // while binding is paused. | |
| 228 std::queue<std::pair<std::string, mojo::ScopedMessagePipeHandle>> | |
| 229 pending_interface_requests_; | |
| 230 | |
| 231 std::list<base::Closure> connection_lost_closures_; | |
| 232 | |
| 233 base::WeakPtrFactory<InterfaceRegistry> weak_factory_; | |
| 234 | |
| 235 DISALLOW_COPY_AND_ASSIGN(InterfaceRegistry); | |
| 236 }; | |
| 237 | |
| 238 } // namespace service_manager | |
| 239 | |
| 240 #endif // SERVICES_SERVICE_MANAGER_PUBLIC_CPP_INTERFACE_REGISTRY_H_ | |
| OLD | NEW |