From 9e3c08db40b8916968b9f30096c7be3f00ce9647 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 21 Apr 2024 13:44:51 +0200 Subject: Adding upstream version 1:115.7.0. Signed-off-by: Daniel Baumann --- .../internal/siteperms-addon-utils.sys.mjs | 72 ++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 toolkit/mozapps/extensions/internal/siteperms-addon-utils.sys.mjs (limited to 'toolkit/mozapps/extensions/internal/siteperms-addon-utils.sys.mjs') 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} 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); +} -- cgit v1.2.3