Bug 899125 - Part 1 of 2 - Allow using the JavaScript API instead of nsIDownloadManager when clearing recent history. r=paolo

This commit is contained in:
Neil Deakin 2013-08-17 15:19:55 +02:00
parent 4d786fea72
commit a8fbb09a43

View File

@ -8,9 +8,15 @@ XPCOMUtils.defineLazyModuleGetter(this, "PlacesUtils",
"resource://gre/modules/PlacesUtils.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "FormHistory",
"resource://gre/modules/FormHistory.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "Downloads",
"resource://gre/modules/Downloads.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "Promise",
"resource://gre/modules/commonjs/sdk/core/promise.js");
XPCOMUtils.defineLazyModuleGetter(this, "Task",
"resource://gre/modules/Task.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "DownloadsCommon",
"resource:///modules/DownloadsCommon.jsm");
function Sanitizer() {}
Sanitizer.prototype = {
// warning to the caller: this one may raise an exception (e.g. bug #265028)
@ -303,47 +309,47 @@ Sanitizer.prototype = {
downloads: {
clear: function ()
{
var dlMgr = Components.classes["@mozilla.org/download-manager;1"]
.getService(Components.interfaces.nsIDownloadManager);
if (DownloadsCommon.useJSTransfer) {
Task.spawn(function () {
let filterByTime = this.range ?
(download => download.startTime >= this.range[0] &&
download.startTime <= this.range[1]) : null;
var dlsToRemove = [];
if (this.range) {
// First, remove the completed/cancelled downloads
dlMgr.removeDownloadsByTimeframe(this.range[0], this.range[1]);
// Clear all completed/cancelled downloads
let publicList = yield Downloads.getPublicDownloadList();
publicList.removeFinished(filterByTime);
// Queue up any active downloads that started in the time span as well
for (let dlsEnum of [dlMgr.activeDownloads, dlMgr.activePrivateDownloads]) {
while (dlsEnum.hasMoreElements()) {
var dl = dlsEnum.next();
if (dl.startTime >= this.range[0])
dlsToRemove.push(dl);
}
}
let privateList = yield Downloads.getPrivateDownloadList();
privateList.removeFinished(filterByTime);
}.bind(this)).then(null, Cu.reportError);
}
else {
// Clear all completed/cancelled downloads
dlMgr.cleanUp();
dlMgr.cleanUpPrivate();
// Queue up all active ones as well
for (let dlsEnum of [dlMgr.activeDownloads, dlMgr.activePrivateDownloads]) {
while (dlsEnum.hasMoreElements()) {
dlsToRemove.push(dlsEnum.next());
}
var dlMgr = Components.classes["@mozilla.org/download-manager;1"]
.getService(Components.interfaces.nsIDownloadManager);
if (this.range) {
// First, remove the completed/cancelled downloads
dlMgr.removeDownloadsByTimeframe(this.range[0], this.range[1]);
}
else {
// Clear all completed/cancelled downloads
dlMgr.cleanUp();
dlMgr.cleanUpPrivate();
}
}
// Remove any queued up active downloads
dlsToRemove.forEach(function (dl) {
dl.remove();
});
},
get canClear()
canClear : function(aCallback, aArg)
{
var dlMgr = Components.classes["@mozilla.org/download-manager;1"]
.getService(Components.interfaces.nsIDownloadManager);
return dlMgr.canCleanUp || dlMgr.canCleanUpPrivate;
if (DownloadsCommon.useJSTransfer) {
aCallback("downloads", true, aArg);
}
else {
var dlMgr = Components.classes["@mozilla.org/download-manager;1"]
.getService(Components.interfaces.nsIDownloadManager);
aCallback("downloads", dlMgr.canCleanUp || dlMgr.canCleanUpPrivate, aArg);
}
return false;
}
},