2019-01-29 18:18:42 +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 = ["Session"];
|
|
|
|
|
2019-02-22 02:04:13 +03:00
|
|
|
const {ParentProcessDomains} = ChromeUtils.import("chrome://remote/content/domains/ParentProcessDomains.jsm");
|
|
|
|
const {Domains} = ChromeUtils.import("chrome://remote/content/domains/Domains.jsm");
|
2019-05-27 17:51:56 +03:00
|
|
|
const {RemoteAgentError} = ChromeUtils.import("chrome://remote/content/Error.jsm");
|
2019-01-29 18:18:42 +03:00
|
|
|
|
2019-03-08 01:11:11 +03:00
|
|
|
/**
|
|
|
|
* A session represents exactly one client WebSocket connection.
|
|
|
|
*
|
|
|
|
* Every new WebSocket connections is associated with one session that
|
2019-04-04 14:29:31 +03:00
|
|
|
* deals with dispatching incoming command requests to the right
|
2019-03-08 01:11:11 +03:00
|
|
|
* target, sending back responses, and propagating events originating
|
|
|
|
* from domains.
|
2019-04-04 14:29:31 +03:00
|
|
|
* Then, some subsequent Sessions may be created over a single WebSocket
|
|
|
|
* connection. In this case, the subsequent session will have an `id`
|
|
|
|
* being passed to their constructor and each packet of these sessions
|
|
|
|
* will have a `sessionId` attribute in order to filter the packets
|
|
|
|
* by session both on client and server side.
|
2019-03-08 01:11:11 +03:00
|
|
|
*/
|
2019-02-12 16:58:23 +03:00
|
|
|
class Session {
|
2019-04-04 14:29:31 +03:00
|
|
|
/**
|
|
|
|
* @param Connection connection
|
|
|
|
* The connection used to communicate with the server.
|
|
|
|
* @param Target target
|
|
|
|
* The target to which this session communicates with.
|
|
|
|
* @param Number id (optional)
|
|
|
|
* If this session isn't the default one used for the HTTP endpoint we
|
|
|
|
* connected to, the session requires an id to distinguish it from the default
|
|
|
|
* one. This id is used to filter our request, responses and events between
|
|
|
|
* all active sessions.
|
|
|
|
*/
|
2019-03-10 15:51:09 +03:00
|
|
|
constructor(connection, target, id) {
|
2019-01-29 18:18:42 +03:00
|
|
|
this.connection = connection;
|
2019-02-23 19:12:03 +03:00
|
|
|
this.target = target;
|
2019-03-10 15:51:09 +03:00
|
|
|
this.id = id;
|
2019-01-29 18:18:42 +03:00
|
|
|
|
2019-03-10 15:51:09 +03:00
|
|
|
this.destructor = this.destructor.bind(this);
|
2019-02-22 02:04:13 +03:00
|
|
|
|
2019-03-11 15:48:05 +03:00
|
|
|
this.connection.registerSession(this);
|
2019-03-10 15:51:09 +03:00
|
|
|
this.connection.transport.on("close", this.destructor);
|
2019-02-23 18:47:10 +03:00
|
|
|
|
2019-03-10 15:51:09 +03:00
|
|
|
this.domains = new Domains(this, ParentProcessDomains);
|
2019-01-29 18:18:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
destructor() {
|
2019-02-23 19:12:03 +03:00
|
|
|
this.domains.clear();
|
2019-01-29 18:18:42 +03:00
|
|
|
}
|
|
|
|
|
2019-03-10 15:51:09 +03:00
|
|
|
async onMessage({id, method, params}) {
|
2019-01-29 18:18:42 +03:00
|
|
|
try {
|
|
|
|
if (typeof id == "undefined") {
|
|
|
|
throw new TypeError("Message missing 'id' field");
|
|
|
|
}
|
|
|
|
if (typeof method == "undefined") {
|
|
|
|
throw new TypeError("Message missing 'method' field");
|
|
|
|
}
|
|
|
|
|
2019-04-04 12:39:28 +03:00
|
|
|
const {domain, command} = Domains.splitMethod(method);
|
2019-04-04 14:35:24 +03:00
|
|
|
await this.execute(id, domain, command, params);
|
2019-01-29 18:18:42 +03:00
|
|
|
} catch (e) {
|
2019-03-10 15:51:09 +03:00
|
|
|
this.onError(id, e);
|
2019-01-29 18:18:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-04 14:35:24 +03:00
|
|
|
async execute(id, domain, command, params) {
|
2019-02-23 19:38:41 +03:00
|
|
|
const inst = this.domains.get(domain);
|
2019-04-04 14:35:24 +03:00
|
|
|
const result = await inst[command](params);
|
2019-03-10 15:51:09 +03:00
|
|
|
this.onResult(id, result);
|
2019-02-23 19:38:41 +03:00
|
|
|
}
|
|
|
|
|
2019-03-10 15:51:09 +03:00
|
|
|
onResult(id, result) {
|
|
|
|
this.connection.send({
|
|
|
|
id,
|
|
|
|
sessionId: this.id,
|
|
|
|
result,
|
2019-02-23 19:38:41 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-03-10 15:51:09 +03:00
|
|
|
onError(id, error) {
|
|
|
|
this.connection.send({
|
|
|
|
id,
|
|
|
|
sessionId: this.id,
|
|
|
|
error: {
|
2019-04-04 14:16:23 +03:00
|
|
|
message: RemoteAgentError.format(error, {stack: true}),
|
2019-03-10 15:51:09 +03:00
|
|
|
},
|
|
|
|
});
|
2019-02-23 19:12:03 +03:00
|
|
|
}
|
|
|
|
|
2019-02-23 21:59:31 +03:00
|
|
|
// Domain event listener
|
|
|
|
|
|
|
|
onEvent(eventName, params) {
|
|
|
|
this.connection.send({
|
2019-03-10 15:51:09 +03:00
|
|
|
sessionId: this.id,
|
2019-02-23 21:59:31 +03:00
|
|
|
method: eventName,
|
|
|
|
params,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
toString() {
|
2019-03-10 15:51:09 +03:00
|
|
|
return `[object ${this.constructor.name} ${this.connection.id}]`;
|
2019-02-22 02:04:13 +03:00
|
|
|
}
|
2019-02-12 17:44:54 +03:00
|
|
|
}
|