| 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 "shell/application_manager/network_fetcher.h" | 5 #include "shell/application_manager/network_fetcher.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/files/file.h" | 8 #include "base/files/file.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" |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 base::StringPrintf("%s %s\n", path.value().c_str(), url.spec().c_str()); | 80 base::StringPrintf("%s %s\n", path.value().c_str(), url.spec().c_str()); |
| 81 // TODO(eseidel): AppendToFile is missing O_CREAT, crbug.com/450696 | 81 // TODO(eseidel): AppendToFile is missing O_CREAT, crbug.com/450696 |
| 82 if (!PathExists(map_path)) | 82 if (!PathExists(map_path)) |
| 83 base::WriteFile(map_path, map_entry.data(), map_entry.length()); | 83 base::WriteFile(map_path, map_entry.data(), map_entry.length()); |
| 84 else | 84 else |
| 85 base::AppendToFile(map_path, map_entry.data(), map_entry.length()); | 85 base::AppendToFile(map_path, map_entry.data(), map_entry.length()); |
| 86 } | 86 } |
| 87 | 87 |
| 88 // AppIds should be be both predictable and unique, but any hash would work. | 88 // AppIds should be be both predictable and unique, but any hash would work. |
| 89 // Currently we use sha256 from crypto/secure_hash.h | 89 // Currently we use sha256 from crypto/secure_hash.h |
| 90 bool NetworkFetcher::ComputeAppId(const base::FilePath& path, | 90 bool NetworkFetcher::ComputeAppId(const GURL& canonical_url, |
| 91 const base::FilePath& path, |
| 91 std::string* digest_string) { | 92 std::string* digest_string) { |
| 92 scoped_ptr<crypto::SecureHash> ctx( | 93 scoped_ptr<crypto::SecureHash> ctx( |
| 93 crypto::SecureHash::Create(crypto::SecureHash::SHA256)); | 94 crypto::SecureHash::Create(crypto::SecureHash::SHA256)); |
| 94 base::File file(path, base::File::FLAG_OPEN | base::File::FLAG_READ); | 95 base::File file(path, base::File::FLAG_OPEN | base::File::FLAG_READ); |
| 95 if (!file.IsValid()) { | 96 if (!file.IsValid()) { |
| 96 LOG(ERROR) << "Failed to open " << path.value() << " for computing AppId"; | 97 LOG(ERROR) << "Failed to open " << path.value() << " for computing AppId"; |
| 97 return false; | 98 return false; |
| 98 } | 99 } |
| 100 std::string spec = canonical_url.spec(); |
| 101 uint32_t len = static_cast<uint32_t>(spec.size()); |
| 102 // Prevent URL vs. content spoofing. |
| 103 ctx->Update(&len, sizeof(len)); |
| 104 ctx->Update(spec.c_str(), spec.size()); |
| 99 char buf[1024]; | 105 char buf[1024]; |
| 100 while (file.IsValid()) { | 106 while (file.IsValid()) { |
| 101 int bytes_read = file.ReadAtCurrentPos(buf, sizeof(buf)); | 107 int bytes_read = file.ReadAtCurrentPos(buf, sizeof(buf)); |
| 102 if (bytes_read == 0) | 108 if (bytes_read == 0) |
| 103 break; | 109 break; |
| 104 ctx->Update(buf, bytes_read); | 110 ctx->Update(buf, bytes_read); |
| 105 } | 111 } |
| 106 if (!file.IsValid()) { | 112 if (!file.IsValid()) { |
| 107 LOG(ERROR) << "Error reading " << path.value(); | 113 LOG(ERROR) << "Error reading " << path.value(); |
| 108 return false; | 114 return false; |
| 109 } | 115 } |
| 110 // The output is really a vector of unit8, we're cheating by using a string. | 116 // The output is really a vector of unit8, we're cheating by using a string. |
| 111 std::string output(crypto::kSHA256Length, 0); | 117 std::string output(crypto::kSHA256Length, 0); |
| 112 ctx->Finish(string_as_array(&output), output.size()); | 118 ctx->Finish(string_as_array(&output), output.size()); |
| 113 output = base::HexEncode(output.c_str(), output.size()); | 119 output = base::HexEncode(output.c_str(), output.size()); |
| 114 // Using lowercase for compatiblity with sha256sum output. | 120 // Using lowercase for compatiblity with sha256sum output. |
| 115 *digest_string = base::StringToLowerASCII(output); | 121 *digest_string = base::StringToLowerASCII(output); |
| 116 return true; | 122 return true; |
| 117 } | 123 } |
| 118 | 124 |
| 119 bool NetworkFetcher::RenameToAppId(const base::FilePath& old_path, | 125 bool NetworkFetcher::RenameToAppId(const GURL& canonical_url, |
| 126 const base::FilePath& old_path, |
| 120 base::FilePath* new_path) { | 127 base::FilePath* new_path) { |
| 121 std::string app_id; | 128 std::string app_id; |
| 122 if (!ComputeAppId(old_path, &app_id)) | 129 if (!ComputeAppId(canonical_url, old_path, &app_id)) |
| 123 return false; | 130 return false; |
| 124 | 131 |
| 125 base::FilePath temp_dir; | 132 base::FilePath temp_dir; |
| 126 base::GetTempDir(&temp_dir); | 133 base::GetTempDir(&temp_dir); |
| 127 std::string unique_name = base::StringPrintf("%s.mojo", app_id.c_str()); | 134 std::string unique_name = base::StringPrintf("%s.mojo", app_id.c_str()); |
| 128 *new_path = temp_dir.Append(unique_name); | 135 *new_path = temp_dir.Append(unique_name); |
| 129 return base::Move(old_path, *new_path); | 136 return base::Move(old_path, *new_path); |
| 130 } | 137 } |
| 131 | 138 |
| 132 void NetworkFetcher::CopyCompleted( | 139 void NetworkFetcher::CopyCompleted( |
| 133 base::Callback<void(const base::FilePath&, bool)> callback, | 140 base::Callback<void(const base::FilePath&, bool)> callback, |
| 134 bool success) { | 141 bool success) { |
| 135 // The copy completed, now move to $TMP/$APP_ID.mojo before the dlopen. | 142 // The copy completed, now move to $TMP/$APP_ID.mojo before the dlopen. |
| 136 if (success) { | 143 if (success) { |
| 137 success = false; | 144 success = false; |
| 138 base::FilePath new_path; | 145 base::FilePath new_path; |
| 139 if (RenameToAppId(path_, &new_path)) { | 146 if (RenameToAppId(GURL(response_->url), path_, &new_path)) { |
| 140 if (base::PathExists(new_path)) { | 147 if (base::PathExists(new_path)) { |
| 141 path_ = new_path; | 148 path_ = new_path; |
| 142 success = true; | 149 success = true; |
| 143 RecordCacheToURLMapping(path_, url_); | 150 RecordCacheToURLMapping(path_, url_); |
| 144 } | 151 } |
| 145 } | 152 } |
| 146 } | 153 } |
| 147 | 154 |
| 148 base::MessageLoop::current()->PostTask(FROM_HERE, | 155 base::MessageLoop::current()->PostTask(FROM_HERE, |
| 149 base::Bind(callback, path_, success)); | 156 base::Bind(callback, path_, success)); |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 loader_callback_.Run(make_scoped_ptr<Fetcher>(NULL)); | 216 loader_callback_.Run(make_scoped_ptr<Fetcher>(NULL)); |
| 210 return; | 217 return; |
| 211 } | 218 } |
| 212 | 219 |
| 213 response_ = response.Pass(); | 220 response_ = response.Pass(); |
| 214 loader_callback_.Run(make_scoped_ptr(this)); | 221 loader_callback_.Run(make_scoped_ptr(this)); |
| 215 } | 222 } |
| 216 | 223 |
| 217 } // namespace shell | 224 } // namespace shell |
| 218 } // namespace mojo | 225 } // namespace mojo |
| OLD | NEW |