OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "shell/application_manager/application_manager.h" | 5 #include "shell/application_manager/application_manager.h" |
6 | 6 |
7 #include <stdio.h> | 7 #include <stdio.h> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 void ApplicationManager::TerminateShellConnections() { | 97 void ApplicationManager::TerminateShellConnections() { |
98 STLDeleteValues(&url_to_shell_impl_); | 98 STLDeleteValues(&url_to_shell_impl_); |
99 } | 99 } |
100 | 100 |
101 void ApplicationManager::ConnectToApplication( | 101 void ApplicationManager::ConnectToApplication( |
102 const GURL& requested_url, | 102 const GURL& requested_url, |
103 const GURL& requestor_url, | 103 const GURL& requestor_url, |
104 InterfaceRequest<ServiceProvider> services, | 104 InterfaceRequest<ServiceProvider> services, |
105 ServiceProviderPtr exposed_services) { | 105 ServiceProviderPtr exposed_services) { |
106 DCHECK(requested_url.is_valid()); | 106 DCHECK(requested_url.is_valid()); |
107 GURL mapped_url = delegate_->ResolveMappings(requested_url); | |
108 | 107 |
109 // We check both the mapped and resolved urls for existing shell_impls because | 108 // We check both the mapped and resolved urls for existing shell_impls because |
110 // external applications can be registered for the unresolved mojo:foo urls. | 109 // external applications can be registered for the unresolved mojo:foo urls. |
111 ShellImpl* shell_impl = GetShellImpl(mapped_url); | 110 |
112 if (shell_impl) { | 111 GURL mapped_url = delegate_->ResolveMappings(requested_url); |
113 ConnectToClient(shell_impl, mapped_url, requestor_url, services.Pass(), | 112 if (ConnectToRunningApplication(mapped_url, requestor_url, &services, |
114 exposed_services.Pass()); | 113 &exposed_services)) { |
115 return; | 114 return; |
116 } | 115 } |
117 | 116 |
118 GURL resolved_url = delegate_->ResolveURL(mapped_url); | 117 GURL resolved_url = delegate_->ResolveURL(mapped_url); |
119 shell_impl = GetShellImpl(resolved_url); | 118 if (ConnectToRunningApplication(resolved_url, requestor_url, &services, |
120 if (shell_impl) { | 119 &exposed_services)) { |
121 ConnectToClient(shell_impl, resolved_url, requestor_url, services.Pass(), | |
122 exposed_services.Pass()); | |
123 return; | 120 return; |
124 } | 121 } |
125 | 122 |
126 ApplicationLoader* loader = | 123 if (ConnectToApplicationWithLoader(requested_url, mapped_url, requestor_url, |
127 GetLoaderForURL(mapped_url, DONT_INCLUDE_DEFAULT_LOADER); | 124 &services, &exposed_services, |
128 if (loader) { | 125 GetLoaderForURL(mapped_url))) { |
129 ConnectToApplicationImpl(requested_url, mapped_url, requestor_url, | |
130 services.Pass(), exposed_services.Pass(), loader); | |
131 return; | 126 return; |
132 } | 127 } |
133 | 128 |
134 loader = GetLoaderForURL(resolved_url, INCLUDE_DEFAULT_LOADER); | 129 if (ConnectToApplicationWithLoader(requested_url, resolved_url, requestor_url, |
135 if (loader) { | 130 &services, &exposed_services, |
136 ConnectToApplicationImpl(requested_url, resolved_url, requestor_url, | 131 GetLoaderForURL(resolved_url))) { |
137 services.Pass(), exposed_services.Pass(), loader); | |
138 return; | 132 return; |
139 } | 133 } |
140 | 134 |
| 135 if (ConnectToApplicationWithLoader(requested_url, resolved_url, requestor_url, |
| 136 &services, &exposed_services, |
| 137 default_loader_.get())) { |
| 138 return; |
| 139 } |
| 140 |
| 141 // TODO(aa): This case should go away, see comments in |
| 142 // NativeApplicationLoader. |
| 143 if (native_application_loader_.get()) { |
| 144 native_application_loader_->Load( |
| 145 resolved_url, RegisterShell(requested_url, resolved_url, requestor_url, |
| 146 services.Pass(), exposed_services.Pass()), |
| 147 base::Bind(&ApplicationManager::LoadWithContentHandler, |
| 148 weak_ptr_factory_.GetWeakPtr())); |
| 149 return; |
| 150 } |
| 151 |
141 LOG(WARNING) << "Could not find loader to load application: " | 152 LOG(WARNING) << "Could not find loader to load application: " |
142 << requested_url.spec(); | 153 << requested_url.spec(); |
143 } | 154 } |
144 | 155 |
145 void ApplicationManager::ConnectToApplicationImpl( | 156 bool ApplicationManager::ConnectToRunningApplication( |
| 157 const GURL& application_url, |
| 158 const GURL& requestor_url, |
| 159 InterfaceRequest<ServiceProvider>* services, |
| 160 ServiceProviderPtr* exposed_services) { |
| 161 ShellImpl* shell_impl = GetShellImpl(application_url); |
| 162 if (!shell_impl) |
| 163 return false; |
| 164 |
| 165 ConnectToClient(shell_impl, application_url, requestor_url, services->Pass(), |
| 166 exposed_services->Pass()); |
| 167 return true; |
| 168 } |
| 169 |
| 170 bool ApplicationManager::ConnectToApplicationWithLoader( |
| 171 const GURL& requested_url, |
| 172 const GURL& resolved_url, |
| 173 const GURL& requestor_url, |
| 174 InterfaceRequest<ServiceProvider>* services, |
| 175 ServiceProviderPtr* exposed_services, |
| 176 ApplicationLoader* loader) { |
| 177 if (!loader) |
| 178 return false; |
| 179 |
| 180 loader->Load(resolved_url, |
| 181 RegisterShell(requested_url, resolved_url, requestor_url, |
| 182 services->Pass(), exposed_services->Pass())); |
| 183 return true; |
| 184 } |
| 185 |
| 186 InterfaceRequest<Application> ApplicationManager::RegisterShell( |
146 const GURL& requested_url, | 187 const GURL& requested_url, |
147 const GURL& resolved_url, | 188 const GURL& resolved_url, |
148 const GURL& requestor_url, | 189 const GURL& requestor_url, |
149 InterfaceRequest<ServiceProvider> services, | 190 InterfaceRequest<ServiceProvider> services, |
150 ServiceProviderPtr exposed_services, | 191 ServiceProviderPtr exposed_services) { |
151 ApplicationLoader* loader) { | |
152 ApplicationPtr application; | 192 ApplicationPtr application; |
153 InterfaceRequest<Application> application_request = GetProxy(&application); | 193 InterfaceRequest<Application> application_request = GetProxy(&application); |
154 ShellImpl* shell = | 194 ShellImpl* shell = |
155 new ShellImpl(application.Pass(), this, requested_url, resolved_url); | 195 new ShellImpl(application.Pass(), this, requested_url, resolved_url); |
156 url_to_shell_impl_[resolved_url] = shell; | 196 url_to_shell_impl_[resolved_url] = shell; |
157 shell->InitializeApplication(GetArgsForURL(requested_url)); | 197 shell->InitializeApplication(GetArgsForURL(requested_url)); |
158 | |
159 loader->Load(this, resolved_url, application_request.Pass(), | |
160 base::Bind(&ApplicationManager::LoadWithContentHandler, | |
161 weak_ptr_factory_.GetWeakPtr())); | |
162 ConnectToClient(shell, resolved_url, requestor_url, services.Pass(), | 198 ConnectToClient(shell, resolved_url, requestor_url, services.Pass(), |
163 exposed_services.Pass()); | 199 exposed_services.Pass()); |
| 200 return application_request.Pass(); |
164 } | 201 } |
165 | 202 |
166 ShellImpl* ApplicationManager::GetShellImpl(const GURL& url) { | 203 ShellImpl* ApplicationManager::GetShellImpl(const GURL& url) { |
167 const auto& shell_it = url_to_shell_impl_.find(url); | 204 const auto& shell_it = url_to_shell_impl_.find(url); |
168 if (shell_it != url_to_shell_impl_.end()) | 205 if (shell_it != url_to_shell_impl_.end()) |
169 return shell_it->second; | 206 return shell_it->second; |
170 return nullptr; | 207 return nullptr; |
171 } | 208 } |
172 | 209 |
173 void ApplicationManager::ConnectToClient( | 210 void ApplicationManager::ConnectToClient( |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
227 if (it != scheme_to_loader_.end()) | 264 if (it != scheme_to_loader_.end()) |
228 delete it->second; | 265 delete it->second; |
229 scheme_to_loader_[scheme] = loader.release(); | 266 scheme_to_loader_[scheme] = loader.release(); |
230 } | 267 } |
231 | 268 |
232 void ApplicationManager::SetArgsForURL(const std::vector<std::string>& args, | 269 void ApplicationManager::SetArgsForURL(const std::vector<std::string>& args, |
233 const GURL& url) { | 270 const GURL& url) { |
234 url_to_args_[url] = args; | 271 url_to_args_[url] = args; |
235 } | 272 } |
236 | 273 |
237 ApplicationLoader* ApplicationManager::GetLoaderForURL( | 274 ApplicationLoader* ApplicationManager::GetLoaderForURL(const GURL& url) { |
238 const GURL& url, | |
239 IncludeDefaultLoader include_default_loader) { | |
240 auto url_it = url_to_loader_.find(url); | 275 auto url_it = url_to_loader_.find(url); |
241 if (url_it != url_to_loader_.end()) | 276 if (url_it != url_to_loader_.end()) |
242 return url_it->second; | 277 return url_it->second; |
243 auto scheme_it = scheme_to_loader_.find(url.scheme()); | 278 auto scheme_it = scheme_to_loader_.find(url.scheme()); |
244 if (scheme_it != scheme_to_loader_.end()) | 279 if (scheme_it != scheme_to_loader_.end()) |
245 return scheme_it->second; | 280 return scheme_it->second; |
246 if (include_default_loader == INCLUDE_DEFAULT_LOADER) | |
247 return default_loader_.get(); | |
248 return NULL; | 281 return NULL; |
249 } | 282 } |
250 | 283 |
251 void ApplicationManager::OnShellImplError(ShellImpl* shell_impl) { | 284 void ApplicationManager::OnShellImplError(ShellImpl* shell_impl) { |
252 // Called from ~ShellImpl, so we do not need to call Destroy here. | 285 // Called from ~ShellImpl, so we do not need to call Destroy here. |
253 const GURL url = shell_impl->url(); | 286 const GURL url = shell_impl->url(); |
254 const GURL requested_url = shell_impl->requested_url(); | 287 const GURL requested_url = shell_impl->requested_url(); |
255 // Remove the shell. | 288 // Remove the shell. |
256 URLToShellImplMap::iterator it = url_to_shell_impl_.find(url); | 289 URLToShellImplMap::iterator it = url_to_shell_impl_.find(url); |
257 DCHECK(it != url_to_shell_impl_.end()); | 290 DCHECK(it != url_to_shell_impl_.end()); |
258 delete it->second; | 291 delete it->second; |
259 url_to_shell_impl_.erase(it); | 292 url_to_shell_impl_.erase(it); |
260 ApplicationLoader* loader = | |
261 GetLoaderForURL(requested_url, INCLUDE_DEFAULT_LOADER); | |
262 if (loader) | |
263 loader->OnApplicationError(this, url); | |
264 delegate_->OnApplicationError(requested_url); | 293 delegate_->OnApplicationError(requested_url); |
265 } | 294 } |
266 | 295 |
267 void ApplicationManager::OnContentHandlerError( | 296 void ApplicationManager::OnContentHandlerError( |
268 ContentHandlerConnection* content_handler) { | 297 ContentHandlerConnection* content_handler) { |
269 // Remove the mapping to the content handler. | 298 // Remove the mapping to the content handler. |
270 auto it = | 299 auto it = |
271 url_to_content_handler_.find(content_handler->content_handler_url()); | 300 url_to_content_handler_.find(content_handler->content_handler_url()); |
272 DCHECK(it != url_to_content_handler_.end()); | 301 DCHECK(it != url_to_content_handler_.end()); |
273 delete it->second; | 302 delete it->second; |
(...skipping 10 matching lines...) Expand all Loading... |
284 return pipe.handle0.Pass(); | 313 return pipe.handle0.Pass(); |
285 } | 314 } |
286 | 315 |
287 Array<String> ApplicationManager::GetArgsForURL(const GURL& url) { | 316 Array<String> ApplicationManager::GetArgsForURL(const GURL& url) { |
288 const auto& args_it = url_to_args_.find(url); | 317 const auto& args_it = url_to_args_.find(url); |
289 if (args_it != url_to_args_.end()) | 318 if (args_it != url_to_args_.end()) |
290 return Array<String>::From(args_it->second); | 319 return Array<String>::From(args_it->second); |
291 return Array<String>(); | 320 return Array<String>(); |
292 } | 321 } |
293 } // namespace mojo | 322 } // namespace mojo |
OLD | NEW |