tor-browser

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

ForceRefreshParent.sys.mjs (2259B)


      1 var maxCacheLoadCount = 3;
      2 var cachedLoadCount = 0;
      3 var baseLoadCount = 0;
      4 var done = false;
      5 
      6 export class ForceRefreshParent extends JSWindowActorParent {
      7  constructor() {
      8    super();
      9  }
     10 
     11  receiveMessage(msg) {
     12    // if done is called, ignore the msg.
     13    if (done) {
     14      return;
     15    }
     16    if (msg.data.type === "base-load") {
     17      baseLoadCount += 1;
     18      if (cachedLoadCount === maxCacheLoadCount) {
     19        ForceRefreshParent.SimpleTest.is(
     20          baseLoadCount,
     21          2,
     22          "cached load should occur before second base load"
     23        );
     24        done = true;
     25        ForceRefreshParent.done();
     26        return;
     27      }
     28      if (baseLoadCount !== 1) {
     29        ForceRefreshParent.SimpleTest.ok(
     30          false,
     31          "base load without cached load should only occur once"
     32        );
     33        done = true;
     34        ForceRefreshParent.done();
     35      }
     36    } else if (msg.data.type === "base-register") {
     37      ForceRefreshParent.SimpleTest.ok(
     38        !cachedLoadCount,
     39        "cached load should not occur before base register"
     40      );
     41      ForceRefreshParent.SimpleTest.is(
     42        baseLoadCount,
     43        1,
     44        "register should occur after first base load"
     45      );
     46    } else if (msg.data.type === "base-sw-ready") {
     47      ForceRefreshParent.SimpleTest.ok(
     48        !cachedLoadCount,
     49        "cached load should not occur before base ready"
     50      );
     51      ForceRefreshParent.SimpleTest.is(
     52        baseLoadCount,
     53        1,
     54        "ready should occur after first base load"
     55      );
     56      ForceRefreshParent.refresh();
     57    } else if (msg.data.type === "cached-load") {
     58      ForceRefreshParent.SimpleTest.ok(
     59        cachedLoadCount < maxCacheLoadCount,
     60        "cached load should not occur too many times"
     61      );
     62      ForceRefreshParent.SimpleTest.is(
     63        baseLoadCount,
     64        1,
     65        "cache load occur after first base load"
     66      );
     67      cachedLoadCount += 1;
     68      if (cachedLoadCount < maxCacheLoadCount) {
     69        ForceRefreshParent.refresh();
     70        return;
     71      }
     72      ForceRefreshParent.forceRefresh();
     73    } else if (msg.data.type === "cached-failure") {
     74      ForceRefreshParent.SimpleTest.ok(false, "failure: " + msg.data.detail);
     75      done = true;
     76      ForceRefreshParent.done();
     77    }
     78  }
     79 }