tor-browser

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

helpers.js (1797B)


      1 /**
      2 * Any copyright is dedicated to the Public Domain.
      3 * http://creativecommons.org/publicdomain/zero/1.0/
      4 */
      5 
      6 // testSteps is expected to be defined by the test using this file.
      7 /* global testSteps:false */
      8 
      9 function executeSoon(aFun) {
     10  SpecialPowers.Services.tm.dispatchToMainThread({
     11    run() {
     12      aFun();
     13    },
     14  });
     15 }
     16 
     17 function clearAllDatabases() {
     18  let qms = SpecialPowers.Services.qms;
     19  let principal = SpecialPowers.wrap(document).nodePrincipal;
     20  let request = qms.clearStoragesForPrincipal(principal);
     21  return request;
     22 }
     23 
     24 if (!window.runTest) {
     25  window.runTest = async function () {
     26    SimpleTest.waitForExplicitFinish();
     27 
     28    info("Pushing preferences");
     29 
     30    await SpecialPowers.pushPrefEnv({
     31      set: [
     32        ["dom.storage.testing", true],
     33        ["dom.quotaManager.testing", true],
     34      ],
     35    });
     36 
     37    info("Clearing old databases");
     38 
     39    await requestFinished(clearAllDatabases());
     40 
     41    SimpleTest.registerCleanupFunction(async function () {
     42      await requestFinished(clearAllDatabases());
     43    });
     44  };
     45 }
     46 
     47 function returnToEventLoop() {
     48  return new Promise(function (resolve) {
     49    executeSoon(resolve);
     50  });
     51 }
     52 
     53 function getLocalStorage() {
     54  return localStorage;
     55 }
     56 
     57 class RequestError extends Error {
     58  constructor(resultCode, resultName) {
     59    super(`Request failed (code: ${resultCode}, name: ${resultName})`);
     60    this.name = "RequestError";
     61    this.resultCode = resultCode;
     62    this.resultName = resultName;
     63  }
     64 }
     65 
     66 async function requestFinished(request) {
     67  await new Promise(function (resolve) {
     68    request.callback = SpecialPowers.wrapCallback(function () {
     69      resolve();
     70    });
     71  });
     72 
     73  if (request.resultCode !== SpecialPowers.Cr.NS_OK) {
     74    throw new RequestError(request.resultCode, request.resultName);
     75  }
     76 
     77  return request.result;
     78 }