tor-browser

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

GeckoViewPreferences.sys.mjs (5073B)


      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 https://mozilla.org/MPL/2.0/. */
      4 
      5 import { GeckoViewUtils } from "resource://gre/modules/GeckoViewUtils.sys.mjs";
      6 
      7 const lazy = {};
      8 ChromeUtils.defineESModuleGetters(lazy, {
      9  EventDispatcher: "resource://gre/modules/Messaging.sys.mjs",
     10 });
     11 
     12 const { PREF_STRING, PREF_BOOL, PREF_INT } = Ci.nsIPrefBranch;
     13 const { debug, warn } = GeckoViewUtils.initLogging("GeckoViewPreferences");
     14 
     15 /**
     16 * Make TS definitions available.
     17 *
     18 * @typedef {import(".").GeckoPreference} GeckoPreference
     19 */
     20 
     21 export const GeckoViewPreferences = {
     22  /* eslint-disable complexity */
     23  onEvent(aEvent, aData, aCallback) {
     24    debug`onEvent ${aEvent} ${aData}`;
     25 
     26    switch (aEvent) {
     27      case "GeckoView:Preferences:GetPref": {
     28        aCallback.onSuccess({
     29          prefs: aData.prefs.map(pref => this.getPreference(pref)),
     30        });
     31        return;
     32      }
     33      case "GeckoView:Preferences:SetPref": {
     34        aCallback.onSuccess({
     35          prefs: aData.prefs.map(pref => ({
     36            pref: pref.pref,
     37            isSet: this.setPreference(
     38              pref.pref,
     39              pref.type,
     40              pref.value,
     41              pref.branch
     42            ),
     43          })),
     44        });
     45        return;
     46      }
     47      case "GeckoView:Preferences:ClearPref": {
     48        Services.prefs.clearUserPref(aData.pref);
     49        aCallback.onSuccess();
     50        break;
     51      }
     52      case "GeckoView:Preferences:RegisterObserver": {
     53        for (const pref of aData.prefs) {
     54          Services.prefs.addObserver(pref, this);
     55        }
     56        aCallback.onSuccess();
     57        break;
     58      }
     59      case "GeckoView:Preferences:UnregisterObserver": {
     60        for (const pref of aData.prefs) {
     61          Services.prefs.removeObserver(pref, this);
     62        }
     63        aCallback.onSuccess();
     64        break;
     65      }
     66    }
     67  },
     68 
     69  /**
     70   * Gets the preference value from a specific branch given the type.
     71   *
     72   * @param {nsIPrefBranch} branch - The object representing the requested branch.
     73   * @param {long} type - The nsIPrefBranch type of preference. (e.g. PREF_STRING)
     74   * @param {string} prefName - The name of the preference. (e.g. "some.preference.item")
     75  //  * @return {string | boolean | number | null } Preference value.
     76   */
     77  readBranch(branch, type, prefName) {
     78    switch (type) {
     79      case PREF_STRING:
     80        return branch.getStringPref(prefName);
     81      case PREF_BOOL:
     82        return branch.getBoolPref(prefName);
     83      case PREF_INT:
     84        return branch.getIntPref(prefName);
     85      default:
     86        warn`Attempted to get a preference that doesn't conform to a known type.`;
     87        return null;
     88    }
     89  },
     90  /**
     91   * Gets the Gecko preference and associated information.
     92   *
     93   * @param {string} prefName - The name of the preference. (e.g. "some.preference.item")
     94   * @return {GeckoPreference} Information about the preference.
     95   */
     96  getPreference(prefName) {
     97    let pref = {
     98      pref: prefName,
     99      type: Services.prefs.getPrefType(prefName),
    100      defaultValue: null,
    101      userValue: null,
    102    };
    103 
    104    if (Services.prefs.prefHasDefaultValue(prefName)) {
    105      pref.defaultValue = this.readBranch(
    106        Services.prefs.getDefaultBranch(null),
    107        pref.type,
    108        prefName
    109      );
    110    }
    111    if (Services.prefs.prefHasUserValue(prefName)) {
    112      pref.userValue = this.readBranch(Services.prefs, pref.type, prefName);
    113    }
    114    return pref;
    115  },
    116 
    117  /**
    118   * Sets the Gecko preference with the associated information.
    119   *
    120   * @param {string} pref - The name of the preference. (e.g. "some.preference.item")
    121   * @param {long} type - The nsIPrefBranch type of preference. (e.g. PREF_STRING)
    122   * @param {string | integer | boolean} value - The value of the preference. (e.g. "hello-world")
    123   * @param {"user" | "default"} setBranch - The branch to operate on.
    124   * @return {boolean} Will return true if the preference set as requested or else false.
    125   */
    126  setPreference(pref, type, value, setBranch) {
    127    const branch =
    128      setBranch == "user"
    129        ? Services.prefs
    130        : Services.prefs.getDefaultBranch(null);
    131    try {
    132      switch (type) {
    133        case PREF_STRING:
    134          branch.setStringPref(pref, value);
    135          return true;
    136 
    137        case PREF_BOOL:
    138          branch.setBoolPref(pref, value);
    139          return true;
    140 
    141        case PREF_INT:
    142          branch.setIntPref(pref, value);
    143          return true;
    144 
    145        default:
    146          warn`Attempted to set against an unknown type of: ${type}`;
    147          return false;
    148      }
    149    } catch (e) {
    150      warn`There was an issue with the preference: ${e}`;
    151      return false;
    152    }
    153  },
    154 
    155  /** nsIObserver */
    156  observe(subject, topic, data) {
    157    if (topic == "nsPref:changed") {
    158      lazy.EventDispatcher.instance.sendRequest({
    159        type: "GeckoView:GeckoPreferences:Change",
    160        data: this.getPreference(data),
    161      });
    162    }
    163  },
    164 
    165  /** nsISupports */
    166  QueryInterface: ChromeUtils.generateQI(["nsIObserver"]),
    167 };