tor-browser

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

aboutConfigPrefsChild.js (2567B)


      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 */
      8 
      9 this.aboutConfigPrefs = class AboutConfigPrefsChildAPI extends ExtensionAPI {
     10  static ALLOWED_GLOBAL_PREFS = Object.freeze(
     11    [
     12      "layout.css.prefixes.transforms",
     13      "layout.css.webkit-fill-available.enabled",
     14      "timer.auto_increase_timer_resolution",
     15    ].concat(
     16      Cu.isInAutomation ? ["webcompat.test.pref1", "webcompat.test.pref2"] : []
     17    )
     18  );
     19 
     20  getAPI(context) {
     21    const EventManager = ExtensionCommon.EventManager;
     22    const extensionIDBase = context.extension.id.split("@")[0];
     23    const extensionPrefNameBase = `extensions.${extensionIDBase}.`;
     24 
     25    function getSafePref(name) {
     26      if (AboutConfigPrefsChildAPI.ALLOWED_GLOBAL_PREFS.includes(name)) {
     27        return name;
     28      }
     29      return `${extensionPrefNameBase}${name}`;
     30    }
     31 
     32    return {
     33      aboutConfigPrefs: {
     34        onPrefChange: new EventManager({
     35          context,
     36          name: "aboutConfigPrefs.onUAOverridesPrefChange",
     37          register: (fire, name) => {
     38            const prefName = getSafePref(name);
     39            const callback = () => {
     40              fire.async(name).catch(() => {}); // ignore Message Manager disconnects
     41            };
     42            Services.prefs.addObserver(prefName, callback);
     43            return () => {
     44              Services.prefs.removeObserver(prefName, callback);
     45            };
     46          },
     47        }).api(),
     48        getCheckableGlobalPrefs() {
     49          return AboutConfigPrefsChildAPI.ALLOWED_GLOBAL_PREFS;
     50        },
     51        getPref(_name, defaultValue) {
     52          const name = getSafePref(_name);
     53          try {
     54            switch (Services.prefs.getPrefType(name)) {
     55              case Ci.nsIPrefBranch.PREF_BOOL:
     56                return Services.prefs.getBoolPref(
     57                  name,
     58                  defaultValue ?? undefined
     59                );
     60              case Ci.nsIPrefBranch.PREF_INT:
     61                return Services.prefs.getIntPref(
     62                  name,
     63                  defaultValue ?? undefined
     64                );
     65              case Ci.nsIPrefBranch.PREF_STRING:
     66                return Services.prefs.getStringPref(
     67                  name,
     68                  defaultValue ?? undefined
     69                );
     70            }
     71          } catch (_) {}
     72          return defaultValue ?? undefined;
     73        },
     74      },
     75    };
     76  }
     77 };