make_profile.js (3584B)
1 /** 2 * Any copyright is dedicated to the Public Domain. 3 * http://creativecommons.org/publicdomain/zero/1.0/ 4 * 5 * All images in schema_15_profile.zip are from https://github.com/mdn/sw-test/ 6 * and are CC licensed by https://www.flickr.com/photos/legofenris/. 7 */ 8 9 // Enumerate the directory tree and store results in entryList as 10 // 11 // { path: 'a/b/c', file: <nsIFile> } 12 // 13 // The algorithm starts with the first entry already in entryList. 14 function enumerate_tree(entryList) { 15 for (var index = 0; index < entryList.length; ++index) { 16 var path = entryList[index].path; 17 var file = entryList[index].file; 18 19 if (file.isDirectory()) { 20 var dirList = file.directoryEntries; 21 while (dirList.hasMoreElements()) { 22 var dirFile = dirList.nextFile; 23 entryList.push({ path: path + "/" + dirFile.leafName, file: dirFile }); 24 } 25 } 26 } 27 } 28 29 function zip_profile(zipFile, profileDir) { 30 var zipWriter = Cc["@mozilla.org/zipwriter;1"].createInstance( 31 Ci.nsIZipWriter 32 ); 33 zipWriter.open(zipFile, 0x04 | 0x08 | 0x20); 34 35 var root = profileDir.clone(); 36 root.append("storage"); 37 root.append("default"); 38 root.append("chrome"); 39 40 var entryList = [{ path: "storage/default/chrome", file: root }]; 41 enumerate_tree(entryList); 42 43 entryList.forEach(function (entry) { 44 if (entry.file.isDirectory()) { 45 zipWriter.addEntryDirectory( 46 entry.path, 47 entry.file.lastModifiedTime, 48 false 49 ); 50 } else { 51 var istream = Cc[ 52 "@mozilla.org/network/file-input-stream;1" 53 ].createInstance(Ci.nsIFileInputStream); 54 istream.init(entry.file, -1, -1, 0); 55 zipWriter.addEntryStream( 56 entry.path, 57 entry.file.lastModifiedTime, 58 Ci.nsIZipWriter.COMPRESSION_DEFAULT, 59 istream, 60 false 61 ); 62 istream.close(); 63 } 64 }); 65 66 zipWriter.close(); 67 } 68 69 function exactGC() { 70 return new Promise(function (resolve) { 71 var count = 0; 72 function doPreciseGCandCC() { 73 function scheduleGCCallback() { 74 Cu.forceCC(); 75 76 if (++count < 2) { 77 doPreciseGCandCC(); 78 } else { 79 resolve(); 80 } 81 } 82 Cu.schedulePreciseGC(scheduleGCCallback); 83 } 84 doPreciseGCandCC(); 85 }); 86 } 87 88 function resetQuotaManager() { 89 return new Promise(function (resolve) { 90 var prefService = Services.prefs; 91 92 // enable quota manager testing mode 93 var pref = "dom.quotaManager.testing"; 94 prefService.getBranch(null).setBoolPref(pref, true); 95 96 var request = Services.qms.reset(); 97 request.callback = resolve; 98 99 // disable quota manager testing mode 100 // prefService.getBranch(null).setBoolPref(pref, false); 101 }); 102 } 103 104 function run_test() { 105 do_test_pending(); 106 do_get_profile(); 107 108 var directoryService = Services.dirsvc; 109 var profileDir = directoryService.get("ProfD", Ci.nsIFile); 110 var currentDir = directoryService.get("CurWorkD", Ci.nsIFile); 111 112 var zipFile = currentDir.clone(); 113 zipFile.append("new_profile.zip"); 114 if (zipFile.exists()) { 115 zipFile.remove(false); 116 } 117 ok(!zipFile.exists()); 118 119 caches 120 .open("xpcshell-test") 121 .then(function (c) { 122 var request = new Request("http://example.com/index.html"); 123 var response = new Response("hello world"); 124 return c.put(request, response); 125 }) 126 .then(exactGC) 127 .then(resetQuotaManager) 128 .then(function () { 129 zip_profile(zipFile, profileDir); 130 dump("### ### created zip at: " + zipFile.path + "\n"); 131 do_test_finished(); 132 }) 133 .catch(function (e) { 134 do_test_finished(); 135 ok(false, e); 136 }); 137 }