From 36d22d82aa202bb199967e9512281e9a53db42c9 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 21:33:14 +0200 Subject: Adding upstream version 115.7.0esr. Signed-off-by: Daniel Baumann --- devtools/shared/transport/worker-transport.js | 113 ++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 devtools/shared/transport/worker-transport.js (limited to 'devtools/shared/transport/worker-transport.js') diff --git a/devtools/shared/transport/worker-transport.js b/devtools/shared/transport/worker-transport.js new file mode 100644 index 0000000000..903fd69cf4 --- /dev/null +++ b/devtools/shared/transport/worker-transport.js @@ -0,0 +1,113 @@ +/* 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/. */ + +"use strict"; + +// Each worker debugger supports only a single connection to the main thread. +// However, its theoretically possible for multiple servers to connect to the +// same worker. Consequently, each transport has a connection id, to allow +// messages from multiple connections to be multiplexed on a single channel. + +/** + * A transport that uses a WorkerDebugger to send packets from the main + * thread to a worker thread. + */ +class MainThreadWorkerDebuggerTransport { + constructor(dbg, id) { + this._dbg = dbg; + this._id = id; + + this._dbgListener = { + onMessage: this._onMessage.bind(this), + }; + } + + ready() { + this._dbg.addListener(this._dbgListener); + } + + close() { + if (this._dbgListener) { + this._dbg.removeListener(this._dbgListener); + } + this._dbgListener = null; + this.hooks?.onTransportClosed(); + } + + send(packet) { + this._dbg.postMessage( + JSON.stringify({ + type: "message", + id: this._id, + message: packet, + }) + ); + } + + startBulkSend() { + throw new Error("Can't send bulk data from worker threads!"); + } + + _onMessage(message) { + const packet = JSON.parse(message); + if (packet.type !== "message" || packet.id !== this._id || !this.hooks) { + return; + } + + this.hooks.onPacket(packet.message); + } +} + +exports.MainThreadWorkerDebuggerTransport = MainThreadWorkerDebuggerTransport; + +/** + * A transport that uses a WorkerDebuggerGlobalScope to send packets from a + * worker thread to the main thread. + */ +function WorkerThreadWorkerDebuggerTransport(scope, id) { + this._scope = scope; + this._id = id; + this._onMessage = this._onMessage.bind(this); +} + +WorkerThreadWorkerDebuggerTransport.prototype = { + constructor: WorkerThreadWorkerDebuggerTransport, + + ready() { + this._scope.addEventListener("message", this._onMessage); + }, + + close() { + this._scope.removeEventListener("message", this._onMessage); + this.hooks?.onTransportClosed(); + }, + + send(packet) { + this._scope.postMessage( + JSON.stringify({ + type: "message", + id: this._id, + message: packet, + }) + ); + }, + + startBulkSend() { + throw new Error("Can't send bulk data from worker threads!"); + }, + + _onMessage(event) { + const packet = JSON.parse(event.data); + if (packet.type !== "message" || packet.id !== this._id) { + return; + } + + if (this.hooks) { + this.hooks.onPacket(packet.message); + } + }, +}; + +exports.WorkerThreadWorkerDebuggerTransport = + WorkerThreadWorkerDebuggerTransport; -- cgit v1.2.3