1 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs";
10 ChromeUtils.defineESModuleGetters(lazy, {
11 Log: "chrome://remote/content/shared/Log.sys.mjs",
12 truncate: "chrome://remote/content/shared/Format.sys.mjs",
14 "chrome://remote/content/server/WebSocketTransport.sys.mjs",
17 XPCOMUtils.defineLazyGetter(lazy, "logger", () => lazy.Log.get());
19 export class WebSocketConnection {
21 * @param {WebSocket} webSocket
22 * The WebSocket server connection to wrap.
23 * @param {Connection} httpdConnection
24 * Reference to the httpd.js's connection needed for clean-up.
26 constructor(webSocket, httpdConnection) {
27 this.id = Services.uuid
32 this.httpdConnection = httpdConnection;
34 this.transport = new lazy.WebSocketTransport(webSocket);
35 this.transport.hooks = this;
36 this.transport.ready();
38 lazy.logger.debug(`${this.constructor.name} ${this.id} accepted`);
41 _log(direction, data) {
42 function replacer(key, value) {
43 if (typeof value === "string") {
44 return lazy.truncate`${value}`;
49 const payload = JSON.stringify(
52 lazy.Log.verbose ? "\t" : null
56 `${this.constructor.name} ${this.id} ${direction} ${payload}`
61 * Close the WebSocket connection.
64 this.transport.close();
66 // In addition to the WebSocket transport, we also have to close the
67 // connection used internally within httpd.js. Otherwise the server doesn't
68 // shut down correctly, and keeps these Connection instances alive.
69 this.httpdConnection.close();
73 * Register a new Session to forward the messages to.
75 * Needs to be implemented in the sub class.
77 * @param Session session
78 * The session to register.
80 registerSession(session) {
81 throw new Error("Not implemented");
85 * Send the JSON-serializable object to the client.
87 * @param {Object} data
88 * The object to be sent.
91 this._log("<-", data);
92 this.transport.send(data);
96 * Send an error back to the client.
98 * Needs to be implemented in the sub class.
101 throw new Error("Not implemented");
105 * Send an event back to the client.
107 * Needs to be implemented in the sub class.
110 throw new Error("Not implemented");
114 * Send the result of a call to a method back to the client.
116 * Needs to be implemented in the sub class.
119 throw new Error("Not implemented");
123 return `[object ${this.constructor.name} ${this.id}]`;
129 * Called by the `transport` when the connection is closed.
132 lazy.logger.debug(`${this.constructor.name} ${this.id} closed`);
136 * Receive a packet from the WebSocket layer.
138 * This packet is sent by a WebSocket client and is meant to execute
139 * a particular method.
141 * Needs to be implemented in the sub class.
143 * @param {Object} packet
144 * JSON-serializable object sent by the client.
146 async onPacket(packet) {
147 this._log("->", packet);