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
|
/* 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.defineLazyGetter(lazy, "console", () => {
return console.createInstance({
maxLogLevelPref: "browser.translations.logLevel",
prefix: "Translations",
});
});
/**
* The engine child is responsible for exposing privileged code to the un-privileged
* space the engine runs in.
*/
export class TranslationsEngineChild extends JSWindowActorChild {
/**
* The resolve function for the Promise returned by the
* "TranslationsEngine:ForceShutdown" message.
*
* @type {null | () => {}}
*/
#resolveForceShutdown = null;
actorCreated() {
this.#exportFunctions();
}
handleEvent(event) {
switch (event.type) {
case "DOMContentLoaded":
this.sendAsyncMessage("TranslationsEngine:Ready");
break;
}
}
// eslint-disable-next-line consistent-return
async receiveMessage({ name, data }) {
switch (name) {
case "TranslationsEngine:StartTranslation": {
const { fromLanguage, toLanguage, innerWindowId, port } = data;
const transferables = [port];
const message = {
type: "StartTranslation",
fromLanguage,
toLanguage,
innerWindowId,
port,
};
this.contentWindow.postMessage(message, "*", transferables);
break;
}
case "TranslationsEngine:DiscardTranslations": {
const { innerWindowId } = data;
this.contentWindow.postMessage({
type: "DiscardTranslations",
innerWindowId,
});
break;
}
case "TranslationsEngine:ForceShutdown": {
this.contentWindow.postMessage({
type: "ForceShutdown",
});
return new Promise(resolve => {
this.#resolveForceShutdown = resolve;
});
}
default:
console.error("Unknown message received", name);
}
}
/**
* Export any of the child functions that start with "TE_" to the unprivileged content
* page. This restricts the security capabilities of the content page.
*/
#exportFunctions() {
const fns = [
"TE_addProfilerMarker",
"TE_getLogLevel",
"TE_log",
"TE_logError",
"TE_requestEnginePayload",
"TE_reportEngineStatus",
"TE_resolveForceShutdown",
"TE_destroyEngineProcess",
];
for (const defineAs of fns) {
Cu.exportFunction(this[defineAs].bind(this), this.contentWindow, {
defineAs,
});
}
}
/**
* 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 TranslationsEngine 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));
})
);
}
/**
* @param {object} options
* @param {number?} options.startTime
* @param {string} options.message
* @param {number} options.innerWindowId
*/
TE_addProfilerMarker({ startTime, message, innerWindowId }) {
ChromeUtils.addProfilerMarker(
"TranslationsEngine",
{ startTime, innerWindowId },
message
);
}
/**
* Pass the message from content that the engines were shut down.
*/
TE_resolveForceShutdown() {
this.#resolveForceShutdown();
}
/**
* @returns {string}
*/
TE_getLogLevel() {
return Services.prefs.getCharPref("browser.translations.logLevel");
}
/**
* Log messages if "browser.translations.logLevel" is set to "All".
*
* @param {...any} args
*/
TE_log(...args) {
lazy.console.log(...args);
}
/**
* Report an error to the console.
*
* @param {...any} args
*/
TE_logError(...args) {
lazy.console.error(...args);
}
/**
* @param {string} fromLanguage
* @param {string} toLanguage
*/
TE_requestEnginePayload(fromLanguage, toLanguage) {
return this.#convertToContentPromise(
this.sendQuery("TranslationsEngine:RequestEnginePayload", {
fromLanguage,
toLanguage,
})
);
}
/**
* @param {number} innerWindowId
* @param {"ready" | "error"} status
*/
TE_reportEngineStatus(innerWindowId, status) {
this.sendAsyncMessage("TranslationsEngine:ReportEngineStatus", {
innerWindowId,
status,
});
}
/**
* No engines are still alive, signal that the process can be destroyed.
*/
TE_destroyEngineProcess() {
this.sendAsyncMessage("TranslationsEngine:DestroyEngineProcess");
}
}
|