| 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 <utility> | |
| 8 | |
| 9 #include "mojo/common/common_type_converters.h" | |
| 10 | |
| 11 namespace content { | |
| 12 | |
| 13 ServiceRegistry* ServiceRegistry::Create() { | |
| 14 return new ServiceRegistryImpl; | |
| 15 } | |
| 16 | |
| 17 ServiceRegistryImpl::ServiceRegistryImpl() | |
| 18 : binding_(this), | |
| 19 weak_factory_(this) { | |
| 20 remote_provider_request_ = GetProxy(&remote_provider_); | |
| 21 } | |
| 22 | |
| 23 ServiceRegistryImpl::~ServiceRegistryImpl() { | |
| 24 } | |
| 25 | |
| 26 void ServiceRegistryImpl::Bind(shell::mojom::InterfaceProviderRequest request) { | |
| 27 binding_.Bind(std::move(request)); | |
| 28 binding_.set_connection_error_handler(base::Bind( | |
| 29 &ServiceRegistryImpl::OnConnectionError, base::Unretained(this))); | |
| 30 } | |
| 31 | |
| 32 shell::mojom::InterfaceProviderRequest | |
| 33 ServiceRegistryImpl::TakeRemoteRequest() { | |
| 34 return std::move(remote_provider_request_); | |
| 35 } | |
| 36 | |
| 37 void ServiceRegistryImpl::AddService( | |
| 38 const std::string& service_name, | |
| 39 const ServiceFactory& service_factory, | |
| 40 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) { | |
| 41 service_factories_[service_name] = | |
| 42 std::make_pair(service_factory, task_runner); | |
| 43 } | |
| 44 | |
| 45 void ServiceRegistryImpl::RemoveService(const std::string& service_name) { | |
| 46 service_factories_.erase(service_name); | |
| 47 } | |
| 48 | |
| 49 void ServiceRegistryImpl::ConnectToRemoteService( | |
| 50 base::StringPiece service_name, | |
| 51 mojo::ScopedMessagePipeHandle handle) { | |
| 52 auto override_it = service_overrides_.find(service_name.as_string()); | |
| 53 if (override_it != service_overrides_.end()) { | |
| 54 override_it->second.Run(std::move(handle)); | |
| 55 return; | |
| 56 } | |
| 57 remote_provider_->GetInterface( | |
| 58 mojo::String::From(service_name.as_string()), std::move(handle)); | |
| 59 } | |
| 60 | |
| 61 void ServiceRegistryImpl::AddServiceOverrideForTesting( | |
| 62 const std::string& service_name, | |
| 63 const ServiceFactory& factory) { | |
| 64 service_overrides_[service_name] = factory; | |
| 65 } | |
| 66 | |
| 67 void ServiceRegistryImpl::ClearServiceOverridesForTesting() { | |
| 68 service_overrides_.clear(); | |
| 69 } | |
| 70 | |
| 71 bool ServiceRegistryImpl::IsBound() const { | |
| 72 return binding_.is_bound(); | |
| 73 } | |
| 74 | |
| 75 base::WeakPtr<ServiceRegistry> ServiceRegistryImpl::GetWeakPtr() { | |
| 76 return weak_factory_.GetWeakPtr(); | |
| 77 } | |
| 78 | |
| 79 void ServiceRegistryImpl::GetInterface( | |
| 80 const mojo::String& name, | |
| 81 mojo::ScopedMessagePipeHandle client_handle) { | |
| 82 auto it = service_factories_.find(name); | |
| 83 if (it == service_factories_.end()) { | |
| 84 DLOG(ERROR) << name << " not found"; | |
| 85 return; | |
| 86 } | |
| 87 | |
| 88 // It's possible and effectively unavoidable that under certain conditions | |
| 89 // an invalid handle may be received. Don't invoke the factory in that case. | |
| 90 if (!client_handle.is_valid()) { | |
| 91 DVLOG(2) << "Invalid pipe handle for " << name << " interface request."; | |
| 92 return; | |
| 93 } | |
| 94 | |
| 95 if (it->second.second) { | |
| 96 it->second.second->PostTask( | |
| 97 FROM_HERE, | |
| 98 base::Bind(&ServiceRegistryImpl::RunServiceFactoryOnTaskRunner, | |
| 99 it->second.first, base::Passed(&client_handle))); | |
| 100 return; | |
| 101 } | |
| 102 it->second.first.Run(std::move(client_handle)); | |
| 103 } | |
| 104 | |
| 105 // static | |
| 106 void ServiceRegistryImpl::RunServiceFactoryOnTaskRunner( | |
| 107 const ServiceRegistryImpl::ServiceFactory& factory, | |
| 108 mojo::ScopedMessagePipeHandle handle) { | |
| 109 factory.Run(std::move(handle)); | |
| 110 } | |
| 111 | |
| 112 void ServiceRegistryImpl::OnConnectionError() { | |
| 113 binding_.Close(); | |
| 114 } | |
| 115 | |
| 116 } // namespace content | |
| OLD | NEW |