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 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
274 uint32_t skip) override { | 275 uint32_t skip) override { |
275 if (skip != 0) { | 276 if (skip != 0) { |
276 MojoResult result = ReadDataRaw( | 277 MojoResult result = ReadDataRaw( |
277 response_->body.get(), nullptr, &skip, | 278 response_->body.get(), nullptr, &skip, |
278 MOJO_READ_DATA_FLAG_ALL_OR_NONE | MOJO_READ_DATA_FLAG_DISCARD); | 279 MOJO_READ_DATA_FLAG_ALL_OR_NONE | MOJO_READ_DATA_FLAG_DISCARD); |
279 DCHECK_EQ(result, MOJO_RESULT_OK); | 280 DCHECK_EQ(result, MOJO_RESULT_OK); |
280 } | 281 } |
281 return response_.Pass(); | 282 return response_.Pass(); |
282 } | 283 } |
283 | 284 |
285 static | |
viettrungluu
2015/01/21 23:32:50
nit: Probably the "canonical" way to format this w
| |
286 void RecordCacheToURLMapping(const base::FilePath& path, const GURL& url) { | |
287 // This is used to extract symbols on android. | |
288 // TODO(eseidel): All users of this log should move to using the map file. | |
289 LOG(INFO) << "Caching mojo app " << url << " at " << path.value(); | |
290 | |
291 base::FilePath temp_dir; | |
292 base::GetTempDir(&temp_dir); | |
293 base::ProcessId pid = base::Process::Current().pid(); | |
294 std::string map_name = base::StringPrintf("mojo_shell.%d.maps", pid); | |
295 base::FilePath map_path = temp_dir.Append(map_name); | |
296 | |
297 // TODO(eseidel): Paths or URLs with spaces will need quoting. | |
298 std::string map_entry = base::StringPrintf("%s %s\n", | |
299 path.value().data(), url.spec().data()); | |
300 // TODO(eseidel): AppendToFile is missing O_CREAT, crbug.com/450696 | |
301 if (!PathExists(map_path)) | |
302 base::WriteFile(map_path, map_entry.data(), map_entry.length()); | |
303 else | |
304 base::AppendToFile(map_path, map_entry.data(), map_entry.length()); | |
305 } | |
306 | |
284 void AsPath( | 307 void AsPath( |
285 base::TaskRunner* task_runner, | 308 base::TaskRunner* task_runner, |
286 base::Callback<void(const base::FilePath&, bool)> callback) override { | 309 base::Callback<void(const base::FilePath&, bool)> callback) override { |
287 if (!path_.empty() || !response_) { | 310 if (!path_.empty() || !response_) { |
288 base::MessageLoop::current()->PostTask( | 311 base::MessageLoop::current()->PostTask( |
289 FROM_HERE, base::Bind(callback, path_, base::PathExists(path_))); | 312 FROM_HERE, base::Bind(callback, path_, base::PathExists(path_))); |
290 return; | 313 return; |
291 } | 314 } |
315 // We don't use the created file, just want the directory and random name. | |
292 base::CreateTemporaryFile(&path_); | 316 base::CreateTemporaryFile(&path_); |
293 // This is used to extract symbols on android. | 317 base::DeleteFile(path_, false); |
294 LOG(INFO) << "Caching mojo app " << url_ << " at " << path_.value(); | 318 path_ = path_.AddExtension(".mojo"); // Make libraries easy to spot. |
295 common::CopyToFile(response_->body.Pass(), path_, task_runner, | 319 common::CopyToFile(response_->body.Pass(), path_, task_runner, |
296 base::Bind(callback, path_)); | 320 base::Bind(callback, path_)); |
321 | |
322 RecordCacheToURLMapping(path_, url_); | |
297 } | 323 } |
298 | 324 |
299 std::string MimeType() override { | 325 std::string MimeType() override { |
300 DCHECK(response_); | 326 DCHECK(response_); |
301 return response_->mime_type; | 327 return response_->mime_type; |
302 } | 328 } |
303 | 329 |
304 bool HasMojoMagic() override { | 330 bool HasMojoMagic() override { |
305 std::string magic; | 331 std::string magic; |
306 return BlockingPeekNBytes(response_->body.get(), &magic, strlen(kMojoMagic), | 332 return BlockingPeekNBytes(response_->body.get(), &magic, strlen(kMojoMagic), |
(...skipping 27 matching lines...) Expand all Loading... | |
334 LOG(ERROR) << "Error (" << response->error->code << ": " | 360 LOG(ERROR) << "Error (" << response->error->code << ": " |
335 << response->error->description << ") while fetching " | 361 << response->error->description << ") while fetching " |
336 << response->url; | 362 << response->url; |
337 ReportComplete(); | 363 ReportComplete(); |
338 return; | 364 return; |
339 } | 365 } |
340 response_ = response.Pass(); | 366 response_ = response.Pass(); |
341 Load(); | 367 Load(); |
342 } | 368 } |
343 | 369 |
370 void ReportComplete() override { | |
371 Loader::ReportComplete(); | |
372 // As soon as we've loaded the library we can delete the cache file. | |
373 // Tools can read the mojo_shell.PID.maps file to find the original library. | |
374 if (!path_.empty()) | |
375 DeleteFile(path_, false); | |
qsr
2015/01/22 16:29:07
I don't think this fix the issue with file leaks.
| |
376 } | |
377 | |
344 const GURL url_; | 378 const GURL url_; |
345 URLLoaderPtr url_loader_; | 379 URLLoaderPtr url_loader_; |
346 URLResponsePtr response_; | 380 URLResponsePtr response_; |
347 base::FilePath path_; | 381 base::FilePath path_; |
348 base::WeakPtrFactory<NetworkLoader> weak_ptr_factory_; | 382 base::WeakPtrFactory<NetworkLoader> weak_ptr_factory_; |
349 | 383 |
350 DISALLOW_COPY_AND_ASSIGN(NetworkLoader); | 384 DISALLOW_COPY_AND_ASSIGN(NetworkLoader); |
351 }; | 385 }; |
352 | 386 |
353 DynamicApplicationLoader::DynamicApplicationLoader( | 387 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 | 435 // 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? | 436 // the app closed its handle to the service manager. Maybe we don't care? |
403 } | 437 } |
404 | 438 |
405 void DynamicApplicationLoader::LoaderComplete(Loader* loader) { | 439 void DynamicApplicationLoader::LoaderComplete(Loader* loader) { |
406 loaders_.erase(std::find(loaders_.begin(), loaders_.end(), loader)); | 440 loaders_.erase(std::find(loaders_.begin(), loaders_.end(), loader)); |
407 } | 441 } |
408 | 442 |
409 } // namespace shell | 443 } // namespace shell |
410 } // namespace mojo | 444 } // namespace mojo |
OLD | NEW |