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_error_handler(this);
16 ServiceRegistryImpl::~ServiceRegistryImpl() {
17 while (!pending_connects_
.empty()) {
18 mojo::CloseRaw(pending_connects_
.front().second
);
19 pending_connects_
.pop();
23 void ServiceRegistryImpl::Bind(
24 mojo::InterfaceRequest
<mojo::ServiceProvider
> request
) {
25 binding_
.Bind(request
.Pass());
28 void ServiceRegistryImpl::BindRemoteServiceProvider(
29 mojo::ServiceProviderPtr service_provider
) {
30 CHECK(!remote_provider_
);
31 remote_provider_
= service_provider
.Pass();
32 while (!pending_connects_
.empty()) {
33 remote_provider_
->ConnectToService(
34 mojo::String::From(pending_connects_
.front().first
),
35 mojo::ScopedMessagePipeHandle(pending_connects_
.front().second
));
36 pending_connects_
.pop();
40 void ServiceRegistryImpl::AddService(
41 const std::string
& service_name
,
42 const base::Callback
<void(mojo::ScopedMessagePipeHandle
)> service_factory
) {
43 service_factories_
[service_name
] = service_factory
;
46 void ServiceRegistryImpl::RemoveService(const std::string
& service_name
) {
47 service_factories_
.erase(service_name
);
50 void ServiceRegistryImpl::ConnectToRemoteService(
51 const base::StringPiece
& service_name
,
52 mojo::ScopedMessagePipeHandle handle
) {
53 if (!remote_provider_
) {
54 pending_connects_
.push(
55 std::make_pair(service_name
.as_string(), handle
.release()));
58 remote_provider_
->ConnectToService(mojo::String::From(service_name
),
62 bool ServiceRegistryImpl::IsBound() const {
63 return binding_
.is_bound();
66 base::WeakPtr
<ServiceRegistry
> ServiceRegistryImpl::GetWeakPtr() {
67 return weak_factory_
.GetWeakPtr();
70 void ServiceRegistryImpl::ConnectToService(
71 const mojo::String
& name
,
72 mojo::ScopedMessagePipeHandle client_handle
) {
74 base::Callback
<void(mojo::ScopedMessagePipeHandle
)> >::iterator it
=
75 service_factories_
.find(name
);
76 if (it
== service_factories_
.end())
79 it
->second
.Run(client_handle
.Pass());
82 void ServiceRegistryImpl::OnConnectionError() {
86 } // namespace content