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/. */
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2020-09-08 21:19:32 +03:00
|
|
|
const EXPORTED_SYMBOLS = ["TCPConnection", "TCPListener"];
|
|
|
|
|
2020-09-08 21:22:43 +03:00
|
|
|
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
|
|
|
|
const { XPCOMUtils } = ChromeUtils.import(
|
|
|
|
"resource://gre/modules/XPCOMUtils.jsm"
|
|
|
|
);
|
|
|
|
|
|
|
|
XPCOMUtils.defineLazyModuleGetters(this, {
|
|
|
|
OS: "resource://gre/modules/osfile.jsm",
|
|
|
|
|
|
|
|
assert: "chrome://marionette/content/assert.js",
|
|
|
|
Command: "chrome://marionette/content/message.js",
|
|
|
|
DebuggerTransport: "chrome://marionette/content/transport.js",
|
|
|
|
error: "chrome://marionette/content/error.js",
|
|
|
|
GeckoDriver: "chrome://marionette/content/driver.js",
|
|
|
|
Log: "chrome://marionette/content/log.js",
|
|
|
|
MarionettePrefs: "chrome://marionette/content/prefs.js",
|
|
|
|
Message: "chrome://marionette/content/message.js",
|
|
|
|
Response: "chrome://marionette/content/message.js",
|
|
|
|
WebElement: "chrome://marionette/content/element.js",
|
|
|
|
});
|
|
|
|
|
2020-09-16 14:51:36 +03:00
|
|
|
XPCOMUtils.defineLazyGetter(this, "logger", () => Log.get());
|
2020-09-08 21:32:37 +03:00
|
|
|
XPCOMUtils.defineLazyGetter(this, "ServerSocket", () => {
|
|
|
|
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-07-26 15:11:53 +03:00
|
|
|
/** @namespace */
|
2017-03-06 20:09:07 +03:00
|
|
|
this.server = {};
|
2016-01-19 01:05:30 +03:00
|
|
|
|
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
|
|
|
*/
|
2018-04-23 12:19:52 +03:00
|
|
|
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() {
|
2018-04-23 09:26:09 +03:00
|
|
|
MarionettePrefs.contentListener = false;
|
2019-01-10 13:14:24 +03:00
|
|
|
return new 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;
|
|
|
|
this.socket = new ServerSocket(this.port, flags, backlog);
|
|
|
|
} 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);
|
2018-05-10 19:20:17 +03:00
|
|
|
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;
|
2018-05-10 19:20:17 +03:00
|
|
|
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;
|
2018-04-23 09:29:38 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-06-30 02:40:24 +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);
|
|
|
|
let transport = new DebuggerTransport(input, output);
|
2015-03-20 00:12:58 +03:00
|
|
|
|
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
|
|
|
|
2017-06-30 02:40:24 +03:00
|
|
|
logger.debug(
|
|
|
|
`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) {
|
2017-03-06 20:39:42 +03:00
|
|
|
logger.debug(`Closed connection ${conn.id}`);
|
|
|
|
this.conns.delete(conn);
|
|
|
|
}
|
2018-04-23 12:19:52 +03:00
|
|
|
}
|
|
|
|
this.TCPListener = TCPListener;
|
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
|
|
|
*/
|
2018-04-23 12:19:52 +03:00
|
|
|
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();
|
2017-12-06 00:28:59 +03:00
|
|
|
this.driver.init();
|
2017-03-06 20:39:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Debugger transport callback that cleans up
|
|
|
|
* after a connection is closed.
|
|
|
|
*/
|
2017-10-03 16:35:47 +03:00
|
|
|
onClosed() {
|
2017-03-06 20:39:42 +03:00
|
|
|
this.driver.deleteSession();
|
2017-12-06 00:28:59 +03:00
|
|
|
this.driver.uninit();
|
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)
|
|
|
|
);
|
|
|
|
error.report(e);
|
|
|
|
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 {
|
2017-09-30 20:10:39 +03:00
|
|
|
msg = Message.fromPacket(data);
|
2017-09-30 20:13:23 +03:00
|
|
|
msg.origin = Message.Origin.Client;
|
2017-02-09 21:29:25 +03:00
|
|
|
this.log_(msg);
|
|
|
|
} catch (e) {
|
|
|
|
let resp = this.createResponse(data[1]);
|
|
|
|
resp.sendError(e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// execute new command
|
2018-10-03 18:11:43 +03:00
|
|
|
if (msg instanceof Command) {
|
2017-08-07 21:02:20 +03:00
|
|
|
(async () => {
|
|
|
|
await this.execute(msg);
|
|
|
|
})();
|
2018-10-03 18:11:43 +03:00
|
|
|
} else {
|
|
|
|
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)
|
|
|
|
.catch(error.report);
|
|
|
|
}
|
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") {
|
2020-08-26 11:42:14 +03:00
|
|
|
throw new 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") {
|
|
|
|
assert.session(
|
|
|
|
this.driver,
|
|
|
|
"Tried to run command without establishing a connection"
|
|
|
|
);
|
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
|
|
|
|
2018-05-15 16:17:41 +03:00
|
|
|
if (rv != null) {
|
2017-11-24 19:20:21 +03:00
|
|
|
if (rv instanceof WebElement || typeof rv != "object") {
|
2017-08-07 21:02:20 +03:00
|
|
|
resp.body = { value: rv };
|
|
|
|
} else {
|
|
|
|
resp.body = rv;
|
|
|
|
}
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
return new Response(msgID, this.send.bind(this));
|
|
|
|
}
|
|
|
|
|
2017-06-30 02:40:24 +03:00
|
|
|
sendError(err, cmdID) {
|
2017-03-06 20:39:42 +03:00
|
|
|
let resp = new Response(cmdID, this.send.bind(this));
|
|
|
|
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) {
|
2017-09-30 20:13:23 +03:00
|
|
|
msg.origin = Message.Origin.Server;
|
2018-10-03 18:11:43 +03:00
|
|
|
if (msg instanceof Response) {
|
2017-03-06 20:39:42 +03:00
|
|
|
this.sendToClient(msg);
|
2018-10-03 18:11:43 +03:00
|
|
|
} else {
|
|
|
|
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) {
|
2017-03-06 20:39:42 +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
|
|
|
log_(msg) {
|
2017-09-30 20:13:23 +03:00
|
|
|
let dir = msg.origin == Message.Origin.Client ? "->" : "<-";
|
2018-11-29 22:31:38 +03:00
|
|
|
logger.debug(`${this.id} ${dir} ${msg}`);
|
2017-03-06 20:39:42 +03:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
this.TCPConnection = TCPConnection;
|