Chromium Code Reviews| 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 | 65 // Construct with an InterfaceProviderRequest and a Connection (which may be |
| 70 // null, see note above about filtering). | 66 // null, see note above about filtering). |
| 71 InterfaceRegistry(mojom::InterfaceProviderPtr remote_interfaces, | 67 InterfaceRegistry(mojom::InterfaceProviderRequest local_interfaces_request, |
| 72 mojom::InterfaceProviderRequest local_interfaces_request, | |
| 73 Connection* connection); | 68 Connection* connection); |
| 74 ~InterfaceRegistry() override; | 69 ~InterfaceRegistry() override; |
| 75 | 70 |
| 76 // Takes the client end of the InterfaceProvider pipe created in the | 71 // Takes the client end of the InterfaceProvider pipe created in the |
| 77 // constructor. | 72 // constructor. |
| 78 mojom::InterfaceProviderPtr TakeClientHandle(); | 73 mojom::InterfaceProviderPtr TakeClientHandle(); |
|
Ken Rockot(use gerrit already)
2016/06/16 23:48:58
Given that you're adding a Bind method, let's get
| |
| 79 | 74 |
| 80 // Returns a raw pointer to the remote InterfaceProvider. | 75 void Bind(mojom::InterfaceProviderRequest local_interfaces_request); |
|
Ken Rockot(use gerrit already)
2016/06/16 23:48:58
nit: Document this, specifically worth noting that
| |
| 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 | 76 |
| 87 // Allows |Interface| to be exposed via this registry. Requests to bind will | 77 // 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 | 78 // be handled by |factory|. Returns true if the interface was exposed, false |
| 89 // if Connection policy prevented exposure. | 79 // if Connection policy prevented exposure. |
| 90 template <typename Interface> | 80 template <typename Interface> |
| 91 bool AddInterface(InterfaceFactory<Interface>* factory) { | 81 bool AddInterface(InterfaceFactory<Interface>* factory) { |
| 92 return SetInterfaceBinderForName( | 82 return SetInterfaceBinderForName( |
| 93 base::WrapUnique( | 83 base::WrapUnique( |
| 94 new internal::InterfaceFactoryBinder<Interface>(factory)), | 84 new internal::InterfaceFactoryBinder<Interface>(factory)), |
| 95 Interface::Name_); | 85 Interface::Name_); |
| 96 } | 86 } |
| 97 | 87 |
| 98 // Like AddInterface above, except supplies a callback to bind the MP instead | 88 // Like AddInterface above, except supplies a callback to bind the MP instead |
| 99 // of an InterfaceFactory, and optionally provides a task runner where the | 89 // of an InterfaceFactory, and optionally provides a task runner where the |
| 100 // callback will be run. | 90 // callback will be run. |
| 101 template <typename Interface> | 91 template <typename Interface> |
| 102 bool AddInterface( | 92 bool AddInterface( |
| 103 const base::Callback<void(mojo::InterfaceRequest<Interface>)>& callback, | 93 const base::Callback<void(mojo::InterfaceRequest<Interface>)>& callback, |
| 104 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner = | 94 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner = |
| 105 nullptr) { | 95 nullptr) { |
| 106 return SetInterfaceBinderForName( | 96 return SetInterfaceBinderForName( |
| 107 base::WrapUnique( | 97 base::WrapUnique( |
| 108 new internal::CallbackBinder<Interface>(callback, task_runner)), | 98 new internal::CallbackBinder<Interface>(callback, task_runner)), |
| 109 Interface::Name_); | 99 Interface::Name_); |
| 110 } | 100 } |
| 111 | 101 |
| 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> | 102 template <typename Interface> |
| 116 void GetInterface(mojo::InterfacePtr<Interface>* ptr) { | 103 void RemoveInterface() { |
| 117 mojo::MessagePipe pipe; | 104 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 } | 105 } |
| 106 void RemoveInterface(const std::string& name); | |
| 129 | 107 |
| 130 private: | 108 private: |
| 131 using NameToInterfaceBinderMap = | 109 using NameToInterfaceBinderMap = |
| 132 std::map<std::string, std::unique_ptr<InterfaceBinder>>; | 110 std::map<std::string, std::unique_ptr<InterfaceBinder>>; |
| 133 | 111 |
| 134 // mojom::InterfaceProvider: | 112 // mojom::InterfaceProvider: |
| 135 void GetInterface(const mojo::String& interface_name, | 113 void GetInterface(const mojo::String& interface_name, |
| 136 mojo::ScopedMessagePipeHandle handle) override; | 114 mojo::ScopedMessagePipeHandle handle) override; |
| 137 | 115 |
| 138 // Returns true if the binder was set, false if it was not set (e.g. by | 116 // 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). | 117 // some filtering policy preventing this interface from being exposed). |
| 140 bool SetInterfaceBinderForName(std::unique_ptr<InterfaceBinder> binder, | 118 bool SetInterfaceBinderForName(std::unique_ptr<InterfaceBinder> binder, |
| 141 const std::string& name); | 119 const std::string& name); |
| 142 | 120 |
| 143 void RemoveInterfaceBinderForName(const std::string& interface_name); | |
| 144 | |
| 145 mojom::InterfaceProviderPtr client_handle_; | 121 mojom::InterfaceProviderPtr client_handle_; |
| 146 mojo::Binding<mojom::InterfaceProvider> binding_; | 122 mojo::Binding<mojom::InterfaceProvider> binding_; |
| 147 Connection* connection_; | 123 Connection* connection_; |
| 148 | 124 |
| 149 NameToInterfaceBinderMap name_to_binder_; | 125 NameToInterfaceBinderMap name_to_binder_; |
| 150 | 126 |
| 151 mojom::InterfaceProviderPtr remote_interfaces_; | |
| 152 | |
| 153 DISALLOW_COPY_AND_ASSIGN(InterfaceRegistry); | 127 DISALLOW_COPY_AND_ASSIGN(InterfaceRegistry); |
| 154 }; | 128 }; |
| 155 | 129 |
| 156 } // namespace shell | 130 } // namespace shell |
| 157 | 131 |
| 158 #endif // SERVICES_SHELL_PUBLIC_CPP_INTERFACE_REGISTRY_H_ | 132 #endif // SERVICES_SHELL_PUBLIC_CPP_INTERFACE_REGISTRY_H_ |
| OLD | NEW |