Blink roll 25b6bd3a7a131ffe68d809546ad1a20707915cdc:3a503f41ae42e5b79cfcd2ff10e65afde...
[chromium-blink-merge.git] / content / common / mojo / service_registry_impl.cc
blob348d1d45c811a523af7213fca9cf933857d5e333
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.
5 #include "content/common/mojo/service_registry_impl.h"
7 #include "mojo/common/common_type_converters.h"
9 namespace content {
11 ServiceRegistryImpl::ServiceRegistryImpl()
12 : bound_(false), weak_factory_(this) {
15 ServiceRegistryImpl::ServiceRegistryImpl(mojo::ScopedMessagePipeHandle handle)
16 : bound_(false), weak_factory_(this) {
17 BindRemoteServiceProvider(handle.Pass());
20 ServiceRegistryImpl::~ServiceRegistryImpl() {
21 while (!pending_connects_.empty()) {
22 mojo::CloseRaw(pending_connects_.front().second);
23 pending_connects_.pop();
27 void ServiceRegistryImpl::BindRemoteServiceProvider(
28 mojo::ScopedMessagePipeHandle handle) {
29 CHECK(!bound_);
30 bound_ = true;
31 mojo::WeakBindToPipe(this, handle.Pass());
32 while (!pending_connects_.empty()) {
33 client()->ConnectToService(
34 mojo::String::From(pending_connects_.front().first),
35 mojo::ScopedMessagePipeHandle(pending_connects_.front().second));
36 pending_connects_.pop();
40 void ServiceRegistryImpl::OnConnectionError() {
41 // TODO(sammc): Support reporting this to our owner.
44 void ServiceRegistryImpl::AddService(
45 const std::string& service_name,
46 const base::Callback<void(mojo::ScopedMessagePipeHandle)> service_factory) {
47 service_factories_[service_name] = service_factory;
50 void ServiceRegistryImpl::RemoveService(const std::string& service_name) {
51 service_factories_.erase(service_name);
54 void ServiceRegistryImpl::ConnectToRemoteService(
55 const base::StringPiece& service_name,
56 mojo::ScopedMessagePipeHandle handle) {
57 if (!bound_) {
58 pending_connects_.push(
59 std::make_pair(service_name.as_string(), handle.release()));
60 return;
62 client()->ConnectToService(mojo::String::From(service_name), handle.Pass());
65 base::WeakPtr<ServiceRegistry> ServiceRegistryImpl::GetWeakPtr() {
66 return weak_factory_.GetWeakPtr();
69 void ServiceRegistryImpl::ConnectToService(
70 const mojo::String& name,
71 mojo::ScopedMessagePipeHandle client_handle) {
72 std::map<std::string,
73 base::Callback<void(mojo::ScopedMessagePipeHandle)> >::iterator it =
74 service_factories_.find(name);
75 if (it == service_factories_.end())
76 return;
78 it->second.Run(client_handle.Pass());
81 } // namespace content