OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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/package_manager/package_manager_impl.h" | 5 #include "mojo/shell/package_manager/package_manager_impl.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "mojo/application/public/interfaces/content_handler.mojom.h" | 12 #include "mojo/application/public/interfaces/content_handler.mojom.h" |
13 #include "mojo/fetcher/about_fetcher.h" | 13 #include "mojo/fetcher/about_fetcher.h" |
14 #include "mojo/fetcher/data_fetcher.h" | 14 #include "mojo/fetcher/data_fetcher.h" |
15 #include "mojo/fetcher/local_fetcher.h" | 15 #include "mojo/fetcher/local_fetcher.h" |
16 #include "mojo/fetcher/network_fetcher.h" | 16 #include "mojo/fetcher/network_fetcher.h" |
17 #include "mojo/fetcher/switches.h" | 17 #include "mojo/fetcher/switches.h" |
18 #include "mojo/fetcher/update_fetcher.h" | 18 #include "mojo/fetcher/update_fetcher.h" |
19 #include "mojo/package_manager/content_handler_connection.h" | |
20 #include "mojo/shell/application_manager.h" | 19 #include "mojo/shell/application_manager.h" |
21 #include "mojo/shell/connect_util.h" | 20 #include "mojo/shell/connect_util.h" |
| 21 #include "mojo/shell/package_manager/content_handler_connection.h" |
22 #include "mojo/shell/query_util.h" | 22 #include "mojo/shell/query_util.h" |
23 #include "mojo/shell/switches.h" | 23 #include "mojo/shell/switches.h" |
24 #include "mojo/util/filename_util.h" | 24 #include "mojo/util/filename_util.h" |
25 #include "url/gurl.h" | 25 #include "url/gurl.h" |
26 | 26 |
27 namespace mojo { | 27 namespace mojo { |
28 namespace package_manager { | 28 namespace shell { |
29 | 29 |
30 PackageManagerImpl::PackageManagerImpl( | 30 PackageManagerImpl::PackageManagerImpl( |
31 const base::FilePath& shell_file_root, | 31 const base::FilePath& shell_file_root, |
32 base::TaskRunner* task_runner) | 32 base::TaskRunner* task_runner) |
33 : application_manager_(nullptr), | 33 : application_manager_(nullptr), |
34 disable_cache_(base::CommandLine::ForCurrentProcess()->HasSwitch( | 34 disable_cache_(base::CommandLine::ForCurrentProcess()->HasSwitch( |
35 switches::kDisableCache)), | 35 switches::kDisableCache)), |
36 content_handler_id_counter_(0u), | 36 content_handler_id_counter_(0u), |
37 task_runner_(task_runner), | 37 task_runner_(task_runner), |
38 shell_file_root_(shell_file_root) { | 38 shell_file_root_(shell_file_root) { |
(...skipping 20 matching lines...) Expand all Loading... |
59 } | 59 } |
60 | 60 |
61 void PackageManagerImpl::RegisterApplicationPackageAlias( | 61 void PackageManagerImpl::RegisterApplicationPackageAlias( |
62 const GURL& alias, | 62 const GURL& alias, |
63 const GURL& content_handler_package, | 63 const GURL& content_handler_package, |
64 const std::string& qualifier) { | 64 const std::string& qualifier) { |
65 application_package_alias_[alias] = | 65 application_package_alias_[alias] = |
66 std::make_pair(content_handler_package, qualifier); | 66 std::make_pair(content_handler_package, qualifier); |
67 } | 67 } |
68 | 68 |
69 void PackageManagerImpl::SetApplicationManager( | 69 void PackageManagerImpl::SetApplicationManager(ApplicationManager* manager) { |
70 shell::ApplicationManager* manager) { | |
71 application_manager_ = manager; | 70 application_manager_ = manager; |
72 } | 71 } |
73 | 72 |
74 void PackageManagerImpl::FetchRequest( | 73 void PackageManagerImpl::FetchRequest( |
75 URLRequestPtr request, | 74 URLRequestPtr request, |
76 const shell::Fetcher::FetchCallback& loader_callback) { | 75 const Fetcher::FetchCallback& loader_callback) { |
77 GURL url(request->url); | 76 GURL url(request->url); |
78 if (url.SchemeIs(fetcher::AboutFetcher::kAboutScheme)) { | 77 if (url.SchemeIs(fetcher::AboutFetcher::kAboutScheme)) { |
79 fetcher::AboutFetcher::Start(url, loader_callback); | 78 fetcher::AboutFetcher::Start(url, loader_callback); |
80 return; | 79 return; |
81 } | 80 } |
82 | 81 |
83 if (url.SchemeIs(url::kDataScheme)) { | 82 if (url.SchemeIs(url::kDataScheme)) { |
84 fetcher::DataFetcher::Start(url, loader_callback); | 83 fetcher::DataFetcher::Start(url, loader_callback); |
85 return; | 84 return; |
86 } | 85 } |
87 | 86 |
88 GURL resolved_url = ResolveURL(url); | 87 GURL resolved_url = ResolveURL(url); |
89 if (resolved_url.SchemeIsFile()) { | 88 if (resolved_url.SchemeIsFile()) { |
90 // LocalFetcher uses the network service to infer MIME types from URLs. | 89 // LocalFetcher uses the network service to infer MIME types from URLs. |
91 // Skip this for mojo URLs to avoid recursively loading the network service. | 90 // Skip this for mojo URLs to avoid recursively loading the network service. |
92 if (!network_service_ && !url.SchemeIs("mojo") && !url.SchemeIs("exe")) { | 91 if (!network_service_ && !url.SchemeIs("mojo") && !url.SchemeIs("exe")) { |
93 shell::ConnectToService(application_manager_, | 92 ConnectToService(application_manager_, GURL("mojo:network_service"), |
94 GURL("mojo:network_service"), &network_service_); | 93 &network_service_); |
95 } | 94 } |
96 // Ownership of this object is transferred to |loader_callback|. | 95 // Ownership of this object is transferred to |loader_callback|. |
97 // TODO(beng): this is eff'n weird. | 96 // TODO(beng): this is eff'n weird. |
98 new fetcher::LocalFetcher( | 97 new fetcher::LocalFetcher( |
99 network_service_.get(), resolved_url, | 98 network_service_.get(), resolved_url, |
100 shell::GetBaseURLAndQuery(resolved_url, nullptr), | 99 GetBaseURLAndQuery(resolved_url, nullptr), |
101 shell_file_root_, loader_callback); | 100 shell_file_root_, loader_callback); |
102 return; | 101 return; |
103 } | 102 } |
104 | 103 |
105 #if 0 | |
106 // TODO(beng): figure out how this should be integrated now that mapped_url | |
107 // is toast. | |
108 // TODO(scottmg): to quote someone I know, if you liked this you shouldda put | |
109 // a test on it. | |
110 if (url.SchemeIs("mojo") && | |
111 base::CommandLine::ForCurrentProcess()->HasSwitch( | |
112 switches::kUseUpdater)) { | |
113 shell::ConnectToService(application_manager_, GURL("mojo:updater"), | |
114 &updater_); | |
115 // Ownership of this object is transferred to |loader_callback|. | |
116 // TODO(beng): this is eff'n weird. | |
117 new fetcher::UpdateFetcher(url, updater_.get(), loader_callback); | |
118 return; | |
119 } | |
120 #endif | |
121 | |
122 if (!url_loader_factory_) { | 104 if (!url_loader_factory_) { |
123 shell::ConnectToService(application_manager_, GURL("mojo:network_service"), | 105 ConnectToService(application_manager_, GURL("mojo:network_service"), |
124 &url_loader_factory_); | 106 &url_loader_factory_); |
125 } | 107 } |
126 | 108 |
127 // Ownership of this object is transferred to |loader_callback|. | 109 // Ownership of this object is transferred to |loader_callback|. |
128 // TODO(beng): this is eff'n weird. | 110 // TODO(beng): this is eff'n weird. |
129 new fetcher::NetworkFetcher(disable_cache_, std::move(request), | 111 new fetcher::NetworkFetcher(disable_cache_, std::move(request), |
130 url_loader_factory_.get(), loader_callback); | 112 url_loader_factory_.get(), loader_callback); |
131 } | 113 } |
132 | 114 |
133 uint32_t PackageManagerImpl::HandleWithContentHandler( | 115 uint32_t PackageManagerImpl::HandleWithContentHandler( |
134 shell::Fetcher* fetcher, | 116 Fetcher* fetcher, |
135 const shell::Identity& source, | 117 const Identity& source, |
136 const GURL& target_url, | 118 const GURL& target_url, |
137 const shell::CapabilityFilter& target_filter, | 119 const CapabilityFilter& target_filter, |
138 InterfaceRequest<Application>* application_request) { | 120 InterfaceRequest<Application>* application_request) { |
139 shell::Identity content_handler_identity; | 121 Identity content_handler_identity; |
140 URLResponsePtr response; | 122 URLResponsePtr response; |
141 if (ShouldHandleWithContentHandler(fetcher, | 123 if (ShouldHandleWithContentHandler(fetcher, |
142 target_url, | 124 target_url, |
143 target_filter, | 125 target_filter, |
144 &content_handler_identity, | 126 &content_handler_identity, |
145 &response)) { | 127 &response)) { |
146 ContentHandlerConnection* connection = | 128 ContentHandlerConnection* connection = |
147 GetContentHandler(content_handler_identity, source); | 129 GetContentHandler(content_handler_identity, source); |
148 connection->StartApplication(std::move(*application_request), | 130 connection->StartApplication(std::move(*application_request), |
149 std::move(response)); | 131 std::move(response)); |
150 return connection->id(); | 132 return connection->id(); |
151 } | 133 } |
152 return Shell::kInvalidContentHandlerID; | 134 return Shell::kInvalidContentHandlerID; |
153 } | 135 } |
154 | 136 |
155 GURL PackageManagerImpl::ResolveURL(const GURL& url) { | 137 GURL PackageManagerImpl::ResolveURL(const GURL& url) { |
156 return url_resolver_.get() ? url_resolver_->ResolveMojoURL(url) : url; | 138 return url_resolver_.get() ? url_resolver_->ResolveMojoURL(url) : url; |
157 } | 139 } |
158 | 140 |
159 bool PackageManagerImpl::ShouldHandleWithContentHandler( | 141 bool PackageManagerImpl::ShouldHandleWithContentHandler( |
160 shell::Fetcher* fetcher, | 142 Fetcher* fetcher, |
161 const GURL& target_url, | 143 const GURL& target_url, |
162 const shell::CapabilityFilter& target_filter, | 144 const CapabilityFilter& target_filter, |
163 shell::Identity* content_handler_identity, | 145 Identity* content_handler_identity, |
164 URLResponsePtr* response) const { | 146 URLResponsePtr* response) const { |
165 // TODO(beng): it seems like some delegate should/would want to have a say in | 147 // TODO(beng): it seems like some delegate should/would want to have a say in |
166 // configuring the qualifier also. | 148 // configuring the qualifier also. |
167 // Why can't we use the real qualifier in single process mode? Because of | 149 // Why can't we use the real qualifier in single process mode? Because of |
168 // base::AtExitManager. If you link in ApplicationRunner into your code, and | 150 // base::AtExitManager. If you link in ApplicationRunner into your code, and |
169 // then we make initialize multiple copies of the application, we end up | 151 // then we make initialize multiple copies of the application, we end up |
170 // with multiple AtExitManagers and will check on the second one being | 152 // with multiple AtExitManagers and will check on the second one being |
171 // created. | 153 // created. |
172 // | 154 // |
173 // Why doesn't that happen when running different apps? Because | 155 // Why doesn't that happen when running different apps? Because |
174 // your_thing.mojo!base::AtExitManager and | 156 // your_thing.mojo!base::AtExitManager and |
175 // my_thing.mojo!base::AtExitManager are different symbols. | 157 // my_thing.mojo!base::AtExitManager are different symbols. |
176 bool use_real_qualifier = base::CommandLine::ForCurrentProcess()->HasSwitch( | 158 bool use_real_qualifier = base::CommandLine::ForCurrentProcess()->HasSwitch( |
177 switches::kEnableMultiprocess); | 159 switches::kEnableMultiprocess); |
178 | 160 |
179 GURL content_handler_url; | 161 GURL content_handler_url; |
180 // The response begins with a #!mojo <content-handler-url>. | 162 // The response begins with a #!mojo <content-handler-url>. |
181 std::string shebang; | 163 std::string shebang; |
182 if (fetcher->PeekContentHandler(&shebang, &content_handler_url)) { | 164 if (fetcher->PeekContentHandler(&shebang, &content_handler_url)) { |
183 *response = fetcher->AsURLResponse(task_runner_, | 165 *response = fetcher->AsURLResponse(task_runner_, |
184 static_cast<int>(shebang.size())); | 166 static_cast<int>(shebang.size())); |
185 *content_handler_identity = shell::Identity( | 167 *content_handler_identity = Identity( |
186 content_handler_url, | 168 content_handler_url, |
187 use_real_qualifier ? (*response)->site.To<std::string>() | 169 use_real_qualifier ? (*response)->site.To<std::string>() |
188 : std::string(), | 170 : std::string(), |
189 target_filter); | 171 target_filter); |
190 return true; | 172 return true; |
191 } | 173 } |
192 | 174 |
193 // The response MIME type matches a registered content handler. | 175 // The response MIME type matches a registered content handler. |
194 auto iter = mime_type_to_url_.find(fetcher->MimeType()); | 176 auto iter = mime_type_to_url_.find(fetcher->MimeType()); |
195 if (iter != mime_type_to_url_.end()) { | 177 if (iter != mime_type_to_url_.end()) { |
196 *response = fetcher->AsURLResponse(task_runner_, 0); | 178 *response = fetcher->AsURLResponse(task_runner_, 0); |
197 *content_handler_identity = shell::Identity( | 179 *content_handler_identity = Identity( |
198 iter->second, | 180 iter->second, |
199 use_real_qualifier ? (*response)->site.To<std::string>() | 181 use_real_qualifier ? (*response)->site.To<std::string>() |
200 : std::string(), | 182 : std::string(), |
201 target_filter); | 183 target_filter); |
202 return true; | 184 return true; |
203 } | 185 } |
204 | 186 |
205 // The response URL matches a registered content handler. | 187 // The response URL matches a registered content handler. |
206 auto alias_iter = application_package_alias_.find(target_url); | 188 auto alias_iter = application_package_alias_.find(target_url); |
207 if (alias_iter != application_package_alias_.end()) { | 189 if (alias_iter != application_package_alias_.end()) { |
208 // We replace the qualifier with the one our package alias requested. | 190 // We replace the qualifier with the one our package alias requested. |
209 *response = URLResponse::New(); | 191 *response = URLResponse::New(); |
210 (*response)->url = target_url.spec(); | 192 (*response)->url = target_url.spec(); |
211 *content_handler_identity = shell::Identity( | 193 *content_handler_identity = Identity( |
212 alias_iter->second.first, | 194 alias_iter->second.first, |
213 use_real_qualifier ? alias_iter->second.second : std::string(), | 195 use_real_qualifier ? alias_iter->second.second : std::string(), |
214 target_filter); | 196 target_filter); |
215 return true; | 197 return true; |
216 } | 198 } |
217 | 199 |
218 return false; | 200 return false; |
219 } | 201 } |
220 | 202 |
221 ContentHandlerConnection* PackageManagerImpl::GetContentHandler( | 203 ContentHandlerConnection* PackageManagerImpl::GetContentHandler( |
222 const shell::Identity& content_handler_identity, | 204 const Identity& content_handler_identity, |
223 const shell::Identity& source_identity) { | 205 const Identity& source_identity) { |
224 auto it = identity_to_content_handler_.find(content_handler_identity); | 206 auto it = identity_to_content_handler_.find(content_handler_identity); |
225 if (it != identity_to_content_handler_.end()) | 207 if (it != identity_to_content_handler_.end()) |
226 return it->second; | 208 return it->second; |
227 | 209 |
228 ContentHandlerConnection* connection = new ContentHandlerConnection( | 210 ContentHandlerConnection* connection = new ContentHandlerConnection( |
229 application_manager_, source_identity, | 211 application_manager_, source_identity, |
230 content_handler_identity, | 212 content_handler_identity, |
231 ++content_handler_id_counter_, | 213 ++content_handler_id_counter_, |
232 base::Bind(&PackageManagerImpl::OnContentHandlerConnectionClosed, | 214 base::Bind(&PackageManagerImpl::OnContentHandlerConnectionClosed, |
233 base::Unretained(this))); | 215 base::Unretained(this))); |
234 identity_to_content_handler_[content_handler_identity] = connection; | 216 identity_to_content_handler_[content_handler_identity] = connection; |
235 return connection; | 217 return connection; |
236 } | 218 } |
237 | 219 |
238 void PackageManagerImpl::OnContentHandlerConnectionClosed( | 220 void PackageManagerImpl::OnContentHandlerConnectionClosed( |
239 ContentHandlerConnection* connection) { | 221 ContentHandlerConnection* connection) { |
240 // Remove the mapping. | 222 // Remove the mapping. |
241 auto it = identity_to_content_handler_.find(connection->identity()); | 223 auto it = identity_to_content_handler_.find(connection->identity()); |
242 DCHECK(it != identity_to_content_handler_.end()); | 224 DCHECK(it != identity_to_content_handler_.end()); |
243 identity_to_content_handler_.erase(it); | 225 identity_to_content_handler_.erase(it); |
244 } | 226 } |
245 | 227 |
246 } // namespace package_manager | 228 } // namespace shell |
247 } // namespace mojo | 229 } // namespace mojo |
OLD | NEW |