dom/apps/tests/asmjs/asmjs_app.sjs

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 var Cc = Components.classes;
michael@0 2 var Ci = Components.interfaces;
michael@0 3 var Cu = Components.utils;
michael@0 4
michael@0 5 Cu.import("resource://gre/modules/Services.jsm");
michael@0 6 Cu.import("resource://gre/modules/NetUtil.jsm");
michael@0 7
michael@0 8 function handleRequest(aRequest, aResponse) {
michael@0 9 aResponse.setHeader("Access-Control-Allow-Origin", "*", false);
michael@0 10
michael@0 11 var query = getQuery(aRequest);
michael@0 12
michael@0 13 if ("setVersion" in query) {
michael@0 14 setState("version", query.setVersion);
michael@0 15 aResponse.write("OK");
michael@0 16 return;
michael@0 17 }
michael@0 18 var version = Number(getState("version"));
michael@0 19
michael@0 20 // setPrecompile sets the "precompile" field in the app manifest
michael@0 21 if ("setPrecompile" in query) {
michael@0 22 setState("precompile", query.setPrecompile);
michael@0 23 aResponse.write("OK");
michael@0 24 return;
michael@0 25 }
michael@0 26 var precompile = getState("precompile");
michael@0 27
michael@0 28 if ("getPackage" in query) {
michael@0 29 aResponse.setHeader("Content-Type", "application/zip", false);
michael@0 30 aResponse.write(buildAppPackage(version, precompile));
michael@0 31 return;
michael@0 32 }
michael@0 33
michael@0 34 if ("getManifest" in query) {
michael@0 35 aResponse.setHeader("Content-Type", "application/x-web-app-manifest+json", false);
michael@0 36 aResponse.write(getManifest(version, precompile));
michael@0 37 return;
michael@0 38 }
michael@0 39 }
michael@0 40
michael@0 41 function getQuery(aRequest) {
michael@0 42 var query = {};
michael@0 43 aRequest.queryString.split('&').forEach(function(val) {
michael@0 44 var [name, value] = val.split('=');
michael@0 45 query[decodeURIComponent(name)] = decodeURIComponent(value);
michael@0 46 });
michael@0 47 return query;
michael@0 48 }
michael@0 49
michael@0 50 function getTestFile(aName) {
michael@0 51 var file = Services.dirsvc.get("CurWorkD", Ci.nsIFile);
michael@0 52
michael@0 53 var path = "chrome/dom/apps/tests/asmjs/" + aName;
michael@0 54
michael@0 55 path.split("/").forEach(function(component) {
michael@0 56 file.append(component);
michael@0 57 });
michael@0 58
michael@0 59 return file;
michael@0 60 }
michael@0 61
michael@0 62 function readFile(aFile) {
michael@0 63 var fstream = Cc["@mozilla.org/network/file-input-stream;1"].
michael@0 64 createInstance(Ci.nsIFileInputStream);
michael@0 65 fstream.init(aFile, -1, 0, 0);
michael@0 66 var data = NetUtil.readInputStreamToString(fstream, fstream.available());
michael@0 67 fstream.close();
michael@0 68 return data;
michael@0 69 }
michael@0 70
michael@0 71 function getManifest(aVersion, aPrecompile) {
michael@0 72 return readFile(getTestFile("manifest.webapp")).
michael@0 73 replace(/VERSIONTOKEN/g, aVersion).
michael@0 74 replace(/PRECOMPILETOKEN/g, aPrecompile);
michael@0 75 }
michael@0 76
michael@0 77 function buildAppPackage(aVersion, aPrecompile) {
michael@0 78 const PR_RDWR = 0x04;
michael@0 79 const PR_CREATE_FILE = 0x08;
michael@0 80 const PR_TRUNCATE = 0x20;
michael@0 81
michael@0 82 let zipFile = Services.dirsvc.get("TmpD", Ci.nsIFile);
michael@0 83 zipFile.append("application.zip");
michael@0 84
michael@0 85 let zipWriter = Cc["@mozilla.org/zipwriter;1"].createInstance(Ci.nsIZipWriter);
michael@0 86 zipWriter.open(zipFile, PR_RDWR | PR_CREATE_FILE | PR_TRUNCATE);
michael@0 87
michael@0 88 // Add index.html file to the zip file
michael@0 89 zipWriter.addEntryFile("index.html",
michael@0 90 Ci.nsIZipWriter.COMPRESSION_NONE,
michael@0 91 getTestFile("index.html"),
michael@0 92 false);
michael@0 93
michael@0 94 // Add manifest to the zip file
michael@0 95 var manStream = Cc["@mozilla.org/io/string-input-stream;1"].
michael@0 96 createInstance(Ci.nsIStringInputStream);
michael@0 97 var manifest = getManifest(aVersion, aPrecompile);
michael@0 98 manStream.setData(manifest, manifest.length);
michael@0 99 zipWriter.addEntryStream("manifest.webapp", Date.now(),
michael@0 100 Ci.nsIZipWriter.COMPRESSION_NONE,
michael@0 101 manStream, false);
michael@0 102
michael@0 103 // Add an asm.js module to the zip file
michael@0 104 // The module also contains some code to perform tests
michael@0 105
michael@0 106 // Creates a module big enough to be cached
michael@0 107 // The module is different according to the app version, this way
michael@0 108 // we can test that on update the new module is compiled
michael@0 109 var code = "function f() { 'use asm';\n";
michael@0 110 for (var i = 0; i < 5000; i++) {
michael@0 111 code += "function g" + i + "() { return " + i + "}\n";
michael@0 112 }
michael@0 113 code += "return g" + aVersion + " }\n\
michael@0 114 var jsFuns = SpecialPowers.Cu.getJSTestingFunctions();\n\
michael@0 115 ok(jsFuns.isAsmJSCompilationAvailable(), 'asm.js compilation is available');\n\
michael@0 116 ok(jsFuns.isAsmJSModule(f), 'f is an asm.js module');\n\
michael@0 117 var g" + aVersion + " = f();\n\
michael@0 118 ok(jsFuns.isAsmJSFunction(g" + aVersion + "), 'g" + aVersion + " is an asm.js function');\n\
michael@0 119 ok(g" + aVersion + "() === " + aVersion + ", 'g" + aVersion + " returns the correct result');";
michael@0 120
michael@0 121 // If the "precompile" field contains this module, we test that it is loaded
michael@0 122 // from the cache.
michael@0 123 // If the "precompile" field doesn't contain the module or is incorrect, we
michael@0 124 // test that the module is not loaded from the cache.
michael@0 125 if (aPrecompile == '["asmjsmodule.js"]') {
michael@0 126 code += "ok(jsFuns.isAsmJSModuleLoadedFromCache(f), 'module loaded from cache');\n";
michael@0 127 } else {
michael@0 128 code += "ok(!jsFuns.isAsmJSModuleLoadedFromCache(f), 'module not loaded from cache');\n";
michael@0 129 }
michael@0 130
michael@0 131 code += "alert('DONE');";
michael@0 132
michael@0 133 var jsStream = Cc["@mozilla.org/io/string-input-stream;1"].
michael@0 134 createInstance(Ci.nsIStringInputStream);
michael@0 135 jsStream.setData(code, code.length);
michael@0 136 zipWriter.addEntryStream("asmjsmodule.js", Date.now(),
michael@0 137 Ci.nsIZipWriter.COMPRESSION_NONE,
michael@0 138 jsStream, false);
michael@0 139
michael@0 140 zipWriter.close();
michael@0 141
michael@0 142 return readFile(zipFile);
michael@0 143 }

mercurial