OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef MOJO_PUBLIC_APPLICATION_SERVICE_PROVIDER_IMPL_H_ | |
6 #define MOJO_PUBLIC_APPLICATION_SERVICE_PROVIDER_IMPL_H_ | |
7 | |
8 #include <functional> | |
9 #include <map> | |
10 #include <string> | |
11 #include <utility> | |
12 | |
13 #include "mojo/public/cpp/application/connection_context.h" | |
14 #include "mojo/public/cpp/application/service_connector.h" | |
15 #include "mojo/public/cpp/bindings/binding.h" | |
16 #include "mojo/public/interfaces/application/service_provider.mojom.h" | |
17 | |
18 namespace mojo { | |
19 | |
20 // An implementation of |ServiceProvider|, which can be customized appropriately | |
21 // (to select what services it provides). | |
22 class ServiceProviderImpl : public ServiceProvider { | |
23 public: | |
24 // A |InterfaceRequestHandler<Interface>| is simply a function that handles an | |
25 // interface request for |Interface|. If it determines (e.g., based on the | |
26 // given |ConnectionContext|) that the request should be "accepted", then it | |
27 // should "connect" ("take ownership of") request. Otherwise, it can simply | |
28 // drop |interface_request| (as implied by the interface). | |
29 template <typename Interface> | |
30 using InterfaceRequestHandler = | |
31 std::function<void(const ConnectionContext& connection_context, | |
32 InterfaceRequest<Interface> interface_request)>; | |
33 | |
34 // Constructs this service provider implementation in an unbound state. | |
35 ServiceProviderImpl(); | |
36 | |
37 // Constructs this service provider implementation, binding it to the given | |
38 // interface request. Note: If |service_provider_request| is not valid | |
39 // ("pending"), then the object will be put into an unbound state. | |
40 explicit ServiceProviderImpl( | |
41 const ConnectionContext& connection_context, | |
42 InterfaceRequest<ServiceProvider> service_provider_request); | |
43 | |
44 ~ServiceProviderImpl() override; | |
45 | |
46 // Binds this service provider implementation to the given interface request. | |
47 // This may only be called if this object is unbound. | |
48 void Bind(const ConnectionContext& connection_context, | |
49 InterfaceRequest<ServiceProvider> service_provider_request); | |
50 | |
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 | |
53 // unbound state). This may be called even if this object is already unbound. | |
54 void Close(); | |
55 | |
56 // Adds a supported service with the given |service_name|, using the given | |
57 // |service_connector|. | |
58 void AddServiceForName(std::unique_ptr<ServiceConnector> service_connector, | |
59 const std::string& service_name); | |
60 | |
61 // Adds a supported service with the given |service_name|, using the given | |
62 // |interface_request_handler| (see above for information about | |
63 // |InterfaceRequestHandler<Interface>|). |interface_request_handler| should | |
64 // remain valid for the lifetime of this object. | |
65 // | |
66 // A typical usage may be: | |
67 // | |
68 // service_provider_impl_->AddService<Foobar>( | |
69 // [](const ConnectionContext& connection_context, | |
70 // InterfaceRequest<FooBar> foobar_request) { | |
71 // // |FoobarImpl| owns itself. | |
72 // new FoobarImpl(std::move(foobar_request)); | |
73 // }); | |
74 template <typename Interface> | |
75 void AddService(InterfaceRequestHandler<Interface> interface_request_handler, | |
76 const std::string& service_name = Interface::Name_) { | |
77 AddServiceForName( | |
78 std::unique_ptr<ServiceConnector>(new ServiceConnectorImpl<Interface>( | |
79 std::move(interface_request_handler))), | |
80 service_name); | |
81 } | |
82 | |
83 // Removes support for the service with the given |service_name|. | |
84 void RemoveServiceForName(const std::string& service_name); | |
85 | |
86 // Like |RemoveServiceForName()| (above), but designed so that it can be used | |
87 // like |RemoveService<Interface>()| or even | |
88 // |RemoveService<Interface>(service_name)| (to parallel | |
89 // |AddService<Interface>()|). | |
90 template <typename Interface> | |
91 void RemoveService(const std::string& service_name = Interface::Name_) { | |
92 RemoveServiceForName(service_name); | |
93 } | |
94 | |
95 // This uses the provided |fallback_service_provider| for connection requests | |
96 // for services that are not known (haven't been added). (Set it to null to | |
97 // not have any fallback.) A fallback must outlive this object (or until it is | |
98 // "cleared" or replaced by a different fallback. | |
99 void set_fallback_service_provider( | |
100 ServiceProvider* fallback_service_provider) { | |
101 fallback_service_provider_ = fallback_service_provider; | |
102 } | |
103 | |
104 // Gets the context for the connection that this object is bound to (if not | |
105 // bound, the context is just a default-initialized |ConnectionContext|). | |
106 const ConnectionContext& connection_context() const { | |
107 return connection_context_; | |
108 } | |
109 | |
110 private: | |
111 // Objects of this class are used to adapt a generic (untyped) connection | |
112 // request (i.e., |ServiceConnector::ConnectToService()|) to the type-safe | |
113 // |InterfaceRequestHandler<Interface>|. | |
114 template <typename Interface> | |
115 class ServiceConnectorImpl : public ServiceConnector { | |
116 public: | |
117 explicit ServiceConnectorImpl( | |
118 InterfaceRequestHandler<Interface> interface_request_handler) | |
119 : interface_request_handler_(std::move(interface_request_handler)) {} | |
120 ~ServiceConnectorImpl() override {} | |
121 | |
122 void ConnectToService(const mojo::ConnectionContext& connection_context, | |
123 ScopedMessagePipeHandle client_handle) override { | |
124 interface_request_handler_( | |
125 connection_context, | |
126 InterfaceRequest<Interface>(client_handle.Pass())); | |
127 } | |
128 | |
129 private: | |
130 const InterfaceRequestHandler<Interface> interface_request_handler_; | |
131 | |
132 MOJO_DISALLOW_COPY_AND_ASSIGN(ServiceConnectorImpl); | |
133 }; | |
134 | |
135 // Overridden from |ServiceProvider|: | |
136 void ConnectToService(const String& service_name, | |
137 ScopedMessagePipeHandle client_handle) override; | |
138 | |
139 ConnectionContext connection_context_; | |
140 Binding<ServiceProvider> binding_; | |
141 | |
142 std::map<std::string, std::unique_ptr<ServiceConnector>> | |
143 name_to_service_connector_; | |
144 | |
145 ServiceProvider* fallback_service_provider_; | |
146 | |
147 MOJO_DISALLOW_COPY_AND_ASSIGN(ServiceProviderImpl); | |
148 }; | |
149 | |
150 } // namespace mojo | |
151 | |
152 #endif // MOJO_PUBLIC_APPLICATION_SERVICE_PROVIDER_IMPL_H_ | |
OLD | NEW |