summaryrefslogtreecommitdiffstats
path: root/content/common/mojo/service_registry_impl.cc
blob: 4557598499cd7906bb5cc7d28559ec22a8150c7b (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// 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.

#include "content/common/mojo/service_registry_impl.h"

#include "mojo/common/common_type_converters.h"

namespace content {

ServiceRegistryImpl::ServiceRegistryImpl()
    : binding_(this), weak_factory_(this) {
  binding_.set_connection_error_handler(
      base::Bind(&ServiceRegistryImpl::OnConnectionError,
                 base::Unretained(this)));
}

ServiceRegistryImpl::~ServiceRegistryImpl() {
  while (!pending_connects_.empty()) {
    mojo::CloseRaw(pending_connects_.front().second);
    pending_connects_.pop();
  }
}

void ServiceRegistryImpl::Bind(
    mojo::InterfaceRequest<mojo::ServiceProvider> request) {
  binding_.Bind(request.Pass());
}

void ServiceRegistryImpl::BindRemoteServiceProvider(
    mojo::ServiceProviderPtr service_provider) {
  CHECK(!remote_provider_);
  remote_provider_ = service_provider.Pass();
  while (!pending_connects_.empty()) {
    remote_provider_->ConnectToService(
        mojo::String::From(pending_connects_.front().first),
        mojo::ScopedMessagePipeHandle(pending_connects_.front().second));
    pending_connects_.pop();
  }
}

void ServiceRegistryImpl::AddService(
    const std::string& service_name,
    const base::Callback<void(mojo::ScopedMessagePipeHandle)> service_factory) {
  service_factories_[service_name] = service_factory;
}

void ServiceRegistryImpl::RemoveService(const std::string& service_name) {
  service_factories_.erase(service_name);
}

void ServiceRegistryImpl::ConnectToRemoteService(
    const base::StringPiece& service_name,
    mojo::ScopedMessagePipeHandle handle) {
  if (!remote_provider_) {
    pending_connects_.push(
        std::make_pair(service_name.as_string(), handle.release()));
    return;
  }
  remote_provider_->ConnectToService(mojo::String::From(service_name),
                                     handle.Pass());
}

bool ServiceRegistryImpl::IsBound() const {
  return binding_.is_bound();
}

base::WeakPtr<ServiceRegistry> ServiceRegistryImpl::GetWeakPtr() {
  return weak_factory_.GetWeakPtr();
}

void ServiceRegistryImpl::ConnectToService(
    const mojo::String& name,
    mojo::ScopedMessagePipeHandle client_handle) {
  std::map<std::string,
           base::Callback<void(mojo::ScopedMessagePipeHandle)> >::iterator it =
      service_factories_.find(name);
  if (it == service_factories_.end())
    return;

  it->second.Run(client_handle.Pass());
}

void ServiceRegistryImpl::OnConnectionError() {
  binding_.Close();
}

}  // namespace content