Bug 1960319 - [remote] Use shared data to sync the WebDriver active status with content processes. r=webdriver-reviewers,jdescottes,nika

Differential Revision: https://phabricator.services.mozilla.com/D248432
This commit is contained in:
Henrik Skupin
2025-05-15 06:43:35 +00:00
committed by hskupin@mozilla.com
parent 2d73f43f43
commit e5b4d19014
2 changed files with 34 additions and 26 deletions

View File

@@ -21,6 +21,7 @@ ChromeUtils.defineLazyGetter(lazy, "logger", () =>
ChromeUtils.defineLazyGetter(lazy, "textEncoder", () => new TextEncoder()); ChromeUtils.defineLazyGetter(lazy, "textEncoder", () => new TextEncoder());
const NOTIFY_LISTENING = "marionette-listening"; const NOTIFY_LISTENING = "marionette-listening";
const SHARED_DATA_ACTIVE_KEY = "Marionette:Active";
// Complements -marionette flag for starting the Marionette server. // Complements -marionette flag for starting the Marionette server.
// We also set this if Marionette is running in order to start the server // We also set this if Marionette is running in order to start the server
@@ -56,8 +57,6 @@ class MarionetteParentProcess {
// Initially set the enabled state based on the environment variable. // Initially set the enabled state based on the environment variable.
this.enabled = Services.env.exists(ENV_ENABLED); this.enabled = Services.env.exists(ENV_ENABLED);
Services.ppmm.addMessageListener("Marionette:IsRunning", this);
this.#browserStartupFinished = lazy.Deferred(); this.#browserStartupFinished = lazy.Deferred();
} }
@@ -90,15 +89,14 @@ class MarionetteParentProcess {
return !!this.server && this.server.alive; return !!this.server && this.server.alive;
} }
receiveMessage({ name }) { /**
switch (name) { * Syncs the Marionette active flag with the web content processes.
case "Marionette:IsRunning": *
return this.running; * @param {boolean} value - Flag indicating if Marionette is active or not.
*/
default: updateWebdriverActiveFlag(value) {
lazy.logger.warn("Unknown IPC message to parent process: " + name); Services.ppmm.sharedData.set(SHARED_DATA_ACTIVE_KEY, value);
return null; Services.ppmm.sharedData.flush();
}
} }
handle(cmdLine) { handle(cmdLine) {
@@ -235,6 +233,8 @@ class MarionetteParentProcess {
return; return;
} }
this.updateWebdriverActiveFlag(true);
Services.env.set(ENV_ENABLED, "1"); Services.env.set(ENV_ENABLED, "1");
Services.obs.notifyObservers(this, NOTIFY_LISTENING, true); Services.obs.notifyObservers(this, NOTIFY_LISTENING, true);
lazy.logger.debug("Marionette is listening"); lazy.logger.debug("Marionette is listening");
@@ -258,8 +258,9 @@ class MarionetteParentProcess {
async uninit() { async uninit() {
if (this.running) { if (this.running) {
await this.server.stop(); await this.server.stop();
this.updateWebdriverActiveFlag(false);
Services.obs.notifyObservers(this, NOTIFY_LISTENING); Services.obs.notifyObservers(this, NOTIFY_LISTENING);
lazy.logger.debug("Marionette stopped listening");
try { try {
await IOUtils.remove(this._activePortPath); await IOUtils.remove(this._activePortPath);
@@ -268,6 +269,8 @@ class MarionetteParentProcess {
`Failed to remove ${this._activePortPath} (${e.message})` `Failed to remove ${this._activePortPath} (${e.message})`
); );
} }
lazy.logger.debug("Marionette stopped listening");
} }
} }
@@ -286,12 +289,7 @@ class MarionetteContentProcess {
} }
get running() { get running() {
let reply = Services.cpmm.sendSyncMessage("Marionette:IsRunning"); return Services.cpmm.sharedData.get(SHARED_DATA_ACTIVE_KEY) ?? false;
if (!reply.length) {
lazy.logger.warn("No reply from parent process");
return false;
}
return reply[0];
} }
get QueryInterface() { get QueryInterface() {

View File

@@ -36,6 +36,8 @@ const DEFAULT_PORT = 9222;
// their values when the application is restarted internally. // their values when the application is restarted internally.
const ENV_ALLOW_SYSTEM_ACCESS = "MOZ_REMOTE_ALLOW_SYSTEM_ACCESS"; const ENV_ALLOW_SYSTEM_ACCESS = "MOZ_REMOTE_ALLOW_SYSTEM_ACCESS";
const SHARED_DATA_ACTIVE_KEY = "RemoteAgent:Active";
const isRemote = const isRemote =
Services.appinfo.processType == Services.appinfo.PROCESS_TYPE_CONTENT; Services.appinfo.processType == Services.appinfo.PROCESS_TYPE_CONTENT;
@@ -69,8 +71,6 @@ class RemoteAgentParentProcess {
// Supported protocols // Supported protocols
this.#cdp = null; this.#cdp = null;
this.#webDriverBiDi = null; this.#webDriverBiDi = null;
Services.ppmm.addMessageListener("RemoteAgent:IsRunning", this);
} }
get allowHosts() { get allowHosts() {
@@ -168,6 +168,16 @@ class RemoteAgentParentProcess {
return this.#server; return this.#server;
} }
/**
* Syncs the WebDriver active flag with the web content processes.
*
* @param {boolean} value - Flag indicating if Remote Agent is active or not.
*/
updateWebdriverActiveFlag(value) {
Services.ppmm.sharedData.set(SHARED_DATA_ACTIVE_KEY, value);
Services.ppmm.sharedData.flush();
}
get webDriverBiDi() { get webDriverBiDi() {
return this.#webDriverBiDi; return this.#webDriverBiDi;
} }
@@ -326,6 +336,8 @@ class RemoteAgentParentProcess {
this.server.identity.add("http", this.#host, this.#port); this.server.identity.add("http", this.#host, this.#port);
} }
this.updateWebdriverActiveFlag(true);
Services.obs.notifyObservers(null, "remote-listening", true); Services.obs.notifyObservers(null, "remote-listening", true);
await Promise.all([this.#webDriverBiDi?.start(), this.#cdp?.start()]); await Promise.all([this.#webDriverBiDi?.start(), this.#cdp?.start()]);
@@ -407,6 +419,9 @@ class RemoteAgentParentProcess {
try { try {
await this.#server.stop(); await this.#server.stop();
this.#server = null; this.#server = null;
this.updateWebdriverActiveFlag(false);
Services.obs.notifyObservers(null, "remote-listening"); Services.obs.notifyObservers(null, "remote-listening");
} catch (e) { } catch (e) {
// this function must never fail // this function must never fail
@@ -554,12 +569,7 @@ class RemoteAgentParentProcess {
class RemoteAgentContentProcess { class RemoteAgentContentProcess {
get running() { get running() {
let reply = Services.cpmm.sendSyncMessage("RemoteAgent:IsRunning"); return Services.cpmm.sharedData.get(SHARED_DATA_ACTIVE_KEY) ?? false;
if (!reply.length) {
lazy.logger.warn("No reply from parent process");
return false;
}
return reply[0];
} }
get QueryInterface() { get QueryInterface() {