I have a simple plugin that intercepts new downloads using Downloads.jsm and Task.jsm as suggested in my previous question:
Intercept new downloads in Firefox Addon SDK
The idea is that the add-on should perform the following:
- Intercept a download
- Cancel the download
- Delete any partial data already downloaded
- Remove the download from the download history list
- Send the download info to an external download manager
Using the following code I am able to intercept a download and get it's information such as source and destination but cancelling and removing the download is causing me a few problems.
const {Cu} = require("chrome");
Cu.import("resource://gre/modules/Downloads.jsm");
Cu.import("resource://gre/modules/Task.jsm");
Task.spawn(function() {
let list = yield Downloads.getList(Downloads.ALL);
let view = {
onDownloadAdded: download => {
console.log("Added", download);
// cancel the download
download.cancel();
// finialize (remove partial data)
download.finalize(true);
// delete the partial data
download.removePartialData();
// remove it from the list
list.remove(download);
},
onDownloadChanged: download => console.log("Changed", download),
onDownloadRemoved: download => console.log("Removed", download)
};
yield list.addView(view);
});
The DownloadList
and Download
objects expose all the functions that I should need but something is not working as expected.
Although the download is removed from the Firefox download list and in the Download Library I can see the download as "Cancelled" the call to finalize(true)
does not remove the partial file downloads (neither does removePartialData
).
I believe this is because the downloads are not being properly deleted. Even though they are shown as cancelled in the Library if I browse to my downloads folder I can see the .part file growing as if the download was still in progress.
I suspect this might be because I am trying to cancel the download before it really starts so perhaps the download is not processing the cancel()
call properly?