tor-browser

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

AIWindowAccountAuth.sys.mjs (2904B)


      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 import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs";
      6 
      7 const lazy = {};
      8 
      9 ChromeUtils.defineESModuleGetters(lazy, {
     10  SpecialMessageActions:
     11    "resource://messaging-system/lib/SpecialMessageActions.sys.mjs",
     12 });
     13 
     14 ChromeUtils.defineLazyGetter(lazy, "fxAccounts", () => {
     15  return ChromeUtils.importESModule(
     16    "resource://gre/modules/FxAccounts.sys.mjs"
     17  ).getFxAccountsSingleton();
     18 });
     19 
     20 ChromeUtils.defineLazyGetter(lazy, "log", function () {
     21  return console.createInstance({
     22    prefix: "AIWindowAccountAuth",
     23    maxLogLevelPref: Services.prefs.getBoolPref("browser.aiwindow.log", false)
     24      ? "Debug"
     25      : "Warn",
     26  });
     27 });
     28 
     29 // Temporary gating while feature is in development
     30 // To be set to true by default before MVP launch
     31 XPCOMUtils.defineLazyPreferenceGetter(
     32  lazy,
     33  "AIWindowRequireSignIn",
     34  "browser.aiwindow.requireSignIn",
     35  false
     36 );
     37 XPCOMUtils.defineLazyPreferenceGetter(
     38  lazy,
     39  "hasAIWindowToSConsent",
     40  "browser.aiwindow.tos.hasConsent",
     41  false
     42 );
     43 
     44 export const AIWindowAccountAuth = {
     45  get hasToSConsent() {
     46    return lazy.hasAIWindowToSConsent;
     47  },
     48 
     49  set hasToSConsent(value) {
     50    Services.prefs.setBoolPref("browser.aiwindow.tos.hasConsent", value);
     51  },
     52 
     53  async isSignedIn() {
     54    try {
     55      const userData = await lazy.fxAccounts.getSignedInUser();
     56      return !!userData;
     57    } catch (error) {
     58      lazy.log.error("Error checking sign-in status:", error);
     59      return false;
     60    }
     61  },
     62 
     63  requiresSignIn() {
     64    return lazy.AIWindowRequireSignIn;
     65  },
     66 
     67  async canAccessAIWindow() {
     68    if (!this.requiresSignIn()) {
     69      return true;
     70    }
     71    if (!this.hasToSConsent) {
     72      return false;
     73    }
     74    return await this.isSignedIn();
     75  },
     76 
     77  async promptSignIn(browser) {
     78    try {
     79      const data = {
     80        autoClose: false,
     81        entrypoint: "aiwindow",
     82        extraParams: {
     83          service: "aiwindow",
     84        },
     85      };
     86      const signedIn = await lazy.SpecialMessageActions.fxaSignInFlow(
     87        data,
     88        browser
     89      );
     90      if (signedIn) {
     91        this.hasToSConsent = true;
     92      }
     93      return signedIn;
     94    } catch (error) {
     95      lazy.log.error("Error prompting sign-in:", error);
     96      throw error;
     97    }
     98  },
     99 
    100  async launchAIWindow(browser) {
    101    if (!(await this.canAccessAIWindow())) {
    102      const signedIn = await this.promptSignIn(browser);
    103      if (!signedIn) {
    104        lazy.log.error("User did not sign in successfully.");
    105        return false;
    106      }
    107    }
    108    // Proceed with launching the AI window
    109    // Tobe updated with window switching toggleWindow call implemented with fix of bug 2006469
    110    browser.ownerGlobal.OpenBrowserWindow({ aiWindow: true });
    111    return true;
    112  },
    113 };