Chromium Code Reviews| Index: content/common/mojo/service_registry_impl.cc |
| diff --git a/content/common/mojo/service_registry_impl.cc b/content/common/mojo/service_registry_impl.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..0006c939e03a2736e6109529277256d95ba7e432 |
| --- /dev/null |
| +++ b/content/common/mojo/service_registry_impl.cc |
| @@ -0,0 +1,79 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "content/common/mojo/service_registry_impl.h" |
| + |
| +#include "mojo/common/common_type_converters.h" |
| + |
| +namespace content { |
| + |
| +ServiceRegistryImpl::ServiceRegistryImpl() : bound_(false) { |
| +} |
| + |
| +ServiceRegistryImpl::ServiceRegistryImpl(mojo::ScopedMessagePipeHandle handle) |
| + : bound_(false) { |
| + BindRemoteServiceProvider(handle.Pass()); |
| +} |
| + |
| +ServiceRegistryImpl::~ServiceRegistryImpl() { |
| + while (!pending_connects_.empty()) { |
| + mojo::ScopedMessagePipeHandle handle(pending_connects_.front().second); |
|
darin (slow to review)
2014/06/21 04:33:35
You could also use mojo::CloseRaw here. That might
Sam McNally
2014/06/24 04:00:29
Done.
|
| + pending_connects_.pop(); |
| + } |
| +} |
| + |
| +void ServiceRegistryImpl::BindRemoteServiceProvider( |
| + mojo::ScopedMessagePipeHandle handle) { |
| + if (bound_) |
| + return; |
| + |
| + mojo::BindToPipe(this, handle.Pass()); |
| + bound_ = true; |
| + while (!pending_connects_.empty()) { |
| + client()->GetInterface( |
| + mojo::String::From(pending_connects_.front().first), |
| + mojo::ScopedMessagePipeHandle(pending_connects_.front().second)); |
| + pending_connects_.pop(); |
| + } |
| +} |
| + |
| +void ServiceRegistryImpl::OnConnectionError() { |
| + // TODO(sammc): Support reporting this to our owner. |
| + bound_ = false; |
| +} |
| + |
| +void ServiceRegistryImpl::AddService( |
| + const std::string& service_name, |
| + const base::Callback<void(mojo::ScopedMessagePipeHandle)> service_factory) { |
| + service_factories_[service_name] = service_factory; |
| +} |
| + |
| +void ServiceRegistryImpl::RemoveService(const std::string& service_name) { |
| + service_factories_.erase(service_name); |
| +} |
| + |
| +void ServiceRegistryImpl::GetRemoteInterface( |
| + const base::StringPiece& service_name, |
| + mojo::ScopedMessagePipeHandle handle) { |
| + if (!bound_) { |
| + pending_connects_.push( |
| + std::make_pair(service_name.as_string(), handle.release())); |
| + return; |
| + } |
| + client()->GetInterface(mojo::String::From(service_name), handle.Pass()); |
| +} |
| + |
| +void ServiceRegistryImpl::GetInterface( |
| + const mojo::String& name, |
| + mojo::ScopedMessagePipeHandle client_handle) { |
| + std::map<std::string, |
| + base::Callback<void(mojo::ScopedMessagePipeHandle)> >::iterator it = |
| + service_factories_.find(name); |
| + if (it == service_factories_.end()) |
| + return; |
| + |
| + it->second.Run(client_handle.Pass()); |
| +} |
| + |
| +} // namespace content |