tor-browser

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

StorageUtils.mjs (1848B)


      1 /**
      2 * Any copyright is dedicated to the Public Domain.
      3 * http://creativecommons.org/publicdomain/zero/1.0/
      4 */
      5 
      6 // This file expectes the SpecialPowers to be available in the scope
      7 // it is loaded into.
      8 /* global SpecialPowers */
      9 
     10 class RequestError extends Error {
     11  constructor(resultCode, resultName) {
     12    super(`Request failed (code: ${resultCode}, name: ${resultName})`);
     13    this.name = "RequestError";
     14    this.resultCode = resultCode;
     15    this.resultName = resultName;
     16  }
     17 }
     18 
     19 export async function setStoragePrefs(optionalPrefsToSet) {
     20  const prefsToSet = [
     21    // Not needed right now, but might be needed in future.
     22    // ["dom.quotaManager.testing", true],
     23  ];
     24 
     25  if (SpecialPowers.Services.appinfo.OS === "WINNT") {
     26    prefsToSet.push(["dom.quotaManager.useDOSDevicePathSyntax", true]);
     27  }
     28 
     29  if (optionalPrefsToSet) {
     30    prefsToSet.push(...optionalPrefsToSet);
     31  }
     32 
     33  await SpecialPowers.pushPrefEnv({ set: prefsToSet });
     34 }
     35 
     36 export async function getCachedUsageForOrigin(principal) {
     37  const request = SpecialPowers.Services.qms.getCachedUsageForPrincipal(
     38    principal,
     39    function () {}
     40  );
     41 
     42  await new Promise(function (resolve) {
     43    request.callback = SpecialPowers.wrapCallback(function () {
     44      resolve();
     45    });
     46  });
     47 
     48  if (request.resultCode != SpecialPowers.Cr.NS_OK) {
     49    throw new RequestError(request.resultCode, request.resultName);
     50  }
     51 
     52  return request.result;
     53 }
     54 
     55 export async function clearStoragesForOrigin(principal) {
     56  const request =
     57    SpecialPowers.Services.qms.clearStoragesForPrincipal(principal);
     58 
     59  await new Promise(function (resolve) {
     60    request.callback = SpecialPowers.wrapCallback(function () {
     61      resolve();
     62    });
     63  });
     64 
     65  if (request.resultCode != SpecialPowers.Cr.NS_OK) {
     66    throw new RequestError(request.resultCode, request.resultName);
     67  }
     68 
     69  return request.result;
     70 }