| 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 #include <queue> |
| 10 #include <utility> |
| 9 | 11 |
| 12 #include "base/callback.h" |
| 10 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
| 11 #include "mojo/public/cpp/bindings/binding.h" | 14 #include "mojo/public/cpp/bindings/binding.h" |
| 12 #include "services/shell/public/cpp/lib/callback_binder.h" | 15 #include "services/shell/public/cpp/lib/callback_binder.h" |
| 13 #include "services/shell/public/cpp/lib/interface_factory_binder.h" | 16 #include "services/shell/public/cpp/lib/interface_factory_binder.h" |
| 14 #include "services/shell/public/interfaces/interface_provider.mojom.h" | 17 #include "services/shell/public/interfaces/interface_provider.mojom.h" |
| 15 | 18 |
| 16 namespace shell { | 19 namespace shell { |
| 17 class InterfaceBinder; | 20 class InterfaceBinder; |
| 18 | 21 |
| 19 // An implementation of mojom::InterfaceProvider that allows the user to | 22 // An implementation of mojom::InterfaceProvider that allows the user to |
| (...skipping 13 matching lines...) Expand all Loading... |
| 33 // | 36 // |
| 34 // The InterfaceFactory must outlive the InterfaceRegistry. | 37 // The InterfaceFactory must outlive the InterfaceRegistry. |
| 35 // | 38 // |
| 36 // Additionally you may specify a default InterfaceBinder to handle requests for | 39 // Additionally you may specify a default InterfaceBinder to handle requests for |
| 37 // interfaces unhandled by any registered InterfaceFactory. Just as with | 40 // interfaces unhandled by any registered InterfaceFactory. Just as with |
| 38 // InterfaceFactory, the default InterfaceBinder supplied must outlive | 41 // InterfaceFactory, the default InterfaceBinder supplied must outlive |
| 39 // InterfaceRegistry. | 42 // InterfaceRegistry. |
| 40 // | 43 // |
| 41 class InterfaceRegistry : public mojom::InterfaceProvider { | 44 class InterfaceRegistry : public mojom::InterfaceProvider { |
| 42 public: | 45 public: |
| 46 using Binder = base::Callback<void(const mojo::String&, |
| 47 mojo::ScopedMessagePipeHandle)>; |
| 48 |
| 43 class TestApi { | 49 class TestApi { |
| 44 public: | 50 public: |
| 45 explicit TestApi(InterfaceRegistry* registry) : registry_(registry) {} | 51 explicit TestApi(InterfaceRegistry* registry) : registry_(registry) {} |
| 46 ~TestApi() {} | 52 ~TestApi() {} |
| 47 | 53 |
| 48 void SetInterfaceBinderForName(InterfaceBinder* binder, | 54 void SetInterfaceBinderForName(InterfaceBinder* binder, |
| 49 const std::string& interface_name) { | 55 const std::string& interface_name) { |
| 50 registry_->SetInterfaceBinderForName( | 56 registry_->SetInterfaceBinderForName( |
| 51 base::WrapUnique(binder), interface_name); | 57 base::WrapUnique(binder), interface_name); |
| 52 } | 58 } |
| 53 | 59 |
| 54 private: | 60 private: |
| 55 InterfaceRegistry* registry_; | 61 InterfaceRegistry* registry_; |
| 56 DISALLOW_COPY_AND_ASSIGN(TestApi); | 62 DISALLOW_COPY_AND_ASSIGN(TestApi); |
| 57 }; | 63 }; |
| 58 | 64 |
| 59 // Construct with a Connection (which may be null), and create an | 65 // Construct with a Connection (which may be null), and create an |
| 60 // InterfaceProvider pipe, the client end of which may be obtained by calling | 66 // InterfaceProvider pipe, the client end of which may be obtained by calling |
| 61 // TakeClientHandle(). If |connection| is non-null, the Mojo Shell's | 67 // TakeClientHandle(). If |connection| is non-null, the Mojo Shell's |
| 62 // rules filtering which interfaces are allowed to be exposed to clients are | 68 // rules filtering which interfaces are allowed to be exposed to clients are |
| 63 // imposed on this registry. If null, they are not. | 69 // imposed on this registry. If null, they are not. |
| 64 explicit InterfaceRegistry(Connection* connection); | 70 explicit InterfaceRegistry(Connection* connection); |
| 65 ~InterfaceRegistry() override; | 71 ~InterfaceRegistry() override; |
| 66 | 72 |
| 73 // Sets a default handler for incoming interface requests which are allowed by |
| 74 // capability filters but have no registered handler in this registry. |
| 75 void set_default_binder(const Binder& binder) { default_binder_ = binder; } |
| 76 |
| 67 void Bind(mojom::InterfaceProviderRequest local_interfaces_request); | 77 void Bind(mojom::InterfaceProviderRequest local_interfaces_request); |
| 68 | 78 |
| 69 base::WeakPtr<InterfaceRegistry> GetWeakPtr(); | 79 base::WeakPtr<InterfaceRegistry> GetWeakPtr(); |
| 70 | 80 |
| 71 // Allows |Interface| to be exposed via this registry. Requests to bind will | 81 // Allows |Interface| to be exposed via this registry. Requests to bind will |
| 72 // be handled by |factory|. Returns true if the interface was exposed, false | 82 // be handled by |factory|. Returns true if the interface was exposed, false |
| 73 // if Connection policy prevented exposure. | 83 // if Connection policy prevented exposure. |
| 74 template <typename Interface> | 84 template <typename Interface> |
| 75 bool AddInterface(InterfaceFactory<Interface>* factory) { | 85 bool AddInterface(InterfaceFactory<Interface>* factory) { |
| 76 return SetInterfaceBinderForName( | 86 return SetInterfaceBinderForName( |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 bool SetInterfaceBinderForName(std::unique_ptr<InterfaceBinder> binder, | 135 bool SetInterfaceBinderForName(std::unique_ptr<InterfaceBinder> binder, |
| 126 const std::string& name); | 136 const std::string& name); |
| 127 | 137 |
| 128 mojom::InterfaceProviderRequest pending_request_; | 138 mojom::InterfaceProviderRequest pending_request_; |
| 129 | 139 |
| 130 mojo::Binding<mojom::InterfaceProvider> binding_; | 140 mojo::Binding<mojom::InterfaceProvider> binding_; |
| 131 Connection* connection_; | 141 Connection* connection_; |
| 132 | 142 |
| 133 NameToInterfaceBinderMap name_to_binder_; | 143 NameToInterfaceBinderMap name_to_binder_; |
| 134 | 144 |
| 145 Binder default_binder_; |
| 146 |
| 147 bool is_paused_ = false; |
| 148 |
| 149 // Pending interface requests which can accumulate if GetInterface() is called |
| 150 // while binding is paused. |
| 151 std::queue<std::pair<mojo::String, mojo::ScopedMessagePipeHandle>> |
| 152 pending_interface_requests_; |
| 153 |
| 135 base::WeakPtrFactory<InterfaceRegistry> weak_factory_; | 154 base::WeakPtrFactory<InterfaceRegistry> weak_factory_; |
| 136 | 155 |
| 137 DISALLOW_COPY_AND_ASSIGN(InterfaceRegistry); | 156 DISALLOW_COPY_AND_ASSIGN(InterfaceRegistry); |
| 138 }; | 157 }; |
| 139 | 158 |
| 140 } // namespace shell | 159 } // namespace shell |
| 141 | 160 |
| 142 #endif // SERVICES_SHELL_PUBLIC_CPP_INTERFACE_REGISTRY_H_ | 161 #endif // SERVICES_SHELL_PUBLIC_CPP_INTERFACE_REGISTRY_H_ |
| OLD | NEW |