tor-browser

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

aboutConfigPipPrefs.js (2501B)


      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 /* global ExtensionAPI, ExtensionCommon, Services, XPCOMUtils */
      8 
      9 /**
     10 * Class extending the ExtensionAPI, ensures we can set/get preferences
     11 */
     12 this.aboutConfigPipPrefs = class extends ExtensionAPI {
     13  /**
     14   * Override ExtensionAPI with PiP override's specific preference API, prefixed by `disabled_picture_in_picture_overrides`
     15   *
     16   * @param {ExtensionContext} context the context of an extension
     17   * @returns {object} returns the necessary API structure required to manage prefs within this extension
     18   */
     19  getAPI(context) {
     20    const EventManager = ExtensionCommon.EventManager;
     21    const extensionIDBase = context.extension.id.split("@")[0];
     22    const extensionPrefNameBase = `extensions.${extensionIDBase}.`;
     23 
     24    return {
     25      aboutConfigPipPrefs: {
     26        onPrefChange: new EventManager({
     27          context,
     28          name: "aboutConfigPipPrefs.onSiteOverridesPrefChange",
     29          register: (fire, name) => {
     30            const prefName = `${extensionPrefNameBase}${name}`;
     31            const callback = () => {
     32              fire.async(name).catch(() => {}); // ignore Message Manager disconnects
     33            };
     34            Services.prefs.addObserver(prefName, callback);
     35            return () => {
     36              Services.prefs.removeObserver(prefName, callback);
     37            };
     38          },
     39        }).api(),
     40        /**
     41         * Calls `Services.prefs.getBoolPref` to get a preference
     42         *
     43         * @param {string} name The name of the preference to get; will be prefixed with this extension's branch
     44         * @returns {boolean|undefined} the preference, or undefined
     45         */
     46        async getPref(name) {
     47          try {
     48            return Services.prefs.getBoolPref(
     49              `${extensionPrefNameBase}${name}`
     50            );
     51          } catch (_) {
     52            return undefined;
     53          }
     54        },
     55 
     56        /**
     57         * Calls `Services.prefs.setBoolPref` to set a preference
     58         *
     59         * @param {string} name the name of the preference to set; will be prefixed with this extension's branch
     60         * @param {boolean} value the bool value to save in the pref
     61         */
     62        async setPref(name, value) {
     63          Services.prefs.setBoolPref(`${extensionPrefNameBase}${name}`, value);
     64        },
     65      },
     66    };
     67  }
     68 };