2012-07-19 00:16:58 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2012-07-18 01:36:48 +00:00
|
|
|
// Code that is shared between clients and workers.
|
2012-10-31 16:13:28 +00:00
|
|
|
this.EXPORTED_SYMBOLS = ["AbstractPort"];
|
2012-07-18 01:36:48 +00:00
|
|
|
|
2012-10-31 16:13:28 +00:00
|
|
|
this.AbstractPort = function AbstractPort(portid) {
|
2012-07-18 01:36:48 +00:00
|
|
|
this._portid = portid;
|
|
|
|
this._handler = undefined;
|
2012-12-11 00:34:09 +00:00
|
|
|
this._closed = false;
|
2012-07-18 01:36:48 +00:00
|
|
|
// pending messages sent to this port before it has a message handler.
|
|
|
|
this._pendingMessagesIncoming = [];
|
2012-10-31 16:13:28 +00:00
|
|
|
};
|
2012-07-18 01:36:48 +00:00
|
|
|
|
|
|
|
AbstractPort.prototype = {
|
|
|
|
_portType: null, // set by a subclass.
|
|
|
|
// abstract methods to be overridden.
|
|
|
|
_dopost: function fw_AbstractPort_dopost(data) {
|
|
|
|
throw new Error("not implemented");
|
|
|
|
},
|
|
|
|
_onerror: function fw_AbstractPort_onerror(err) {
|
|
|
|
throw new Error("not implemented");
|
|
|
|
},
|
|
|
|
|
|
|
|
// and concrete methods shared by client and workers.
|
|
|
|
toString: function fw_AbstractPort_toString() {
|
2012-12-11 00:34:09 +00:00
|
|
|
return "MessagePort(portType='" + this._portType + "', portId="
|
|
|
|
+ this._portid + (this._closed ? ", closed=true" : "") + ")";
|
2012-07-18 01:36:48 +00:00
|
|
|
},
|
2015-09-24 11:32:23 +00:00
|
|
|
_JSONParse: function fw_AbstractPort_JSONParse(data) {
|
|
|
|
return JSON.parse(data);
|
|
|
|
},
|
2012-07-18 01:36:48 +00:00
|
|
|
|
|
|
|
_postControlMessage: function fw_AbstractPort_postControlMessage(topic, data) {
|
2012-07-13 20:51:35 +00:00
|
|
|
let postData = {
|
|
|
|
portTopic: topic,
|
|
|
|
portId: this._portid,
|
|
|
|
portFromType: this._portType,
|
|
|
|
data: data
|
|
|
|
};
|
2012-07-18 01:36:48 +00:00
|
|
|
this._dopost(postData);
|
|
|
|
},
|
|
|
|
|
|
|
|
_onmessage: function fw_AbstractPort_onmessage(data) {
|
|
|
|
// See comments in postMessage below - we work around a cloning
|
|
|
|
// issue by using JSON for these messages.
|
|
|
|
// Further, we allow the workers to override exactly how the JSON parsing
|
|
|
|
// is done - we try and do such parsing in the client window so things
|
|
|
|
// like prototype overrides on Array work as expected.
|
|
|
|
if (!this._handler) {
|
|
|
|
this._pendingMessagesIncoming.push(data);
|
2012-07-13 20:51:35 +00:00
|
|
|
} else {
|
2012-10-16 23:47:42 +00:00
|
|
|
data = this._JSONParse(data);
|
2012-07-18 01:36:48 +00:00
|
|
|
try {
|
2012-07-13 20:51:35 +00:00
|
|
|
this._handler({
|
|
|
|
data: data,
|
|
|
|
__exposedProps__: {
|
|
|
|
data: 'r'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} catch (ex) {
|
2012-07-18 01:36:48 +00:00
|
|
|
this._onerror(ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
set onmessage(handler) { // property setter for onmessage
|
|
|
|
this._handler = handler;
|
|
|
|
while (this._pendingMessagesIncoming.length) {
|
|
|
|
this._onmessage(this._pendingMessagesIncoming.shift());
|
|
|
|
}
|
|
|
|
},
|
2012-07-13 20:51:35 +00:00
|
|
|
get onmessage() {
|
|
|
|
return this._handler;
|
|
|
|
},
|
2012-07-18 01:36:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* postMessage
|
|
|
|
*
|
|
|
|
* Send data to the onmessage handler on the other end of the port. The
|
|
|
|
* data object should have a topic property.
|
|
|
|
*
|
|
|
|
* @param {jsobj} data
|
|
|
|
*/
|
|
|
|
postMessage: function fw_AbstractPort_postMessage(data) {
|
2012-12-11 00:34:09 +00:00
|
|
|
if (this._closed) {
|
2012-07-18 01:36:48 +00:00
|
|
|
throw new Error("port is closed");
|
|
|
|
}
|
|
|
|
// There seems to be an issue with passing objects directly and letting
|
|
|
|
// the structured clone thing work - we sometimes get:
|
|
|
|
// [Exception... "The object could not be cloned." code: "25" nsresult: "0x80530019 (DataCloneError)"]
|
|
|
|
// The best guess is that this happens when funky things have been added to the prototypes.
|
|
|
|
// It doesn't happen for our "control" messages, only in messages from
|
|
|
|
// content - so we explicitly use JSON on these messages as that avoids
|
|
|
|
// the problem.
|
|
|
|
this._postControlMessage("port-message", JSON.stringify(data));
|
|
|
|
},
|
|
|
|
|
|
|
|
close: function fw_AbstractPort_close() {
|
2012-12-11 00:34:09 +00:00
|
|
|
if (this._closed) {
|
2012-07-18 01:36:48 +00:00
|
|
|
return; // already closed.
|
|
|
|
}
|
|
|
|
this._postControlMessage("port-close");
|
|
|
|
// and clean myself up.
|
|
|
|
this._handler = null;
|
|
|
|
this._pendingMessagesIncoming = [];
|
2012-12-11 00:34:09 +00:00
|
|
|
this._closed = true;
|
2012-07-18 01:36:48 +00:00
|
|
|
}
|
2012-10-31 16:13:28 +00:00
|
|
|
};
|