OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 <stdio.h> | 5 #include <stdio.h> |
6 | 6 |
7 #include "mojo/service_manager/service_manager.h" | 7 #include "mojo/service_manager/service_manager.h" |
8 | 8 |
9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
63 // static | 63 // static |
64 bool ServiceManager::TestAPI::HasCreatedInstance() { | 64 bool ServiceManager::TestAPI::HasCreatedInstance() { |
65 return has_created_instance; | 65 return has_created_instance; |
66 } | 66 } |
67 | 67 |
68 bool ServiceManager::TestAPI::HasFactoryForURL(const GURL& url) const { | 68 bool ServiceManager::TestAPI::HasFactoryForURL(const GURL& url) const { |
69 return manager_->url_to_service_factory_.find(url) != | 69 return manager_->url_to_service_factory_.find(url) != |
70 manager_->url_to_service_factory_.end(); | 70 manager_->url_to_service_factory_.end(); |
71 } | 71 } |
72 | 72 |
73 ServiceManager::ServiceManager() : default_loader_(NULL) { | 73 ServiceManager::ServiceManager() |
74 : default_loader_(NULL), | |
75 interceptor_(NULL) { | |
74 } | 76 } |
75 | 77 |
76 ServiceManager::~ServiceManager() { | 78 ServiceManager::~ServiceManager() { |
77 for (URLToServiceFactoryMap::iterator it = url_to_service_factory_.begin(); | 79 for (URLToServiceFactoryMap::iterator it = url_to_service_factory_.begin(); |
78 it != url_to_service_factory_.end(); ++it) { | 80 it != url_to_service_factory_.end(); ++it) { |
79 delete it->second; | 81 delete it->second; |
80 } | 82 } |
81 url_to_service_factory_.clear(); | 83 url_to_service_factory_.clear(); |
82 } | 84 } |
83 | 85 |
84 // static | 86 // static |
85 ServiceManager* ServiceManager::GetInstance() { | 87 ServiceManager* ServiceManager::GetInstance() { |
86 static base::LazyInstance<ServiceManager> instance = | 88 static base::LazyInstance<ServiceManager> instance = |
87 LAZY_INSTANCE_INITIALIZER; | 89 LAZY_INSTANCE_INITIALIZER; |
88 has_created_instance = true; | 90 has_created_instance = true; |
89 return &instance.Get(); | 91 return &instance.Get(); |
90 } | 92 } |
91 | 93 |
92 void ServiceManager::Connect(const GURL& url, | 94 void ServiceManager::Connect(const GURL& url, |
93 ScopedMessagePipeHandle client_handle) { | 95 ScopedMessagePipeHandle client_handle) { |
94 URLToServiceFactoryMap::const_iterator service_it = | 96 URLToServiceFactoryMap::const_iterator service_it = |
95 url_to_service_factory_.find(url); | 97 url_to_service_factory_.find(url); |
96 ServiceFactory* service_factory; | 98 ServiceFactory* service_factory; |
97 if (service_it != url_to_service_factory_.end()) { | 99 if (service_it != url_to_service_factory_.end()) { |
98 service_factory = service_it->second; | 100 service_factory = service_it->second; |
99 } else { | 101 } else { |
100 service_factory = new ServiceFactory(this, url); | 102 service_factory = new ServiceFactory(this, url); |
101 url_to_service_factory_[url] = service_factory; | 103 url_to_service_factory_[url] = service_factory; |
102 } | 104 } |
103 service_factory->ConnectToClient(client_handle.Pass()); | 105 if (interceptor_) |
DaveMoore
2014/04/04 21:06:46
Please include braces even if it's one statement o
cpu_(ooo_6.6-7.5)
2014/04/04 23:42:45
Done.
| |
106 service_factory->ConnectToClient( | |
107 interceptor_->OnConnectToClient(url, client_handle.Pass())); | |
108 else | |
109 service_factory->ConnectToClient(client_handle.Pass()); | |
104 } | 110 } |
105 | 111 |
106 void ServiceManager::SetLoaderForURL(ServiceLoader* loader, const GURL& url) { | 112 void ServiceManager::SetLoaderForURL(ServiceLoader* loader, const GURL& url) { |
107 DCHECK(url_to_loader_.find(url) == url_to_loader_.end()); | 113 DCHECK(url_to_loader_.find(url) == url_to_loader_.end()); |
108 url_to_loader_[url] = loader; | 114 url_to_loader_[url] = loader; |
109 } | 115 } |
110 | 116 |
111 void ServiceManager::SetLoaderForScheme(ServiceLoader* loader, | 117 void ServiceManager::SetLoaderForScheme(ServiceLoader* loader, |
112 const std::string& scheme) { | 118 const std::string& scheme) { |
113 DCHECK(scheme_to_loader_.find(scheme) == scheme_to_loader_.end()); | 119 DCHECK(scheme_to_loader_.find(scheme) == scheme_to_loader_.end()); |
114 scheme_to_loader_[scheme] = loader; | 120 scheme_to_loader_[scheme] = loader; |
115 } | 121 } |
116 | 122 |
123 void ServiceManager::SetInterceptor(ServiceInterceptor* interceptor) { | |
124 interceptor_ = interceptor; | |
125 } | |
126 | |
117 ServiceLoader* ServiceManager::GetLoaderForURL(const GURL& url) { | 127 ServiceLoader* ServiceManager::GetLoaderForURL(const GURL& url) { |
118 URLToLoaderMap::const_iterator url_it = url_to_loader_.find(url); | 128 URLToLoaderMap::const_iterator url_it = url_to_loader_.find(url); |
119 if (url_it != url_to_loader_.end()) | 129 if (url_it != url_to_loader_.end()) |
120 return url_it->second; | 130 return url_it->second; |
121 SchemeToLoaderMap::const_iterator scheme_it = | 131 SchemeToLoaderMap::const_iterator scheme_it = |
122 scheme_to_loader_.find(url.scheme()); | 132 scheme_to_loader_.find(url.scheme()); |
123 if (scheme_it != scheme_to_loader_.end()) | 133 if (scheme_it != scheme_to_loader_.end()) |
124 return scheme_it->second; | 134 return scheme_it->second; |
125 DCHECK(default_loader_); | 135 DCHECK(default_loader_); |
126 return default_loader_; | 136 return default_loader_; |
127 } | 137 } |
128 | 138 |
129 void ServiceManager::OnServiceFactoryError(ServiceFactory* service_factory) { | 139 void ServiceManager::OnServiceFactoryError(ServiceFactory* service_factory) { |
130 const GURL url = service_factory->url(); | 140 const GURL url = service_factory->url(); |
131 URLToServiceFactoryMap::iterator it = url_to_service_factory_.find(url); | 141 URLToServiceFactoryMap::iterator it = url_to_service_factory_.find(url); |
132 DCHECK(it != url_to_service_factory_.end()); | 142 DCHECK(it != url_to_service_factory_.end()); |
133 delete it->second; | 143 delete it->second; |
134 url_to_service_factory_.erase(it); | 144 url_to_service_factory_.erase(it); |
135 GetLoaderForURL(url)->OnServiceError(this, url); | 145 GetLoaderForURL(url)->OnServiceError(this, url); |
136 } | 146 } |
137 | 147 |
138 } // namespace mojo | 148 } // namespace mojo |
OLD | NEW |