summaryrefslogtreecommitdiffstats
path: root/dom/workers/test/script_createFile.js
blob: d49ea188670cd39f82f3da25d95bdebd1f66ca58 (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
/* eslint-env mozilla/chrome-script */

Cu.importGlobalProperties(["File"]);

addMessageListener("file.open", function (e) {
  var tmpFile = Cc["@mozilla.org/file/directory_service;1"]
    .getService(Ci.nsIDirectoryService)
    .QueryInterface(Ci.nsIProperties)
    .get("TmpD", Ci.nsIFile);
  tmpFile.append("file.txt");
  tmpFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0o600);

  File.createFromNsIFile(tmpFile).then(function (file) {
    sendAsyncMessage("file.opened", { data: file });
  });
});

addMessageListener("nonEmptyFile.open", function (e) {
  var tmpFile = Cc["@mozilla.org/file/directory_service;1"]
    .getService(Ci.nsIDirectoryService)
    .QueryInterface(Ci.nsIProperties)
    .get("TmpD", Ci.nsIFile);
  tmpFile.append("file.txt");
  tmpFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0o600);

  var outStream = Cc[
    "@mozilla.org/network/file-output-stream;1"
  ].createInstance(Ci.nsIFileOutputStream);
  outStream.init(
    tmpFile,
    0x02 | 0x08 | 0x20, // write, create, truncate
    0o666,
    0
  );
  var fileData = "Hello world!";
  outStream.write(fileData, fileData.length);
  outStream.close();

  File.createFromNsIFile(tmpFile).then(function (file) {
    sendAsyncMessage("nonEmptyFile.opened", { data: file });
  });
});