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 "mojo/public/cpp/application/lib/service_registry.h" | |
6 | |
7 #include "mojo/public/cpp/application/application_connection.h" | |
8 #include "mojo/public/cpp/application/application_impl.h" | |
9 #include "mojo/public/cpp/application/lib/service_connector.h" | |
10 | |
11 namespace mojo { | |
12 namespace internal { | |
13 | |
14 ServiceRegistry::ServiceRegistry( | |
15 ApplicationImpl* application_impl, | |
16 const std::string& url, | |
17 ServiceProviderPtr remote_services, | |
18 InterfaceRequest<ServiceProvider> local_services) | |
19 : application_impl_(application_impl), | |
20 url_(url), | |
21 local_binding_(this, local_services.Pass()), | |
22 remote_service_provider_(remote_services.Pass()) { | |
23 } | |
24 | |
25 ServiceRegistry::ServiceRegistry() | |
26 : application_impl_(nullptr), local_binding_(this) { | |
27 } | |
28 | |
29 ServiceRegistry::~ServiceRegistry() { | |
30 for (NameToServiceConnectorMap::iterator i = | |
31 name_to_service_connector_.begin(); | |
32 i != name_to_service_connector_.end(); | |
33 ++i) { | |
34 delete i->second; | |
35 } | |
36 name_to_service_connector_.clear(); | |
37 } | |
38 | |
39 void ServiceRegistry::AddServiceConnector( | |
40 ServiceConnectorBase* service_connector) { | |
41 RemoveServiceConnectorInternal(service_connector); | |
42 name_to_service_connector_[service_connector->name()] = service_connector; | |
43 service_connector->set_application_connection(this); | |
44 } | |
45 | |
46 void ServiceRegistry::RemoveServiceConnector( | |
47 ServiceConnectorBase* service_connector) { | |
48 RemoveServiceConnectorInternal(service_connector); | |
49 if (name_to_service_connector_.empty()) | |
50 remote_service_provider_.reset(); | |
51 } | |
52 | |
53 bool ServiceRegistry::RemoveServiceConnectorInternal( | |
54 ServiceConnectorBase* service_connector) { | |
55 NameToServiceConnectorMap::iterator it = | |
56 name_to_service_connector_.find(service_connector->name()); | |
57 if (it == name_to_service_connector_.end()) | |
58 return false; | |
59 delete it->second; | |
60 name_to_service_connector_.erase(it); | |
61 return true; | |
62 } | |
63 | |
64 const std::string& ServiceRegistry::GetRemoteApplicationURL() { | |
65 return url_; | |
66 } | |
67 | |
68 ServiceProvider* ServiceRegistry::GetServiceProvider() { | |
69 return remote_service_provider_.get(); | |
70 } | |
71 | |
72 ApplicationConnection* ServiceRegistry::ConnectToApplication( | |
73 const std::string& url) { | |
74 return application_impl_->ConnectToApplication(url); | |
75 } | |
76 | |
77 void ServiceRegistry::ConnectToService(const mojo::String& service_name, | |
78 ScopedMessagePipeHandle client_handle) { | |
79 if (name_to_service_connector_.find(service_name) == | |
80 name_to_service_connector_.end()) { | |
81 client_handle.reset(); | |
82 return; | |
83 } | |
84 internal::ServiceConnectorBase* service_connector = | |
85 name_to_service_connector_[service_name]; | |
86 return service_connector->ConnectToService(service_name, | |
87 client_handle.Pass()); | |
88 } | |
89 | |
90 } // namespace internal | |
91 } // namespace mojo | |
OLD | NEW |