summaryrefslogtreecommitdiffstats
path: root/browser/components/translation/TranslationParent.jsm
blob: 8baf3b2779b820eb9d0b5b023f893d0009566c29 (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
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
/* 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/. */

"use strict";

var EXPORTED_SYMBOLS = [
  "Translation",
  "TranslationParent",
  "TranslationTelemetry",
];

const TRANSLATION_PREF_SHOWUI = "browser.translation.ui.show";
const TRANSLATION_PREF_DETECT_LANG = "browser.translation.detectLanguage";

var Translation = {
  STATE_OFFER: 0,
  STATE_TRANSLATING: 1,
  STATE_TRANSLATED: 2,
  STATE_ERROR: 3,
  STATE_UNAVAILABLE: 4,

  translationListener: null,

  serviceUnavailable: false,

  supportedSourceLanguages: [
    "bg",
    "cs",
    "de",
    "en",
    "es",
    "fr",
    "ja",
    "ko",
    "nl",
    "no",
    "pl",
    "pt",
    "ru",
    "tr",
    "vi",
    "zh",
  ],
  supportedTargetLanguages: [
    "bg",
    "cs",
    "de",
    "en",
    "es",
    "fr",
    "ja",
    "ko",
    "nl",
    "no",
    "pl",
    "pt",
    "ru",
    "tr",
    "vi",
    "zh",
  ],

  setListenerForTests(listener) {
    this.translationListener = listener;
  },

  _defaultTargetLanguage: "",
  get defaultTargetLanguage() {
    if (!this._defaultTargetLanguage) {
      this._defaultTargetLanguage =
        Services.locale.appLocaleAsBCP47.split("-")[0];
    }
    return this._defaultTargetLanguage;
  },

  openProviderAttribution() {
    let attribution = this.supportedEngines[this.translationEngine];
    const { BrowserWindowTracker } = ChromeUtils.import(
      "resource:///modules/BrowserWindowTracker.jsm"
    );
    BrowserWindowTracker.getTopWindow().openWebLinkIn(attribution, "tab");
  },

  /**
   * The list of translation engines and their attributions.
   */
  supportedEngines: {
    Google: "",
    Bing: "http://aka.ms/MicrosoftTranslatorAttribution",
    Yandex: "http://translate.yandex.com/",
  },

  /**
   * Fallback engine (currently Google) if the preferences seem confusing.
   */
  get defaultEngine() {
    return Object.keys(this.supportedEngines)[0];
  },

  /**
   * Returns the name of the preferred translation engine.
   */
  get translationEngine() {
    let engine = Services.prefs.getCharPref("browser.translation.engine");
    return !Object.keys(this.supportedEngines).includes(engine)
      ? this.defaultEngine
      : engine;
  },
};

/* Translation objects keep the information related to translation for
 * a specific browser.  The properties exposed to the infobar are:
 * - detectedLanguage, code of the language detected on the web page.
 * - state, the state in which the infobar should be displayed
 * - translatedFrom, if already translated, source language code.
 * - translatedTo, if already translated, target language code.
 * - translate, method starting the translation of the current page.
 * - showOriginalContent, method showing the original page content.
 * - showTranslatedContent, method showing the translation for an
 *   already translated page whose original content is shown.
 * - originalShown, boolean indicating if the original or translated
 *   version of the page is shown.
 */
class TranslationParent extends JSWindowActorParent {
  actorCreated() {
    this._state = 0;
    this.originalShown = true;
  }

  get browser() {
    return this.browsingContext.top.embedderElement;
  }

  receiveMessage(aMessage) {
    switch (aMessage.name) {
      case "Translation:DocumentState":
        this.documentStateReceived(aMessage.data);
        break;
    }
  }

  documentStateReceived(aData) {
    if (aData.state == Translation.STATE_OFFER) {
      if (aData.detectedLanguage == Translation.defaultTargetLanguage) {
        // Detected language is the same as the user's locale.
        return;
      }

      if (
        !Translation.supportedTargetLanguages.includes(aData.detectedLanguage)
      ) {
        // Detected language is not part of the supported languages.
        TranslationTelemetry.recordMissedTranslationOpportunity(
          aData.detectedLanguage
        );
        return;
      }

      TranslationTelemetry.recordTranslationOpportunity(aData.detectedLanguage);
    }

    if (!Services.prefs.getBoolPref(TRANSLATION_PREF_SHOWUI)) {
      return;
    }

    // Set all values before showing a new translation infobar.
    this._state = Translation.serviceUnavailable
      ? Translation.STATE_UNAVAILABLE
      : aData.state;
    this.detectedLanguage = aData.detectedLanguage;
    this.translatedFrom = aData.translatedFrom;
    this.translatedTo = aData.translatedTo;
    this.originalShown = aData.originalShown;

    this.showURLBarIcon();
  }

  translate(aFrom, aTo) {
    if (
      aFrom == aTo ||
      (this.state == Translation.STATE_TRANSLATED &&
        this.translatedFrom == aFrom &&
        this.translatedTo == aTo)
    ) {
      // Nothing to do.
      return;
    }

    if (this.state == Translation.STATE_OFFER) {
      if (this.detectedLanguage != aFrom) {
        TranslationTelemetry.recordDetectedLanguageChange(true);
      }
    } else {
      if (this.translatedFrom != aFrom) {
        TranslationTelemetry.recordDetectedLanguageChange(false);
      }
      if (this.translatedTo != aTo) {
        TranslationTelemetry.recordTargetLanguageChange();
      }
    }

    this.state = Translation.STATE_TRANSLATING;
    this.translatedFrom = aFrom;
    this.translatedTo = aTo;

    this.sendQuery("Translation:TranslateDocument", {
      from: aFrom,
      to: aTo,
    }).then(
      result => {
        this.translationFinished(result);
      },
      () => {}
    );
  }

  showURLBarIcon() {
    let chromeWin = this.browser.ownerGlobal;
    let PopupNotifications = chromeWin.PopupNotifications;
    let removeId = this.originalShown ? "translated" : "translate";
    let notification = PopupNotifications.getNotification(
      removeId,
      this.browser
    );
    if (notification) {
      PopupNotifications.remove(notification);
    }

    let callback = (aTopic, aNewBrowser) => {
      if (aTopic == "swapping") {
        return true;
      }

      if (aTopic != "showing") {
        return false;
      }
      let translationNotification =
        this.notificationBox.getNotificationWithValue("translation");
      if (translationNotification) {
        translationNotification.close();
      }
      return true;
    };

    let addId = this.originalShown ? "translate" : "translated";
    PopupNotifications.show(
      this.browser,
      addId,
      null,
      addId + "-notification-icon",
      null,
      null,
      { dismissed: true, eventCallback: callback }
    );
  }

  get state() {
    return this._state;
  }

  set state(val) {
    let notif = this.notificationBox.getNotificationWithValue("translation");
    if (notif) {
      notif.state = val;
    }
    this._state = val;
  }

  showOriginalContent() {
    this.originalShown = true;
    this.showURLBarIcon();
    this.sendAsyncMessage("Translation:ShowOriginal");
    TranslationTelemetry.recordShowOriginalContent();
  }

  showTranslatedContent() {
    this.originalShown = false;
    this.showURLBarIcon();
    this.sendAsyncMessage("Translation:ShowTranslation");
  }

  get notificationBox() {
    return this.browser.ownerGlobal.gBrowser.getNotificationBox(this.browser);
  }

  translationFinished(result) {
    if (result.success) {
      this.originalShown = false;
      this.state = Translation.STATE_TRANSLATED;
      this.showURLBarIcon();

      // Record the number of characters translated.
      TranslationTelemetry.recordTranslation(
        result.from,
        result.to,
        result.characterCount
      );
    } else if (result.unavailable) {
      Translation.serviceUnavailable = true;
      this.state = Translation.STATE_UNAVAILABLE;
    } else {
      this.state = Translation.STATE_ERROR;
    }

    if (Translation.translationListener) {
      Translation.translationListener();
    }
  }

  infobarClosed() {
    if (this.state == Translation.STATE_OFFER) {
      TranslationTelemetry.recordDeniedTranslationOffer();
    }
  }
}

/**
 * Uses telemetry histograms for collecting statistics on the usage of the
 * translation component.
 *
 * NOTE: Metrics are only recorded if the user enabled the telemetry option.
 */
var TranslationTelemetry = {
  init() {
    // Constructing histograms.
    const plain = id => Services.telemetry.getHistogramById(id);
    const keyed = id => Services.telemetry.getKeyedHistogramById(id);
    this.HISTOGRAMS = {
      OPPORTUNITIES: () => plain("TRANSLATION_OPPORTUNITIES"),
      OPPORTUNITIES_BY_LANG: () =>
        keyed("TRANSLATION_OPPORTUNITIES_BY_LANGUAGE"),
      PAGES: () => plain("TRANSLATED_PAGES"),
      PAGES_BY_LANG: () => keyed("TRANSLATED_PAGES_BY_LANGUAGE"),
      CHARACTERS: () => plain("TRANSLATED_CHARACTERS"),
      DENIED: () => plain("DENIED_TRANSLATION_OFFERS"),
      AUTO_REJECTED: () => plain("AUTO_REJECTED_TRANSLATION_OFFERS"),
      SHOW_ORIGINAL: () => plain("REQUESTS_OF_ORIGINAL_CONTENT"),
      TARGET_CHANGES: () => plain("CHANGES_OF_TARGET_LANGUAGE"),
      DETECTION_CHANGES: () => plain("CHANGES_OF_DETECTED_LANGUAGE"),
      SHOW_UI: () => plain("SHOULD_TRANSLATION_UI_APPEAR"),
      DETECT_LANG: () => plain("SHOULD_AUTO_DETECT_LANGUAGE"),
    };

    // Capturing the values of flags at the startup.
    this.recordPreferences();
  },

  /**
   * Record a translation opportunity in the health report.
   * @param language
   *        The language of the page.
   */
  recordTranslationOpportunity(language) {
    return this._recordOpportunity(language, true);
  },

  /**
   * Record a missed translation opportunity in the health report.
   * A missed opportunity is when the language detected is not part
   * of the supported languages.
   * @param language
   *        The language of the page.
   */
  recordMissedTranslationOpportunity(language) {
    return this._recordOpportunity(language, false);
  },

  /**
   * Record an automatically rejected translation offer in the health
   * report. A translation offer is automatically rejected when a user
   * has previously clicked "Never translate this language" or "Never
   * translate this site", which results in the infobar not being shown for
   * the translation opportunity.
   *
   * These translation opportunities should still be recorded in addition to
   * recording the automatic rejection of the offer.
   */
  recordAutoRejectedTranslationOffer() {
    this.HISTOGRAMS.AUTO_REJECTED().add();
  },

  /**
   * Record a translation in the health report.
   * @param langFrom
   *        The language of the page.
   * @param langTo
   *        The language translated to
   * @param numCharacters
   *        The number of characters that were translated
   */
  recordTranslation(langFrom, langTo, numCharacters) {
    this.HISTOGRAMS.PAGES().add();
    this.HISTOGRAMS.PAGES_BY_LANG().add(langFrom + " -> " + langTo);
    this.HISTOGRAMS.CHARACTERS().add(numCharacters);
  },

  /**
   * Record a change of the detected language in the health report. This should
   * only be called when actually executing a translation, not every time the
   * user changes in the language in the UI.
   *
   * @param beforeFirstTranslation
   *        A boolean indicating if we are recording a change of detected
   *        language before translating the page for the first time. If we
   *        have already translated the page from the detected language and
   *        the user has manually adjusted the detected language false should
   *        be passed.
   */
  recordDetectedLanguageChange(beforeFirstTranslation) {
    this.HISTOGRAMS.DETECTION_CHANGES().add(beforeFirstTranslation);
  },

  /**
   * Record a change of the target language in the health report. This should
   * only be called when actually executing a translation, not every time the
   * user changes in the language in the UI.
   */
  recordTargetLanguageChange() {
    this.HISTOGRAMS.TARGET_CHANGES().add();
  },

  /**
   * Record a denied translation offer.
   */
  recordDeniedTranslationOffer() {
    this.HISTOGRAMS.DENIED().add();
  },

  /**
   * Record a "Show Original" command use.
   */
  recordShowOriginalContent() {
    this.HISTOGRAMS.SHOW_ORIGINAL().add();
  },

  /**
   * Record the state of translation preferences.
   */
  recordPreferences() {
    if (Services.prefs.getBoolPref(TRANSLATION_PREF_SHOWUI)) {
      this.HISTOGRAMS.SHOW_UI().add(1);
    }
    if (Services.prefs.getBoolPref(TRANSLATION_PREF_DETECT_LANG)) {
      this.HISTOGRAMS.DETECT_LANG().add(1);
    }
  },

  _recordOpportunity(language, success) {
    this.HISTOGRAMS.OPPORTUNITIES().add(success);
    this.HISTOGRAMS.OPPORTUNITIES_BY_LANG().add(language, success);
  },
};

TranslationTelemetry.init();