summaryrefslogtreecommitdiffstats
path: root/toolkit/components/translations/actors/AboutTranslationsChild.sys.mjs
blob: 112ac3c444d6612a0f4ec1f6e7b0aa4d38ce0fa0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs";

const lazy = {};

XPCOMUtils.defineLazyGetter(lazy, "console", () => {
  return console.createInstance({
    maxLogLevelPref: "browser.translations.logLevel",
    prefix: "Translations",
  });
});

/**
 * @typedef {import("./TranslationsChild.sys.mjs").LanguageIdEngine} LanguageIdEngine
 * @typedef {import("./TranslationsChild.sys.mjs").TranslationsEngine} TranslationsEngine
 * @typedef {import("./TranslationsChild.sys.mjs").SupportedLanguages} SupportedLanguages
 */

/**
 * The AboutTranslationsChild is responsible for coordinating what privileged APIs
 * are exposed to the un-privileged scope of the about:translations page.
 */
export class AboutTranslationsChild extends JSWindowActorChild {
  /** @type {LanguageIdEngine | null} */
  languageIdEngine = null;

  /** @type {TranslationsEngine | null} */
  translationsEngine = null;

  /**
   * The translations engine uses text translations by default in about:translations,
   * but it can be changed to translate HTML by setting this pref to true. This is
   * useful for manually testing HTML translation behavior, but is not useful to surface
   * as a user-facing feature.
   *
   * @type {bool}
   */
  #isHtmlTranslation = Services.prefs.getBoolPref(
    "browser.translations.useHTML"
  );

  handleEvent(event) {
    if (event.type === "DOMDocElementInserted") {
      this.#exportFunctions();
    }

    if (
      event.type === "DOMContentLoaded" &&
      Services.prefs.getBoolPref("browser.translations.enable")
    ) {
      this.#sendEventToContent({ type: "enable" });
    }
  }

  /**
   * @param {object} detail
   */
  #sendEventToContent(detail) {
    this.contentWindow.dispatchEvent(
      new this.contentWindow.CustomEvent("AboutTranslationsChromeToContent", {
        detail: Cu.cloneInto(detail, this.contentWindow),
      })
    );
  }

  /**
   * @returns {TranslationsChild}
   */
  #getTranslationsChild() {
    const child = this.contentWindow.windowGlobalChild.getActor("Translations");
    if (!child) {
      throw new Error("Unable to find the TranslationsChild");
    }
    return child;
  }

  /**
   * A privileged promise can't be used in the content page, so convert a privileged
   * promise into a content one.
   *
   * @param {Promise<any>} promise
   * @returns {Promise<any>}
   */
  #convertToContentPromise(promise) {
    return new this.contentWindow.Promise((resolve, reject) =>
      promise.then(resolve, error => {
        let contentWindow;
        try {
          contentWindow = this.contentWindow;
        } catch (error) {
          // The content window is no longer available.
          reject();
          return;
        }
        // Create an error in the content window, if the content window is still around.
        let message = "An error occured in the AboutTranslations actor.";
        if (typeof error === "string") {
          message = error;
        }
        if (typeof error?.message === "string") {
          message = error.message;
        }
        if (typeof error?.stack === "string") {
          message += `\n\nOriginal stack:\n\n${error.stack}\n`;
        }

        reject(new contentWindow.Error(message));
      })
    );
  }

  /**
   * Export any of the child functions that start with "AT_" to the unprivileged content
   * page. This restricts the security capabilities of the the content page.
   */
  #exportFunctions() {
    const window = this.contentWindow;

    const fns = [
      "AT_log",
      "AT_logError",
      "AT_getAppLocale",
      "AT_getSupportedLanguages",
      "AT_isTranslationEngineSupported",
      "AT_createLanguageIdEngine",
      "AT_createTranslationsEngine",
      "AT_identifyLanguage",
      "AT_translate",
      "AT_destroyTranslationsEngine",
      "AT_getScriptDirection",
    ];
    for (const name of fns) {
      Cu.exportFunction(this[name].bind(this), window, { defineAs: name });
    }
  }

  /**
   * Log messages if "browser.translations.logLevel" is set to "All".
   *
   * @param {...any} args
   */
  AT_log(...args) {
    lazy.console.log(...args);
  }

  /**
   * Report an error to the console.
   *
   * @param {...any} args
   */
  AT_logError(...args) {
    lazy.console.error(...args);
  }

  /**
   * Returns the app's locale.
   *
   * @returns {Intl.Locale}
   */
  AT_getAppLocale() {
    return Services.locale.appLocaleAsBCP47;
  }

  /**
   * Wire this function to the TranslationsChild.
   *
   * @returns {Promise<SupportedLanguages>}
   */
  AT_getSupportedLanguages() {
    return this.#convertToContentPromise(
      this.#getTranslationsChild()
        .getSupportedLanguages()
        .then(data => Cu.cloneInto(data, this.contentWindow))
    );
  }

  /**
   * Does this device support the translation engine?
   * @returns {Promise<boolean>}
   */
  AT_isTranslationEngineSupported() {
    return this.#convertToContentPromise(
      this.#getTranslationsChild().isTranslationsEngineSupported
    );
  }

  /**
   * Creates the LanguageIdEngine which attempts to identify in which
   * human language a string is written.
   *
   * Unlike TranslationsEngine, which handles only a single language pair
   * and must be rebuilt to handle a new language pair, the LanguageIdEngine
   * is a one-to-many engine that can recognize all of its supported languages.
   *
   * Subsequent calls to this function after the engine is initialized will do nothing
   * instead of rebuilding the engine.
   *
   * @returns {Promise<void>}
   */
  AT_createLanguageIdEngine() {
    if (this.languageIdEngine) {
      return this.#convertToContentPromise(Promise.resolve());
    }
    return this.#convertToContentPromise(
      this.#getTranslationsChild()
        .createLanguageIdEngine()
        .then(engine => {
          this.languageIdEngine = engine;
        })
    );
  }

  /**
   * Creates the TranslationsEngine which is responsible for translating
   * from one language to the other.
   *
   * The instantiated TranslationsEngine is unique to its language pair.
   * In order to translate a different language pair, a new engine must be
   * created for that pair.
   *
   * Subsequent calls to this function will destroy the existing engine and
   * rebuild a new engine for the new language pair.
   *
   * @param {string} fromLanguage
   * @param {string} toLanguage
   * @returns {Promise<void>}
   */
  AT_createTranslationsEngine(fromLanguage, toLanguage) {
    if (this.translationsEngine) {
      this.translationsEngine.terminate();
      this.translationsEngine = null;
    }
    return this.#convertToContentPromise(
      this.#getTranslationsChild()
        .createTranslationsEngine(fromLanguage, toLanguage)
        .then(engine => {
          this.translationsEngine = engine;
        })
    );
  }

  /**
   * Attempts to identify the human language in which the message is written.
   * @see LanguageIdEngine#identifyLanguage for more detailed documentation.
   *
   * @param {string} message
   * @returns {Promise<{ langTag: string, confidence: number }>}
   */
  AT_identifyLanguage(message) {
    if (!this.languageIdEngine) {
      const { Promise, Error } = this.contentWindow;
      return Promise.reject(
        new Error("The language identification was not created.")
      );
    }

    return this.#convertToContentPromise(
      this.languageIdEngine
        .identifyLanguage(message)
        .then(data => Cu.cloneInto(data, this.contentWindow))
    );
  }

  /**
   * @param {string[]} messageBatch
   * @param {number} innerWindowId
   * @returns {Promise<string[]>}
   */
  AT_translate(messageBatch, innerWindowId) {
    if (!this.translationsEngine) {
      throw new this.contentWindow.Error(
        "The translations engine was not created."
      );
    }
    const promise = this.#isHtmlTranslation
      ? this.translationsEngine.translateHTML(messageBatch, innerWindowId)
      : this.translationsEngine.translateText(messageBatch, innerWindowId);

    return this.#convertToContentPromise(
      promise.then(translations =>
        Cu.cloneInto(translations, this.contentWindow)
      )
    );
  }

  /**
   * This is not strictly necessary, but could free up resources quicker.
   */
  AT_destroyTranslationsEngine() {
    if (this.translationsEngine) {
      this.translationsEngine.terminate();
      this.translationsEngine = null;
    }
  }

  /**
   * TODO - Remove this when Intl.Locale.prototype.textInfo is available to
   * content scripts.
   *
   * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale/textInfo
   * https://bugzilla.mozilla.org/show_bug.cgi?id=1693576
   *
   * @param {string} locale
   * @returns {string}
   */
  AT_getScriptDirection(locale) {
    return Services.intl.getScriptDirection(locale);
  }
}