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
141 lines
4.2 KiB
JavaScript
141 lines
4.2 KiB
JavaScript
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
|
|
/* vim: set sts=2 sw=2 et tw=80: */
|
|
"use strict";
|
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "ExtensionSettingsStore",
|
|
"resource://gre/modules/ExtensionSettingsStore.jsm");
|
|
XPCOMUtils.defineLazyModuleGetter(this, "Services",
|
|
"resource://gre/modules/Services.jsm");
|
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "aboutNewTabService",
|
|
"@mozilla.org/browser/aboutnewtab-service;1",
|
|
"nsIAboutNewTabService");
|
|
|
|
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),
|
|
value: await callback(),
|
|
};
|
|
},
|
|
set(details) {
|
|
if (!readOnly) {
|
|
return ExtensionPreferencesManager.setSetting(
|
|
extension, name, details.value);
|
|
}
|
|
},
|
|
clear(details) {
|
|
if (!readOnly) {
|
|
return ExtensionPreferencesManager.removeSetting(extension, name);
|
|
}
|
|
},
|
|
};
|
|
};
|
|
|
|
// Add settings objects for supported APIs to the preferences manager.
|
|
ExtensionPreferencesManager.addSetting("allowPopupsForUserEvents", {
|
|
prefNames: [
|
|
"dom.popup_allowed_events",
|
|
],
|
|
|
|
setCallback(value) {
|
|
let returnObj = {};
|
|
// If the value is true, then reset the pref, otherwise set it to "".
|
|
returnObj[this.prefNames[0]] = value ? undefined : "";
|
|
return returnObj;
|
|
},
|
|
});
|
|
|
|
ExtensionPreferencesManager.addSetting("cacheEnabled", {
|
|
prefNames: [
|
|
"browser.cache.disk.enable",
|
|
"browser.cache.memory.enable",
|
|
],
|
|
|
|
setCallback(value) {
|
|
let returnObj = {};
|
|
for (let pref of this.prefNames) {
|
|
returnObj[pref] = value;
|
|
}
|
|
return returnObj;
|
|
},
|
|
});
|
|
|
|
ExtensionPreferencesManager.addSetting("imageAnimationBehavior", {
|
|
prefNames: [
|
|
"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",
|
|
() => {
|
|
return Services.prefs.getCharPref("dom.popup_allowed_events") != "";
|
|
}),
|
|
cacheEnabled: getSettingsAPI(extension,
|
|
"cacheEnabled",
|
|
() => {
|
|
return Services.prefs.getBoolPref("browser.cache.disk.enable") &&
|
|
Services.prefs.getBoolPref("browser.cache.memory.enable");
|
|
}),
|
|
homepageOverride: getSettingsAPI(extension,
|
|
HOMEPAGE_OVERRIDE_SETTING,
|
|
() => {
|
|
return Services.prefs.getComplexValue(
|
|
HOMEPAGE_URL_PREF, Ci.nsIPrefLocalizedString).data;
|
|
}, undefined, true),
|
|
imageAnimationBehavior: getSettingsAPI(extension,
|
|
"imageAnimationBehavior",
|
|
() => {
|
|
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;
|
|
}),
|
|
},
|
|
};
|
|
}
|
|
};
|