From cd6641f7baebff255621ff60f7a38b740abfe634 Mon Sep 17 00:00:00 2001 From: Greg Tatum Date: Wed, 14 Jun 2023 15:00:17 +0000 Subject: [PATCH] Bug 1837078 - Do not get language id payload if the page is hidden; r=nordzilla This also removes some dead code around clearing the language tag, and properly lists the required events for the TranslationsChild. I guess that code was never even running, and I tests it manually after removing it. Differential Revision: https://phabricator.services.mozilla.com/D180885 --- .../actors/TranslationsChild.sys.mjs | 25 ++++++++++++------- .../actors/TranslationsParent.sys.mjs | 4 --- toolkit/modules/ActorManagerParent.sys.mjs | 3 +-- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/toolkit/components/translations/actors/TranslationsChild.sys.mjs b/toolkit/components/translations/actors/TranslationsChild.sys.mjs index cb676f98c52c..1f103105fff1 100644 --- a/toolkit/components/translations/actors/TranslationsChild.sys.mjs +++ b/toolkit/components/translations/actors/TranslationsChild.sys.mjs @@ -20,24 +20,25 @@ export class TranslationsChild extends JSWindowActorChild { */ innerWindowId = null; isDestroyed = false; - #areLangTagsReported = false; + #isPageHidden = false; #wasTranslationsEngineCreated = false; handleEvent(event) { switch (event.type) { case "DOMContentLoaded": - this.innerWindowId = this.contentWindow.windowGlobalChild.innerWindowId; + this.innerWindowId = + this.contentWindow?.windowGlobalChild.innerWindowId; if (!this.#isRestrictedPage()) { - this.#areLangTagsReported = true; this.sendAsyncMessage("Translations:ReportLangTags", { documentElementLang: this.document.documentElement.lang, }); } break; + case "pageshow": + this.#isPageHidden = false; + break; case "pagehide": - if (this.#areLangTagsReported) { - this.sendAsyncMessage("Translations:ClearLangTags"); - } + this.#isPageHidden = true; break; } } @@ -47,6 +48,9 @@ export class TranslationsChild extends JSWindowActorChild { * about:* pages will not be translated. */ #isRestrictedPage() { + if (!this.contentWindow?.location) { + return true; + } const { href } = this.contentWindow.location; // Keep this logic up to date with TranslationsParent.isRestrictedPage. return !( @@ -141,9 +145,12 @@ export class TranslationsChild extends JSWindowActorChild { } createLanguageIdEngine() { - return lazy.LanguageIdEngine.getOrCreate(() => - this.sendQuery("Translations:GetLanguageIdEnginePayload") - ); + return lazy.LanguageIdEngine.getOrCreate(() => { + if (this.#isPageHidden) { + throw new Error("The page was already hidden."); + } + return this.sendQuery("Translations:GetLanguageIdEnginePayload"); + }); } createTranslationsEngine(fromLanguage, toLanguage) { diff --git a/toolkit/components/translations/actors/TranslationsParent.sys.mjs b/toolkit/components/translations/actors/TranslationsParent.sys.mjs index b3dfa61e9bbb..ce84b2204cc4 100644 --- a/toolkit/components/translations/actors/TranslationsParent.sys.mjs +++ b/toolkit/components/translations/actors/TranslationsParent.sys.mjs @@ -456,10 +456,6 @@ export class TranslationsParent extends JSWindowActorParent { case "Translations:GetPreferredLanguages": { return TranslationsParent.getPreferredLanguages(); } - case "Translations:ClearLangTags": { - this.languageState.detectedLanguages = null; - return undefined; - } case "Translations:ReportLangTags": { const { documentElementLang, href } = data; const detectedLanguages = await this.getDetectedLanguages( diff --git a/toolkit/modules/ActorManagerParent.sys.mjs b/toolkit/modules/ActorManagerParent.sys.mjs index 7805aa1da361..7b9db7f281bd 100644 --- a/toolkit/modules/ActorManagerParent.sys.mjs +++ b/toolkit/modules/ActorManagerParent.sys.mjs @@ -489,9 +489,8 @@ let JSWINDOWACTORS = { child: { esModuleURI: "resource://gre/actors/TranslationsChild.sys.mjs", events: { + pagehide: {}, pageshow: {}, - DOMHeadElementParsed: {}, - DOMDocElementInserted: {}, DOMContentLoaded: {}, }, },