Bug 1837502 [wpt PR 40459] - Update wpt metadata, a=testonly
[gecko.git] / remote / webdriver-bidi / NewSessionHandler.sys.mjs
blob342419033f720424cb6b99c1eae34cfb39531810
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 const lazy = {};
7 ChromeUtils.defineESModuleGetters(lazy, {
8   WebDriverBiDiConnection:
9     "chrome://remote/content/webdriver-bidi/WebDriverBiDiConnection.sys.mjs",
10   WebSocketHandshake:
11     "chrome://remote/content/server/WebSocketHandshake.sys.mjs",
12 });
14 /**
15  * httpd.js JSON handler for direct BiDi connections.
16  */
17 export class WebDriverNewSessionHandler {
18   /**
19    * Construct a new JSON handler.
20    *
21    * @param {WebDriverBiDi} webDriverBiDi
22    *     Reference to the WebDriver BiDi protocol implementation.
23    */
24   constructor(webDriverBiDi) {
25     this.webDriverBiDi = webDriverBiDi;
26   }
28   // nsIHttpRequestHandler
30   /**
31    * Handle new direct WebSocket connection requests.
32    *
33    * WebSocket clients not using the WebDriver BiDi opt-in mechanism via the
34    * WebDriver HTTP implementation will attempt to directly connect at
35    * `/session`.  Hereby a WebSocket upgrade will automatically be performed.
36    *
37    * @param {Request} request
38    *     HTTP request (httpd.js)
39    * @param {Response} response
40    *     Response to an HTTP request (httpd.js)
41    */
42   async handle(request, response) {
43     const webSocket = await lazy.WebSocketHandshake.upgrade(request, response);
44     const conn = new lazy.WebDriverBiDiConnection(
45       webSocket,
46       response._connection
47     );
49     this.webDriverBiDi.addSessionlessConnection(conn);
50   }
52   // XPCOM
54   get QueryInterface() {
55     return ChromeUtils.generateQI(["nsIHttpRequestHandler"]);
56   }