2021-07-09 11:00:52 +03:00
|
|
|
/* 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";
|
|
|
|
|
|
|
|
var EXPORTED_SYMBOLS = ["WebDriverBiDi"];
|
|
|
|
|
2022-07-12 07:21:34 +03:00
|
|
|
const { XPCOMUtils } = ChromeUtils.importESModule(
|
|
|
|
"resource://gre/modules/XPCOMUtils.sys.mjs"
|
2021-07-09 11:00:52 +03:00
|
|
|
);
|
|
|
|
|
2022-06-06 10:10:45 +03:00
|
|
|
const lazy = {};
|
|
|
|
|
|
|
|
XPCOMUtils.defineLazyModuleGetters(lazy, {
|
2021-07-16 14:56:48 +03:00
|
|
|
error: "chrome://remote/content/shared/webdriver/Errors.jsm",
|
|
|
|
Log: "chrome://remote/content/shared/Log.jsm",
|
2021-07-21 12:45:06 +03:00
|
|
|
WebDriverNewSessionHandler:
|
|
|
|
"chrome://remote/content/webdriver-bidi/NewSessionHandler.jsm",
|
2021-07-16 14:56:48 +03:00
|
|
|
WebDriverSession: "chrome://remote/content/shared/webdriver/Session.jsm",
|
2021-07-09 11:00:52 +03:00
|
|
|
});
|
|
|
|
|
2022-06-06 10:10:45 +03:00
|
|
|
XPCOMUtils.defineLazyGetter(lazy, "logger", () =>
|
|
|
|
lazy.Log.get(lazy.Log.TYPES.WEBDRIVER_BIDI)
|
2021-07-16 14:56:48 +03:00
|
|
|
);
|
2022-06-06 10:10:45 +03:00
|
|
|
XPCOMUtils.defineLazyGetter(lazy, "textEncoder", () => new TextEncoder());
|
2021-07-16 14:56:48 +03:00
|
|
|
|
2021-07-09 11:00:52 +03:00
|
|
|
/**
|
|
|
|
* Entry class for the WebDriver BiDi support.
|
|
|
|
*
|
|
|
|
* @see https://w3c.github.io/webdriver-bidi
|
|
|
|
*/
|
|
|
|
class WebDriverBiDi {
|
|
|
|
/**
|
|
|
|
* Creates a new instance of the WebDriverBiDi class.
|
|
|
|
*
|
|
|
|
* @param {RemoteAgent} agent
|
|
|
|
* Reference to the Remote Agent instance.
|
|
|
|
*/
|
|
|
|
constructor(agent) {
|
|
|
|
this.agent = agent;
|
2021-07-15 16:45:27 +03:00
|
|
|
this._running = false;
|
2021-07-16 14:56:48 +03:00
|
|
|
|
|
|
|
this._session = null;
|
2021-07-21 12:45:06 +03:00
|
|
|
this._sessionlessConnections = new Set();
|
2021-07-09 11:00:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
get address() {
|
|
|
|
return `ws://${this.agent.host}:${this.agent.port}`;
|
|
|
|
}
|
|
|
|
|
2021-07-16 14:56:48 +03:00
|
|
|
get session() {
|
|
|
|
return this._session;
|
|
|
|
}
|
|
|
|
|
2021-07-21 12:45:06 +03:00
|
|
|
/**
|
|
|
|
* Add a new connection that is not yet attached to a WebDriver session.
|
|
|
|
*
|
|
|
|
* @param {WebDriverBiDiConnection} connection
|
|
|
|
* The connection without an accociated WebDriver session.
|
|
|
|
*/
|
|
|
|
addSessionlessConnection(connection) {
|
|
|
|
this._sessionlessConnections.add(connection);
|
|
|
|
}
|
|
|
|
|
2021-07-16 14:56:48 +03:00
|
|
|
/**
|
|
|
|
* Create a new WebDriver session.
|
|
|
|
*
|
|
|
|
* @param {Object.<string, *>=} capabilities
|
|
|
|
* JSON Object containing any of the recognised capabilities as listed
|
|
|
|
* on the `WebDriverSession` class.
|
|
|
|
*
|
2021-07-21 12:45:06 +03:00
|
|
|
* @param {WebDriverBiDiConnection=} sessionlessConnection
|
|
|
|
* Optional connection that is not yet accociated with a WebDriver
|
|
|
|
* session, and has to be associated with the new WebDriver session.
|
|
|
|
*
|
2021-07-16 14:56:48 +03:00
|
|
|
* @return {Object<String, Capabilities>}
|
|
|
|
* Object containing the current session ID, and all its capabilities.
|
|
|
|
*
|
|
|
|
* @throws {SessionNotCreatedError}
|
|
|
|
* If, for whatever reason, a session could not be created.
|
|
|
|
*/
|
2022-05-04 07:28:49 +03:00
|
|
|
async createSession(capabilities, sessionlessConnection) {
|
2021-07-16 14:56:48 +03:00
|
|
|
if (this.session) {
|
2022-06-06 10:10:45 +03:00
|
|
|
throw new lazy.error.SessionNotCreatedError(
|
2021-07-16 14:56:48 +03:00
|
|
|
"Maximum number of active sessions"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-06-06 10:10:45 +03:00
|
|
|
const session = new lazy.WebDriverSession(
|
|
|
|
capabilities,
|
|
|
|
sessionlessConnection
|
|
|
|
);
|
2021-07-16 14:56:48 +03:00
|
|
|
|
2021-07-16 14:56:49 +03:00
|
|
|
// When the Remote Agent is listening, and a BiDi WebSocket connection
|
|
|
|
// has been requested, register a path handler for the session.
|
|
|
|
let webSocketUrl = null;
|
2021-07-21 12:45:06 +03:00
|
|
|
if (
|
2022-04-29 20:38:14 +03:00
|
|
|
this.agent.running &&
|
2022-05-04 07:28:49 +03:00
|
|
|
(session.capabilities.get("webSocketUrl") || sessionlessConnection)
|
2021-07-21 12:45:06 +03:00
|
|
|
) {
|
2022-05-04 07:28:49 +03:00
|
|
|
// Creating a WebDriver BiDi session too early can cause issues with
|
|
|
|
// clients in not being able to find any available browsing context.
|
|
|
|
// Also when closing the application while it's still starting up can
|
|
|
|
// cause shutdown hangs. As such WebDriver BiDi will return a new session
|
|
|
|
// once the initial application window has finished initializing.
|
2022-06-06 10:10:45 +03:00
|
|
|
lazy.logger.debug(`Waiting for initial application window`);
|
2022-05-04 07:28:49 +03:00
|
|
|
await this.agent.browserStartupFinished;
|
2021-07-16 14:56:49 +03:00
|
|
|
|
2022-05-04 07:28:49 +03:00
|
|
|
this.agent.server.registerPathHandler(session.path, session);
|
|
|
|
webSocketUrl = `${this.address}${session.path}`;
|
|
|
|
|
2022-06-06 10:10:45 +03:00
|
|
|
lazy.logger.debug(`Registered session handler: ${session.path}`);
|
2021-07-21 12:45:06 +03:00
|
|
|
|
|
|
|
if (sessionlessConnection) {
|
|
|
|
// Remove temporary session-less connection
|
|
|
|
this._sessionlessConnections.delete(sessionlessConnection);
|
|
|
|
}
|
2021-07-16 14:56:48 +03:00
|
|
|
}
|
|
|
|
|
2021-07-16 14:56:49 +03:00
|
|
|
// Also update the webSocketUrl capability to contain the session URL if
|
|
|
|
// a path handler has been registered. Otherwise set its value to null.
|
2022-05-04 07:28:49 +03:00
|
|
|
session.capabilities.set("webSocketUrl", webSocketUrl);
|
|
|
|
|
|
|
|
this._session = session;
|
2021-07-16 14:56:49 +03:00
|
|
|
|
2021-07-16 14:56:48 +03:00
|
|
|
return {
|
|
|
|
sessionId: this.session.id,
|
|
|
|
capabilities: this.session.capabilities,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete the current WebDriver session.
|
|
|
|
*/
|
|
|
|
deleteSession() {
|
|
|
|
if (!this.session) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-07-16 14:56:49 +03:00
|
|
|
// When the Remote Agent is listening, and a BiDi WebSocket is active,
|
|
|
|
// unregister the path handler for the session.
|
2022-04-29 20:38:14 +03:00
|
|
|
if (this.agent.running && this.session.capabilities.get("webSocketUrl")) {
|
2021-07-16 14:56:48 +03:00
|
|
|
this.agent.server.registerPathHandler(this.session.path, null);
|
2022-06-06 10:10:45 +03:00
|
|
|
lazy.logger.debug(`Unregistered session handler: ${this.session.path}`);
|
2021-07-16 14:56:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
this.session.destroy();
|
|
|
|
this._session = null;
|
|
|
|
}
|
|
|
|
|
2021-12-16 21:28:47 +03:00
|
|
|
/**
|
|
|
|
* Retrieve the readiness state of the remote end, regarding the creation of
|
|
|
|
* new WebDriverBiDi sessions.
|
|
|
|
*
|
|
|
|
* See https://w3c.github.io/webdriver-bidi/#command-session-status
|
|
|
|
*
|
|
|
|
* @return {Object}
|
|
|
|
* The readiness state.
|
|
|
|
*/
|
|
|
|
getSessionReadinessStatus() {
|
|
|
|
if (this.session) {
|
|
|
|
// We currently only support one session, see Bug 1720707.
|
|
|
|
return {
|
|
|
|
ready: false,
|
|
|
|
message: "Session already started",
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
ready: true,
|
|
|
|
message: "",
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-07-09 11:00:52 +03:00
|
|
|
/**
|
|
|
|
* Starts the WebDriver BiDi support.
|
|
|
|
*/
|
2022-01-25 16:42:05 +03:00
|
|
|
async start() {
|
2021-07-15 16:45:27 +03:00
|
|
|
if (this._running) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._running = true;
|
|
|
|
|
2021-07-21 12:45:06 +03:00
|
|
|
// Install a HTTP handler for direct WebDriver BiDi connection requests.
|
|
|
|
this.agent.server.registerPathHandler(
|
|
|
|
"/session",
|
2022-06-06 10:10:45 +03:00
|
|
|
new lazy.WebDriverNewSessionHandler(this)
|
2021-07-21 12:45:06 +03:00
|
|
|
);
|
|
|
|
|
2021-07-30 13:44:48 +03:00
|
|
|
Cu.printStderr(`WebDriver BiDi listening on ${this.address}\n`);
|
2022-01-25 16:42:05 +03:00
|
|
|
|
|
|
|
// Write WebSocket port to WebDriverBiDiActivePort file within the profile.
|
|
|
|
this._activePortPath = PathUtils.join(
|
2022-03-10 18:16:40 +03:00
|
|
|
PathUtils.profileDir,
|
2022-01-25 16:42:05 +03:00
|
|
|
"WebDriverBiDiActivePort"
|
|
|
|
);
|
|
|
|
|
|
|
|
const data = `${this.agent.port}`;
|
|
|
|
try {
|
2022-06-06 10:10:45 +03:00
|
|
|
await IOUtils.write(this._activePortPath, lazy.textEncoder.encode(data));
|
2022-01-25 16:42:05 +03:00
|
|
|
} catch (e) {
|
2022-06-06 10:10:45 +03:00
|
|
|
lazy.logger.warn(
|
|
|
|
`Failed to create ${this._activePortPath} (${e.message})`
|
|
|
|
);
|
2022-01-25 16:42:05 +03:00
|
|
|
}
|
2021-07-09 11:00:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stops the WebDriver BiDi support.
|
|
|
|
*/
|
2022-01-25 16:42:05 +03:00
|
|
|
async stop() {
|
2021-07-15 16:45:27 +03:00
|
|
|
if (!this._running) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-07-16 14:56:48 +03:00
|
|
|
try {
|
2022-01-25 16:42:05 +03:00
|
|
|
try {
|
|
|
|
await IOUtils.remove(this._activePortPath);
|
|
|
|
} catch (e) {
|
2022-06-06 10:10:45 +03:00
|
|
|
lazy.logger.warn(
|
|
|
|
`Failed to remove ${this._activePortPath} (${e.message})`
|
|
|
|
);
|
2022-01-25 16:42:05 +03:00
|
|
|
}
|
|
|
|
|
2021-07-16 14:56:48 +03:00
|
|
|
this.deleteSession();
|
2021-07-21 12:45:06 +03:00
|
|
|
|
|
|
|
this.agent.server.registerPathHandler("/session", null);
|
|
|
|
|
|
|
|
// Close all open session-less connections
|
|
|
|
this._sessionlessConnections.forEach(connection => connection.close());
|
|
|
|
this._sessionlessConnections.clear();
|
2021-07-16 14:56:48 +03:00
|
|
|
} finally {
|
|
|
|
this._running = false;
|
|
|
|
}
|
2021-07-15 16:45:27 +03:00
|
|
|
}
|
2021-07-09 11:00:52 +03:00
|
|
|
}
|