Bug 1198181 - Part 5 - Change the unblock dialog actions based on the verdict. r=past
authorPaolo Amadini <paolo.mozmail@amadzone.org>
Wed, 20 Apr 2016 11:16:00 +0100
changeset 294107 19b8851d8d4c19997ecc73960f4de8d90c981c28
parent 294106 597552fedbde3218c35b226786cd83aa1376cc68
child 294108 fd5a45ba0257ec668f55da824e4384b8755eda32
push id75435
push userkwierso@gmail.com
push dateWed, 20 Apr 2016 21:19:31 +0000
treeherdermozilla-inbound@898c9f87a4c4 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerspast
bugs1198181
milestone48.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 1198181 - Part 5 - Change the unblock dialog actions based on the verdict. r=past MozReview-Commit-ID: 3NkvpI9EqpB
browser/components/downloads/DownloadsCommon.jsm
browser/components/downloads/DownloadsViewUI.jsm
browser/components/downloads/content/allDownloadsViewOverlay.js
browser/components/downloads/content/downloads.js
browser/components/downloads/test/browser/browser_confirm_unblock_download.js
--- a/browser/components/downloads/DownloadsCommon.jsm
+++ b/browser/components/downloads/DownloadsCommon.jsm
@@ -523,39 +523,46 @@ this.DownloadsCommon = {
    *
    * @param aVerdict
    *        The detailed reason why the download was blocked, according to the
    *        "Downloads.Error.BLOCK_VERDICT_" constants. If an unknown reason is
    *        specified, "Downloads.Error.BLOCK_VERDICT_MALWARE" is assumed.
    * @param aOwnerWindow
    *        The window with which this action is associated.
    *
-   * @return True to unblock the file, false to keep the user safe and
-   *         cancel the operation.
+   * @return {Promise}
+   * @resolves String representing the action that should be executed:
+   *            - "unblock" to allow the download without opening the file.
+   *            - "confirmBlock" to delete the blocked data permanently.
+   *            - "cancel" to do nothing and cancel the operation.
    */
   confirmUnblockDownload: Task.async(function* (aVerdict, aOwnerWindow) {
     let s = DownloadsCommon.strings;
     let title = s.unblockHeader;
     let buttonFlags = (Ci.nsIPrompt.BUTTON_TITLE_IS_STRING * Ci.nsIPrompt.BUTTON_POS_0) +
-                      (Ci.nsIPrompt.BUTTON_TITLE_IS_STRING * Ci.nsIPrompt.BUTTON_POS_1) +
-                      Ci.nsIPrompt.BUTTON_POS_1_DEFAULT;
+                      (Ci.nsIPrompt.BUTTON_TITLE_CANCEL * Ci.nsIPrompt.BUTTON_POS_1);
     let type = "";
     let message = s.unblockTip;
-    let okButton = s.unblockButtonContinue;
-    let cancelButton = s.unblockButtonCancel;
+    let unblockButton = s.unblockButtonContinue;
+    let confirmBlockButton = s.unblockButtonCancel;
 
     switch (aVerdict) {
       case Downloads.Error.BLOCK_VERDICT_UNCOMMON:
         type = s.unblockTypeUncommon;
+        buttonFlags += (Ci.nsIPrompt.BUTTON_TITLE_IS_STRING * Ci.nsIPrompt.BUTTON_POS_2) +
+                       Ci.nsIPrompt.BUTTON_POS_0_DEFAULT;
         break;
       case Downloads.Error.BLOCK_VERDICT_POTENTIALLY_UNWANTED:
         type = s.unblockTypePotentiallyUnwanted;
+        buttonFlags += (Ci.nsIPrompt.BUTTON_TITLE_IS_STRING * Ci.nsIPrompt.BUTTON_POS_2) +
+                       Ci.nsIPrompt.BUTTON_POS_2_DEFAULT;
         break;
       default: // Assume Downloads.Error.BLOCK_VERDICT_MALWARE
         type = s.unblockTypeMalware;
+        buttonFlags += Ci.nsIPrompt.BUTTON_POS_1_DEFAULT;
         break;
     }
 
     if (type) {
       message = type + "\n\n" + message;
     }
 
     Services.ww.registerNotification(function onOpen(subj, topic) {
@@ -575,18 +582,19 @@ this.DownloadsCommon = {
           }
         });
       }
     });
 
     // The ordering of the ok/cancel buttons is used this way to allow "cancel"
     // to have the same result as hitting the ESC or Close button (see bug 345067).
     let rv = Services.prompt.confirmEx(aOwnerWindow, title, message, buttonFlags,
-                                       okButton, cancelButton, null, null, {});
-    return (rv == 0);
+                                       unblockButton, null, confirmBlockButton,
+                                       null, {});
+    return ["unblock", "cancel", "confirmBlock"][rv];
   }),
 };
 
 XPCOMUtils.defineLazyGetter(this.DownloadsCommon, "log", () => {
   return DownloadsLogger.log.bind(DownloadsLogger);
 });
 XPCOMUtils.defineLazyGetter(this.DownloadsCommon, "error", () => {
   return DownloadsLogger.error.bind(DownloadsLogger);
--- a/browser/components/downloads/DownloadsViewUI.jsm
+++ b/browser/components/downloads/DownloadsViewUI.jsm
@@ -260,16 +260,35 @@ this.DownloadsViewUI.DownloadElementShel
       text = s.statusSeparator(firstPart, displayDate);
       tip = s.statusSeparator(fullHost, fullDate);
     }
 
     return { text, tip: tip || text };
   },
 
   /**
+   * Shows the appropriate unblock dialog based on the verdict, and executes the
+   * action selected by the user in the dialog, which may involve unblocking,
+   * opening or removing the file.
+   *
+   * @param window
+   *        The window to which the dialog should be anchored.
+   */
+  confirmUnblock(window) {
+    let verdict = this.download.error.reputationCheckVerdict;
+    DownloadsCommon.confirmUnblockDownload(verdict, window).then(action => {
+      if (action == "unblock") {
+        return this.download.unblock();
+      } else if (action == "confirmBlock") {
+        return this.download.confirmBlock();
+      }
+    }).catch(Cu.reportError);
+  },
+
+  /**
    * Returns the name of the default command to use for the current state of the
    * download, when there is a double click or another default interaction. If
    * there is no default command for the current state, returns an empty string.
    * The commands are implemented as functions on this object or derived ones.
    */
   get currentDefaultCommandName() {
     switch (DownloadsCommon.stateOfDownload(this.download)) {
       case Ci.nsIDownloadManager.DOWNLOAD_NOTSTARTED:
--- a/browser/components/downloads/content/allDownloadsViewOverlay.js
+++ b/browser/components/downloads/content/allDownloadsViewOverlay.js
@@ -372,22 +372,17 @@ HistoryDownloadElementShell.prototype = 
     }
     if (this._historyDownload) {
       let uri = NetUtil.newURI(this.download.source.url);
       PlacesUtils.bhistory.removePage(uri);
     }
   },
 
   downloadsCmd_unblock() {
-    let verdict = this.download.error.reputationCheckVerdict;
-    DownloadsCommon.confirmUnblockDownload(verdict, window).then(confirmed => {
-      if (confirmed) {
-        return this.download.unblock();
-      }
-    }).catch(Cu.reportError);
+    this.confirmUnblock(window);
   },
 
   // Returns whether or not the download handled by this shell should
   // show up in the search results for the given term.  Both the display
   // name for the download and the url are searched.
   matchesSearchTerm(aTerm) {
     if (!aTerm) {
       return true;
--- a/browser/components/downloads/content/downloads.js
+++ b/browser/components/downloads/content/downloads.js
@@ -1087,22 +1087,17 @@ DownloadsViewItem.prototype = {
   cmd_delete() {
     DownloadsCommon.removeAndFinalizeDownload(this.download);
     PlacesUtils.bhistory.removePage(
                            NetUtil.newURI(this.download.source.url));
   },
 
   downloadsCmd_unblock() {
     DownloadsPanel.hidePanel();
-    let verdict = this.download.error.reputationCheckVerdict;
-    DownloadsCommon.confirmUnblockDownload(verdict, window).then(confirmed => {
-      if (confirmed) {
-        return this.download.unblock();
-      }
-    }).catch(Cu.reportError);
+    this.confirmUnblock(window);
   },
 
   downloadsCmd_open() {
     this.download.launch().catch(Cu.reportError);
 
     // We explicitly close the panel here to give the user the feedback that
     // their click has been received, and we're handling the action.
     // Otherwise, we'd have to wait for the file-type handler to execute
--- a/browser/components/downloads/test/browser/browser_confirm_unblock_download.js
+++ b/browser/components/downloads/test/browser/browser_confirm_unblock_download.js
@@ -30,17 +30,17 @@ function addDialogOpenObserver(buttonAct
     }
   });
 }
 
 add_task(function* test_confirm_unblock_dialog_unblock() {
   addDialogOpenObserver("accept");
   let result = yield DownloadsCommon.confirmUnblockDownload(Downloads.Error.BLOCK_VERDICT_MALWARE,
                                                             window);
-  ok(result, "Should return true when the user clicks on `Unblock` button.");
+  is(result, "unblock");
 });
 
 add_task(function* test_confirm_unblock_dialog_keep_safe() {
   addDialogOpenObserver("cancel");
   let result = yield DownloadsCommon.confirmUnblockDownload(Downloads.Error.BLOCK_VERDICT_MALWARE,
                                                             window);
-  ok(!result, "Should return false when the user clicks on `Keep me safe` button.");
+  is(result, "cancel");
 });