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 |
11 namespace content { | 11 namespace content { |
12 | 12 |
13 ServiceRegistryImpl::ServiceRegistryImpl() | 13 ServiceRegistryImpl::ServiceRegistryImpl() |
14 : binding_(this), weak_factory_(this) {} | 14 : binding_(this), weak_factory_(this) {} |
15 | 15 |
16 ServiceRegistryImpl::~ServiceRegistryImpl() { | 16 ServiceRegistryImpl::~ServiceRegistryImpl() { |
17 while (!pending_connects_.empty()) { | 17 while (!pending_connects_.empty()) { |
18 mojo::CloseRaw(pending_connects_.front().second); | 18 mojo::CloseRaw(pending_connects_.front().second); |
19 pending_connects_.pop(); | 19 pending_connects_.pop(); |
20 } | 20 } |
21 } | 21 } |
22 | 22 |
23 void ServiceRegistryImpl::Bind( | 23 void ServiceRegistryImpl::Bind( |
24 mojo::InterfaceRequest<mojo::ServiceProvider> request) { | 24 mojo::InterfaceRequest<mojo::InterfaceProvider> request) { |
25 binding_.Bind(std::move(request)); | 25 binding_.Bind(std::move(request)); |
26 binding_.set_connection_error_handler(base::Bind( | 26 binding_.set_connection_error_handler(base::Bind( |
27 &ServiceRegistryImpl::OnConnectionError, base::Unretained(this))); | 27 &ServiceRegistryImpl::OnConnectionError, base::Unretained(this))); |
28 } | 28 } |
29 | 29 |
30 void ServiceRegistryImpl::BindRemoteServiceProvider( | 30 void ServiceRegistryImpl::BindRemoteServiceProvider( |
31 mojo::ServiceProviderPtr service_provider) { | 31 mojo::InterfaceProviderPtr service_provider) { |
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_->GetInterface( |
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::AddServiceOverrideForTesting( | 42 void ServiceRegistryImpl::AddServiceOverrideForTesting( |
43 const std::string& service_name, | 43 const std::string& service_name, |
44 const ServiceFactory& factory) { | 44 const ServiceFactory& factory) { |
45 service_overrides_[service_name] = factory; | 45 service_overrides_[service_name] = factory; |
(...skipping 15 matching lines...) Expand all Loading... |
61 if (override_it != service_overrides_.end()) { | 61 if (override_it != service_overrides_.end()) { |
62 override_it->second.Run(std::move(handle)); | 62 override_it->second.Run(std::move(handle)); |
63 return; | 63 return; |
64 } | 64 } |
65 | 65 |
66 if (!remote_provider_) { | 66 if (!remote_provider_) { |
67 pending_connects_.push( | 67 pending_connects_.push( |
68 std::make_pair(service_name.as_string(), handle.release())); | 68 std::make_pair(service_name.as_string(), handle.release())); |
69 return; | 69 return; |
70 } | 70 } |
71 remote_provider_->ConnectToService( | 71 remote_provider_->GetInterface( |
72 mojo::String::From(service_name.as_string()), std::move(handle)); | 72 mojo::String::From(service_name.as_string()), std::move(handle)); |
73 } | 73 } |
74 | 74 |
75 bool ServiceRegistryImpl::IsBound() const { | 75 bool ServiceRegistryImpl::IsBound() const { |
76 return binding_.is_bound(); | 76 return binding_.is_bound(); |
77 } | 77 } |
78 | 78 |
79 base::WeakPtr<ServiceRegistry> ServiceRegistryImpl::GetWeakPtr() { | 79 base::WeakPtr<ServiceRegistry> ServiceRegistryImpl::GetWeakPtr() { |
80 return weak_factory_.GetWeakPtr(); | 80 return weak_factory_.GetWeakPtr(); |
81 } | 81 } |
82 | 82 |
83 void ServiceRegistryImpl::ConnectToService( | 83 void ServiceRegistryImpl::GetInterface( |
84 const mojo::String& name, | 84 const mojo::String& name, |
85 mojo::ScopedMessagePipeHandle client_handle) { | 85 mojo::ScopedMessagePipeHandle client_handle) { |
86 auto it = service_factories_.find(name); | 86 auto it = service_factories_.find(name); |
87 if (it == service_factories_.end()) | 87 if (it == service_factories_.end()) |
88 return; | 88 return; |
89 | 89 |
90 // It's possible and effectively unavoidable that under certain conditions | 90 // It's possible and effectively unavoidable that under certain conditions |
91 // an invalid handle may be received. Don't invoke the factory in that case. | 91 // an invalid handle may be received. Don't invoke the factory in that case. |
92 if (!client_handle.is_valid()) { | 92 if (!client_handle.is_valid()) { |
93 DVLOG(2) << "Invalid pipe handle for " << name << " interface request."; | 93 DVLOG(2) << "Invalid pipe handle for " << name << " interface request."; |
94 return; | 94 return; |
95 } | 95 } |
96 | 96 |
97 it->second.Run(std::move(client_handle)); | 97 it->second.Run(std::move(client_handle)); |
98 } | 98 } |
99 | 99 |
100 void ServiceRegistryImpl::OnConnectionError() { | 100 void ServiceRegistryImpl::OnConnectionError() { |
101 binding_.Close(); | 101 binding_.Close(); |
102 } | 102 } |
103 | 103 |
104 } // namespace content | 104 } // namespace content |
OLD | NEW |