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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
|
/* 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/. */
/* eslint-env browser */
/* globals TE_addProfilerMarker, TE_getLogLevel, TE_log, TE_logError, TE_getLogLevel,
TE_destroyEngineProcess, TE_requestEnginePayload, TE_reportEngineStatus,
TE_resolveForceShutdown */
/**
* This file lives in the translation engine's process and is in charge of managing the
* lifecycle of the translations engines. This process is a singleton Web Content
* process that can be created and destroyed as needed.
*
* The goal of the code in this file is to be as unprivileged as possible, which should
* unlock Bug 1813789, which will make this file fully unprivileged.
*
* Each translation needs an engine for that specific translation pair. This engine is
* kept around as long as the CACHE_TIMEOUT_MS, after this if some keepAlive event does
* not happen, the engine is destroyed. An engine may be destroyed even when a page is
* still open and may need translations in the future. This is handled gracefully by
* creating new engines and MessagePorts on the fly.
*
* The engine communicates directly with the content page via a MessagePort. Each end
* of the port is transfered from the parent process to the content process, and this
* engine process. This port is transitory, and may be closed at any time. Only when a
* translation has been requested once (which is initiated by the parent process) can
* the content process re-request translation ports. This ensures a rogue content process
* only has the capabilities to perform tasks that the parent process has given it.
*
* The messaging flow can get a little convoluted to handle all of the correctness cases,
* but ideally communication passes through the message port as much as possible. There
* are many scenarios such as:
*
* - Translation pages becoming idle
* - Tab changing causing "pageshow" and "pagehide" visibility changes
* - Translation actor destruction (this can happen long after the page has been
* navigated away from, but is still alive in the
* page history)
* - Error states
* - Engine Process being graceful shut down (no engines left)
* - Engine Process being killed by the OS.
*
* The following is a diagram that attempts to illustrate the structure of the processes
* and the communication channels that exist between them.
*
* ┌─────────────────────────────────────────────────────────────┐
* │ PARENT PROCESS │
* │ │
* │ [TranslationsParent] ←────→ [TranslationsEngineParent] │
* │ ↑ ↑ │
* └──────────────────│────────────────────────────────────│─────┘
* │ JSWindowActor IPC calls │ JSWindowActor IPC calls
* │ │
* ┌──────────────────│────────┐ ┌─────│─────────────────────────────┐
* │ CONTENT PROCESS │ │ │ │ ENGINE PROCESS │
* │ │ │ │ ↓ │
* │ [french.html] │ │ │ [TranslationsEngineChild] │
* │ ↕ ↓ │ │ ↕ │
* │ [TranslationsChild] │ │ [translations-engine.html] │
* │ └──TranslationsDocument │ │ ├── "fr to en" engine │
* │ └──port1 « ═══════════ MessageChannel ════ » │ └── port2 │
* │ │ │ └── "de to en" engine (idle) │
* └───────────────────────────┘ └───────────────────────────────────┘
*/
// How long the cache remains alive between uses, in milliseconds. In automation the
// engine is manually created and destroyed to avoid timing issues.
const CACHE_TIMEOUT_MS = 15_000;
/**
* @typedef {import("./translations-document.sys.mjs").TranslationsDocument} TranslationsDocument
* @typedef {import("../translations.js").TranslationsEnginePayload} TranslationsEnginePayload
*/
/**
* The TranslationsEngine encapsulates the logic for translating messages. It can
* only be set up for a single language translation pair. In order to change languages
* a new engine should be constructed.
*
* The actual work for the translations happens in a worker. This class manages
* instantiating and messaging the worker.
*
* Keep unused engines around in the TranslationsEngine.#cachedEngine cache in case
* page navigation happens and we can re-use previous engines. The engines are very
* heavy-weight, so get rid of them after a timeout. Once all are destroyed the
* TranslationsEngineParent is notified that it can be destroyed.
*/
export class TranslationsEngine {
/**
* Maps a language pair key to a cached engine. Engines are kept around for a timeout
* before they are removed so that they can be re-used during navigation.
*
* @type {Map<string, Promise<TranslationsEngine>>}
*/
static #cachedEngines = new Map();
/** @type {TimeoutID | null} */
#keepAliveTimeout = null;
/** @type {Worker} */
#worker;
/**
* Multiple messages can be sent before a response is received. This ID is used to keep
* track of the messages. It is incremented on every use.
*/
#messageId = 0;
/**
* Returns a getter function that will create a translations engine on the first
* call, and then return the cached one. After a timeout when the engine hasn't
* been used, it is destroyed.
*
* @param {string} fromLanguage
* @param {string} toLanguage
* @param {number} innerWindowId
* @returns {Promise<TranslationsEngine>}
*/
static getOrCreate(fromLanguage, toLanguage, innerWindowId) {
const languagePairKey = getLanguagePairKey(fromLanguage, toLanguage);
let enginePromise = TranslationsEngine.#cachedEngines.get(languagePairKey);
if (enginePromise) {
return enginePromise;
}
TE_log(`Creating a new engine for "${fromLanguage}" to "${toLanguage}".`);
// A new engine needs to be created.
enginePromise = TranslationsEngine.create(
fromLanguage,
toLanguage,
innerWindowId
);
TranslationsEngine.#cachedEngines.set(languagePairKey, enginePromise);
enginePromise.catch(error => {
TE_logError(
`The engine failed to load for translating "${fromLanguage}" to "${toLanguage}". Removing it from the cache.`,
error
);
// Remove the engine if it fails to initialize.
TranslationsEngine.#removeEngineFromCache(languagePairKey);
});
return enginePromise;
}
/**
* Removes the engine, and if it's the last, call the process to destroy itself.
*
* @param {string} languagePairKey
* @param {boolean} force - On forced shutdowns, it's not necessary to notify the
* parent process.
*/
static #removeEngineFromCache(languagePairKey, force) {
TranslationsEngine.#cachedEngines.delete(languagePairKey);
if (TranslationsEngine.#cachedEngines.size === 0 && !force) {
TE_log("The last engine was removed, destroying this process.");
TE_destroyEngineProcess();
}
}
/**
* Create a TranslationsEngine and bypass the cache.
*
* @param {string} fromLanguage
* @param {string} toLanguage
* @param {number} innerWindowId
* @returns {Promise<TranslationsEngine>}
*/
static async create(fromLanguage, toLanguage, innerWindowId) {
const startTime = performance.now();
const engine = new TranslationsEngine(
fromLanguage,
toLanguage,
await TE_requestEnginePayload(fromLanguage, toLanguage)
);
await engine.isReady;
TE_addProfilerMarker({
startTime,
message: `Translations engine loaded for "${fromLanguage}" to "${toLanguage}"`,
innerWindowId,
});
return engine;
}
/**
* Signal to the engines that they are being forced to shutdown.
*/
static forceShutdown() {
return Promise.allSettled(
[...TranslationsEngine.#cachedEngines].map(
async ([langPair, enginePromise]) => {
TE_log(`Force shutdown of the engine "${langPair}"`);
const engine = await enginePromise;
engine.terminate(true /* force */);
}
)
);
}
/**
* Terminates the engine and its worker after a timeout.
*
* @param {boolean} force
*/
terminate = (force = false) => {
const message = `Terminating translations engine "${this.languagePairKey}".`;
TE_addProfilerMarker({ message });
TE_log(message);
this.#worker.terminate();
this.#worker = null;
if (this.#keepAliveTimeout) {
clearTimeout(this.#keepAliveTimeout);
}
for (const [innerWindowId, data] of ports) {
const { fromLanguage, toLanguage, port } = data;
if (
fromLanguage === this.fromLanguage &&
toLanguage === this.toLanguage
) {
// This port is still active but being closed.
ports.delete(innerWindowId);
port.postMessage({ type: "TranslationsPort:EngineTerminated" });
port.close();
}
}
TranslationsEngine.#removeEngineFromCache(this.languagePairKey, force);
};
/**
* The worker needs to be shutdown after some amount of time of not being used.
*/
keepAlive() {
if (this.#keepAliveTimeout) {
// Clear any previous timeout.
clearTimeout(this.#keepAliveTimeout);
}
// In automated tests, the engine is manually destroyed.
if (!Cu.isInAutomation) {
this.#keepAliveTimeout = setTimeout(this.terminate, CACHE_TIMEOUT_MS);
}
}
/**
* Construct and initialize the worker.
*
* @param {string} fromLanguage
* @param {string} toLanguage
* @param {TranslationsEnginePayload} enginePayload - If there is no engine payload
* then the engine will be mocked. This allows this class to be used in tests.
*/
constructor(fromLanguage, toLanguage, enginePayload) {
/** @type {string} */
this.fromLanguage = fromLanguage;
/** @type {string} */
this.toLanguage = toLanguage;
this.languagePairKey = getLanguagePairKey(fromLanguage, toLanguage);
this.#worker = new Worker(
"chrome://global/content/translations/translations-engine.worker.js"
);
/** @type {Promise<void>} */
this.isReady = new Promise((resolve, reject) => {
const onMessage = ({ data }) => {
TE_log("Received initialization message", data);
if (data.type === "initialization-success") {
resolve();
} else if (data.type === "initialization-error") {
reject(data.error);
}
this.#worker.removeEventListener("message", onMessage);
};
this.#worker.addEventListener("message", onMessage);
// Schedule the first timeout for keeping the engine alive.
this.keepAlive();
});
// Make sure the ArrayBuffers are transferred, not cloned.
// https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Transferable_objects
const transferables = [];
if (enginePayload) {
transferables.push(enginePayload.bergamotWasmArrayBuffer);
for (const files of enginePayload.languageModelFiles) {
for (const { buffer } of Object.values(files)) {
transferables.push(buffer);
}
}
}
this.#worker.postMessage(
{
type: "initialize",
fromLanguage,
toLanguage,
enginePayload,
messageId: this.#messageId++,
logLevel: TE_getLogLevel(),
},
transferables
);
}
/**
* The implementation for translation. Use translateText or translateHTML for the
* public API.
*
* @param {string} sourceText
* @param {boolean} isHTML
* @param {number} innerWindowId
* @returns {Promise<string[]>}
*/
translate(sourceText, isHTML, innerWindowId) {
this.keepAlive();
const messageId = this.#messageId++;
return new Promise((resolve, reject) => {
const onMessage = ({ data }) => {
if (
data.type === "translations-discarded" &&
data.innerWindowId === innerWindowId
) {
// The page was unloaded, and we no longer need to listen for a response.
this.#worker.removeEventListener("message", onMessage);
return;
}
if (data.messageId !== messageId) {
// Multiple translation requests can be sent before a response is received.
// Ensure that the response received here is the correct one.
return;
}
if (data.type === "translation-response") {
// Also keep the translation alive after getting a result, as many translations
// can queue up at once, and then it can take minutes to resolve them all.
this.keepAlive();
resolve(data.targetText);
}
if (data.type === "translation-error") {
reject(data.error);
}
this.#worker.removeEventListener("message", onMessage);
};
this.#worker.addEventListener("message", onMessage);
this.#worker.postMessage({
type: "translation-request",
isHTML,
sourceText,
messageId,
innerWindowId,
});
});
}
/**
* Applies a function only if a cached engine exists.
*
* @param {string} fromLanguage
* @param {string} toLanguage
* @param {(engine: TranslationsEngine) => void} fn
*/
static withCachedEngine(fromLanguage, toLanguage, fn) {
const engine = TranslationsEngine.#cachedEngines.get(
getLanguagePairKey(fromLanguage, toLanguage)
);
if (engine) {
engine.then(fn).catch(() => {});
}
}
/**
* Stop processing the translation queue. All in-progress messages will be discarded.
*
* @param {number} innerWindowId
*/
discardTranslationQueue(innerWindowId) {
this.#worker.postMessage({
type: "discard-translation-queue",
innerWindowId,
});
}
/**
* Pause or resume the translations from a cached engine.
*
* @param {boolean} pause
* @param {string} fromLanguage
* @param {string} toLanguage
* @param {number} innerWindowId
*/
static pause(pause, fromLanguage, toLanguage, innerWindowId) {
TranslationsEngine.withCachedEngine(fromLanguage, toLanguage, engine => {
engine.pause(pause, innerWindowId);
});
}
}
/**
* Creates a lookup key that is unique to each fromLanguage-toLanguage pair.
*
* @param {string} fromLanguage
* @param {string} toLanguage
* @returns {string}
*/
function getLanguagePairKey(fromLanguage, toLanguage) {
return `${fromLanguage},${toLanguage}`;
}
/**
* Maps the innerWindowId to the port.
*
* @type {Map<number, { fromLanguage: string, toLanguage: string, port: MessagePort }>}
*/
const ports = new Map();
/**
* Listen to the port to the content process for incoming messages, and pass
* them to the TranslationsEngine manager. The other end of the port is held
* in the content process by the TranslationsDocument.
*
* @param {string} fromLanguage
* @param {string} toLanguage
* @param {number} innerWindowId
* @param {MessagePort} port
*/
function listenForPortMessages(fromLanguage, toLanguage, innerWindowId, port) {
async function handleMessage({ data }) {
switch (data.type) {
case "TranslationsPort:GetEngineStatusRequest": {
// This message gets sent first before the translation queue is processed.
// The engine is most likely to fail on the initial invocation. Any failure
// past the first one is not reported to the UI.
TranslationsEngine.getOrCreate(
fromLanguage,
toLanguage,
innerWindowId
).then(
() => {
TE_log("The engine is ready for translations.", {
innerWindowId,
});
TE_reportEngineStatus(innerWindowId, "ready");
port.postMessage({
type: "TranslationsPort:GetEngineStatusResponse",
status: "ready",
});
},
() => {
TE_reportEngineStatus(innerWindowId, "error");
port.postMessage({
type: "TranslationsPort:GetEngineStatusResponse",
status: "error",
});
// After an error no more translation requests will be sent. Go ahead
// and close the port.
port.close();
ports.delete(innerWindowId);
}
);
break;
}
case "TranslationsPort:TranslationRequest": {
const { sourceText, isHTML, messageId } = data;
const engine = await TranslationsEngine.getOrCreate(
fromLanguage,
toLanguage,
innerWindowId
);
const targetText = await engine.translate(
sourceText,
isHTML,
innerWindowId
);
port.postMessage({
type: "TranslationsPort:TranslationResponse",
messageId,
targetText,
});
break;
}
case "TranslationsPort:DiscardTranslations": {
discardTranslations(innerWindowId);
break;
}
default:
TE_logError("Unknown translations port message: " + data.type);
break;
}
}
if (port.onmessage) {
TE_logError(
new Error("The MessagePort onmessage handler was already present.")
);
}
port.onmessage = event => {
handleMessage(event).catch(error => TE_logError(error));
};
}
/**
* Discards the queue and removes the port.
*
* @param {number} innerWindowId
*/
function discardTranslations(innerWindowId) {
TE_log("Discarding translations, innerWindowId:", innerWindowId);
const portData = ports.get(innerWindowId);
if (portData) {
const { port, fromLanguage, toLanguage } = portData;
port.close();
ports.delete(innerWindowId);
TranslationsEngine.withCachedEngine(fromLanguage, toLanguage, engine => {
engine.discardTranslationQueue(innerWindowId);
});
}
}
/**
* Listen for events coming from the TranslationsEngine actor.
*/
window.addEventListener("message", ({ data }) => {
switch (data.type) {
case "StartTranslation": {
const { fromLanguage, toLanguage, innerWindowId, port } = data;
TE_log("Starting translation", innerWindowId);
listenForPortMessages(fromLanguage, toLanguage, innerWindowId, port);
ports.set(innerWindowId, { port, fromLanguage, toLanguage });
break;
}
case "DiscardTranslations": {
const { innerWindowId } = data;
discardTranslations(innerWindowId);
break;
}
case "ForceShutdown": {
TranslationsEngine.forceShutdown().then(() => {
TE_resolveForceShutdown();
});
break;
}
default:
throw new Error("Unknown TranslationsEngineChromeToContent event.");
}
});
|