| 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 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 // Construct with a Connection (which may be null), and create an | 59 // Construct with a Connection (which may be null), and create an |
| 60 // 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 |
| 61 // TakeClientHandle(). If |connection| is non-null, the Mojo Shell's | 61 // TakeClientHandle(). If |connection| is non-null, the Mojo Shell's |
| 62 // 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 |
| 63 // imposed on this registry. If null, they are not. | 63 // imposed on this registry. If null, they are not. |
| 64 explicit InterfaceRegistry(Connection* connection); | 64 explicit InterfaceRegistry(Connection* connection); |
| 65 ~InterfaceRegistry() override; | 65 ~InterfaceRegistry() override; |
| 66 | 66 |
| 67 void Bind(mojom::InterfaceProviderRequest local_interfaces_request); | 67 void Bind(mojom::InterfaceProviderRequest local_interfaces_request); |
| 68 | 68 |
| 69 base::WeakPtr<InterfaceRegistry> GetWeakPtr(); |
| 70 |
| 69 // Allows |Interface| to be exposed via this registry. Requests to bind will | 71 // Allows |Interface| to be exposed via this registry. Requests to bind will |
| 70 // be handled by |factory|. Returns true if the interface was exposed, false | 72 // be handled by |factory|. Returns true if the interface was exposed, false |
| 71 // if Connection policy prevented exposure. | 73 // if Connection policy prevented exposure. |
| 72 template <typename Interface> | 74 template <typename Interface> |
| 73 bool AddInterface(InterfaceFactory<Interface>* factory) { | 75 bool AddInterface(InterfaceFactory<Interface>* factory) { |
| 74 return SetInterfaceBinderForName( | 76 return SetInterfaceBinderForName( |
| 75 base::WrapUnique( | 77 base::WrapUnique( |
| 76 new internal::InterfaceFactoryBinder<Interface>(factory)), | 78 new internal::InterfaceFactoryBinder<Interface>(factory)), |
| 77 Interface::Name_); | 79 Interface::Name_); |
| 78 } | 80 } |
| 79 | 81 |
| 80 // Like AddInterface above, except supplies a callback to bind the MP instead | 82 // Like AddInterface above, except supplies a callback to bind the MP instead |
| 81 // of an InterfaceFactory, and optionally provides a task runner where the | 83 // of an InterfaceFactory, and optionally provides a task runner where the |
| 82 // callback will be run. | 84 // callback will be run. |
| 83 template <typename Interface> | 85 template <typename Interface> |
| 84 bool AddInterface( | 86 bool AddInterface( |
| 85 const base::Callback<void(mojo::InterfaceRequest<Interface>)>& callback, | 87 const base::Callback<void(mojo::InterfaceRequest<Interface>)>& callback, |
| 86 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner = | 88 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner = |
| 87 nullptr) { | 89 nullptr) { |
| 88 return SetInterfaceBinderForName( | 90 return SetInterfaceBinderForName( |
| 89 base::WrapUnique( | 91 base::WrapUnique( |
| 90 new internal::CallbackBinder<Interface>(callback, task_runner)), | 92 new internal::CallbackBinder<Interface>(callback, task_runner)), |
| 91 Interface::Name_); | 93 Interface::Name_); |
| 92 } | 94 } |
| 95 bool AddInterface( |
| 96 const std::string& name, |
| 97 const base::Callback<void(mojo::ScopedMessagePipeHandle)>& callback, |
| 98 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner = |
| 99 nullptr); |
| 93 | 100 |
| 94 template <typename Interface> | 101 template <typename Interface> |
| 95 void RemoveInterface() { | 102 void RemoveInterface() { |
| 96 RemoveInterface(Interface::Name_); | 103 RemoveInterface(Interface::Name_); |
| 97 } | 104 } |
| 98 void RemoveInterface(const std::string& name); | 105 void RemoveInterface(const std::string& name); |
| 99 | 106 |
| 100 private: | 107 private: |
| 101 using NameToInterfaceBinderMap = | 108 using NameToInterfaceBinderMap = |
| 102 std::map<std::string, std::unique_ptr<InterfaceBinder>>; | 109 std::map<std::string, std::unique_ptr<InterfaceBinder>>; |
| 103 | 110 |
| 104 // mojom::InterfaceProvider: | 111 // mojom::InterfaceProvider: |
| 105 void GetInterface(const mojo::String& interface_name, | 112 void GetInterface(const mojo::String& interface_name, |
| 106 mojo::ScopedMessagePipeHandle handle) override; | 113 mojo::ScopedMessagePipeHandle handle) override; |
| 107 | 114 |
| 108 // Returns true if the binder was set, false if it was not set (e.g. by | 115 // Returns true if the binder was set, false if it was not set (e.g. by |
| 109 // some filtering policy preventing this interface from being exposed). | 116 // some filtering policy preventing this interface from being exposed). |
| 110 bool SetInterfaceBinderForName(std::unique_ptr<InterfaceBinder> binder, | 117 bool SetInterfaceBinderForName(std::unique_ptr<InterfaceBinder> binder, |
| 111 const std::string& name); | 118 const std::string& name); |
| 112 | 119 |
| 113 mojo::Binding<mojom::InterfaceProvider> binding_; | 120 mojo::Binding<mojom::InterfaceProvider> binding_; |
| 114 Connection* connection_; | 121 Connection* connection_; |
| 115 | 122 |
| 116 NameToInterfaceBinderMap name_to_binder_; | 123 NameToInterfaceBinderMap name_to_binder_; |
| 117 | 124 |
| 125 base::WeakPtrFactory<InterfaceRegistry> weak_factory_; |
| 126 |
| 118 DISALLOW_COPY_AND_ASSIGN(InterfaceRegistry); | 127 DISALLOW_COPY_AND_ASSIGN(InterfaceRegistry); |
| 119 }; | 128 }; |
| 120 | 129 |
| 121 } // namespace shell | 130 } // namespace shell |
| 122 | 131 |
| 123 #endif // SERVICES_SHELL_PUBLIC_CPP_INTERFACE_REGISTRY_H_ | 132 #endif // SERVICES_SHELL_PUBLIC_CPP_INTERFACE_REGISTRY_H_ |
| OLD | NEW |