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 "mojo/shell/application_manager/application_manager.h" | 5 #include "mojo/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" |
(...skipping 11 matching lines...) Expand all Loading... |
22 #include "third_party/mojo_services/src/content_handler/public/interfaces/conten
t_handler.mojom.h" | 22 #include "third_party/mojo_services/src/content_handler/public/interfaces/conten
t_handler.mojom.h" |
23 | 23 |
24 namespace mojo { | 24 namespace mojo { |
25 namespace shell { | 25 namespace shell { |
26 | 26 |
27 namespace { | 27 namespace { |
28 | 28 |
29 // Used by TestAPI. | 29 // Used by TestAPI. |
30 bool has_created_instance = false; | 30 bool has_created_instance = false; |
31 | 31 |
32 std::vector<std::string> Concatenate(const std::vector<std::string>& v1, | |
33 const std::vector<std::string>& v2) { | |
34 if (!v1.size()) | |
35 return v2; | |
36 if (!v2.size()) | |
37 return v1; | |
38 std::vector<std::string> result(v1); | |
39 result.insert(result.end(), v1.begin(), v1.end()); | |
40 return result; | |
41 } | |
42 | |
43 } // namespace | 32 } // namespace |
44 | 33 |
45 ApplicationManager::Delegate::~Delegate() { | 34 ApplicationManager::Delegate::~Delegate() { |
46 } | 35 } |
47 | 36 |
48 GURL ApplicationManager::Delegate::ResolveURL(const GURL& url) { | 37 GURL ApplicationManager::Delegate::ResolveURL(const GURL& url) { |
49 return url; | 38 return url; |
50 } | 39 } |
51 | 40 |
52 GURL ApplicationManager::Delegate::ResolveMappings(const GURL& url) { | 41 GURL ApplicationManager::Delegate::ResolveMappings(const GURL& url) { |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 return; | 136 return; |
148 } | 137 } |
149 | 138 |
150 GURL resolved_url = delegate_->ResolveURL(mapped_url); | 139 GURL resolved_url = delegate_->ResolveURL(mapped_url); |
151 if (ConnectToRunningApplication(resolved_url, requestor_url, &services, | 140 if (ConnectToRunningApplication(resolved_url, requestor_url, &services, |
152 &exposed_services)) { | 141 &exposed_services)) { |
153 return; | 142 return; |
154 } | 143 } |
155 | 144 |
156 // The application is not running, let's compute the parameters. | 145 // The application is not running, let's compute the parameters. |
157 std::vector<std::string> parameters = | |
158 Concatenate(pre_redirect_parameters, GetArgsForURL(resolved_url)); | |
159 | |
160 if (ConnectToApplicationWithLoader(mapped_url, requestor_url, &services, | 146 if (ConnectToApplicationWithLoader(mapped_url, requestor_url, &services, |
161 &exposed_services, on_application_end, | 147 &exposed_services, on_application_end, |
162 parameters, GetLoaderForURL(mapped_url))) { | 148 pre_redirect_parameters, |
| 149 GetLoaderForURL(mapped_url))) { |
| 150 return; |
| 151 } |
| 152 |
| 153 if (ConnectToApplicationWithLoader(resolved_url, requestor_url, &services, |
| 154 &exposed_services, on_application_end, |
| 155 pre_redirect_parameters, |
| 156 GetLoaderForURL(resolved_url))) { |
163 return; | 157 return; |
164 } | 158 } |
165 | 159 |
166 if (ConnectToApplicationWithLoader( | 160 if (ConnectToApplicationWithLoader( |
167 resolved_url, requestor_url, &services, &exposed_services, | 161 resolved_url, requestor_url, &services, &exposed_services, |
168 on_application_end, parameters, GetLoaderForURL(resolved_url))) { | 162 on_application_end, pre_redirect_parameters, default_loader_.get())) { |
169 return; | 163 return; |
170 } | 164 } |
171 | 165 |
172 if (ConnectToApplicationWithLoader(resolved_url, requestor_url, &services, | 166 auto callback = base::Bind(&ApplicationManager::HandleFetchCallback, |
173 &exposed_services, on_application_end, | 167 weak_ptr_factory_.GetWeakPtr(), requestor_url, |
174 parameters, default_loader_.get())) { | 168 base::Passed(services.Pass()), |
175 return; | 169 base::Passed(exposed_services.Pass()), |
176 } | 170 on_application_end, pre_redirect_parameters); |
177 | |
178 auto callback = base::Bind( | |
179 &ApplicationManager::HandleFetchCallback, weak_ptr_factory_.GetWeakPtr(), | |
180 requestor_url, base::Passed(services.Pass()), | |
181 base::Passed(exposed_services.Pass()), on_application_end, | |
182 parameters); | |
183 | 171 |
184 if (resolved_url.SchemeIsFile()) { | 172 if (resolved_url.SchemeIsFile()) { |
185 new LocalFetcher( | 173 new LocalFetcher( |
186 resolved_url, GetBaseURLAndQuery(resolved_url, nullptr), | 174 resolved_url, GetBaseURLAndQuery(resolved_url, nullptr), |
187 base::Bind(callback, NativeApplicationCleanup::DONT_DELETE)); | 175 base::Bind(callback, NativeApplicationCleanup::DONT_DELETE)); |
188 return; | 176 return; |
189 } | 177 } |
190 | 178 |
191 if (!network_service_) | 179 if (!network_service_) |
192 ConnectToService(GURL("mojo:network_service"), &network_service_); | 180 ConnectToService(GURL("mojo:network_service"), &network_service_); |
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
368 | 356 |
369 TRACE_EVENT1("mojo_shell", "ApplicationManager::RunNativeApplication", "path", | 357 TRACE_EVENT1("mojo_shell", "ApplicationManager::RunNativeApplication", "path", |
370 path.AsUTF8Unsafe()); | 358 path.AsUTF8Unsafe()); |
371 NativeRunner* runner = native_runner_factory_->Create(options).release(); | 359 NativeRunner* runner = native_runner_factory_->Create(options).release(); |
372 native_runners_.push_back(runner); | 360 native_runners_.push_back(runner); |
373 runner->Start(path, cleanup, application_request.Pass(), | 361 runner->Start(path, cleanup, application_request.Pass(), |
374 base::Bind(&ApplicationManager::CleanupRunner, | 362 base::Bind(&ApplicationManager::CleanupRunner, |
375 weak_ptr_factory_.GetWeakPtr(), runner)); | 363 weak_ptr_factory_.GetWeakPtr(), runner)); |
376 } | 364 } |
377 | 365 |
378 void ApplicationManager::RegisterExternalApplication( | |
379 const GURL& url, | |
380 const std::vector<std::string>& args, | |
381 ApplicationPtr application) { | |
382 const auto& args_it = url_to_args_.find(url); | |
383 if (args_it != url_to_args_.end()) { | |
384 LOG(WARNING) << "--args-for provided for external application " << url | |
385 << " <ignored>"; | |
386 } | |
387 Identity identity(url); | |
388 ShellImpl* shell_impl = | |
389 new ShellImpl(application.Pass(), this, identity, base::Closure()); | |
390 identity_to_shell_impl_[identity] = shell_impl; | |
391 shell_impl->InitializeApplication(Array<String>::From(args)); | |
392 } | |
393 | |
394 void ApplicationManager::RegisterContentHandler( | 366 void ApplicationManager::RegisterContentHandler( |
395 const std::string& mime_type, | 367 const std::string& mime_type, |
396 const GURL& content_handler_url) { | 368 const GURL& content_handler_url) { |
397 DCHECK(content_handler_url.is_valid()) | 369 DCHECK(content_handler_url.is_valid()) |
398 << "Content handler URL is invalid for mime type " << mime_type; | 370 << "Content handler URL is invalid for mime type " << mime_type; |
399 mime_type_to_url_[mime_type] = content_handler_url; | 371 mime_type_to_url_[mime_type] = content_handler_url; |
400 } | 372 } |
401 | 373 |
402 void ApplicationManager::LoadWithContentHandler( | 374 void ApplicationManager::LoadWithContentHandler( |
403 const GURL& content_handler_url, | 375 const GURL& content_handler_url, |
(...skipping 23 matching lines...) Expand all Loading... |
427 | 399 |
428 void ApplicationManager::SetLoaderForScheme( | 400 void ApplicationManager::SetLoaderForScheme( |
429 scoped_ptr<ApplicationLoader> loader, | 401 scoped_ptr<ApplicationLoader> loader, |
430 const std::string& scheme) { | 402 const std::string& scheme) { |
431 SchemeToLoaderMap::iterator it = scheme_to_loader_.find(scheme); | 403 SchemeToLoaderMap::iterator it = scheme_to_loader_.find(scheme); |
432 if (it != scheme_to_loader_.end()) | 404 if (it != scheme_to_loader_.end()) |
433 delete it->second; | 405 delete it->second; |
434 scheme_to_loader_[scheme] = loader.release(); | 406 scheme_to_loader_[scheme] = loader.release(); |
435 } | 407 } |
436 | 408 |
437 void ApplicationManager::SetArgsForURL(const std::vector<std::string>& args, | |
438 const GURL& url) { | |
439 url_to_args_[url].insert(url_to_args_[url].end(), args.begin(), args.end()); | |
440 GURL mapped_url = delegate_->ResolveMappings(url); | |
441 if (mapped_url != url) { | |
442 url_to_args_[mapped_url].insert(url_to_args_[mapped_url].end(), | |
443 args.begin(), args.end()); | |
444 } | |
445 GURL resolved_url = delegate_->ResolveURL(mapped_url); | |
446 if (resolved_url != mapped_url) { | |
447 url_to_args_[resolved_url].insert(url_to_args_[resolved_url].end(), | |
448 args.begin(), args.end()); | |
449 } | |
450 } | |
451 | |
452 void ApplicationManager::SetNativeOptionsForURL( | 409 void ApplicationManager::SetNativeOptionsForURL( |
453 const NativeRunnerFactory::Options& options, | 410 const NativeRunnerFactory::Options& options, |
454 const GURL& url) { | 411 const GURL& url) { |
455 DCHECK(!url.has_query()); // Precondition. | 412 DCHECK(!url.has_query()); // Precondition. |
456 // Apply mappings and resolution to get the resolved URL. | 413 // Apply mappings and resolution to get the resolved URL. |
457 GURL resolved_url = delegate_->ResolveURL(delegate_->ResolveMappings(url)); | 414 GURL resolved_url = delegate_->ResolveURL(delegate_->ResolveMappings(url)); |
458 DCHECK(!resolved_url.has_query()); // Still shouldn't have query. | 415 DCHECK(!resolved_url.has_query()); // Still shouldn't have query. |
459 // TODO(vtl): We should probably also remove/disregard the query string (and | 416 // TODO(vtl): We should probably also remove/disregard the query string (and |
460 // maybe canonicalize in other ways). | 417 // maybe canonicalize in other ways). |
461 DVLOG(2) << "Storing native options for resolved URL " << resolved_url | 418 DVLOG(2) << "Storing native options for resolved URL " << resolved_url |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
500 const GURL& application_url, | 457 const GURL& application_url, |
501 const std::string& interface_name) { | 458 const std::string& interface_name) { |
502 ServiceProviderPtr services; | 459 ServiceProviderPtr services; |
503 ConnectToApplication(application_url, GURL(), GetProxy(&services), nullptr, | 460 ConnectToApplication(application_url, GURL(), GetProxy(&services), nullptr, |
504 base::Closure()); | 461 base::Closure()); |
505 MessagePipe pipe; | 462 MessagePipe pipe; |
506 services->ConnectToService(interface_name, pipe.handle1.Pass()); | 463 services->ConnectToService(interface_name, pipe.handle1.Pass()); |
507 return pipe.handle0.Pass(); | 464 return pipe.handle0.Pass(); |
508 } | 465 } |
509 | 466 |
510 std::vector<std::string> ApplicationManager::GetArgsForURL(const GURL& url) { | |
511 const auto& args_it = url_to_args_.find(url); | |
512 if (args_it != url_to_args_.end()) | |
513 return args_it->second; | |
514 return std::vector<std::string>(); | |
515 } | |
516 | |
517 void ApplicationManager::CleanupRunner(NativeRunner* runner) { | 467 void ApplicationManager::CleanupRunner(NativeRunner* runner) { |
518 native_runners_.erase( | 468 native_runners_.erase( |
519 std::find(native_runners_.begin(), native_runners_.end(), runner)); | 469 std::find(native_runners_.begin(), native_runners_.end(), runner)); |
520 } | 470 } |
521 | 471 |
522 } // namespace shell | 472 } // namespace shell |
523 } // namespace mojo | 473 } // namespace mojo |
OLD | NEW |