Bug 905123 - Move prompts from DownloadIntegration to DownloadPrompter. r=paolo
authorAndres Hernandez <andres@appcoast.com>
Tue, 03 Sep 2013 22:01:42 -0600
changeset 158638 4880f320522131843c7b7fd302c47b75c32e0eeb
parent 158637 2f2a91e0445a27128dd27049f1e8e386ed9c2db2
child 158639 2e7256075903ff62ae3f62f03cb95885db467f28
push id2961
push userlsblakk@mozilla.com
push dateMon, 28 Oct 2013 21:59:28 +0000
treeherdermozilla-beta@73ef4f13486f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerspaolo
bugs905123
milestone26.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 905123 - Move prompts from DownloadIntegration to DownloadPrompter. r=paolo
toolkit/components/jsdownloads/src/DownloadIntegration.jsm
toolkit/components/jsdownloads/src/DownloadUIHelper.jsm
toolkit/components/jsdownloads/src/moz.build
--- a/toolkit/components/jsdownloads/src/DownloadIntegration.jsm
+++ b/toolkit/components/jsdownloads/src/DownloadIntegration.jsm
@@ -61,22 +61,16 @@ XPCOMUtils.defineLazyServiceGetter(this,
 XPCOMUtils.defineLazyGetter(this, "gParentalControlsService", function() {
   if ("@mozilla.org/parental-controls-service;1" in Cc) {
     return Cc["@mozilla.org/parental-controls-service;1"]
       .createInstance(Ci.nsIParentalControlsService);
   }
   return null;
 });
 
-// This will be replaced by "DownloadUIHelper.strings" (see bug 905123).
-XPCOMUtils.defineLazyGetter(this, "gStringBundle", function() {
-  return Services.strings.
-    createBundle("chrome://mozapps/locale/downloads/downloads.properties");
-});
-
 const Timer = Components.Constructor("@mozilla.org/timer;1", "nsITimer",
                                      "initWithCallback");
 
 /**
  * Indicates the delay between a change to the downloads data and the related
  * save operation.  This value is the result of a delicate trade-off, assuming
  * the host application uses the browser history instead of the download store
  * to save completed downloads.
@@ -719,109 +713,65 @@ this.DownloadObserver = {
         downloadsSet.delete(aDownload);
       }
     };
 
     aList.addView(downloadsView);
   },
 
   /**
-   * Shows the confirm cancel downloads dialog.
+   * Wrapper that handles the test mode before calling the prompt that display
+   * a warning message box that informs that there are active downloads,
+   * and asks whether the user wants to cancel them or not.
    *
    * @param aCancel
    *        The observer notification subject.
    * @param aDownloadsCount
    *        The current downloads count.
-   * @param aIdTitle
-   *        The string bundle id for the dialog title.
-   * @param aIdMessageSingle
-   *        The string bundle id for the single download message.
-   * @param aIdMessageMultiple
-   *        The string bundle id for the multiple downloads message.
-   * @param aIdButton
-   *        The string bundle id for the don't cancel button text.
+   * @param aPrompter
+   *        The prompter object that shows the confirm dialog.
+   * @param aPromptType
+   *        The type of prompt notification depending on the observer.
    */
   _confirmCancelDownloads: function DO_confirmCancelDownload(
-    aCancel, aDownloadsCount, aIdTitle, aIdMessageSingle, aIdMessageMultiple, aIdButton) {
+    aCancel, aDownloadsCount, aPrompter, aPromptType) {
     // If user has already dismissed the request, then do nothing.
     if ((aCancel instanceof Ci.nsISupportsPRBool) && aCancel.data) {
       return;
     }
     // Handle test mode
     if (DownloadIntegration.testMode) {
       DownloadIntegration.testPromptDownloads = aDownloadsCount;
       return;
     }
-    // If there are no active downloads, then do nothing.
-    if (aDownloadsCount <= 0) {
-      return;
-    }
 
-    let win = Services.wm.getMostRecentWindow("navigator:browser");
-    let buttonFlags = (Ci.nsIPrompt.BUTTON_TITLE_IS_STRING * Ci.nsIPrompt.BUTTON_POS_0) +
-                      (Ci.nsIPrompt.BUTTON_TITLE_IS_STRING * Ci.nsIPrompt.BUTTON_POS_1);
-    let title = gStringBundle.GetStringFromName(aIdTitle);
-    let dontQuitButton = gStringBundle.GetStringFromName(aIdButton);
-    let quitButton;
-    let message;
-
-    if (aDownloadsCount > 1) {
-      message = gStringBundle.formatStringFromName(aIdMessageMultiple,
-                                                   [aDownloadsCount], 1);
-      quitButton = gStringBundle.formatStringFromName("cancelDownloadsOKTextMultiple",
-                                                      [aDownloadsCount], 1);
-    } else {
-      message = gStringBundle.GetStringFromName(aIdMessageSingle);
-      quitButton = gStringBundle.GetStringFromName("cancelDownloadsOKText");
-    }
-
-    let rv = Services.prompt.confirmEx(win, title, message, buttonFlags,
-                                       quitButton, dontQuitButton, null, null, {});
-    aCancel.data = (rv == 1);
+    aCancel.data = aPrompter.confirmCancelDownloads(aDownloadsCount, aPromptType);
   },
 
   ////////////////////////////////////////////////////////////////////////////
   //// nsIObserver
 
   observe: function DO_observe(aSubject, aTopic, aData) {
     let downloadsCount;
+    let p = DownloadUIHelper.getPrompter();
     switch (aTopic) {
       case "quit-application-requested":
         downloadsCount = this._publicInProgressDownloads.size +
                          this._privateInProgressDownloads.size;
-#ifndef XP_MACOSX
-        this._confirmCancelDownloads(aSubject, downloadsCount,
-                                     "quitCancelDownloadsAlertTitle",
-                                     "quitCancelDownloadsAlertMsg",
-                                     "quitCancelDownloadsAlertMsgMultiple",
-                                     "dontQuitButtonWin");
-#else
-        this._confirmCancelDownloads(aSubject, downloadsCount,
-                                     "quitCancelDownloadsAlertTitle",
-                                     "quitCancelDownloadsAlertMsgMac",
-                                     "quitCancelDownloadsAlertMsgMacMultiple",
-                                     "dontQuitButtonMac");
-#endif
+        this._confirmCancelDownloads(aSubject, downloadsCount, p, p.ON_QUIT);
         break;
       case "offline-requested":
         downloadsCount = this._publicInProgressDownloads.size +
                          this._privateInProgressDownloads.size;
-        this._confirmCancelDownloads(aSubject, downloadsCount,
-                                     "offlineCancelDownloadsAlertTitle",
-                                     "offlineCancelDownloadsAlertMsg",
-                                     "offlineCancelDownloadsAlertMsgMultiple",
-                                     "dontGoOfflineButton");
+        this._confirmCancelDownloads(aSubject, downloadsCount, p, p.ON_OFFLINE);
         break;
       case "last-pb-context-exiting":
-        this._confirmCancelDownloads(aSubject,
-                                     this._privateInProgressDownloads.size,
-                                     "leavePrivateBrowsingCancelDownloadsAlertTitle",
-                                     "leavePrivateBrowsingWindowsCancelDownloadsAlertMsg",
-                                     "leavePrivateBrowsingWindowsCancelDownloadsAlertMsgMultiple",
-                                     "dontLeavePrivateBrowsingButton");
+        downloadsCount = this._privateInProgressDownloads.size;
+        this._confirmCancelDownloads(aSubject, downloadsCount, p,
+                                     p.ON_LEAVE_PRIVATE_BROWSING);
         break;
     }
   },
 
   ////////////////////////////////////////////////////////////////////////////
   //// nsISupports
 
   QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver,
--- a/toolkit/components/jsdownloads/src/DownloadUIHelper.jsm
+++ b/toolkit/components/jsdownloads/src/DownloadUIHelper.jsm
@@ -31,16 +31,21 @@ XPCOMUtils.defineLazyModuleGetter(this, 
 XPCOMUtils.defineLazyModuleGetter(this, "Services",
                                   "resource://gre/modules/Services.jsm");
 
 const kStringBundleUrl =
   "chrome://mozapps/locale/downloads/downloads.properties";
 
 const kStringsRequiringFormatting = {
   fileExecutableSecurityWarning: true,
+  cancelDownloadsOKTextMultiple: true,
+  quitCancelDownloadsAlertMsgMultiple: true,
+  quitCancelDownloadsAlertMsgMacMultiple: true,
+  offlineCancelDownloadsAlertMsgMultiple: true,
+  leavePrivateBrowsingWindowsCancelDownloadsAlertMsgMultiple: true
 };
 
 ////////////////////////////////////////////////////////////////////////////////
 //// DownloadUIHelper
 
 /**
  * Provides functions to handle status and messages in the user interface.
  */
@@ -103,16 +108,23 @@ XPCOMUtils.defineLazyGetter(DownloadUIHe
  */
 function DownloadPrompter(aParent)
 {
   this._prompter = Services.ww.getNewPrompter(aParent);
 }
 
 DownloadPrompter.prototype = {
   /**
+   * Constants with the different type of prompts.
+   */
+  ON_QUIT: "prompt-on-quit",
+  ON_OFFLINE: "prompt-on-offline",
+  ON_LEAVE_PRIVATE_BROWSING: "prompt-on-leave-private-browsing",
+
+  /**
    * nsIPrompt instance for displaying messages.
    */
   _prompter: null,
 
   /**
    * Displays a warning message box that informs that the specified file is
    * executable, and asks whether the user wants to launch it.  The user is
    * given the option of disabling future instances of this warning.
@@ -151,9 +163,72 @@ DownloadPrompter.prototype = {
         Services.prefs.setBoolPref(kPrefAlertOnEXEOpen, !checkState.value);
       }
 
       return Promise.resolve(shouldLaunch);
     } catch (ex) {
       return Promise.reject(ex);
     }
   },
+
+  /**
+   * Displays a warning message box that informs that there are active
+   * downloads, and asks whether the user wants to cancel them or not.
+   *
+   * @param aDownloadsCount
+   *        The current downloads count.
+   * @param aPromptType
+   *        The type of prompt notification depending on the observer.
+   *
+   * @return True to cancel the downloads and continue, false to abort the
+   *         operation.
+   */
+  confirmCancelDownloads: function DP_confirmCancelDownload(aDownloadsCount,
+                                                            aPromptType)
+  {
+    // If there are no active downloads, then do nothing.
+    if (aDownloadsCount <= 0) {
+      return false;
+    }
+
+    let s = DownloadUIHelper.strings;
+    let buttonFlags = (Ci.nsIPrompt.BUTTON_TITLE_IS_STRING * Ci.nsIPrompt.BUTTON_POS_0) +
+                      (Ci.nsIPrompt.BUTTON_TITLE_IS_STRING * Ci.nsIPrompt.BUTTON_POS_1);
+    let okButton = aDownloadsCount > 1 ? s.cancelDownloadsOKTextMultiple(aDownloadsCount)
+                                       : s.cancelDownloadsOKText;
+    let title, message, cancelButton;
+
+    switch (aPromptType) {
+      case this.ON_QUIT:
+        title = s.quitCancelDownloadsAlertTitle;
+#ifndef XP_MACOSX
+        message = aDownloadsCount > 1
+                  ? s.quitCancelDownloadsAlertMsgMultiple(aDownloadsCount)
+                  : s.quitCancelDownloadsAlertMsg;
+        cancelButton = s.dontQuitButtonWin;
+#else
+        message = aDownloadsCount > 1
+                  ? s.quitCancelDownloadsAlertMsgMacMultiple(aDownloadsCount)
+                  : s.quitCancelDownloadsAlertMsgMac;
+        cancelButton = s.dontQuitButtonMac;
+#endif
+        break;
+      case this.ON_OFFLINE:
+        title = s.offlineCancelDownloadsAlertTitle;
+        message = aDownloadsCount > 1
+                  ? s.offlineCancelDownloadsAlertMsgMultiple(aDownloadsCount)
+                  : s.offlineCancelDownloadsAlertMsg;
+        cancelButton = s.dontGoOfflineButton;
+        break;
+      case this.ON_LEAVE_PRIVATE_BROWSING:
+        title = s.leavePrivateBrowsingCancelDownloadsAlertTitle;
+        message = aDownloadsCount > 1
+                  ? s.leavePrivateBrowsingWindowsCancelDownloadsAlertMsgMultiple(aDownloadsCount)
+                  : s.leavePrivateBrowsingWindowsCancelDownloadsAlertMsg;
+        cancelButton = s.dontLeavePrivateBrowsingButton;
+        break;
+    }
+
+    let rv = this._prompter.confirmEx(title, message, buttonFlags, okButton,
+                                      cancelButton, null, null, {});
+    return (rv == 1);
+  }
 };
--- a/toolkit/components/jsdownloads/src/moz.build
+++ b/toolkit/components/jsdownloads/src/moz.build
@@ -19,16 +19,15 @@ EXTRA_COMPONENTS += [
     'Downloads.manifest',
 ]
 
 EXTRA_JS_MODULES += [
     'DownloadCore.jsm',
     'DownloadImport.jsm',
     'DownloadList.jsm',
     'DownloadStore.jsm',
-    'DownloadUIHelper.jsm',
     'Downloads.jsm',
 ]
 
 EXTRA_PP_JS_MODULES += [
     'DownloadIntegration.jsm',
+    'DownloadUIHelper.jsm',
 ]
-