Skip to content

Commit

Permalink
Bug 1820229 - Detect if the translations engine is supported; r=nordz…
Browse files Browse the repository at this point in the history
  • Loading branch information
gregtatum committed Mar 24, 2023
1 parent 944d80a commit 012e5e2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 13 deletions.
41 changes: 34 additions & 7 deletions toolkit/components/translations/actors/TranslationsChild.sys.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -393,11 +393,21 @@ export class TranslationsChild extends JSWindowActorChild {
*/
#bergamotWasmArrayBuffer = null;

/**
* @override https://firefox-source-docs.mozilla.org/dom/ipc/jsactors.html#actorcreated
*/
actorCreated() {
this.#isTranslationsEngineMocked = this.sendQuery(
"Translations:GetIsTranslationsEngineMocked"
);
}

/**
* The translations engine could be mocked for tests, since the wasm and the language
* models must be downloaded from Remote Settings.
* @type {undefined | Promise<boolean>}
*/
#isTranslationsEngineMocked = false;
#isTranslationsEngineMocked;

/**
* The getter for the TranslationsEngine, managed by the EngineCache.
Expand Down Expand Up @@ -435,7 +445,7 @@ export class TranslationsChild extends JSWindowActorChild {
* @returns {Promise<ArrayBuffer>}
*/
async #getBergamotWasmArrayBuffer() {
if (this.#isTranslationsEngineMocked) {
if (await this.#isTranslationsEngineMocked) {
throw new Error(
"The engine is mocked, the Bergamot wasm is not available."
);
Expand Down Expand Up @@ -474,7 +484,7 @@ export class TranslationsChild extends JSWindowActorChild {
* @returns {Promise<LanguageTranslationModelFiles[]>}
*/
async #getLanguageTranslationModelFiles(fromLanguage, toLanguage) {
if (this.#isTranslationsEngineMocked) {
if (await this.#isTranslationsEngineMocked) {
throw new Error(
"The engine is mocked, there are no language model files available."
);
Expand Down Expand Up @@ -601,6 +611,14 @@ export class TranslationsChild extends JSWindowActorChild {
*/
async maybeOfferTranslation() {
const translationsStart = this.docShell.now();

if (!(await this.isTranslationsEngineSupported())) {
lazy.console.log(
"The translations engine is not supported on this device."
);
return;
}

const langTags = await this.getLangTagsForTranslation();

this.#langTags = langTags;
Expand All @@ -611,6 +629,15 @@ export class TranslationsChild extends JSWindowActorChild {
}
}

async isTranslationsEngineSupported() {
if (await this.#isTranslationsEngineMocked) {
// A mocked engine is always supported.
return true;
}
// Bergamot requires intgemm support.
return Boolean(WebAssembly.mozIntGemm);
}

/**
* Load the translation engine and translate the page.
*
Expand Down Expand Up @@ -713,9 +740,6 @@ export class TranslationsChild extends JSWindowActorChild {
*/
receiveMessage(message) {
switch (message.name) {
case "Translations:IsMocked":
this.#isTranslationsEngineMocked = message.data;
break;
case "Translations:TranslatePage":
if (!this.#langTags) {
lazy.console.warn(
Expand Down Expand Up @@ -782,7 +806,10 @@ export class TranslationsChild extends JSWindowActorChild {
* @returns {null | TranslationsEnginePayload}
*/
async #getTranslationsEnginePayload(fromLanguage, toLanguage) {
if (this.#isTranslationsEngineMocked) {
if (!this.#isTranslationsEngineMocked) {
throw new Error("Expected #isTranslationsEngineMocked to be a promise.");
}
if (await this.#isTranslationsEngineMocked) {
return null;
}
const [bergamotWasmArrayBuffer, languageModelFiles] = await Promise.all([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,6 @@ export class TranslationsParent extends JSWindowActorParent {
*/
static #mockedLanguageIdConfidence = null;

actorCreated() {
if (TranslationsParent.#mockedLanguagePairs) {
this.sendAsyncMessage("Translations:IsMocked", true);
}
}

async receiveMessage({ name, data }) {
switch (name) {
case "Translations:GetBergamotWasmArrayBuffer": {
Expand All @@ -137,6 +131,9 @@ export class TranslationsParent extends JSWindowActorParent {
case "Translations:GetLanguageIdEngineMockedPayload": {
return this.#getLanguageIdEngineMockedPayload();
}
case "Translations:GetIsTranslationsEngineMocked": {
return Boolean(TranslationsParent.#mockedLanguagePairs);
}
case "Translations:GetLanguageTranslationModelFiles": {
const { fromLanguage, toLanguage } = data;
const files = await this.getLanguageTranslationModelFiles(
Expand Down

0 comments on commit 012e5e2

Please sign in to comment.