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 MOJO_PUBLIC_APPLICATION_SERVICE_PROVIDER_IMPL_H_
6#define MOJO_PUBLIC_APPLICATION_SERVICE_PROVIDER_IMPL_H_
7
8#include "mojo/public/cpp/application/lib/service_connector.h"
9#include "mojo/public/interfaces/application/service_provider.mojom.h"
10
11namespace mojo {
12namespace internal {
13class WeakServiceProvider;
14class ServiceConnectorBase;
15}
16
17// Implements a registry that can be used to expose services to another app.
18class ServiceProviderImpl : public InterfaceImpl<ServiceProvider> {
19 public:
20  ServiceProviderImpl();
21  virtual ~ServiceProviderImpl();
22
23  template <typename Interface>
24  void AddService(InterfaceFactory<Interface>* factory) {
25    AddServiceConnector(
26        new internal::InterfaceFactoryConnector<Interface>(factory));
27  }
28
29  // Returns an instance of a ServiceProvider that weakly wraps this impl's
30  // connection to some other app. Whereas the lifetime of an instance of
31  // ServiceProviderImpl is bound to the lifetime of the pipe it
32  // encapsulates, the lifetime of the ServiceProvider instance returned by this
33  // method is assumed by the caller. After the pipe is closed
34  // ConnectToService() calls on this object will be silently dropped.
35  // This method must only be called once per ServiceProviderImpl.
36  ServiceProvider* CreateRemoteServiceProvider();
37
38 private:
39  typedef std::map<std::string, internal::ServiceConnectorBase*>
40      NameToServiceConnectorMap;
41
42  friend class internal::WeakServiceProvider;
43
44  // Overridden from ServiceProvider:
45  virtual void ConnectToService(
46      const String& service_name,
47      ScopedMessagePipeHandle client_handle) MOJO_OVERRIDE;
48
49  // Overridden from InterfaceImpl:
50  virtual void OnConnectionError() MOJO_OVERRIDE;
51
52  void AddServiceConnector(
53      internal::ServiceConnectorBase* service_connector);
54  void RemoveServiceConnector(
55      internal::ServiceConnectorBase* service_connector);
56
57  void ClearRemote();
58
59  NameToServiceConnectorMap service_connectors_;
60
61  internal::WeakServiceProvider* remote_;
62
63  MOJO_DISALLOW_COPY_AND_ASSIGN(ServiceProviderImpl);
64};
65
66}  // namespace mojo
67
68#endif  // MOJO_PUBLIC_APPLICATION_SERVICE_PROVIDER_IMPL_H_
69