diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:22:09 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:22:09 +0000 |
commit | 43a97878ce14b72f0981164f87f2e35e14151312 (patch) | |
tree | 620249daf56c0258faa40cbdcf9cfba06de2a846 /toolkit/components/antitracking/ContentBlockingAllowList.jsm | |
parent | Initial commit. (diff) | |
download | firefox-43a97878ce14b72f0981164f87f2e35e14151312.tar.xz firefox-43a97878ce14b72f0981164f87f2e35e14151312.zip |
Adding upstream version 110.0.1.upstream/110.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'toolkit/components/antitracking/ContentBlockingAllowList.jsm')
-rw-r--r-- | toolkit/components/antitracking/ContentBlockingAllowList.jsm | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/toolkit/components/antitracking/ContentBlockingAllowList.jsm b/toolkit/components/antitracking/ContentBlockingAllowList.jsm new file mode 100644 index 0000000000..663af6cec9 --- /dev/null +++ b/toolkit/components/antitracking/ContentBlockingAllowList.jsm @@ -0,0 +1,115 @@ +/* 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/. */ + +"use strict"; + +var EXPORTED_SYMBOLS = ["ContentBlockingAllowList"]; + +const lazy = {}; + +ChromeUtils.defineESModuleGetters(lazy, { + PrivateBrowsingUtils: "resource://gre/modules/PrivateBrowsingUtils.sys.mjs", +}); + +/** + * A helper module to manage the Content Blocking Allow List. + * + * This module provides a couple of utility APIs for adding or + * removing a given browser object to the Content Blocking allow + * list. + */ +const ContentBlockingAllowList = { + _observingLastPBContext: false, + + _maybeSetupLastPBContextObserver() { + if (!this._observingLastPBContext) { + this._observer = { + QueryInterface: ChromeUtils.generateQI([ + "nsIObserver", + "nsISupportsWeakReference", + ]), + + observe(subject, topic, data) { + if (topic == "last-pb-context-exited") { + Services.perms.removeByType("trackingprotection-pb"); + } + }, + }; + Services.obs.addObserver(this._observer, "last-pb-context-exited", true); + this._observingLastPBContext = true; + } + }, + + _basePrincipalForAntiTrackingCommon(browser) { + let principal = + browser.browsingContext.currentWindowGlobal + ?.contentBlockingAllowListPrincipal; + // We can only use content principals for this purpose. + if (!principal || !principal.isContentPrincipal) { + return null; + } + return principal; + }, + + _permissionTypeFor(browser) { + return lazy.PrivateBrowsingUtils.isBrowserPrivate(browser) + ? "trackingprotection-pb" + : "trackingprotection"; + }, + + _expiryFor(browser) { + return lazy.PrivateBrowsingUtils.isBrowserPrivate(browser) + ? Ci.nsIPermissionManager.EXPIRE_SESSION + : Ci.nsIPermissionManager.EXPIRE_NEVER; + }, + + /** + * Returns false if this module cannot handle the current document loaded in + * the browser object. This can happen for example for about: or file: + * documents. + */ + canHandle(browser) { + return this._basePrincipalForAntiTrackingCommon(browser) != null; + }, + + /** + * Add the given browser object to the Content Blocking allow list. + */ + add(browser) { + // Start observing PB last-context-exit notification to do the needed cleanup. + this._maybeSetupLastPBContextObserver(); + + let prin = this._basePrincipalForAntiTrackingCommon(browser); + let type = this._permissionTypeFor(browser); + let expire = this._expiryFor(browser); + Services.perms.addFromPrincipal( + prin, + type, + Services.perms.ALLOW_ACTION, + expire + ); + }, + + /** + * Remove the given browser object from the Content Blocking allow list. + */ + remove(browser) { + let prin = this._basePrincipalForAntiTrackingCommon(browser); + let type = this._permissionTypeFor(browser); + Services.perms.removeFromPrincipal(prin, type); + }, + + /** + * Returns true if the current browser has loaded a document that is on the + * Content Blocking allow list. + */ + includes(browser) { + let prin = this._basePrincipalForAntiTrackingCommon(browser); + let type = this._permissionTypeFor(browser); + return ( + Services.perms.testExactPermissionFromPrincipal(prin, type) == + Services.perms.ALLOW_ACTION + ); + }, +}; |