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