From 6bf0a5cb5034a7e684dcc3500e841785237ce2dd Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 19:32:43 +0200 Subject: Adding upstream version 1:115.7.0. Signed-off-by: Daniel Baumann --- .../lib/picture_in_picture_overrides.js | 98 ++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 browser/extensions/pictureinpicture/lib/picture_in_picture_overrides.js (limited to 'browser/extensions/pictureinpicture/lib/picture_in_picture_overrides.js') diff --git a/browser/extensions/pictureinpicture/lib/picture_in_picture_overrides.js b/browser/extensions/pictureinpicture/lib/picture_in_picture_overrides.js new file mode 100644 index 0000000000..829f33e15b --- /dev/null +++ b/browser/extensions/pictureinpicture/lib/picture_in_picture_overrides.js @@ -0,0 +1,98 @@ +/* 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"; + +/* globals browser, module */ + +/** + * Picture-in-Picture Overrides + */ +class PictureInPictureOverrides { + /** + * Class constructor + * @param {Object} availableOverrides Contains all overrides provided in data/picture_in_picture_overrides.js + */ + constructor(availableOverrides) { + this.pref = "enable_picture_in_picture_overrides"; + this._prefEnabledOverrides = new Set(); + this._availableOverrides = availableOverrides; + this.policies = browser.pictureInPictureChild.getPolicies(); + } + + /** + * Ensures the "enable_picture_in_picture_overrides" pref is set; if it is undefined, sets the pref to true + */ + async _checkGlobalPref() { + await browser.aboutConfigPipPrefs.getPref(this.pref).then(value => { + if (value === false) { + this._enabled = false; + } else { + if (value === undefined) { + browser.aboutConfigPipPrefs.setPref(this.pref, true); + } + this._enabled = true; + } + }); + } + + /** + * Checks the status of a specified override, and updates the set, `this._prefEnabledOverrides`, accordingly + * @param {String} id the id of the specific override contained in `this._availableOverrides` + * @param {String} pref the specific preference to check, in the form `disabled_picture_in_picture_overrides.${id}` + */ + async _checkSpecificOverridePref(id, pref) { + const isDisabled = await browser.aboutConfigPipPrefs.getPref(pref); + if (isDisabled === true) { + this._prefEnabledOverrides.delete(id); + } else { + this._prefEnabledOverrides.add(id); + } + } + + /** + * The function that `run.js` calls to begin checking for changes to the PiP overrides + */ + bootup() { + const checkGlobal = async () => { + await this._checkGlobalPref(); + this._onAvailableOverridesChanged(); + }; + browser.aboutConfigPipPrefs.onPrefChange.addListener( + checkGlobal, + this.pref + ); + + const bootupPrefCheckPromises = [this._checkGlobalPref()]; + + for (const id of Object.keys(this._availableOverrides)) { + const pref = `disabled_picture_in_picture_overrides.${id}`; + const checkSingle = async () => { + await this._checkSpecificOverridePref(id, pref); + this._onAvailableOverridesChanged(); + }; + browser.aboutConfigPipPrefs.onPrefChange.addListener(checkSingle, pref); + bootupPrefCheckPromises.push(this._checkSpecificOverridePref(id, pref)); + } + + Promise.all(bootupPrefCheckPromises).then(() => { + this._onAvailableOverridesChanged(); + }); + } + + /** + * Sets pictureInPictureParent's overrides + */ + async _onAvailableOverridesChanged() { + const policies = await this.policies; + let enabledOverrides = {}; + for (const [id, override] of Object.entries(this._availableOverrides)) { + const enabled = this._enabled && this._prefEnabledOverrides.has(id); + for (const [url, policy] of Object.entries(override)) { + enabledOverrides[url] = enabled ? policy : policies.DEFAULT; + } + } + browser.pictureInPictureParent.setOverrides(enabledOverrides); + } +} -- cgit v1.2.3