summaryrefslogtreecommitdiffstats
path: root/content/common/mojo/service_registry_impl.h
blob: d3ca173738df63071ee4048a47c656b3148b04c9 (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
// 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 CONTENT_COMMON_MOJO_SERVICE_REGISTRY_IMPL_H_
#define CONTENT_COMMON_MOJO_SERVICE_REGISTRY_IMPL_H_

#include <map>
#include <queue>
#include <string>
#include <utility>

#include "base/callback.h"
#include "base/memory/weak_ptr.h"
#include "content/public/common/service_registry.h"
#include "mojo/public/cpp/bindings/interface_impl.h"
#include "mojo/public/cpp/system/core.h"
#include "mojo/public/interfaces/service_provider/service_provider.mojom.h"

namespace content {

class ServiceRegistryImpl : public ServiceRegistry,
                            public mojo::InterfaceImpl<mojo::ServiceProvider> {
 public:
  ServiceRegistryImpl();
  explicit ServiceRegistryImpl(mojo::ScopedMessagePipeHandle handle);
  virtual ~ServiceRegistryImpl();

  // Binds to a remote ServiceProvider. This will expose added services to the
  // remote ServiceProvider with the corresponding handle and enable
  // ConnectToRemoteService to provide access to services exposed by the remote
  // ServiceProvider.
  void BindRemoteServiceProvider(mojo::ScopedMessagePipeHandle handle);

  // ServiceRegistry overrides.
  virtual void AddService(
      const std::string& service_name,
      const base::Callback<void(mojo::ScopedMessagePipeHandle)> service_factory)
      OVERRIDE;
  virtual void RemoveService(const std::string& service_name) OVERRIDE;
  virtual void ConnectToRemoteService(
      const base::StringPiece& service_name,
      mojo::ScopedMessagePipeHandle handle) OVERRIDE;

  base::WeakPtr<ServiceRegistry> GetWeakPtr();

 private:
  // mojo::InterfaceImpl<mojo::ServiceProvider> overrides.
  virtual void ConnectToService(
      const mojo::String& name,
      mojo::ScopedMessagePipeHandle client_handle) OVERRIDE;
  virtual void OnConnectionError() OVERRIDE;

  std::map<std::string, base::Callback<void(mojo::ScopedMessagePipeHandle)> >
      service_factories_;
  std::queue<std::pair<std::string, mojo::MessagePipeHandle> >
      pending_connects_;
  bool bound_;

  base::WeakPtrFactory<ServiceRegistry> weak_factory_;
};

}  // namespace content

#endif  // CONTENT_COMMON_MOJO_SERVICE_REGISTRY_IMPL_H_