Bug 1533682 - Implement sessions. r=ato

In order to be able to query/instantiate sub targets like remote frames, or tab targets from the MainProcessTarget,
we have to support session at the protocol layer.
This is all based on a `sessionId` attribute put on all inbound/outbound messages.

This patch will be later used, once we start instantiating sub targets.

Differential Revision: https://phabricator.services.mozilla.com/D22694

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Alexandre Poirot 2019-03-11 12:48:05 +00:00
Родитель 87c6809c86
Коммит 2e14b7808c
3 изменённых файлов: 43 добавлений и 5 удалений

Просмотреть файл

@ -18,9 +18,29 @@ class Connection {
this.transport = transport;
this.transport.hooks = this;
this.onmessage = () => {};
this.transport.ready();
this.defaultSession = null;
this.sessions = new Map();
}
/**
* Register a new Session to forward the messages to.
* Session without any `id` attribute will be considered to be the
* default one, to which messages without `sessionId` attribute are
* forwarded to. Only one such session can be registered.
*
* @param Session session
*/
registerSession(session) {
if (!session.id) {
if (this.defaultSession) {
throw new Error("Default session is already set on Connection," +
"can't register another one.");
}
this.defaultSession = session;
}
this.sessions.set(session.id, session);
}
send(message) {
@ -51,7 +71,19 @@ class Connection {
let message = {id: null};
try {
message = this.deserialize(packet);
this.onmessage.call(null, message);
const { sessionId } = packet;
if (!sessionId) {
if (!this.defaultSession) {
throw new Error(`Connection is missing a default Session.`);
}
this.defaultSession.onMessage(message);
} else {
const session = this.sessions.get(sessionId);
if (!session) {
throw new Error(`Session '${sessionId}' doesn't exists.`);
}
session.onMessage(message);
}
} catch (e) {
log.warn(e);
this.error(message.id, e);
@ -60,6 +92,7 @@ class Connection {
close() {
this.transport.close();
this.sessions.clear();
}
onClosed(status) {}

Просмотреть файл

@ -26,7 +26,7 @@ class Session {
this.destructor = this.destructor.bind(this);
this.connection.onmessage = this.onMessage.bind(this);
this.connection.registerSession(this);
this.connection.transport.on("close", this.destructor);
this.domains = new Domains(this, ParentProcessDomains);

Просмотреть файл

@ -5,7 +5,12 @@
const {Session} = ChromeUtils.import("chrome://remote/content/sessions/Session.jsm");
const connection = {onmessage: () => {}};
const connection = {
registerSession: () => {},
transport: {
on: () => {},
},
};
class MockTarget {
constructor() {