Bug 1509047 - Part 4: Add support for PermissionPromptPrototype.onBeforeShow() cancelling a prompt r=johannh
☠☠ backed out by 0f4be93319f6 ☠ ☠
authorEhsan Akhgari <ehsan@mozilla.com>
Wed, 28 Nov 2018 22:02:52 +0000
changeset 505087 37172d2d6f99802975761c9e26bac9f2b94d8c44
parent 505086 cc9d468d6ba6a52d06f7fb03b8778e1ce6e70e69
child 505088 7bfa4179a341674e066171c33a8bad746b68cd92
push id10290
push userffxbld-merge
push dateMon, 03 Dec 2018 16:23:23 +0000
treeherdermozilla-beta@700bed2445e6 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjohannh
bugs1509047
milestone65.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 1509047 - Part 4: Add support for PermissionPromptPrototype.onBeforeShow() cancelling a prompt r=johannh Depends on D12864 Differential Revision: https://phabricator.services.mozilla.com/D12865
browser/modules/PermissionUI.jsm
browser/modules/test/browser/browser_PermissionUI.js
--- a/browser/modules/PermissionUI.jsm
+++ b/browser/modules/PermissionUI.jsm
@@ -248,18 +248,22 @@ var PermissionPromptPrototype = {
     return [];
   },
 
   /**
    * If the prompt will be shown to the user, this callback will
    * be called just before. Subclasses may want to override this
    * in order to, for example, bump a counter Telemetry probe for
    * how often a particular permission request is seen.
+   *
+   * If this returns false, it cancels the process of showing the prompt.  In
+   * that case, it is the responsibility of the onBeforeShow() implementation
+   * to ensure that allow() or cancel() are called on the object appropriately.
    */
-  onBeforeShow() {},
+  onBeforeShow() { return true; },
 
   /**
    * If the prompt was shown to the user, this callback will be called just
    * after it's been shown.
    */
   onShown() {},
 
   /**
@@ -435,24 +439,25 @@ var PermissionPromptPrototype = {
       }
       // The prompt has been removed, notify the PermissionUI.
       if (topic == "removed") {
         this.onAfterShow();
       }
       return false;
     };
 
-    this.onBeforeShow();
-    chromeWin.PopupNotifications.show(this.browser,
-                                      this.notificationID,
-                                      this.message,
-                                      this.anchorID,
-                                      mainAction,
-                                      secondaryActions,
-                                      options);
+    if (this.onBeforeShow() !== false) {
+      chromeWin.PopupNotifications.show(this.browser,
+                                        this.notificationID,
+                                        this.message,
+                                        this.anchorID,
+                                        mainAction,
+                                        secondaryActions,
+                                        options);
+    }
   },
 };
 
 PermissionUI.PermissionPromptPrototype = PermissionPromptPrototype;
 
 /**
  * A subclass of PermissionPromptPrototype that assumes
  * that this.request is an nsIContentPermissionRequest
@@ -585,16 +590,17 @@ GeolocationPermissionPrompt.prototype = 
       },
     }];
   },
 
   onBeforeShow() {
     let secHistogram = Services.telemetry.getHistogramById("SECURITY_UI");
     const SHOW_REQUEST = Ci.nsISecurityUITelemetry.WARNING_GEOLOCATION_REQUEST;
     secHistogram.add(SHOW_REQUEST);
+    return true;
   },
 };
 
 PermissionUI.GeolocationPermissionPrompt = GeolocationPermissionPrompt;
 
 /**
  * Creates a PermissionPrompt for a nsIContentPermissionRequest for
  * the Desktop Notification API.
@@ -821,19 +827,16 @@ MIDIPermissionPrompt.prototype = {
         action: Ci.nsIPermissionManager.ALLOW_ACTION,
       },
       {
         label: gBrowserBundle.GetStringFromName("midi.DontAllow.label"),
         accessKey: gBrowserBundle.GetStringFromName("midi.DontAllow.accesskey"),
         action: Ci.nsIPermissionManager.DENY_ACTION,
     }];
   },
-
-  onBeforeShow() {
-  },
 };
 
 PermissionUI.MIDIPermissionPrompt = MIDIPermissionPrompt;
 
 function AutoplayPermissionPrompt(request) {
   this.request = request;
 }
 
@@ -906,16 +909,17 @@ AutoplayPermissionPrompt.prototype = {
       let notification = chromeWin.PopupNotifications.getNotification(
         this.notificationID, this.browser);
       if (notification) {
         chromeWin.PopupNotifications.remove(notification);
       }
     };
     this.browser.addEventListener(
       "DOMAudioPlaybackStarted", this.handlePlaybackStart);
+    return true;
   },
 };
 
 PermissionUI.AutoplayPermissionPrompt = AutoplayPermissionPrompt;
 
 function StorageAccessPermissionPrompt(request) {
   this.request = request;
 }
--- a/browser/modules/test/browser/browser_PermissionUI.js
+++ b/browser/modules/test/browser/browser_PermissionUI.js
@@ -289,16 +289,17 @@ add_task(async function test_on_before_s
     let TestPrompt = {
       __proto__: PermissionUI.PermissionPromptForRequestPrototype,
       request: mockRequest,
       notificationID: kTestNotificationID,
       message: kTestMessage,
       promptActions: [mainAction],
       onBeforeShow() {
         beforeShown = true;
+        return true;
       },
     };
 
     let shownPromise =
       BrowserTestUtils.waitForEvent(PopupNotifications.panel, "popupshown");
     TestPrompt.prompt();
     Assert.ok(beforeShown, "Should have called onBeforeShown");
     await shownPromise;
@@ -348,16 +349,17 @@ add_task(async function test_no_request(
       __proto__: PermissionUI.PermissionPromptPrototype,
       notificationID: kTestNotificationID,
       principal,
       browser,
       message: kTestMessage,
       promptActions: [mainAction, secondaryAction],
       onBeforeShow() {
         beforeShown = true;
+        return true;
       },
     };
 
     let shownPromise =
       BrowserTestUtils.waitForEvent(PopupNotifications.panel, "popupshown");
     TestPrompt.prompt();
     Assert.ok(beforeShown, "Should have called onBeforeShown");
     await shownPromise;