fune/dom/cache/test/xpcshell/head.js
monmanuela 585af41b87 Bug 1497604 - Enable ESLint for dom/cache (manual changes) r=Standard8,hsivonen
Depends on D20943

Differential Revision: https://phabricator.services.mozilla.com/D20944

--HG--
extra : moz-landing-system : lando
2019-03-06 08:03:42 +00:00

84 lines
2.5 KiB
JavaScript

/**
* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/
*
* All images in schema_15_profile.zip are from https://github.com/mdn/sw-test/
* and are CC licensed by https://www.flickr.com/photos/legofenris/.
*/
const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
// services required be initialized in order to run CacheStorage
var ss = Cc["@mozilla.org/storage/service;1"]
.createInstance(Ci.mozIStorageService);
var sts = Cc["@mozilla.org/network/stream-transport-service;1"]
.getService(Ci.nsIStreamTransportService);
var hash = Cc["@mozilla.org/security/hash;1"]
.createInstance(Ci.nsICryptoHash);
// Expose Cache and Fetch symbols on the global
Cu.importGlobalProperties(["caches", "File", "fetch"]);
// Extract a zip file into the profile
function create_test_profile(zipFileName) {
do_get_profile();
var directoryService = Services.dirsvc;
var profileDir = directoryService.get("ProfD", Ci.nsIFile);
var currentDir = directoryService.get("CurWorkD", Ci.nsIFile);
var packageFile = currentDir.clone();
packageFile.append(zipFileName);
var zipReader = Cc["@mozilla.org/libjar/zip-reader;1"]
.createInstance(Ci.nsIZipReader);
zipReader.open(packageFile);
var entryNames = Array.from(zipReader.findEntries(null));
entryNames.sort();
for (var entryName of entryNames) {
var zipentry = zipReader.getEntry(entryName);
var file = profileDir.clone();
entryName.split("/").forEach(function(part) {
file.append(part);
});
if (zipentry.isDirectory) {
file.create(Ci.nsIFile.DIRECTORY_TYPE, parseInt("0755", 8));
} else {
var istream = zipReader.getInputStream(entryName);
var ostream = Cc["@mozilla.org/network/file-output-stream;1"]
.createInstance(Ci.nsIFileOutputStream);
ostream.init(file, -1, parseInt("0644", 8), 0);
var bostream = Cc["@mozilla.org/network/buffered-output-stream;1"]
.createInstance(Ci.nsIBufferedOutputStream);
bostream.init(ostream, 32 * 1024);
bostream.writeFrom(istream, istream.available());
istream.close();
bostream.close();
}
}
zipReader.close();
}
function getCacheDir() {
let dirService = Services.dirsvc;
let profileDir = dirService.get("ProfD", Ci.nsIFile);
let cacheDir = profileDir.clone();
cacheDir.append("storage");
cacheDir.append("default");
cacheDir.append("chrome");
cacheDir.append("cache");
return cacheDir;
}