Bug 1226291 - Add SpecialPowers API for importing a jsm into the main process. r=jmaher

This commit is contained in:
Andrew McCreight 2015-11-25 12:34:08 -08:00
Родитель 9be17fad9a
Коммит 4898740306
12 изменённых файлов: 93 добавлений и 8 удалений

Просмотреть файл

@ -1,2 +0,0 @@
// Ensure the contacts service is running in the parent.
Components.utils.import("resource://gre/modules/ContactService.jsm");

Просмотреть файл

@ -1,5 +1,5 @@
[DEFAULT]
support-files = shared.js contacts_chromescript.js
support-files = shared.js
[test_contacts_basics.html]
skip-if = (toolkit == 'gonk' && debug) #debug-only failure

Просмотреть файл

@ -1,11 +1,7 @@
"use strict";
// Fix the environment to run Contacts tests
if (SpecialPowers.isMainProcess()) {
SpecialPowers.Cu.import("resource://gre/modules/ContactService.jsm");
} else {
SpecialPowers.loadChromeScript(SimpleTest.getTestFileURL('contacts_chromescript.js'));
}
SpecialPowers.importInMainProcess("resource://gre/modules/ContactService.jsm");
// Some helpful global vars
var isAndroid = (navigator.userAgent.indexOf("Android") !== -1);

Просмотреть файл

@ -0,0 +1,5 @@
this.EXPORTED_SYMBOLS = ["ImportTesting"];
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
// Empty module for testing via SpecialPowers.importInMainProcess.

Просмотреть файл

@ -0,0 +1,3 @@
addMessageListener("ImportTesting:IsModuleLoaded", function (msg) {
sendAsyncMessage("ImportTesting:IsModuleLoadedReply", Components.utils.isModuleLoaded(msg));
});

Просмотреть файл

@ -4,6 +4,9 @@ skip-if = buildapp == 'mulet' || buildapp == 'b2g'
skip-if = true #depends on fix for bug 1048446
[test_add_task.html]
[test_createFiles.html]
[test_importInMainProcess.html]
skip-if = buildapp == 'android' #bug 1228060
support-files = importtesting_chromescript.js
[test_sanity.html]
[test_sanityException.html]
[test_sanityException2.html]

Просмотреть файл

@ -0,0 +1,54 @@
<!DOCTYPE HTML>
<html>
<head>
<title>Test for SpecialPowers.importInMainProcess</title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<div id="content" class="testbody">
<script type="text/javascript">
SimpleTest.waitForExplicitFinish();
var failed = false;
try {
SpecialPowers.importInMainProcess("invalid file for import");
} catch (e) {
ok(e.toString().indexOf("NS_ERROR_MALFORMED_URI") > -1, "Exception should be for a malformed URI");
failed = true;
}
ok(failed, "An invalid import should throw");
const testingResource = "resource://testing-common/ImportTesting.jsm";
var script = SpecialPowers.loadChromeScript(SimpleTest.getTestFileURL('importtesting_chromescript.js'));
script.addMessageListener("ImportTesting:IsModuleLoadedReply", handleFirstReply);
script.sendAsyncMessage("ImportTesting:IsModuleLoaded", testingResource);
function handleFirstReply(aMsg) {
ok(!aMsg, "ImportTesting.jsm shouldn't be loaded before we import it");
try {
SpecialPowers.importInMainProcess(testingResource);
} catch (e) {
ok(false, "Unexpected exception when importing a valid resource: " + e.toString());
}
script.removeMessageListener("ImportTesting:IsModuleLoadedReply", handleFirstReply);
script.addMessageListener("ImportTesting:IsModuleLoadedReply", handleSecondReply);
script.sendAsyncMessage("ImportTesting:IsModuleLoaded", testingResource);
}
function handleSecondReply(aMsg) {
script.removeMessageListener("ImportTesting:IsModuleLoadedReply", handleSecondReply);
ok(aMsg, "ImportTesting.jsm should be loaded after we import it");
SimpleTest.finish();
}
</script>
</div>
</body>
</html>

Просмотреть файл

@ -8,5 +8,9 @@ DIRS += [
'SimpleTest'
]
TESTING_JS_MODULES += [
'Harness_sanity/ImportTesting.jsm',
]
MOCHITEST_MANIFESTS += ['Harness_sanity/mochitest.ini']
BROWSER_CHROME_MANIFESTS += ['browser/browser.ini']

Просмотреть файл

@ -81,6 +81,7 @@ SpecialPowersObserver.prototype._loadFrameScript = function()
this._messageManager.addMessageListener("SPWebAppService", this);
this._messageManager.addMessageListener("SPObserverService", this);
this._messageManager.addMessageListener("SPLoadChromeScript", this);
this._messageManager.addMessageListener("SPImportInMainProcess", this);
this._messageManager.addMessageListener("SPChromeScriptMessage", this);
this._messageManager.addMessageListener("SPQuotaManager", this);
this._messageManager.addMessageListener("SPSetTestPluginEnabledState", this);
@ -162,6 +163,7 @@ SpecialPowersObserver.prototype.uninit = function()
this._messageManager.removeMessageListener("SPWebAppService", this);
this._messageManager.removeMessageListener("SPObserverService", this);
this._messageManager.removeMessageListener("SPLoadChromeScript", this);
this._messageManager.removeMessageListener("SPImportInMainProcess", this);
this._messageManager.removeMessageListener("SPChromeScriptMessage", this);
this._messageManager.removeMessageListener("SPQuotaManager", this);
this._messageManager.removeMessageListener("SPSetTestPluginEnabledState", this);

Просмотреть файл

@ -491,6 +491,17 @@ SpecialPowersObserverAPI.prototype = {
return undefined; // See comment at the beginning of this function.
}
case "SPImportInMainProcess": {
var message = { hadError: false, errorMessage: null };
try {
Components.utils.import(aMessage.data);
} catch (e) {
message.hadError = true;
message.errorMessage = e.toString();
}
return message;
}
case "SPCleanUpSTSData": {
let origin = aMessage.data.origin;
let flags = aMessage.data.flags;

Просмотреть файл

@ -28,6 +28,7 @@ function SpecialPowers(window) {
this._createFilesOnSuccess = null;
this.SP_SYNC_MESSAGES = ["SPChromeScriptMessage",
"SPLoadChromeScript",
"SPImportInMainProcess",
"SPObserverService",
"SPPermissionManager",
"SPPrefService",

Просмотреть файл

@ -643,6 +643,14 @@ SpecialPowersAPI.prototype = {
return this.wrap(chromeScript);
},
importInMainProcess: function (importString) {
var message = this._sendSyncMessage("SPImportInMainProcess", importString)[0];
if (message.hadError) {
throw "SpecialPowers.importInMainProcess failed with error " + message.errorMessage;
}
return;
},
get Services() {
return wrapPrivileged(Services);
},