Bug 1344822 Remove downloads.onChanged hack
This bug highlights a difference between "legacy" downloads and those
created directly from Downloads.jsm, that the onDownloadCreated event for
legacy downloads comes when the download is already in progress while the
same event for non-legacy downloads comes before the actual transfer starts.
A clumsy workaround was originally used to avoid generating nonsense
onChanged events for non-legacy downloads but if we are just careful to
handle downloads that have not yet started, we don't need the workaround.
MozReview-Commit-ID: 7QyKvnR5ZMN
--- a/toolkit/components/extensions/ext-downloads.js
+++ b/toolkit/components/extensions/ext-downloads.js
@@ -69,17 +69,17 @@ class DownloadItem {
get paused() {
return this.download.canceled && this.download.hasPartialData && !this.download.error;
}
get canResume() {
return (this.download.stopped || this.download.canceled) &&
this.download.hasPartialData && !this.download.error;
}
get error() {
- if (!this.download.stopped || this.download.succeeded) {
+ if (!this.download.startTime || !this.download.stopped || this.download.succeeded) {
return null;
}
// TODO store this instead of calculating it
if (this.download.error) {
if (this.download.error.becauseSourceFailed) {
return "NETWORK_FAILED"; // TODO
}
@@ -121,17 +121,17 @@ class DownloadItem {
}
return obj;
}
// When a change event fires, handlers can look at how an individual
// field changed by comparing item.fieldname with item.prechange.fieldname.
// After all handlers have been invoked, this gets called to store the
// current values of all fields ahead of the next event.
- _change() {
+ _storePrechange() {
for (let field of DOWNLOAD_ITEM_CHANGE_FIELDS) {
this.prechange[field] = this[field];
}
}
}
// DownloadMap maps back and forth betwen the numeric identifiers used in
@@ -151,38 +151,35 @@ const DownloadMap = {
if (this.loadPromise == null) {
EventEmitter.decorate(this);
this.loadPromise = Downloads.getList(Downloads.ALL).then(list => {
let self = this;
return list.addView({
onDownloadAdded(download) {
const item = self.newFromDownload(download, null);
self.emit("create", item);
+ item._storePrechange();
},
onDownloadRemoved(download) {
const item = self.byDownload.get(download);
if (item != null) {
self.emit("erase", item);
self.byDownload.delete(download);
self.byId.delete(item.id);
}
},
onDownloadChanged(download) {
const item = self.byDownload.get(download);
if (item == null) {
Cu.reportError("Got onDownloadChanged for unknown download object");
} else {
- // We get the first one of these when the download is started.
- // In this case, don't emit anything, just initialize prechange.
- if (Object.keys(item.prechange).length > 0) {
- self.emit("change", item);
- }
- item._change();
+ self.emit("change", item);
+ item._storePrechange();
}
},
}).then(() => list.getAll())
.then(downloads => {
downloads.forEach(download => {
this.newFromDownload(download, null);
});
})