Bug 696911 - In case the downloaded file is opened automatically with the default application, hide the download notification. It is let visible if the download is completed while in background. r=margaret

This commit is contained in:
Federico Paolinelli 2013-05-30 08:10:01 -04:00
parent 4d64543998
commit e0ec3b2acf

View File

@ -16,6 +16,7 @@ var Downloads = {
_dlmgr: null,
_progressAlert: null,
_privateDownloads: [],
isForeground : true,
_getLocalFile: function dl__getLocalFile(aFileURI) {
// if this is a URL, get the file from that
@ -33,8 +34,9 @@ var Downloads = {
this._dlmgr = Cc["@mozilla.org/download-manager;1"].getService(Ci.nsIDownloadManager);
this._progressAlert = new AlertDownloadProgressListener();
this._dlmgr.addPrivacyAwareListener(this._progressAlert);
Services.obs.addObserver(this, "xpcom-shutdown", true);
Services.obs.addObserver(this, "last-pb-context-exited", true);
Services.obs.addObserver(this, "application-background", false);
Services.obs.addObserver(this, "application-foreground", false);
},
openDownload: function dl_openDownload(aDownload) {
@ -105,16 +107,29 @@ var Downloads = {
// observer for last-pb-context-exited
observe: function dl_observe(aSubject, aTopic, aData) {
let alertsService = Cc["@mozilla.org/alerts-service;1"].getService(Ci.nsIAlertsService);
let progressListener = alertsService.QueryInterface(Ci.nsIAlertsProgressListener);
let download;
while ((download = this._privateDownloads.pop())) {
try {
let notificationName = download.target.spec.replace("file:", "download:");
progressListener.onCancel(notificationName);
} catch (e) {
dump("Error removing private download: " + e);
switch (aTopic) {
case "last-pb-context-exited": {
let alertsService = Cc["@mozilla.org/alerts-service;1"].getService(Ci.nsIAlertsService);
let progressListener = alertsService.QueryInterface(Ci.nsIAlertsProgressListener);
let download;
while ((download = this._privateDownloads.pop())) {
try {
let notificationName = download.target.spec.replace("file:", "download:");
progressListener.onCancel(notificationName);
} catch (e) {
dump("Error removing private download: " + e);
}
}
break;
}
case "application-foreground":
this.isForeground = true;
break;
case "application-background":
this.isForeground = false;
break;
}
},
@ -183,7 +198,10 @@ AlertDownloadProgressListener.prototype = {
}
}
if (state == Ci.nsIDownloadManager.DOWNLOAD_FINISHED) {
// We want to show the download finished notification only if it is not automatically opened.
// A download is automatically opened if it has a default handler and fennec is in foreground.
if (state == Ci.nsIDownloadManager.DOWNLOAD_FINISHED &&
!(aDownload.MIMEInfo.hasDefaultHandler && Downloads.isForeground)) {
Downloads.showAlert(aDownload, Strings.browser.GetStringFromName("alertDownloadsDone2"),
aDownload.displayName);
}