2015-09-26 19:12:01 +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 = ["Command", "Message", "Response"];
|
|
|
|
|
2020-09-08 21:22:43 +03:00
|
|
|
const { XPCOMUtils } = ChromeUtils.import(
|
|
|
|
"resource://gre/modules/XPCOMUtils.jsm"
|
2019-01-17 21:18:31 +03:00
|
|
|
);
|
2015-09-26 19:12:01 +03:00
|
|
|
|
2020-09-08 21:22:43 +03:00
|
|
|
XPCOMUtils.defineLazyModuleGetters(this, {
|
2021-07-21 12:45:06 +03:00
|
|
|
assert: "chrome://remote/content/shared/webdriver/Assert.jsm",
|
2021-06-21 15:53:48 +03:00
|
|
|
error: "chrome://remote/content/shared/webdriver/Errors.jsm",
|
2021-06-07 13:08:24 +03:00
|
|
|
truncate: "chrome://remote/content/marionette/format.js",
|
2020-09-08 21:22:43 +03:00
|
|
|
});
|
|
|
|
|
2017-09-30 20:10:39 +03:00
|
|
|
/** Representation of the packets transproted over the wire. */
|
|
|
|
class Message {
|
|
|
|
/**
|
|
|
|
* @param {number} messageID
|
|
|
|
* Message ID unique identifying this message.
|
|
|
|
*/
|
|
|
|
constructor(messageID) {
|
|
|
|
this.id = assert.integer(messageID);
|
|
|
|
}
|
|
|
|
|
|
|
|
toString() {
|
2017-11-30 17:09:32 +03:00
|
|
|
let content = JSON.stringify(this.toPacket());
|
|
|
|
return truncate`${content}`;
|
2017-09-30 20:10:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts a data packet into a {@link Command} or {@link Response}.
|
|
|
|
*
|
|
|
|
* @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.
|
|
|
|
*
|
|
|
|
* @return {Message}
|
|
|
|
* Based on the message type, a {@link Command} or {@link Response}
|
|
|
|
* instance.
|
|
|
|
*
|
|
|
|
* @throws {TypeError}
|
|
|
|
* If the message type is not recognised.
|
|
|
|
*/
|
|
|
|
static fromPacket(data) {
|
|
|
|
const [type] = data;
|
|
|
|
|
|
|
|
switch (type) {
|
2017-09-30 20:28:32 +03:00
|
|
|
case Command.Type:
|
2017-09-30 20:10:39 +03:00
|
|
|
return Command.fromPacket(data);
|
|
|
|
|
2017-09-30 20:28:32 +03:00
|
|
|
case Response.Type:
|
2017-09-30 20:10:39 +03:00
|
|
|
return Response.fromPacket(data);
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw new TypeError(
|
|
|
|
"Unrecognised message type in packet: " + JSON.stringify(data)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-26 15:11:53 +03:00
|
|
|
/**
|
|
|
|
* Messages may originate from either the server or the client.
|
2017-09-30 20:13:23 +03:00
|
|
|
* Because the remote protocol is full duplex, both endpoints may be
|
|
|
|
* the origin of both commands and responses.
|
2017-07-26 15:11:53 +03:00
|
|
|
*
|
|
|
|
* @enum
|
|
|
|
* @see {@link Message}
|
|
|
|
*/
|
2017-09-30 20:13:23 +03:00
|
|
|
Message.Origin = {
|
2017-07-26 15:11:53 +03:00
|
|
|
/** Indicates that the message originates from the client. */
|
2015-09-26 19:12:01 +03:00
|
|
|
Client: 0,
|
2017-07-26 15:11:53 +03:00
|
|
|
/** Indicates that the message originates from the server. */
|
2015-09-26 19:12:01 +03:00
|
|
|
Server: 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A command is a request from the client to run a series of remote end
|
|
|
|
* steps and return a fitting response.
|
|
|
|
*
|
|
|
|
* The command can be synthesised from the message passed over the
|
2017-09-30 20:10:39 +03:00
|
|
|
* Marionette socket using the {@link fromPacket} function. The format of
|
2015-09-26 19:12:01 +03:00
|
|
|
* a message is:
|
|
|
|
*
|
2017-09-30 20:15:03 +03:00
|
|
|
* <pre>
|
|
|
|
* [<var>type</var>, <var>id</var>, <var>name</var>, <var>params</var>]
|
|
|
|
* </pre>
|
2015-09-26 19:12:01 +03:00
|
|
|
*
|
|
|
|
* where
|
|
|
|
*
|
2017-09-30 20:15:03 +03:00
|
|
|
* <dl>
|
|
|
|
* <dt><var>type</var> (integer)
|
|
|
|
* <dd>
|
|
|
|
* Must be zero (integer). Zero means that this message is
|
|
|
|
* a command.
|
2015-09-26 19:12:01 +03:00
|
|
|
*
|
2017-09-30 20:15:03 +03:00
|
|
|
* <dt><var>id</var> (integer)
|
|
|
|
* <dd>
|
|
|
|
* Integer used as a sequence number. The server replies with
|
|
|
|
* the same ID for the response.
|
2015-09-26 19:12:01 +03:00
|
|
|
*
|
2017-09-30 20:15:03 +03:00
|
|
|
* <dt><var>name</var> (string)
|
|
|
|
* <dd>
|
|
|
|
* String representing the command name with an associated set
|
|
|
|
* of remote end steps.
|
2015-09-26 19:12:01 +03:00
|
|
|
*
|
2017-09-30 20:15:03 +03:00
|
|
|
* <dt><var>params</var> (JSON Object or null)
|
|
|
|
* <dd>
|
2015-09-26 19:12:01 +03:00
|
|
|
* Object of command function arguments. The keys of this object
|
|
|
|
* must be strings, but the values can be arbitrary values.
|
2017-09-30 20:15:03 +03:00
|
|
|
* </dl>
|
2015-09-26 19:12:01 +03:00
|
|
|
*
|
2017-09-30 20:15:03 +03:00
|
|
|
* A command has an associated message <var>id</var> that prevents
|
|
|
|
* the dispatcher from sending responses in the wrong order.
|
2015-09-26 19:12:01 +03:00
|
|
|
*
|
|
|
|
* The command may also have optional error- and result handlers that
|
|
|
|
* are called when the client returns with a response. These are
|
2017-09-30 20:15:03 +03:00
|
|
|
* <code>function onerror({Object})</code>,
|
|
|
|
* <code>function onresult({Object})</code>, and
|
|
|
|
* <code>function onresult({Response})</code>:
|
2015-09-26 19:12:01 +03:00
|
|
|
*
|
2017-09-30 20:10:39 +03:00
|
|
|
* @param {number} messageID
|
2015-09-26 19:12:01 +03:00
|
|
|
* Message ID unique identifying this message.
|
|
|
|
* @param {string} name
|
|
|
|
* Command name.
|
2017-07-26 15:11:53 +03:00
|
|
|
* @param {Object.<string, ?>} params
|
2015-09-26 19:12:01 +03:00
|
|
|
* Command parameters.
|
|
|
|
*/
|
2017-09-30 20:10:39 +03:00
|
|
|
class Command extends Message {
|
|
|
|
constructor(messageID, name, params = {}) {
|
|
|
|
super(messageID);
|
|
|
|
|
2017-02-09 21:22:09 +03:00
|
|
|
this.name = assert.string(name);
|
|
|
|
this.parameters = assert.object(params);
|
2015-09-26 19:12:01 +03:00
|
|
|
|
|
|
|
this.onerror = null;
|
|
|
|
this.onresult = null;
|
|
|
|
|
2017-09-30 20:13:23 +03:00
|
|
|
this.origin = Message.Origin.Client;
|
2015-09-26 19:12:01 +03:00
|
|
|
this.sent = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calls the error- or result handler associated with this command.
|
|
|
|
* This function can be replaced with a custom response handler.
|
|
|
|
*
|
|
|
|
* @param {Response} resp
|
|
|
|
* The response to pass on to the result or error to the
|
2017-09-30 20:15:03 +03:00
|
|
|
* <code>onerror</code> or <code>onresult</code> handlers to.
|
2015-09-26 19:12:01 +03:00
|
|
|
*/
|
|
|
|
onresponse(resp) {
|
2017-02-09 21:22:09 +03:00
|
|
|
if (this.onerror && resp.error) {
|
2015-09-26 19:12:01 +03:00
|
|
|
this.onerror(resp.error);
|
2017-02-09 21:22:09 +03:00
|
|
|
} else if (this.onresult && resp.body) {
|
2015-09-26 19:12:01 +03:00
|
|
|
this.onresult(resp.body);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-30 20:10:39 +03:00
|
|
|
/**
|
|
|
|
* Encodes the command to a packet.
|
|
|
|
*
|
|
|
|
* @return {Array}
|
|
|
|
* Packet.
|
|
|
|
*/
|
|
|
|
toPacket() {
|
|
|
|
return [Command.Type, this.id, this.name, this.parameters];
|
2015-09-26 19:12:01 +03:00
|
|
|
}
|
|
|
|
|
2017-09-30 20:10:39 +03:00
|
|
|
/**
|
|
|
|
* Converts a data packet into {@link Command}.
|
|
|
|
*
|
|
|
|
* @param {Array.<number, number, ?, ?>} data
|
|
|
|
* A four element array where the elements, in sequence, signifies
|
|
|
|
* message type, message ID, command name, and parameters.
|
|
|
|
*
|
|
|
|
* @return {Command}
|
|
|
|
* Representation of packet.
|
|
|
|
*
|
|
|
|
* @throws {TypeError}
|
|
|
|
* If the message type is not recognised.
|
|
|
|
*/
|
|
|
|
static fromPacket(payload) {
|
|
|
|
let [type, msgID, name, params] = payload;
|
2017-09-30 20:28:32 +03:00
|
|
|
assert.that(n => n === Command.Type)(type);
|
2017-02-09 19:38:13 +03:00
|
|
|
|
|
|
|
// if parameters are given but null, treat them as undefined
|
|
|
|
if (params === null) {
|
|
|
|
params = undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Command(msgID, name, params);
|
2015-09-26 19:12:01 +03:00
|
|
|
}
|
2017-06-30 02:40:24 +03:00
|
|
|
}
|
2017-09-30 20:28:32 +03:00
|
|
|
Command.Type = 0;
|
2015-09-26 19:12:01 +03:00
|
|
|
|
2017-07-26 15:11:53 +03:00
|
|
|
/**
|
|
|
|
* @callback ResponseCallback
|
|
|
|
*
|
|
|
|
* @param {Response} resp
|
|
|
|
* Response to handle.
|
|
|
|
*/
|
|
|
|
|
2015-09-26 19:12:01 +03:00
|
|
|
/**
|
|
|
|
* Represents the response returned from the remote end after execution
|
|
|
|
* of its corresponding command.
|
|
|
|
*
|
|
|
|
* The response is a mutable object passed to each command for
|
|
|
|
* modification through the available setters. To send data in a response,
|
|
|
|
* you modify the body property on the response. The body property can
|
|
|
|
* also be replaced completely.
|
|
|
|
*
|
2017-09-30 20:15:03 +03:00
|
|
|
* The response is sent implicitly by
|
|
|
|
* {@link server.TCPConnection#execute when a command has finished
|
|
|
|
* executing, and any modifications made subsequent to that will have
|
|
|
|
* no effect.
|
2015-09-26 19:12:01 +03:00
|
|
|
*
|
2017-09-30 20:10:39 +03:00
|
|
|
* @param {number} messageID
|
|
|
|
* Message ID tied to the corresponding command request this is
|
|
|
|
* a response for.
|
2017-07-26 15:11:53 +03:00
|
|
|
* @param {ResponseHandler} respHandler
|
2015-09-26 19:12:01 +03:00
|
|
|
* Function callback called on sending the response.
|
|
|
|
*/
|
2017-09-30 20:10:39 +03:00
|
|
|
class Response extends Message {
|
|
|
|
constructor(messageID, respHandler = () => {}) {
|
|
|
|
super(messageID);
|
|
|
|
|
2017-02-09 21:22:09 +03:00
|
|
|
this.respHandler_ = assert.callable(respHandler);
|
2015-09-26 19:12:01 +03:00
|
|
|
|
|
|
|
this.error = null;
|
2018-05-15 16:17:41 +03:00
|
|
|
this.body = { value: null };
|
2015-09-26 19:12:01 +03:00
|
|
|
|
2017-09-30 20:13:23 +03:00
|
|
|
this.origin = Message.Origin.Server;
|
2015-09-26 19:12:01 +03:00
|
|
|
this.sent = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends response conditionally, given a predicate.
|
|
|
|
*
|
|
|
|
* @param {function(Response): boolean} predicate
|
|
|
|
* A predicate taking a Response object and returning a boolean.
|
|
|
|
*/
|
|
|
|
sendConditionally(predicate) {
|
|
|
|
if (predicate(this)) {
|
|
|
|
this.send();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-09-30 20:15:03 +03:00
|
|
|
* Sends response using the response handler provided on
|
|
|
|
* construction.
|
2015-09-26 19:12:01 +03:00
|
|
|
*
|
|
|
|
* @throws {RangeError}
|
|
|
|
* If the response has already been sent.
|
|
|
|
*/
|
|
|
|
send() {
|
|
|
|
if (this.sent) {
|
|
|
|
throw new RangeError("Response has already been sent: " + this);
|
|
|
|
}
|
|
|
|
this.respHandler_(this);
|
|
|
|
this.sent = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-09-30 20:15:03 +03:00
|
|
|
* Send error to client.
|
2015-09-26 19:12:01 +03:00
|
|
|
*
|
|
|
|
* Turns the response into an error response, clears any previously
|
|
|
|
* set body data, and sends it using the response handler provided
|
|
|
|
* on construction.
|
|
|
|
*
|
|
|
|
* @param {Error} err
|
|
|
|
* The Error instance to send.
|
|
|
|
*
|
|
|
|
* @throws {Error}
|
2017-09-30 20:15:03 +03:00
|
|
|
* If <var>err</var> is not a {@link WebDriverError}, the error
|
|
|
|
* is propagated, i.e. rethrown.
|
2015-09-26 19:12:01 +03:00
|
|
|
*/
|
|
|
|
sendError(err) {
|
2017-02-10 21:36:52 +03:00
|
|
|
this.error = error.wrap(err).toJSON();
|
2015-09-26 19:12:01 +03:00
|
|
|
this.body = null;
|
|
|
|
this.send();
|
|
|
|
|
2017-02-10 21:36:52 +03:00
|
|
|
// propagate errors which are implementation problems
|
|
|
|
if (!error.isWebDriverError(err)) {
|
2015-09-26 19:12:01 +03:00
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-30 20:10:39 +03:00
|
|
|
/**
|
|
|
|
* Encodes the response to a packet.
|
|
|
|
*
|
|
|
|
* @return {Array}
|
|
|
|
* Packet.
|
|
|
|
*/
|
|
|
|
toPacket() {
|
2017-09-30 20:28:32 +03:00
|
|
|
return [Response.Type, this.id, this.error, this.body];
|
2015-09-26 19:12:01 +03:00
|
|
|
}
|
|
|
|
|
2017-09-30 20:10:39 +03:00
|
|
|
/**
|
|
|
|
* Converts a data packet into {@link Response}.
|
|
|
|
*
|
|
|
|
* @param {Array.<number, number, ?, ?>} data
|
|
|
|
* A four element array where the elements, in sequence, signifies
|
|
|
|
* message type, message ID, error, and result.
|
|
|
|
*
|
|
|
|
* @return {Response}
|
|
|
|
* Representation of packet.
|
|
|
|
*
|
|
|
|
* @throws {TypeError}
|
|
|
|
* If the message type is not recognised.
|
|
|
|
*/
|
|
|
|
static fromPacket(payload) {
|
|
|
|
let [type, msgID, err, body] = payload;
|
2017-09-30 20:28:32 +03:00
|
|
|
assert.that(n => n === Response.Type)(type);
|
2017-02-09 21:22:09 +03:00
|
|
|
|
|
|
|
let resp = new Response(msgID);
|
|
|
|
resp.error = assert.string(err);
|
|
|
|
|
|
|
|
resp.body = body;
|
2015-09-26 19:12:01 +03:00
|
|
|
return resp;
|
|
|
|
}
|
2017-06-30 02:40:24 +03:00
|
|
|
}
|
2017-09-30 20:28:32 +03:00
|
|
|
Response.Type = 1;
|
2017-09-30 20:10:39 +03:00
|
|
|
|
|
|
|
this.Message = Message;
|
|
|
|
this.Command = Command;
|
|
|
|
this.Response = Response;
|