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/public/cpp/bindings/allocation_scope.h"
14 #include "mojo/service_manager/service_loader.h"
20 bool has_created_instance = false;
23 class ServiceManager::ServiceFactory : public InterfaceImpl<Shell> {
25 ServiceFactory(ServiceManager* manager, const GURL& url)
31 virtual ~ServiceFactory() {
34 void ConnectToClient(ScopedMessagePipeHandle handle) {
35 if (!handle.is_valid()) {
39 AllocationScope scope;
40 client_->AcceptConnection(url_.spec(), handle.Pass());
43 // Shell implementation:
44 virtual void SetClient(ShellClient* client) OVERRIDE {
47 virtual void Connect(const String& url,
48 ScopedMessagePipeHandle client_pipe) OVERRIDE {
49 manager_->Connect(GURL(url.To<std::string>()), client_pipe.Pass());
52 const GURL& url() const { return url_; }
55 virtual void OnConnectionError() OVERRIDE {
56 manager_->OnServiceFactoryError(this);
59 ServiceManager* const manager_;
63 DISALLOW_COPY_AND_ASSIGN(ServiceFactory);
66 class ServiceManager::TestAPI::TestShellConnection
67 : public InterfaceImpl<Shell> {
69 explicit TestShellConnection(ServiceManager* manager)
73 virtual ~TestShellConnection() {}
75 virtual void OnConnectionError() OVERRIDE {
76 // TODO(darin): How should we handle this error?
80 virtual void SetClient(ShellClient* client) OVERRIDE {
83 virtual void Connect(const String& url,
84 ScopedMessagePipeHandle client_pipe) OVERRIDE {
85 manager_->Connect(GURL(url.To<std::string>()), client_pipe.Pass());
89 ServiceManager* manager_;
92 DISALLOW_COPY_AND_ASSIGN(TestShellConnection);
96 ServiceManager::TestAPI::TestAPI(ServiceManager* manager) : manager_(manager) {
99 ServiceManager::TestAPI::~TestAPI() {
102 bool ServiceManager::TestAPI::HasCreatedInstance() {
103 return has_created_instance;
106 ScopedMessagePipeHandle ServiceManager::TestAPI::GetShellHandle() {
109 BindToPipe(new TestShellConnection(manager_), pipe.handle0.Pass()));
110 return pipe.handle1.Pass();
113 bool ServiceManager::TestAPI::HasFactoryForURL(const GURL& url) const {
114 return manager_->url_to_service_factory_.find(url) !=
115 manager_->url_to_service_factory_.end();
118 ServiceManager::ServiceManager()
119 : interceptor_(NULL) {
122 ServiceManager::~ServiceManager() {
123 STLDeleteValues(&url_to_service_factory_);
124 STLDeleteValues(&url_to_loader_);
125 STLDeleteValues(&scheme_to_loader_);
129 ServiceManager* ServiceManager::GetInstance() {
130 static base::LazyInstance<ServiceManager> instance =
131 LAZY_INSTANCE_INITIALIZER;
132 has_created_instance = true;
133 return &instance.Get();
136 void ServiceManager::Connect(const GURL& url,
137 ScopedMessagePipeHandle client_handle) {
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(
154 interceptor_->OnConnectToClient(url, client_handle.Pass()));
156 service_factory->ConnectToClient(client_handle.Pass());
160 void ServiceManager::SetLoaderForURL(scoped_ptr<ServiceLoader> loader,
162 URLToLoaderMap::iterator it = url_to_loader_.find(url);
163 if (it != url_to_loader_.end())
165 url_to_loader_[url] = loader.release();
168 void ServiceManager::SetLoaderForScheme(scoped_ptr<ServiceLoader> loader,
169 const std::string& scheme) {
170 SchemeToLoaderMap::iterator it = scheme_to_loader_.find(scheme);
171 if (it != scheme_to_loader_.end())
173 scheme_to_loader_[scheme] = loader.release();
176 void ServiceManager::SetInterceptor(Interceptor* interceptor) {
177 interceptor_ = interceptor;
180 ServiceLoader* ServiceManager::GetLoaderForURL(const GURL& url) {
181 URLToLoaderMap::const_iterator url_it = url_to_loader_.find(url);
182 if (url_it != url_to_loader_.end())
183 return url_it->second;
184 SchemeToLoaderMap::const_iterator scheme_it =
185 scheme_to_loader_.find(url.scheme());
186 if (scheme_it != scheme_to_loader_.end())
187 return scheme_it->second;
188 DCHECK(default_loader_);
189 return default_loader_.get();
192 void ServiceManager::OnServiceFactoryError(ServiceFactory* service_factory) {
193 // Called from ~ServiceFactory, so we do not need to call Destroy here.
194 const GURL url = service_factory->url();
195 URLToServiceFactoryMap::iterator it = url_to_service_factory_.find(url);
196 DCHECK(it != url_to_service_factory_.end());
198 url_to_service_factory_.erase(it);
199 GetLoaderForURL(url)->OnServiceError(this, url);