summaryrefslogtreecommitdiff
path: root/dom/cache/test/xpcshell/head.js
blob: 3d51929b3514a544f60525495adf03ce2cd4151c (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
/**
 * 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/.
 */

var Cc = Components.classes;
var Ci = Components.interfaces;
var Cu = Components.utils;

// 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', 'fetch']);

// Extract a zip file into the profile
function create_test_profile(zipFileName) {
  do_get_profile();

  var directoryService = Cc['@mozilla.org/file/directory_service;1']
                         .getService(Ci.nsIProperties);
  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 = [];
  var entries = zipReader.findEntries(null);
  while (entries.hasMore()) {
    var entry = entries.getNext();
    entryNames.push(entry);
  }
  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();
}