tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

picture_in_picture_overrides.js (3295B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 "use strict";
      6 
      7 /* globals browser, module */
      8 
      9 /**
     10 * Picture-in-Picture Overrides
     11 */
     12 class PictureInPictureOverrides {
     13  /**
     14   * Class constructor
     15   *
     16   * @param {object} availableOverrides Contains all overrides provided in data/picture_in_picture_overrides.js
     17   */
     18  constructor(availableOverrides) {
     19    this.pref = "enable_picture_in_picture_overrides";
     20    this._prefEnabledOverrides = new Set();
     21    this._availableOverrides = availableOverrides;
     22    this.policies = browser.pictureInPictureChild.getPolicies();
     23  }
     24 
     25  /**
     26   * Ensures the "enable_picture_in_picture_overrides" pref is set; if it is undefined, sets the pref to true
     27   */
     28  async _checkGlobalPref() {
     29    await browser.aboutConfigPipPrefs.getPref(this.pref).then(value => {
     30      if (value === false) {
     31        this._enabled = false;
     32      } else {
     33        if (value === undefined) {
     34          browser.aboutConfigPipPrefs.setPref(this.pref, true);
     35        }
     36        this._enabled = true;
     37      }
     38    });
     39  }
     40 
     41  /**
     42   * Checks the status of a specified override, and updates the set, `this._prefEnabledOverrides`, accordingly
     43   *
     44   * @param {string} id the id of the specific override contained in `this._availableOverrides`
     45   * @param {string} pref the specific preference to check, in the form `disabled_picture_in_picture_overrides.${id}`
     46   */
     47  async _checkSpecificOverridePref(id, pref) {
     48    const isDisabled = await browser.aboutConfigPipPrefs.getPref(pref);
     49    if (isDisabled === true) {
     50      this._prefEnabledOverrides.delete(id);
     51    } else {
     52      this._prefEnabledOverrides.add(id);
     53    }
     54  }
     55 
     56  /**
     57   * The function that `run.js` calls to begin checking for changes to the PiP overrides
     58   */
     59  bootup() {
     60    const checkGlobal = async () => {
     61      await this._checkGlobalPref();
     62      this._onAvailableOverridesChanged();
     63    };
     64    browser.aboutConfigPipPrefs.onPrefChange.addListener(
     65      checkGlobal,
     66      this.pref
     67    );
     68 
     69    const bootupPrefCheckPromises = [this._checkGlobalPref()];
     70 
     71    for (const id of Object.keys(this._availableOverrides)) {
     72      const pref = `disabled_picture_in_picture_overrides.${id}`;
     73      const checkSingle = async () => {
     74        await this._checkSpecificOverridePref(id, pref);
     75        this._onAvailableOverridesChanged();
     76      };
     77      browser.aboutConfigPipPrefs.onPrefChange.addListener(checkSingle, pref);
     78      bootupPrefCheckPromises.push(this._checkSpecificOverridePref(id, pref));
     79    }
     80 
     81    Promise.all(bootupPrefCheckPromises).then(() => {
     82      this._onAvailableOverridesChanged();
     83    });
     84  }
     85 
     86  /**
     87   * Sets pictureInPictureParent's overrides
     88   */
     89  async _onAvailableOverridesChanged() {
     90    const policies = await this.policies;
     91    let enabledOverrides = {};
     92    for (const [id, override] of Object.entries(this._availableOverrides)) {
     93      const enabled = this._enabled && this._prefEnabledOverrides.has(id);
     94      for (const [url, policy] of Object.entries(override)) {
     95        enabledOverrides[url] = enabled ? policy : policies.DEFAULT;
     96      }
     97    }
     98    browser.pictureInPictureParent.setOverrides(enabledOverrides);
     99  }
    100 }