Bug 1153128 - Add sendSyncMessage to SpecialPowers.loadChromeScript. r=jmaher

MozReview-Commit-ID: AsbBiOkhhLo
This commit is contained in:
Hiroyuki Ikezoe 2016-02-24 21:43:23 -08:00
parent edfa4cdbaa
commit fa0d17026f
4 changed files with 22 additions and 4 deletions

View File

@ -9,3 +9,7 @@ addMessageListener("valid-assert", function (message) {
assert.equal(1, 1, "another valid assertion");
sendAsyncMessage("valid-assert-done");
});
addMessageListener("sync-message", () => {
return "Received a synchronous message.";
});

View File

@ -33,6 +33,16 @@ function endOfTest() {
}
script.sendAsyncMessage("foo", MESSAGE);
/*
* [0][0] is because we're using one real message listener in SpecialPowersObserverAPI.js
* and dispatching that to multiple _chromeScriptListeners. The outer array comes
* from the message manager since there can be multiple real listeners. The inner
* array is for the return values of _chromeScriptListeners.
*/
is(script.sendSyncMessage("sync-message")[0][0], "Received a synchronous message.",
"Check sync return value");
</script>
</pre>
</body>

View File

@ -483,10 +483,9 @@ SpecialPowersObserverAPI.prototype = {
let id = aMessage.json.id;
let name = aMessage.json.name;
let message = aMessage.json.message;
this._chromeScriptListeners
.filter(o => (o.name == name && o.id == id))
.forEach(o => o.listener(message));
return undefined; // See comment at the beginning of this function.
return this._chromeScriptListeners
.filter(o => (o.name == name && o.id == id))
.map(o => o.listener(message));
}
case "SPImportInMainProcess": {

View File

@ -482,6 +482,11 @@ SpecialPowersAPI.prototype = {
{ id: id, name: name, message: message });
},
sendSyncMessage: (name, message) => {
return this._sendSyncMessage("SPChromeScriptMessage",
{ id, name, message });
},
destroy: () => {
listeners = [];
this._removeMessageListener("SPChromeScriptMessage", chromeScript);