tor-browser

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

IPPOnboardingMessageHelper.sys.mjs (3237B)


      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 { ONBOARDING_PREF_FLAGS } from "chrome://browser/content/ipprotection/ipprotection-constants.mjs";
      6 
      7 const lazy = {};
      8 
      9 ChromeUtils.defineESModuleGetters(lazy, {
     10  IPPProxyManager:
     11    "moz-src:///browser/components/ipprotection/IPPProxyManager.sys.mjs",
     12  IPPProxyStates:
     13    "moz-src:///browser/components/ipprotection/IPPProxyManager.sys.mjs",
     14 });
     15 
     16 const ONBOARDING_MESSAGE_MASK_PREF =
     17  "browser.ipProtection.onboardingMessageMask";
     18 const AUTOSTART_PREF = "browser.ipProtection.autoStartEnabled";
     19 const PERM_NAME = "ipp-vpn";
     20 
     21 /**
     22 * This class handles in-panel continuous onboarding messages, including setting
     23 * the browser.ipProtection.onboardingMessageMask, a pref that gates messages
     24 * according to feature (general VPN, autostart, site exceptions) through bit mask
     25 */
     26 class IPPOnboardingMessageHelper {
     27  #observingPermChanges = false;
     28 
     29  constructor() {
     30    this.handleEvent = this.#handleEvent.bind(this);
     31 
     32    // If at least one exception is saved, don't show site exceptions onboarding message
     33    let savedSites = Services.perms.getAllByTypes([PERM_NAME]);
     34    if (savedSites.length) {
     35      this.setOnboardingFlag(ONBOARDING_PREF_FLAGS.EVER_USED_SITE_EXCEPTIONS);
     36    } else {
     37      Services.obs.addObserver(this, "perm-changed");
     38      this.#observingPermChanges = true;
     39    }
     40 
     41    Services.prefs.addObserver(AUTOSTART_PREF, () =>
     42      this.setOnboardingFlag(ONBOARDING_PREF_FLAGS.EVER_TURNED_ON_AUTOSTART)
     43    );
     44 
     45    let autoStartPref = Services.prefs.getBoolPref(AUTOSTART_PREF, false);
     46    if (autoStartPref) {
     47      this.setOnboardingFlag(ONBOARDING_PREF_FLAGS.EVER_TURNED_ON_AUTOSTART);
     48    }
     49  }
     50 
     51  init() {
     52    lazy.IPPProxyManager.addEventListener(
     53      "IPPProxyManager:StateChanged",
     54      this.handleEvent
     55    );
     56  }
     57 
     58  initOnStartupCompleted() {}
     59 
     60  uninit() {
     61    if (this.#observingPermChanges) {
     62      Services.obs.removeObserver(this, "perm-changed");
     63      this.#observingPermChanges = false;
     64    }
     65 
     66    lazy.IPPProxyManager.removeEventListener(
     67      "IPPProxyManager:StateChanged",
     68      this.handleEvent
     69    );
     70  }
     71 
     72  observe(subject, topic, data) {
     73    let permission = subject.QueryInterface(Ci.nsIPermission);
     74    if (
     75      topic === "perm-changed" &&
     76      permission.type === PERM_NAME &&
     77      data === "added"
     78    ) {
     79      this.setOnboardingFlag(ONBOARDING_PREF_FLAGS.EVER_USED_SITE_EXCEPTIONS);
     80    }
     81  }
     82 
     83  readPrefMask() {
     84    return Services.prefs.getIntPref(ONBOARDING_MESSAGE_MASK_PREF, 0);
     85  }
     86 
     87  writeOnboardingTriggerPref(mask) {
     88    Services.prefs.setIntPref(ONBOARDING_MESSAGE_MASK_PREF, mask);
     89  }
     90 
     91  setOnboardingFlag(flag) {
     92    const mask = this.readPrefMask();
     93    this.writeOnboardingTriggerPref(mask | flag);
     94  }
     95 
     96  #handleEvent(event) {
     97    if (
     98      event.type == "IPPProxyManager:StateChanged" &&
     99      lazy.IPPProxyManager.state === lazy.IPPProxyStates.ACTIVE
    100    ) {
    101      this.setOnboardingFlag(ONBOARDING_PREF_FLAGS.EVER_TURNED_ON_VPN);
    102    }
    103  }
    104 }
    105 
    106 const IPPOnboardingMessage = new IPPOnboardingMessageHelper();
    107 
    108 export { IPPOnboardingMessage };