Chromium Code Reviews| Index: chrome/browser/download/download_item.cc |
| diff --git a/chrome/browser/download/download_item.cc b/chrome/browser/download/download_item.cc |
| index 7f75dc19169707189890a634d6c9d5df4385b3ff..49b414676b6e7c7d75d2554a5b4f5b30db042f53 100644 |
| --- a/chrome/browser/download/download_item.cc |
| +++ b/chrome/browser/download/download_item.cc |
| @@ -341,6 +341,21 @@ void DownloadItem::UpdateSize(int64 bytes_so_far) { |
| total_bytes_ = 0; |
| } |
| +void DownloadItem::StopInternal(DownloadState target_state) { |
| + // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
| + CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + |
| + VLOG(20) << __FUNCTION__ << "() download = " << DebugString(true); |
| + DCHECK(IsInProgress()); |
| + |
| + download_util::RecordDownloadCount(download_util::CANCELLED_COUNT); |
|
Randy Smith (Not in Mondays)
2011/06/30 23:05:13
This recording is incorrect--it should be only in
Randy Smith (Not in Mondays)
2011/07/05 20:28:44
Done.
|
| + |
| + state_ = target_state; |
| + download_manager_->DownloadStopped(download_id_); |
| + UpdateObservers(); |
| + StopProgressTimer(); |
| +} |
| + |
| void DownloadItem::StartProgressTimer() { |
| // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
| CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| @@ -371,27 +386,35 @@ void DownloadItem::Update(int64 bytes_so_far) { |
| UpdateObservers(); |
| } |
| -// Triggered by a user action. |
| -void DownloadItem::Cancel(bool update_history) { |
| + |
| +// May be triggered by user action or because of various kinds of error. |
| +// Note that a cancel that occurs before a downoad item is persisted |
|
Randy Smith (Not in Mondays)
2011/06/30 23:05:13
"download"
Randy Smith (Not in Mondays)
2011/07/05 20:28:44
Done.
|
| +// into the history system will result in a removal of the Download |
| +// from the system. |
| +void DownloadItem::Cancel() { |
| // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
| CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| - |
| VLOG(20) << __FUNCTION__ << "() download = " << DebugString(true); |
| - if (!IsPartialDownload()) { |
| - // Small downloads might be complete before this method has |
| - // a chance to run. |
| + |
| + // Small downloads might be complete before we have a chance to run. |
| + if (!IsInProgress()) |
| return; |
| - } |
| + |
| + StopInternal(CANCELLED); |
| download_util::RecordDownloadCount(download_util::CANCELLED_COUNT); |
| - state_ = CANCELLED; |
| - UpdateObservers(); |
| - StopProgressTimer(); |
| - if (update_history) |
| - download_manager_->DownloadCancelled(download_id_); |
| + // History insertion is the point at which we have finalized download |
| + // details and persist them if something goes wrong. Before history |
| + // insertion, interrupt or cancel results in download removal. |
| + if (db_handle() == DownloadHistory::kUninitializedHandle) { |
| + download_manager_->RemoveDownload(this); |
| + // We are now deleted. |
| + return; |
| + } |
| } |
| + |
| void DownloadItem::MarkAsComplete() { |
| // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
| CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| @@ -491,20 +514,32 @@ void DownloadItem::Observe(NotificationType type, |
| Completed(); |
| } |
| -void DownloadItem::Interrupted(int64 size, int os_error) { |
| +void DownloadItem::Interrupt(int64 size, int os_error) { |
| // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
| CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + VLOG(20) << __FUNCTION__ << "() download = " << DebugString(true); |
| + // Small downloads might be complete before we have a chance to run. |
| if (!IsInProgress()) |
| return; |
| - state_ = INTERRUPTED; |
| - last_os_error_ = os_error; |
| + |
| UpdateSize(size); |
| - StopProgressTimer(); |
| + last_os_error_ = os_error; |
| + |
| + StopInternal(INTERRUPTED); |
| + |
| download_util::RecordDownloadInterrupted(os_error, |
| received_bytes_, |
| total_bytes_); |
| - UpdateObservers(); |
| + |
| + // History insertion is the point at which we have finalized download |
| + // details and persist them if something goes wrong. Before history |
| + // insertion, interrupt or cancel results in download removal. |
| + if (db_handle() == DownloadHistory::kUninitializedHandle) { |
| + download_manager_->RemoveDownload(this); |
| + // We are now deleted. |
| + return; |
| + } |
| } |
| void DownloadItem::Delete(DeleteReason reason) { |
| @@ -535,11 +570,11 @@ void DownloadItem::Remove() { |
| CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| download_manager_->AssertQueueStateConsistent(this); |
| - Cancel(true); |
| + if (IsInProgress()) |
| + StopInternal(CANCELLED); |
| download_manager_->AssertQueueStateConsistent(this); |
| - state_ = REMOVING; |
| - download_manager_->RemoveDownload(db_handle_); |
| + download_manager_->RemoveDownload(this); |
| // We have now been deleted. |
| } |