summaryrefslogtreecommitdiffstats
path: root/toolkit/components/translations/actors/AboutTranslationsParent.sys.mjs
diff options
context:
space:
mode:
Diffstat (limited to 'toolkit/components/translations/actors/AboutTranslationsParent.sys.mjs')
-rw-r--r--toolkit/components/translations/actors/AboutTranslationsParent.sys.mjs62
1 files changed, 62 insertions, 0 deletions
diff --git a/toolkit/components/translations/actors/AboutTranslationsParent.sys.mjs b/toolkit/components/translations/actors/AboutTranslationsParent.sys.mjs
new file mode 100644
index 0000000000..4680dbeef5
--- /dev/null
+++ b/toolkit/components/translations/actors/AboutTranslationsParent.sys.mjs
@@ -0,0 +1,62 @@
+/* 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/. */
+
+const lazy = {};
+ChromeUtils.defineESModuleGetters(lazy, {
+ TranslationsParent: "resource://gre/actors/TranslationsParent.sys.mjs",
+});
+
+/**
+ * This parent is blank because the Translations actor handles most of the features
+ * needed in AboutTranslations.
+ */
+export class AboutTranslationsParent extends JSWindowActorParent {
+ #isDestroyed = false;
+
+ didDestroy() {
+ this.#isDestroyed = true;
+ }
+
+ async receiveMessage({ name, data }) {
+ switch (name) {
+ case "AboutTranslations:GetTranslationsPort": {
+ const { fromLanguage, toLanguage } = data;
+ const engineProcess = await lazy.TranslationsParent.getEngineProcess();
+ if (this.#isDestroyed) {
+ return undefined;
+ }
+ const { port1, port2 } = new MessageChannel();
+ engineProcess.actor.startTranslation(
+ fromLanguage,
+ toLanguage,
+ port1,
+ this.browsingContext.top.embedderElement.innerWindowID
+ );
+
+ // At the time of writing, you can't return a port via the `sendQuery` API,
+ // so results can't just be returned. The `sendAsyncMessage` method must be
+ // invoked. Additionally, in the AboutTranslationsChild, the port must
+ // be transfered to the content page with `postMessage`.
+ this.sendAsyncMessage(
+ "AboutTranslations:SendTranslationsPort",
+ {
+ fromLanguage,
+ toLanguage,
+ port: port2,
+ },
+ [port2] // Mark the port as transerable.
+ );
+ return undefined;
+ }
+ case "AboutTranslations:GetSupportedLanguages": {
+ return lazy.TranslationsParent.getSupportedLanguages();
+ }
+ case "AboutTranslations:IsTranslationsEngineSupported": {
+ return lazy.TranslationsParent.getIsTranslationsEngineSupported();
+ }
+ default:
+ throw new Error("Unknown AboutTranslations message: " + name);
+ }
+ }
+}