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/download_item_impl.h" | 5 #include "content/browser/download/download_item_impl.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 125 | 125 |
| 126 // Constructor for reading from the history service. | 126 // Constructor for reading from the history service. |
| 127 DownloadItemImpl::DownloadItemImpl(DownloadManager* download_manager, | 127 DownloadItemImpl::DownloadItemImpl(DownloadManager* download_manager, |
| 128 const DownloadPersistentStoreInfo& info) | 128 const DownloadPersistentStoreInfo& info) |
| 129 : download_id_(download_manager->GetNextId()), | 129 : download_id_(download_manager->GetNextId()), |
| 130 full_path_(info.path), | 130 full_path_(info.path), |
| 131 url_chain_(1, info.url), | 131 url_chain_(1, info.url), |
| 132 referrer_url_(info.referrer_url), | 132 referrer_url_(info.referrer_url), |
| 133 total_bytes_(info.total_bytes), | 133 total_bytes_(info.total_bytes), |
| 134 received_bytes_(info.received_bytes), | 134 received_bytes_(info.received_bytes), |
| 135 last_reason_(DOWNLOAD_INTERRUPT_REASON_NONE), | |
| 135 start_tick_(base::TimeTicks()), | 136 start_tick_(base::TimeTicks()), |
| 136 state_(static_cast<DownloadState>(info.state)), | 137 state_(static_cast<DownloadState>(info.state)), |
| 137 start_time_(info.start_time), | 138 start_time_(info.start_time), |
| 138 end_time_(info.end_time), | 139 end_time_(info.end_time), |
| 139 db_handle_(info.db_handle), | 140 db_handle_(info.db_handle), |
| 140 download_manager_(download_manager), | 141 download_manager_(download_manager), |
| 141 is_paused_(false), | 142 is_paused_(false), |
| 142 open_when_complete_(false), | 143 open_when_complete_(false), |
| 143 file_externally_removed_(false), | 144 file_externally_removed_(false), |
| 144 safety_state_(SAFE), | 145 safety_state_(SAFE), |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 317 UMA_HISTOGRAM_ENUMERATION("Download.DangerousDownloadValidated", | 318 UMA_HISTOGRAM_ENUMERATION("Download.DangerousDownloadValidated", |
| 318 GetDangerType(), | 319 GetDangerType(), |
| 319 DownloadStateInfo::DANGEROUS_TYPE_MAX); | 320 DownloadStateInfo::DANGEROUS_TYPE_MAX); |
| 320 | 321 |
| 321 safety_state_ = DANGEROUS_BUT_VALIDATED; | 322 safety_state_ = DANGEROUS_BUT_VALIDATED; |
| 322 UpdateObservers(); | 323 UpdateObservers(); |
| 323 | 324 |
| 324 download_manager_->MaybeCompleteDownload(this); | 325 download_manager_->MaybeCompleteDownload(this); |
| 325 } | 326 } |
| 326 | 327 |
| 327 void DownloadItemImpl::UpdateSize(int64 bytes_so_far) { | 328 void DownloadItemImpl::ProgressComplete(int64 bytes_so_far, |
| 329 const std::string& final_hash) { | |
| 328 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 330 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
| 329 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 331 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 330 | 332 |
| 333 hash_ = final_hash; | |
| 334 hash_state_ = ""; | |
| 335 | |
| 331 received_bytes_ = bytes_so_far; | 336 received_bytes_ = bytes_so_far; |
| 332 | 337 |
| 333 // If we've received more data than we were expecting (bad server info?), | 338 // If we've received more data than we were expecting (bad server info?), |
| 334 // revert to 'unknown size mode'. | 339 // revert to 'unknown size mode'. |
| 335 if (received_bytes_ > total_bytes_) | 340 if (received_bytes_ > total_bytes_) |
| 336 total_bytes_ = 0; | 341 total_bytes_ = 0; |
| 337 } | 342 } |
| 338 | 343 |
| 344 void DownloadItemImpl::UpdateProgress(int64 bytes_so_far, | |
| 345 const std::string& hash_state) { | |
| 346 hash_state_ = hash_state; | |
| 347 | |
| 348 received_bytes_ = bytes_so_far; | |
| 349 | |
| 350 // If we've received more data than we were expecting (bad server info?), | |
| 351 // revert to 'unknown size mode'. | |
| 352 if (received_bytes_ > total_bytes_) | |
| 353 total_bytes_ = 0; | |
| 354 } | |
| 355 | |
| 339 // Updates from the download thread may have been posted while this download | 356 // Updates from the download thread may have been posted while this download |
| 340 // was being cancelled in the UI thread, so we'll accept them unless we're | 357 // was being cancelled in the UI thread, so we'll accept them unless we're |
| 341 // complete. | 358 // complete. |
| 342 void DownloadItemImpl::Update(int64 bytes_so_far) { | 359 void DownloadItemImpl::Update(int64 bytes_so_far, |
| 360 const std::string& hash_state) { | |
| 343 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 361 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
| 344 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 362 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 345 | 363 |
| 346 if (!IsInProgress()) { | 364 if (!IsInProgress()) { |
| 347 NOTREACHED(); | 365 NOTREACHED(); |
| 348 return; | 366 return; |
| 349 } | 367 } |
| 350 UpdateSize(bytes_so_far); | 368 UpdateProgress(bytes_so_far, hash_state); |
| 351 UpdateObservers(); | 369 UpdateObservers(); |
| 352 } | 370 } |
| 353 | 371 |
| 354 // Triggered by a user action. | 372 // Triggered by a user action. |
| 355 void DownloadItemImpl::Cancel(bool user_cancel) { | 373 void DownloadItemImpl::Cancel(bool user_cancel) { |
| 356 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 374 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
| 357 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 375 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 358 | 376 |
| 359 last_reason_ = user_cancel ? | 377 last_reason_ = user_cancel ? |
| 360 DOWNLOAD_INTERRUPT_REASON_USER_CANCELED : | 378 DOWNLOAD_INTERRUPT_REASON_USER_CANCELED : |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 388 Completed(); | 406 Completed(); |
| 389 } | 407 } |
| 390 | 408 |
| 391 void DownloadItemImpl::OnAllDataSaved( | 409 void DownloadItemImpl::OnAllDataSaved( |
| 392 int64 size, const std::string& final_hash) { | 410 int64 size, const std::string& final_hash) { |
| 393 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 411 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
| 394 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 412 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 395 | 413 |
| 396 DCHECK(!all_data_saved_); | 414 DCHECK(!all_data_saved_); |
| 397 all_data_saved_ = true; | 415 all_data_saved_ = true; |
| 398 UpdateSize(size); | 416 ProgressComplete(size, final_hash); |
| 399 hash_ = final_hash; | |
| 400 } | 417 } |
| 401 | 418 |
| 402 void DownloadItemImpl::OnDownloadedFileRemoved() { | 419 void DownloadItemImpl::OnDownloadedFileRemoved() { |
| 403 file_externally_removed_ = true; | 420 file_externally_removed_ = true; |
| 404 UpdateObservers(); | 421 UpdateObservers(); |
| 405 } | 422 } |
| 406 | 423 |
| 407 void DownloadItemImpl::Completed() { | 424 void DownloadItemImpl::Completed() { |
| 408 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 425 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
| 409 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 426 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 450 } | 467 } |
| 451 | 468 |
| 452 void DownloadItemImpl::UpdateTarget() { | 469 void DownloadItemImpl::UpdateTarget() { |
| 453 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 470 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
| 454 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 471 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 455 | 472 |
| 456 if (state_info_.target_name.value().empty()) | 473 if (state_info_.target_name.value().empty()) |
| 457 state_info_.target_name = full_path_.BaseName(); | 474 state_info_.target_name = full_path_.BaseName(); |
| 458 } | 475 } |
| 459 | 476 |
| 460 void DownloadItemImpl::Interrupted(int64 size, InterruptReason reason) { | 477 void DownloadItemImpl::Interrupted(int64 size, |
| 478 const std::string& hash_state, | |
| 479 InterruptReason reason) { | |
| 461 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 480 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
| 462 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 481 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 463 | 482 |
| 464 if (!IsInProgress()) | 483 if (!IsInProgress()) |
| 465 return; | 484 return; |
| 466 | 485 |
| 467 last_reason_ = reason; | 486 last_reason_ = reason; |
| 468 UpdateSize(size); | 487 UpdateProgress(size, hash_state); |
| 469 download_stats::RecordDownloadInterrupted(reason, | 488 download_stats::RecordDownloadInterrupted(reason, |
| 470 received_bytes_, | 489 received_bytes_, |
| 471 total_bytes_); | 490 total_bytes_); |
| 472 TransitionTo(INTERRUPTED); | 491 TransitionTo(INTERRUPTED); |
| 473 } | 492 } |
| 474 | 493 |
| 475 void DownloadItemImpl::Delete(DeleteReason reason) { | 494 void DownloadItemImpl::Delete(DeleteReason reason) { |
| 476 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 495 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
| 477 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 496 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 478 | 497 |
| (...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 728 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 747 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 729 base::Bind(&DownloadFileManager::CancelDownload, | 748 base::Bind(&DownloadFileManager::CancelDownload, |
| 730 file_manager, GetGlobalId())); | 749 file_manager, GetGlobalId())); |
| 731 } | 750 } |
| 732 | 751 |
| 733 void DownloadItemImpl::Init(bool active) { | 752 void DownloadItemImpl::Init(bool active) { |
| 734 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 753 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
| 735 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 754 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 736 | 755 |
| 737 UpdateTarget(); | 756 UpdateTarget(); |
| 738 if (active) { | 757 if (active) |
| 739 download_stats::RecordDownloadCount(download_stats::START_COUNT); | 758 download_stats::RecordDownloadCount(download_stats::START_COUNT); |
| 740 } | |
| 741 VLOG(20) << __FUNCTION__ << "() " << DebugString(true); | 759 VLOG(20) << __FUNCTION__ << "() " << DebugString(true); |
| 742 } | 760 } |
| 743 | 761 |
| 744 // TODO(ahendrickson) -- Move |INTERRUPTED| from |IsCancelled()| to | 762 // TODO(ahendrickson) -- Move |INTERRUPTED| from |IsCancelled()| to |
| 745 // |IsPartialDownload()|, when resuming interrupted downloads is implemented. | 763 // |IsPartialDownload()|, when resuming interrupted downloads is implemented. |
| 746 bool DownloadItemImpl::IsPartialDownload() const { | 764 bool DownloadItemImpl::IsPartialDownload() const { |
| 747 return (state_ == IN_PROGRESS); | 765 return (state_ == IN_PROGRESS); |
| 748 } | 766 } |
| 749 | 767 |
| 750 bool DownloadItemImpl::IsInProgress() const { | 768 bool DownloadItemImpl::IsInProgress() const { |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 791 } | 809 } |
| 792 | 810 |
| 793 if (verbose) { | 811 if (verbose) { |
| 794 description += base::StringPrintf( | 812 description += base::StringPrintf( |
| 795 " db_handle = %" PRId64 | 813 " db_handle = %" PRId64 |
| 796 " total_bytes = %" PRId64 | 814 " total_bytes = %" PRId64 |
| 797 " received_bytes = %" PRId64 | 815 " received_bytes = %" PRId64 |
| 798 " is_paused = %c" | 816 " is_paused = %c" |
| 799 " is_otr = %c" | 817 " is_otr = %c" |
| 800 " safety_state = %s" | 818 " safety_state = %s" |
| 819 " last_modified = '%s'" | |
| 820 " etag = '%s'" | |
| 801 " url_chain = \n\t\"%s\"\n\t" | 821 " url_chain = \n\t\"%s\"\n\t" |
| 802 " target_name = \"%" PRFilePath "\"" | 822 " target_name = \"%" PRFilePath "\"" |
| 803 " full_path = \"%" PRFilePath "\"", | 823 " full_path = \"%" PRFilePath "\"", |
| 804 GetDbHandle(), | 824 GetDbHandle(), |
| 805 GetTotalBytes(), | 825 GetTotalBytes(), |
| 806 GetReceivedBytes(), | 826 GetReceivedBytes(), |
| 807 IsPaused() ? 'T' : 'F', | 827 IsPaused() ? 'T' : 'F', |
| 808 IsOtr() ? 'T' : 'F', | 828 IsOtr() ? 'T' : 'F', |
| 809 DebugSafetyStateString(GetSafetyState()), | 829 DebugSafetyStateString(GetSafetyState()), |
| 830 GetLastModifiedTime().c_str(), | |
| 831 GetETag().c_str(), | |
| 810 url_list.c_str(), | 832 url_list.c_str(), |
| 811 state_info_.target_name.value().c_str(), | 833 state_info_.target_name.value().c_str(), |
| 812 GetFullPath().value().c_str()); | 834 GetFullPath().value().c_str()); |
| 813 } else { | 835 } else { |
| 814 description += base::StringPrintf(" url = \"%s\"", url_list.c_str()); | 836 description += base::StringPrintf(" url = \"%s\"", url_list.c_str()); |
| 815 } | 837 } |
| 816 | 838 |
| 817 description += " }"; | 839 description += " }"; |
| 818 | 840 |
| 819 return description; | 841 return description; |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 846 } | 868 } |
| 847 std::string DownloadItemImpl::GetReferrerCharset() const { | 869 std::string DownloadItemImpl::GetReferrerCharset() const { |
| 848 return referrer_charset_; | 870 return referrer_charset_; |
| 849 } | 871 } |
| 850 int64 DownloadItemImpl::GetTotalBytes() const { return total_bytes_; } | 872 int64 DownloadItemImpl::GetTotalBytes() const { return total_bytes_; } |
| 851 void DownloadItemImpl::SetTotalBytes(int64 total_bytes) { | 873 void DownloadItemImpl::SetTotalBytes(int64 total_bytes) { |
| 852 total_bytes_ = total_bytes; | 874 total_bytes_ = total_bytes; |
| 853 } | 875 } |
| 854 const std::string& DownloadItemImpl::GetHash() const { return hash_; } | 876 const std::string& DownloadItemImpl::GetHash() const { return hash_; } |
| 855 int64 DownloadItemImpl::GetReceivedBytes() const { return received_bytes_; } | 877 int64 DownloadItemImpl::GetReceivedBytes() const { return received_bytes_; } |
| 878 const std::string& DownloadItemImpl::GetHashState() const { | |
|
Randy Smith (Not in Mondays)
2011/11/23 19:40:24
Just noticed that we use HashState here and Sha256
ahendrickson
2011/12/20 00:04:14
Changed to using just "Hash" instead of "Sha256Has
| |
| 879 return hash_state_; | |
| 880 } | |
| 856 int32 DownloadItemImpl::GetId() const { return download_id_.local(); } | 881 int32 DownloadItemImpl::GetId() const { return download_id_.local(); } |
| 857 DownloadId DownloadItemImpl::GetGlobalId() const { return download_id_; } | 882 DownloadId DownloadItemImpl::GetGlobalId() const { return download_id_; } |
| 858 base::Time DownloadItemImpl::GetStartTime() const { return start_time_; } | 883 base::Time DownloadItemImpl::GetStartTime() const { return start_time_; } |
| 859 base::Time DownloadItemImpl::GetEndTime() const { return end_time_; } | 884 base::Time DownloadItemImpl::GetEndTime() const { return end_time_; } |
| 860 void DownloadItemImpl::SetDbHandle(int64 handle) { db_handle_ = handle; } | 885 void DownloadItemImpl::SetDbHandle(int64 handle) { db_handle_ = handle; } |
| 861 int64 DownloadItemImpl::GetDbHandle() const { return db_handle_; } | 886 int64 DownloadItemImpl::GetDbHandle() const { return db_handle_; } |
| 862 DownloadManager* DownloadItemImpl::GetDownloadManager() { | 887 DownloadManager* DownloadItemImpl::GetDownloadManager() { |
| 863 return download_manager_; | 888 return download_manager_; |
| 864 } | 889 } |
| 865 bool DownloadItemImpl::IsPaused() const { return is_paused_; } | 890 bool DownloadItemImpl::IsPaused() const { return is_paused_; } |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 882 } | 907 } |
| 883 bool DownloadItemImpl::PromptUserForSaveLocation() const { | 908 bool DownloadItemImpl::PromptUserForSaveLocation() const { |
| 884 return state_info_.prompt_user_for_save_location; | 909 return state_info_.prompt_user_for_save_location; |
| 885 } | 910 } |
| 886 const FilePath& DownloadItemImpl::GetSuggestedPath() const { | 911 const FilePath& DownloadItemImpl::GetSuggestedPath() const { |
| 887 return state_info_.suggested_path; | 912 return state_info_.suggested_path; |
| 888 } | 913 } |
| 889 bool DownloadItemImpl::IsTemporary() const { return is_temporary_; } | 914 bool DownloadItemImpl::IsTemporary() const { return is_temporary_; } |
| 890 void DownloadItemImpl::SetOpened(bool opened) { opened_ = opened; } | 915 void DownloadItemImpl::SetOpened(bool opened) { opened_ = opened; } |
| 891 bool DownloadItemImpl::GetOpened() const { return opened_; } | 916 bool DownloadItemImpl::GetOpened() const { return opened_; } |
| 917 const std::string& DownloadItemImpl::GetLastModifiedTime() const { | |
| 918 return last_modified_time_; | |
| 919 } | |
| 920 const std::string& DownloadItemImpl::GetETag() const { return etag_; } | |
| 892 InterruptReason DownloadItemImpl::GetLastReason() const { | 921 InterruptReason DownloadItemImpl::GetLastReason() const { |
| 893 return last_reason_; | 922 return last_reason_; |
| 894 } | 923 } |
| 895 DownloadStateInfo DownloadItemImpl::GetStateInfo() const { return state_info_; } | 924 DownloadStateInfo DownloadItemImpl::GetStateInfo() const { return state_info_; } |
| 896 bool DownloadItemImpl::NeedsRename() const { | 925 bool DownloadItemImpl::NeedsRename() const { |
| 897 return state_info_.target_name != full_path_.BaseName(); | 926 return state_info_.target_name != full_path_.BaseName(); |
| 898 } | 927 } |
| 899 void DownloadItemImpl::MockDownloadOpenForTesting() { open_enabled_ = false; } | 928 void DownloadItemImpl::MockDownloadOpenForTesting() { open_enabled_ = false; } |
| OLD | NEW |