1 // Copyright 2013 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.
7 #include "mojo/service_manager/service_manager.h"
9 #include "base/lazy_instance.h"
10 #include "base/logging.h"
11 #include "base/macros.h"
12 #include "base/stl_util.h"
13 #include "mojo/service_manager/service_loader.h"
19 bool has_created_instance = false;
22 class ServiceManager::ServiceFactory : public InterfaceImpl<ServiceProvider> {
24 ServiceFactory(ServiceManager* manager, const GURL& url)
29 virtual ~ServiceFactory() {
32 void ConnectToClient(const std::string& service_name,
33 ScopedMessagePipeHandle handle,
34 const GURL& requestor_url) {
35 if (handle.is_valid()) {
36 client()->ConnectToService(
37 url_.spec(), service_name, handle.Pass(), requestor_url.spec());
41 // ServiceProvider implementation:
42 virtual void ConnectToService(const String& service_url,
43 const String& service_name,
44 ScopedMessagePipeHandle client_pipe,
45 const String& requestor_url) OVERRIDE {
46 // Ignore provided requestor_url and use url from connection.
47 manager_->ConnectToService(
48 GURL(service_url), service_name, client_pipe.Pass(), url_);
51 const GURL& url() const { return url_; }
54 virtual void OnConnectionError() OVERRIDE {
55 manager_->OnServiceFactoryError(this);
58 ServiceManager* const manager_;
61 DISALLOW_COPY_AND_ASSIGN(ServiceFactory);
64 class ServiceManager::TestAPI::TestServiceProviderConnection
65 : public InterfaceImpl<ServiceProvider> {
67 explicit TestServiceProviderConnection(ServiceManager* manager)
68 : manager_(manager) {}
69 virtual ~TestServiceProviderConnection() {}
71 virtual void OnConnectionError() OVERRIDE {
72 // TODO(darin): How should we handle this error?
76 virtual void ConnectToService(const String& service_url,
77 const String& service_name,
78 ScopedMessagePipeHandle client_pipe,
79 const String& requestor_url) OVERRIDE {
80 manager_->ConnectToService(GURL(service_url),
87 ServiceManager* manager_;
89 DISALLOW_COPY_AND_ASSIGN(TestServiceProviderConnection);
93 ServiceManager::TestAPI::TestAPI(ServiceManager* manager) : manager_(manager) {
96 ServiceManager::TestAPI::~TestAPI() {
99 bool ServiceManager::TestAPI::HasCreatedInstance() {
100 return has_created_instance;
103 ScopedMessagePipeHandle ServiceManager::TestAPI::GetServiceProviderHandle() {
105 service_provider_.reset(
106 BindToPipe(new TestServiceProviderConnection(manager_),
107 pipe.handle0.Pass()));
108 return pipe.handle1.Pass();
111 bool ServiceManager::TestAPI::HasFactoryForURL(const GURL& url) const {
112 return manager_->url_to_service_factory_.find(url) !=
113 manager_->url_to_service_factory_.end();
116 ServiceManager::ServiceManager()
117 : interceptor_(NULL) {
120 ServiceManager::~ServiceManager() {
121 STLDeleteValues(&url_to_service_factory_);
122 STLDeleteValues(&url_to_loader_);
123 STLDeleteValues(&scheme_to_loader_);
127 ServiceManager* ServiceManager::GetInstance() {
128 static base::LazyInstance<ServiceManager> instance =
129 LAZY_INSTANCE_INITIALIZER;
130 has_created_instance = true;
131 return &instance.Get();
134 void ServiceManager::ConnectToService(const GURL& url,
135 const std::string& name,
136 ScopedMessagePipeHandle client_handle,
137 const GURL& requestor_url) {
138 URLToServiceFactoryMap::const_iterator service_it =
139 url_to_service_factory_.find(url);
140 ServiceFactory* service_factory;
141 if (service_it != url_to_service_factory_.end()) {
142 service_factory = service_it->second;
145 GetLoaderForURL(url)->LoadService(this, url, pipe.handle0.Pass());
148 BindToPipe(new ServiceFactory(this, url), pipe.handle1.Pass());
150 url_to_service_factory_[url] = service_factory;
153 service_factory->ConnectToClient(
155 interceptor_->OnConnectToClient(url, client_handle.Pass()),
158 service_factory->ConnectToClient(name, client_handle.Pass(), requestor_url);
162 void ServiceManager::SetLoaderForURL(scoped_ptr<ServiceLoader> loader,
164 URLToLoaderMap::iterator it = url_to_loader_.find(url);
165 if (it != url_to_loader_.end())
167 url_to_loader_[url] = loader.release();
170 void ServiceManager::SetLoaderForScheme(scoped_ptr<ServiceLoader> loader,
171 const std::string& scheme) {
172 SchemeToLoaderMap::iterator it = scheme_to_loader_.find(scheme);
173 if (it != scheme_to_loader_.end())
175 scheme_to_loader_[scheme] = loader.release();
178 void ServiceManager::SetInterceptor(Interceptor* interceptor) {
179 interceptor_ = interceptor;
182 ServiceLoader* ServiceManager::GetLoaderForURL(const GURL& url) {
183 URLToLoaderMap::const_iterator url_it = url_to_loader_.find(url);
184 if (url_it != url_to_loader_.end())
185 return url_it->second;
186 SchemeToLoaderMap::const_iterator scheme_it =
187 scheme_to_loader_.find(url.scheme());
188 if (scheme_it != scheme_to_loader_.end())
189 return scheme_it->second;
190 DCHECK(default_loader_);
191 return default_loader_.get();
194 void ServiceManager::OnServiceFactoryError(ServiceFactory* service_factory) {
195 // Called from ~ServiceFactory, so we do not need to call Destroy here.
196 const GURL url = service_factory->url();
197 URLToServiceFactoryMap::iterator it = url_to_service_factory_.find(url);
198 DCHECK(it != url_to_service_factory_.end());
200 url_to_service_factory_.erase(it);
201 ServiceLoader* loader = GetLoaderForURL(url);
203 loader->OnServiceError(this, url);