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
authorFederico Paolinelli <fedepaol@gmail.com>
Thu, 30 May 2013 08:10:01 -0400
changeset 133437 684cbf5daa8079a996ffd878de12e8b747aefba3
parent 133436 4fe5735bca9e279ff571e6a308374303d1ab44f4
child 133438 4ea75f8edf0b8cb8e6e362eae9ff709dd5b5321a
push id24751
push userryanvm@gmail.com
push dateThu, 30 May 2013 15:43:11 +0000
treeherdermozilla-central@75407626ba46 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmargaret
bugs696911
milestone24.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
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
mobile/android/chrome/content/downloads.js
--- a/mobile/android/chrome/content/downloads.js
+++ b/mobile/android/chrome/content/downloads.js
@@ -11,16 +11,17 @@ function dump(a) {
 
 const URI_GENERIC_ICON_DOWNLOAD = "drawable://alert_download";
 
 var Downloads = {
   _initialized: false,
   _dlmgr: null,
   _progressAlert: null,
   _privateDownloads: [],
+  isForeground : true,
 
   _getLocalFile: function dl__getLocalFile(aFileURI) {
     // if this is a URL, get the file from that
     // XXX it's possible that using a null char-set here is bad
     const fileUrl = Services.io.newURI(aFileURI, null, null).QueryInterface(Ci.nsIFileURL);
     return fileUrl.file.clone().QueryInterface(Ci.nsILocalFile);
   },
 
@@ -28,18 +29,19 @@ var Downloads = {
     if (this._initialized)
       return;
     this._initialized = true;
 
     // Monitor downloads and display alerts
     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) {
     let fileUri = aDownload.target.spec;
     let guid = aDownload.guid;
     let f = this._getLocalFile(fileUri);
     try {
       f.launch();
@@ -100,26 +102,39 @@ var Downloads = {
 
     var notifier = Cc["@mozilla.org/alerts-service;1"].getService(Ci.nsIAlertsService);
     notifier.showAlertNotification(aIcon, aTitle, aMessage, true, "", observer,
                                    aDownload.target.spec.replace("file:", "download:"));
   },
 
   // 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;
     }
   },
 
   QueryInterface: function (aIID) {
     if (!aIID.equals(Ci.nsISupports) &&
         !aIID.equals(Ci.nsIObserver) &&
         !aIID.equals(Ci.nsISupportsWeakReference))
       throw Components.results.NS_ERROR_NO_INTERFACE;
@@ -178,17 +193,20 @@ AlertDownloadProgressListener.prototype 
 
         if (aDownload.isPrivate) {
           let index = this._privateDownloads.indexOf(aDownload);
           if (index != -1) {
             this._privateDownloads.splice(index, 1);
           }
         }
 
-        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);
         }
         break;
       }
     }
   },