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 686317 e4d6fab0f1bd4c7c3f33102222b59d8641ba6958
parent 686074 dfb54d604158f5605fb07f41751e36bfef641a2f
child 737352 ce8646b047ef4651a3b4105a51de31e8439b52a5
push id86155
push userbmo:bob.silverberg@gmail.com
push dateWed, 25 Oct 2017 18:17:47 +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,19 @@ 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,
+    {"permissions.default.desktop-notification": PERM_UNKNOWN_ACTION});
+
   await extension.unload();
 
   await promiseShutdownManager();
 });