Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4452)

Unified Diff: chrome/browser/download/download_item.cc

Issue 7294013: Modified cancel and interrupt processing to avoid race with history. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Merged to TOT. Again :-}. Created 9 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chrome/browser/download/download_item.h ('k') | chrome/browser/download/download_item_model.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/download/download_item.cc
diff --git a/chrome/browser/download/download_item.cc b/chrome/browser/download/download_item.cc
index 0c3dca16675ab9f03bee8be3beb5e699cb078464..d657140064e6e9aee25552789d1ab4d9afaca3a6 100644
--- a/chrome/browser/download/download_item.cc
+++ b/chrome/browser/download/download_item.cc
@@ -342,6 +342,20 @@ 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));
+ DCHECK(target_state == CANCELLED || target_state == INTERRUPTED);
+
+ VLOG(20) << __FUNCTION__ << "() download = " << DebugString(true);
+ DCHECK(IsInProgress());
+
+ 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));
@@ -372,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 download item is persisted
+// 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; no further code should be executed on this
+ // object.
+ }
}
+
void DownloadItem::MarkAsComplete() {
// TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved.
CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
@@ -492,20 +514,32 @@ void DownloadItem::Observe(int 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; no further code should be executed on this
+ // object.
+ }
}
void DownloadItem::Delete(DeleteReason reason) {
@@ -536,11 +570,14 @@ void DownloadItem::Remove() {
CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
download_manager_->AssertQueueStateConsistent(this);
- Cancel(true);
+ if (IsInProgress()) {
+ StopInternal(CANCELLED);
+ download_util::RecordDownloadCount(download_util::CANCELLED_COUNT);
+ }
download_manager_->AssertQueueStateConsistent(this);
+ download_util::RecordDownloadCount(download_util::REMOVED_COUNT);
- state_ = REMOVING;
- download_manager_->RemoveDownload(db_handle_);
+ download_manager_->RemoveDownload(this);
// We have now been deleted.
}
« no previous file with comments | « chrome/browser/download/download_item.h ('k') | chrome/browser/download/download_item_model.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698