forked from mirrors/gecko-dev
***
Bug 1514594: Part 3a - Change ChromeUtils.import to return an exports object; not pollute global. r=mccr8
This changes the behavior of ChromeUtils.import() to return an exports object,
rather than a module global, in all cases except when `null` is passed as a
second argument, and changes the default behavior not to pollute the global
scope with the module's exports. Thus, the following code written for the old
model:
ChromeUtils.import("resource://gre/modules/Services.jsm");
is approximately the same as the following, in the new model:
var {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
Since the two behaviors are mutually incompatible, this patch will land with a
scripted rewrite to update all existing callers to use the new model rather
than the old.
***
Bug 1514594: Part 3b - Mass rewrite all JS code to use the new ChromeUtils.import API. rs=Gijs
This was done using the followng script:
https://bitbucket.org/kmaglione/m-c-rewrites/src/tip/processors/cu-import-exports.jsm
***
Bug 1514594: Part 3c - Update ESLint plugin for ChromeUtils.import API changes. r=Standard8
Differential Revision: https://phabricator.services.mozilla.com/D16747
***
Bug 1514594: Part 3d - Remove/fix hundreds of duplicate imports from sync tests. r=Gijs
Differential Revision: https://phabricator.services.mozilla.com/D16748
***
Bug 1514594: Part 3e - Remove no-op ChromeUtils.import() calls. r=Gijs
Differential Revision: https://phabricator.services.mozilla.com/D16749
***
Bug 1514594: Part 3f.1 - Cleanup various test corner cases after mass rewrite. r=Gijs
***
Bug 1514594: Part 3f.2 - Cleanup various non-test corner cases after mass rewrite. r=Gijs
Differential Revision: https://phabricator.services.mozilla.com/D16750
--HG--
extra : rebase_source : 359574ee3064c90f33bf36c2ebe3159a24cc8895
extra : histedit_source : b93c8f42808b1599f9122d7842d2c0b3e656a594%2C64a3a4e3359dc889e2ab2b49461bab9e27fc10a7
142 lines
4.8 KiB
JavaScript
142 lines
4.8 KiB
JavaScript
/* 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 = ["LanguageDetector"];
|
|
|
|
const {clearTimeout, setTimeout} = ChromeUtils.import("resource://gre/modules/Timer.jsm");
|
|
|
|
// Since Emscripten can handle heap growth, but not heap shrinkage, we
|
|
// need to refresh the worker after we've processed a particularly large
|
|
// string in order to prevent unnecessary resident memory growth.
|
|
//
|
|
// These values define the cut-off string length and the idle timeout
|
|
// (in milliseconds) before destroying a worker. Once a string of the
|
|
// maximum size has been processed, the worker is marked for
|
|
// destruction, and is terminated as soon as it has been idle for the
|
|
// given timeout.
|
|
//
|
|
// 1.5MB. This is the approximate string length that forces heap growth
|
|
// for a 2MB heap.
|
|
var LARGE_STRING = 1.5 * 1024 * 1024;
|
|
var IDLE_TIMEOUT = 10 * 1000;
|
|
|
|
const WORKER_URL = "resource:///modules/translation/cld-worker.js";
|
|
|
|
var workerManager = {
|
|
detectionQueue: [],
|
|
|
|
detectLanguage(aParams) {
|
|
return this.workerReady.then(worker => {
|
|
return new Promise(resolve => {
|
|
this.detectionQueue.push({resolve});
|
|
worker.postMessage(aParams);
|
|
});
|
|
}).then(result => {
|
|
// We have our asynchronous result from the worker.
|
|
//
|
|
// Determine if our input was large enough to trigger heap growth,
|
|
// or if we're already waiting to destroy the worker when it's
|
|
// idle. If so, schedule termination after the idle timeout.
|
|
if (aParams.text.length >= LARGE_STRING || this._idleTimeout != null)
|
|
this.flushWorker();
|
|
|
|
return result;
|
|
});
|
|
},
|
|
|
|
_worker: null,
|
|
_workerReadyPromise: null,
|
|
|
|
get workerReady() {
|
|
if (!this._workerReadyPromise)
|
|
this._workerReadyPromise = new Promise(resolve => {
|
|
let worker = new Worker(WORKER_URL);
|
|
worker.onmessage = (aMsg) => {
|
|
if (aMsg.data == "ready")
|
|
resolve(worker);
|
|
else
|
|
this.detectionQueue.shift().resolve(aMsg.data);
|
|
};
|
|
this._worker = worker;
|
|
});
|
|
|
|
return this._workerReadyPromise;
|
|
},
|
|
|
|
// Holds the ID of the current pending idle cleanup setTimeout.
|
|
_idleTimeout: null,
|
|
|
|
// Schedule the current worker to be terminated after the idle timeout.
|
|
flushWorker() {
|
|
if (this._idleTimeout != null)
|
|
clearTimeout(this._idleTimeout);
|
|
|
|
this._idleTimeout = setTimeout(this._flushWorker.bind(this), IDLE_TIMEOUT);
|
|
},
|
|
|
|
// Immediately terminate the worker, as long as there no pending
|
|
// results. Otherwise, reschedule termination until after the next
|
|
// idle timeout.
|
|
_flushWorker() {
|
|
if (this.detectionQueue.length)
|
|
this.flushWorker();
|
|
else {
|
|
if (this._worker)
|
|
this._worker.terminate();
|
|
|
|
this._worker = null;
|
|
this._workerReadyPromise = null;
|
|
this._idleTimeout = null;
|
|
}
|
|
},
|
|
};
|
|
|
|
var LanguageDetector = {
|
|
/**
|
|
* Detect the language of a given string.
|
|
*
|
|
* The argument may be either a string containing the text to analyze,
|
|
* or an object with the following properties:
|
|
*
|
|
* - 'text' The text to analyze.
|
|
*
|
|
* - 'isHTML' (optional) A boolean, indicating whether the text
|
|
* should be analyzed as HTML rather than plain text.
|
|
*
|
|
* - 'language' (optional) A string indicating the expected language.
|
|
* For text extracted from HTTP documents, this is expected to
|
|
* come from the Content-Language header.
|
|
*
|
|
* - 'tld' (optional) A string indicating the top-level domain of the
|
|
* document the text was extracted from.
|
|
*
|
|
* - 'encoding' (optional) A string describing the encoding of the
|
|
* document the string was extracted from. Note that, regardless
|
|
* of the value of this property, the 'text' property must be a
|
|
* UTF-16 JavaScript string.
|
|
*
|
|
* @returns {Promise<Object>}
|
|
* @resolves When detection is finished, with a object containing
|
|
* these fields:
|
|
* - 'language' (string with a language code)
|
|
* - 'confident' (boolean) Whether the detector is confident of the
|
|
* result.
|
|
* - 'languages' (array) An array of up to three elements, containing
|
|
* the most prevalent languages detected. It contains a
|
|
* 'languageCode' property, containing the ISO language code of
|
|
* the language, and a 'percent' property, describing the
|
|
* approximate percentage of the input which is in that language.
|
|
* For text of an unknown language, the result may contain an
|
|
* entry with the languge code 'un', indicating the percent of
|
|
* the text which is unknown.
|
|
*/
|
|
detectLanguage(aParams) {
|
|
if (typeof aParams == "string")
|
|
aParams = { text: aParams };
|
|
|
|
return workerManager.detectLanguage(aParams);
|
|
},
|
|
};
|