| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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 #ifndef SERVICES_SHELL_PUBLIC_CPP_INTERFACE_REGISTRY_H_ | 5 #ifndef SERVICES_SHELL_PUBLIC_CPP_INTERFACE_REGISTRY_H_ |
| 6 #define SERVICES_SHELL_PUBLIC_CPP_INTERFACE_REGISTRY_H_ | 6 #define SERVICES_SHELL_PUBLIC_CPP_INTERFACE_REGISTRY_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 | 9 |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 public: | 44 public: |
| 45 explicit TestApi(InterfaceRegistry* registry) : registry_(registry) {} | 45 explicit TestApi(InterfaceRegistry* registry) : registry_(registry) {} |
| 46 ~TestApi() {} | 46 ~TestApi() {} |
| 47 | 47 |
| 48 void SetInterfaceBinderForName(InterfaceBinder* binder, | 48 void SetInterfaceBinderForName(InterfaceBinder* binder, |
| 49 const std::string& interface_name) { | 49 const std::string& interface_name) { |
| 50 registry_->SetInterfaceBinderForName( | 50 registry_->SetInterfaceBinderForName( |
| 51 base::WrapUnique(binder), interface_name); | 51 base::WrapUnique(binder), interface_name); |
| 52 } | 52 } |
| 53 | 53 |
| 54 void RemoveInterfaceBinderForName(const std::string& interface_name) { | |
| 55 registry_->RemoveInterfaceBinderForName(interface_name); | |
| 56 } | |
| 57 | |
| 58 private: | 54 private: |
| 59 InterfaceRegistry* registry_; | 55 InterfaceRegistry* registry_; |
| 60 DISALLOW_COPY_AND_ASSIGN(TestApi); | 56 DISALLOW_COPY_AND_ASSIGN(TestApi); |
| 61 }; | 57 }; |
| 62 | 58 |
| 63 // Construct with a Connection (which may be null), and create an | 59 // Construct with a Connection (which may be null), and create an |
| 64 // InterfaceProvider pipe, the client end of which may be obtained by calling | 60 // InterfaceProvider pipe, the client end of which may be obtained by calling |
| 65 // TakeClientHandle(). If |connection| is non-null, the Mojo Shell's | 61 // TakeClientHandle(). If |connection| is non-null, the Mojo Shell's |
| 66 // rules filtering which interfaces are allowed to be exposed to clients are | 62 // rules filtering which interfaces are allowed to be exposed to clients are |
| 67 // imposed on this registry. If null, they are not. | 63 // imposed on this registry. If null, they are not. |
| 68 explicit InterfaceRegistry(Connection* connection); | 64 explicit InterfaceRegistry(Connection* connection); |
| 69 // Construct with an InterfaceProviderRequest and a Connection (which may be | |
| 70 // null, see note above about filtering). | |
| 71 InterfaceRegistry(mojom::InterfaceProviderPtr remote_interfaces, | |
| 72 mojom::InterfaceProviderRequest local_interfaces_request, | |
| 73 Connection* connection); | |
| 74 ~InterfaceRegistry() override; | 65 ~InterfaceRegistry() override; |
| 75 | 66 |
| 76 // Takes the client end of the InterfaceProvider pipe created in the | 67 void Bind(mojom::InterfaceProviderRequest local_interfaces_request); |
| 77 // constructor. | |
| 78 mojom::InterfaceProviderPtr TakeClientHandle(); | |
| 79 | |
| 80 // Returns a raw pointer to the remote InterfaceProvider. | |
| 81 mojom::InterfaceProvider* GetRemoteInterfaces(); | |
| 82 | |
| 83 // Sets a closure to be run when the remote InterfaceProvider pipe is closed. | |
| 84 void SetRemoteInterfacesConnectionLostClosure( | |
| 85 const base::Closure& connection_lost_closure); | |
| 86 | 68 |
| 87 // Allows |Interface| to be exposed via this registry. Requests to bind will | 69 // Allows |Interface| to be exposed via this registry. Requests to bind will |
| 88 // be handled by |factory|. Returns true if the interface was exposed, false | 70 // be handled by |factory|. Returns true if the interface was exposed, false |
| 89 // if Connection policy prevented exposure. | 71 // if Connection policy prevented exposure. |
| 90 template <typename Interface> | 72 template <typename Interface> |
| 91 bool AddInterface(InterfaceFactory<Interface>* factory) { | 73 bool AddInterface(InterfaceFactory<Interface>* factory) { |
| 92 return SetInterfaceBinderForName( | 74 return SetInterfaceBinderForName( |
| 93 base::WrapUnique( | 75 base::WrapUnique( |
| 94 new internal::InterfaceFactoryBinder<Interface>(factory)), | 76 new internal::InterfaceFactoryBinder<Interface>(factory)), |
| 95 Interface::Name_); | 77 Interface::Name_); |
| 96 } | 78 } |
| 97 | 79 |
| 98 // Like AddInterface above, except supplies a callback to bind the MP instead | 80 // Like AddInterface above, except supplies a callback to bind the MP instead |
| 99 // of an InterfaceFactory, and optionally provides a task runner where the | 81 // of an InterfaceFactory, and optionally provides a task runner where the |
| 100 // callback will be run. | 82 // callback will be run. |
| 101 template <typename Interface> | 83 template <typename Interface> |
| 102 bool AddInterface( | 84 bool AddInterface( |
| 103 const base::Callback<void(mojo::InterfaceRequest<Interface>)>& callback, | 85 const base::Callback<void(mojo::InterfaceRequest<Interface>)>& callback, |
| 104 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner = | 86 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner = |
| 105 nullptr) { | 87 nullptr) { |
| 106 return SetInterfaceBinderForName( | 88 return SetInterfaceBinderForName( |
| 107 base::WrapUnique( | 89 base::WrapUnique( |
| 108 new internal::CallbackBinder<Interface>(callback, task_runner)), | 90 new internal::CallbackBinder<Interface>(callback, task_runner)), |
| 109 Interface::Name_); | 91 Interface::Name_); |
| 110 } | 92 } |
| 111 | 93 |
| 112 // Binds |ptr| to an implementation of Interface in the remote application. | |
| 113 // |ptr| can immediately be used to start sending requests to the remote | |
| 114 // interface. | |
| 115 template <typename Interface> | 94 template <typename Interface> |
| 116 void GetInterface(mojo::InterfacePtr<Interface>* ptr) { | 95 void RemoveInterface() { |
| 117 mojo::MessagePipe pipe; | 96 RemoveInterface(Interface::Name_); |
| 118 ptr->Bind(mojo::InterfacePtrInfo<Interface>(std::move(pipe.handle0), 0u)); | |
| 119 | |
| 120 // Local binders can be registered via TestApi. | |
| 121 auto it = name_to_binder_.find(Interface::Name_); | |
| 122 if (it != name_to_binder_.end()) { | |
| 123 it->second->BindInterface(connection_, Interface::Name_, | |
| 124 std::move(pipe.handle1)); | |
| 125 return; | |
| 126 } | |
| 127 remote_interfaces_->GetInterface(Interface::Name_, std::move(pipe.handle1)); | |
| 128 } | 97 } |
| 98 void RemoveInterface(const std::string& name); |
| 129 | 99 |
| 130 private: | 100 private: |
| 131 using NameToInterfaceBinderMap = | 101 using NameToInterfaceBinderMap = |
| 132 std::map<std::string, std::unique_ptr<InterfaceBinder>>; | 102 std::map<std::string, std::unique_ptr<InterfaceBinder>>; |
| 133 | 103 |
| 134 // mojom::InterfaceProvider: | 104 // mojom::InterfaceProvider: |
| 135 void GetInterface(const mojo::String& interface_name, | 105 void GetInterface(const mojo::String& interface_name, |
| 136 mojo::ScopedMessagePipeHandle handle) override; | 106 mojo::ScopedMessagePipeHandle handle) override; |
| 137 | 107 |
| 138 // Returns true if the binder was set, false if it was not set (e.g. by | 108 // Returns true if the binder was set, false if it was not set (e.g. by |
| 139 // some filtering policy preventing this interface from being exposed). | 109 // some filtering policy preventing this interface from being exposed). |
| 140 bool SetInterfaceBinderForName(std::unique_ptr<InterfaceBinder> binder, | 110 bool SetInterfaceBinderForName(std::unique_ptr<InterfaceBinder> binder, |
| 141 const std::string& name); | 111 const std::string& name); |
| 142 | 112 |
| 143 void RemoveInterfaceBinderForName(const std::string& interface_name); | |
| 144 | |
| 145 mojom::InterfaceProviderPtr client_handle_; | |
| 146 mojo::Binding<mojom::InterfaceProvider> binding_; | 113 mojo::Binding<mojom::InterfaceProvider> binding_; |
| 147 Connection* connection_; | 114 Connection* connection_; |
| 148 | 115 |
| 149 NameToInterfaceBinderMap name_to_binder_; | 116 NameToInterfaceBinderMap name_to_binder_; |
| 150 | 117 |
| 151 mojom::InterfaceProviderPtr remote_interfaces_; | |
| 152 | |
| 153 DISALLOW_COPY_AND_ASSIGN(InterfaceRegistry); | 118 DISALLOW_COPY_AND_ASSIGN(InterfaceRegistry); |
| 154 }; | 119 }; |
| 155 | 120 |
| 156 } // namespace shell | 121 } // namespace shell |
| 157 | 122 |
| 158 #endif // SERVICES_SHELL_PUBLIC_CPP_INTERFACE_REGISTRY_H_ | 123 #endif // SERVICES_SHELL_PUBLIC_CPP_INTERFACE_REGISTRY_H_ |
| OLD | NEW |