Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / content / public / common / service_registry.h
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
23 // ConnectToRemoteService.
24 class CONTENT_EXPORT ServiceRegistry {
25  public:
26   virtual ~ServiceRegistry() {}
27
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
33   // unaffected.
34   template <typename Interface>
35   void AddService(const base::Callback<void(mojo::InterfaceRequest<Interface>)>
36                       service_factory) {
37     AddService(Interface::Name_,
38                base::Bind(&ServiceRegistry::ForwardToServiceFactory<Interface>,
39                           service_factory));
40   }
41   virtual void AddService(
42       const std::string& service_name,
43       const base::Callback<void(mojo::ScopedMessagePipeHandle)>
44           service_factory) = 0;
45
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_);
51   }
52   virtual void RemoveService(const std::string& service_name) = 0;
53
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());
60   }
61   virtual void ConnectToRemoteService(const base::StringPiece& name,
62                                       mojo::ScopedMessagePipeHandle handle) = 0;
63
64  private:
65   template <typename Interface>
66   static void ForwardToServiceFactory(
67       const base::Callback<void(mojo::InterfaceRequest<Interface>)>
68           service_factory,
69       mojo::ScopedMessagePipeHandle handle) {
70     service_factory.Run(mojo::MakeRequest<Interface>(handle.Pass()));
71   }
72 };
73
74 }  // namespace content
75
76 #endif  // CONTENT_PUBLIC_COMMON_SERVICE_REGISTRY_H_