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 : binding_(this), weak_factory_(this) {
15 ServiceRegistryImpl::~ServiceRegistryImpl() {
16 while (!pending_connects_
.empty()) {
17 mojo::CloseRaw(pending_connects_
.front().second
);
18 pending_connects_
.pop();
22 void ServiceRegistryImpl::Bind(
23 mojo::InterfaceRequest
<mojo::ServiceProvider
> request
) {
24 binding_
.Bind(request
.Pass());
27 void ServiceRegistryImpl::BindRemoteServiceProvider(
28 mojo::ServiceProviderPtr service_provider
) {
29 CHECK(!remote_provider_
);
30 remote_provider_
= service_provider
.Pass();
31 while (!pending_connects_
.empty()) {
32 remote_provider_
->ConnectToService(
33 mojo::String::From(pending_connects_
.front().first
),
34 mojo::ScopedMessagePipeHandle(pending_connects_
.front().second
));
35 pending_connects_
.pop();
39 void ServiceRegistryImpl::AddService(
40 const std::string
& service_name
,
41 const base::Callback
<void(mojo::ScopedMessagePipeHandle
)> service_factory
) {
42 service_factories_
[service_name
] = service_factory
;
45 void ServiceRegistryImpl::RemoveService(const std::string
& service_name
) {
46 service_factories_
.erase(service_name
);
49 void ServiceRegistryImpl::ConnectToRemoteService(
50 const base::StringPiece
& service_name
,
51 mojo::ScopedMessagePipeHandle handle
) {
52 if (!remote_provider_
) {
53 pending_connects_
.push(
54 std::make_pair(service_name
.as_string(), handle
.release()));
57 remote_provider_
->ConnectToService(mojo::String::From(service_name
),
61 base::WeakPtr
<ServiceRegistry
> ServiceRegistryImpl::GetWeakPtr() {
62 return weak_factory_
.GetWeakPtr();
65 void ServiceRegistryImpl::ConnectToService(
66 const mojo::String
& name
,
67 mojo::ScopedMessagePipeHandle client_handle
) {
69 base::Callback
<void(mojo::ScopedMessagePipeHandle
)> >::iterator it
=
70 service_factories_
.find(name
);
71 if (it
== service_factories_
.end())
74 it
->second
.Run(client_handle
.Pass());
77 } // namespace content