tor-browser

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

worker_wrapper.js (1964B)


      1 importScripts("utils.js");
      2 
      3 function getScriptUrl() {
      4  return new URL(location.href).searchParams.get("script");
      5 }
      6 
      7 importScripts(getScriptUrl());
      8 
      9 var client;
     10 var context;
     11 
     12 function ok(a, msg) {
     13  client.postMessage({
     14    type: "status",
     15    status: !!a,
     16    msg: a + ": " + msg,
     17    context,
     18  });
     19 }
     20 
     21 function is(a, b, msg) {
     22  client.postMessage({
     23    type: "status",
     24    status: a === b,
     25    msg: a + " === " + b + ": " + msg,
     26    context,
     27  });
     28 }
     29 
     30 addEventListener("message", function workerWrapperOnMessage(e) {
     31  removeEventListener("message", workerWrapperOnMessage);
     32  var data = e.data;
     33 
     34  function runTestAndReportToClient(event) {
     35    var done = function (res) {
     36      client.postMessage({ type: "finish", context });
     37      return res;
     38    };
     39 
     40    try {
     41      // runTest() is provided by the test.
     42      var result = runTest().then(done, done);
     43      if ("waitUntil" in event) {
     44        event.waitUntil(result);
     45      }
     46    } catch (e) {
     47      client.postMessage({
     48        type: "status",
     49        status: false,
     50        msg: "worker failed to run " + data.script + "; error: " + e.message,
     51        context,
     52      });
     53      done();
     54    }
     55  }
     56 
     57  if ("ServiceWorkerGlobalScope" in self) {
     58    // Fetch requests from a service worker are not intercepted.
     59    self.isSWPresent = false;
     60 
     61    e.waitUntil(
     62      self.clients
     63        .matchAll({ includeUncontrolled: true })
     64        .then(function (clients) {
     65          for (var i = 0; i < clients.length; ++i) {
     66            if (clients[i].url.indexOf("message_receiver.html") > -1) {
     67              client = clients[i];
     68              break;
     69            }
     70          }
     71          if (!client) {
     72            dump(
     73              "We couldn't find the message_receiver window, the test will fail\n"
     74            );
     75          }
     76          context = "ServiceWorker";
     77          runTestAndReportToClient(e);
     78        })
     79    );
     80  } else {
     81    client = self;
     82    context = "Worker";
     83    runTestAndReportToClient(e);
     84  }
     85 });