| 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 "chrome/browser/download/download_item.h" | 5 #include "chrome/browser/download/download_item.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.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/i18n/case_conversion.h" | 10 #include "base/i18n/case_conversion.h" |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 114 } | 114 } |
| 115 return dangerous_file ? | 115 return dangerous_file ? |
| 116 DownloadItem::DANGEROUS_FILE : DownloadItem::NOT_DANGEROUS; | 116 DownloadItem::DANGEROUS_FILE : DownloadItem::NOT_DANGEROUS; |
| 117 } | 117 } |
| 118 | 118 |
| 119 } // namespace | 119 } // namespace |
| 120 | 120 |
| 121 // Constructor for reading from the history service. | 121 // Constructor for reading from the history service. |
| 122 DownloadItem::DownloadItem(DownloadManager* download_manager, | 122 DownloadItem::DownloadItem(DownloadManager* download_manager, |
| 123 const DownloadHistoryInfo& info) | 123 const DownloadHistoryInfo& info) |
| 124 : download_id_(-1), | 124 : download_id_(info.id), |
| 125 is_in_history_(true), |
| 125 full_path_(info.path), | 126 full_path_(info.path), |
| 126 url_chain_(1, info.url), | 127 url_chain_(1, info.url), |
| 127 referrer_url_(info.referrer_url), | 128 referrer_url_(info.referrer_url), |
| 128 total_bytes_(info.total_bytes), | 129 total_bytes_(info.total_bytes), |
| 129 received_bytes_(info.received_bytes), | 130 received_bytes_(info.received_bytes), |
| 130 start_tick_(base::TimeTicks()), | 131 start_tick_(base::TimeTicks()), |
| 131 state_(static_cast<DownloadState>(info.state)), | 132 state_(static_cast<DownloadState>(info.state)), |
| 132 start_time_(info.start_time), | 133 start_time_(info.start_time), |
| 133 db_handle_(info.db_handle), | |
| 134 download_manager_(download_manager), | 134 download_manager_(download_manager), |
| 135 is_paused_(false), | 135 is_paused_(false), |
| 136 open_when_complete_(false), | 136 open_when_complete_(false), |
| 137 file_externally_removed_(false), | 137 file_externally_removed_(false), |
| 138 safety_state_(SAFE), | 138 safety_state_(SAFE), |
| 139 auto_opened_(false), | 139 auto_opened_(false), |
| 140 is_otr_(false), | 140 is_otr_(false), |
| 141 is_temporary_(false), | 141 is_temporary_(false), |
| 142 all_data_saved_(false), | 142 all_data_saved_(false), |
| 143 opened_(false), | 143 opened_(false), |
| 144 open_enabled_(true) { | 144 open_enabled_(true) { |
| 145 if (IsInProgress()) | 145 if (IsInProgress()) |
| 146 state_ = CANCELLED; | 146 state_ = CANCELLED; |
| 147 if (IsComplete()) | 147 if (IsComplete()) |
| 148 all_data_saved_ = true; | 148 all_data_saved_ = true; |
| 149 Init(false /* not actively downloading */); | 149 Init(false /* not actively downloading */); |
| 150 } | 150 } |
| 151 | 151 |
| 152 // Constructing for a regular download: | 152 // Constructing for a regular download: |
| 153 DownloadItem::DownloadItem(DownloadManager* download_manager, | 153 DownloadItem::DownloadItem(DownloadManager* download_manager, |
| 154 const DownloadCreateInfo& info, | 154 const DownloadCreateInfo& info, |
| 155 bool is_otr) | 155 bool is_otr) |
| 156 : state_info_(info.original_name, info.save_info.file_path, | 156 : state_info_(info.original_name, info.save_info.file_path, |
| 157 info.has_user_gesture, info.prompt_user_for_save_location, | 157 info.has_user_gesture, info.prompt_user_for_save_location, |
| 158 info.path_uniquifier, false, false, | 158 info.path_uniquifier, false, false, |
| 159 info.is_extension_install), | 159 info.is_extension_install), |
| 160 request_handle_(info.request_handle), | 160 request_handle_(info.request_handle), |
| 161 download_id_(info.download_id), | 161 download_id_(info.download_id), |
| 162 is_in_history_(false), |
| 162 full_path_(info.path), | 163 full_path_(info.path), |
| 163 url_chain_(info.url_chain), | 164 url_chain_(info.url_chain), |
| 164 referrer_url_(info.referrer_url), | 165 referrer_url_(info.referrer_url), |
| 165 content_disposition_(info.content_disposition), | 166 content_disposition_(info.content_disposition), |
| 166 mime_type_(info.mime_type), | 167 mime_type_(info.mime_type), |
| 167 original_mime_type_(info.original_mime_type), | 168 original_mime_type_(info.original_mime_type), |
| 168 referrer_charset_(info.referrer_charset), | 169 referrer_charset_(info.referrer_charset), |
| 169 total_bytes_(info.total_bytes), | 170 total_bytes_(info.total_bytes), |
| 170 received_bytes_(0), | 171 received_bytes_(0), |
| 171 last_os_error_(0), | 172 last_os_error_(0), |
| 172 start_tick_(base::TimeTicks::Now()), | 173 start_tick_(base::TimeTicks::Now()), |
| 173 state_(IN_PROGRESS), | 174 state_(IN_PROGRESS), |
| 174 start_time_(info.start_time), | 175 start_time_(info.start_time), |
| 175 db_handle_(DownloadHistory::kUninitializedHandle), | |
| 176 download_manager_(download_manager), | 176 download_manager_(download_manager), |
| 177 is_paused_(false), | 177 is_paused_(false), |
| 178 open_when_complete_(false), | 178 open_when_complete_(false), |
| 179 file_externally_removed_(false), | 179 file_externally_removed_(false), |
| 180 safety_state_(SAFE), | 180 safety_state_(SAFE), |
| 181 auto_opened_(false), | 181 auto_opened_(false), |
| 182 is_otr_(is_otr), | 182 is_otr_(is_otr), |
| 183 is_temporary_(!info.save_info.file_path.empty()), | 183 is_temporary_(!info.save_info.file_path.empty()), |
| 184 all_data_saved_(false), | 184 all_data_saved_(false), |
| 185 opened_(false), | 185 opened_(false), |
| 186 open_enabled_(true) { | 186 open_enabled_(true) { |
| 187 Init(true /* actively downloading */); | 187 Init(true /* actively downloading */); |
| 188 } | 188 } |
| 189 | 189 |
| 190 // Constructing for the "Save Page As..." feature: | 190 // Constructing for the "Save Page As..." feature: |
| 191 DownloadItem::DownloadItem(DownloadManager* download_manager, | 191 DownloadItem::DownloadItem(DownloadManager* download_manager, |
| 192 const FilePath& path, | 192 const FilePath& path, |
| 193 const GURL& url, | 193 const GURL& url, |
| 194 bool is_otr) | 194 bool is_otr) |
| 195 : download_id_(1), | 195 : download_id_(-1/*TODO(benjhayden) XXX*/), |
| 196 is_in_history_(false), |
| 196 full_path_(path), | 197 full_path_(path), |
| 197 url_chain_(1, url), | 198 url_chain_(1, url), |
| 198 referrer_url_(GURL()), | 199 referrer_url_(GURL()), |
| 199 total_bytes_(0), | 200 total_bytes_(0), |
| 200 received_bytes_(0), | 201 received_bytes_(0), |
| 201 last_os_error_(0), | 202 last_os_error_(0), |
| 202 start_tick_(base::TimeTicks::Now()), | 203 start_tick_(base::TimeTicks::Now()), |
| 203 state_(IN_PROGRESS), | 204 state_(IN_PROGRESS), |
| 204 start_time_(base::Time::Now()), | 205 start_time_(base::Time::Now()), |
| 205 db_handle_(DownloadHistory::kUninitializedHandle), | |
| 206 download_manager_(download_manager), | 206 download_manager_(download_manager), |
| 207 is_paused_(false), | 207 is_paused_(false), |
| 208 open_when_complete_(false), | 208 open_when_complete_(false), |
| 209 file_externally_removed_(false), | 209 file_externally_removed_(false), |
| 210 safety_state_(SAFE), | 210 safety_state_(SAFE), |
| 211 auto_opened_(false), | 211 auto_opened_(false), |
| 212 is_otr_(is_otr), | 212 is_otr_(is_otr), |
| 213 is_temporary_(false), | 213 is_temporary_(false), |
| 214 all_data_saved_(false), | 214 all_data_saved_(false), |
| 215 opened_(false), | 215 opened_(false), |
| (...skipping 317 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 533 | 533 |
| 534 void DownloadItem::Remove() { | 534 void DownloadItem::Remove() { |
| 535 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 535 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
| 536 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 536 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 537 | 537 |
| 538 download_manager_->AssertQueueStateConsistent(this); | 538 download_manager_->AssertQueueStateConsistent(this); |
| 539 Cancel(true); | 539 Cancel(true); |
| 540 download_manager_->AssertQueueStateConsistent(this); | 540 download_manager_->AssertQueueStateConsistent(this); |
| 541 | 541 |
| 542 state_ = REMOVING; | 542 state_ = REMOVING; |
| 543 download_manager_->RemoveDownload(db_handle_); | 543 download_manager_->RemoveDownload(id()); |
| 544 // We have now been deleted. | 544 // We have now been deleted. |
| 545 } | 545 } |
| 546 | 546 |
| 547 bool DownloadItem::TimeRemaining(base::TimeDelta* remaining) const { | 547 bool DownloadItem::TimeRemaining(base::TimeDelta* remaining) const { |
| 548 if (total_bytes_ <= 0) | 548 if (total_bytes_ <= 0) |
| 549 return false; // We never received the content_length for this download. | 549 return false; // We never received the content_length for this download. |
| 550 | 550 |
| 551 int64 speed = CurrentSpeed(); | 551 int64 speed = CurrentSpeed(); |
| 552 if (speed == 0) | 552 if (speed == 0) |
| 553 return false; | 553 return false; |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 596 request_handle_.ResumeRequest(); | 596 request_handle_.ResumeRequest(); |
| 597 else | 597 else |
| 598 request_handle_.PauseRequest(); | 598 request_handle_.PauseRequest(); |
| 599 is_paused_ = !is_paused_; | 599 is_paused_ = !is_paused_; |
| 600 UpdateObservers(); | 600 UpdateObservers(); |
| 601 } | 601 } |
| 602 | 602 |
| 603 void DownloadItem::OnDownloadCompleting(DownloadFileManager* file_manager) { | 603 void DownloadItem::OnDownloadCompleting(DownloadFileManager* file_manager) { |
| 604 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 604 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
| 605 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 605 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 606 | 606 DVLOG(1) << __FUNCTION__ << "()" |
| 607 VLOG(20) << __FUNCTION__ << "()" | |
| 608 << " needs rename = " << NeedsRename() | 607 << " needs rename = " << NeedsRename() |
| 609 << " " << DebugString(true); | 608 << " " << DebugString(true); |
| 610 DCHECK_NE(DANGEROUS, safety_state()); | 609 DCHECK_NE(DANGEROUS, safety_state()); |
| 611 DCHECK(file_manager); | 610 DCHECK(file_manager); |
| 612 | 611 |
| 613 if (NeedsRename()) { | 612 if (NeedsRename()) { |
| 614 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 613 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 615 NewRunnableMethod(file_manager, | 614 NewRunnableMethod(file_manager, |
| 616 &DownloadFileManager::RenameCompletingDownloadFile, id(), | 615 &DownloadFileManager::RenameCompletingDownloadFile, id(), |
| 617 GetTargetFilePath(), safety_state() == SAFE)); | 616 GetTargetFilePath(), safety_state() == SAFE)); |
| 618 return; | 617 return; |
| 619 } | 618 } |
| 620 | 619 |
| 621 DCHECK(!is_extension_install()); | 620 DCHECK(!is_extension_install()); |
| 622 Completed(); | 621 Completed(); |
| 623 | 622 |
| 624 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 623 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 625 NewRunnableMethod(file_manager, &DownloadFileManager::CompleteDownload, | 624 NewRunnableMethod(file_manager, &DownloadFileManager::CompleteDownload, |
| 626 id())); | 625 id())); |
| 627 } | 626 } |
| 628 | 627 |
| 629 void DownloadItem::OnDownloadRenamedToFinalName(const FilePath& full_path) { | 628 void DownloadItem::OnDownloadRenamedToFinalName(const FilePath& full_path) { |
| 630 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 629 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
| 631 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 630 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 632 | 631 |
| 633 VLOG(20) << __FUNCTION__ << "()" | 632 DVLOG(1) << __FUNCTION__ << "()" |
| 634 << " full_path = \"" << full_path.value() << "\"" | 633 << " full_path = \"" << full_path.value() << "\"" |
| 635 << " needed rename = " << NeedsRename() | 634 << " needed rename = " << NeedsRename() |
| 636 << " " << DebugString(false); | 635 << " " << DebugString(false); |
| 637 DCHECK(NeedsRename()); | 636 DCHECK(NeedsRename()); |
| 638 | 637 |
| 639 Rename(full_path); | 638 Rename(full_path); |
| 640 | 639 |
| 641 if (is_extension_install()) { | 640 if (is_extension_install()) { |
| 642 StartCrxInstall(); | 641 StartCrxInstall(); |
| 643 // Completed() will be called when the installer finishes. | 642 // Completed() will be called when the installer finishes. |
| 644 return; | 643 return; |
| 645 } | 644 } |
| 646 | 645 |
| 647 Completed(); | 646 Completed(); |
| 647 DVLOG(1) << DebugString(true); |
| 648 } | 648 } |
| 649 | 649 |
| 650 bool DownloadItem::MatchesQuery(const string16& query) const { | 650 bool DownloadItem::MatchesQuery(const string16& query) const { |
| 651 if (query.empty()) | 651 if (query.empty()) |
| 652 return true; | 652 return true; |
| 653 | 653 |
| 654 DCHECK_EQ(query, base::i18n::ToLower(query)); | 654 DCHECK_EQ(query, base::i18n::ToLower(query)); |
| 655 | 655 |
| 656 string16 url_raw(base::i18n::ToLower(UTF8ToUTF16(GetURL().spec()))); | 656 string16 url_raw(base::i18n::ToLower(UTF8ToUTF16(GetURL().spec()))); |
| 657 if (url_raw.find(query) != string16::npos) | 657 if (url_raw.find(query) != string16::npos) |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 725 } | 725 } |
| 726 | 726 |
| 727 DownloadHistoryInfo DownloadItem::GetHistoryInfo() const { | 727 DownloadHistoryInfo DownloadItem::GetHistoryInfo() const { |
| 728 return DownloadHistoryInfo(full_path(), | 728 return DownloadHistoryInfo(full_path(), |
| 729 GetURL(), | 729 GetURL(), |
| 730 referrer_url(), | 730 referrer_url(), |
| 731 start_time(), | 731 start_time(), |
| 732 received_bytes(), | 732 received_bytes(), |
| 733 total_bytes(), | 733 total_bytes(), |
| 734 state(), | 734 state(), |
| 735 db_handle()); | 735 id()); |
| 736 } | 736 } |
| 737 | 737 |
| 738 FilePath DownloadItem::GetTargetFilePath() const { | 738 FilePath DownloadItem::GetTargetFilePath() const { |
| 739 return full_path_.DirName().Append(state_info_.target_name); | 739 return full_path_.DirName().Append(state_info_.target_name); |
| 740 } | 740 } |
| 741 | 741 |
| 742 FilePath DownloadItem::GetFileNameToReportUser() const { | 742 FilePath DownloadItem::GetFileNameToReportUser() const { |
| 743 if (state_info_.path_uniquifier > 0) { | 743 if (state_info_.path_uniquifier > 0) { |
| 744 FilePath name(state_info_.target_name); | 744 FilePath name(state_info_.target_name); |
| 745 download_util::AppendNumberToPath(&name, state_info_.path_uniquifier); | 745 download_util::AppendNumberToPath(&name, state_info_.path_uniquifier); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 781 } | 781 } |
| 782 | 782 |
| 783 bool DownloadItem::IsInterrupted() const { | 783 bool DownloadItem::IsInterrupted() const { |
| 784 return (state_ == INTERRUPTED); | 784 return (state_ == INTERRUPTED); |
| 785 } | 785 } |
| 786 | 786 |
| 787 bool DownloadItem::IsComplete() const { | 787 bool DownloadItem::IsComplete() const { |
| 788 return (state_ == COMPLETE); | 788 return (state_ == COMPLETE); |
| 789 } | 789 } |
| 790 | 790 |
| 791 bool DownloadItem::IsInHistory() const { |
| 792 return is_in_history_; |
| 793 } |
| 794 |
| 791 const GURL& DownloadItem::GetURL() const { | 795 const GURL& DownloadItem::GetURL() const { |
| 792 return url_chain_.empty() ? | 796 return url_chain_.empty() ? |
| 793 GURL::EmptyGURL() : url_chain_.back(); | 797 GURL::EmptyGURL() : url_chain_.back(); |
| 794 } | 798 } |
| 795 | 799 |
| 796 std::string DownloadItem::DebugString(bool verbose) const { | 800 std::string DownloadItem::DebugString(bool verbose) const { |
| 797 std::string description = | 801 std::string description = |
| 798 base::StringPrintf("{ id = %d" | 802 base::StringPrintf("{ id = %d" |
| 799 " state = %s", | 803 " state = %s", |
| 800 download_id_, | 804 id(), |
| 801 DebugDownloadStateString(state())); | 805 DebugDownloadStateString(state())); |
| 802 | 806 |
| 803 // Construct a string of the URL chain. | 807 // Construct a string of the URL chain. |
| 804 std::string url_list("<none>"); | 808 std::string url_list("<none>"); |
| 805 if (!url_chain_.empty()) { | 809 if (!url_chain_.empty()) { |
| 806 std::vector<GURL>::const_iterator iter = url_chain_.begin(); | 810 std::vector<GURL>::const_iterator iter = url_chain_.begin(); |
| 807 std::vector<GURL>::const_iterator last = url_chain_.end(); | 811 std::vector<GURL>::const_iterator last = url_chain_.end(); |
| 808 url_list = (*iter).spec(); | 812 url_list = (*iter).spec(); |
| 809 ++iter; | 813 ++iter; |
| 810 for ( ; verbose && (iter != last); ++iter) { | 814 for ( ; verbose && (iter != last); ++iter) { |
| 811 url_list += " ->\n\t"; | 815 url_list += " ->\n\t"; |
| 812 const GURL& next_url = *iter; | 816 const GURL& next_url = *iter; |
| 813 url_list += next_url.spec(); | 817 url_list += next_url.spec(); |
| 814 } | 818 } |
| 815 } | 819 } |
| 816 | 820 |
| 817 if (verbose) { | 821 if (verbose) { |
| 818 description += base::StringPrintf( | 822 description += base::StringPrintf( |
| 819 " db_handle = %" PRId64 | |
| 820 " total_bytes = %" PRId64 | 823 " total_bytes = %" PRId64 |
| 821 " received_bytes = %" PRId64 | 824 " received_bytes = %" PRId64 |
| 822 " is_paused = %c" | 825 " is_paused = %c" |
| 823 " is_extension_install = %c" | 826 " is_extension_install = %c" |
| 824 " is_otr = %c" | 827 " is_otr = %c" |
| 825 " safety_state = %s" | 828 " safety_state = %s" |
| 826 " url_chain = \n\t\"%s\"\n\t" | 829 " url_chain = \n\t\"%s\"\n\t" |
| 827 " target_name = \"%" PRFilePath "\"" | 830 " target_name = \"%" PRFilePath "\"" |
| 828 " full_path = \"%" PRFilePath "\"", | 831 " full_path = \"%" PRFilePath "\"", |
| 829 db_handle(), | |
| 830 total_bytes(), | 832 total_bytes(), |
| 831 received_bytes(), | 833 received_bytes(), |
| 832 is_paused() ? 'T' : 'F', | 834 is_paused() ? 'T' : 'F', |
| 833 is_extension_install() ? 'T' : 'F', | 835 is_extension_install() ? 'T' : 'F', |
| 834 is_otr() ? 'T' : 'F', | 836 is_otr() ? 'T' : 'F', |
| 835 DebugSafetyStateString(safety_state()), | 837 DebugSafetyStateString(safety_state()), |
| 836 url_list.c_str(), | 838 url_list.c_str(), |
| 837 state_info_.target_name.value().c_str(), | 839 state_info_.target_name.value().c_str(), |
| 838 full_path().value().c_str()); | 840 full_path().value().c_str()); |
| 839 } else { | 841 } else { |
| 840 description += base::StringPrintf(" url = \"%s\"", url_list.c_str()); | 842 description += base::StringPrintf(" url = \"%s\"", url_list.c_str()); |
| 841 } | 843 } |
| 842 | 844 |
| 843 description += " }"; | 845 description += " }"; |
| 844 | 846 |
| 845 return description; | 847 return description; |
| 846 } | 848 } |
| OLD | NEW |