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"
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
) {
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
) {
58 pending_connects_
.push(
59 std::make_pair(service_name
.as_string(), handle
.release()));
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
) {
73 base::Callback
<void(mojo::ScopedMessagePipeHandle
)> >::iterator it
=
74 service_factories_
.find(name
);
75 if (it
== service_factories_
.end())
78 it
->second
.Run(client_handle
.Pass());
81 } // namespace content