[PALEMOON] Bug 1115983 - Keep only minimal state information in the DataItem

This commit is contained in:
janekptacijarabaci 2018-07-29 08:40:06 +02:00 • committed by Roy Tam
commit 23f4f8a235
3 changed files with 118 additions and 209 deletions

View file

@ -57,6 +57,8 @@ XPCOMUtils.defineLazyModuleGetter(this, "DownloadUIHelper",
"resource://gre/modules/DownloadUIHelper.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "DownloadUtils",
"resource://gre/modules/DownloadUtils.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "FileUtils",
"resource://gre/modules/FileUtils.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "OS",
"resource://gre/modules/osfile.jsm")
XPCOMUtils.defineLazyModuleGetter(this, "PlacesUtils",
@ -94,11 +96,6 @@ const kDownloadsStringsRequiringPluralForm = {
otherDownloads2: true
};
XPCOMUtils.defineLazyGetter(this, "DownloadsLocalFileCtor", function () {
return Components.Constructor("@mozilla.org/file/local;1",
"nsILocalFile", "initWithPath");
});
const kPartialDownloadSuffix = ".part";
const kPrefBranch = Services.prefs.getBranch("browser.download.");
@ -431,12 +428,12 @@ this.DownloadsCommon = {
break;
case nsIDM.DOWNLOAD_DOWNLOADING:
summary.numDownloading++;
if (dataItem.maxBytes > 0 && dataItem.speed > 0) {
let sizeLeft = dataItem.maxBytes - dataItem.currBytes;
if (dataItem.maxBytes > 0 && dataItem.download.speed > 0) {
let sizeLeft = dataItem.maxBytes - dataItem.download.currentBytes;
summary.rawTimeLeft = Math.max(summary.rawTimeLeft,
sizeLeft / dataItem.speed);
sizeLeft / dataItem.download.speed);
summary.slowestSpeed = Math.min(summary.slowestSpeed,
dataItem.speed);
dataItem.download.speed);
}
break;
}
@ -445,7 +442,7 @@ this.DownloadsCommon = {
dataItem.state != nsIDM.DOWNLOAD_CANCELED &&
dataItem.state != nsIDM.DOWNLOAD_FAILED) {
summary.totalSize += dataItem.maxBytes;
summary.totalTransferred += dataItem.currBytes;
summary.totalTransferred += dataItem.download.currentBytes;
}
}
@ -574,7 +571,6 @@ this.DownloadsCommon = {
/**
* Show a downloaded file in the system file manager.
* If you have a dataItem, use dataItem.showLocalFile.
*
* @param aFile
* a downloaded file.
@ -793,7 +789,8 @@ DownloadsDataCtor.prototype = {
// RRR: Annotation service throws here. commented out for now.
/*PlacesUtils.annotations.setPageAnnotation(
NetUtil.newURI(aDataItem.uri), "downloads/metaData",
NetUtil.newURI(aDataItem.download.source.url),
"downloads/metaData",
JSON.stringify(downloadMetaData), 0,
PlacesUtils.annotations.EXPIRE_WITH_HISTORY);*/
} catch (ex) {
@ -873,7 +870,7 @@ DownloadsDataCtor.prototype = {
}
}
loadedItemsArray.sort(function(a, b) b.startTime - a.startTime);
loadedItemsArray.sort(function(a, b) b.download.startTime - a.download.startTime);
loadedItemsArray.forEach(
function (dataItem) aView.onDataItemAdded(dataItem, false)
);
@ -1364,12 +1361,9 @@ DownloadsDataItem.prototype = {
*/
_initFromJSDownload: function (aDownload)
{
this._download = aDownload;
this.download = aDownload;
this.downloadGuid = "id:" + this._autoIncrementId;
this.file = aDownload.target.path;
this.target = OS.Path.basename(aDownload.target.path);
this.uri = aDownload.source.url;
this.endTime = Date.now();
this.updateFromJSDownload();
@ -1381,41 +1375,35 @@ DownloadsDataItem.prototype = {
updateFromJSDownload: function ()
{
// Collapse state using the correct priority.
if (this._download.succeeded) {
if (this.download.succeeded) {
this.state = nsIDM.DOWNLOAD_FINISHED;
} else if (this._download.error &&
this._download.error.becauseBlockedByParentalControls) {
} else if (this.download.error &&
this.download.error.becauseBlockedByParentalControls) {
this.state = nsIDM.DOWNLOAD_BLOCKED_PARENTAL;
} else if (this._download.error) {
} else if (this.download.error) {
this.state = nsIDM.DOWNLOAD_FAILED;
} else if (this._download.canceled && this._download.hasPartialData) {
} else if (this.download.canceled && this.download.hasPartialData) {
this.state = nsIDM.DOWNLOAD_PAUSED;
} else if (this._download.canceled) {
} else if (this.download.canceled) {
this.state = nsIDM.DOWNLOAD_CANCELED;
} else if (this._download.stopped) {
} else if (this.download.stopped) {
this.state = nsIDM.DOWNLOAD_NOTSTARTED;
} else {
this.state = nsIDM.DOWNLOAD_DOWNLOADING;
}
this.referrer = this._download.source.referrer;
this.startTime = this._download.startTime;
this.currBytes = this._download.currentBytes;
this.resumable = this._download.hasPartialData;
this.speed = this._download.speed;
if (this._download.succeeded) {
if (this.download.succeeded) {
// If the download succeeded, show the final size if available, otherwise
// use the last known number of bytes transferred. The final size on disk
// will be available when bug 941063 is resolved.
this.maxBytes = this._download.hasProgress ?
this._download.totalBytes :
this._download.currentBytes;
this.maxBytes = this.download.hasProgress ?
this.download.totalBytes :
this.download.currentBytes;
this.percentComplete = 100;
} else if (this._download.hasProgress) {
} else if (this.download.hasProgress) {
// If the final size and progress are known, use them.
this.maxBytes = this._download.totalBytes;
this.percentComplete = this._download.progress;
this.maxBytes = this.download.totalBytes;
this.percentComplete = this.download.progress;
} else {
// The download final size and progress percentage is unknown.
this.maxBytes = -1;
@ -1580,14 +1568,6 @@ DownloadsDataItem.prototype = {
].indexOf(this.state) != -1;
},
/**
* Indicates whether the download is finished and can be opened.
*/
get openable()
{
return this.state == nsIDM.DOWNLOAD_FINISHED;
},
/**
* Indicates whether the download stopped because of an error, and can be
* resumed manually.
@ -1604,10 +1584,13 @@ DownloadsDataItem.prototype = {
* @throws if the native path is not valid. This can happen if the same
* profile is used on different platforms, for example if a native
* Windows path is stored and then the item is accessed on a Mac.
*
* @deprecated Callers should use OS.File and "download.target.path".
*/
get localFile()
{
return this._getFile(this.file);
// We should remove should use this.download.target.partFilePath and check asyncrhonously.
return new FileUtils.File(this.download.target.path);
},
/**
@ -1616,80 +1599,12 @@ DownloadsDataItem.prototype = {
* @throws if the native path is not valid. This can happen if the same
* profile is used on different platforms, for example if a native
* Windows path is stored and then the item is accessed on a Mac.
*
* @deprecated Callers should use OS.File and "download.target.partFilePath".
*/
get partFile()
{
return this._getFile(this.file + kPartialDownloadSuffix);
},
/**
* Returns an nsILocalFile for aFilename. aFilename might be a file URL or
* a native path.
*
* @param aFilename the filename of the file to retrieve.
* @return an nsILocalFile for the file.
* @throws if the native path is not valid. This can happen if the same
* profile is used on different platforms, for example if a native
* Windows path is stored and then the item is accessed on a Mac.
* @note This function makes no guarantees about the file's existence -
* callers should check that the returned file exists.
*/
_getFile: function DDI__getFile(aFilename)
{
// The download database may contain targets stored as file URLs or native
// paths. This can still be true for previously stored items, even if new
// items are stored using their file URL. See also bug 239948 comment 12.
if (aFilename.startsWith("file:")) {
// Assume the file URL we obtained from the downloads database or from the
// "spec" property of the target has the UTF-8 charset.
let fileUrl = NetUtil.newURI(aFilename).QueryInterface(Ci.nsIFileURL);
return fileUrl.file.clone().QueryInterface(Ci.nsILocalFile);
} else {
// The downloads database contains a native path. Try to create a local
// file, though this may throw an exception if the path is invalid.
return new DownloadsLocalFileCtor(aFilename);
}
},
/**
* Open the target file for this download.
*
* @param aOwnerWindow
* The window with which the required action is associated.
* @throws if the file cannot be opened.
*/
openLocalFile: function DDI_openLocalFile(aOwnerWindow) {
this._download.launch().then(null, Cu.reportError);
return;
},
/**
* Show the downloaded file in the system file manager.
*/
showLocalFile: function DDI_showLocalFile() {
DownloadsCommon.showDownloadedFile(this.localFile);
},
/**
* Resumes the download if paused, pauses it if active.
* @throws if the download is not resumable or if has already done.
*/
togglePauseResume: function DDI_togglePauseResume() {
if (this._download.stopped) {
this._download.start();
} else {
this._download.cancel();
}
return;
},
/**
* Attempts to retry the download.
* @throws if we cannot.
*/
retry: function DDI_retry() {
this._download.start();
return;
return new FileUtils.File(this.download.target.path + kPartialDownloadSuffix);
},
/**
@ -1705,29 +1620,6 @@ DownloadsDataItem.prototype = {
localFile.remove(false);
}
} catch (ex) { }
},
/**
* Cancels the download.
* @throws if the download is already done.
*/
cancel: function() {
this._download.cancel();
this._download.removePartialData().then(null, Cu.reportError);
return;
},
/**
* Remove the download.
*/
remove: function DDI_remove() {
let promiseList = this._download.source.isPrivate
? Downloads.getList(Downloads.PUBLIC)
: Downloads.getList(Downloads.PRIVATE);
promiseList.then(list => list.remove(this._download))
.then(() => this._download.finalize(true))
.then(null, Cu.reportError);
return;
}
};