2015-03-20 00:12:58 +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/. */
|
|
|
|
|
2022-09-30 14:08:08 +03:00
|
|
|
import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs";
|
2020-09-08 21:22:43 +03:00
|
|
|
|
2022-06-06 10:10:43 +03:00
|
|
|
const lazy = {};
|
|
|
|
|
2022-09-30 14:08:06 +03:00
|
|
|
ChromeUtils.defineESModuleGetters(lazy, {
|
|
|
|
assert: "chrome://remote/content/shared/webdriver/Assert.sys.mjs",
|
2022-09-30 14:08:08 +03:00
|
|
|
Command: "chrome://remote/content/marionette/message.sys.mjs",
|
|
|
|
DebuggerTransport: "chrome://remote/content/marionette/transport.sys.mjs",
|
2022-09-30 14:08:06 +03:00
|
|
|
error: "chrome://remote/content/shared/webdriver/Errors.sys.mjs",
|
2022-09-30 14:08:08 +03:00
|
|
|
GeckoDriver: "chrome://remote/content/marionette/driver.sys.mjs",
|
2022-09-30 14:08:06 +03:00
|
|
|
Log: "chrome://remote/content/shared/Log.sys.mjs",
|
2022-09-30 14:08:08 +03:00
|
|
|
MarionettePrefs: "chrome://remote/content/marionette/prefs.sys.mjs",
|
|
|
|
Message: "chrome://remote/content/marionette/message.sys.mjs",
|
|
|
|
Response: "chrome://remote/content/marionette/message.sys.mjs",
|
2020-09-08 21:22:43 +03:00
|
|
|
});
|
|
|
|
|
2022-06-06 10:10:43 +03:00
|
|
|
XPCOMUtils.defineLazyGetter(lazy, "logger", () =>
|
|
|
|
lazy.Log.get(lazy.Log.TYPES.MARIONETTE)
|
2021-07-08 11:12:53 +03:00
|
|
|
);
|
2022-06-06 10:10:43 +03:00
|
|
|
XPCOMUtils.defineLazyGetter(lazy, "ServerSocket", () => {
|
2020-09-08 21:32:37 +03:00
|
|
|
return Components.Constructor(
|
|
|
|
"@mozilla.org/network/server-socket;1",
|
|
|
|
"nsIServerSocket",
|
|
|
|
"initSpecialConnection"
|
|
|
|
);
|
|
|
|
});
|
2015-03-20 00:12:58 +03:00
|
|
|
|
2017-04-19 17:10:28 +03:00
|
|
|
const { KeepWhenOffline, LoopbackOnly } = Ci.nsIServerSocket;
|
|
|
|
|
2017-03-06 20:39:42 +03:00
|
|
|
const PROTOCOL_VERSION = 3;
|
2015-03-20 00:12:58 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Bootstraps Marionette and handles incoming client connections.
|
|
|
|
*
|
2017-03-06 20:39:42 +03:00
|
|
|
* Starting the Marionette server will open a TCP socket sporting the
|
2018-04-23 12:19:52 +03:00
|
|
|
* debugger transport interface on the provided `port`. For every
|
|
|
|
* new connection, a {@link TCPConnection} is created.
|
2015-03-20 00:12:58 +03:00
|
|
|
*/
|
2022-09-30 14:08:08 +03:00
|
|
|
export class TCPListener {
|
2017-03-06 20:09:07 +03:00
|
|
|
/**
|
|
|
|
* @param {number} port
|
|
|
|
* Port for server to listen to.
|
|
|
|
*/
|
2017-06-30 02:40:24 +03:00
|
|
|
constructor(port) {
|
2017-03-06 20:09:07 +03:00
|
|
|
this.port = port;
|
2017-04-24 12:28:57 +03:00
|
|
|
this.socket = null;
|
2017-03-06 20:39:42 +03:00
|
|
|
this.conns = new Set();
|
|
|
|
this.nextConnID = 0;
|
2017-03-06 20:09:07 +03:00
|
|
|
this.alive = false;
|
|
|
|
}
|
2015-03-20 00:12:58 +03:00
|
|
|
|
2017-03-06 20:09:07 +03:00
|
|
|
/**
|
2018-04-23 12:19:52 +03:00
|
|
|
* Function produces a {@link GeckoDriver}.
|
2017-03-06 20:09:07 +03:00
|
|
|
*
|
2017-10-12 14:21:17 +03:00
|
|
|
* Determines the application to initialise the driver with.
|
2017-03-06 20:09:07 +03:00
|
|
|
*
|
|
|
|
* @return {GeckoDriver}
|
|
|
|
* A driver instance.
|
|
|
|
*/
|
2017-06-30 02:40:24 +03:00
|
|
|
driverFactory() {
|
2022-06-06 10:10:43 +03:00
|
|
|
return new lazy.GeckoDriver(this);
|
2016-10-17 14:19:19 +03:00
|
|
|
}
|
|
|
|
|
2017-06-30 02:40:24 +03:00
|
|
|
set acceptConnections(value) {
|
2017-10-23 15:08:15 +03:00
|
|
|
if (value) {
|
|
|
|
if (!this.socket) {
|
2018-03-28 20:17:46 +03:00
|
|
|
try {
|
|
|
|
const flags = KeepWhenOffline | LoopbackOnly;
|
|
|
|
const backlog = 1;
|
2022-06-06 10:10:43 +03:00
|
|
|
this.socket = new lazy.ServerSocket(this.port, flags, backlog);
|
2018-03-28 20:17:46 +03:00
|
|
|
} catch (e) {
|
|
|
|
throw new Error(`Could not bind to port ${this.port} (${e.name})`);
|
|
|
|
}
|
|
|
|
|
2017-10-23 15:08:15 +03:00
|
|
|
this.port = this.socket.port;
|
|
|
|
|
|
|
|
this.socket.asyncListen(this);
|
2022-06-06 10:10:43 +03:00
|
|
|
lazy.logger.info(`Listening on port ${this.port}`);
|
2017-10-23 15:08:15 +03:00
|
|
|
}
|
|
|
|
} else if (this.socket) {
|
2018-05-10 19:20:17 +03:00
|
|
|
// Note that closing the server socket will not close currently active
|
|
|
|
// connections.
|
2017-10-23 15:08:15 +03:00
|
|
|
this.socket.close();
|
|
|
|
this.socket = null;
|
2022-06-06 10:10:43 +03:00
|
|
|
lazy.logger.info(`Stopped listening on port ${this.port}`);
|
2017-10-23 15:08:15 +03:00
|
|
|
}
|
2015-03-20 00:12:58 +03:00
|
|
|
}
|
Bug 1344748 - Set recommended prefs when Marionette starts; r=automatedtester,maja_zf,whimboo
This makes the Marionette server itself set a long list of recommended
automation preferences when it starts up, and reset those it changed
when stopping.
Preferences used in automation are currently written to the Firefox
profile before Firefox starts, but after a closer examination of the
preferences, it is thought that many of them can be set at runtime.
There is a subset of preferences that are checked on startup and which
must be set in the profile. These are clearly called out in the comments.
We still set them at runtime, since we foresee a future where it will
be possible to attach an existing Firefox session to geckodriver, and
many of the prefs can also be checked at runtime during the course of
that automation session.
For example, if we would not set the "app.update.auto" preference in
such a runtime, opening the About dialogue would cause a forced update
of Firefox. This is not desirable when the browser is under Marionette
control. When the Marionette server is stopped, the altered preferences
are reset and the browser session's state is returned to its pre-existing
condition.
This change does not mean it is dangerous or wrong for consumers to write
their own preferences to the profile. Any preferences written to the
profile will take precedence over this set of recommended preferences.
If the recommended Marionette preference has a user-defined value (i.e. it
is written to the profile before starting up or has manually changed),
that user-set value is preferred.
The list of preferences in this file is the authorative reference of
recommended preferences for using Marionette in automation. They have
been gathered from geckoinstance.py and geckodriver.
MozReview-Commit-ID: INHSQRg2XjF
--HG--
extra : rebase_source : e1684133d287d2891feaa52ae4d267e8df4fa8e2
2017-03-06 19:47:38 +03:00
|
|
|
|
2017-04-24 12:28:57 +03:00
|
|
|
/**
|
2018-01-12 17:41:35 +03:00
|
|
|
* Bind this listener to {@link #port} and start accepting incoming
|
|
|
|
* socket connections on {@link #onSocketAccepted}.
|
2017-04-24 12:28:57 +03:00
|
|
|
*
|
|
|
|
* The marionette.port preference will be populated with the value
|
2018-01-12 17:41:35 +03:00
|
|
|
* of {@link #port}.
|
2017-04-24 12:28:57 +03:00
|
|
|
*/
|
2017-06-30 02:40:24 +03:00
|
|
|
start() {
|
2017-03-06 20:09:07 +03:00
|
|
|
if (this.alive) {
|
|
|
|
return;
|
Bug 1344748 - Set recommended prefs when Marionette starts; r=automatedtester,maja_zf,whimboo
This makes the Marionette server itself set a long list of recommended
automation preferences when it starts up, and reset those it changed
when stopping.
Preferences used in automation are currently written to the Firefox
profile before Firefox starts, but after a closer examination of the
preferences, it is thought that many of them can be set at runtime.
There is a subset of preferences that are checked on startup and which
must be set in the profile. These are clearly called out in the comments.
We still set them at runtime, since we foresee a future where it will
be possible to attach an existing Firefox session to geckodriver, and
many of the prefs can also be checked at runtime during the course of
that automation session.
For example, if we would not set the "app.update.auto" preference in
such a runtime, opening the About dialogue would cause a forced update
of Firefox. This is not desirable when the browser is under Marionette
control. When the Marionette server is stopped, the altered preferences
are reset and the browser session's state is returned to its pre-existing
condition.
This change does not mean it is dangerous or wrong for consumers to write
their own preferences to the profile. Any preferences written to the
profile will take precedence over this set of recommended preferences.
If the recommended Marionette preference has a user-defined value (i.e. it
is written to the profile before starting up or has manually changed),
that user-set value is preferred.
The list of preferences in this file is the authorative reference of
recommended preferences for using Marionette in automation. They have
been gathered from geckoinstance.py and geckodriver.
MozReview-Commit-ID: INHSQRg2XjF
--HG--
extra : rebase_source : e1684133d287d2891feaa52ae4d267e8df4fa8e2
2017-03-06 19:47:38 +03:00
|
|
|
}
|
|
|
|
|
2017-10-23 15:08:15 +03:00
|
|
|
// Start socket server and listening for connection attempts
|
|
|
|
this.acceptConnections = true;
|
2022-06-06 10:10:43 +03:00
|
|
|
lazy.MarionettePrefs.port = this.port;
|
2017-03-06 20:09:07 +03:00
|
|
|
this.alive = true;
|
2015-03-20 00:12:58 +03:00
|
|
|
}
|
Bug 1344748 - Set recommended prefs when Marionette starts; r=automatedtester,maja_zf,whimboo
This makes the Marionette server itself set a long list of recommended
automation preferences when it starts up, and reset those it changed
when stopping.
Preferences used in automation are currently written to the Firefox
profile before Firefox starts, but after a closer examination of the
preferences, it is thought that many of them can be set at runtime.
There is a subset of preferences that are checked on startup and which
must be set in the profile. These are clearly called out in the comments.
We still set them at runtime, since we foresee a future where it will
be possible to attach an existing Firefox session to geckodriver, and
many of the prefs can also be checked at runtime during the course of
that automation session.
For example, if we would not set the "app.update.auto" preference in
such a runtime, opening the About dialogue would cause a forced update
of Firefox. This is not desirable when the browser is under Marionette
control. When the Marionette server is stopped, the altered preferences
are reset and the browser session's state is returned to its pre-existing
condition.
This change does not mean it is dangerous or wrong for consumers to write
their own preferences to the profile. Any preferences written to the
profile will take precedence over this set of recommended preferences.
If the recommended Marionette preference has a user-defined value (i.e. it
is written to the profile before starting up or has manually changed),
that user-set value is preferred.
The list of preferences in this file is the authorative reference of
recommended preferences for using Marionette in automation. They have
been gathered from geckoinstance.py and geckodriver.
MozReview-Commit-ID: INHSQRg2XjF
--HG--
extra : rebase_source : e1684133d287d2891feaa52ae4d267e8df4fa8e2
2017-03-06 19:47:38 +03:00
|
|
|
|
2017-06-30 02:40:24 +03:00
|
|
|
stop() {
|
2017-03-06 20:09:07 +03:00
|
|
|
if (!this.alive) {
|
|
|
|
return;
|
|
|
|
}
|
Bug 1344748 - Set recommended prefs when Marionette starts; r=automatedtester,maja_zf,whimboo
This makes the Marionette server itself set a long list of recommended
automation preferences when it starts up, and reset those it changed
when stopping.
Preferences used in automation are currently written to the Firefox
profile before Firefox starts, but after a closer examination of the
preferences, it is thought that many of them can be set at runtime.
There is a subset of preferences that are checked on startup and which
must be set in the profile. These are clearly called out in the comments.
We still set them at runtime, since we foresee a future where it will
be possible to attach an existing Firefox session to geckodriver, and
many of the prefs can also be checked at runtime during the course of
that automation session.
For example, if we would not set the "app.update.auto" preference in
such a runtime, opening the About dialogue would cause a forced update
of Firefox. This is not desirable when the browser is under Marionette
control. When the Marionette server is stopped, the altered preferences
are reset and the browser session's state is returned to its pre-existing
condition.
This change does not mean it is dangerous or wrong for consumers to write
their own preferences to the profile. Any preferences written to the
profile will take precedence over this set of recommended preferences.
If the recommended Marionette preference has a user-defined value (i.e. it
is written to the profile before starting up or has manually changed),
that user-set value is preferred.
The list of preferences in this file is the authorative reference of
recommended preferences for using Marionette in automation. They have
been gathered from geckoinstance.py and geckodriver.
MozReview-Commit-ID: INHSQRg2XjF
--HG--
extra : rebase_source : e1684133d287d2891feaa52ae4d267e8df4fa8e2
2017-03-06 19:47:38 +03:00
|
|
|
|
2017-10-23 15:08:15 +03:00
|
|
|
// Shutdown server socket, and no longer listen for new connections
|
|
|
|
this.acceptConnections = false;
|
2017-03-06 20:09:07 +03:00
|
|
|
this.alive = false;
|
2016-10-17 14:19:19 +03:00
|
|
|
}
|
|
|
|
|
2021-09-21 17:09:45 +03:00
|
|
|
onSocketAccepted(serverSocket, clientSocket) {
|
2017-03-06 20:09:07 +03:00
|
|
|
let input = clientSocket.openInputStream(0, 0, 0);
|
|
|
|
let output = clientSocket.openOutputStream(0, 0, 0);
|
2022-06-06 10:10:43 +03:00
|
|
|
let transport = new lazy.DebuggerTransport(input, output);
|
2015-03-20 00:12:58 +03:00
|
|
|
|
2021-05-19 21:12:10 +03:00
|
|
|
// Only allow a single active WebDriver session at a time
|
|
|
|
const hasActiveSession = [...this.conns].find(
|
|
|
|
conn => !!conn.driver.currentSession
|
|
|
|
);
|
|
|
|
if (hasActiveSession) {
|
2022-06-06 10:10:43 +03:00
|
|
|
lazy.logger.warn(
|
2021-05-19 21:12:10 +03:00
|
|
|
"Connection attempt denied because an active session has been found"
|
|
|
|
);
|
|
|
|
|
|
|
|
// Ideally we should stop the server to listen for new connection
|
|
|
|
// attempts, but the current architecture doesn't allow us to do that.
|
|
|
|
// As such just close the transport if no further connections are allowed.
|
2021-09-21 17:09:45 +03:00
|
|
|
transport.close();
|
2021-05-19 21:12:10 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-23 12:19:52 +03:00
|
|
|
let conn = new TCPConnection(
|
2017-03-06 20:39:42 +03:00
|
|
|
this.nextConnID++,
|
|
|
|
transport,
|
|
|
|
this.driverFactory.bind(this)
|
|
|
|
);
|
|
|
|
conn.onclose = this.onConnectionClosed.bind(this);
|
|
|
|
this.conns.add(conn);
|
2015-03-20 00:12:58 +03:00
|
|
|
|
2022-06-06 10:10:43 +03:00
|
|
|
lazy.logger.debug(
|
2017-06-30 02:40:24 +03:00
|
|
|
`Accepted connection ${conn.id} ` +
|
|
|
|
`from ${clientSocket.host}:${clientSocket.port}`
|
|
|
|
);
|
2017-03-06 20:39:42 +03:00
|
|
|
conn.sayHello();
|
2017-03-06 20:09:07 +03:00
|
|
|
transport.ready();
|
|
|
|
}
|
|
|
|
|
2017-06-30 02:40:24 +03:00
|
|
|
onConnectionClosed(conn) {
|
2022-06-06 10:10:43 +03:00
|
|
|
lazy.logger.debug(`Closed connection ${conn.id}`);
|
2017-03-06 20:39:42 +03:00
|
|
|
this.conns.delete(conn);
|
|
|
|
}
|
2018-04-23 12:19:52 +03:00
|
|
|
}
|
2017-03-06 20:39:42 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Marionette client connection.
|
|
|
|
*
|
|
|
|
* Dispatches packets received to their correct service destinations
|
|
|
|
* and sends back the service endpoint's return values.
|
|
|
|
*
|
|
|
|
* @param {number} connID
|
|
|
|
* Unique identifier of the connection this dispatcher should handle.
|
|
|
|
* @param {DebuggerTransport} transport
|
|
|
|
* Debugger transport connection to the client.
|
|
|
|
* @param {function(): GeckoDriver} driverFactory
|
2018-01-12 17:41:35 +03:00
|
|
|
* Factory function that produces a {@link GeckoDriver}.
|
2017-03-06 20:39:42 +03:00
|
|
|
*/
|
2022-09-30 14:08:08 +03:00
|
|
|
export class TCPConnection {
|
2017-06-30 02:40:24 +03:00
|
|
|
constructor(connID, transport, driverFactory) {
|
2017-03-06 20:39:42 +03:00
|
|
|
this.id = connID;
|
|
|
|
this.conn = transport;
|
|
|
|
|
|
|
|
// transport hooks are TCPConnection#onPacket
|
|
|
|
// and TCPConnection#onClosed
|
|
|
|
this.conn.hooks = this;
|
|
|
|
|
|
|
|
// callback for when connection is closed
|
|
|
|
this.onclose = null;
|
|
|
|
|
|
|
|
// last received/sent message ID
|
|
|
|
this.lastID = 0;
|
|
|
|
|
|
|
|
this.driver = driverFactory();
|
|
|
|
}
|
|
|
|
|
2022-09-19 22:19:22 +03:00
|
|
|
#log(msg) {
|
|
|
|
let dir = msg.origin == lazy.Message.Origin.Client ? "->" : "<-";
|
|
|
|
lazy.logger.debug(`${this.id} ${dir} ${msg.toString()}`);
|
|
|
|
}
|
|
|
|
|
2017-03-06 20:39:42 +03:00
|
|
|
/**
|
|
|
|
* Debugger transport callback that cleans up
|
|
|
|
* after a connection is closed.
|
|
|
|
*/
|
2021-09-21 17:09:45 +03:00
|
|
|
onClosed() {
|
|
|
|
this.driver.deleteSession();
|
2017-03-06 20:39:42 +03:00
|
|
|
if (this.onclose) {
|
|
|
|
this.onclose(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback that receives data packets from the client.
|
|
|
|
*
|
|
|
|
* If the message is a Response, we look up the command previously
|
|
|
|
* issued to the client and run its callback, if any. In case of
|
|
|
|
* a Command, the corresponding is executed.
|
|
|
|
*
|
|
|
|
* @param {Array.<number, number, ?, ?>} data
|
|
|
|
* A four element array where the elements, in sequence, signifies
|
|
|
|
* message type, message ID, method name or error, and parameters
|
|
|
|
* or result.
|
|
|
|
*/
|
2017-06-30 02:40:24 +03:00
|
|
|
onPacket(data) {
|
2017-02-09 21:29:25 +03:00
|
|
|
// unable to determine how to respond
|
|
|
|
if (!Array.isArray(data)) {
|
|
|
|
let e = new TypeError(
|
|
|
|
"Unable to unmarshal packet data: " + JSON.stringify(data)
|
|
|
|
);
|
2022-06-06 10:10:43 +03:00
|
|
|
lazy.error.report(e);
|
2017-02-09 21:29:25 +03:00
|
|
|
return;
|
|
|
|
}
|
2017-03-06 20:39:42 +03:00
|
|
|
|
2017-02-09 21:29:25 +03:00
|
|
|
// return immediately with any error trying to unmarshal message
|
|
|
|
let msg;
|
|
|
|
try {
|
2022-06-06 10:10:43 +03:00
|
|
|
msg = lazy.Message.fromPacket(data);
|
|
|
|
msg.origin = lazy.Message.Origin.Client;
|
2022-09-19 22:19:22 +03:00
|
|
|
this.#log(msg);
|
2017-02-09 21:29:25 +03:00
|
|
|
} catch (e) {
|
|
|
|
let resp = this.createResponse(data[1]);
|
|
|
|
resp.sendError(e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// execute new command
|
2022-06-06 10:10:43 +03:00
|
|
|
if (msg instanceof lazy.Command) {
|
2017-08-07 21:02:20 +03:00
|
|
|
(async () => {
|
|
|
|
await this.execute(msg);
|
|
|
|
})();
|
2018-10-03 18:11:43 +03:00
|
|
|
} else {
|
2022-06-06 10:10:43 +03:00
|
|
|
lazy.logger.fatal("Cannot process messages other than Command");
|
2017-03-06 20:39:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-07 21:02:20 +03:00
|
|
|
* Executes a Marionette command and sends back a response when it
|
|
|
|
* has finished executing.
|
2017-03-06 20:39:42 +03:00
|
|
|
*
|
|
|
|
* If the command implementation sends the response itself by calling
|
2017-08-07 21:02:20 +03:00
|
|
|
* <code>resp.send()</code>, the response is guaranteed to not be
|
|
|
|
* sent twice.
|
2017-03-06 20:39:42 +03:00
|
|
|
*
|
|
|
|
* Errors thrown in commands are marshaled and sent back, and if they
|
2017-08-07 21:02:20 +03:00
|
|
|
* are not {@link WebDriverError} instances, they are additionally
|
|
|
|
* propagated and reported to {@link Components.utils.reportError}.
|
2017-03-06 20:39:42 +03:00
|
|
|
*
|
|
|
|
* @param {Command} cmd
|
2017-08-07 21:02:20 +03:00
|
|
|
* Command to execute.
|
2017-03-06 20:39:42 +03:00
|
|
|
*/
|
2017-08-07 21:02:20 +03:00
|
|
|
async execute(cmd) {
|
2017-02-09 21:29:25 +03:00
|
|
|
let resp = this.createResponse(cmd.id);
|
2017-03-06 20:39:42 +03:00
|
|
|
let sendResponse = () => resp.sendConditionally(resp => !resp.sent);
|
|
|
|
let sendError = resp.sendError.bind(resp);
|
|
|
|
|
2017-08-07 21:02:20 +03:00
|
|
|
await this.despatch(cmd, resp)
|
|
|
|
.then(sendResponse, sendError)
|
2022-06-06 10:10:43 +03:00
|
|
|
.catch(lazy.error.report);
|
2017-08-07 21:02:20 +03:00
|
|
|
}
|
2017-03-06 20:39:42 +03:00
|
|
|
|
2017-08-07 21:02:20 +03:00
|
|
|
/**
|
|
|
|
* Despatches command to appropriate Marionette service.
|
|
|
|
*
|
|
|
|
* @param {Command} cmd
|
|
|
|
* Command to run.
|
|
|
|
* @param {Response} resp
|
|
|
|
* Mutable response where the command's return value will be
|
|
|
|
* assigned.
|
|
|
|
*
|
|
|
|
* @throws {Error}
|
|
|
|
* A command's implementation may throw at any time.
|
|
|
|
*/
|
|
|
|
async despatch(cmd, resp) {
|
|
|
|
let fn = this.driver.commands[cmd.name];
|
|
|
|
if (typeof fn == "undefined") {
|
2022-06-06 10:10:43 +03:00
|
|
|
throw new lazy.error.UnknownCommandError(cmd.name);
|
2017-08-07 21:02:20 +03:00
|
|
|
}
|
2017-03-06 20:39:42 +03:00
|
|
|
|
2018-08-21 09:07:23 +03:00
|
|
|
if (cmd.name != "WebDriver:NewSession") {
|
2022-06-06 10:10:43 +03:00
|
|
|
lazy.assert.session(this.driver.currentSession);
|
2017-08-07 21:02:20 +03:00
|
|
|
}
|
2017-03-06 20:39:42 +03:00
|
|
|
|
2018-05-15 16:17:41 +03:00
|
|
|
let rv = await fn.bind(this.driver)(cmd);
|
2017-03-06 20:39:42 +03:00
|
|
|
|
2023-02-28 23:55:55 +03:00
|
|
|
// Bug 1819029: Some older commands cannot return a response wrapped within
|
|
|
|
// a value field because it would break compatibility with geckodriver and
|
|
|
|
// Marionette client. It's unlikely that we are going to fix that.
|
|
|
|
//
|
|
|
|
// Warning: No more commands should be added to this list!
|
|
|
|
const commandsNoValueResponse = [
|
|
|
|
"Marionette:Quit",
|
|
|
|
"WebDriver:FindElements",
|
2023-03-16 22:19:40 +03:00
|
|
|
"WebDriver:FindElementsFromShadowRoot",
|
2023-02-28 23:55:55 +03:00
|
|
|
"WebDriver:CloseChromeWindow",
|
|
|
|
"WebDriver:CloseWindow",
|
|
|
|
"WebDriver:FullscreenWindow",
|
|
|
|
"WebDriver:GetCookies",
|
|
|
|
"WebDriver:GetElementRect",
|
|
|
|
"WebDriver:GetTimeouts",
|
|
|
|
"WebDriver:GetWindowHandles",
|
|
|
|
"WebDriver:GetWindowRect",
|
|
|
|
"WebDriver:MaximizeWindow",
|
|
|
|
"WebDriver:MinimizeWindow",
|
|
|
|
"WebDriver:NewSession",
|
|
|
|
"WebDriver:NewWindow",
|
|
|
|
"WebDriver:SetWindowRect",
|
|
|
|
];
|
|
|
|
|
2018-05-15 16:17:41 +03:00
|
|
|
if (rv != null) {
|
2023-02-28 23:55:55 +03:00
|
|
|
// By default the Response' constructor sets the body to `{ value: null }`.
|
|
|
|
// As such we only want to override the value if it's neither `null` nor
|
|
|
|
// `undefined`.
|
|
|
|
if (commandsNoValueResponse.includes(cmd.name)) {
|
2017-08-07 21:02:20 +03:00
|
|
|
resp.body = rv;
|
2023-02-28 23:55:55 +03:00
|
|
|
} else {
|
|
|
|
resp.body.value = rv;
|
2017-08-07 21:02:20 +03:00
|
|
|
}
|
|
|
|
}
|
2017-03-06 20:39:42 +03:00
|
|
|
}
|
|
|
|
|
2017-02-09 21:29:25 +03:00
|
|
|
/**
|
2018-01-12 17:41:35 +03:00
|
|
|
* Fail-safe creation of a new instance of {@link Response}.
|
2017-02-09 21:29:25 +03:00
|
|
|
*
|
2017-07-26 15:11:53 +03:00
|
|
|
* @param {number} msgID
|
2017-02-09 21:29:25 +03:00
|
|
|
* Message ID to respond to. If it is not a number, -1 is used.
|
|
|
|
*
|
2018-01-12 17:41:35 +03:00
|
|
|
* @return {Response}
|
2018-04-23 12:19:52 +03:00
|
|
|
* Response to the message with `msgID`.
|
2017-02-09 21:29:25 +03:00
|
|
|
*/
|
2017-06-30 02:40:24 +03:00
|
|
|
createResponse(msgID) {
|
2017-02-09 21:29:25 +03:00
|
|
|
if (typeof msgID != "number") {
|
|
|
|
msgID = -1;
|
|
|
|
}
|
2022-06-06 10:10:43 +03:00
|
|
|
return new lazy.Response(msgID, this.send.bind(this));
|
2017-02-09 21:29:25 +03:00
|
|
|
}
|
|
|
|
|
2017-06-30 02:40:24 +03:00
|
|
|
sendError(err, cmdID) {
|
2022-06-06 10:10:43 +03:00
|
|
|
let resp = new lazy.Response(cmdID, this.send.bind(this));
|
2017-03-06 20:39:42 +03:00
|
|
|
resp.sendError(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* When a client connects we send across a JSON Object defining the
|
|
|
|
* protocol level.
|
|
|
|
*
|
|
|
|
* This is the only message sent by Marionette that does not follow
|
|
|
|
* the regular message format.
|
|
|
|
*/
|
2017-06-30 02:40:24 +03:00
|
|
|
sayHello() {
|
2017-03-06 20:39:42 +03:00
|
|
|
let whatHo = {
|
|
|
|
applicationType: "gecko",
|
|
|
|
marionetteProtocol: PROTOCOL_VERSION,
|
|
|
|
};
|
|
|
|
this.sendRaw(whatHo);
|
2017-06-30 02:40:24 +03:00
|
|
|
}
|
2017-03-06 20:39:42 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Delegates message to client based on the provided {@code cmdID}.
|
|
|
|
* The message is sent over the debugger transport socket.
|
|
|
|
*
|
|
|
|
* The command ID is a unique identifier assigned to the client's request
|
|
|
|
* that is used to distinguish the asynchronous responses.
|
|
|
|
*
|
|
|
|
* Whilst responses to commands are synchronous and must be sent in the
|
|
|
|
* correct order.
|
|
|
|
*
|
2017-07-26 15:11:53 +03:00
|
|
|
* @param {Message} msg
|
2017-03-06 20:39:42 +03:00
|
|
|
* The command or response to send.
|
|
|
|
*/
|
2017-06-30 02:40:24 +03:00
|
|
|
send(msg) {
|
2022-06-06 10:10:43 +03:00
|
|
|
msg.origin = lazy.Message.Origin.Server;
|
|
|
|
if (msg instanceof lazy.Response) {
|
2017-03-06 20:39:42 +03:00
|
|
|
this.sendToClient(msg);
|
2018-10-03 18:11:43 +03:00
|
|
|
} else {
|
2022-06-06 10:10:43 +03:00
|
|
|
lazy.logger.fatal("Cannot send messages other than Response");
|
2017-03-06 20:39:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Low-level methods:
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send given response to the client over the debugger transport socket.
|
|
|
|
*
|
|
|
|
* @param {Response} resp
|
|
|
|
* The response to send back to the client.
|
|
|
|
*/
|
2017-06-30 02:40:24 +03:00
|
|
|
sendToClient(resp) {
|
2017-03-06 20:39:42 +03:00
|
|
|
this.sendMessage(resp);
|
2017-06-30 02:40:24 +03:00
|
|
|
}
|
2017-03-06 20:39:42 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Marshal message to the Marionette message format and send it.
|
|
|
|
*
|
2017-07-26 15:11:53 +03:00
|
|
|
* @param {Message} msg
|
2017-03-06 20:39:42 +03:00
|
|
|
* The message to send.
|
|
|
|
*/
|
2017-06-30 02:40:24 +03:00
|
|
|
sendMessage(msg) {
|
2022-09-19 22:19:22 +03:00
|
|
|
this.#log(msg);
|
2017-09-30 20:10:39 +03:00
|
|
|
let payload = msg.toPacket();
|
2017-03-06 20:39:42 +03:00
|
|
|
this.sendRaw(payload);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send the given payload over the debugger transport socket to the
|
|
|
|
* connected client.
|
|
|
|
*
|
2017-07-26 15:11:53 +03:00
|
|
|
* @param {Object.<string, ?>} payload
|
2017-03-06 20:39:42 +03:00
|
|
|
* The payload to ship.
|
|
|
|
*/
|
2017-06-30 02:40:24 +03:00
|
|
|
sendRaw(payload) {
|
2017-03-06 20:39:42 +03:00
|
|
|
this.conn.send(payload);
|
|
|
|
}
|
|
|
|
|
2017-06-30 02:40:24 +03:00
|
|
|
toString() {
|
2018-04-23 12:19:52 +03:00
|
|
|
return `[object TCPConnection ${this.id}]`;
|
2017-03-06 20:09:07 +03:00
|
|
|
}
|
2018-04-23 12:19:52 +03:00
|
|
|
}
|