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 CONTENT_PUBLIC_COMMON_SERVICE_REGISTRY_H_ | |
6 #define CONTENT_PUBLIC_COMMON_SERVICE_REGISTRY_H_ | |
7 | |
8 #include <string> | |
9 | |
10 #include "base/bind.h" | |
11 #include "base/callback.h" | |
12 #include "base/strings/string_piece.h" | |
13 #include "content/common/content_export.h" | |
14 #include "mojo/public/cpp/bindings/interface_ptr.h" | |
15 #include "mojo/public/cpp/bindings/interface_request.h" | |
16 #include "mojo/public/cpp/system/core.h" | |
17 | |
18 namespace content { | |
19 | |
20 // A ServiceRegistry exposes local services that have been added using | |
21 // AddService to a paired remote ServiceRegistry and provides local access to | |
22 // services exposed by the remote ServiceRegistry through GetInterface. | |
23 class CONTENT_EXPORT ServiceRegistry { | |
24 public: | |
25 virtual ~ServiceRegistry() {} | |
26 | |
27 // Make the service created by |service_factory| available to the remote | |
28 // InterfaceProvider. In response to each request for a service, | |
29 // |service_factory| will be run with an InterfaceRequest<Interface> | |
30 // representing that request. | |
31 template <typename Interface> | |
32 void AddService(const base::Callback<void(mojo::InterfaceRequest<Interface>)> | |
33 service_factory) { | |
34 AddService(Interface::Name_, | |
35 base::Bind(&ServiceRegistry::ForwardToServiceFactory<Interface>, | |
36 service_factory)); | |
37 } | |
38 virtual void AddService( | |
39 const std::string& service_name, | |
40 const base::Callback<void(mojo::ScopedMessagePipeHandle)> | |
41 service_factory) = 0; | |
42 | |
43 // Remove future access to the service implementing Interface. Existing | |
44 // connections to the service are unaffected. | |
45 template <typename Interface> | |
46 void RemoveService() { | |
47 RemoveService(Interface::Name_); | |
48 } | |
49 virtual void RemoveService(const std::string& service_name) = 0; | |
50 | |
51 // Connect to an interface provided by the remote interface provider. | |
52 template <typename Interface> | |
53 void GetInterface(mojo::InterfacePtr<Interface>* ptr) { | |
darin (slow to review)
2014/06/18 00:09:29
nit: You might call this GetRemoteInterface for cl
Sam McNally
2014/06/18 01:31:40
Done.
| |
54 mojo::MessagePipe pipe; | |
55 ptr->Bind(pipe.handle0.Pass()); | |
56 GetInterface(Interface::Name_, pipe.handle1.Pass()); | |
57 } | |
58 virtual void GetInterface(const base::StringPiece& name, | |
59 mojo::ScopedMessagePipeHandle handle) = 0; | |
60 | |
61 private: | |
62 template <typename Interface> | |
63 static void ForwardToServiceFactory( | |
64 const base::Callback<void(mojo::InterfaceRequest<Interface>)> | |
65 service_factory, | |
66 mojo::ScopedMessagePipeHandle handle) { | |
67 service_factory.Run(mojo::MakeRequest<Interface>(handle.Pass())); | |
68 } | |
69 }; | |
70 | |
71 } // namespace content | |
72 | |
73 #endif // CONTENT_PUBLIC_COMMON_SERVICE_REGISTRY_H_ | |
OLD | NEW |