Bug 1364942 - Allow WebExtensions to disable Web API notifications, r?mixedpuppy draft
authorBob Silverberg <bsilverberg@mozilla.com>
Wed, 18 Oct 2017 17:46:38 -0400
changeset 687042 c9dffd92d3bd1daa90a41971faa9b93f6e3d5901
parent 686074 dfb54d604158f5605fb07f41751e36bfef641a2f
child 737557 13fa2906c14dcdfb7aa964a693551c4cbe4a2620
push id86393
push userbmo:bob.silverberg@gmail.com
push dateThu, 26 Oct 2017 19:27:22 +0000
reviewersmixedpuppy
bugs1364942
milestone58.0a1
Bug 1364942 - Allow WebExtensions to disable Web API notifications, r?mixedpuppy This works by allowing an extension to set a value of Services.perms.DENY_ACTION for permissions.default.desktop-notification, which stores the default permission for desktop notifications. This means that if no permissions have been explicitly set for a given page, the default will be used, but if a user overrides the permissions for a specific page then their chosen permission will override this default. An extension can only use this to make the default behaviour to disable notifications. It cannot be used to globally enable notifications. MozReview-Commit-ID: H5bDZe1ICiC
toolkit/components/extensions/ext-browserSettings.js
toolkit/components/extensions/schemas/browser_settings.json
toolkit/components/extensions/test/xpcshell/test_ext_browserSettings.js
--- a/toolkit/components/extensions/ext-browserSettings.js
+++ b/toolkit/components/extensions/ext-browserSettings.js
@@ -13,16 +13,18 @@ XPCOMUtils.defineLazyServiceGetter(this,
 
 Cu.import("resource://gre/modules/ExtensionPreferencesManager.jsm");
 
 const HOMEPAGE_OVERRIDE_SETTING = "homepage_override";
 const HOMEPAGE_URL_PREF = "browser.startup.homepage";
 const URL_STORE_TYPE = "url_overrides";
 const NEW_TAB_OVERRIDE_SETTING = "newTabURL";
 
+const PERM_DENY_ACTION = Services.perms.DENY_ACTION;
+
 const getSettingsAPI = (extension, name, callback, storeType, readOnly = false) => {
   return {
     async get(details) {
       return {
         levelOfControl: details.incognito ?
           "not_controllable" :
           await ExtensionPreferencesManager.getLevelOfControl(
             extension, name, storeType),
@@ -77,16 +79,26 @@ ExtensionPreferencesManager.addSetting("
     "image.animation_mode",
   ],
 
   setCallback(value) {
     return {[this.prefNames[0]]: value};
   },
 });
 
+ExtensionPreferencesManager.addSetting("webNotificationsDisabled", {
+  prefNames: [
+    "permissions.default.desktop-notification",
+  ],
+
+  setCallback(value) {
+    return {[this.prefNames[0]]: value ? PERM_DENY_ACTION : undefined};
+  },
+});
+
 this.browserSettings = class extends ExtensionAPI {
   getAPI(context) {
     let {extension} = context;
     return {
       browserSettings: {
         allowPopupsForUserEvents: getSettingsAPI(extension,
           "allowPopupsForUserEvents",
           () => {
@@ -109,12 +121,20 @@ this.browserSettings = class extends Ext
           () => {
             return Services.prefs.getCharPref("image.animation_mode");
           }),
         newTabPageOverride: getSettingsAPI(extension,
           NEW_TAB_OVERRIDE_SETTING,
           () => {
             return aboutNewTabService.newTabURL;
           }, URL_STORE_TYPE, true),
+        webNotificationsDisabled: getSettingsAPI(extension,
+          "webNotificationsDisabled",
+          () => {
+            let prefValue =
+              Services.prefs.getIntPref(
+                "permissions.default.desktop-notification", null);
+            return prefValue === PERM_DENY_ACTION;
+          }),
       },
     };
   }
 };
--- a/toolkit/components/extensions/schemas/browser_settings.json
+++ b/toolkit/components/extensions/schemas/browser_settings.json
@@ -44,12 +44,16 @@
       },
       "imageAnimationBehavior": {
         "$ref": "types.Setting",
         "description": "Controls the behaviour of image animation in the browser. This setting's value is of type ImageAnimationBehavior, defaulting to <code>normal</code>."
       },
       "newTabPageOverride": {
         "$ref": "types.Setting",
         "description": "Returns the value of the overridden new tab page. Read-only."
+      },
+      "webNotificationsDisabled": {
+        "$ref": "types.Setting",
+        "description": "Disables webAPI notifications."
       }
     }
   }
 ]
--- a/toolkit/components/extensions/test/xpcshell/test_ext_browserSettings.js
+++ b/toolkit/components/extensions/test/xpcshell/test_ext_browserSettings.js
@@ -13,22 +13,26 @@ const {
   promiseStartupManager,
 } = AddonTestUtils;
 
 AddonTestUtils.init(this);
 
 createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "42");
 
 add_task(async function test_browser_settings() {
+  const PERM_DENY_ACTION = Services.perms.DENY_ACTION;
+  const PERM_UNKNOWN_ACTION = Services.perms.UNKNOWN_ACTION;
+
   // Create an object to hold the values to which we will initialize the prefs.
   const PREFS = {
     "browser.cache.disk.enable": true,
     "browser.cache.memory.enable": true,
     "dom.popup_allowed_events": Preferences.get("dom.popup_allowed_events"),
     "image.animation_mode": "none",
+    "permissions.default.desktop-notification": PERM_UNKNOWN_ACTION,
   };
 
   async function background() {
     browser.test.onMessage.addListener(async (msg, apiName, value) => {
       let apiObj = browser.browserSettings[apiName];
       await apiObj.set({value});
       browser.test.sendMessage("settingData", await apiObj.get({}));
     });
@@ -90,12 +94,23 @@ add_task(async function test_browser_set
     {"dom.popup_allowed_events": PREFS["dom.popup_allowed_events"]});
 
   for (let value of ["normal", "none", "once"]) {
     await testSetting(
       "imageAnimationBehavior", value,
       {"image.animation_mode": value});
   }
 
+  await testSetting(
+    "webNotificationsDisabled", true,
+    {"permissions.default.desktop-notification": PERM_DENY_ACTION});
+  await testSetting(
+    "webNotificationsDisabled", false,
+    {
+      // This pref is not defaulted on Android.
+      "permissions.default.desktop-notification":
+        AppConstants.MOZ_BUILD_APP !== "browser" ? undefined : PERM_UNKNOWN_ACTION,
+    });
+
   await extension.unload();
 
   await promiseShutdownManager();
 });