Bug 1364942 - Allow WebExtensions to disable Web API notifications, r=mixedpuppy
authorBob Silverberg <bsilverberg@mozilla.com>
Wed, 18 Oct 2017 17:46:38 -0400
changeset 388699 2ac09a374b698d7cbb4cc50bf8245305aa07a347
parent 388698 d8e1ed6dd42c040e8f9e7a6c3d3248a8b15e3865
child 388700 88a41df87dacd767dbbf392ec624917a4e3881d6
push id32757
push userarchaeopteryx@coole-files.de
push dateFri, 27 Oct 2017 21:30:09 +0000
treeherdermozilla-central@b66c75b4aa61 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmixedpuppy
bugs1364942
milestone58.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 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();
 });