| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/common/mojo/service_registry_impl.h" | 5 #include "content/common/mojo/service_registry_impl.h" |
| 6 | 6 |
| 7 #include <utility> |
| 8 |
| 7 #include "mojo/common/common_type_converters.h" | 9 #include "mojo/common/common_type_converters.h" |
| 8 | 10 |
| 9 namespace content { | 11 namespace content { |
| 10 | 12 |
| 11 ServiceRegistryImpl::ServiceRegistryImpl() | 13 ServiceRegistryImpl::ServiceRegistryImpl() |
| 12 : binding_(this), weak_factory_(this) {} | 14 : binding_(this), weak_factory_(this) {} |
| 13 | 15 |
| 14 ServiceRegistryImpl::~ServiceRegistryImpl() { | 16 ServiceRegistryImpl::~ServiceRegistryImpl() { |
| 15 while (!pending_connects_.empty()) { | 17 while (!pending_connects_.empty()) { |
| 16 mojo::CloseRaw(pending_connects_.front().second); | 18 mojo::CloseRaw(pending_connects_.front().second); |
| 17 pending_connects_.pop(); | 19 pending_connects_.pop(); |
| 18 } | 20 } |
| 19 } | 21 } |
| 20 | 22 |
| 21 void ServiceRegistryImpl::Bind( | 23 void ServiceRegistryImpl::Bind( |
| 22 mojo::InterfaceRequest<mojo::ServiceProvider> request) { | 24 mojo::InterfaceRequest<mojo::ServiceProvider> request) { |
| 23 binding_.Bind(request.Pass()); | 25 binding_.Bind(std::move(request)); |
| 24 binding_.set_connection_error_handler(base::Bind( | 26 binding_.set_connection_error_handler(base::Bind( |
| 25 &ServiceRegistryImpl::OnConnectionError, base::Unretained(this))); | 27 &ServiceRegistryImpl::OnConnectionError, base::Unretained(this))); |
| 26 } | 28 } |
| 27 | 29 |
| 28 void ServiceRegistryImpl::BindRemoteServiceProvider( | 30 void ServiceRegistryImpl::BindRemoteServiceProvider( |
| 29 mojo::ServiceProviderPtr service_provider) { | 31 mojo::ServiceProviderPtr service_provider) { |
| 30 CHECK(!remote_provider_); | 32 CHECK(!remote_provider_); |
| 31 remote_provider_ = service_provider.Pass(); | 33 remote_provider_ = std::move(service_provider); |
| 32 while (!pending_connects_.empty()) { | 34 while (!pending_connects_.empty()) { |
| 33 remote_provider_->ConnectToService( | 35 remote_provider_->ConnectToService( |
| 34 mojo::String::From(pending_connects_.front().first), | 36 mojo::String::From(pending_connects_.front().first), |
| 35 mojo::ScopedMessagePipeHandle(pending_connects_.front().second)); | 37 mojo::ScopedMessagePipeHandle(pending_connects_.front().second)); |
| 36 pending_connects_.pop(); | 38 pending_connects_.pop(); |
| 37 } | 39 } |
| 38 } | 40 } |
| 39 | 41 |
| 40 void ServiceRegistryImpl::AddService( | 42 void ServiceRegistryImpl::AddService( |
| 41 const std::string& service_name, | 43 const std::string& service_name, |
| 42 const base::Callback<void(mojo::ScopedMessagePipeHandle)> service_factory) { | 44 const base::Callback<void(mojo::ScopedMessagePipeHandle)> service_factory) { |
| 43 service_factories_[service_name] = service_factory; | 45 service_factories_[service_name] = service_factory; |
| 44 } | 46 } |
| 45 | 47 |
| 46 void ServiceRegistryImpl::RemoveService(const std::string& service_name) { | 48 void ServiceRegistryImpl::RemoveService(const std::string& service_name) { |
| 47 service_factories_.erase(service_name); | 49 service_factories_.erase(service_name); |
| 48 } | 50 } |
| 49 | 51 |
| 50 void ServiceRegistryImpl::ConnectToRemoteService( | 52 void ServiceRegistryImpl::ConnectToRemoteService( |
| 51 const base::StringPiece& service_name, | 53 const base::StringPiece& service_name, |
| 52 mojo::ScopedMessagePipeHandle handle) { | 54 mojo::ScopedMessagePipeHandle handle) { |
| 53 if (!remote_provider_) { | 55 if (!remote_provider_) { |
| 54 pending_connects_.push( | 56 pending_connects_.push( |
| 55 std::make_pair(service_name.as_string(), handle.release())); | 57 std::make_pair(service_name.as_string(), handle.release())); |
| 56 return; | 58 return; |
| 57 } | 59 } |
| 58 remote_provider_->ConnectToService( | 60 remote_provider_->ConnectToService( |
| 59 mojo::String::From(service_name.as_string()), handle.Pass()); | 61 mojo::String::From(service_name.as_string()), std::move(handle)); |
| 60 } | 62 } |
| 61 | 63 |
| 62 bool ServiceRegistryImpl::IsBound() const { | 64 bool ServiceRegistryImpl::IsBound() const { |
| 63 return binding_.is_bound(); | 65 return binding_.is_bound(); |
| 64 } | 66 } |
| 65 | 67 |
| 66 base::WeakPtr<ServiceRegistry> ServiceRegistryImpl::GetWeakPtr() { | 68 base::WeakPtr<ServiceRegistry> ServiceRegistryImpl::GetWeakPtr() { |
| 67 return weak_factory_.GetWeakPtr(); | 69 return weak_factory_.GetWeakPtr(); |
| 68 } | 70 } |
| 69 | 71 |
| 70 void ServiceRegistryImpl::ConnectToService( | 72 void ServiceRegistryImpl::ConnectToService( |
| 71 const mojo::String& name, | 73 const mojo::String& name, |
| 72 mojo::ScopedMessagePipeHandle client_handle) { | 74 mojo::ScopedMessagePipeHandle client_handle) { |
| 73 std::map<std::string, | 75 std::map<std::string, |
| 74 base::Callback<void(mojo::ScopedMessagePipeHandle)> >::iterator it = | 76 base::Callback<void(mojo::ScopedMessagePipeHandle)> >::iterator it = |
| 75 service_factories_.find(name); | 77 service_factories_.find(name); |
| 76 if (it == service_factories_.end()) | 78 if (it == service_factories_.end()) |
| 77 return; | 79 return; |
| 78 | 80 |
| 79 // It's possible and effectively unavoidable that under certain conditions | 81 // It's possible and effectively unavoidable that under certain conditions |
| 80 // an invalid handle may be received. Don't invoke the factory in that case. | 82 // an invalid handle may be received. Don't invoke the factory in that case. |
| 81 if (!client_handle.is_valid()) { | 83 if (!client_handle.is_valid()) { |
| 82 DVLOG(2) << "Invalid pipe handle for " << name << " interface request."; | 84 DVLOG(2) << "Invalid pipe handle for " << name << " interface request."; |
| 83 return; | 85 return; |
| 84 } | 86 } |
| 85 | 87 |
| 86 it->second.Run(client_handle.Pass()); | 88 it->second.Run(std::move(client_handle)); |
| 87 } | 89 } |
| 88 | 90 |
| 89 void ServiceRegistryImpl::OnConnectionError() { | 91 void ServiceRegistryImpl::OnConnectionError() { |
| 90 binding_.Close(); | 92 binding_.Close(); |
| 91 } | 93 } |
| 92 | 94 |
| 93 } // namespace content | 95 } // namespace content |
| OLD | NEW |