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.
5 #ifndef CONTENT_PUBLIC_COMMON_SERVICE_REGISTRY_H_
6 #define CONTENT_PUBLIC_COMMON_SERVICE_REGISTRY_H_
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 "third_party/mojo/src/mojo/public/cpp/bindings/interface_ptr.h"
15 #include "third_party/mojo/src/mojo/public/cpp/bindings/interface_request.h"
16 #include "third_party/mojo/src/mojo/public/cpp/system/core.h"
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
23 // ConnectToRemoteService.
24 class CONTENT_EXPORT ServiceRegistry
{
26 virtual ~ServiceRegistry() {}
28 // Make the service created by |service_factory| available to the remote
29 // ServiceProvider. In response to each request for a service,
30 // |service_factory| will be run with an InterfaceRequest<Interface>
31 // representing that request. Adding a factory for an already registered
32 // service will override the factory. Existing connections to the service are
34 template <typename Interface
>
35 void AddService(const base::Callback
<void(mojo::InterfaceRequest
<Interface
>)>
37 AddService(Interface::Name_
,
38 base::Bind(&ServiceRegistry::ForwardToServiceFactory
<Interface
>,
41 virtual void AddService(
42 const std::string
& service_name
,
43 const base::Callback
<void(mojo::ScopedMessagePipeHandle
)>
46 // Remove future access to the service implementing Interface. Existing
47 // connections to the service are unaffected.
48 template <typename Interface
>
49 void RemoveService() {
50 RemoveService(Interface::Name_
);
52 virtual void RemoveService(const std::string
& service_name
) = 0;
54 // Connect to an interface provided by the remote service provider.
55 template <typename Interface
>
56 void ConnectToRemoteService(mojo::InterfacePtr
<Interface
>* ptr
) {
57 mojo::MessagePipe pipe
;
58 ptr
->Bind(pipe
.handle0
.Pass());
59 ConnectToRemoteService(Interface::Name_
, pipe
.handle1
.Pass());
61 virtual void ConnectToRemoteService(const base::StringPiece
& name
,
62 mojo::ScopedMessagePipeHandle handle
) = 0;
65 template <typename Interface
>
66 static void ForwardToServiceFactory(
67 const base::Callback
<void(mojo::InterfaceRequest
<Interface
>)>
69 mojo::ScopedMessagePipeHandle handle
) {
70 service_factory
.Run(mojo::MakeRequest
<Interface
>(handle
.Pass()));
74 } // namespace content
76 #endif // CONTENT_PUBLIC_COMMON_SERVICE_REGISTRY_H_