Bug 1733593 [wpt PR 31046] - Add a wpt for interaction id., a=testonly
[gecko.git] / remote / shared / WebSocketConnection.jsm
bloba804badee6639a7a970330aa2d5389bf738293ae
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 "use strict";
8 var EXPORTED_SYMBOLS = ["WebSocketConnection"];
10 const { XPCOMUtils } = ChromeUtils.import(
11   "resource://gre/modules/XPCOMUtils.jsm"
13 const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
15 XPCOMUtils.defineLazyModuleGetters(this, {
16   Log: "chrome://remote/content/shared/Log.jsm",
17   Services: "resource://gre/modules/Services.jsm",
18   WebSocketTransport: "chrome://remote/content/server/WebSocketTransport.jsm",
19 });
21 XPCOMUtils.defineLazyGetter(this, "logger", () => Log.get());
23 class WebSocketConnection {
24   /**
25    * @param {WebSocket} webSocket
26    *     The WebSocket server connection to wrap.
27    * @param {Connection} httpdConnection
28    *     Reference to the httpd.js's connection needed for clean-up.
29    */
30   constructor(webSocket, httpdConnection) {
31     this.id = Services.uuid.generateUUID().toString();
32     this.httpdConnection = httpdConnection;
34     this.transport = new WebSocketTransport(webSocket);
35     this.transport.hooks = this;
36     this.transport.ready();
38     logger.debug(`${this.constructor.name} ${this.id} accepted`);
39   }
41   /**
42    * Close the WebSocket connection.
43    */
44   close() {
45     this.transport.close();
47     // In addition to the WebSocket transport, we also have to close the
48     // connection used internally within httpd.js. Otherwise the server doesn't
49     // shut down correctly, and keeps these Connection instances alive.
50     this.httpdConnection.close();
51   }
53   /**
54    * Register a new Session to forward the messages to.
55    *
56    * Needs to be implemented in the sub class.
57    *
58    * @param Session session
59    *     The session to register.
60    */
61   registerSession(session) {
62     throw new Error("Not implemented");
63   }
65   /**
66    * Send the JSON-serializable object to the client.
67    *
68    * @param {Object} data
69    *     The object to be sent.
70    */
71   send(data) {
72     this.transport.send(data);
73   }
75   /**
76    * Send an error back to the client.
77    *
78    * Needs to be implemented in the sub class.
79    */
80   sendError() {
81     throw new Error("Not implemented");
82   }
84   /**
85    * Send an event back to the client.
86    *
87    * Needs to be implemented in the sub class.
88    */
89   sendEvent() {
90     throw new Error("Not implemented");
91   }
93   /**
94    * Send the result of a call to a method back to the client.
95    *
96    * Needs to be implemented in the sub class.
97    */
98   sendResult() {
99     throw new Error("Not implemented");
100   }
102   toString() {
103     return `[object ${this.constructor.name} ${this.id}]`;
104   }
106   // Transport hooks
108   /**
109    * Called by the `transport` when the connection is closed.
110    */
111   onClosed(status) {
112     logger.debug(`${this.constructor.name} ${this.id} closed`);
113   }
115   /**
116    * Receive a packet from the WebSocket layer.
117    *
118    * This packet is sent by a WebSocket client and is meant to execute
119    * a particular method.
120    *
121    * Needs to be implemented in the sub class.
122    *
123    * @param {Object} packet
124    *     JSON-serializable object sent by the client.
125    */
126   async onPacket(packet) {
127     throw new Error("Not implemented");
128   }