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 "mojo/public/cpp/bindings/binding.h" | 8 #include "mojo/public/cpp/bindings/binding.h" |
9 #include "services/shell/public/cpp/lib/interface_factory_binder.h" | 9 #include "services/shell/public/cpp/lib/interface_factory_binder.h" |
10 #include "services/shell/public/interfaces/interface_provider.mojom.h" | 10 #include "services/shell/public/interfaces/interface_provider.mojom.h" |
11 | 11 |
12 namespace mojo { | 12 namespace shell { |
13 | |
14 class InterfaceBinder; | 13 class InterfaceBinder; |
15 | 14 |
16 // An implementation of shell::mojom::InterfaceProvider that allows the user to | 15 // An implementation of mojom::InterfaceProvider that allows the user to |
17 // register services to be exposed to another application. | 16 // register services to be exposed to another application. |
18 // | 17 // |
19 // To use, define a class that implements your specific interface. Then | 18 // To use, define a class that implements your specific interface. Then |
20 // implement an InterfaceFactory<Foo> that binds instances of FooImpl to | 19 // implement an InterfaceFactory<Foo> that binds instances of FooImpl to |
21 // InterfaceRequest<Foo>s and register that on the registry like this: | 20 // InterfaceRequest<Foo>s and register that on the registry like this: |
22 // | 21 // |
23 // registry.AddInterface(&factory); | 22 // registry.AddInterface(&factory); |
24 // | 23 // |
25 // Or, if you have multiple factories implemented by the same type, explicitly | 24 // Or, if you have multiple factories implemented by the same type, explicitly |
26 // specify the interface to register the factory for: | 25 // specify the interface to register the factory for: |
27 // | 26 // |
28 // registry.AddInterface<Foo>(&my_foo_and_bar_factory_); | 27 // registry.AddInterface<Foo>(&my_foo_and_bar_factory_); |
29 // registry.AddInterface<Bar>(&my_foo_and_bar_factory_); | 28 // registry.AddInterface<Bar>(&my_foo_and_bar_factory_); |
30 // | 29 // |
31 // The InterfaceFactory must outlive the InterfaceRegistry. | 30 // The InterfaceFactory must outlive the InterfaceRegistry. |
32 // | 31 // |
33 // Additionally you may specify a default InterfaceBinder to handle requests for | 32 // Additionally you may specify a default InterfaceBinder to handle requests for |
34 // interfaces unhandled by any registered InterfaceFactory. Just as with | 33 // interfaces unhandled by any registered InterfaceFactory. Just as with |
35 // InterfaceFactory, the default InterfaceBinder supplied must outlive | 34 // InterfaceFactory, the default InterfaceBinder supplied must outlive |
36 // InterfaceRegistry. | 35 // InterfaceRegistry. |
37 // | 36 // |
38 class InterfaceRegistry : public shell::mojom::InterfaceProvider { | 37 class InterfaceRegistry : public mojom::InterfaceProvider { |
39 public: | 38 public: |
40 class TestApi { | 39 class TestApi { |
41 public: | 40 public: |
42 explicit TestApi(InterfaceRegistry* registry) : registry_(registry) {} | 41 explicit TestApi(InterfaceRegistry* registry) : registry_(registry) {} |
43 ~TestApi() {} | 42 ~TestApi() {} |
44 | 43 |
45 void SetInterfaceBinderForName(InterfaceBinder* binder, | 44 void SetInterfaceBinderForName(InterfaceBinder* binder, |
46 const std::string& interface_name) { | 45 const std::string& interface_name) { |
47 registry_->SetInterfaceBinderForName(binder, interface_name); | 46 registry_->SetInterfaceBinderForName(binder, interface_name); |
48 } | 47 } |
| 48 |
49 void RemoveInterfaceBinderForName(const std::string& interface_name) { | 49 void RemoveInterfaceBinderForName(const std::string& interface_name) { |
50 registry_->RemoveInterfaceBinderForName(interface_name); | 50 registry_->RemoveInterfaceBinderForName(interface_name); |
51 } | 51 } |
52 | 52 |
53 private: | 53 private: |
54 InterfaceRegistry* registry_; | 54 InterfaceRegistry* registry_; |
55 DISALLOW_COPY_AND_ASSIGN(TestApi); | 55 DISALLOW_COPY_AND_ASSIGN(TestApi); |
56 }; | 56 }; |
57 | 57 |
58 // Construct with a Connection (which may be null), and create an | 58 // Construct with a Connection (which may be null), and create an |
59 // InterfaceProvider pipe, the client end of which may be obtained by calling | 59 // InterfaceProvider pipe, the client end of which may be obtained by calling |
60 // TakeClientHandle(). If |connection| is non-null, the Mojo Shell's | 60 // TakeClientHandle(). If |connection| is non-null, the Mojo Shell's |
61 // rules filtering which interfaces are allowed to be exposed to clients are | 61 // rules filtering which interfaces are allowed to be exposed to clients are |
62 // imposed on this registry. If null, they are not. | 62 // imposed on this registry. If null, they are not. |
63 explicit InterfaceRegistry(Connection* connection); | 63 explicit InterfaceRegistry(Connection* connection); |
64 // Construct with an InterfaceProviderRequest and a Connection (which may be | 64 // Construct with an InterfaceProviderRequest and a Connection (which may be |
65 // null, see note above about filtering). | 65 // null, see note above about filtering). |
66 InterfaceRegistry(shell::mojom::InterfaceProviderRequest request, | 66 InterfaceRegistry(mojom::InterfaceProviderRequest request, |
67 Connection* connection); | 67 Connection* connection); |
68 ~InterfaceRegistry() override; | 68 ~InterfaceRegistry() override; |
69 | 69 |
70 // Takes the client end of the InterfaceProvider pipe created in the | 70 // Takes the client end of the InterfaceProvider pipe created in the |
71 // constructor. | 71 // constructor. |
72 shell::mojom::InterfaceProviderPtr TakeClientHandle(); | 72 mojom::InterfaceProviderPtr TakeClientHandle(); |
73 | 73 |
74 template <typename Interface> | 74 template <typename Interface> |
75 bool AddInterface(InterfaceFactory<Interface>* factory) { | 75 bool AddInterface(InterfaceFactory<Interface>* factory) { |
76 return SetInterfaceBinderForName( | 76 return SetInterfaceBinderForName( |
77 new internal::InterfaceFactoryBinder<Interface>(factory), | 77 new internal::InterfaceFactoryBinder<Interface>(factory), |
78 Interface::Name_); | 78 Interface::Name_); |
79 } | 79 } |
80 | 80 |
81 void set_default_binder(InterfaceBinder* binder) { default_binder_ = binder; } | 81 void set_default_binder(InterfaceBinder* binder) { default_binder_ = binder; } |
82 | 82 |
83 private: | 83 private: |
84 using NameToInterfaceBinderMap = std::map<std::string, InterfaceBinder*>; | 84 using NameToInterfaceBinderMap = std::map<std::string, InterfaceBinder*>; |
85 | 85 |
86 // shell::mojom::InterfaceProvider: | 86 // mojom::InterfaceProvider: |
87 void GetInterface(const String& interface_name, | 87 void GetInterface(const mojo::String& interface_name, |
88 ScopedMessagePipeHandle handle) override; | 88 mojo::ScopedMessagePipeHandle handle) override; |
89 | 89 |
90 // Returns true if the binder was set, false if it was not set (e.g. by | 90 // Returns true if the binder was set, false if it was not set (e.g. by |
91 // some filtering policy preventing this interface from being exposed). | 91 // some filtering policy preventing this interface from being exposed). |
92 bool SetInterfaceBinderForName(InterfaceBinder* binder, | 92 bool SetInterfaceBinderForName(InterfaceBinder* binder, |
93 const std::string& name); | 93 const std::string& name); |
94 | 94 |
95 void RemoveInterfaceBinderForName(const std::string& interface_name); | 95 void RemoveInterfaceBinderForName(const std::string& interface_name); |
96 | 96 |
97 shell::mojom::InterfaceProviderPtr client_handle_; | 97 mojom::InterfaceProviderPtr client_handle_; |
98 Binding<shell::mojom::InterfaceProvider> binding_; | 98 mojo::Binding<mojom::InterfaceProvider> binding_; |
99 Connection* connection_; | 99 Connection* connection_; |
100 | 100 |
101 InterfaceBinder* default_binder_; | 101 InterfaceBinder* default_binder_; |
102 NameToInterfaceBinderMap name_to_binder_; | 102 NameToInterfaceBinderMap name_to_binder_; |
103 | 103 |
104 DISALLOW_COPY_AND_ASSIGN(InterfaceRegistry); | 104 DISALLOW_COPY_AND_ASSIGN(InterfaceRegistry); |
105 }; | 105 }; |
106 | 106 |
107 } // namespace mojo | 107 } // namespace shell |
108 | 108 |
109 #endif // SERVICES_SHELL_PUBLIC_CPP_INTERFACE_REGISTRY_H_ | 109 #endif // SERVICES_SHELL_PUBLIC_CPP_INTERFACE_REGISTRY_H_ |
OLD | NEW |