OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "shell/dynamic_application_loader.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/command_line.h" | |
9 #include "base/files/file.h" | |
10 #include "base/files/file_path.h" | |
11 #include "base/files/file_util.h" | |
12 #include "base/format_macros.h" | |
13 #include "base/logging.h" | |
14 #include "base/memory/scoped_ptr.h" | |
15 #include "base/memory/weak_ptr.h" | |
16 #include "base/message_loop/message_loop.h" | |
17 #include "base/process/process.h" | |
18 #include "base/strings/string_number_conversions.h" | |
19 #include "base/strings/string_util.h" | |
20 #include "base/strings/stringprintf.h" | |
21 #include "base/strings/utf_string_conversions.h" | |
22 #include "crypto/secure_hash.h" | |
23 #include "crypto/sha2.h" | |
24 #include "mojo/common/common_type_converters.h" | |
25 #include "mojo/common/data_pipe_utils.h" | |
26 #include "mojo/public/cpp/system/data_pipe.h" | |
27 #include "mojo/services/network/public/interfaces/url_loader.mojom.h" | |
28 #include "shell/context.h" | |
29 #include "shell/data_pipe_peek.h" | |
30 #include "shell/filename_util.h" | |
31 #include "shell/switches.h" | |
32 #include "url/url_util.h" | |
33 | |
34 namespace mojo { | |
35 namespace shell { | |
36 | |
37 namespace { | |
38 | |
39 static const char kMojoMagic[] = "#!mojo "; | |
40 static const size_t kMaxShebangLength = 2048; | |
41 | |
42 void IgnoreResult(bool result) { | |
43 } | |
44 | |
45 } // namespace | |
46 | |
47 // Encapsulates loading and running one individual application. | |
48 // | |
49 // Loaders are owned by DynamicApplicationLoader. DynamicApplicationLoader must | |
50 // ensure that all the parameters passed to Loader subclasses stay valid through | |
51 // Loader's lifetime. | |
52 // | |
53 // Async operations are done with WeakPtr to protect against | |
54 // DynamicApplicationLoader going away (and taking all the Loaders with it) | |
55 // while the async operation is outstanding. | |
56 class DynamicApplicationLoader::Loader { | |
57 public: | |
58 Loader(DynamicServiceRunner::CleanupBehavior cleanup_behavior, | |
59 MimeTypeToURLMap* mime_type_to_url, | |
60 Context* context, | |
61 DynamicServiceRunnerFactory* runner_factory, | |
62 InterfaceRequest<Application> application_request, | |
63 ApplicationLoader::LoadCallback load_callback, | |
64 const LoaderCompleteCallback& loader_complete_callback) | |
65 : cleanup_behavior_(cleanup_behavior), | |
66 application_request_(application_request.Pass()), | |
67 load_callback_(load_callback), | |
68 loader_complete_callback_(loader_complete_callback), | |
69 context_(context), | |
70 mime_type_to_url_(mime_type_to_url), | |
71 runner_factory_(runner_factory), | |
72 weak_ptr_factory_(this) {} | |
73 | |
74 virtual ~Loader() {} | |
75 | |
76 protected: | |
77 virtual URLResponsePtr AsURLResponse(base::TaskRunner* task_runner, | |
78 uint32_t skip) = 0; | |
79 | |
80 virtual void AsPath( | |
81 base::TaskRunner* task_runner, | |
82 base::Callback<void(const base::FilePath&, bool)> callback) = 0; | |
83 | |
84 virtual std::string MimeType() = 0; | |
85 | |
86 virtual bool HasMojoMagic() = 0; | |
87 | |
88 virtual bool PeekFirstLine(std::string* line) = 0; | |
89 | |
90 void Load() { | |
91 // If the response begins with a #!mojo <content-handler-url>, use it. | |
92 GURL url; | |
93 std::string shebang; | |
94 if (PeekContentHandler(&shebang, &url)) { | |
95 load_callback_.Run( | |
96 url, application_request_.Pass(), | |
97 AsURLResponse(context_->task_runners()->blocking_pool(), | |
98 static_cast<int>(shebang.size()))); | |
99 return; | |
100 } | |
101 | |
102 MimeTypeToURLMap::iterator iter = mime_type_to_url_->find(MimeType()); | |
103 if (iter != mime_type_to_url_->end()) { | |
104 load_callback_.Run( | |
105 iter->second, application_request_.Pass(), | |
106 AsURLResponse(context_->task_runners()->blocking_pool(), 0)); | |
107 return; | |
108 } | |
109 | |
110 // TODO(aa): Sanity check that the thing we got looks vaguely like a mojo | |
111 // application. That could either mean looking for the platform-specific dll | |
112 // header, or looking for some specific mojo signature prepended to the | |
113 // library. | |
114 | |
115 AsPath(context_->task_runners()->blocking_pool(), | |
116 base::Bind(&Loader::RunLibrary, weak_ptr_factory_.GetWeakPtr())); | |
117 } | |
118 | |
119 void ReportComplete() { loader_complete_callback_.Run(this); } | |
120 | |
121 private: | |
122 bool PeekContentHandler(std::string* mojo_shebang, | |
123 GURL* mojo_content_handler_url) { | |
124 std::string shebang; | |
125 if (HasMojoMagic() && PeekFirstLine(&shebang)) { | |
126 GURL url(shebang.substr(arraysize(kMojoMagic) - 1, std::string::npos)); | |
127 if (url.is_valid()) { | |
128 *mojo_shebang = shebang; | |
129 *mojo_content_handler_url = url; | |
130 return true; | |
131 } | |
132 } | |
133 return false; | |
134 } | |
135 | |
136 void RunLibrary(const base::FilePath& path, bool path_exists) { | |
137 DCHECK(application_request_.is_pending()); | |
138 | |
139 if (!path_exists) { | |
140 LOG(ERROR) << "Library not started because library path '" << path.value() | |
141 << "' does not exist."; | |
142 ReportComplete(); | |
143 return; | |
144 } | |
145 | |
146 runner_ = runner_factory_->Create(context_); | |
147 runner_->Start( | |
148 path, cleanup_behavior_, application_request_.Pass(), | |
149 base::Bind(&Loader::ReportComplete, weak_ptr_factory_.GetWeakPtr())); | |
150 } | |
151 | |
152 DynamicServiceRunner::CleanupBehavior cleanup_behavior_; | |
153 InterfaceRequest<Application> application_request_; | |
154 ApplicationLoader::LoadCallback load_callback_; | |
155 LoaderCompleteCallback loader_complete_callback_; | |
156 Context* context_; | |
157 MimeTypeToURLMap* mime_type_to_url_; | |
158 DynamicServiceRunnerFactory* runner_factory_; | |
159 scoped_ptr<DynamicServiceRunner> runner_; | |
160 base::WeakPtrFactory<Loader> weak_ptr_factory_; | |
161 }; | |
162 | |
163 // A loader for local files. | |
164 class DynamicApplicationLoader::LocalLoader : public Loader { | |
165 public: | |
166 LocalLoader(const GURL& url, | |
167 MimeTypeToURLMap* mime_type_to_url, | |
168 Context* context, | |
169 DynamicServiceRunnerFactory* runner_factory, | |
170 InterfaceRequest<Application> application_request, | |
171 ApplicationLoader::LoadCallback load_callback, | |
172 const LoaderCompleteCallback& loader_complete_callback) | |
173 : Loader(DynamicServiceRunner::DontDeleteAppPath, | |
174 mime_type_to_url, | |
175 context, | |
176 runner_factory, | |
177 application_request.Pass(), | |
178 load_callback, | |
179 loader_complete_callback), | |
180 url_(url), | |
181 path_(UrlToFile(url)) { | |
182 Load(); | |
183 } | |
184 | |
185 private: | |
186 static base::FilePath UrlToFile(const GURL& url) { | |
187 DCHECK(url.SchemeIsFile()); | |
188 url::RawCanonOutputW<1024> output; | |
189 url::DecodeURLEscapeSequences( | |
190 url.path().data(), static_cast<int>(url.path().length()), &output); | |
191 base::string16 decoded_path = | |
192 base::string16(output.data(), output.length()); | |
193 #if defined(OS_WIN) | |
194 base::TrimString(decoded_path, L"/", &decoded_path); | |
195 base::FilePath path(decoded_path); | |
196 #else | |
197 base::FilePath path(base::UTF16ToUTF8(decoded_path)); | |
198 #endif | |
199 return path; | |
200 } | |
201 | |
202 URLResponsePtr AsURLResponse(base::TaskRunner* task_runner, | |
203 uint32_t skip) override { | |
204 URLResponsePtr response(URLResponse::New()); | |
205 response->url = String::From(url_); | |
206 DataPipe data_pipe; | |
207 response->body = data_pipe.consumer_handle.Pass(); | |
208 int64 file_size; | |
209 if (base::GetFileSize(path_, &file_size)) { | |
210 response->headers = Array<String>(1); | |
211 response->headers[0] = | |
212 base::StringPrintf("Content-Length: %" PRId64, file_size); | |
213 } | |
214 common::CopyFromFile(path_, data_pipe.producer_handle.Pass(), skip, | |
215 task_runner, base::Bind(&IgnoreResult)); | |
216 return response.Pass(); | |
217 } | |
218 | |
219 void AsPath( | |
220 base::TaskRunner* task_runner, | |
221 base::Callback<void(const base::FilePath&, bool)> callback) override { | |
222 // Async for consistency with network case. | |
223 base::MessageLoop::current()->PostTask( | |
224 FROM_HERE, base::Bind(callback, path_, base::PathExists(path_))); | |
225 } | |
226 | |
227 std::string MimeType() override { return ""; } | |
228 | |
229 bool HasMojoMagic() override { | |
230 std::string magic; | |
231 ReadFileToString(path_, &magic, strlen(kMojoMagic)); | |
232 return magic == kMojoMagic; | |
233 } | |
234 | |
235 bool PeekFirstLine(std::string* line) override { | |
236 std::string start_of_file; | |
237 ReadFileToString(path_, &start_of_file, kMaxShebangLength); | |
238 size_t return_position = start_of_file.find('\n'); | |
239 if (return_position == std::string::npos) | |
240 return false; | |
241 *line = start_of_file.substr(0, return_position + 1); | |
242 return true; | |
243 } | |
244 | |
245 GURL url_; | |
246 base::FilePath path_; | |
247 | |
248 DISALLOW_COPY_AND_ASSIGN(LocalLoader); | |
249 }; | |
250 | |
251 // A loader for network files. | |
252 class DynamicApplicationLoader::NetworkLoader : public Loader { | |
253 public: | |
254 NetworkLoader(const GURL& url, | |
255 NetworkService* network_service, | |
256 MimeTypeToURLMap* mime_type_to_url, | |
257 Context* context, | |
258 DynamicServiceRunnerFactory* runner_factory, | |
259 InterfaceRequest<Application> application_request, | |
260 ApplicationLoader::LoadCallback load_callback, | |
261 const LoaderCompleteCallback& loader_complete_callback) | |
262 : Loader(DynamicServiceRunner::DeleteAppPath, | |
263 mime_type_to_url, | |
264 context, | |
265 runner_factory, | |
266 application_request.Pass(), | |
267 load_callback, | |
268 loader_complete_callback), | |
269 url_(url), | |
270 weak_ptr_factory_(this) { | |
271 StartNetworkRequest(url, network_service); | |
272 } | |
273 | |
274 ~NetworkLoader() override { | |
275 if (!path_.empty()) | |
276 base::DeleteFile(path_, false); | |
277 } | |
278 | |
279 private: | |
280 // TODO(hansmuller): Revisit this when a real peek operation is available. | |
281 static const MojoDeadline kPeekTimeout = MOJO_DEADLINE_INDEFINITE; | |
282 | |
283 URLResponsePtr AsURLResponse(base::TaskRunner* task_runner, | |
284 uint32_t skip) override { | |
285 if (skip != 0) { | |
286 MojoResult result = ReadDataRaw( | |
287 response_->body.get(), nullptr, &skip, | |
288 MOJO_READ_DATA_FLAG_ALL_OR_NONE | MOJO_READ_DATA_FLAG_DISCARD); | |
289 DCHECK_EQ(result, MOJO_RESULT_OK); | |
290 } | |
291 return response_.Pass(); | |
292 } | |
293 | |
294 static void RecordCacheToURLMapping(const base::FilePath& path, | |
295 const GURL& url) { | |
296 // This is used to extract symbols on android. | |
297 // TODO(eseidel): All users of this log should move to using the map file. | |
298 LOG(INFO) << "Caching mojo app " << url << " at " << path.value(); | |
299 | |
300 base::FilePath temp_dir; | |
301 base::GetTempDir(&temp_dir); | |
302 base::ProcessId pid = base::Process::Current().Pid(); | |
303 std::string map_name = base::StringPrintf("mojo_shell.%d.maps", pid); | |
304 base::FilePath map_path = temp_dir.Append(map_name); | |
305 | |
306 // TODO(eseidel): Paths or URLs with spaces will need quoting. | |
307 std::string map_entry = | |
308 base::StringPrintf("%s %s\n", path.value().c_str(), url.spec().c_str()); | |
309 // TODO(eseidel): AppendToFile is missing O_CREAT, crbug.com/450696 | |
310 if (!PathExists(map_path)) | |
311 base::WriteFile(map_path, map_entry.data(), map_entry.length()); | |
312 else | |
313 base::AppendToFile(map_path, map_entry.data(), map_entry.length()); | |
314 } | |
315 | |
316 // AppIds should be be both predictable and unique, but any hash would work. | |
317 // Currently we use sha256 from crypto/secure_hash.h | |
318 static bool ComputeAppId(const base::FilePath& path, | |
319 std::string* digest_string) { | |
320 scoped_ptr<crypto::SecureHash> ctx( | |
321 crypto::SecureHash::Create(crypto::SecureHash::SHA256)); | |
322 base::File file(path, base::File::FLAG_OPEN | base::File::FLAG_READ); | |
323 if (!file.IsValid()) { | |
324 LOG(ERROR) << "Failed to open " << path.value() << " for computing AppId"; | |
325 return false; | |
326 } | |
327 char buf[1024]; | |
328 while (file.IsValid()) { | |
329 int bytes_read = file.ReadAtCurrentPos(buf, sizeof(buf)); | |
330 if (bytes_read == 0) | |
331 break; | |
332 ctx->Update(buf, bytes_read); | |
333 } | |
334 if (!file.IsValid()) { | |
335 LOG(ERROR) << "Error reading " << path.value(); | |
336 return false; | |
337 } | |
338 // The output is really a vector of unit8, we're cheating by using a string. | |
339 std::string output(crypto::kSHA256Length, 0); | |
340 ctx->Finish(string_as_array(&output), output.size()); | |
341 output = base::HexEncode(output.c_str(), output.size()); | |
342 // Using lowercase for compatiblity with sha256sum output. | |
343 *digest_string = base::StringToLowerASCII(output); | |
344 return true; | |
345 } | |
346 | |
347 static bool RenameToAppId(const base::FilePath& old_path, | |
348 base::FilePath* new_path) { | |
349 std::string app_id; | |
350 if (!ComputeAppId(old_path, &app_id)) | |
351 return false; | |
352 | |
353 base::FilePath temp_dir; | |
354 base::GetTempDir(&temp_dir); | |
355 std::string unique_name = base::StringPrintf("%s.mojo", app_id.c_str()); | |
356 *new_path = temp_dir.Append(unique_name); | |
357 return base::Move(old_path, *new_path); | |
358 } | |
359 | |
360 void CopyCompleted(base::Callback<void(const base::FilePath&, bool)> callback, | |
361 bool success) { | |
362 // The copy completed, now move to $TMP/$APP_ID.mojo before the dlopen. | |
363 if (success) { | |
364 success = false; | |
365 base::FilePath new_path; | |
366 if (RenameToAppId(path_, &new_path)) { | |
367 if (base::PathExists(new_path)) { | |
368 path_ = new_path; | |
369 success = true; | |
370 RecordCacheToURLMapping(path_, url_); | |
371 } | |
372 } | |
373 } | |
374 | |
375 base::MessageLoop::current()->PostTask( | |
376 FROM_HERE, base::Bind(callback, path_, success)); | |
377 } | |
378 | |
379 void AsPath( | |
380 base::TaskRunner* task_runner, | |
381 base::Callback<void(const base::FilePath&, bool)> callback) override { | |
382 if (!path_.empty() || !response_) { | |
383 base::MessageLoop::current()->PostTask( | |
384 FROM_HERE, base::Bind(callback, path_, base::PathExists(path_))); | |
385 return; | |
386 } | |
387 | |
388 base::CreateTemporaryFile(&path_); | |
389 common::CopyToFile(response_->body.Pass(), path_, task_runner, | |
390 base::Bind(&NetworkLoader::CopyCompleted, | |
391 weak_ptr_factory_.GetWeakPtr(), callback)); | |
392 } | |
393 | |
394 std::string MimeType() override { | |
395 DCHECK(response_); | |
396 return response_->mime_type; | |
397 } | |
398 | |
399 bool HasMojoMagic() override { | |
400 std::string magic; | |
401 return BlockingPeekNBytes(response_->body.get(), &magic, strlen(kMojoMagic), | |
402 kPeekTimeout) && | |
403 magic == kMojoMagic; | |
404 } | |
405 | |
406 bool PeekFirstLine(std::string* line) override { | |
407 return BlockingPeekLine(response_->body.get(), line, kMaxShebangLength, | |
408 kPeekTimeout); | |
409 } | |
410 | |
411 void StartNetworkRequest(const GURL& url, NetworkService* network_service) { | |
412 URLRequestPtr request(URLRequest::New()); | |
413 request->url = String::From(url); | |
414 request->auto_follow_redirects = true; | |
415 | |
416 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | |
417 switches::kDisableCache)) { | |
418 request->bypass_cache = true; | |
419 } | |
420 | |
421 network_service->CreateURLLoader(GetProxy(&url_loader_)); | |
422 url_loader_->Start(request.Pass(), | |
423 base::Bind(&NetworkLoader::OnLoadComplete, | |
424 weak_ptr_factory_.GetWeakPtr())); | |
425 } | |
426 | |
427 void OnLoadComplete(URLResponsePtr response) { | |
428 if (response->error) { | |
429 LOG(ERROR) << "Error (" << response->error->code << ": " | |
430 << response->error->description << ") while fetching " | |
431 << response->url; | |
432 ReportComplete(); | |
433 return; | |
434 } | |
435 response_ = response.Pass(); | |
436 Load(); | |
437 } | |
438 | |
439 const GURL url_; | |
440 URLLoaderPtr url_loader_; | |
441 URLResponsePtr response_; | |
442 base::FilePath path_; | |
443 base::WeakPtrFactory<NetworkLoader> weak_ptr_factory_; | |
444 | |
445 DISALLOW_COPY_AND_ASSIGN(NetworkLoader); | |
446 }; | |
447 | |
448 DynamicApplicationLoader::DynamicApplicationLoader( | |
449 Context* context, | |
450 scoped_ptr<DynamicServiceRunnerFactory> runner_factory) | |
451 : context_(context), | |
452 runner_factory_(runner_factory.Pass()), | |
453 | |
454 // Unretained() is correct here because DynamicApplicationLoader owns the | |
455 // loaders that we pass this callback to. | |
456 loader_complete_callback_( | |
457 base::Bind(&DynamicApplicationLoader::LoaderComplete, | |
458 base::Unretained(this))) { | |
459 } | |
460 | |
461 DynamicApplicationLoader::~DynamicApplicationLoader() { | |
462 } | |
463 | |
464 void DynamicApplicationLoader::RegisterContentHandler( | |
465 const std::string& mime_type, | |
466 const GURL& content_handler_url) { | |
467 DCHECK(content_handler_url.is_valid()) | |
468 << "Content handler URL is invalid for mime type " << mime_type; | |
469 mime_type_to_url_[mime_type] = content_handler_url; | |
470 } | |
471 | |
472 void DynamicApplicationLoader::Load( | |
473 ApplicationManager* manager, | |
474 const GURL& url, | |
475 InterfaceRequest<Application> application_request, | |
476 LoadCallback load_callback) { | |
477 if (url.SchemeIsFile()) { | |
478 loaders_.push_back(new LocalLoader( | |
479 url, &mime_type_to_url_, context_, runner_factory_.get(), | |
480 application_request.Pass(), load_callback, loader_complete_callback_)); | |
481 return; | |
482 } | |
483 | |
484 if (!network_service_) { | |
485 context_->application_manager()->ConnectToService( | |
486 GURL("mojo:network_service"), &network_service_); | |
487 } | |
488 | |
489 loaders_.push_back(new NetworkLoader( | |
490 url, network_service_.get(), &mime_type_to_url_, context_, | |
491 runner_factory_.get(), application_request.Pass(), load_callback, | |
492 loader_complete_callback_)); | |
493 } | |
494 | |
495 void DynamicApplicationLoader::OnApplicationError(ApplicationManager* manager, | |
496 const GURL& url) { | |
497 // TODO(darin): What should we do about service errors? This implies that | |
498 // the app closed its handle to the service manager. Maybe we don't care? | |
499 } | |
500 | |
501 void DynamicApplicationLoader::LoaderComplete(Loader* loader) { | |
502 loaders_.erase(std::find(loaders_.begin(), loaders_.end(), loader)); | |
503 } | |
504 | |
505 } // namespace shell | |
506 } // namespace mojo | |
OLD | NEW |