Use postMessage to communicate from parent to child. Fixes #532

This commit is contained in:
Marco Castelluccio 2014-11-24 02:19:26 +01:00
Родитель 815ac7e0f7
Коммит a875b3ac0f
3 изменённых файлов: 25 добавлений и 65 удалений

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

@ -36,10 +36,6 @@ var DumbPipe = {
// Functions that receive messages from the other side for active pipes. // Functions that receive messages from the other side for active pipes.
recipients: {}, recipients: {},
// Queue of messages to send to the other side. Retrieved by the other side
// via a "get" message.
outgoingMessages: [],
// Every time we want to make the other side retrieve messages, the hash // Every time we want to make the other side retrieve messages, the hash
// of the other side's web page has to change, so we increment it. // of the other side's web page has to change, so we increment it.
nextHashID: 0, nextHashID: 0,
@ -76,9 +72,6 @@ var DumbPipe = {
//console.log("outer recv: " + JSON.stringify(envelope)); //console.log("outer recv: " + JSON.stringify(envelope));
this.receiveMessage(envelope.pipeID, envelope.message); this.receiveMessage(envelope.pipeID, envelope.message);
break; break;
case "get":
this.getMessages(event);
break;
case "close": case "close":
//console.log("outer recv: " + JSON.stringify(envelope)); //console.log("outer recv: " + JSON.stringify(envelope));
this.closePipe(envelope.pipeID); this.closePipe(envelope.pipeID);
@ -109,12 +102,12 @@ var DumbPipe = {
// Oh my shod, that's some funky git! // Oh my shod, that's some funky git!
var envelope = { pipeID: pipeID, message: message }; var envelope = { pipeID: pipeID, message: message };
//console.log("outer send: " + JSON.stringify(envelope)); //console.log("outer send: " + JSON.stringify(envelope));
this.outgoingMessages.push(envelope);
var mozbrowser = document.getElementById("mozbrowser"); try {
window.setZeroTimeout(function() { document.getElementById("mozbrowser").contentWindow.postMessage(envelope, "*");
mozbrowser.src = mozbrowser.src.split("#")[0] + "#" + this.nextHashID++; } catch (e) {
}.bind(this)); console.log("Error " + e + " while sending message: " + JSON.stringify(message));
}
}, },
receiveMessage: function(pipeID, message, detail) { receiveMessage: function(pipeID, message, detail) {
@ -132,17 +125,6 @@ var DumbPipe = {
}.bind(this)); }.bind(this));
}, },
getMessages: function(event) {
try {
event.detail.returnValue = JSON.stringify(this.outgoingMessages);
} catch(ex) {
console.error("failed to stringify outgoing messages: " + ex);
} finally {
this.outgoingMessages = [];
event.detail.unblock();
}
},
closePipe: function(pipeID) { closePipe: function(pipeID) {
delete this.recipients[pipeID]; delete this.recipients[pipeID];
} }
@ -226,11 +208,7 @@ DumbPipe.registerOpener("socket", function(message, sender) {
} }
socket.ondata = function(event) { socket.ondata = function(event) {
// Turn the buffer into a regular Array to traverse the mozbrowser boundary. sender({ type: "data", data: event.data });
var array = Array.prototype.slice.call(new Uint8Array(event.data));
array.constructor = Array;
sender({ type: "data", data: array });
} }
socket.ondrain = function(event) { socket.ondrain = function(event) {

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

@ -78,46 +78,27 @@ var DumbPipe = {
} }
}, },
handleEvent: function(event) { receiveMessage: function(event) {
// To ensure we don't fill up the browser history over time, we navigate var envelope = event.data;
// "back" every time the other side navigates us "forward" by changing
// the hash. This will trigger a second hashchange event; to avoid getting if (envelope === "zero-timeout-message") {
// messages twice, we only get them for the second hashchange event, return;
// i.e. once we've returned to the hashless page, at which point a second
// call to window.history.back() will have had no effect.
//
// We only do this when we're in mozbrowser (i.e. window.parent === window),
// since window.history.back() affects the parent window otherwise.
//
if (window.parent === window) {
var hash = window.location.hash;
window.history.back();
if (window.location.hash != hash) {
return;
}
} }
this.send({ command: "get" }, function(envelopes) { if (this.recipients[envelope.pipeID]) {
envelopes.forEach((function(envelope) { try {
//console.log("inner recv: " + JSON.stringify(envelope)); this.recipients[envelope.pipeID](envelope.message);
window.setZeroTimeout(function() { } catch(ex) {
if (this.recipients[envelope.pipeID]) { console.error(ex + "\n" + ex.stack);
try { }
this.recipients[envelope.pipeID](envelope.message); } else {
} catch(ex) { console.warn("nonexistent pipe " + envelope.pipeID + " received message " +
console.error(ex + "\n" + ex.stack); JSON.stringify(envelope.message));
} }
} else {
console.warn("nonexistent pipe " + envelope.pipeID + " received message " +
JSON.stringify(envelope.message));
}
}.bind(this));
}).bind(this));
}.bind(this));
}, },
}; };
window.addEventListener("hashchange", DumbPipe.handleEvent.bind(DumbPipe), false); window.addEventListener("message", DumbPipe.receiveMessage.bind(DumbPipe), false);
// If "mozbrowser" isn't enabled on the frame we're loaded in, then override // If "mozbrowser" isn't enabled on the frame we're loaded in, then override
// the alert/prompt functions to funnel messages to the endpoint in the parent. // the alert/prompt functions to funnel messages to the endpoint in the parent.

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

@ -80,9 +80,10 @@ Native.create("com/sun/midp/io/j2me/socket/Protocol.open0.([BI)V", function(ipBy
this.socket.ondata = (function(message) { this.socket.ondata = (function(message) {
// console.log("this.socket.ondata: " + JSON.stringify(message)); // console.log("this.socket.ondata: " + JSON.stringify(message));
var newArray = new Uint8Array(this.data.byteLength + message.data.length); var newData = new Uint8Array(message.data);
var newArray = new Uint8Array(this.data.byteLength + newData.byteLength);
newArray.set(this.data); newArray.set(this.data);
newArray.set(message.data, this.data.byteLength); newArray.set(newData, this.data.byteLength);
this.data = newArray; this.data = newArray;
if (this.waitingData) { if (this.waitingData) {