Bug 1655442 - Make usage of nsIWorkerDebugger.addListener more explicit. r=loganfsmyth

Declare separate listener objects instead of relying on the presence of the
listener method in a broader class.

Differential Revision: https://phabricator.services.mozilla.com/D84964
This commit is contained in:
Nicolas Chevobbe 2020-07-29 07:30:56 +00:00
Родитель 6d68ed9957
Коммит be3c8ebd18
2 изменённых файлов: 32 добавлений и 28 удалений

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

@ -40,6 +40,11 @@ const WorkerTargetActor = protocol.ActorClassWithSpec(workerTargetSpec, {
this._attached = false; this._attached = false;
this._threadActor = null; this._threadActor = null;
this._transport = null; this._transport = null;
this._dbgListener = {
onClose: this._onWorkerClose.bind(this),
onError: this._onWorkerError.bind(this),
};
}, },
form() { form() {
@ -83,7 +88,7 @@ const WorkerTargetActor = protocol.ActorClassWithSpec(workerTargetSpec, {
if (isServiceWorker) { if (isServiceWorker) {
this._preventServiceWorkerShutdown(); this._preventServiceWorkerShutdown();
} }
this._dbg.addListener(this); this._dbg.addListener(this._dbgListener);
this._attached = true; this._attached = true;
} }
@ -152,7 +157,7 @@ const WorkerTargetActor = protocol.ActorClassWithSpec(workerTargetSpec, {
return { type: "pushed" }; return { type: "pushed" };
}, },
onClose() { _onWorkerClose() {
if (this._attached) { if (this._attached) {
this._detach(); this._detach();
} }
@ -160,7 +165,7 @@ const WorkerTargetActor = protocol.ActorClassWithSpec(workerTargetSpec, {
this.conn.sendActorEvent(this.actorID, "close"); this.conn.sendActorEvent(this.actorID, "close");
}, },
onError(filename, lineno, message) { _onWorkerError(filename, lineno, message) {
reportError("ERROR:" + filename + ":" + lineno + ":" + message + "\n"); reportError("ERROR:" + filename + ":" + lineno + ":" + message + "\n");
}, },
@ -194,7 +199,7 @@ const WorkerTargetActor = protocol.ActorClassWithSpec(workerTargetSpec, {
this._allowServiceWorkerShutdown(); this._allowServiceWorkerShutdown();
} }
this._dbg.removeListener(this); this._dbg.removeListener(this._dbgListener);
this._attached = false; this._attached = false;
}, },

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

@ -13,27 +13,28 @@
* A transport that uses a WorkerDebugger to send packets from the main * A transport that uses a WorkerDebugger to send packets from the main
* thread to a worker thread. * thread to a worker thread.
*/ */
function MainThreadWorkerDebuggerTransport(dbg, id) { class MainThreadWorkerDebuggerTransport {
this._dbg = dbg; constructor(dbg, id) {
this._id = id; this._dbg = dbg;
this.onMessage = this._onMessage.bind(this); this._id = id;
}
MainThreadWorkerDebuggerTransport.prototype = { this._dbgListener = {
constructor: MainThreadWorkerDebuggerTransport, onMessage: this._onMessage.bind(this),
};
}
ready: function() { ready() {
this._dbg.addListener(this); this._dbg.addListener(this._dbgListener);
}, }
close: function() { close() {
this._dbg.removeListener(this); this._dbg.removeListener(this._dbgListener);
if (this.hooks) { if (this.hooks) {
this.hooks.onClosed(); this.hooks.onClosed();
} }
}, }
send: function(packet) { send(packet) {
this._dbg.postMessage( this._dbg.postMessage(
JSON.stringify({ JSON.stringify({
type: "message", type: "message",
@ -41,23 +42,21 @@ MainThreadWorkerDebuggerTransport.prototype = {
message: packet, message: packet,
}) })
); );
}, }
startBulkSend: function() { startBulkSend() {
throw new Error("Can't send bulk data from worker threads!"); throw new Error("Can't send bulk data from worker threads!");
}, }
_onMessage: function(message) { _onMessage(message) {
const packet = JSON.parse(message); const packet = JSON.parse(message);
if (packet.type !== "message" || packet.id !== this._id) { if (packet.type !== "message" || packet.id !== this._id || !this.hooks) {
return; return;
} }
if (this.hooks) { this.hooks.onPacket(packet.message);
this.hooks.onPacket(packet.message); }
} }
},
};
exports.MainThreadWorkerDebuggerTransport = MainThreadWorkerDebuggerTransport; exports.MainThreadWorkerDebuggerTransport = MainThreadWorkerDebuggerTransport;