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