| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "content/common/mojo/service_registry_impl.h" | |
| 6 | |
| 7 #include "mojo/common/common_type_converters.h" | |
| 8 | |
| 9 namespace content { | |
| 10 | |
| 11 ServiceRegistryImpl::ServiceRegistryImpl() : bound_(false) { | |
| 12 } | |
| 13 | |
| 14 ServiceRegistryImpl::ServiceRegistryImpl(mojo::ScopedMessagePipeHandle handle) | |
| 15 : bound_(false) { | |
| 16 BindRemoteServiceProvider(handle.Pass()); | |
| 17 } | |
| 18 | |
| 19 ServiceRegistryImpl::~ServiceRegistryImpl() { | |
| 20 while (!pending_connects_.empty()) { | |
| 21 mojo::CloseRaw(pending_connects_.front().second); | |
| 22 pending_connects_.pop(); | |
| 23 } | |
| 24 } | |
| 25 | |
| 26 void ServiceRegistryImpl::BindRemoteServiceProvider( | |
| 27 mojo::ScopedMessagePipeHandle handle) { | |
| 28 if (bound_) | |
| 29 return; | |
| 30 | |
| 31 mojo::BindToPipe(this, handle.Pass()); | |
| 32 bound_ = true; | |
| 33 while (!pending_connects_.empty()) { | |
| 34 client()->GetInterface( | |
| 35 mojo::String::From(pending_connects_.front().first), | |
| 36 mojo::ScopedMessagePipeHandle(pending_connects_.front().second)); | |
| 37 pending_connects_.pop(); | |
| 38 } | |
| 39 } | |
| 40 | |
| 41 void ServiceRegistryImpl::OnConnectionError() { | |
| 42 // TODO(sammc): Support reporting this to our owner. | |
| 43 bound_ = false; | |
| 44 } | |
| 45 | |
| 46 void ServiceRegistryImpl::AddService( | |
| 47 const std::string& service_name, | |
| 48 const base::Callback<void(mojo::ScopedMessagePipeHandle)> service_factory) { | |
| 49 service_factories_[service_name] = service_factory; | |
| 50 } | |
| 51 | |
| 52 void ServiceRegistryImpl::RemoveService(const std::string& service_name) { | |
| 53 service_factories_.erase(service_name); | |
| 54 } | |
| 55 | |
| 56 void ServiceRegistryImpl::GetRemoteInterface( | |
| 57 const base::StringPiece& service_name, | |
| 58 mojo::ScopedMessagePipeHandle handle) { | |
| 59 if (!bound_) { | |
| 60 pending_connects_.push( | |
| 61 std::make_pair(service_name.as_string(), handle.release())); | |
| 62 return; | |
| 63 } | |
| 64 client()->GetInterface(mojo::String::From(service_name), handle.Pass()); | |
| 65 } | |
| 66 | |
| 67 void ServiceRegistryImpl::GetInterface( | |
| 68 const mojo::String& name, | |
| 69 mojo::ScopedMessagePipeHandle client_handle) { | |
| 70 std::map<std::string, | |
| 71 base::Callback<void(mojo::ScopedMessagePipeHandle)> >::iterator it = | |
| 72 service_factories_.find(name); | |
| 73 if (it == service_factories_.end()) | |
| 74 return; | |
| 75 | |
| 76 it->second.Run(client_handle.Pass()); | |
| 77 } | |
| 78 | |
| 79 } // namespace content | |
| OLD | NEW |