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