Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "content/browser/download/base_file.h" | 5 #include "content/browser/download/base_file.h" |
| 6 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/format_macros.h" | 8 #include "base/format_macros.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/stringprintf.h" | 10 #include "base/stringprintf.h" |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 186 | 186 |
| 187 } // namespace | 187 } // namespace |
| 188 | 188 |
| 189 // This will initialize the entire array to zero. | 189 // This will initialize the entire array to zero. |
| 190 const unsigned char BaseFile::kEmptySha256Hash[] = { 0 }; | 190 const unsigned char BaseFile::kEmptySha256Hash[] = { 0 }; |
| 191 | 191 |
| 192 BaseFile::BaseFile(const FilePath& full_path, | 192 BaseFile::BaseFile(const FilePath& full_path, |
| 193 const GURL& source_url, | 193 const GURL& source_url, |
| 194 const GURL& referrer_url, | 194 const GURL& referrer_url, |
| 195 int64 received_bytes, | 195 int64 received_bytes, |
| 196 const std::string& initial_hash, | |
| 196 const linked_ptr<net::FileStream>& file_stream) | 197 const linked_ptr<net::FileStream>& file_stream) |
| 197 : full_path_(full_path), | 198 : full_path_(full_path), |
| 198 source_url_(source_url), | 199 source_url_(source_url), |
| 199 referrer_url_(referrer_url), | 200 referrer_url_(referrer_url), |
| 200 file_stream_(file_stream), | 201 file_stream_(file_stream), |
| 201 bytes_so_far_(received_bytes), | 202 bytes_so_far_(received_bytes), |
| 202 power_save_blocker_(PowerSaveBlocker::kPowerSaveBlockPreventSystemSleep), | 203 power_save_blocker_(PowerSaveBlocker::kPowerSaveBlockPreventSystemSleep), |
| 203 calculate_hash_(false), | 204 calculate_hash_(false), |
| 204 detached_(false) { | 205 detached_(false) { |
| 205 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 206 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 206 memcpy(sha256_hash_, kEmptySha256Hash, kSha256HashLen); | 207 if (initial_hash.empty() || IsEmptySha256Hash(initial_hash)) |
| 208 memcpy(sha256_hash_, kEmptySha256Hash, kSha256HashLen); | |
| 209 else | |
| 210 memcpy(sha256_hash_, initial_hash.c_str(), kSha256HashLen); | |
| 207 if (file_stream_.get()) | 211 if (file_stream_.get()) |
| 208 file_stream_->EnableErrorStatistics(); | 212 file_stream_->EnableErrorStatistics(); |
| 209 } | 213 } |
| 210 | 214 |
| 211 BaseFile::~BaseFile() { | 215 BaseFile::~BaseFile() { |
| 212 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 216 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 213 if (detached_) | 217 if (detached_) |
| 214 Close(); | 218 Close(); |
| 215 else | 219 else |
| 216 Cancel(); // Will delete the file. | 220 Cancel(); // Will delete the file. |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 371 Close(); | 375 Close(); |
| 372 | 376 |
| 373 if (!full_path_.empty()) | 377 if (!full_path_.empty()) |
| 374 file_util::Delete(full_path_, false); | 378 file_util::Delete(full_path_, false); |
| 375 } | 379 } |
| 376 | 380 |
| 377 void BaseFile::Finish() { | 381 void BaseFile::Finish() { |
| 378 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 382 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 379 | 383 |
| 380 if (calculate_hash_) | 384 if (calculate_hash_) |
| 381 secure_hash_->Finish(sha256_hash_, kSha256HashLen); | 385 secure_hash_->Finish(sha256_hash_, kSha256HashLen); |
|
Randy Smith (Not in Mondays)
2011/11/16 21:40:25
I may be repenting my earlier comments; I hadn't r
ahendrickson
2011/11/19 20:18:04
You are correct -- GetSha256Hash() will only work
Randy Smith (Not in Mondays)
2011/11/21 02:10:17
*nod*; makes sense. I'd hold this CL until that o
ahendrickson
2011/11/21 20:34:46
The CL has landed! $-)
| |
| 382 | 386 |
| 383 Close(); | 387 Close(); |
| 384 } | 388 } |
| 385 | 389 |
| 386 bool BaseFile::GetSha256Hash(std::string* hash) { | 390 bool BaseFile::GetSha256Hash(std::string* hash) { |
| 387 DCHECK(!detached_); | 391 DCHECK(!detached_); |
| 388 hash->assign(reinterpret_cast<const char*>(sha256_hash_), | 392 hash->assign(reinterpret_cast<const char*>(sha256_hash_), |
| 389 sizeof(sha256_hash_)); | 393 sizeof(sha256_hash_)); |
| 390 return (calculate_hash_ && !in_progress()); | 394 return (calculate_hash_); |
| 391 } | 395 } |
| 392 | 396 |
| 393 bool BaseFile::IsEmptySha256Hash(const std::string& hash) { | 397 bool BaseFile::IsEmptySha256Hash(const std::string& hash) { |
| 394 return (hash.size() == kSha256HashLen && | 398 return (hash.size() == kSha256HashLen && |
| 395 0 == memcmp(hash.data(), kEmptySha256Hash, sizeof(kSha256HashLen))); | 399 0 == memcmp(hash.data(), kEmptySha256Hash, sizeof(kSha256HashLen))); |
| 396 } | 400 } |
| 397 | 401 |
| 398 void BaseFile::AnnotateWithSourceInformation() { | 402 void BaseFile::AnnotateWithSourceInformation() { |
| 399 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 403 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 400 DCHECK(!detached_); | 404 DCHECK(!detached_); |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 464 std::string BaseFile::DebugString() const { | 468 std::string BaseFile::DebugString() const { |
| 465 return base::StringPrintf("{ source_url_ = \"%s\"" | 469 return base::StringPrintf("{ source_url_ = \"%s\"" |
| 466 " full_path_ = \"%" PRFilePath "\"" | 470 " full_path_ = \"%" PRFilePath "\"" |
| 467 " bytes_so_far_ = %" PRId64 | 471 " bytes_so_far_ = %" PRId64 |
| 468 " detached_ = %c }", | 472 " detached_ = %c }", |
| 469 source_url_.spec().c_str(), | 473 source_url_.spec().c_str(), |
| 470 full_path_.value().c_str(), | 474 full_path_.value().c_str(), |
| 471 bytes_so_far_, | 475 bytes_so_far_, |
| 472 detached_ ? 'T' : 'F'); | 476 detached_ ? 'T' : 'F'); |
| 473 } | 477 } |
| OLD | NEW |