tor-browser

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

remote_settings_update_check.js (2184B)


      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 /* globals browser, module, onMessageFromTab */
      6 
      7 let currentVersion = browser.runtime.getManifest().version;
      8 
      9 function isUpdateWanted(update) {
     10  if (!update) {
     11    return false;
     12  }
     13 
     14  if (!update?.version || (!update?.interventions && !update.shims)) {
     15    console.error(
     16      "Received invalid WebCompat interventions update from Remote Settings",
     17      update
     18    );
     19    return false;
     20  }
     21 
     22  if (!this.isNewerVersion(update.version, currentVersion)) {
     23    console.error(
     24      "Ignoring latest WebCompat Remote Settings update",
     25      update.version,
     26      "<=",
     27      currentVersion
     28    );
     29    return false;
     30  }
     31 
     32  return true;
     33 }
     34 
     35 function isNewerVersion(a_raw, b_raw) {
     36  function num(n) {
     37    const i = parseInt(n);
     38    return isNaN(i) ? 0 : i;
     39  }
     40  const a_comp = a_raw.split(".");
     41  const b_comp = b_raw.split(".");
     42  const a = [num(a_comp[0]), num(a_comp[1]), num(a_comp[2]), num(a_comp[3])];
     43  const b = [num(b_comp[0]), num(b_comp[1]), num(b_comp[2]), num(b_comp[3])];
     44  for (let i = 0; i < 4; ++i) {
     45    if (a[i] > b[i]) {
     46      return true;
     47    }
     48    if (a[i] < b[i]) {
     49      return false;
     50    }
     51  }
     52  return false;
     53 }
     54 
     55 function listenForRemoteSettingsUpdates(interventions, shims) {
     56  browser.remoteSettings.onRemoteSettingsUpdate.addListener(async update => {
     57    if (!isUpdateWanted(update)) {
     58      console.info(
     59        "Ignoring older version of webcompat interventions",
     60        update.version
     61      );
     62      return;
     63    }
     64 
     65    console.info("Received update to webcompat interventions", update.version);
     66    currentVersion = update.version;
     67 
     68    if (update.interventions) {
     69      await interventions.onRemoteSettingsUpdate(update.interventions);
     70    }
     71 
     72    if (update.shims) {
     73      await shims.onRemoteSettingsUpdate(update.shims);
     74    }
     75  });
     76 
     77  window._downgradeForTesting = async () => {
     78    currentVersion = browser.runtime.getManifest().version;
     79    await interventions._resetToDefaultInterventions();
     80    await shims._resetToDefaultShims();
     81  };
     82 }