Chromium Code Reviews| 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.h" | 9 #include "base/files/file.h" |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 11 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
| 12 #include "base/format_macros.h" | 12 #include "base/format_macros.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/md5.h" | |
| 15 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 16 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
| 17 #include "base/message_loop/message_loop.h" | 16 #include "base/message_loop/message_loop.h" |
| 18 #include "base/process/process.h" | 17 #include "base/process/process.h" |
| 18 #include "base/strings/string_number_conversions.h" | |
| 19 #include "base/strings/string_util.h" | 19 #include "base/strings/string_util.h" |
| 20 #include "base/strings/stringprintf.h" | 20 #include "base/strings/stringprintf.h" |
| 21 #include "base/strings/utf_string_conversions.h" | 21 #include "base/strings/utf_string_conversions.h" |
| 22 #include "crypto/secure_hash.h" | |
| 23 #include "crypto/sha2.h" | |
| 22 #include "mojo/common/common_type_converters.h" | 24 #include "mojo/common/common_type_converters.h" |
| 23 #include "mojo/common/data_pipe_utils.h" | 25 #include "mojo/common/data_pipe_utils.h" |
| 24 #include "mojo/public/cpp/system/data_pipe.h" | 26 #include "mojo/public/cpp/system/data_pipe.h" |
| 25 #include "mojo/services/network/public/interfaces/url_loader.mojom.h" | 27 #include "mojo/services/network/public/interfaces/url_loader.mojom.h" |
| 26 #include "shell/context.h" | 28 #include "shell/context.h" |
| 27 #include "shell/data_pipe_peek.h" | 29 #include "shell/data_pipe_peek.h" |
| 28 #include "shell/filename_util.h" | 30 #include "shell/filename_util.h" |
| 29 #include "shell/switches.h" | 31 #include "shell/switches.h" |
| 30 #include "url/url_util.h" | 32 #include "url/url_util.h" |
| 31 | 33 |
| (...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 305 std::string map_entry = | 307 std::string map_entry = |
| 306 base::StringPrintf("%s %s\n", path.value().c_str(), url.spec().c_str()); | 308 base::StringPrintf("%s %s\n", path.value().c_str(), url.spec().c_str()); |
| 307 // TODO(eseidel): AppendToFile is missing O_CREAT, crbug.com/450696 | 309 // TODO(eseidel): AppendToFile is missing O_CREAT, crbug.com/450696 |
| 308 if (!PathExists(map_path)) | 310 if (!PathExists(map_path)) |
| 309 base::WriteFile(map_path, map_entry.data(), map_entry.length()); | 311 base::WriteFile(map_path, map_entry.data(), map_entry.length()); |
| 310 else | 312 else |
| 311 base::AppendToFile(map_path, map_entry.data(), map_entry.length()); | 313 base::AppendToFile(map_path, map_entry.data(), map_entry.length()); |
| 312 } | 314 } |
| 313 | 315 |
| 314 // AppIds should be be both predictable and unique, but any hash would work. | 316 // AppIds should be be both predictable and unique, but any hash would work. |
| 315 // TODO(eseidel): Use sha256 once it has an incremental API, crbug.com/451588 | 317 // Currently we use sha256 from crypto/secure_hash.h |
|
qsr
2015/01/30 15:15:58
Eric: there is a few things to consider for this:
| |
| 316 // Derived from tools/android/md5sum/md5sum.cc with fstream usage removed. | |
| 317 static bool ComputeAppId(const base::FilePath& path, | 318 static bool ComputeAppId(const base::FilePath& path, |
| 318 std::string* digest_string) { | 319 std::string* digest_string) { |
| 319 | 320 scoped_ptr<crypto::SecureHash> ctx( |
| 321 crypto::SecureHash::Create(crypto::SecureHash::SHA256)); | |
| 320 base::File file(path, base::File::FLAG_OPEN | base::File::FLAG_READ); | 322 base::File file(path, base::File::FLAG_OPEN | base::File::FLAG_READ); |
| 321 if (!file.IsValid()) { | 323 if (!file.IsValid()) { |
| 322 LOG(ERROR) << "Failed to open " << path.value() << " for computing AppId"; | 324 LOG(ERROR) << "Failed to open " << path.value() << " for computing AppId"; |
| 323 return false; | 325 return false; |
| 324 } | 326 } |
| 325 base::MD5Context ctx; | |
| 326 base::MD5Init(&ctx); | |
| 327 char buf[1024]; | 327 char buf[1024]; |
| 328 while (file.IsValid()) { | 328 while (file.IsValid()) { |
| 329 int bytes_read = file.ReadAtCurrentPos(buf, sizeof(buf)); | 329 int bytes_read = file.ReadAtCurrentPos(buf, sizeof(buf)); |
| 330 if (bytes_read == 0) | 330 if (bytes_read == 0) |
| 331 break; | 331 break; |
| 332 base::MD5Update(&ctx, base::StringPiece(buf, bytes_read)); | 332 ctx->Update(buf, bytes_read); |
| 333 } | 333 } |
| 334 if (!file.IsValid()) { | 334 if (!file.IsValid()) { |
| 335 LOG(ERROR) << "Error reading " << path.value(); | 335 LOG(ERROR) << "Error reading " << path.value(); |
| 336 return false; | 336 return false; |
| 337 } | 337 } |
| 338 base::MD5Digest digest; | 338 // The output is really a vector of unit8, we're cheating by using a string. |
| 339 base::MD5Final(&digest, &ctx); | 339 std::string output(crypto::kSHA256Length, 0); |
| 340 *digest_string = base::MD5DigestToBase16(digest); | 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); | |
| 341 return true; | 344 return true; |
| 342 } | 345 } |
| 343 | 346 |
| 344 static bool RenameToAppId(const base::FilePath& old_path, | 347 static bool RenameToAppId(const base::FilePath& old_path, |
| 345 base::FilePath* new_path) { | 348 base::FilePath* new_path) { |
| 346 std::string app_id; | 349 std::string app_id; |
| 347 if (!ComputeAppId(old_path, &app_id)) | 350 if (!ComputeAppId(old_path, &app_id)) |
| 348 return false; | 351 return false; |
| 349 | 352 |
| 350 base::FilePath temp_dir; | 353 base::FilePath temp_dir; |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 494 // TODO(darin): What should we do about service errors? This implies that | 497 // TODO(darin): What should we do about service errors? This implies that |
| 495 // the app closed its handle to the service manager. Maybe we don't care? | 498 // the app closed its handle to the service manager. Maybe we don't care? |
| 496 } | 499 } |
| 497 | 500 |
| 498 void DynamicApplicationLoader::LoaderComplete(Loader* loader) { | 501 void DynamicApplicationLoader::LoaderComplete(Loader* loader) { |
| 499 loaders_.erase(std::find(loaders_.begin(), loaders_.end(), loader)); | 502 loaders_.erase(std::find(loaders_.begin(), loaders_.end(), loader)); |
| 500 } | 503 } |
| 501 | 504 |
| 502 } // namespace shell | 505 } // namespace shell |
| 503 } // namespace mojo | 506 } // namespace mojo |
| OLD | NEW |