tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

commit 8210d1cbf50057a0c008ebf418a35ac584cbcba8
parent c7b4d83f88ffd2a3d4831ca49005663a101cdb8f
Author: Yunju Lee <yunjulee@google.com>
Date:   Wed, 17 Dec 2025 06:28:40 +0000

Bug 2004209 - Turn devtools/client/devtools-client.js into an ES class. r=devtools-reviewers,nchevobbe

Differential Revision: https://phabricator.services.mozilla.com/D276308

Diffstat:
Mdevtools/client/devtools-client.js | 155++++++++++++++++++++++++++++++++++++++++---------------------------------------
1 file changed, 78 insertions(+), 77 deletions(-)

diff --git a/devtools/client/devtools-client.js b/devtools/client/devtools-client.js @@ -53,64 +53,68 @@ loader.lazyRequireGetter( * provides the means to communicate with the server and exchange the messages * required by the protocol in a traditional JavaScript API. */ -function DevToolsClient(transport) { - this._transport = transport; - this._transport.hooks = this; +class DevToolsClient extends EventEmitter { + constructor(transport) { + super(); - this._pendingRequests = new Map(); - this._activeRequests = new Map(); - this._eventsEnabled = true; + this._transport = transport; + this._transport.hooks = this; - this.traits = {}; + this._pendingRequests = new Map(); + this._activeRequests = new Map(); + this._eventsEnabled = true; - this.request = this.request.bind(this); + this.traits = {}; - /* - * As the first thing on the connection, expect a greeting packet from - * the connection's root actor. - */ - this.mainRoot = null; - this.expectReply("root", async packet => { - if (packet.error) { - console.error("Error when waiting for root actor", packet); - return; - } + this.request = this.request.bind(this); - this.mainRoot = createRootFront(this, packet); + /* + * As the first thing on the connection, expect a greeting packet from + * the connection's root actor. + */ + this.mainRoot = null; + this.expectReply("root", async packet => { + if (packet.error) { + console.error("Error when waiting for root actor", packet); + return; + } - // Once the root actor has been communicated by the server, - // emit a request to it to also push informations down to the server. - // - // This request has been added in Firefox 133. - try { - await this.mainRoot.connect({ - frontendVersion: AppConstants.MOZ_APP_VERSION, - }); - } catch (e) { - // Ignore errors of unsupported packet as the server may not yet support this request. - // The request may also fail to complete in tests when closing DevTools quickly after opening. - if (!e.message.includes("unrecognizedPacketType")) { - throw e; + this.mainRoot = createRootFront(this, packet); + + // Once the root actor has been communicated by the server, + // emit a request to it to also push informations down to the server. + // + // This request has been added in Firefox 133. + try { + await this.mainRoot.connect({ + frontendVersion: AppConstants.MOZ_APP_VERSION, + }); + } catch (e) { + // Ignore errors of unsupported packet as the server may not yet support this request. + // The request may also fail to complete in tests when closing DevTools quickly after opening. + if (!e.message.includes("unrecognizedPacketType")) { + throw e; + } } - } - this.emit("connected", packet.applicationType, packet.traits); - }); -} + this.emit("connected", packet.applicationType, packet.traits); + }); + } + + // Expose these to save callers the trouble of importing DebuggerSocket + static socketConnect(options) { + // Defined here instead of just copying the function to allow lazy-load + return DebuggerSocket.connect(options); + } + + static get Authenticators() { + return Authentication.Authenticators; + } + + static get AuthenticationResult() { + return Authentication.AuthenticationResult; + } -// Expose these to save callers the trouble of importing DebuggerSocket -DevToolsClient.socketConnect = function (options) { - // Defined here instead of just copying the function to allow lazy-load - return DebuggerSocket.connect(options); -}; -DevToolsUtils.defineLazyGetter(DevToolsClient, "Authenticators", () => { - return Authentication.Authenticators; -}); -DevToolsUtils.defineLazyGetter(DevToolsClient, "AuthenticationResult", () => { - return Authentication.AuthenticationResult; -}); - -DevToolsClient.prototype = { /** * Connect to the server and start exchanging protocol messages. * @@ -130,7 +134,7 @@ DevToolsClient.prototype = { this._transport.ready(); }); - }, + } /** * Shut down communication with the debugging server. @@ -159,7 +163,7 @@ DevToolsClient.prototype = { } return this._closePromise; - }, + } /** * Send a request to the debugging server. @@ -267,7 +271,7 @@ DevToolsClient.prototype = { request.catch = promise.catch.bind(promise); return request; - }, + } /** * Transmit streaming data via a bulk request. @@ -385,7 +389,7 @@ DevToolsClient.prototype = { this._sendOrQueueRequest(request); return request; - }, + } /** * If a new request can be sent immediately, do so. Otherwise, queue it. @@ -397,7 +401,7 @@ DevToolsClient.prototype = { } else { this._queueRequest(request); } - }, + } /** * Send a request. @@ -417,7 +421,7 @@ DevToolsClient.prototype = { this._transport.startBulkSend(request.request).then((...args) => { request.emit("bulk-send-ready", ...args); }); - }, + } /** * Queue a request to be sent later. Queues are only drained when an in @@ -428,7 +432,7 @@ DevToolsClient.prototype = { const queue = this._pendingRequests.get(actor) || []; queue.push(request); this._pendingRequests.set(actor, queue); - }, + } /** * Attempt the next request to a given actor (if any). @@ -446,7 +450,7 @@ DevToolsClient.prototype = { this._pendingRequests.delete(actor); } this._sendRequest(request); - }, + } /** * Arrange to hand the next reply from |actor| to the handler bound to @@ -471,7 +475,7 @@ DevToolsClient.prototype = { } this._activeRequests.set(actor, request); - }, + } // Transport hooks. @@ -548,7 +552,7 @@ DevToolsClient.prototype = { emitReply(); } } - }, + } /** * Called by the DebuggerTransport to dispatch incoming bulk packets as @@ -625,7 +629,7 @@ DevToolsClient.prototype = { this._attemptNextRequest(actor); activeRequest.emit("bulk-reply", packet); - }, + } /** * Called by DebuggerTransport when the underlying stream is closed. @@ -663,7 +667,7 @@ DevToolsClient.prototype = { for (const pool of this._pools) { pool.destroy(); } - }, + } /** * Purge pending and active requests in this client. @@ -729,7 +733,7 @@ DevToolsClient.prototype = { front.baseFrontClassDestroy(); } } - }, + } /** * Search for all requests in process for this client, including those made via @@ -795,7 +799,7 @@ DevToolsClient.prototype = { // Repeat, more requests may have started in response to those we just waited for return this.waitForRequestsToSettle({ ignoreOrphanedFronts }); }); - }, + } getAllFronts() { // Use a Set because some fronts (like domwalker) seem to have multiple parents. @@ -816,26 +820,25 @@ DevToolsClient.prototype = { } } return fronts; - }, + } /** * Actor lifetime management, echos the server's actor pools. */ - __pools: null, get _pools() { if (this.__pools) { return this.__pools; } this.__pools = new Set(); return this.__pools; - }, + } addActorPool(pool) { this._pools.add(pool); - }, + } removeActorPool(pool) { this._pools.delete(pool); - }, + } /** * Return the Front for the Actor whose ID is the one passed in argument. @@ -845,7 +848,7 @@ DevToolsClient.prototype = { getFrontByID(actorID) { const pool = this.poolFor(actorID); return pool ? pool.getActorByID(actorID) : null; - }, + } poolFor(actorID) { for (const pool of this._pools) { @@ -854,7 +857,7 @@ DevToolsClient.prototype = { } } return null; - }, + } /** * Creates an object front for this DevToolsClient and the grip in parameter, @@ -871,11 +874,11 @@ DevToolsClient.prototype = { } return new ObjectFront(this, threadFront.targetFront, parentFront, grip); - }, + } get transport() { return this._transport; - }, + } /** * Boolean flag to help identify client connected to the current runtime, @@ -883,7 +886,7 @@ DevToolsClient.prototype = { */ get isLocalClient() { return !!this._transport.isLocalTransport; - }, + } dumpPools() { for (const pool of this._pools) { @@ -891,10 +894,8 @@ DevToolsClient.prototype = { ...pool.__poolMap.keys(), ]); } - }, -}; - -EventEmitter.decorate(DevToolsClient.prototype); + } +} class Request extends EventEmitter { constructor(request) {