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 "mojo/public/cpp/application/service_provider_impl.h" | 5 #include "mojo/public/cpp/application/service_provider_impl.h" |
6 | 6 |
7 #include "mojo/public/cpp/application/lib/service_connector.h" | 7 #include "mojo/public/cpp/application/lib/service_connector.h" |
8 #include "mojo/public/cpp/application/lib/weak_service_provider.h" | |
9 #include "mojo/public/cpp/environment/logging.h" | 8 #include "mojo/public/cpp/environment/logging.h" |
10 | 9 |
11 namespace mojo { | 10 namespace mojo { |
12 | 11 |
13 ServiceProviderImpl::ServiceProviderImpl() : remote_(nullptr) { | 12 ServiceProviderImpl::ServiceProviderImpl() : binding_(this) { |
| 13 } |
| 14 |
| 15 ServiceProviderImpl::ServiceProviderImpl( |
| 16 InterfaceRequest<ServiceProvider> request) |
| 17 : binding_(this, request.Pass()) { |
14 } | 18 } |
15 | 19 |
16 ServiceProviderImpl::~ServiceProviderImpl() { | 20 ServiceProviderImpl::~ServiceProviderImpl() { |
17 } | 21 } |
18 | 22 |
19 ServiceProvider* ServiceProviderImpl::CreateRemoteServiceProvider() { | 23 void ServiceProviderImpl::Bind(InterfaceRequest<ServiceProvider> request) { |
20 // TODO(beng): it sure would be nice if this method could return a scoped_ptr. | 24 binding_.Bind(request.Pass()); |
21 MOJO_DCHECK(!remote_); | |
22 remote_ = new internal::WeakServiceProvider(this, client()); | |
23 return remote_; | |
24 } | 25 } |
25 | 26 |
26 void ServiceProviderImpl::ConnectToService( | 27 void ServiceProviderImpl::ConnectToService( |
27 const String& service_name, | 28 const String& service_name, |
28 ScopedMessagePipeHandle client_handle) { | 29 ScopedMessagePipeHandle client_handle) { |
29 if (service_connectors_.find(service_name) == service_connectors_.end()) { | 30 if (service_connectors_.find(service_name) == service_connectors_.end()) { |
30 client_handle.reset(); | 31 client_handle.reset(); |
31 return; | 32 return; |
32 } | 33 } |
33 | 34 |
34 internal::ServiceConnectorBase* service_connector = | 35 internal::ServiceConnectorBase* service_connector = |
35 service_connectors_[service_name]; | 36 service_connectors_[service_name]; |
36 return service_connector->ConnectToService(service_name, | 37 return service_connector->ConnectToService(service_name, |
37 client_handle.Pass()); | 38 client_handle.Pass()); |
38 } | 39 } |
39 | 40 |
40 void ServiceProviderImpl::OnConnectionError() { | |
41 ClearRemote(); | |
42 } | |
43 | |
44 void ServiceProviderImpl::AddServiceConnector( | 41 void ServiceProviderImpl::AddServiceConnector( |
45 internal::ServiceConnectorBase* service_connector) { | 42 internal::ServiceConnectorBase* service_connector) { |
46 RemoveServiceConnector(service_connector); | 43 RemoveServiceConnector(service_connector); |
47 service_connectors_[service_connector->name()] = service_connector; | 44 service_connectors_[service_connector->name()] = service_connector; |
48 // TODO(beng): perhaps take app connection thru ctor?? | 45 // TODO(beng): perhaps take app connection thru ctor?? |
49 service_connector->set_application_connection(nullptr); | 46 service_connector->set_application_connection(nullptr); |
50 } | 47 } |
51 | 48 |
52 void ServiceProviderImpl::RemoveServiceConnector( | 49 void ServiceProviderImpl::RemoveServiceConnector( |
53 internal::ServiceConnectorBase* service_connector) { | 50 internal::ServiceConnectorBase* service_connector) { |
54 NameToServiceConnectorMap::iterator it = | 51 NameToServiceConnectorMap::iterator it = |
55 service_connectors_.find(service_connector->name()); | 52 service_connectors_.find(service_connector->name()); |
56 if (it == service_connectors_.end()) | 53 if (it == service_connectors_.end()) |
57 return; | 54 return; |
58 delete it->second; | 55 delete it->second; |
59 service_connectors_.erase(it); | 56 service_connectors_.erase(it); |
60 } | 57 } |
61 | 58 |
62 void ServiceProviderImpl::ClearRemote() { | |
63 if (remote_) { | |
64 remote_->Clear(); | |
65 remote_ = nullptr; | |
66 } | |
67 } | |
68 | |
69 } // namespace mojo | 59 } // namespace mojo |
OLD | NEW |