diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 17:32:43 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 17:32:43 +0000 |
commit | 6bf0a5cb5034a7e684dcc3500e841785237ce2dd (patch) | |
tree | a68f146d7fa01f0134297619fbe7e33db084e0aa /toolkit/mozapps/extensions/internal/siteperms-addon-utils.sys.mjs | |
parent | Initial commit. (diff) | |
download | thunderbird-6bf0a5cb5034a7e684dcc3500e841785237ce2dd.tar.xz thunderbird-6bf0a5cb5034a7e684dcc3500e841785237ce2dd.zip |
Adding upstream version 1:115.7.0.upstream/1%115.7.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'toolkit/mozapps/extensions/internal/siteperms-addon-utils.sys.mjs')
-rw-r--r-- | toolkit/mozapps/extensions/internal/siteperms-addon-utils.sys.mjs | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/toolkit/mozapps/extensions/internal/siteperms-addon-utils.sys.mjs b/toolkit/mozapps/extensions/internal/siteperms-addon-utils.sys.mjs new file mode 100644 index 0000000000..86853d7168 --- /dev/null +++ b/toolkit/mozapps/extensions/internal/siteperms-addon-utils.sys.mjs @@ -0,0 +1,72 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs"; + +export const GATED_PERMISSIONS = ["midi", "midi-sysex"]; +export const SITEPERMS_ADDON_PROVIDER_PREF = + "dom.sitepermsaddon-provider.enabled"; +export const SITEPERMS_ADDON_TYPE = "sitepermission"; +export const SITEPERMS_ADDON_BLOCKEDLIST_PREF = + "dom.sitepermsaddon-provider.separatedBlocklistedDomains"; + +const lazy = {}; +XPCOMUtils.defineLazyPreferenceGetter( + lazy, + "blocklistedOriginsSet", + SITEPERMS_ADDON_BLOCKEDLIST_PREF, + // Default value + "", + // onUpdate + null, + // transform + prefValue => new Set(prefValue.split(",")) +); + +/** + * @param {string} type + * @returns {boolean} + */ +export function isGatedPermissionType(type) { + return GATED_PERMISSIONS.includes(type); +} + +/** + * @param {string} siteOrigin + * @returns {boolean} + */ +export function isKnownPublicSuffix(siteOrigin) { + const { host } = new URL(siteOrigin); + + let isPublic = false; + // getKnownPublicSuffixFromHost throws when passed an IP, in such case, assume + // this is not a public etld. + try { + isPublic = Services.eTLD.getKnownPublicSuffixFromHost(host) == host; + } catch (e) {} + + return isPublic; +} + +/** + * ⚠️ This should be only used for testing purpose ⚠️ + * + * @param {Array<String>} permissionTypes + * @throws if not called from xpcshell test + */ +export function addGatedPermissionTypesForXpcShellTests(permissionTypes) { + if (!Services.env.exists("XPCSHELL_TEST_PROFILE_DIR")) { + throw new Error("This should only be called from XPCShell tests"); + } + + GATED_PERMISSIONS.push(...permissionTypes); +} + +/** + * @param {nsIPrincipal} principal + * @returns {Boolean} + */ +export function isPrincipalInSitePermissionsBlocklist(principal) { + return lazy.blocklistedOriginsSet.has(principal.baseDomain); +} |