gecko-dev/toolkit/components/telemetry/pings/ModulesPing.jsm
Kris Maglione e930b89c34 Bug 1514594: Part 3 - Change ChromeUtils.import API.
***
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
2019-01-17 10:18:31 -08:00

102 lines
3.4 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";
const {XPCOMUtils} = ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
ChromeUtils.defineModuleGetter(this, "Preferences",
"resource://gre/modules/Preferences.jsm");
ChromeUtils.defineModuleGetter(this, "Log",
"resource://gre/modules/Log.jsm");
ChromeUtils.defineModuleGetter(this, "TelemetryController",
"resource://gre/modules/TelemetryController.jsm");
ChromeUtils.defineModuleGetter(this, "AppConstants",
"resource://gre/modules/AppConstants.jsm");
XPCOMUtils.defineLazyServiceGetter(this, "gUpdateTimerManager",
"@mozilla.org/updates/timer-manager;1", "nsIUpdateTimerManager");
XPCOMUtils.defineLazyServiceGetter(this, "Telemetry",
"@mozilla.org/base/telemetry;1", "nsITelemetry");
var EXPORTED_SYMBOLS = [
"TelemetryModules",
];
const LOGGER_NAME = "Toolkit.Telemetry";
const LOGGER_PREFIX = "TelemetryModules::";
// The default is 1 week.
const MODULES_PING_INTERVAL_SECONDS = 7 * 24 * 60 * 60;
const MODULES_PING_INTERVAL_PREFERENCE = "toolkit.telemetry.modulesPing.interval";
const MAX_MODULES_NUM = 512;
const MAX_NAME_LENGTH = 64;
const TRUNCATION_DELIMITER = "\u2026";
var TelemetryModules = Object.freeze({
_log: Log.repository.getLoggerWithMessagePrefix(LOGGER_NAME, LOGGER_PREFIX),
start() {
// The list of loaded modules is obtainable only when the profiler is enabled.
// If it isn't, we don't want to send the ping at all.
if (!AppConstants.MOZ_GECKO_PROFILER) {
return;
}
// Use nsIUpdateTimerManager for a long-duration timer that survives across sessions.
gUpdateTimerManager.registerTimer(
"telemetry_modules_ping",
this,
Preferences.get(MODULES_PING_INTERVAL_PREFERENCE, MODULES_PING_INTERVAL_SECONDS)
);
},
/**
* Called when the 'telemetry_modules_ping' timer fires.
*/
notify() {
try {
Telemetry.getLoadedModules().then(
modules => {
modules = modules.filter(module => module.name.length > 0);
// Cut the list of modules to MAX_MODULES_NUM entries.
if (modules.length > MAX_MODULES_NUM) {
modules = modules.slice(0, MAX_MODULES_NUM);
}
// Cut the file names of the modules to MAX_NAME_LENGTH characters.
for (let module of modules) {
if (module.name.length > MAX_NAME_LENGTH) {
module.name = module.name.substr(0, MAX_NAME_LENGTH - 1) + TRUNCATION_DELIMITER;
}
if (module.debugName !== null && module.debugName.length > MAX_NAME_LENGTH) {
module.debugName = module.debugName.substr(0, MAX_NAME_LENGTH - 1) + TRUNCATION_DELIMITER;
}
if (module.certSubject !== undefined && module.certSubject.length > MAX_NAME_LENGTH) {
module.certSubject = module.certSubject.substr(0, MAX_NAME_LENGTH - 1) + TRUNCATION_DELIMITER;
}
}
TelemetryController.submitExternalPing("modules",
{
version: 1,
modules,
},
{
addClientId: true,
addEnvironment: true,
}
);
},
err => this._log.error("notify - promise failed", err)
);
} catch (ex) {
this._log.error("notify - caught exception", ex);
}
},
});