OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 MOJO_PUBLIC_APPLICATION_SERVICE_PROVIDER_IMPL_H_ | 5 #ifndef MOJO_PUBLIC_APPLICATION_SERVICE_PROVIDER_IMPL_H_ |
6 #define MOJO_PUBLIC_APPLICATION_SERVICE_PROVIDER_IMPL_H_ | 6 #define MOJO_PUBLIC_APPLICATION_SERVICE_PROVIDER_IMPL_H_ |
7 | 7 |
8 #include <functional> | 8 #include <functional> |
| 9 #include <map> |
9 #include <string> | 10 #include <string> |
10 #include <utility> | 11 #include <utility> |
11 | 12 |
12 #include "mojo/public/cpp/application/connection_context.h" | 13 #include "mojo/public/cpp/application/connection_context.h" |
13 #include "mojo/public/cpp/application/lib/service_connector_registry.h" | |
14 #include "mojo/public/cpp/application/service_connector.h" | 14 #include "mojo/public/cpp/application/service_connector.h" |
15 #include "mojo/public/cpp/bindings/binding.h" | 15 #include "mojo/public/cpp/bindings/binding.h" |
16 #include "mojo/public/interfaces/application/service_provider.mojom.h" | 16 #include "mojo/public/interfaces/application/service_provider.mojom.h" |
17 | 17 |
18 namespace mojo { | 18 namespace mojo { |
19 | 19 |
20 // An implementation of |ServiceProvider|, which can be customized appropriately | 20 // An implementation of |ServiceProvider|, which can be customized appropriately |
21 // (to select what services it provides). | 21 // (to select what services it provides). |
22 class ServiceProviderImpl : public ServiceProvider { | 22 class ServiceProviderImpl : public ServiceProvider { |
23 public: | 23 public: |
(...skipping 22 matching lines...) Expand all Loading... |
46 // Binds this service provider implementation to the given interface request. | 46 // Binds this service provider implementation to the given interface request. |
47 // This may only be called if this object is unbound. | 47 // This may only be called if this object is unbound. |
48 void Bind(const ConnectionContext& connection_context, | 48 void Bind(const ConnectionContext& connection_context, |
49 InterfaceRequest<ServiceProvider> service_provider_request); | 49 InterfaceRequest<ServiceProvider> service_provider_request); |
50 | 50 |
51 // Disconnect this service provider implementation and put it in a state where | 51 // Disconnect this service provider implementation and put it in a state where |
52 // it can be rebound to a new request (i.e., restores this object to an | 52 // it can be rebound to a new request (i.e., restores this object to an |
53 // unbound state). This may be called even if this object is already unbound. | 53 // unbound state). This may be called even if this object is already unbound. |
54 void Close(); | 54 void Close(); |
55 | 55 |
56 // Adds a supported service with the given |name|, using the given | 56 // Adds a supported service with the given |service_name|, using the given |
57 // |service_connector|. | 57 // |service_connector|. |
58 void AddServiceForName(std::unique_ptr<ServiceConnector> service_connector, | 58 void AddServiceForName(std::unique_ptr<ServiceConnector> service_connector, |
59 const std::string& interface_name) { | 59 const std::string& service_name); |
60 service_connector_registry_.SetServiceConnectorForName( | |
61 std::move(service_connector), interface_name); | |
62 } | |
63 | 60 |
64 // Adds a supported service with the given |name|, using the given | 61 // Adds a supported service with the given |service_name|, using the given |
65 // |interface_request_handler| (see above for information about | 62 // |interface_request_handler| (see above for information about |
66 // |InterfaceRequestHandler<Interface>|). |interface_request_handler| should | 63 // |InterfaceRequestHandler<Interface>|). |interface_request_handler| should |
67 // remain valid for the lifetime of this object. | 64 // remain valid for the lifetime of this object. |
68 // | 65 // |
69 // A typical usage may be: | 66 // A typical usage may be: |
70 // | 67 // |
71 // service_provider_impl_->AddService<Foobar>( | 68 // service_provider_impl_->AddService<Foobar>( |
72 // [](const ConnectionContext& connection_context, | 69 // [](const ConnectionContext& connection_context, |
73 // InterfaceRequest<FooBar> foobar_request) { | 70 // InterfaceRequest<FooBar> foobar_request) { |
74 // // |FoobarImpl| owns itself. | 71 // // |FoobarImpl| owns itself. |
75 // new FoobarImpl(std::move(foobar_request)); | 72 // new FoobarImpl(std::move(foobar_request)); |
76 // }); | 73 // }); |
77 template <typename Interface> | 74 template <typename Interface> |
78 void AddService(InterfaceRequestHandler<Interface> interface_request_handler, | 75 void AddService(InterfaceRequestHandler<Interface> interface_request_handler, |
79 const std::string& name = Interface::Name_) { | 76 const std::string& service_name = Interface::Name_) { |
80 AddServiceForName( | 77 AddServiceForName( |
81 std::unique_ptr<ServiceConnector>(new ServiceConnectorImpl<Interface>( | 78 std::unique_ptr<ServiceConnector>(new ServiceConnectorImpl<Interface>( |
82 std::move(interface_request_handler))), | 79 std::move(interface_request_handler))), |
83 name); | 80 service_name); |
84 } | 81 } |
85 | 82 |
86 // Removes support for the service with the given |name|. | 83 // Removes support for the service with the given |service_name|. |
87 void RemoveServiceForName(const std::string& name) { | 84 void RemoveServiceForName(const std::string& service_name); |
88 service_connector_registry_.RemoveServiceConnectorForName(name); | |
89 } | |
90 | 85 |
91 // Like |RemoveServiceForName()| (above), but designed so that it can be used | 86 // Like |RemoveServiceForName()| (above), but designed so that it can be used |
92 // like |RemoveService<Interface>()| or even |RemoveService<Interface>(name)| | 87 // like |RemoveService<Interface>()| or even |
93 // (to parallel |AddService<Interface>()|). | 88 // |RemoveService<Interface>(service_name)| (to parallel |
| 89 // |AddService<Interface>()|). |
94 template <typename Interface> | 90 template <typename Interface> |
95 void RemoveService(const std::string& name = Interface::Name_) { | 91 void RemoveService(const std::string& service_name = Interface::Name_) { |
96 RemoveServiceForName(name); | 92 RemoveServiceForName(service_name); |
97 } | 93 } |
98 | 94 |
99 // This uses the provided |fallback_service_provider| for connection requests | 95 // This uses the provided |fallback_service_provider| for connection requests |
100 // for services that are not known (haven't been added). (Set it to null to | 96 // for services that are not known (haven't been added). (Set it to null to |
101 // not have any fallback.) A fallback must outlive this object (or until it is | 97 // not have any fallback.) A fallback must outlive this object (or until it is |
102 // "cleared" or replaced by a different fallback. | 98 // "cleared" or replaced by a different fallback. |
103 void set_fallback_service_provider( | 99 void set_fallback_service_provider( |
104 ServiceProvider* fallback_service_provider) { | 100 ServiceProvider* fallback_service_provider) { |
105 fallback_service_provider_ = fallback_service_provider; | 101 fallback_service_provider_ = fallback_service_provider; |
106 } | 102 } |
(...skipping 10 matching lines...) Expand all Loading... |
117 // |InterfaceRequestHandler<Interface>|. | 113 // |InterfaceRequestHandler<Interface>|. |
118 template <typename Interface> | 114 template <typename Interface> |
119 class ServiceConnectorImpl : public ServiceConnector { | 115 class ServiceConnectorImpl : public ServiceConnector { |
120 public: | 116 public: |
121 explicit ServiceConnectorImpl( | 117 explicit ServiceConnectorImpl( |
122 InterfaceRequestHandler<Interface> interface_request_handler) | 118 InterfaceRequestHandler<Interface> interface_request_handler) |
123 : interface_request_handler_(std::move(interface_request_handler)) {} | 119 : interface_request_handler_(std::move(interface_request_handler)) {} |
124 ~ServiceConnectorImpl() override {} | 120 ~ServiceConnectorImpl() override {} |
125 | 121 |
126 void ConnectToService(const mojo::ConnectionContext& connection_context, | 122 void ConnectToService(const mojo::ConnectionContext& connection_context, |
127 const std::string& interface_name, | |
128 ScopedMessagePipeHandle client_handle) override { | 123 ScopedMessagePipeHandle client_handle) override { |
129 interface_request_handler_( | 124 interface_request_handler_( |
130 connection_context, | 125 connection_context, |
131 InterfaceRequest<Interface>(client_handle.Pass())); | 126 InterfaceRequest<Interface>(client_handle.Pass())); |
132 } | 127 } |
133 | 128 |
134 private: | 129 private: |
135 const InterfaceRequestHandler<Interface> interface_request_handler_; | 130 const InterfaceRequestHandler<Interface> interface_request_handler_; |
136 | 131 |
137 MOJO_DISALLOW_COPY_AND_ASSIGN(ServiceConnectorImpl); | 132 MOJO_DISALLOW_COPY_AND_ASSIGN(ServiceConnectorImpl); |
138 }; | 133 }; |
139 | 134 |
140 // Overridden from |ServiceProvider|: | 135 // Overridden from |ServiceProvider|: |
141 void ConnectToService(const String& service_name, | 136 void ConnectToService(const String& service_name, |
142 ScopedMessagePipeHandle client_handle) override; | 137 ScopedMessagePipeHandle client_handle) override; |
143 | 138 |
144 ConnectionContext connection_context_; | 139 ConnectionContext connection_context_; |
145 Binding<ServiceProvider> binding_; | 140 Binding<ServiceProvider> binding_; |
146 | 141 |
147 internal::ServiceConnectorRegistry service_connector_registry_; | 142 std::map<std::string, std::unique_ptr<ServiceConnector>> |
| 143 name_to_service_connector_; |
| 144 |
148 ServiceProvider* fallback_service_provider_; | 145 ServiceProvider* fallback_service_provider_; |
149 | 146 |
150 MOJO_DISALLOW_COPY_AND_ASSIGN(ServiceProviderImpl); | 147 MOJO_DISALLOW_COPY_AND_ASSIGN(ServiceProviderImpl); |
151 }; | 148 }; |
152 | 149 |
153 } // namespace mojo | 150 } // namespace mojo |
154 | 151 |
155 #endif // MOJO_PUBLIC_APPLICATION_SERVICE_PROVIDER_IMPL_H_ | 152 #endif // MOJO_PUBLIC_APPLICATION_SERVICE_PROVIDER_IMPL_H_ |
OLD | NEW |