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) {
13 binding_
.set_connection_error_handler(
14 base::Bind(&ServiceRegistryImpl::OnConnectionError
,
15 base::Unretained(this)));
18 ServiceRegistryImpl::~ServiceRegistryImpl() {
19 while (!pending_connects_
.empty()) {
20 mojo::CloseRaw(pending_connects_
.front().second
);
21 pending_connects_
.pop();
25 void ServiceRegistryImpl::Bind(
26 mojo::InterfaceRequest
<mojo::ServiceProvider
> request
) {
27 binding_
.Bind(request
.Pass());
30 void ServiceRegistryImpl::BindRemoteServiceProvider(
31 mojo::ServiceProviderPtr service_provider
) {
32 CHECK(!remote_provider_
);
33 remote_provider_
= service_provider
.Pass();
34 while (!pending_connects_
.empty()) {
35 remote_provider_
->ConnectToService(
36 mojo::String::From(pending_connects_
.front().first
),
37 mojo::ScopedMessagePipeHandle(pending_connects_
.front().second
));
38 pending_connects_
.pop();
42 void ServiceRegistryImpl::AddService(
43 const std::string
& service_name
,
44 const base::Callback
<void(mojo::ScopedMessagePipeHandle
)> service_factory
) {
45 service_factories_
[service_name
] = service_factory
;
48 void ServiceRegistryImpl::RemoveService(const std::string
& service_name
) {
49 service_factories_
.erase(service_name
);
52 void ServiceRegistryImpl::ConnectToRemoteService(
53 const base::StringPiece
& service_name
,
54 mojo::ScopedMessagePipeHandle handle
) {
55 if (!remote_provider_
) {
56 pending_connects_
.push(
57 std::make_pair(service_name
.as_string(), handle
.release()));
60 remote_provider_
->ConnectToService(
61 mojo::String::From(service_name
.as_string()), handle
.Pass());
64 bool ServiceRegistryImpl::IsBound() const {
65 return binding_
.is_bound();
68 base::WeakPtr
<ServiceRegistry
> ServiceRegistryImpl::GetWeakPtr() {
69 return weak_factory_
.GetWeakPtr();
72 void ServiceRegistryImpl::ConnectToService(
73 const mojo::String
& name
,
74 mojo::ScopedMessagePipeHandle client_handle
) {
76 base::Callback
<void(mojo::ScopedMessagePipeHandle
)> >::iterator it
=
77 service_factories_
.find(name
);
78 if (it
== service_factories_
.end())
81 it
->second
.Run(client_handle
.Pass());
84 void ServiceRegistryImpl::OnConnectionError() {
88 } // namespace content