| 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> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "mojo/common/common_type_converters.h" | 9 #include "mojo/common/common_type_converters.h" |
| 10 | 10 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 CHECK(!remote_provider_); | 32 CHECK(!remote_provider_); |
| 33 remote_provider_ = std::move(service_provider); | 33 remote_provider_ = std::move(service_provider); |
| 34 while (!pending_connects_.empty()) { | 34 while (!pending_connects_.empty()) { |
| 35 remote_provider_->ConnectToService( | 35 remote_provider_->ConnectToService( |
| 36 mojo::String::From(pending_connects_.front().first), | 36 mojo::String::From(pending_connects_.front().first), |
| 37 mojo::ScopedMessagePipeHandle(pending_connects_.front().second)); | 37 mojo::ScopedMessagePipeHandle(pending_connects_.front().second)); |
| 38 pending_connects_.pop(); | 38 pending_connects_.pop(); |
| 39 } | 39 } |
| 40 } | 40 } |
| 41 | 41 |
| 42 void ServiceRegistryImpl::AddService( | 42 void ServiceRegistryImpl::AddServiceOverride(const std::string& service_name, |
| 43 const std::string& service_name, | 43 const ServiceFactory& factory) { |
| 44 const base::Callback<void(mojo::ScopedMessagePipeHandle)> service_factory) { | 44 service_overrides_[service_name] = factory; |
| 45 } |
| 46 |
| 47 void ServiceRegistryImpl::AddService(const std::string& service_name, |
| 48 const ServiceFactory service_factory) { |
| 45 service_factories_[service_name] = service_factory; | 49 service_factories_[service_name] = service_factory; |
| 46 } | 50 } |
| 47 | 51 |
| 48 void ServiceRegistryImpl::RemoveService(const std::string& service_name) { | 52 void ServiceRegistryImpl::RemoveService(const std::string& service_name) { |
| 49 service_factories_.erase(service_name); | 53 service_factories_.erase(service_name); |
| 50 } | 54 } |
| 51 | 55 |
| 52 void ServiceRegistryImpl::ConnectToRemoteService( | 56 void ServiceRegistryImpl::ConnectToRemoteService( |
| 53 const base::StringPiece& service_name, | 57 const base::StringPiece& service_name, |
| 54 mojo::ScopedMessagePipeHandle handle) { | 58 mojo::ScopedMessagePipeHandle handle) { |
| 59 auto override_it = service_overrides_.find(service_name.as_string()); |
| 60 if (override_it != service_overrides_.end()) { |
| 61 override_it->second.Run(std::move(handle)); |
| 62 return; |
| 63 } |
| 64 |
| 55 if (!remote_provider_) { | 65 if (!remote_provider_) { |
| 56 pending_connects_.push( | 66 pending_connects_.push( |
| 57 std::make_pair(service_name.as_string(), handle.release())); | 67 std::make_pair(service_name.as_string(), handle.release())); |
| 58 return; | 68 return; |
| 59 } | 69 } |
| 60 remote_provider_->ConnectToService( | 70 remote_provider_->ConnectToService( |
| 61 mojo::String::From(service_name.as_string()), std::move(handle)); | 71 mojo::String::From(service_name.as_string()), std::move(handle)); |
| 62 } | 72 } |
| 63 | 73 |
| 64 bool ServiceRegistryImpl::IsBound() const { | 74 bool ServiceRegistryImpl::IsBound() const { |
| 65 return binding_.is_bound(); | 75 return binding_.is_bound(); |
| 66 } | 76 } |
| 67 | 77 |
| 68 base::WeakPtr<ServiceRegistry> ServiceRegistryImpl::GetWeakPtr() { | 78 base::WeakPtr<ServiceRegistry> ServiceRegistryImpl::GetWeakPtr() { |
| 69 return weak_factory_.GetWeakPtr(); | 79 return weak_factory_.GetWeakPtr(); |
| 70 } | 80 } |
| 71 | 81 |
| 72 void ServiceRegistryImpl::ConnectToService( | 82 void ServiceRegistryImpl::ConnectToService( |
| 73 const mojo::String& name, | 83 const mojo::String& name, |
| 74 mojo::ScopedMessagePipeHandle client_handle) { | 84 mojo::ScopedMessagePipeHandle client_handle) { |
| 75 std::map<std::string, | 85 auto it = service_factories_.find(name); |
| 76 base::Callback<void(mojo::ScopedMessagePipeHandle)> >::iterator it = | |
| 77 service_factories_.find(name); | |
| 78 if (it == service_factories_.end()) | 86 if (it == service_factories_.end()) |
| 79 return; | 87 return; |
| 80 | 88 |
| 81 // It's possible and effectively unavoidable that under certain conditions | 89 // It's possible and effectively unavoidable that under certain conditions |
| 82 // an invalid handle may be received. Don't invoke the factory in that case. | 90 // an invalid handle may be received. Don't invoke the factory in that case. |
| 83 if (!client_handle.is_valid()) { | 91 if (!client_handle.is_valid()) { |
| 84 DVLOG(2) << "Invalid pipe handle for " << name << " interface request."; | 92 DVLOG(2) << "Invalid pipe handle for " << name << " interface request."; |
| 85 return; | 93 return; |
| 86 } | 94 } |
| 87 | 95 |
| 88 it->second.Run(std::move(client_handle)); | 96 it->second.Run(std::move(client_handle)); |
| 89 } | 97 } |
| 90 | 98 |
| 91 void ServiceRegistryImpl::OnConnectionError() { | 99 void ServiceRegistryImpl::OnConnectionError() { |
| 92 binding_.Close(); | 100 binding_.Close(); |
| 93 } | 101 } |
| 94 | 102 |
| 95 } // namespace content | 103 } // namespace content |
| OLD | NEW |