summaryrefslogtreecommitdiffstats
path: root/dom/cache/test/xpcshell/make_profile.js
blob: 5928000e69236fe37dbb72c164e71ca03ac610df (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/**
 * 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;

// Enumerate the directory tree and store results in entryList as
//
//  { path: 'a/b/c', file: <nsIFile> }
//
// The algorithm starts with the first entry already in entryList.
function enumerate_tree(entryList) {
  for (var index = 0; index < entryList.length; ++index) {
    var path = entryList[index].path;
    var file = entryList[index].file;

    if (file.isDirectory()) {
      var dirList = file.directoryEntries;
      while (dirList.hasMoreElements()) {
        var dirFile = dirList.getNext().QueryInterface(Ci.nsIFile);
        entryList.push({ path: path + '/' + dirFile.leafName, file: dirFile });
      }
    }
  }
}

function zip_profile(zipFile, profileDir) {
  var zipWriter = Cc['@mozilla.org/zipwriter;1']
                  .createInstance(Ci.nsIZipWriter);
  zipWriter.open(zipFile, 0x04 | 0x08 | 0x20);

  var root = profileDir.clone();
  root.append('storage');
  root.append('default');
  root.append('chrome');

  var entryList = [{path: 'storage/default/chrome', file: root}];
  enumerate_tree(entryList);

  entryList.forEach(function(entry) {
    if (entry.file.isDirectory()) {
      zipWriter.addEntryDirectory(entry.path, entry.file.lastModifiedTime,
                                  false);
    } else {
      var istream = Cc['@mozilla.org/network/file-input-stream;1']
                    .createInstance(Ci.nsIFileInputStream);
      istream.init(entry.file, -1, -1, 0);
      zipWriter.addEntryStream(entry.path, entry.file.lastModifiedTime,
                               Ci.nsIZipWriter.COMPRESSION_DEFAULT, istream,
                               false);
      istream.close();
    }
  });

  zipWriter.close();
}

function exactGC() {
  return new Promise(function(resolve) {
    var count = 0;
    function doPreciseGCandCC() {
      function scheduleGCCallback() {
        Cu.forceCC();

        if (++count < 2) {
          doPreciseGCandCC();
        } else {
          resolve();
        }
      }
      Cu.schedulePreciseGC(scheduleGCCallback);
    }
    doPreciseGCandCC();
  });
}

function resetQuotaManager() {
  return new Promise(function(resolve) {
    var qm = Cc['@mozilla.org/dom/quota/manager;1']
             .getService(Ci.nsIQuotaManager);

    var prefService = Cc['@mozilla.org/preferences-service;1']
                      .getService(Ci.nsIPrefService);

    // enable quota manager testing mode
    var pref = 'dom.quotaManager.testing';
    prefService.getBranch(null).setBoolPref(pref, true);

    var request = qm.reset();
    request.callback = resolve;

    // disable quota manager testing mode
    //prefService.getBranch(null).setBoolPref(pref, false);
  });
}

function run_test() {
  do_test_pending();
  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 zipFile = currentDir.clone();
  zipFile.append('new_profile.zip');
  if (zipFile.exists()) {
    zipFile.remove(false);
  }
  ok(!zipFile.exists());

  caches.open('xpcshell-test').then(function(c) {
    var request = new Request('http://example.com/index.html');
    var response = new Response('hello world');
    return c.put(request, response);
  }).then(exactGC).then(resetQuotaManager).then(function() {
    zip_profile(zipFile, profileDir);
    dump('### ### created zip at: ' + zipFile.path + '\n');
    do_test_finished();
  }).catch(function(e) {
    do_test_finished();
    ok(false, e);
  });
}