summaryrefslogtreecommitdiffstats
path: root/remote/sessions
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 14:29:10 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 14:29:10 +0000
commit2aa4a82499d4becd2284cdb482213d541b8804dd (patch)
treeb80bf8bf13c3766139fbacc530efd0dd9d54394c /remote/sessions
parentInitial commit. (diff)
downloadfirefox-2aa4a82499d4becd2284cdb482213d541b8804dd.tar.xz
firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.zip
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'remote/sessions')
-rw-r--r--remote/sessions/ContentProcessSession.jsm111
-rw-r--r--remote/sessions/MainProcessSession.jsm18
-rw-r--r--remote/sessions/Session.jsm85
-rw-r--r--remote/sessions/TabSession.jsm116
-rw-r--r--remote/sessions/frame-script.js12
5 files changed, 342 insertions, 0 deletions
diff --git a/remote/sessions/ContentProcessSession.jsm b/remote/sessions/ContentProcessSession.jsm
new file mode 100644
index 0000000000..1aaa822001
--- /dev/null
+++ b/remote/sessions/ContentProcessSession.jsm
@@ -0,0 +1,111 @@
+/* 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 = ["ContentProcessSession"];
+
+const { ContentProcessDomains } = ChromeUtils.import(
+ "chrome://remote/content/domains/ContentProcessDomains.jsm"
+);
+const { DomainCache } = ChromeUtils.import(
+ "chrome://remote/content/domains/DomainCache.jsm"
+);
+
+ChromeUtils.defineModuleGetter(
+ this,
+ "ContextObserver",
+ "chrome://remote/content/observers/ContextObserver.jsm"
+);
+
+class ContentProcessSession {
+ constructor(messageManager, browsingContext, content, docShell) {
+ this.messageManager = messageManager;
+ this.browsingContext = browsingContext;
+ this.content = content;
+ this.docShell = docShell;
+ // Most children or sibling classes are going to assume that docShell
+ // implements the following interface. So do the QI only once from here.
+ this.docShell.QueryInterface(Ci.nsIWebNavigation);
+
+ this.domains = new DomainCache(this, ContentProcessDomains);
+ this.messageManager.addMessageListener("remote:request", this);
+ this.messageManager.addMessageListener("remote:destroy", this);
+ }
+
+ destructor() {
+ this._contextObserver?.destructor();
+
+ this.messageManager.removeMessageListener("remote:request", this);
+ this.messageManager.removeMessageListener("remote:destroy", this);
+ this.domains.clear();
+ }
+
+ get contextObserver() {
+ if (!this._contextObserver) {
+ this._contextObserver = new ContextObserver(
+ this.docShell.chromeEventHandler
+ );
+ }
+ return this._contextObserver;
+ }
+
+ // Domain event listener
+
+ onEvent(eventName, params) {
+ this.messageManager.sendAsyncMessage("remote:event", {
+ browsingContextId: this.browsingContext.id,
+ event: {
+ eventName,
+ params,
+ },
+ });
+ }
+
+ // nsIMessageListener
+
+ async receiveMessage({ name, data }) {
+ const { browsingContextId } = data;
+
+ // We may have more than one tab loaded in the same process,
+ // and debug the two at the same time. We want to ensure not
+ // mixing up the requests made against two such tabs.
+ // Each tab is going to have its own frame script instance
+ // and two communication channels are going to be set up via
+ // the two message managers.
+ if (browsingContextId != this.browsingContext.id) {
+ return;
+ }
+
+ switch (name) {
+ case "remote:request":
+ try {
+ const { id, domain, command, params } = data.request;
+
+ const result = await this.domains.execute(domain, command, params);
+
+ this.messageManager.sendAsyncMessage("remote:result", {
+ browsingContextId,
+ id,
+ result,
+ });
+ } catch (e) {
+ this.messageManager.sendAsyncMessage("remote:error", {
+ browsingContextId,
+ id: data.request.id,
+ error: {
+ name: e.name || "exception",
+ message: e.message || String(e),
+ stack: e.stack,
+ },
+ });
+ }
+ break;
+
+ case "remote:destroy":
+ this.destructor();
+ break;
+ }
+ }
+}
diff --git a/remote/sessions/MainProcessSession.jsm b/remote/sessions/MainProcessSession.jsm
new file mode 100644
index 0000000000..826cb74061
--- /dev/null
+++ b/remote/sessions/MainProcessSession.jsm
@@ -0,0 +1,18 @@
+/* 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 = ["MainProcessSession"];
+
+const { Session } = ChromeUtils.import(
+ "chrome://remote/content/sessions/Session.jsm"
+);
+
+/**
+ * A session, dedicated to the main process target.
+ * For some reason, it doesn't need any specific code and can share the base Session class
+ * aside TabSession.
+ */
+class MainProcessSession extends Session {}
diff --git a/remote/sessions/Session.jsm b/remote/sessions/Session.jsm
new file mode 100644
index 0000000000..9c964ab99f
--- /dev/null
+++ b/remote/sessions/Session.jsm
@@ -0,0 +1,85 @@
+/* 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"];
+
+const { ParentProcessDomains } = ChromeUtils.import(
+ "chrome://remote/content/domains/ParentProcessDomains.jsm"
+);
+const { DomainCache } = ChromeUtils.import(
+ "chrome://remote/content/domains/DomainCache.jsm"
+);
+
+const { NetworkObserver } = ChromeUtils.import(
+ "chrome://remote/content/observers/NetworkObserver.jsm"
+);
+
+/**
+ * A session represents exactly one client WebSocket connection.
+ *
+ * Every new WebSocket connections is associated with one session that
+ * deals with dispatching incoming command requests to the right
+ * target, sending back responses, and propagating events originating
+ * from domains.
+ * 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.
+ */
+class Session {
+ /**
+ * @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. For now, this is only passed by `Target.attachToTarget()`.
+ */
+ constructor(connection, target, id) {
+ this.connection = connection;
+ this.target = target;
+ this.id = id;
+
+ this.domains = new DomainCache(this, ParentProcessDomains);
+ }
+
+ destructor() {
+ if (
+ this.networkObserver &&
+ this.networkObserver.isActive(this.target.browser)
+ ) {
+ this.networkObserver.dispose();
+ }
+ this.domains.clear();
+ }
+
+ execute(id, domain, command, params) {
+ return this.domains.execute(domain, command, params);
+ }
+
+ get networkObserver() {
+ if (!this._networkObserver) {
+ this._networkObserver = new NetworkObserver();
+ }
+ return this._networkObserver;
+ }
+
+ /**
+ * Domains event listener. Called when an event is fired
+ * by any Domain and has to be sent to the client.
+ */
+ onEvent(eventName, params) {
+ this.connection.onEvent(eventName, params, this.id);
+ }
+
+ toString() {
+ return `[object ${this.constructor.name} ${this.connection.id}]`;
+ }
+}
diff --git a/remote/sessions/TabSession.jsm b/remote/sessions/TabSession.jsm
new file mode 100644
index 0000000000..d5135c1a16
--- /dev/null
+++ b/remote/sessions/TabSession.jsm
@@ -0,0 +1,116 @@
+/* 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 = ["TabSession"];
+
+const { Session } = ChromeUtils.import(
+ "chrome://remote/content/sessions/Session.jsm"
+);
+
+/**
+ * A session to communicate with a given tab
+ */
+class TabSession extends Session {
+ /**
+ * @param Connection connection
+ * The connection used to communicate with the server.
+ * @param TabTarget target
+ * The tab 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.
+ * For now, this is only passed by `Target.attachToTarget()`.
+ * Otherwise it will be undefined when you are connecting directly to
+ * a given Tab. i.e. connect directly to the WebSocket URL provided by
+ * /json/list HTTP endpoint.
+ */
+ constructor(connection, target, id) {
+ super(connection, target, id);
+
+ // Request id => { resolve, reject }
+ this.requestPromises = new Map();
+
+ this.mm.addMessageListener("remote:event", this);
+ this.mm.addMessageListener("remote:result", this);
+ this.mm.addMessageListener("remote:error", this);
+
+ this.mm.loadFrameScript(
+ "chrome://remote/content/sessions/frame-script.js",
+ false
+ );
+ }
+
+ destructor() {
+ super.destructor();
+
+ this.requestPromises.clear();
+
+ this.mm.sendAsyncMessage("remote:destroy", {
+ browsingContextId: this.browsingContext.id,
+ });
+
+ this.mm.removeMessageListener("remote:event", this);
+ this.mm.removeMessageListener("remote:result", this);
+ this.mm.removeMessageListener("remote:error", this);
+ }
+
+ execute(id, domain, command, params) {
+ // Check if the domain and command is implemented in the parent
+ // and execute it there. Otherwise forward the command to the content process
+ // in order to try to execute it in the content process.
+ if (this.domains.domainSupportsMethod(domain, command)) {
+ return super.execute(id, domain, command, params);
+ }
+ return this.executeInChild(id, domain, command, params);
+ }
+
+ executeInChild(id, domain, command, params) {
+ return new Promise((resolve, reject) => {
+ // Save the promise's resolution and rejection handler in order to later
+ // resolve this promise once we receive the reply back from the content process.
+ this.requestPromises.set(id, { resolve, reject });
+
+ this.mm.sendAsyncMessage("remote:request", {
+ browsingContextId: this.browsingContext.id,
+ request: { id, domain, command, params },
+ });
+ });
+ }
+
+ get mm() {
+ return this.target.mm;
+ }
+
+ get browsingContext() {
+ return this.target.browsingContext;
+ }
+
+ // nsIMessageListener
+
+ receiveMessage({ name, data }) {
+ const { id, result, event, error } = data;
+
+ switch (name) {
+ case "remote:result":
+ const { resolve } = this.requestPromises.get(id);
+ resolve(result);
+ this.requestPromises.delete(id);
+ break;
+
+ case "remote:event":
+ this.connection.onEvent(event.eventName, event.params, this.id);
+ break;
+
+ case "remote:error":
+ const { reject } = this.requestPromises.get(id);
+ reject(error);
+ this.requestPromises.delete(id);
+ break;
+ }
+ }
+}
diff --git a/remote/sessions/frame-script.js b/remote/sessions/frame-script.js
new file mode 100644
index 0000000000..ef0c5047c5
--- /dev/null
+++ b/remote/sessions/frame-script.js
@@ -0,0 +1,12 @@
+/* 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";
+
+const { ContentProcessSession } = ChromeUtils.import(
+ "chrome://remote/content/sessions/ContentProcessSession.jsm"
+);
+
+/* global content, docShell */
+new ContentProcessSession(this, docShell.browsingContext, content, docShell);