Bug 1967809 - Add more isDestroyed checks to TranslationsEngine actors r=translations-reviewers,gregtatum

Adds a few more checks for isDestroyed in places where we might
send asynchronous messages within the TranslationsEngine actors.

Differential Revision: https://phabricator.services.mozilla.com/D250765
This commit is contained in:
Erik Nordin
2025-05-22 15:45:52 +00:00
committed by enordin@mozilla.com
parent b954773108
commit deffee7c59
3 changed files with 33 additions and 0 deletions

View File

@@ -16,6 +16,11 @@ ChromeUtils.defineESModuleGetters(lazy, {
"chrome://global/content/translations/translations-engine.sys.mjs", "chrome://global/content/translations/translations-engine.sys.mjs",
}); });
/**
* @typedef {import("../translations").LanguagePair} LanguagePair
* @typedef {import("../translations").TranslationsEnginePayload} TranslationsEnginePayload
*/
/** /**
* The engine child is responsible for exposing privileged code to the un-privileged * The engine child is responsible for exposing privileged code to the un-privileged
* space the engine runs in. * space the engine runs in.
@@ -136,6 +141,10 @@ export class TranslationsEngineChild extends JSProcessActorChild {
totalTranslatedWords, totalTranslatedWords,
totalCompletedRequests, totalCompletedRequests,
}) { }) {
if (this.#isDestroyed) {
return;
}
this.sendAsyncMessage("TranslationsEngine:ReportEnginePerformance", { this.sendAsyncMessage("TranslationsEngine:ReportEnginePerformance", {
sourceLanguage, sourceLanguage,
targetLanguage, targetLanguage,
@@ -147,8 +156,14 @@ export class TranslationsEngineChild extends JSProcessActorChild {
/** /**
* @param {LanguagePair} languagePair * @param {LanguagePair} languagePair
*
* @returns {Promise<TranslationsEnginePayload> | undefined}
*/ */
TE_requestEnginePayload(languagePair) { TE_requestEnginePayload(languagePair) {
if (this.#isDestroyed) {
return undefined;
}
return this.sendQuery("TranslationsEngine:RequestEnginePayload", { return this.sendQuery("TranslationsEngine:RequestEnginePayload", {
languagePair, languagePair,
}); });
@@ -159,6 +174,10 @@ export class TranslationsEngineChild extends JSProcessActorChild {
* @param {"ready" | "error"} status * @param {"ready" | "error"} status
*/ */
TE_reportEngineStatus(innerWindowId, status) { TE_reportEngineStatus(innerWindowId, status) {
if (this.#isDestroyed) {
return;
}
this.sendAsyncMessage("TranslationsEngine:ReportEngineStatus", { this.sendAsyncMessage("TranslationsEngine:ReportEngineStatus", {
innerWindowId, innerWindowId,
status, status,
@@ -169,6 +188,10 @@ export class TranslationsEngineChild extends JSProcessActorChild {
* No engines are still alive, signal that the process can be destroyed. * No engines are still alive, signal that the process can be destroyed.
*/ */
TE_destroyEngineProcess() { TE_destroyEngineProcess() {
if (this.#isDestroyed) {
return;
}
this.sendAsyncMessage("TranslationsEngine:DestroyEngineProcess"); this.sendAsyncMessage("TranslationsEngine:DestroyEngineProcess");
} }

View File

@@ -11,6 +11,7 @@ ChromeUtils.defineESModuleGetters(lazy, {
/** /**
* @typedef {import("../translations").LanguagePair} LanguagePair * @typedef {import("../translations").LanguagePair} LanguagePair
* @typedef {import("../translations").TranslationsEnginePayload} TranslationsEnginePayload
*/ */
/** /**
@@ -45,11 +46,15 @@ export class TranslationsEngineParent extends JSProcessActorParent {
switch (name) { switch (name) {
case "TranslationsEngine:RequestEnginePayload": { case "TranslationsEngine:RequestEnginePayload": {
const { languagePair } = data; const { languagePair } = data;
/** @type {Promise<TranslationsEnginePayload>} */
const payloadPromise = const payloadPromise =
lazy.TranslationsParent.getTranslationsEnginePayload(languagePair); lazy.TranslationsParent.getTranslationsEnginePayload(languagePair);
payloadPromise.catch(error => { payloadPromise.catch(error => {
lazy.TranslationsParent.telemetry().onError(String(error)); lazy.TranslationsParent.telemetry().onError(String(error));
}); });
return payloadPromise; return payloadPromise;
} }
case "TranslationsEngine:ReportEnginePerformance": { case "TranslationsEngine:ReportEnginePerformance": {

View File

@@ -210,6 +210,7 @@ const VERIFY_SIGNATURES_FROM_FS = false;
* @typedef {import("../translations").TranslationModelRecord} TranslationModelRecord * @typedef {import("../translations").TranslationModelRecord} TranslationModelRecord
* @typedef {import("../translations").RemoteSettingsClient} RemoteSettingsClient * @typedef {import("../translations").RemoteSettingsClient} RemoteSettingsClient
* @typedef {import("../translations").TranslationModelPayload} TranslationModelPayload * @typedef {import("../translations").TranslationModelPayload} TranslationModelPayload
* @typedef {import("../translations").TranslationsEnginePayload} TranslationsEnginePayload
* @typedef {import("../translations").LanguageTranslationModelFiles} LanguageTranslationModelFiles * @typedef {import("../translations").LanguageTranslationModelFiles} LanguageTranslationModelFiles
* @typedef {import("../translations").WasmRecord} WasmRecord * @typedef {import("../translations").WasmRecord} WasmRecord
* @typedef {import("../translations").LangTags} LangTags * @typedef {import("../translations").LangTags} LangTags
@@ -1587,7 +1588,11 @@ export class TranslationsParent extends JSWindowActorParent {
} }
/** /**
* Retrieves the payload required to construct the TranslationsEngine for the given language pair.
*
* @param {LanguagePair} languagePair * @param {LanguagePair} languagePair
*
* @returns {Promise<TranslationsEnginePayload>}
*/ */
static async getTranslationsEnginePayload(languagePair) { static async getTranslationsEnginePayload(languagePair) {
const wasmStartTime = Cu.now(); const wasmStartTime = Cu.now();