Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(408)

Side by Side Diff: mojo/public/cpp/application/service_provider_impl.h

Issue 1973653002: Add a new way of using ServiceProviderImpl without InterfaceFactory. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <string> 9 #include <string>
9 10
11 #include "mojo/public/cpp/application/application_connection.h"
12 #include "mojo/public/cpp/application/connection_context.h"
10 #include "mojo/public/cpp/application/lib/interface_factory_connector.h" 13 #include "mojo/public/cpp/application/lib/interface_factory_connector.h"
11 #include "mojo/public/cpp/application/lib/service_connector_registry.h" 14 #include "mojo/public/cpp/application/lib/service_connector_registry.h"
15 #include "mojo/public/cpp/application/service_connector.h"
12 #include "mojo/public/cpp/bindings/binding.h" 16 #include "mojo/public/cpp/bindings/binding.h"
13 #include "mojo/public/interfaces/application/service_provider.mojom.h" 17 #include "mojo/public/interfaces/application/service_provider.mojom.h"
14 18
15 namespace mojo { 19 namespace mojo {
16 20
17 // Implements a registry that can be used to expose services to another app. 21 // An implementation of |ServiceProvider|, which can be customized appropriately
22 // (to select what services it provides).
18 class ServiceProviderImpl : public ServiceProvider { 23 class ServiceProviderImpl : public ServiceProvider {
19 public: 24 public:
25 // A |RequestHandler<Interface>| is simply a function that handles a request
26 // for |Interface|. If it determines (e.g., based on the given
27 // |ConnectionContext|) that the request should be "accepted", then it should
28 // "connect" ("take ownership of") request. Otherwise, it can simply drop
29 // |request| (as implied by the interface).
30 template <typename Interface>
31 using RequestHandler =
vardhan 2016/05/11 23:09:35 nit: more specific typename? (sorry!)
32 std::function<void(const ConnectionContext& connection_context,
33 InterfaceRequest<Interface> request)>;
34
35 // Constructs this service provider implementation in an unbound state.
20 ServiceProviderImpl(); 36 ServiceProviderImpl();
37
38 // Constructs this service provider implementation, binding it to the given
39 // interface request.
40 // TODO(vtl): This should take a |ConnectionContext|, to provide
41 // |RequestHandler<I>|s.
21 explicit ServiceProviderImpl(InterfaceRequest<ServiceProvider> request); 42 explicit ServiceProviderImpl(InterfaceRequest<ServiceProvider> request);
43
22 ~ServiceProviderImpl() override; 44 ~ServiceProviderImpl() override;
23 45
46 // Binds this service provider implementation to the given interface request.
47 // This may only be called if this object is unbound.
48 // TODO(vtl): This should take a |ConnectionContext|, to provide
49 // |RequestHandler<I>|s.
24 void Bind(InterfaceRequest<ServiceProvider> request); 50 void Bind(InterfaceRequest<ServiceProvider> request);
25 // Disconnect this service provider and put it in a state where it can be 51
26 // rebound to a new request. 52 // Disconnect this service provider implementation and put it in a state where
53 // it can be rebound to a new request (i.e., restores this object to an
54 // unbound state). This may be called even if this object is already unbound.
27 void Close(); 55 void Close();
28 56
57 // TODO(vtl): Remove this.
29 template <typename Interface> 58 template <typename Interface>
30 void AddService(InterfaceFactory<Interface>* factory, 59 void AddService(InterfaceFactory<Interface>* factory,
31 const std::string& interface_name = Interface::Name_) { 60 const std::string& interface_name = Interface::Name_) {
32 service_connector_registry_.SetServiceConnectorForName( 61 service_connector_registry_.SetServiceConnectorForName(
33 std::unique_ptr<ServiceConnector>( 62 std::unique_ptr<ServiceConnector>(
34 new internal::InterfaceFactoryConnector<Interface>(factory)), 63 new internal::InterfaceFactoryConnector<Interface>(factory)),
35 interface_name); 64 interface_name);
36 } 65 }
37 66
38 // ServiceProviderImpl uses the fallback_service_provider_ whenever someone 67 // Adds a supported service with the given |name|, using the given
39 // asks a service that doesn't exist in the service_connector_registry_. 68 // |request_handler| (see above for information about
40 // 69 // |RequestHandler<Interface>|). |request_handler| should remain valid for the
41 // Note: ServiceProviderImpl does not take ownership of |fallback|. The caller 70 // lifetime of this object.
42 // must ensure that |fallback| outlives the ServiceProviderImpl. 71 // TODO(vtl): Remove the AddService() above and rename this to AddService().
43 // 72 template <typename Interface>
44 void set_fallback_service_provider(ServiceProvider* fallback) { 73 void AddServiceNew(RequestHandler<Interface> request_handler,
45 fallback_service_provider_ = fallback; 74 const std::string& name = Interface::Name_) {
75 service_connector_registry_.SetServiceConnectorForName(
76 std::unique_ptr<ServiceConnector>(
77 new ServiceConnectorImpl<Interface>(std::move(request_handler))),
78 name);
79 }
80
81 // This uses the provided |fallback_service_provider| for connection requests
82 // for services that are not known (haven't been added). (Set it to null to
83 // not have any fallback.) A fallback must outlive this object (or until it is
84 // "cleared" or replaced by a different fallback.
85 void set_fallback_service_provider(
86 ServiceProvider* fallback_service_provider) {
87 fallback_service_provider_ = fallback_service_provider;
46 } 88 }
47 89
48 private: 90 private:
49 // Overridden from ServiceProvider: 91 // Objects of this class are used to adapt a generic (untyped) connection
92 // request (i.e., |ServiceConnector::ConnectToService()|) to the type-safe
93 // |RequestHandler<Interface>|.
94 template <typename Interface>
95 class ServiceConnectorImpl : public ServiceConnector {
96 public:
97 explicit ServiceConnectorImpl(RequestHandler<Interface> request_handler)
98 : request_handler_(std::move(request_handler)) {}
99 ~ServiceConnectorImpl() override {}
100
101 void ConnectToService(ApplicationConnection* application_connection,
102 const std::string& interface_name,
103 ScopedMessagePipeHandle client_handle) override {
104 // TODO(vtl): This should be given a |const ConnectionContext&|, instead
105 // of an |ApplicationConnection*| -- which may be null!
106 request_handler_(application_connection
107 ? application_connection->GetConnectionContext()
108 : ConnectionContext(),
109 InterfaceRequest<Interface>(client_handle.Pass()));
110 }
111
112 private:
113 const RequestHandler<Interface> request_handler_;
114
115 MOJO_DISALLOW_COPY_AND_ASSIGN(ServiceConnectorImpl);
116 };
117
118 // Overridden from |ServiceProvider|:
50 void ConnectToService(const String& service_name, 119 void ConnectToService(const String& service_name,
51 ScopedMessagePipeHandle client_handle) override; 120 ScopedMessagePipeHandle client_handle) override;
52 121
53 Binding<ServiceProvider> binding_; 122 Binding<ServiceProvider> binding_;
54 123
55 internal::ServiceConnectorRegistry service_connector_registry_; 124 internal::ServiceConnectorRegistry service_connector_registry_;
56 ServiceProvider* fallback_service_provider_; 125 ServiceProvider* fallback_service_provider_;
57 126
58 MOJO_DISALLOW_COPY_AND_ASSIGN(ServiceProviderImpl); 127 MOJO_DISALLOW_COPY_AND_ASSIGN(ServiceProviderImpl);
59 }; 128 };
60 129
61 } // namespace mojo 130 } // namespace mojo
62 131
63 #endif // MOJO_PUBLIC_APPLICATION_SERVICE_PROVIDER_IMPL_H_ 132 #endif // MOJO_PUBLIC_APPLICATION_SERVICE_PROVIDER_IMPL_H_
OLDNEW
« no previous file with comments | « mojo/public/cpp/application/lib/service_registry.cc ('k') | mojo/public/cpp/application/tests/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698