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.
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
// of the other side's web page has to change, so we increment it.
nextHashID: 0,
@ -76,9 +72,6 @@ var DumbPipe = {
//console.log("outer recv: " + JSON.stringify(envelope));
this.receiveMessage(envelope.pipeID, envelope.message);
break;
case "get":
this.getMessages(event);
break;
case "close":
//console.log("outer recv: " + JSON.stringify(envelope));
this.closePipe(envelope.pipeID);
@ -109,12 +102,12 @@ var DumbPipe = {
// Oh my shod, that's some funky git!
var envelope = { pipeID: pipeID, message: message };
//console.log("outer send: " + JSON.stringify(envelope));
this.outgoingMessages.push(envelope);
var mozbrowser = document.getElementById("mozbrowser");
window.setZeroTimeout(function() {
mozbrowser.src = mozbrowser.src.split("#")[0] + "#" + this.nextHashID++;
}.bind(this));
try {
document.getElementById("mozbrowser").contentWindow.postMessage(envelope, "*");
} catch (e) {
console.log("Error " + e + " while sending message: " + JSON.stringify(message));
}
},
receiveMessage: function(pipeID, message, detail) {
@ -132,17 +125,6 @@ var DumbPipe = {
}.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) {
delete this.recipients[pipeID];
}
@ -226,11 +208,7 @@ DumbPipe.registerOpener("socket", function(message, sender) {
}
socket.ondata = function(event) {
// Turn the buffer into a regular Array to traverse the mozbrowser boundary.
var array = Array.prototype.slice.call(new Uint8Array(event.data));
array.constructor = Array;
sender({ type: "data", data: array });
sender({ type: "data", data: event.data });
}
socket.ondrain = function(event) {

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

@ -78,46 +78,27 @@ var DumbPipe = {
}
},
handleEvent: function(event) {
// To ensure we don't fill up the browser history over time, we navigate
// "back" every time the other side navigates us "forward" by changing
// the hash. This will trigger a second hashchange event; to avoid getting
// messages twice, we only get them for the second hashchange event,
// 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;
}
receiveMessage: function(event) {
var envelope = event.data;
if (envelope === "zero-timeout-message") {
return;
}
this.send({ command: "get" }, function(envelopes) {
envelopes.forEach((function(envelope) {
//console.log("inner recv: " + JSON.stringify(envelope));
window.setZeroTimeout(function() {
if (this.recipients[envelope.pipeID]) {
try {
this.recipients[envelope.pipeID](envelope.message);
} catch(ex) {
console.error(ex + "\n" + ex.stack);
}
} else {
console.warn("nonexistent pipe " + envelope.pipeID + " received message " +
JSON.stringify(envelope.message));
}
}.bind(this));
}).bind(this));
}.bind(this));
if (this.recipients[envelope.pipeID]) {
try {
this.recipients[envelope.pipeID](envelope.message);
} catch(ex) {
console.error(ex + "\n" + ex.stack);
}
} else {
console.warn("nonexistent pipe " + envelope.pipeID + " received message " +
JSON.stringify(envelope.message));
}
},
};
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
// 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) {
// 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(message.data, this.data.byteLength);
newArray.set(newData, this.data.byteLength);
this.data = newArray;
if (this.waitingData) {