summaryrefslogtreecommitdiffstats
path: root/toolkit/components/translations/actors/AboutTranslationsParent.sys.mjs
blob: 4680dbeef535081875a372f3d0ff497af19a4d20 (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
/* 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);
    }
  }
}