tor-browser

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

IPPSignInWatcher.sys.mjs (1985B)


      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  IPProtectionService:
      9    "moz-src:///browser/components/ipprotection/IPProtectionService.sys.mjs",
     10  UIState: "resource://services-sync/UIState.sys.mjs",
     11 });
     12 
     13 /**
     14 * This class monitors the Sign-In state and triggers the update of the service
     15 * if needed.
     16 */
     17 class IPPSignInWatcherSingleton extends EventTarget {
     18  #signedIn = false;
     19 
     20  get isSignedIn() {
     21    return this.#signedIn;
     22  }
     23 
     24  set isSignedIn(signedIn) {
     25    this.#signedIn = signedIn;
     26  }
     27 
     28  init() {
     29    this.#signedIn = Services.prefs.prefHasUserValue("services.sync.username");
     30  }
     31 
     32  /**
     33   * Adds an observer for the FxA sign-in state, only when the browser is fully started.
     34   */
     35  async initOnStartupCompleted() {
     36    this.fxaObserver = {
     37      QueryInterface: ChromeUtils.generateQI([
     38        Ci.nsIObserver,
     39        Ci.nsISupportsWeakReference,
     40      ]),
     41 
     42      observe() {
     43        let { status } = lazy.UIState.get();
     44        let signedIn = status == lazy.UIState.STATUS_SIGNED_IN;
     45        if (signedIn !== IPPSignInWatcher.isSignedIn) {
     46          IPPSignInWatcher.isSignedIn = signedIn;
     47          lazy.IPProtectionService.updateState();
     48 
     49          IPPSignInWatcher.dispatchEvent(
     50            new CustomEvent("IPPSignInWatcher:StateChanged", {
     51              bubbles: true,
     52              composed: true,
     53            })
     54          );
     55        }
     56      },
     57    };
     58 
     59    Services.obs.addObserver(this.fxaObserver, lazy.UIState.ON_UPDATE);
     60  }
     61 
     62  /**
     63   * Removes the FxA sign-in state observer
     64   */
     65  uninit() {
     66    if (this.fxaObserver) {
     67      Services.obs.removeObserver(this.fxaObserver, lazy.UIState.ON_UPDATE);
     68      this.fxaObserver = null;
     69    }
     70  }
     71 }
     72 
     73 const IPPSignInWatcher = new IPPSignInWatcherSingleton();
     74 
     75 export { IPPSignInWatcher };