Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/format_macros.h" | 9 #include "base/format_macros.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 258 return LOG_ERROR("unable to create", net::ERR_FILE_NOT_FOUND); | 258 return LOG_ERROR("unable to create", net::ERR_FILE_NOT_FOUND); |
| 259 } | 259 } |
| 260 full_path_ = temp_file; | 260 full_path_ = temp_file; |
| 261 } | 261 } |
| 262 | 262 |
| 263 return Open(); | 263 return Open(); |
| 264 } | 264 } |
| 265 | 265 |
| 266 net::Error BaseFile::AppendDataToFile(const char* data, size_t data_len) { | 266 net::Error BaseFile::AppendDataToFile(const char* data, size_t data_len) { |
| 267 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 267 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 268 DCHECK(!detached_); | 268 if (detached_) return net::ERR_INVALID_HANDLE; |
|
Randy Smith (Not in Mondays)
2012/09/11 18:36:53
You didn't respond on the other CL, so I'll ask ag
benjhayden
2012/09/13 15:18:16
I had found a race that meant that there might be
| |
| 269 | 269 |
| 270 // NOTE(benwells): The above DCHECK won't be present in release builds, | 270 // NOTE(benwells): The above DCHECK won't be present in release builds, |
| 271 // so we log any occurences to see how common this error is in the wild. | 271 // so we log any occurences to see how common this error is in the wild. |
| 272 if (detached_) { | 272 if (detached_) { |
| 273 download_stats::RecordDownloadCount( | 273 download_stats::RecordDownloadCount( |
| 274 download_stats::APPEND_TO_DETACHED_FILE_COUNT); | 274 download_stats::APPEND_TO_DETACHED_FILE_COUNT); |
| 275 } | 275 } |
| 276 | 276 |
| 277 if (!file_stream_.get()) | 277 if (!file_stream_.get()) |
| 278 return LOG_ERROR("get", net::ERR_INVALID_HANDLE); | 278 return LOG_ERROR("get", net::ERR_INVALID_HANDLE); |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 395 return Open(); | 395 return Open(); |
| 396 } | 396 } |
| 397 | 397 |
| 398 void BaseFile::Detach() { | 398 void BaseFile::Detach() { |
| 399 detached_ = true; | 399 detached_ = true; |
| 400 bound_net_log_.AddEvent(net::NetLog::TYPE_DOWNLOAD_FILE_DETACHED); | 400 bound_net_log_.AddEvent(net::NetLog::TYPE_DOWNLOAD_FILE_DETACHED); |
| 401 } | 401 } |
| 402 | 402 |
| 403 void BaseFile::Cancel() { | 403 void BaseFile::Cancel() { |
| 404 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 404 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 405 DCHECK(!detached_); | 405 if (detached_) return; |
| 406 | 406 |
| 407 bound_net_log_.AddEvent(net::NetLog::TYPE_CANCELLED); | 407 bound_net_log_.AddEvent(net::NetLog::TYPE_CANCELLED); |
| 408 | 408 |
| 409 Close(); | 409 Close(); |
| 410 | 410 |
| 411 if (!full_path_.empty()) { | 411 if (!full_path_.empty()) { |
| 412 bound_net_log_.AddEvent(net::NetLog::TYPE_DOWNLOAD_FILE_DELETED); | 412 bound_net_log_.AddEvent(net::NetLog::TYPE_DOWNLOAD_FILE_DELETED); |
| 413 | 413 |
| 414 file_util::Delete(full_path_, false); | 414 file_util::Delete(full_path_, false); |
| 415 } | 415 } |
| 416 } | 416 } |
| 417 | 417 |
| 418 void BaseFile::Finish() { | 418 void BaseFile::Finish() { |
| 419 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 419 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 420 | 420 |
| 421 if (calculate_hash_) | 421 if (calculate_hash_) |
| 422 secure_hash_->Finish(sha256_hash_, kSha256HashLen); | 422 secure_hash_->Finish(sha256_hash_, kSha256HashLen); |
| 423 | 423 |
| 424 Close(); | 424 Close(); |
| 425 } | 425 } |
| 426 | 426 |
| 427 bool BaseFile::GetHash(std::string* hash) { | 427 bool BaseFile::GetHash(std::string* hash) { |
| 428 DCHECK(!detached_); | 428 if (detached_) return false; |
| 429 hash->assign(reinterpret_cast<const char*>(sha256_hash_), | 429 hash->assign(reinterpret_cast<const char*>(sha256_hash_), |
| 430 sizeof(sha256_hash_)); | 430 sizeof(sha256_hash_)); |
| 431 return (calculate_hash_ && !in_progress()); | 431 return (calculate_hash_ && !in_progress()); |
| 432 } | 432 } |
| 433 | 433 |
| 434 std::string BaseFile::GetHashState() { | 434 std::string BaseFile::GetHashState() { |
| 435 if (!calculate_hash_) | 435 if (!calculate_hash_) |
| 436 return ""; | 436 return ""; |
| 437 | 437 |
| 438 Pickle hash_state; | 438 Pickle hash_state; |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 453 return secure_hash_->Deserialize(&data_iterator); | 453 return secure_hash_->Deserialize(&data_iterator); |
| 454 } | 454 } |
| 455 | 455 |
| 456 bool BaseFile::IsEmptyHash(const std::string& hash) { | 456 bool BaseFile::IsEmptyHash(const std::string& hash) { |
| 457 return (hash.size() == kSha256HashLen && | 457 return (hash.size() == kSha256HashLen && |
| 458 0 == memcmp(hash.data(), kEmptySha256Hash, sizeof(kSha256HashLen))); | 458 0 == memcmp(hash.data(), kEmptySha256Hash, sizeof(kSha256HashLen))); |
| 459 } | 459 } |
| 460 | 460 |
| 461 void BaseFile::AnnotateWithSourceInformation() { | 461 void BaseFile::AnnotateWithSourceInformation() { |
| 462 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 462 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 463 DCHECK(!detached_); | 463 if (detached_) return; |
| 464 | 464 |
| 465 #if defined(OS_WIN) | 465 #if defined(OS_WIN) |
| 466 // Sets the Zone to tell Windows that this file comes from the internet. | 466 // Sets the Zone to tell Windows that this file comes from the internet. |
| 467 // We ignore the return value because a failure is not fatal. | 467 // We ignore the return value because a failure is not fatal. |
| 468 win_util::SetInternetZoneIdentifier(full_path_, | 468 win_util::SetInternetZoneIdentifier(full_path_, |
| 469 UTF8ToWide(source_url_.spec())); | 469 UTF8ToWide(source_url_.spec())); |
| 470 #elif defined(OS_MACOSX) | 470 #elif defined(OS_MACOSX) |
| 471 content::AddQuarantineMetadataToFile(full_path_, source_url_, referrer_url_); | 471 content::AddQuarantineMetadataToFile(full_path_, source_url_, referrer_url_); |
| 472 content::AddOriginMetadataToFile(full_path_, source_url_, referrer_url_); | 472 content::AddOriginMetadataToFile(full_path_, source_url_, referrer_url_); |
| 473 #elif defined(OS_LINUX) | 473 #elif defined(OS_LINUX) |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 551 base::TimeDelta diff = current_time - start_tick_; | 551 base::TimeDelta diff = current_time - start_tick_; |
| 552 int64 diff_ms = diff.InMilliseconds(); | 552 int64 diff_ms = diff.InMilliseconds(); |
| 553 return diff_ms == 0 ? 0 : bytes_so_far() * 1000 / diff_ms; | 553 return diff_ms == 0 ? 0 : bytes_so_far() * 1000 / diff_ms; |
| 554 } | 554 } |
| 555 | 555 |
| 556 int64 BaseFile::CurrentSpeed() const { | 556 int64 BaseFile::CurrentSpeed() const { |
| 557 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 557 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 558 return CurrentSpeedAtTime(base::TimeTicks::Now()); | 558 return CurrentSpeedAtTime(base::TimeTicks::Now()); |
| 559 } | 559 } |
| 560 | 560 |
| OLD | NEW |