dom/tests/browser/browser_xhr_sandbox.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 // This code is evaluated in a sandbox courtesy of toSource();
michael@0 2 let sandboxCode = (function() {
michael@0 3 let req = new XMLHttpRequest();
michael@0 4 req.open("GET", "http://mochi.test:8888/browser/dom/tests/browser/", true);
michael@0 5 req.onreadystatechange = function() {
michael@0 6 if (req.readyState === 4) {
michael@0 7 // If we get past the problem above, we end up with a req.status of zero
michael@0 8 // (ie, blocked due to CORS) even though we are fetching from the same
michael@0 9 // origin as the window itself.
michael@0 10 let result;
michael@0 11 if (req.status != 200) {
michael@0 12 result = "ERROR: got request status of " + req.status;
michael@0 13 } else if (req.responseText.length == 0) {
michael@0 14 result = "ERROR: got zero byte response text";
michael@0 15 } else {
michael@0 16 result = "ok";
michael@0 17 }
michael@0 18 postMessage({result: result}, "*");
michael@0 19 }
michael@0 20 };
michael@0 21 req.send(null);
michael@0 22 }).toSource() + "();";
michael@0 23
michael@0 24 function test() {
michael@0 25 waitForExplicitFinish();
michael@0 26 let appShell = Cc["@mozilla.org/appshell/appShellService;1"]
michael@0 27 .getService(Ci.nsIAppShellService);
michael@0 28 let doc = appShell.hiddenDOMWindow.document;
michael@0 29 let frame = doc.createElement("iframe");
michael@0 30 frame.setAttribute("type", "content");
michael@0 31 frame.setAttribute("src", "http://mochi.test:8888/browser/dom/tests/browser/browser_xhr_sandbox.js");
michael@0 32
michael@0 33 frame.addEventListener("load", function () {
michael@0 34 let workerWindow = frame.contentWindow;
michael@0 35 workerWindow.addEventListener("message", function(evt) {
michael@0 36 is(evt.data.result, "ok", "check the sandbox code was happy");
michael@0 37 frame.remove();
michael@0 38 finish();
michael@0 39 }, true);
michael@0 40 let sandbox = new Cu.Sandbox(workerWindow);
michael@0 41 // inject some functions from the window into the sandbox.
michael@0 42 // postMessage so the async code in the sandbox can report a result.
michael@0 43 sandbox.importFunction(workerWindow.postMessage.bind(workerWindow), "postMessage");
michael@0 44 sandbox.importFunction(workerWindow.XMLHttpRequest, "XMLHttpRequest");
michael@0 45 Cu.evalInSandbox(sandboxCode, sandbox, "1.8");
michael@0 46 }, true);
michael@0 47
michael@0 48 let container = doc.body ? doc.body : doc.documentElement;
michael@0 49 container.appendChild(frame);
michael@0 50 }

mercurial