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

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: RequestHandler -> InterfaceRequestHandler 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 |InterfaceRequestHandler<Interface>| is simply a function that handles an
26 // interface request for |Interface|. If it determines (e.g., based on the
27 // given |ConnectionContext|) that the request should be "accepted", then it
28 // should "connect" ("take ownership of") request. Otherwise, it can simply
29 // drop |interface_request| (as implied by the interface).
30 template <typename Interface>
31 using InterfaceRequestHandler =
32 std::function<void(const ConnectionContext& connection_context,
33 InterfaceRequest<Interface> interface_request)>;
34
35 // Constructs this service provider implementation in an unbound state.
20 ServiceProviderImpl(); 36 ServiceProviderImpl();
21 explicit ServiceProviderImpl(InterfaceRequest<ServiceProvider> request); 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 // |InterfaceRequestHandler<I>|s.
42 explicit ServiceProviderImpl(
43 InterfaceRequest<ServiceProvider> service_provider_request);
44
22 ~ServiceProviderImpl() override; 45 ~ServiceProviderImpl() override;
23 46
24 void Bind(InterfaceRequest<ServiceProvider> request); 47 // Binds this service provider implementation to the given interface request.
25 // Disconnect this service provider and put it in a state where it can be 48 // This may only be called if this object is unbound.
26 // rebound to a new request. 49 // TODO(vtl): This should take a |ConnectionContext|, to provide
50 // |InterfaceRequestHandler<I>|s.
51 void Bind(InterfaceRequest<ServiceProvider> service_provider_request);
52
53 // Disconnect this service provider implementation and put it in a state where
54 // it can be rebound to a new request (i.e., restores this object to an
55 // unbound state). This may be called even if this object is already unbound.
27 void Close(); 56 void Close();
28 57
58 // TODO(vtl): Remove this.
29 template <typename Interface> 59 template <typename Interface>
30 void AddService(InterfaceFactory<Interface>* factory, 60 void AddService(InterfaceFactory<Interface>* factory,
31 const std::string& interface_name = Interface::Name_) { 61 const std::string& interface_name = Interface::Name_) {
32 service_connector_registry_.SetServiceConnectorForName( 62 service_connector_registry_.SetServiceConnectorForName(
33 std::unique_ptr<ServiceConnector>( 63 std::unique_ptr<ServiceConnector>(
34 new internal::InterfaceFactoryConnector<Interface>(factory)), 64 new internal::InterfaceFactoryConnector<Interface>(factory)),
35 interface_name); 65 interface_name);
36 } 66 }
37 67
38 // ServiceProviderImpl uses the fallback_service_provider_ whenever someone 68 // Adds a supported service with the given |name|, using the given
39 // asks a service that doesn't exist in the service_connector_registry_. 69 // |interface_request_handler| (see above for information about
70 // |InterfaceRequestHandler<Interface>|). |interface_request_handler| should
71 // remain valid for the lifetime of this object.
40 // 72 //
41 // Note: ServiceProviderImpl does not take ownership of |fallback|. The caller 73 // A typical usage may be:
42 // must ensure that |fallback| outlives the ServiceProviderImpl.
43 // 74 //
44 void set_fallback_service_provider(ServiceProvider* fallback) { 75 // service_provider_impl_->AddServiceNew<Foobar>(
45 fallback_service_provider_ = fallback; 76 // [](const ConnectionContext& connection_context,
77 // InterfaceRequest<FooBar> foobar_request) {
78 // // |FoobarImpl| owns itself.
79 // new FoobarImpl(std::move(foobar_request));
80 // });
81 // TODO(vtl): Remove the AddService() above and rename this to AddService().
82 template <typename Interface>
83 void AddServiceNew(
84 InterfaceRequestHandler<Interface> interface_request_handler,
85 const std::string& name = Interface::Name_) {
86 service_connector_registry_.SetServiceConnectorForName(
87 std::unique_ptr<ServiceConnector>(new ServiceConnectorImpl<Interface>(
88 std::move(interface_request_handler))),
89 name);
90 }
91
92 // This uses the provided |fallback_service_provider| for connection requests
93 // for services that are not known (haven't been added). (Set it to null to
94 // not have any fallback.) A fallback must outlive this object (or until it is
95 // "cleared" or replaced by a different fallback.
96 void set_fallback_service_provider(
97 ServiceProvider* fallback_service_provider) {
98 fallback_service_provider_ = fallback_service_provider;
46 } 99 }
47 100
48 private: 101 private:
49 // Overridden from ServiceProvider: 102 // Objects of this class are used to adapt a generic (untyped) connection
103 // request (i.e., |ServiceConnector::ConnectToService()|) to the type-safe
104 // |InterfaceRequestHandler<Interface>|.
105 template <typename Interface>
106 class ServiceConnectorImpl : public ServiceConnector {
107 public:
108 explicit ServiceConnectorImpl(
109 InterfaceRequestHandler<Interface> interface_request_handler)
110 : interface_request_handler_(std::move(interface_request_handler)) {}
111 ~ServiceConnectorImpl() override {}
112
113 void ConnectToService(ApplicationConnection* application_connection,
114 const std::string& interface_name,
115 ScopedMessagePipeHandle client_handle) override {
116 // TODO(vtl): This should be given a |const ConnectionContext&|, instead
117 // of an |ApplicationConnection*| -- which may be null!
118 interface_request_handler_(
119 application_connection
120 ? application_connection->GetConnectionContext()
121 : ConnectionContext(),
122 InterfaceRequest<Interface>(client_handle.Pass()));
123 }
124
125 private:
126 const InterfaceRequestHandler<Interface> interface_request_handler_;
127
128 MOJO_DISALLOW_COPY_AND_ASSIGN(ServiceConnectorImpl);
129 };
130
131 // Overridden from |ServiceProvider|:
50 void ConnectToService(const String& service_name, 132 void ConnectToService(const String& service_name,
51 ScopedMessagePipeHandle client_handle) override; 133 ScopedMessagePipeHandle client_handle) override;
52 134
53 Binding<ServiceProvider> binding_; 135 Binding<ServiceProvider> binding_;
54 136
55 internal::ServiceConnectorRegistry service_connector_registry_; 137 internal::ServiceConnectorRegistry service_connector_registry_;
56 ServiceProvider* fallback_service_provider_; 138 ServiceProvider* fallback_service_provider_;
57 139
58 MOJO_DISALLOW_COPY_AND_ASSIGN(ServiceProviderImpl); 140 MOJO_DISALLOW_COPY_AND_ASSIGN(ServiceProviderImpl);
59 }; 141 };
60 142
61 } // namespace mojo 143 } // namespace mojo
62 144
63 #endif // MOJO_PUBLIC_APPLICATION_SERVICE_PROVIDER_IMPL_H_ 145 #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