summaryrefslogtreecommitdiffstats
path: root/dom/quota/test/common/xpcshell.js
blob: ed3afaa4670707de033914534c181b83d35f8e7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/**
 * Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

loadScript("dom/quota/test/common/system.js");

function enableStorageTesting() {
  Services.prefs.setBoolPref("dom.quotaManager.testing", true);
  Services.prefs.setBoolPref("dom.storageManager.enabled", true);
  Services.prefs.setBoolPref("dom.simpleDB.enabled", true);
  if (Services.appinfo.OS === "WINNT") {
    Services.prefs.setBoolPref("dom.quotaManager.useDOSDevicePathSyntax", true);
  }
}

function resetStorageTesting() {
  Services.prefs.clearUserPref("dom.quotaManager.testing");
  Services.prefs.clearUserPref("dom.storageManager.enabled");
  Services.prefs.clearUserPref("dom.simpleDB.enabled");
  if (Services.appinfo.OS === "WINNT") {
    Services.prefs.clearUserPref("dom.quotaManager.useDOSDevicePathSyntax");
  }
}

function clear(callback) {
  let request = Services.qms.clear();
  request.callback = callback;

  return request;
}

function reset(callback) {
  let request = Services.qms.reset();
  request.callback = callback;

  return request;
}

function installPackage(packageRelativePath, allowFileOverwrites) {
  let currentDir = Services.dirsvc.get("CurWorkD", Ci.nsIFile);

  let packageFile = getRelativeFile(packageRelativePath + ".zip", currentDir);

  let zipReader = Cc["@mozilla.org/libjar/zip-reader;1"].createInstance(
    Ci.nsIZipReader
  );
  zipReader.open(packageFile);

  let entryNames = Array.from(zipReader.findEntries(null));
  entryNames.sort();

  for (let entryName of entryNames) {
    if (entryName.match(/^create_db\.(html|js)/)) {
      continue;
    }

    let zipentry = zipReader.getEntry(entryName);

    let file = getRelativeFile(entryName);

    if (zipentry.isDirectory) {
      if (!file.exists()) {
        file.create(Ci.nsIFile.DIRECTORY_TYPE, parseInt("0755", 8));
      }
    } else {
      if (!allowFileOverwrites && file.exists()) {
        throw new Error("File already exists!");
      }

      let 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);

      let bostream = Cc[
        "@mozilla.org/network/buffered-output-stream;1"
      ].createInstance(Ci.nsIBufferedOutputStream);
      bostream.init(ostream, 32768);

      bostream.writeFrom(istream, istream.available());

      istream.close();
      bostream.close();
    }
  }

  zipReader.close();
}