tor-browser

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

IPPVPNAddonHelper.sys.mjs (2066B)


      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 const lazy = {};
      6 
      7 ChromeUtils.defineESModuleGetters(lazy, {
      8  AddonManager: "resource://gre/modules/AddonManager.sys.mjs",
      9  IPProtectionService:
     10    "moz-src:///browser/components/ipprotection/IPProtectionService.sys.mjs",
     11 });
     12 
     13 const VPN_ADDON_ID = "vpn@mozilla.com";
     14 
     15 /**
     16 * This class monitors the VPN add-on installation.
     17 */
     18 class VPNAddonHelperSingleton {
     19  #vpnAddonDetected = false;
     20 
     21  init() {}
     22 
     23  initOnStartupCompleted() {
     24    const self = this;
     25    this.addonVPNListener = {
     26      onInstalled(addon) {
     27        if (addon.id === VPN_ADDON_ID) {
     28          self.#vpnAddonDetected = true;
     29          lazy.IPProtectionService.updateState();
     30        }
     31      },
     32 
     33      onUninstalled(addon) {
     34        if (addon.id === VPN_ADDON_ID) {
     35          self.#vpnAddonDetected = false;
     36          lazy.IPProtectionService.updateState();
     37        }
     38      },
     39 
     40      onUninstalling(addon) {
     41        // In some scenarios, the add-on is not fully uninstalled, but it's set
     42        // in a pending state. When this happens, `onUninstalled` is not
     43        // triggered. Let's use `onUninstalling` instead.
     44        if (addon.id === VPN_ADDON_ID) {
     45          self.#vpnAddonDetected = false;
     46          lazy.IPProtectionService.updateState();
     47        }
     48      },
     49    };
     50    lazy.AddonManager.addAddonListener(this.addonVPNListener);
     51 
     52    lazy.AddonManager.readyPromise.then(() => {
     53      lazy.AddonManager.getAddonByID(VPN_ADDON_ID).then(addon => {
     54        this.#vpnAddonDetected = !!addon;
     55        lazy.IPProtectionService.updateState();
     56      });
     57    });
     58  }
     59 
     60  uninit() {
     61    if (this.addonVPNListener) {
     62      lazy.AddonManager.removeAddonListener(this.addonVPNListener);
     63      this.#vpnAddonDetected = false;
     64    }
     65  }
     66 
     67  get vpnAddonDetected() {
     68    return this.#vpnAddonDetected;
     69  }
     70 }
     71 
     72 const IPPVPNAddonHelper = new VPNAddonHelperSingleton();
     73 
     74 export { IPPVPNAddonHelper };