Bug 1260076 - [Mochitest] implement SpecialPowers.loadPrivilegedScript, r=bholley
authorJunior Hsu <juhsu@mozilla.com>
Wed, 20 Apr 2016 19:47:00 +0200
changeset 331960 dea40ab40c2ad83c6cc68a9f9dcf10e0f31dac74
parent 331959 849d0f0165a6b882894249ef4c465d989c002740
child 331961 85616706a29b8944f3a09f5cc32e9e219f6ceb40
push id6048
push userkmoir@mozilla.com
push dateMon, 06 Jun 2016 19:02:08 +0000
treeherdermozilla-beta@46d72a56c57d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbholley
bugs1260076
milestone48.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1260076 - [Mochitest] implement SpecialPowers.loadPrivilegedScript, r=bholley
testing/mochitest/tests/Harness_sanity/mochitest.ini
testing/mochitest/tests/Harness_sanity/test_SpecialPowersLoadPrivilegedScript.html
testing/specialpowers/content/specialpowersAPI.js
--- a/testing/mochitest/tests/Harness_sanity/mochitest.ini
+++ b/testing/mochitest/tests/Harness_sanity/mochitest.ini
@@ -27,16 +27,17 @@ support-files =
     file_app.sjs
     file_app.template.webapp
     app.html
 [test_SpecialPowersPushPrefEnv.html]
 [test_SimpletestGetTestFileURL.html]
 [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
 [test_sanity_cleanup.html]
 [test_sanity_cleanup2.html]
 [test_sanityEventUtils.html]
 skip-if = buildapp == 'mulet' || toolkit == 'android' #bug 688052
 [test_sanitySimpletest.html]
new file mode 100644
--- /dev/null
+++ b/testing/mochitest/tests/Harness_sanity/test_SpecialPowersLoadPrivilegedScript.html
@@ -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>
--- a/testing/specialpowers/content/specialpowersAPI.js
+++ b/testing/specialpowers/content/specialpowersAPI.js
@@ -451,16 +451,36 @@ SpecialPowersAPI.prototype = {
   get MockColorPicker() {
     return MockColorPicker;
   },
 
   get MockPermissionPrompt() {
     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"]
                           .getService(Ci.nsIUUIDGenerator);
     let id = uuidGenerator.generateUUID().toString();
 
     // Tells chrome code to evaluate this chrome script
     let scriptArgs = { id };