Bug 1260076 - [Mochitest] implement SpecialPowers.loadPrivilegedScript, r=bholley

This commit is contained in:
Junior Hsu 2016-04-20 19:47:00 +02:00
parent 739e3445b6
commit 6d15e2b50b
3 changed files with 58 additions and 0 deletions

View File

@ -32,6 +32,7 @@ support-files =
[test_SpecialPowersLoadChromeScript.html]
support-files = SpecialPowersLoadChromeScript.js
[test_SpecialPowersLoadChromeScript_function.html]
[test_SpecialPowersLoadPrivilegedScript.html]
[test_bug649012.html]
[test_bug816847.html]
skip-if = toolkit == 'android' || e10s #No test app installed

View File

@ -0,0 +1,37 @@
<!DOCTYPE HTML>
<html>
<head>
<title>Test for SpecialPowers.loadChromeScript</title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<pre id="test">
<script class="testbody" type="text/javascript">
SimpleTest.waitForExplicitFinish();
function loadPrivilegedScriptTest() {
var Cc = Components.classes;
var Ci = Components.interfaces;
function isMainProcess() {
return Cc["@mozilla.org/xre/app-info;1"].
getService(Ci.nsIXULRuntime).
processType == Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT;
}
port.postMessage({'isMainProcess': isMainProcess()});
}
var contentProcessType = SpecialPowers.isMainProcess();
var port;
try {
port = SpecialPowers.loadPrivilegedScript(loadPrivilegedScriptTest.toSource());
} catch (e) {
ok(false, "loadPrivilegedScript shoulde not throw");
}
port.onmessage = (e) => {
is(contentProcessType, e.data['isMainProcess'], "content and the script should be in the same process");
SimpleTest.finish();
};
</script>

View File

@ -456,6 +456,26 @@ SpecialPowersAPI.prototype = {
return MockPermissionPrompt;
},
/*
* Load a privileged script that runs same-process. This is different from
* |loadChromeScript|, which will run in the parent process in e10s mode.
*/
loadPrivilegedScript: function (aFunction) {
var str = "(" + aFunction.toString() + ")();";
var systemPrincipal = Services.scriptSecurityManager.getSystemPrincipal();
var sb = Cu.Sandbox(systemPrincipal);
var window = this.window.get();
var mc = new window.MessageChannel();
sb.port = mc.port1;
try {
sb.eval(str);
} catch (e) {
throw wrapIfUnwrapped(e);
}
return mc.port2;
},
loadChromeScript: function (urlOrFunction) {
// Create a unique id for this chrome script
let uuidGenerator = Cc["@mozilla.org/uuid-generator;1"]