tor-browser

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

head.js (5462B)


      1 /**
      2 * Any copyright is dedicated to the Public Domain.
      3 * http://creativecommons.org/publicdomain/zero/1.0/
      4 *
      5 * All images in schema_15_profile.zip are from https://github.com/mdn/sw-test/
      6 * and are CC licensed by https://www.flickr.com/photos/legofenris/.
      7 */
      8 
      9 // testSteps is expected to be defined by the file including this file.
     10 /* global testSteps */
     11 
     12 const NS_APP_USER_PROFILE_50_DIR = "ProfD";
     13 const osWindowsName = "WINNT";
     14 const pathDelimiter = "/";
     15 
     16 const persistentPersistence = "persistent";
     17 const defaultPersistence = "default";
     18 
     19 const storageDirName = "storage";
     20 const persistentPersistenceDirName = "permanent";
     21 const defaultPersistenceDirName = "default";
     22 
     23 function cacheClientDirName() {
     24  return "cache";
     25 }
     26 
     27 // services required be initialized in order to run CacheStorage
     28 var ss = Cc["@mozilla.org/storage/service;1"].createInstance(
     29  Ci.mozIStorageService
     30 );
     31 var sts = Cc["@mozilla.org/network/stream-transport-service;1"].getService(
     32  Ci.nsIStreamTransportService
     33 );
     34 var hash = Cc["@mozilla.org/security/hash;1"].createInstance(Ci.nsICryptoHash);
     35 
     36 class RequestError extends Error {
     37  constructor(resultCode, resultName) {
     38    super(`Request failed (code: ${resultCode}, name: ${resultName})`);
     39    this.name = "RequestError";
     40    this.resultCode = resultCode;
     41    this.resultName = resultName;
     42  }
     43 }
     44 
     45 add_setup(function () {
     46  do_get_profile();
     47 
     48  enableTesting();
     49 
     50  Cu.importGlobalProperties(["caches"]);
     51 
     52  registerCleanupFunction(resetTesting);
     53 });
     54 
     55 function enableTesting() {
     56  Services.prefs.setBoolPref("dom.caches.testing.enabled", true);
     57  Services.prefs.setBoolPref("dom.simpleDB.enabled", true);
     58  Services.prefs.setBoolPref("dom.quotaManager.testing", true);
     59 }
     60 
     61 function resetTesting() {
     62  Services.prefs.clearUserPref("dom.quotaManager.testing");
     63  Services.prefs.clearUserPref("dom.simpleDB.enabled");
     64  Services.prefs.clearUserPref("dom.caches.testing.enabled");
     65 }
     66 
     67 function initStorage() {
     68  return Services.qms.init();
     69 }
     70 
     71 function initTemporaryStorage() {
     72  return Services.qms.initTemporaryStorage();
     73 }
     74 
     75 function initPersistentOrigin(principal) {
     76  return Services.qms.initializePersistentOrigin(principal);
     77 }
     78 
     79 function initTemporaryOrigin(principal, createIfNonExistent = true) {
     80  return Services.qms.initializeTemporaryOrigin(
     81    "default",
     82    principal,
     83    createIfNonExistent
     84  );
     85 }
     86 
     87 function clearOrigin(principal, persistence) {
     88  let request = Services.qms.clearStoragesForPrincipal(principal, persistence);
     89 
     90  return request;
     91 }
     92 
     93 function reset() {
     94  return Services.qms.reset();
     95 }
     96 
     97 async function requestFinished(request) {
     98  await new Promise(function (resolve) {
     99    request.callback = function () {
    100      resolve();
    101    };
    102  });
    103 
    104  if (request.resultCode !== Cr.NS_OK) {
    105    throw new RequestError(request.resultCode, request.resultName);
    106  }
    107 
    108  return request.result;
    109 }
    110 
    111 // Extract a zip file into the profile
    112 function create_test_profile(zipFileName) {
    113  var directoryService = Services.dirsvc;
    114 
    115  var profileDir = directoryService.get(NS_APP_USER_PROFILE_50_DIR, Ci.nsIFile);
    116  var currentDir = directoryService.get("CurWorkD", Ci.nsIFile);
    117 
    118  var packageFile = currentDir.clone();
    119  packageFile.append(zipFileName);
    120 
    121  var zipReader = Cc["@mozilla.org/libjar/zip-reader;1"].createInstance(
    122    Ci.nsIZipReader
    123  );
    124  zipReader.open(packageFile);
    125 
    126  var entryNames = Array.from(zipReader.findEntries(null));
    127  entryNames.sort();
    128 
    129  for (var entryName of entryNames) {
    130    var zipentry = zipReader.getEntry(entryName);
    131 
    132    var file = profileDir.clone();
    133    entryName.split(pathDelimiter).forEach(function (part) {
    134      file.append(part);
    135    });
    136 
    137    if (zipentry.isDirectory) {
    138      file.create(Ci.nsIFile.DIRECTORY_TYPE, parseInt("0755", 8));
    139    } else {
    140      var istream = zipReader.getInputStream(entryName);
    141 
    142      var ostream = Cc[
    143        "@mozilla.org/network/file-output-stream;1"
    144      ].createInstance(Ci.nsIFileOutputStream);
    145      ostream.init(file, -1, parseInt("0644", 8), 0);
    146 
    147      var bostream = Cc[
    148        "@mozilla.org/network/buffered-output-stream;1"
    149      ].createInstance(Ci.nsIBufferedOutputStream);
    150      bostream.init(ostream, 32 * 1024);
    151 
    152      bostream.writeFrom(istream, istream.available());
    153 
    154      istream.close();
    155      bostream.close();
    156    }
    157  }
    158 
    159  zipReader.close();
    160 }
    161 
    162 function getCacheDir() {
    163  return getRelativeFile(
    164    `${storageDirName}/${defaultPersistenceDirName}/chrome/${cacheClientDirName()}`
    165  );
    166 }
    167 
    168 function getPrincipal(url, attrs) {
    169  let uri = Services.io.newURI(url);
    170  if (!attrs) {
    171    attrs = {};
    172  }
    173  return Services.scriptSecurityManager.createContentPrincipal(uri, attrs);
    174 }
    175 
    176 function getDefaultPrincipal() {
    177  return getPrincipal("http://example.com");
    178 }
    179 
    180 function getRelativeFile(relativePath) {
    181  let file = Services.dirsvc
    182    .get(NS_APP_USER_PROFILE_50_DIR, Ci.nsIFile)
    183    .clone();
    184 
    185  if (Services.appinfo.OS === osWindowsName) {
    186    let winFile = file.QueryInterface(Ci.nsILocalFileWin);
    187    winFile.useDOSDevicePathSyntax = true;
    188  }
    189 
    190  relativePath.split(pathDelimiter).forEach(function (component) {
    191    if (component == "..") {
    192      file = file.parent;
    193    } else {
    194      file.append(component);
    195    }
    196  });
    197 
    198  return file;
    199 }
    200 
    201 function getSimpleDatabase(principal, persistence) {
    202  let connection = Cc["@mozilla.org/dom/sdb-connection;1"].createInstance(
    203    Ci.nsISDBConnection
    204  );
    205 
    206  if (!principal) {
    207    principal = getDefaultPrincipal();
    208  }
    209 
    210  connection.init(principal, persistence);
    211 
    212  return connection;
    213 }