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 "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
13 #include "base/trace_event/trace_event.h" | 13 #include "base/trace_event/trace_event.h" |
14 #include "mojo/public/cpp/bindings/binding.h" | 14 #include "mojo/public/cpp/bindings/binding.h" |
15 #include "mojo/public/cpp/bindings/error_handler.h" | 15 #include "mojo/public/cpp/bindings/error_handler.h" |
16 #include "mojo/services/content_handler/public/interfaces/content_handler.mojom.
h" | 16 #include "mojo/services/content_handler/public/interfaces/content_handler.mojom.
h" |
17 #include "shell/application_manager/fetcher.h" | 17 #include "shell/application_manager/fetcher.h" |
18 #include "shell/application_manager/local_fetcher.h" | 18 #include "shell/application_manager/local_fetcher.h" |
19 #include "shell/application_manager/network_fetcher.h" | 19 #include "shell/application_manager/network_fetcher.h" |
20 #include "shell/application_manager/query_util.h" | 20 #include "shell/application_manager/query_util.h" |
21 #include "shell/application_manager/shell_impl.h" | 21 #include "shell/application_manager/shell_impl.h" |
22 #include "shell/switches.h" | 22 #include "shell/switches.h" |
23 | 23 |
| 24 using mojo::Application; |
| 25 using mojo::ApplicationPtr; |
| 26 using mojo::InterfaceRequest; |
| 27 using mojo::ServiceProvider; |
| 28 using mojo::ServiceProviderPtr; |
| 29 |
24 namespace shell { | 30 namespace shell { |
25 | 31 |
26 namespace { | 32 namespace { |
27 | 33 |
28 // Used by TestAPI. | 34 // Used by TestAPI. |
29 bool has_created_instance = false; | 35 bool has_created_instance = false; |
30 | 36 |
31 std::vector<std::string> Concatenate(const std::vector<std::string>& v1, | 37 std::vector<std::string> Concatenate(const std::vector<std::string>& v1, |
32 const std::vector<std::string>& v2) { | 38 const std::vector<std::string>& v2) { |
33 if (!v1.size()) | 39 if (!v1.size()) |
(...skipping 16 matching lines...) Expand all Loading... |
50 | 56 |
51 GURL ApplicationManager::Delegate::ResolveMappings(const GURL& url) { | 57 GURL ApplicationManager::Delegate::ResolveMappings(const GURL& url) { |
52 return url; | 58 return url; |
53 } | 59 } |
54 | 60 |
55 class ApplicationManager::ContentHandlerConnection : public mojo::ErrorHandler { | 61 class ApplicationManager::ContentHandlerConnection : public mojo::ErrorHandler { |
56 public: | 62 public: |
57 ContentHandlerConnection(ApplicationManager* manager, | 63 ContentHandlerConnection(ApplicationManager* manager, |
58 const GURL& content_handler_url) | 64 const GURL& content_handler_url) |
59 : manager_(manager), content_handler_url_(content_handler_url) { | 65 : manager_(manager), content_handler_url_(content_handler_url) { |
60 mojo::ServiceProviderPtr services; | 66 ServiceProviderPtr services; |
61 manager->ConnectToApplication(content_handler_url, GURL(), | 67 manager->ConnectToApplication(content_handler_url, GURL(), |
62 mojo::GetProxy(&services), nullptr, | 68 mojo::GetProxy(&services), nullptr, |
63 base::Closure()); | 69 base::Closure()); |
64 mojo::MessagePipe pipe; | 70 mojo::MessagePipe pipe; |
65 content_handler_.Bind(pipe.handle0.Pass()); | 71 content_handler_.Bind(pipe.handle0.Pass()); |
66 services->ConnectToService(mojo::ContentHandler::Name_, | 72 services->ConnectToService(mojo::ContentHandler::Name_, |
67 pipe.handle1.Pass()); | 73 pipe.handle1.Pass()); |
68 content_handler_.set_error_handler(this); | 74 content_handler_.set_error_handler(this); |
69 } | 75 } |
70 | 76 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 STLDeleteValues(&scheme_to_loader_); | 117 STLDeleteValues(&scheme_to_loader_); |
112 } | 118 } |
113 | 119 |
114 void ApplicationManager::TerminateShellConnections() { | 120 void ApplicationManager::TerminateShellConnections() { |
115 STLDeleteValues(&identity_to_shell_impl_); | 121 STLDeleteValues(&identity_to_shell_impl_); |
116 } | 122 } |
117 | 123 |
118 void ApplicationManager::ConnectToApplication( | 124 void ApplicationManager::ConnectToApplication( |
119 const GURL& requested_url, | 125 const GURL& requested_url, |
120 const GURL& requestor_url, | 126 const GURL& requestor_url, |
121 mojo::InterfaceRequest<mojo::ServiceProvider> services, | 127 InterfaceRequest<ServiceProvider> services, |
122 mojo::ServiceProviderPtr exposed_services, | 128 ServiceProviderPtr exposed_services, |
123 const base::Closure& on_application_end) { | 129 const base::Closure& on_application_end) { |
124 ConnectToApplicationWithParameters( | 130 ConnectToApplicationWithParameters( |
125 requested_url, requestor_url, services.Pass(), exposed_services.Pass(), | 131 requested_url, requestor_url, services.Pass(), exposed_services.Pass(), |
126 on_application_end, std::vector<std::string>()); | 132 on_application_end, std::vector<std::string>()); |
127 } | 133 } |
128 | 134 |
129 void ApplicationManager::ConnectToApplicationWithParameters( | 135 void ApplicationManager::ConnectToApplicationWithParameters( |
130 const GURL& requested_url, | 136 const GURL& requested_url, |
131 const GURL& requestor_url, | 137 const GURL& requestor_url, |
132 mojo::InterfaceRequest<mojo::ServiceProvider> services, | 138 InterfaceRequest<ServiceProvider> services, |
133 mojo::ServiceProviderPtr exposed_services, | 139 ServiceProviderPtr exposed_services, |
134 const base::Closure& on_application_end, | 140 const base::Closure& on_application_end, |
135 const std::vector<std::string>& pre_redirect_parameters) { | 141 const std::vector<std::string>& pre_redirect_parameters) { |
136 TRACE_EVENT_INSTANT1( | 142 TRACE_EVENT_INSTANT1( |
137 "mojo_shell", "ApplicationManager::ConnectToApplicationWithParameters", | 143 "mojo_shell", "ApplicationManager::ConnectToApplicationWithParameters", |
138 TRACE_EVENT_SCOPE_THREAD, "requested_url", requested_url.spec()); | 144 TRACE_EVENT_SCOPE_THREAD, "requested_url", requested_url.spec()); |
139 DCHECK(requested_url.is_valid()); | 145 DCHECK(requested_url.is_valid()); |
140 | 146 |
141 // We check both the mapped and resolved urls for existing shell_impls because | 147 // We check both the mapped and resolved urls for existing shell_impls because |
142 // external applications can be registered for the unresolved mojo:foo urls. | 148 // external applications can be registered for the unresolved mojo:foo urls. |
143 | 149 |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
197 ? NativeApplicationCleanup::DONT_DELETE | 203 ? NativeApplicationCleanup::DONT_DELETE |
198 : NativeApplicationCleanup::DELETE; | 204 : NativeApplicationCleanup::DELETE; |
199 | 205 |
200 new NetworkFetcher(disable_cache_, resolved_url, network_service_.get(), | 206 new NetworkFetcher(disable_cache_, resolved_url, network_service_.get(), |
201 base::Bind(callback, cleanup)); | 207 base::Bind(callback, cleanup)); |
202 } | 208 } |
203 | 209 |
204 bool ApplicationManager::ConnectToRunningApplication( | 210 bool ApplicationManager::ConnectToRunningApplication( |
205 const GURL& resolved_url, | 211 const GURL& resolved_url, |
206 const GURL& requestor_url, | 212 const GURL& requestor_url, |
207 mojo::InterfaceRequest<mojo::ServiceProvider>* services, | 213 InterfaceRequest<ServiceProvider>* services, |
208 mojo::ServiceProviderPtr* exposed_services) { | 214 ServiceProviderPtr* exposed_services) { |
209 GURL application_url = GetBaseURLAndQuery(resolved_url, nullptr); | 215 GURL application_url = GetBaseURLAndQuery(resolved_url, nullptr); |
210 ShellImpl* shell_impl = GetShellImpl(application_url); | 216 ShellImpl* shell_impl = GetShellImpl(application_url); |
211 if (!shell_impl) | 217 if (!shell_impl) |
212 return false; | 218 return false; |
213 | 219 |
214 ConnectToClient(shell_impl, resolved_url, requestor_url, services->Pass(), | 220 ConnectToClient(shell_impl, resolved_url, requestor_url, services->Pass(), |
215 exposed_services->Pass()); | 221 exposed_services->Pass()); |
216 return true; | 222 return true; |
217 } | 223 } |
218 | 224 |
219 bool ApplicationManager::ConnectToApplicationWithLoader( | 225 bool ApplicationManager::ConnectToApplicationWithLoader( |
220 const GURL& resolved_url, | 226 const GURL& resolved_url, |
221 const GURL& requestor_url, | 227 const GURL& requestor_url, |
222 mojo::InterfaceRequest<mojo::ServiceProvider>* services, | 228 InterfaceRequest<ServiceProvider>* services, |
223 mojo::ServiceProviderPtr* exposed_services, | 229 ServiceProviderPtr* exposed_services, |
224 const base::Closure& on_application_end, | 230 const base::Closure& on_application_end, |
225 const std::vector<std::string>& parameters, | 231 const std::vector<std::string>& parameters, |
226 ApplicationLoader* loader) { | 232 ApplicationLoader* loader) { |
227 if (!loader) | 233 if (!loader) |
228 return false; | 234 return false; |
229 | 235 |
230 loader->Load( | 236 loader->Load( |
231 resolved_url, | 237 resolved_url, |
232 RegisterShell(resolved_url, requestor_url, services->Pass(), | 238 RegisterShell(resolved_url, requestor_url, services->Pass(), |
233 exposed_services->Pass(), on_application_end, parameters)); | 239 exposed_services->Pass(), on_application_end, parameters)); |
234 return true; | 240 return true; |
235 } | 241 } |
236 | 242 |
237 mojo::InterfaceRequest<mojo::Application> ApplicationManager::RegisterShell( | 243 InterfaceRequest<Application> ApplicationManager::RegisterShell( |
238 const GURL& resolved_url, | 244 const GURL& resolved_url, |
239 const GURL& requestor_url, | 245 const GURL& requestor_url, |
240 mojo::InterfaceRequest<mojo::ServiceProvider> services, | 246 InterfaceRequest<ServiceProvider> services, |
241 mojo::ServiceProviderPtr exposed_services, | 247 ServiceProviderPtr exposed_services, |
242 const base::Closure& on_application_end, | 248 const base::Closure& on_application_end, |
243 const std::vector<std::string>& parameters) { | 249 const std::vector<std::string>& parameters) { |
244 Identity app_identity(resolved_url); | 250 Identity app_identity(resolved_url); |
245 | 251 |
246 mojo::ApplicationPtr application; | 252 mojo::ApplicationPtr application; |
247 mojo::InterfaceRequest<mojo::Application> application_request = | 253 InterfaceRequest<Application> application_request = |
248 mojo::GetProxy(&application); | 254 mojo::GetProxy(&application); |
249 ShellImpl* shell = | 255 ShellImpl* shell = |
250 new ShellImpl(application.Pass(), this, app_identity, on_application_end); | 256 new ShellImpl(application.Pass(), this, app_identity, on_application_end); |
251 identity_to_shell_impl_[app_identity] = shell; | 257 identity_to_shell_impl_[app_identity] = shell; |
252 shell->InitializeApplication(mojo::Array<mojo::String>::From(parameters)); | 258 shell->InitializeApplication(mojo::Array<mojo::String>::From(parameters)); |
253 ConnectToClient(shell, resolved_url, requestor_url, services.Pass(), | 259 ConnectToClient(shell, resolved_url, requestor_url, services.Pass(), |
254 exposed_services.Pass()); | 260 exposed_services.Pass()); |
255 return application_request.Pass(); | 261 return application_request.Pass(); |
256 } | 262 } |
257 | 263 |
258 ShellImpl* ApplicationManager::GetShellImpl(const GURL& url) { | 264 ShellImpl* ApplicationManager::GetShellImpl(const GURL& url) { |
259 const auto& shell_it = identity_to_shell_impl_.find(Identity(url)); | 265 const auto& shell_it = identity_to_shell_impl_.find(Identity(url)); |
260 if (shell_it != identity_to_shell_impl_.end()) | 266 if (shell_it != identity_to_shell_impl_.end()) |
261 return shell_it->second; | 267 return shell_it->second; |
262 return nullptr; | 268 return nullptr; |
263 } | 269 } |
264 | 270 |
265 void ApplicationManager::ConnectToClient( | 271 void ApplicationManager::ConnectToClient( |
266 ShellImpl* shell_impl, | 272 ShellImpl* shell_impl, |
267 const GURL& resolved_url, | 273 const GURL& resolved_url, |
268 const GURL& requestor_url, | 274 const GURL& requestor_url, |
269 mojo::InterfaceRequest<mojo::ServiceProvider> services, | 275 InterfaceRequest<ServiceProvider> services, |
270 mojo::ServiceProviderPtr exposed_services) { | 276 ServiceProviderPtr exposed_services) { |
271 shell_impl->ConnectToClient(resolved_url, requestor_url, services.Pass(), | 277 shell_impl->ConnectToClient(resolved_url, requestor_url, services.Pass(), |
272 exposed_services.Pass()); | 278 exposed_services.Pass()); |
273 } | 279 } |
274 | 280 |
275 void ApplicationManager::HandleFetchCallback( | 281 void ApplicationManager::HandleFetchCallback( |
276 const GURL& requestor_url, | 282 const GURL& requestor_url, |
277 mojo::InterfaceRequest<mojo::ServiceProvider> services, | 283 InterfaceRequest<ServiceProvider> services, |
278 mojo::ServiceProviderPtr exposed_services, | 284 ServiceProviderPtr exposed_services, |
279 const base::Closure& on_application_end, | 285 const base::Closure& on_application_end, |
280 const std::vector<std::string>& parameters, | 286 const std::vector<std::string>& parameters, |
281 NativeApplicationCleanup cleanup, | 287 NativeApplicationCleanup cleanup, |
282 scoped_ptr<Fetcher> fetcher) { | 288 scoped_ptr<Fetcher> fetcher) { |
283 if (!fetcher) { | 289 if (!fetcher) { |
284 // Network error. Drop |application_request| to tell requestor. | 290 // Network error. Drop |application_request| to tell requestor. |
285 return; | 291 return; |
286 } | 292 } |
287 | 293 |
288 GURL redirect_url = fetcher->GetRedirectURL(); | 294 GURL redirect_url = fetcher->GetRedirectURL(); |
289 if (!redirect_url.is_empty()) { | 295 if (!redirect_url.is_empty()) { |
290 // And around we go again... Whee! | 296 // And around we go again... Whee! |
291 ConnectToApplicationWithParameters(redirect_url, requestor_url, | 297 ConnectToApplicationWithParameters(redirect_url, requestor_url, |
292 services.Pass(), exposed_services.Pass(), | 298 services.Pass(), exposed_services.Pass(), |
293 on_application_end, parameters); | 299 on_application_end, parameters); |
294 return; | 300 return; |
295 } | 301 } |
296 | 302 |
297 // We already checked if the application was running before we fetched it, but | 303 // We already checked if the application was running before we fetched it, but |
298 // it might have started while the fetch was outstanding. We don't want to | 304 // it might have started while the fetch was outstanding. We don't want to |
299 // have two copies of the app running, so check again. | 305 // have two copies of the app running, so check again. |
300 // | 306 // |
301 // Also, it's possible the original URL was redirected to an app that is | 307 // Also, it's possible the original URL was redirected to an app that is |
302 // already running. | 308 // already running. |
303 if (ConnectToRunningApplication(fetcher->GetURL(), requestor_url, &services, | 309 if (ConnectToRunningApplication(fetcher->GetURL(), requestor_url, &services, |
304 &exposed_services)) { | 310 &exposed_services)) { |
305 return; | 311 return; |
306 } | 312 } |
307 | 313 |
308 mojo::InterfaceRequest<mojo::Application> request( | 314 InterfaceRequest<Application> request( |
309 RegisterShell(fetcher->GetURL(), requestor_url, services.Pass(), | 315 RegisterShell(fetcher->GetURL(), requestor_url, services.Pass(), |
310 exposed_services.Pass(), on_application_end, parameters)); | 316 exposed_services.Pass(), on_application_end, parameters)); |
311 | 317 |
312 // If the response begins with a #!mojo <content-handler-url>, use it. | 318 // If the response begins with a #!mojo <content-handler-url>, use it. |
313 GURL content_handler_url; | 319 GURL content_handler_url; |
314 std::string shebang; | 320 std::string shebang; |
315 if (fetcher->PeekContentHandler(&shebang, &content_handler_url)) { | 321 if (fetcher->PeekContentHandler(&shebang, &content_handler_url)) { |
316 LoadWithContentHandler( | 322 LoadWithContentHandler( |
317 content_handler_url, request.Pass(), | 323 content_handler_url, request.Pass(), |
318 fetcher->AsURLResponse(blocking_pool_, | 324 fetcher->AsURLResponse(blocking_pool_, |
(...skipping 24 matching lines...) Expand all Loading... |
343 } | 349 } |
344 | 350 |
345 fetcher->AsPath( | 351 fetcher->AsPath( |
346 blocking_pool_, | 352 blocking_pool_, |
347 base::Bind(&ApplicationManager::RunNativeApplication, | 353 base::Bind(&ApplicationManager::RunNativeApplication, |
348 weak_ptr_factory_.GetWeakPtr(), base::Passed(request.Pass()), | 354 weak_ptr_factory_.GetWeakPtr(), base::Passed(request.Pass()), |
349 options, cleanup, base::Passed(fetcher.Pass()))); | 355 options, cleanup, base::Passed(fetcher.Pass()))); |
350 } | 356 } |
351 | 357 |
352 void ApplicationManager::RunNativeApplication( | 358 void ApplicationManager::RunNativeApplication( |
353 mojo::InterfaceRequest<mojo::Application> application_request, | 359 InterfaceRequest<Application> application_request, |
354 const NativeRunnerFactory::Options& options, | 360 const NativeRunnerFactory::Options& options, |
355 NativeApplicationCleanup cleanup, | 361 NativeApplicationCleanup cleanup, |
356 scoped_ptr<Fetcher> fetcher, | 362 scoped_ptr<Fetcher> fetcher, |
357 const base::FilePath& path, | 363 const base::FilePath& path, |
358 bool path_exists) { | 364 bool path_exists) { |
359 // We only passed fetcher to keep it alive. Done with it now. | 365 // We only passed fetcher to keep it alive. Done with it now. |
360 fetcher.reset(); | 366 fetcher.reset(); |
361 | 367 |
362 DCHECK(application_request.is_pending()); | 368 DCHECK(application_request.is_pending()); |
363 | 369 |
(...skipping 15 matching lines...) Expand all Loading... |
379 void ApplicationManager::RegisterContentHandler( | 385 void ApplicationManager::RegisterContentHandler( |
380 const std::string& mime_type, | 386 const std::string& mime_type, |
381 const GURL& content_handler_url) { | 387 const GURL& content_handler_url) { |
382 DCHECK(content_handler_url.is_valid()) | 388 DCHECK(content_handler_url.is_valid()) |
383 << "Content handler URL is invalid for mime type " << mime_type; | 389 << "Content handler URL is invalid for mime type " << mime_type; |
384 mime_type_to_url_[mime_type] = content_handler_url; | 390 mime_type_to_url_[mime_type] = content_handler_url; |
385 } | 391 } |
386 | 392 |
387 void ApplicationManager::LoadWithContentHandler( | 393 void ApplicationManager::LoadWithContentHandler( |
388 const GURL& content_handler_url, | 394 const GURL& content_handler_url, |
389 mojo::InterfaceRequest<mojo::Application> application_request, | 395 InterfaceRequest<Application> application_request, |
390 mojo::URLResponsePtr url_response) { | 396 mojo::URLResponsePtr url_response) { |
391 ContentHandlerConnection* connection = nullptr; | 397 ContentHandlerConnection* connection = nullptr; |
392 URLToContentHandlerMap::iterator iter = | 398 URLToContentHandlerMap::iterator iter = |
393 url_to_content_handler_.find(content_handler_url); | 399 url_to_content_handler_.find(content_handler_url); |
394 if (iter != url_to_content_handler_.end()) { | 400 if (iter != url_to_content_handler_.end()) { |
395 connection = iter->second; | 401 connection = iter->second; |
396 } else { | 402 } else { |
397 connection = new ContentHandlerConnection(this, content_handler_url); | 403 connection = new ContentHandlerConnection(this, content_handler_url); |
398 url_to_content_handler_[content_handler_url] = connection; | 404 url_to_content_handler_[content_handler_url] = connection; |
399 } | 405 } |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
477 auto it = | 483 auto it = |
478 url_to_content_handler_.find(content_handler->content_handler_url()); | 484 url_to_content_handler_.find(content_handler->content_handler_url()); |
479 DCHECK(it != url_to_content_handler_.end()); | 485 DCHECK(it != url_to_content_handler_.end()); |
480 delete it->second; | 486 delete it->second; |
481 url_to_content_handler_.erase(it); | 487 url_to_content_handler_.erase(it); |
482 } | 488 } |
483 | 489 |
484 mojo::ScopedMessagePipeHandle ApplicationManager::ConnectToServiceByName( | 490 mojo::ScopedMessagePipeHandle ApplicationManager::ConnectToServiceByName( |
485 const GURL& application_url, | 491 const GURL& application_url, |
486 const std::string& interface_name) { | 492 const std::string& interface_name) { |
487 mojo::ServiceProviderPtr services; | 493 ServiceProviderPtr services; |
488 ConnectToApplication(application_url, GURL(), mojo::GetProxy(&services), | 494 ConnectToApplication(application_url, GURL(), mojo::GetProxy(&services), |
489 nullptr, base::Closure()); | 495 nullptr, base::Closure()); |
490 mojo::MessagePipe pipe; | 496 mojo::MessagePipe pipe; |
491 services->ConnectToService(interface_name, pipe.handle1.Pass()); | 497 services->ConnectToService(interface_name, pipe.handle1.Pass()); |
492 return pipe.handle0.Pass(); | 498 return pipe.handle0.Pass(); |
493 } | 499 } |
494 | 500 |
495 std::vector<std::string> ApplicationManager::GetArgsForURL(const GURL& url) { | 501 std::vector<std::string> ApplicationManager::GetArgsForURL(const GURL& url) { |
496 const auto& args_it = url_to_args_.find(url); | 502 const auto& args_it = url_to_args_.find(url); |
497 if (args_it != url_to_args_.end()) | 503 if (args_it != url_to_args_.end()) |
498 return args_it->second; | 504 return args_it->second; |
499 return std::vector<std::string>(); | 505 return std::vector<std::string>(); |
500 } | 506 } |
501 | 507 |
502 void ApplicationManager::CleanupRunner(NativeRunner* runner) { | 508 void ApplicationManager::CleanupRunner(NativeRunner* runner) { |
503 native_runners_.erase( | 509 native_runners_.erase( |
504 std::find(native_runners_.begin(), native_runners_.end(), runner)); | 510 std::find(native_runners_.begin(), native_runners_.end(), runner)); |
505 } | 511 } |
506 | 512 |
507 } // namespace shell | 513 } // namespace shell |
OLD | NEW |