зеркало из https://github.com/mozilla/pluotsorbet.git
Use postMessage to communicate from parent to child. Fixes #532
This commit is contained in:
Родитель
815ac7e0f7
Коммит
a875b3ac0f
34
index.js
34
index.js
|
@ -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) {
|
||||||
|
|
29
libs/pipe.js
29
libs/pipe.js
|
@ -78,29 +78,13 @@ 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,
|
|
||||||
// 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;
|
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]) {
|
if (this.recipients[envelope.pipeID]) {
|
||||||
try {
|
try {
|
||||||
this.recipients[envelope.pipeID](envelope.message);
|
this.recipients[envelope.pipeID](envelope.message);
|
||||||
|
@ -111,13 +95,10 @@ var DumbPipe = {
|
||||||
console.warn("nonexistent pipe " + envelope.pipeID + " received message " +
|
console.warn("nonexistent pipe " + envelope.pipeID + " received message " +
|
||||||
JSON.stringify(envelope.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) {
|
||||||
|
|
Загрузка…
Ссылка в новой задаче