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/dynamic_application_loader.h" | 5 #include "shell/dynamic_application_loader.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/files/file_path.h" | 9 #include "base/files/file_path.h" |
10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
11 #include "base/format_macros.h" | 11 #include "base/format_macros.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
15 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
16 #include "base/process/process.h" | |
16 #include "base/strings/string_util.h" | 17 #include "base/strings/string_util.h" |
17 #include "base/strings/stringprintf.h" | 18 #include "base/strings/stringprintf.h" |
18 #include "base/strings/utf_string_conversions.h" | 19 #include "base/strings/utf_string_conversions.h" |
19 #include "mojo/common/common_type_converters.h" | 20 #include "mojo/common/common_type_converters.h" |
20 #include "mojo/common/data_pipe_utils.h" | 21 #include "mojo/common/data_pipe_utils.h" |
21 #include "mojo/public/cpp/system/data_pipe.h" | 22 #include "mojo/public/cpp/system/data_pipe.h" |
22 #include "mojo/services/network/public/interfaces/url_loader.mojom.h" | 23 #include "mojo/services/network/public/interfaces/url_loader.mojom.h" |
23 #include "shell/context.h" | 24 #include "shell/context.h" |
24 #include "shell/data_pipe_peek.h" | 25 #include "shell/data_pipe_peek.h" |
25 #include "shell/filename_util.h" | 26 #include "shell/filename_util.h" |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
102 | 103 |
103 // TODO(aa): Sanity check that the thing we got looks vaguely like a mojo | 104 // TODO(aa): Sanity check that the thing we got looks vaguely like a mojo |
104 // application. That could either mean looking for the platform-specific dll | 105 // application. That could either mean looking for the platform-specific dll |
105 // header, or looking for some specific mojo signature prepended to the | 106 // header, or looking for some specific mojo signature prepended to the |
106 // library. | 107 // library. |
107 | 108 |
108 AsPath(context_->task_runners()->blocking_pool(), | 109 AsPath(context_->task_runners()->blocking_pool(), |
109 base::Bind(&Loader::RunLibrary, weak_ptr_factory_.GetWeakPtr())); | 110 base::Bind(&Loader::RunLibrary, weak_ptr_factory_.GetWeakPtr())); |
110 } | 111 } |
111 | 112 |
112 void ReportComplete() { loader_complete_callback_.Run(this); } | 113 virtual void ReportComplete() { loader_complete_callback_.Run(this); } |
113 | 114 |
114 private: | 115 private: |
115 bool PeekContentHandler(std::string* mojo_shebang, | 116 bool PeekContentHandler(std::string* mojo_shebang, |
116 GURL* mojo_content_handler_url) { | 117 GURL* mojo_content_handler_url) { |
117 std::string shebang; | 118 std::string shebang; |
118 if (HasMojoMagic() && PeekFirstLine(&shebang)) { | 119 if (HasMojoMagic() && PeekFirstLine(&shebang)) { |
119 GURL url(shebang.substr(arraysize(kMojoMagic) - 1, std::string::npos)); | 120 GURL url(shebang.substr(arraysize(kMojoMagic) - 1, std::string::npos)); |
120 if (url.is_valid()) { | 121 if (url.is_valid()) { |
121 *mojo_shebang = shebang; | 122 *mojo_shebang = shebang; |
122 *mojo_content_handler_url = url; | 123 *mojo_content_handler_url = url; |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
282 } | 283 } |
283 | 284 |
284 void AsPath( | 285 void AsPath( |
285 base::TaskRunner* task_runner, | 286 base::TaskRunner* task_runner, |
286 base::Callback<void(const base::FilePath&, bool)> callback) override { | 287 base::Callback<void(const base::FilePath&, bool)> callback) override { |
287 if (!path_.empty() || !response_) { | 288 if (!path_.empty() || !response_) { |
288 base::MessageLoop::current()->PostTask( | 289 base::MessageLoop::current()->PostTask( |
289 FROM_HERE, base::Bind(callback, path_, base::PathExists(path_))); | 290 FROM_HERE, base::Bind(callback, path_, base::PathExists(path_))); |
290 return; | 291 return; |
291 } | 292 } |
293 // We don't use the created file, just want the directory and random name. | |
292 base::CreateTemporaryFile(&path_); | 294 base::CreateTemporaryFile(&path_); |
293 // This is used to extract symbols on android. | 295 base::DeleteFile(path_, false); |
vtl
2015/01/21 23:16:21
This makes me sad, but oh well.
eseidel
2015/01/21 23:23:30
Acknowledged.
| |
294 LOG(INFO) << "Caching mojo app " << url_ << " at " << path_.value(); | 296 path_ = path_.AddExtension(".mojo"); // Make libraries easy to spot. |
vtl
2015/01/21 23:16:21
nit: Usually we put two spaces before the "//" for
eseidel
2015/01/21 23:23:30
Done.
| |
295 common::CopyToFile(response_->body.Pass(), path_, task_runner, | 297 common::CopyToFile(response_->body.Pass(), path_, task_runner, |
296 base::Bind(callback, path_)); | 298 base::Bind(callback, path_)); |
299 | |
300 // This is used to extract symbols on android. | |
301 // TODO(eseidel): All users of this log should move to using the map file. | |
302 LOG(INFO) << "Caching mojo app " << url_ << " at " << path_.value(); | |
303 | |
304 // Record the URL -> temp-file mapping for debugging tools. | |
vtl
2015/01/21 23:16:21
Could you factor this out into a separate function
eseidel
2015/01/21 23:23:30
Done.
| |
305 base::FilePath temp_dir; | |
306 GetTempDir(&temp_dir); | |
vtl
2015/01/21 23:16:21
You should probably say base::GetTempDir, rather t
eseidel
2015/01/21 23:23:30
Done.
| |
307 base::ProcessId pid = base::Process::Current().pid(); | |
308 std::string map_name = base::StringPrintf("mojo_shell.%d.maps", pid); | |
309 base::FilePath map_path = temp_dir.Append(map_name); | |
310 | |
311 std::string map_entry = base::StringPrintf("%s %s\n", | |
vtl
2015/01/21 23:16:21
Probably we should consider quoting/escaping the p
eseidel
2015/01/21 23:23:30
Acknowledged.
| |
312 path_.value().data(), url_.spec().data()); | |
313 // TODO(eseidel): AppendToFile is missing O_CREAT, crbug.com/450696 | |
314 if (!PathExists(map_path)) | |
315 base::WriteFile(map_path, map_entry.data(), map_entry.length()); | |
316 else | |
317 base::AppendToFile(map_path, map_entry.data(), map_entry.length()); | |
297 } | 318 } |
298 | 319 |
299 std::string MimeType() override { | 320 std::string MimeType() override { |
300 DCHECK(response_); | 321 DCHECK(response_); |
301 return response_->mime_type; | 322 return response_->mime_type; |
302 } | 323 } |
303 | 324 |
304 bool HasMojoMagic() override { | 325 bool HasMojoMagic() override { |
305 std::string magic; | 326 std::string magic; |
306 return BlockingPeekNBytes(response_->body.get(), &magic, strlen(kMojoMagic), | 327 return BlockingPeekNBytes(response_->body.get(), &magic, strlen(kMojoMagic), |
(...skipping 27 matching lines...) Expand all Loading... | |
334 LOG(ERROR) << "Error (" << response->error->code << ": " | 355 LOG(ERROR) << "Error (" << response->error->code << ": " |
335 << response->error->description << ") while fetching " | 356 << response->error->description << ") while fetching " |
336 << response->url; | 357 << response->url; |
337 ReportComplete(); | 358 ReportComplete(); |
338 return; | 359 return; |
339 } | 360 } |
340 response_ = response.Pass(); | 361 response_ = response.Pass(); |
341 Load(); | 362 Load(); |
342 } | 363 } |
343 | 364 |
365 void ReportComplete() override { | |
366 Loader::ReportComplete(); | |
367 // As soon as we've loaded the library we can delete the tmp file. | |
vtl
2015/01/21 23:16:21
"tmp" -> "temp"?
eseidel
2015/01/21 23:23:30
Done.
| |
368 // Tools can read the mojo_shell.PID.maps file to find the original library. | |
369 if (!path_.empty()) | |
370 DeleteFile(path_, false); | |
371 } | |
372 | |
vtl
2015/01/21 23:16:21
nit: One fewer blank line.
eseidel
2015/01/21 23:23:30
Done.
| |
373 | |
344 const GURL url_; | 374 const GURL url_; |
345 URLLoaderPtr url_loader_; | 375 URLLoaderPtr url_loader_; |
346 URLResponsePtr response_; | 376 URLResponsePtr response_; |
347 base::FilePath path_; | 377 base::FilePath path_; |
348 base::WeakPtrFactory<NetworkLoader> weak_ptr_factory_; | 378 base::WeakPtrFactory<NetworkLoader> weak_ptr_factory_; |
349 | 379 |
350 DISALLOW_COPY_AND_ASSIGN(NetworkLoader); | 380 DISALLOW_COPY_AND_ASSIGN(NetworkLoader); |
351 }; | 381 }; |
352 | 382 |
353 DynamicApplicationLoader::DynamicApplicationLoader( | 383 DynamicApplicationLoader::DynamicApplicationLoader( |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
401 // TODO(darin): What should we do about service errors? This implies that | 431 // TODO(darin): What should we do about service errors? This implies that |
402 // the app closed its handle to the service manager. Maybe we don't care? | 432 // the app closed its handle to the service manager. Maybe we don't care? |
403 } | 433 } |
404 | 434 |
405 void DynamicApplicationLoader::LoaderComplete(Loader* loader) { | 435 void DynamicApplicationLoader::LoaderComplete(Loader* loader) { |
406 loaders_.erase(std::find(loaders_.begin(), loaders_.end(), loader)); | 436 loaders_.erase(std::find(loaders_.begin(), loaders_.end(), loader)); |
407 } | 437 } |
408 | 438 |
409 } // namespace shell | 439 } // namespace shell |
410 } // namespace mojo | 440 } // namespace mojo |
OLD | NEW |