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
This commit is contained in:
Greg Tatum 2023-06-14 15:00:17 +00:00
Родитель abbfd0718a
Коммит cd6641f7ba
3 изменённых файлов: 17 добавлений и 15 удалений

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

@ -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) {

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

@ -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(

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

@ -489,9 +489,8 @@ let JSWINDOWACTORS = {
child: {
esModuleURI: "resource://gre/actors/TranslationsChild.sys.mjs",
events: {
pagehide: {},
pageshow: {},
DOMHeadElementParsed: {},
DOMDocElementInserted: {},
DOMContentLoaded: {},
},
},