tor-browser

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

syncChooseWhatToSync.js (4187B)


      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 file,
      3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 /* import-globals-from /toolkit/content/preferencesBindings.js */
      6 
      7 const lazy = {};
      8 
      9 ChromeUtils.defineLazyGetter(lazy, "fxAccounts", () => {
     10  return ChromeUtils.importESModule(
     11    "resource://gre/modules/FxAccounts.sys.mjs"
     12  ).getFxAccountsSingleton();
     13 });
     14 
     15 Preferences.addAll([
     16  { id: "services.sync.engine.addons", type: "bool" },
     17  { id: "services.sync.engine.bookmarks", type: "bool" },
     18  { id: "services.sync.engine.history", type: "bool" },
     19  { id: "services.sync.engine.tabs", type: "bool" },
     20  { id: "services.sync.engine.prefs", type: "bool" },
     21  { id: "services.sync.engine.passwords", type: "bool" },
     22  { id: "services.sync.engine.addresses", type: "bool" },
     23  { id: "services.sync.engine.creditcards", type: "bool" },
     24 ]);
     25 
     26 let gSyncChooseWhatToSync = {
     27  init() {
     28    this._setupEventListeners();
     29    this._adjustForPrefs();
     30    let options = window.arguments[0];
     31    if (options.disconnectFun) {
     32      // Offer 'Disconnect' functionality if it was provided
     33      document.addEventListener("dialogextra2", function () {
     34        options.disconnectFun().then(disconnected => {
     35          if (disconnected) {
     36            window.close();
     37          }
     38        });
     39      });
     40    } else {
     41      // Hide the 'Disconnect' button if not applicable
     42      document.getElementById("syncChooseOptions").getButton("extra2").hidden =
     43        true;
     44    }
     45  },
     46 
     47  // make whatever tweaks we need based on preferences.
     48  _adjustForPrefs() {
     49    // These 2 engines are unique in that there are prefs that make the
     50    // entire engine unavailable (which is distinct from "disabled").
     51    let enginePrefs = [
     52      ["services.sync.engine.addresses", ".sync-engine-addresses"],
     53      ["services.sync.engine.creditcards", ".sync-engine-creditcards"],
     54    ];
     55    for (let [enabledPref, className] of enginePrefs) {
     56      let availablePref = enabledPref + ".available";
     57      // If the engine is enabled we force it to be available, otherwise we see
     58      // spooky things happen (like it magically re-appear later)
     59      if (Services.prefs.getBoolPref(enabledPref, false)) {
     60        Services.prefs.setBoolPref(availablePref, true);
     61      }
     62      if (!Services.prefs.getBoolPref(availablePref)) {
     63        let elt = document.querySelector(className);
     64        elt.hidden = true;
     65      }
     66    }
     67  },
     68  _setupEventListeners() {
     69    document.addEventListener("dialogaccept", () => {
     70      // Record when the user saves sync settings.
     71      let settings = this._getSyncEngineEnablementChanges();
     72      lazy.fxAccounts.telemetry.recordSaveSyncSettings(settings).catch(err => {
     73        console.error("Failed to record save sync settings event", err);
     74      });
     75    });
     76  },
     77  _getSyncEngineEnablementChanges() {
     78    let engines = [
     79      "addons",
     80      "bookmarks",
     81      "history",
     82      "tabs",
     83      "prefs",
     84      "passwords",
     85      "addresses",
     86      "creditcards",
     87    ];
     88    let settings = {
     89      enabledEngines: [],
     90      disabledEngines: [],
     91    };
     92 
     93    for (let engine of engines) {
     94      let enabledPref = "services.sync.engine." + engine;
     95      let checkboxId = "syncEngine" + engine[0].toUpperCase() + engine.slice(1);
     96      let checkboxValue = document.getElementById(checkboxId).checked;
     97 
     98      // Check if the engine's stored pref value is the same as the engine's
     99      // checkbox value in the choose what to sync menu. If they aren't equal
    100      // and the checkbox is checked, we add the engine to the enabled list; if
    101      // the checkbox isn't checked we add the engine to the disabled list. If
    102      // the pref and the checkbox value are equal we do nothing as nothing was
    103      // changed.
    104      if (Services.prefs.getBoolPref(enabledPref, false) !== checkboxValue) {
    105        if (checkboxValue === true) {
    106          settings.enabledEngines.push(engine);
    107        } else if (checkboxValue === false) {
    108          settings.disabledEngines.push(engine);
    109        }
    110      }
    111    }
    112    return settings;
    113  },
    114 };
    115 
    116 window.addEventListener("load", () => gSyncChooseWhatToSync.init());