1 // Copyright 2015 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.
5 #include "mojo/package_manager/package_manager_impl.h"
7 #include "mojo/fetcher/about_fetcher.h"
8 #include "mojo/fetcher/data_fetcher.h"
9 #include "mojo/fetcher/local_fetcher.h"
10 #include "mojo/fetcher/network_fetcher.h"
11 #include "mojo/fetcher/switches.h"
12 #include "mojo/fetcher/update_fetcher.h"
13 #include "mojo/shell/application_manager.h"
14 #include "mojo/shell/connect_util.h"
15 #include "mojo/shell/query_util.h"
16 #include "mojo/shell/switches.h"
17 #include "mojo/util/filename_util.h"
21 namespace package_manager
{
23 PackageManagerImpl::PackageManagerImpl(
24 const base::FilePath
& shell_file_root
)
25 : application_manager_(nullptr),
26 disable_cache_(base::CommandLine::ForCurrentProcess()->HasSwitch(
27 switches::kDisableCache
)) {
28 if (!shell_file_root
.empty()) {
29 GURL mojo_root_file_url
=
30 util::FilePathToFileURL(shell_file_root
).Resolve(std::string());
31 url_resolver_
.reset(new fetcher::URLResolver(mojo_root_file_url
));
35 PackageManagerImpl::~PackageManagerImpl() {
38 void PackageManagerImpl::RegisterContentHandler(
39 const std::string
& mime_type
,
40 const GURL
& content_handler_url
) {
41 DCHECK(content_handler_url
.is_valid())
42 << "Content handler URL is invalid for mime type " << mime_type
;
43 mime_type_to_url_
[mime_type
] = content_handler_url
;
46 void PackageManagerImpl::RegisterApplicationPackageAlias(
48 const GURL
& content_handler_package
,
49 const std::string
& qualifier
) {
50 application_package_alias_
[alias
] =
51 std::make_pair(content_handler_package
, qualifier
);
54 void PackageManagerImpl::SetApplicationManager(
55 shell::ApplicationManager
* manager
) {
56 application_manager_
= manager
;
59 void PackageManagerImpl::FetchRequest(
60 URLRequestPtr request
,
61 const shell::Fetcher::FetchCallback
& loader_callback
) {
62 GURL
url(request
->url
);
63 if (url
.SchemeIs(fetcher::AboutFetcher::kAboutScheme
)) {
64 fetcher::AboutFetcher::Start(url
, loader_callback
);
68 if (url
.SchemeIs(url::kDataScheme
)) {
69 fetcher::DataFetcher::Start(url
, loader_callback
);
73 GURL resolved_url
= ResolveURL(url
);
74 if (resolved_url
.SchemeIsFile()) {
75 // LocalFetcher uses the network service to infer MIME types from URLs.
76 // Skip this for mojo URLs to avoid recursively loading the network service.
77 if (!network_service_
&& !url
.SchemeIs("mojo")) {
78 shell::ConnectToService(application_manager_
,
79 GURL("mojo:network_service"), &network_service_
);
81 // Ownership of this object is transferred to |loader_callback|.
82 // TODO(beng): this is eff'n weird.
83 new fetcher::LocalFetcher(
84 network_service_
.get(), resolved_url
,
85 shell::GetBaseURLAndQuery(resolved_url
, nullptr),
91 // TODO(beng): figure out how this should be integrated now that mapped_url
93 // TODO(scottmg): to quote someone I know, if you liked this you shouldda put
95 if (url
.SchemeIs("mojo") &&
96 base::CommandLine::ForCurrentProcess()->HasSwitch(
97 switches::kUseUpdater
)) {
98 shell::ConnectToService(application_manager_
, GURL("mojo:updater"),
100 // Ownership of this object is transferred to |loader_callback|.
101 // TODO(beng): this is eff'n weird.
102 new fetcher::UpdateFetcher(url
, updater_
.get(), loader_callback
);
107 if (!url_loader_factory_
) {
108 shell::ConnectToService(application_manager_
, GURL("mojo:network_service"),
109 &url_loader_factory_
);
112 // Ownership of this object is transferred to |loader_callback|.
113 // TODO(beng): this is eff'n weird.
114 new fetcher::NetworkFetcher(disable_cache_
, request
.Pass(),
115 url_loader_factory_
.get(), loader_callback
);
118 bool PackageManagerImpl::HandleWithContentHandler(shell::Fetcher
* fetcher
,
120 base::TaskRunner
* task_runner
,
121 URLResponsePtr
* new_response
,
122 GURL
* content_handler_url
,
123 std::string
* qualifier
) {
124 // TODO(beng): it seems like some delegate should/would want to have a say in
125 // configuring the qualifier also.
126 bool enable_multi_process
= base::CommandLine::ForCurrentProcess()->HasSwitch(
127 switches::kEnableMultiprocess
);
129 // The response begins with a #!mojo <content-handler-url>.
131 if (fetcher
->PeekContentHandler(&shebang
, content_handler_url
)) {
132 *new_response
= fetcher
->AsURLResponse(
133 task_runner
, static_cast<int>(shebang
.size()));
134 *qualifier
= enable_multi_process
? (*new_response
)->site
.To
<std::string
>()
139 // The response MIME type matches a registered content handler.
140 MimeTypeToURLMap::iterator iter
= mime_type_to_url_
.find(fetcher
->MimeType());
141 if (iter
!= mime_type_to_url_
.end()) {
142 *new_response
= fetcher
->AsURLResponse(task_runner
, 0);
143 *content_handler_url
= iter
->second
;
144 *qualifier
= enable_multi_process
? (*new_response
)->site
.To
<std::string
>()
149 // The response URL matches a registered content handler.
150 auto alias_iter
= application_package_alias_
.find(url
);
151 if (alias_iter
!= application_package_alias_
.end()) {
152 // We replace the qualifier with the one our package alias requested.
153 *new_response
= URLResponse::New();
154 (*new_response
)->url
= url
.spec();
156 // Why can't we use this in single process mode? Because of
157 // base::AtExitManager. If you link in ApplicationRunner into your code, and
158 // then we make initialize multiple copies of the application, we end up
159 // with multiple AtExitManagers and will check on the second one being
162 // Why doesn't that happen when running different apps? Because
163 // your_thing.mojo!base::AtExitManager and
164 // my_thing.mojo!base::AtExitManager are different symbols.
165 *qualifier
= enable_multi_process
? alias_iter
->second
.second
167 *content_handler_url
= alias_iter
->second
.first
;
174 GURL
PackageManagerImpl::ResolveURL(const GURL
& url
) {
175 return url_resolver_
.get() ? url_resolver_
->ResolveMojoURL(url
) : url
;
178 } // namespace package_manager