tor-browser

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

head_cache.js (3463B)


      1 "use strict";
      2 
      3 var { XPCOMUtils } = ChromeUtils.importESModule(
      4  "resource://gre/modules/XPCOMUtils.sys.mjs"
      5 );
      6 
      7 function evict_cache_entries(where, lci = null) {
      8  var clearDisk = !where || where == "disk" || where == "all";
      9  var clearMem = !where || where == "memory" || where == "all";
     10 
     11  var storage;
     12 
     13  if (clearMem) {
     14    storage = Services.cache2.memoryCacheStorage(
     15      lci ? lci : Services.loadContextInfo.default
     16    );
     17    storage.asyncEvictStorage(null);
     18  }
     19 
     20  if (clearDisk) {
     21    storage = Services.cache2.diskCacheStorage(
     22      lci ? lci : Services.loadContextInfo.default
     23    );
     24    storage.asyncEvictStorage(null);
     25  }
     26 }
     27 
     28 function createURI(urispec) {
     29  return Services.io.newURI(urispec);
     30 }
     31 
     32 function getCacheStorage(where, lci) {
     33  if (!lci) {
     34    lci = Services.loadContextInfo.default;
     35  }
     36  switch (where) {
     37    case "disk":
     38      return Services.cache2.diskCacheStorage(lci);
     39    case "memory":
     40      return Services.cache2.memoryCacheStorage(lci);
     41    case "pin":
     42      return Services.cache2.pinningCacheStorage(lci);
     43  }
     44  return null;
     45 }
     46 
     47 function asyncOpenCacheEntry(key, where, flags, lci, callback) {
     48  key = createURI(key);
     49 
     50  function CacheListener() {}
     51  CacheListener.prototype = {
     52    QueryInterface: ChromeUtils.generateQI(["nsICacheEntryOpenCallback"]),
     53 
     54    onCacheEntryCheck(entry) {
     55      if (typeof callback === "object") {
     56        return callback.onCacheEntryCheck(entry);
     57      }
     58      return Ci.nsICacheEntryOpenCallback.ENTRY_WANTED;
     59    },
     60 
     61    onCacheEntryAvailable(entry, isnew, status) {
     62      if (typeof callback === "object") {
     63        // Root us at the callback
     64        callback.__cache_listener_root = this;
     65        callback.onCacheEntryAvailable(entry, isnew, status);
     66      } else {
     67        callback(status, entry);
     68      }
     69    },
     70 
     71    run() {
     72      var storage = getCacheStorage(where, lci);
     73      storage.asyncOpenURI(key, "", flags, this);
     74    },
     75  };
     76 
     77  new CacheListener().run();
     78 }
     79 
     80 function syncWithCacheIOThread(callback, force) {
     81  if (force) {
     82    asyncOpenCacheEntry(
     83      "http://nonexistententry/",
     84      "disk",
     85      Ci.nsICacheStorage.OPEN_READONLY,
     86      null,
     87      function (status) {
     88        Assert.equal(status, Cr.NS_ERROR_CACHE_KEY_NOT_FOUND);
     89        callback();
     90      }
     91    );
     92  } else {
     93    callback();
     94  }
     95 }
     96 
     97 function get_device_entry_count(where, lci, continuation) {
     98  var storage = getCacheStorage(where, lci);
     99  if (!storage) {
    100    continuation(-1, 0);
    101    return;
    102  }
    103 
    104  var visitor = {
    105    onCacheStorageInfo(entryCount, consumption) {
    106      executeSoon(function () {
    107        continuation(entryCount, consumption);
    108      });
    109    },
    110  };
    111 
    112  // get the device entry count
    113  storage.asyncVisitStorage(visitor, false);
    114 }
    115 
    116 function asyncCheckCacheEntryPresence(
    117  key,
    118  where,
    119  shouldExist,
    120  lci,
    121  continuation
    122 ) {
    123  asyncOpenCacheEntry(
    124    key,
    125    where,
    126    Ci.nsICacheStorage.OPEN_READONLY,
    127    lci,
    128    function (status, entry) {
    129      if (shouldExist) {
    130        dump(
    131          "TEST-INFO | status: " +
    132            status +
    133            " checking cache key " +
    134            key +
    135            " exists @ " +
    136            where
    137        );
    138        Assert.equal(status, Cr.NS_OK);
    139        Assert.ok(!!entry);
    140      } else {
    141        dump(
    142          "TEST-INFO | checking cache key " + key + " doesn't exist @ " + where
    143        );
    144        Assert.equal(status, Cr.NS_ERROR_CACHE_KEY_NOT_FOUND);
    145        Assert.equal(null, entry);
    146      }
    147      continuation();
    148    }
    149  );
    150 }