summaryrefslogtreecommitdiffstats
path: root/mojo/public/cpp/application/service_provider_impl.h
blob: 45ad7b8456ea8e14c1df5a1fc235ecde2cebea53 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef MOJO_PUBLIC_APPLICATION_SERVICE_PROVIDER_IMPL_H_
#define MOJO_PUBLIC_APPLICATION_SERVICE_PROVIDER_IMPL_H_

#include "mojo/public/cpp/application/lib/service_connector.h"
#include "mojo/public/interfaces/application/service_provider.mojom.h"

namespace mojo {
namespace internal {
class WeakServiceProvider;
class ServiceConnectorBase;
}

// Implements a registry that can be used to expose services to another app.
class ServiceProviderImpl : public InterfaceImpl<ServiceProvider> {
 public:
  ServiceProviderImpl();
  ~ServiceProviderImpl() override;

  template <typename Interface>
  void AddService(InterfaceFactory<Interface>* factory) {
    AddServiceConnector(
        new internal::InterfaceFactoryConnector<Interface>(factory));
  }

  // Returns an instance of a ServiceProvider that weakly wraps this impl's
  // connection to some other app. Whereas the lifetime of an instance of
  // ServiceProviderImpl is bound to the lifetime of the pipe it
  // encapsulates, the lifetime of the ServiceProvider instance returned by this
  // method is assumed by the caller. After the pipe is closed
  // ConnectToService() calls on this object will be silently dropped.
  // This method must only be called once per ServiceProviderImpl.
  ServiceProvider* CreateRemoteServiceProvider();

 private:
  typedef std::map<std::string, internal::ServiceConnectorBase*>
      NameToServiceConnectorMap;

  friend class internal::WeakServiceProvider;

  // Overridden from ServiceProvider:
  void ConnectToService(const String& service_name,
                        ScopedMessagePipeHandle client_handle) override;

  // Overridden from InterfaceImpl:
  void OnConnectionError() override;

  void AddServiceConnector(internal::ServiceConnectorBase* service_connector);
  void RemoveServiceConnector(
      internal::ServiceConnectorBase* service_connector);

  void ClearRemote();

  NameToServiceConnectorMap service_connectors_;

  internal::WeakServiceProvider* remote_;

  MOJO_DISALLOW_COPY_AND_ASSIGN(ServiceProviderImpl);
};

}  // namespace mojo

#endif  // MOJO_PUBLIC_APPLICATION_SERVICE_PROVIDER_IMPL_H_