2014-03-18 14:52:28 -07:00
|
|
|
/* 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";
|
|
|
|
|
|
|
|
this.EXPORTED_SYMBOLS = [
|
|
|
|
"Experiments",
|
|
|
|
];
|
|
|
|
|
|
|
|
const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Task.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Promise.jsm");
|
|
|
|
Cu.import("resource://gre/modules/osfile.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Log.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Preferences.jsm");
|
2014-03-18 14:52:30 -07:00
|
|
|
Cu.import("resource://gre/modules/AsyncShutdown.jsm");
|
2014-03-18 14:52:28 -07:00
|
|
|
|
2015-09-14 11:04:19 -07:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "UpdateUtils",
|
|
|
|
"resource://gre/modules/UpdateUtils.jsm");
|
2014-03-18 14:52:28 -07:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "AddonManager",
|
|
|
|
"resource://gre/modules/AddonManager.jsm");
|
2014-04-23 05:34:48 -07:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "AddonManagerPrivate",
|
|
|
|
"resource://gre/modules/AddonManager.jsm");
|
2015-06-23 11:56:11 -07:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "TelemetryEnvironment",
|
|
|
|
"resource://gre/modules/TelemetryEnvironment.jsm");
|
2014-03-24 01:58:57 -07:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "TelemetryLog",
|
|
|
|
"resource://gre/modules/TelemetryLog.jsm");
|
2015-12-04 00:49:00 -08:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "TelemetryUtils",
|
|
|
|
"resource://gre/modules/TelemetryUtils.jsm");
|
2014-04-08 14:18:17 -07:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "CommonUtils",
|
|
|
|
"resource://services-common/utils.js");
|
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "Metrics",
|
|
|
|
"resource://gre/modules/Metrics.jsm");
|
|
|
|
|
2014-03-24 12:38:13 -07:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "gCrashReporter",
|
|
|
|
"@mozilla.org/xre/app-info;1",
|
|
|
|
"nsICrashReporter");
|
2014-03-18 14:52:28 -07:00
|
|
|
|
|
|
|
const FILE_CACHE = "experiments.json";
|
2014-05-06 03:18:10 -07:00
|
|
|
const EXPERIMENTS_CHANGED_TOPIC = "experiments-changed";
|
2014-03-18 14:52:28 -07:00
|
|
|
const MANIFEST_VERSION = 1;
|
|
|
|
const CACHE_VERSION = 1;
|
|
|
|
|
|
|
|
const KEEP_HISTORY_N_DAYS = 180;
|
|
|
|
const MIN_EXPERIMENT_ACTIVE_SECONDS = 60;
|
|
|
|
|
|
|
|
const PREF_BRANCH = "experiments.";
|
|
|
|
const PREF_ENABLED = "enabled"; // experiments.enabled
|
2014-04-15 09:12:26 -07:00
|
|
|
const PREF_ACTIVE_EXPERIMENT = "activeExperiment"; // whether we have an active experiment
|
2014-03-18 14:52:28 -07:00
|
|
|
const PREF_LOGGING = "logging";
|
|
|
|
const PREF_LOGGING_LEVEL = PREF_LOGGING + ".level"; // experiments.logging.level
|
|
|
|
const PREF_LOGGING_DUMP = PREF_LOGGING + ".dump"; // experiments.logging.dump
|
|
|
|
const PREF_MANIFEST_URI = "manifest.uri"; // experiments.logging.manifest.uri
|
2014-08-08 05:14:22 -07:00
|
|
|
const PREF_FORCE_SAMPLE = "force-sample-value"; // experiments.force-sample-value
|
2014-03-18 14:52:28 -07:00
|
|
|
|
|
|
|
const PREF_BRANCH_TELEMETRY = "toolkit.telemetry.";
|
|
|
|
const PREF_TELEMETRY_ENABLED = "enabled";
|
|
|
|
|
2014-04-23 05:34:48 -07:00
|
|
|
const URI_EXTENSION_STRINGS = "chrome://mozapps/locale/extensions/extensions.properties";
|
|
|
|
const STRING_TYPE_NAME = "type.%ID%.name";
|
|
|
|
|
2014-08-14 13:21:15 -07:00
|
|
|
const CACHE_WRITE_RETRY_DELAY_SEC = 60 * 3;
|
2014-09-01 06:26:07 -07:00
|
|
|
const MANIFEST_FETCH_TIMEOUT_MSEC = 60 * 3 * 1000; // 3 minutes
|
2014-08-14 13:21:15 -07:00
|
|
|
|
2014-03-24 01:58:57 -07:00
|
|
|
const TELEMETRY_LOG = {
|
|
|
|
// log(key, [kind, experimentId, details])
|
|
|
|
ACTIVATION_KEY: "EXPERIMENT_ACTIVATION",
|
|
|
|
ACTIVATION: {
|
2014-04-17 06:47:36 -07:00
|
|
|
// Successfully activated.
|
|
|
|
ACTIVATED: "ACTIVATED",
|
|
|
|
// Failed to install the add-on.
|
|
|
|
INSTALL_FAILURE: "INSTALL_FAILURE",
|
|
|
|
// Experiment does not meet activation requirements. Details will
|
|
|
|
// be provided.
|
|
|
|
REJECTED: "REJECTED",
|
2014-03-24 01:58:57 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
// log(key, [kind, experimentId, optionalDetails...])
|
|
|
|
TERMINATION_KEY: "EXPERIMENT_TERMINATION",
|
|
|
|
TERMINATION: {
|
2014-04-17 06:47:36 -07:00
|
|
|
// The Experiments service was disabled.
|
|
|
|
SERVICE_DISABLED: "SERVICE_DISABLED",
|
|
|
|
// Add-on uninstalled.
|
|
|
|
ADDON_UNINSTALLED: "ADDON_UNINSTALLED",
|
|
|
|
// The experiment disabled itself.
|
|
|
|
FROM_API: "FROM_API",
|
|
|
|
// The experiment expired (e.g. by exceeding the end date).
|
|
|
|
EXPIRED: "EXPIRED",
|
|
|
|
// Disabled after re-evaluating conditions. If this is specified,
|
|
|
|
// details will be provided.
|
|
|
|
RECHECK: "RECHECK",
|
2014-03-24 01:58:57 -07:00
|
|
|
},
|
|
|
|
};
|
2015-10-07 05:03:21 -07:00
|
|
|
XPCOMUtils.defineConstant(this, "TELEMETRY_LOG", TELEMETRY_LOG);
|
2014-03-24 01:58:57 -07:00
|
|
|
|
2014-03-18 14:52:28 -07:00
|
|
|
const gPrefs = new Preferences(PREF_BRANCH);
|
|
|
|
const gPrefsTelemetry = new Preferences(PREF_BRANCH_TELEMETRY);
|
2015-09-15 11:19:45 -07:00
|
|
|
var gExperimentsEnabled = false;
|
|
|
|
var gAddonProvider = null;
|
|
|
|
var gExperiments = null;
|
|
|
|
var gLogAppenderDump = null;
|
|
|
|
var gPolicyCounter = 0;
|
|
|
|
var gExperimentsCounter = 0;
|
|
|
|
var gExperimentEntryCounter = 0;
|
|
|
|
var gPreviousProviderCounter = 0;
|
2014-03-18 14:52:28 -07:00
|
|
|
|
2014-04-04 15:58:26 -07:00
|
|
|
// Tracks active AddonInstall we know about so we can deny external
|
|
|
|
// installs.
|
2015-09-15 11:19:45 -07:00
|
|
|
var gActiveInstallURLs = new Set();
|
2014-04-04 15:58:26 -07:00
|
|
|
|
2014-04-17 06:47:36 -07:00
|
|
|
// Tracks add-on IDs that are being uninstalled by us. This allows us
|
|
|
|
// to differentiate between expected uninstalled and user-driven uninstalls.
|
2015-09-15 11:19:45 -07:00
|
|
|
var gActiveUninstallAddonIDs = new Set();
|
2014-04-17 06:47:36 -07:00
|
|
|
|
2015-09-15 11:19:45 -07:00
|
|
|
var gLogger;
|
|
|
|
var gLogDumping = false;
|
2014-03-18 14:52:28 -07:00
|
|
|
|
|
|
|
function configureLogging() {
|
2014-03-25 10:50:34 -07:00
|
|
|
if (!gLogger) {
|
|
|
|
gLogger = Log.repository.getLogger("Browser.Experiments");
|
|
|
|
gLogger.addAppender(new Log.ConsoleAppender(new Log.BasicFormatter()));
|
|
|
|
}
|
2014-03-27 06:30:25 -07:00
|
|
|
gLogger.level = gPrefs.get(PREF_LOGGING_LEVEL, Log.Level.Warn);
|
2014-03-18 14:52:28 -07:00
|
|
|
|
2014-03-25 10:50:34 -07:00
|
|
|
let logDumping = gPrefs.get(PREF_LOGGING_DUMP, false);
|
|
|
|
if (logDumping != gLogDumping) {
|
|
|
|
if (logDumping) {
|
|
|
|
gLogAppenderDump = new Log.DumpAppender(new Log.BasicFormatter());
|
|
|
|
gLogger.addAppender(gLogAppenderDump);
|
|
|
|
} else {
|
|
|
|
gLogger.removeAppender(gLogAppenderDump);
|
|
|
|
gLogAppenderDump = null;
|
|
|
|
}
|
|
|
|
gLogDumping = logDumping;
|
2014-03-18 14:52:28 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Loads a JSON file using OS.file. file is a string representing the path
|
|
|
|
// of the file to be read, options contains additional options to pass to
|
|
|
|
// OS.File.read.
|
|
|
|
// Returns a Promise resolved with the json payload or rejected with
|
|
|
|
// OS.File.Error or JSON.parse() errors.
|
|
|
|
function loadJSONAsync(file, options) {
|
2015-11-30 03:42:25 -08:00
|
|
|
return Task.spawn(function*() {
|
2014-03-18 14:52:28 -07:00
|
|
|
let rawData = yield OS.File.read(file, options);
|
|
|
|
// Read json file into a string
|
|
|
|
let data;
|
|
|
|
try {
|
|
|
|
// Obtain a converter to read from a UTF-8 encoded input stream.
|
|
|
|
let converter = new TextDecoder();
|
|
|
|
data = JSON.parse(converter.decode(rawData));
|
|
|
|
} catch (ex) {
|
|
|
|
gLogger.error("Experiments: Could not parse JSON: " + file + " " + ex);
|
|
|
|
throw ex;
|
|
|
|
}
|
2015-11-30 03:42:25 -08:00
|
|
|
return data;
|
2014-03-18 14:52:28 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-03-27 14:26:59 -07:00
|
|
|
// Returns a promise that is resolved with the AddonInstall for that URL.
|
|
|
|
function addonInstallForURL(url, hash) {
|
|
|
|
let deferred = Promise.defer();
|
|
|
|
AddonManager.getInstallForURL(url, install => deferred.resolve(install),
|
|
|
|
"application/x-xpinstall", hash);
|
|
|
|
return deferred.promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a promise that is resolved with an Array<Addon> of the installed
|
|
|
|
// experiment addons.
|
|
|
|
function installedExperimentAddons() {
|
|
|
|
let deferred = Promise.defer();
|
2014-04-23 05:34:48 -07:00
|
|
|
AddonManager.getAddonsByTypes(["experiment"], (addons) => {
|
2015-12-17 11:42:24 -08:00
|
|
|
deferred.resolve(addons.filter(a => !a.appDisabled));
|
2014-04-23 05:34:48 -07:00
|
|
|
});
|
2014-03-27 14:26:59 -07:00
|
|
|
return deferred.promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Takes an Array<Addon> and returns a promise that is resolved when the
|
|
|
|
// addons are uninstalled.
|
|
|
|
function uninstallAddons(addons) {
|
2015-12-17 11:42:24 -08:00
|
|
|
let ids = new Set(addons.map(addon => addon.id));
|
2014-03-27 14:26:59 -07:00
|
|
|
let deferred = Promise.defer();
|
|
|
|
|
|
|
|
let listener = {};
|
|
|
|
listener.onUninstalled = addon => {
|
|
|
|
if (!ids.has(addon.id)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ids.delete(addon.id);
|
|
|
|
if (ids.size == 0) {
|
|
|
|
AddonManager.removeAddonListener(listener);
|
|
|
|
deferred.resolve();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
AddonManager.addAddonListener(listener);
|
|
|
|
|
|
|
|
for (let addon of addons) {
|
2014-04-04 15:58:27 -07:00
|
|
|
// Disabling the add-on before uninstalling is necessary to cause tests to
|
|
|
|
// pass. This might be indicative of a bug in XPIProvider.
|
|
|
|
// TODO follow up in bug 992396.
|
|
|
|
addon.userDisabled = true;
|
2014-03-27 14:26:59 -07:00
|
|
|
addon.uninstall();
|
|
|
|
}
|
|
|
|
|
|
|
|
return deferred.promise;
|
|
|
|
}
|
|
|
|
|
2014-03-18 14:52:28 -07:00
|
|
|
/**
|
|
|
|
* The experiments module.
|
|
|
|
*/
|
|
|
|
|
2015-09-15 11:19:45 -07:00
|
|
|
var Experiments = {
|
2014-03-18 14:52:28 -07:00
|
|
|
/**
|
|
|
|
* Provides access to the global `Experiments.Experiments` instance.
|
|
|
|
*/
|
|
|
|
instance: function () {
|
|
|
|
if (!gExperiments) {
|
|
|
|
gExperiments = new Experiments.Experiments();
|
|
|
|
}
|
|
|
|
|
|
|
|
return gExperiments;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The policy object allows us to inject fake enviroment data from the
|
|
|
|
* outside by monkey-patching.
|
|
|
|
*/
|
|
|
|
|
|
|
|
Experiments.Policy = function () {
|
2014-03-28 12:57:43 -07:00
|
|
|
this._log = Log.repository.getLoggerWithMessagePrefix(
|
2014-03-28 13:04:09 -07:00
|
|
|
"Browser.Experiments.Policy",
|
|
|
|
"Policy #" + gPolicyCounter++ + "::");
|
2014-04-17 06:47:36 -07:00
|
|
|
|
|
|
|
// Set to true to ignore hash verification on downloaded XPIs. This should
|
|
|
|
// not be used outside of testing.
|
|
|
|
this.ignoreHashes = false;
|
2014-03-18 14:52:28 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
Experiments.Policy.prototype = {
|
|
|
|
now: function () {
|
|
|
|
return new Date();
|
|
|
|
},
|
|
|
|
|
|
|
|
random: function () {
|
2014-03-20 08:19:19 -07:00
|
|
|
let pref = gPrefs.get(PREF_FORCE_SAMPLE);
|
|
|
|
if (pref !== undefined) {
|
|
|
|
let val = Number.parseFloat(pref);
|
2014-03-28 12:57:43 -07:00
|
|
|
this._log.debug("random sample forced: " + val);
|
2014-04-04 06:16:58 -07:00
|
|
|
if (isNaN(val) || val < 0) {
|
2014-03-20 08:19:19 -07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (val > 1) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return val;
|
|
|
|
}
|
2014-03-18 14:52:28 -07:00
|
|
|
return Math.random();
|
|
|
|
},
|
|
|
|
|
|
|
|
futureDate: function (offset) {
|
|
|
|
return new Date(this.now().getTime() + offset);
|
|
|
|
},
|
|
|
|
|
|
|
|
oneshotTimer: function (callback, timeout, thisObj, name) {
|
|
|
|
return CommonUtils.namedTimer(callback, timeout, thisObj, name);
|
|
|
|
},
|
|
|
|
|
|
|
|
updatechannel: function () {
|
2015-09-14 11:04:19 -07:00
|
|
|
return UpdateUtils.UpdateChannel;
|
2014-03-18 14:52:28 -07:00
|
|
|
},
|
|
|
|
|
2014-03-26 08:35:59 -07:00
|
|
|
locale: function () {
|
|
|
|
let chrome = Cc["@mozilla.org/chrome/chrome-registry;1"].getService(Ci.nsIXULChromeRegistry);
|
|
|
|
return chrome.getSelectedLocale("global");
|
|
|
|
},
|
|
|
|
|
2014-08-28 07:25:05 -07:00
|
|
|
/**
|
|
|
|
* For testing a race condition, one of the tests delays the callback of
|
|
|
|
* writing the cache by replacing this policy function.
|
|
|
|
*/
|
|
|
|
delayCacheWrite: function(promise) {
|
|
|
|
return promise;
|
|
|
|
},
|
2014-03-18 14:52:28 -07:00
|
|
|
};
|
|
|
|
|
2014-04-17 06:47:37 -07:00
|
|
|
function AlreadyShutdownError(message="already shut down") {
|
2014-05-14 18:12:00 -07:00
|
|
|
Error.call(this, message);
|
|
|
|
let error = new Error();
|
2014-04-17 06:47:37 -07:00
|
|
|
this.name = "AlreadyShutdownError";
|
|
|
|
this.message = message;
|
2014-05-14 18:12:00 -07:00
|
|
|
this.stack = error.stack;
|
2014-04-17 06:47:37 -07:00
|
|
|
}
|
2014-05-14 18:12:00 -07:00
|
|
|
AlreadyShutdownError.prototype = Object.create(Error.prototype);
|
2014-04-17 06:47:37 -07:00
|
|
|
AlreadyShutdownError.prototype.constructor = AlreadyShutdownError;
|
|
|
|
|
2014-08-14 13:21:15 -07:00
|
|
|
function CacheWriteError(message="Error writing cache file") {
|
|
|
|
Error.call(this, message);
|
|
|
|
let error = new Error();
|
|
|
|
this.name = "CacheWriteError";
|
|
|
|
this.message = message;
|
|
|
|
this.stack = error.stack;
|
|
|
|
}
|
|
|
|
CacheWriteError.prototype = Object.create(Error.prototype);
|
|
|
|
CacheWriteError.prototype.constructor = CacheWriteError;
|
|
|
|
|
2014-03-18 14:52:28 -07:00
|
|
|
/**
|
|
|
|
* Manages the experiments and provides an interface to control them.
|
|
|
|
*/
|
|
|
|
|
|
|
|
Experiments.Experiments = function (policy=new Experiments.Policy()) {
|
2014-07-25 04:40:17 -07:00
|
|
|
let log = Log.repository.getLoggerWithMessagePrefix(
|
|
|
|
"Browser.Experiments.Experiments",
|
|
|
|
"Experiments #" + gExperimentsCounter++ + "::");
|
|
|
|
|
|
|
|
// At the time of this writing, Experiments.jsm has severe
|
|
|
|
// crashes. For forensics purposes, keep the last few log
|
|
|
|
// messages in memory and upload them in case of crash.
|
|
|
|
this._forensicsLogs = [];
|
2014-09-15 06:28:33 -07:00
|
|
|
this._forensicsLogs.length = 20;
|
2014-07-25 04:40:17 -07:00
|
|
|
this._log = Object.create(log);
|
|
|
|
this._log.log = (level, string, params) => {
|
|
|
|
this._forensicsLogs.shift();
|
|
|
|
this._forensicsLogs.push(level + ": " + string);
|
|
|
|
log.log(level, string, params);
|
|
|
|
};
|
|
|
|
|
2014-04-17 07:52:23 -07:00
|
|
|
this._log.trace("constructor");
|
2014-03-28 11:20:31 -07:00
|
|
|
|
2014-07-25 04:40:17 -07:00
|
|
|
// Capture the latest error, for forensics purposes.
|
|
|
|
this._latestError = null;
|
|
|
|
|
|
|
|
|
2014-03-18 14:52:28 -07:00
|
|
|
this._policy = policy;
|
|
|
|
|
|
|
|
// This is a Map of (string -> ExperimentEntry), keyed with the experiment id.
|
|
|
|
// It holds both the current experiments and history.
|
|
|
|
// Map() preserves insertion order, which means we preserve the manifest order.
|
2014-03-25 10:50:34 -07:00
|
|
|
// This is null until we've successfully completed loading the cache from
|
|
|
|
// disk the first time.
|
2014-03-18 14:52:28 -07:00
|
|
|
this._experiments = null;
|
2014-03-25 10:50:34 -07:00
|
|
|
this._refresh = false;
|
|
|
|
this._terminateReason = null; // or TELEMETRY_LOG.TERMINATION....
|
|
|
|
this._dirty = false;
|
|
|
|
|
|
|
|
// Loading the cache happens once asynchronously on startup
|
|
|
|
this._loadTask = null;
|
|
|
|
|
|
|
|
// The _main task handles all other actions:
|
|
|
|
// * refreshing the manifest off the network (if _refresh)
|
|
|
|
// * disabling/enabling experiments
|
|
|
|
// * saving the cache (if _dirty)
|
|
|
|
this._mainTask = null;
|
|
|
|
|
2014-03-18 14:52:28 -07:00
|
|
|
// Timer for re-evaluating experiment status.
|
|
|
|
this._timer = null;
|
|
|
|
|
2014-03-18 14:52:30 -07:00
|
|
|
this._shutdown = false;
|
2014-09-01 06:26:07 -07:00
|
|
|
this._networkRequest = null;
|
2014-03-18 14:52:30 -07:00
|
|
|
|
2014-05-16 03:56:25 -07:00
|
|
|
// We need to tell when we first evaluated the experiments to fire an
|
|
|
|
// experiments-changed notification when we only loaded completed experiments.
|
|
|
|
this._firstEvaluate = true;
|
|
|
|
|
2014-03-18 14:52:28 -07:00
|
|
|
this.init();
|
|
|
|
};
|
|
|
|
|
|
|
|
Experiments.Experiments.prototype = {
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsITimerCallback, Ci.nsIObserver]),
|
|
|
|
|
2015-03-10 05:36:17 -07:00
|
|
|
/**
|
|
|
|
* `true` if the experiments manager is currently setup (has been fully initialized
|
|
|
|
* and not uninitialized yet).
|
|
|
|
*/
|
|
|
|
get isReady() {
|
|
|
|
return !this._shutdown;
|
|
|
|
},
|
|
|
|
|
2014-03-18 14:52:28 -07:00
|
|
|
init: function () {
|
2014-04-07 14:11:31 -07:00
|
|
|
this._shutdown = false;
|
2014-03-18 14:52:28 -07:00
|
|
|
configureLogging();
|
|
|
|
|
2015-12-04 00:49:00 -08:00
|
|
|
gExperimentsEnabled = gPrefs.get(PREF_ENABLED, false) && TelemetryUtils.isTelemetryEnabled;
|
2014-03-28 11:20:31 -07:00
|
|
|
this._log.trace("enabled=" + gExperimentsEnabled + ", " + this.enabled);
|
2014-03-18 14:52:28 -07:00
|
|
|
|
|
|
|
gPrefs.observe(PREF_LOGGING, configureLogging);
|
|
|
|
gPrefs.observe(PREF_MANIFEST_URI, this.updateManifest, this);
|
|
|
|
gPrefs.observe(PREF_ENABLED, this._toggleExperimentsEnabled, this);
|
|
|
|
|
|
|
|
gPrefsTelemetry.observe(PREF_TELEMETRY_ENABLED, this._telemetryStatusChanged, this);
|
|
|
|
|
2014-07-25 04:40:17 -07:00
|
|
|
AddonManager.shutdown.addBlocker("Experiments.jsm shutdown",
|
|
|
|
this.uninit.bind(this),
|
|
|
|
this._getState.bind(this)
|
|
|
|
);
|
2014-03-18 14:52:30 -07:00
|
|
|
|
2014-04-17 06:47:37 -07:00
|
|
|
this._registerWithAddonManager();
|
2014-04-10 13:29:02 -07:00
|
|
|
|
2014-04-17 06:47:37 -07:00
|
|
|
this._loadTask = this._loadFromCache();
|
2014-05-14 18:12:00 -07:00
|
|
|
|
|
|
|
return this._loadTask.then(
|
2014-03-25 10:50:34 -07:00
|
|
|
() => {
|
2014-03-28 12:57:43 -07:00
|
|
|
this._log.trace("_loadTask finished ok");
|
2014-03-25 10:50:34 -07:00
|
|
|
this._loadTask = null;
|
2014-05-14 18:12:00 -07:00
|
|
|
return this._run();
|
2014-03-25 10:50:34 -07:00
|
|
|
},
|
|
|
|
(e) => {
|
2014-03-28 12:57:43 -07:00
|
|
|
this._log.error("_loadFromCache caught error: " + e);
|
2014-07-25 04:40:17 -07:00
|
|
|
this._latestError = e;
|
2014-05-14 18:12:00 -07:00
|
|
|
throw e;
|
2014-03-25 10:50:34 -07:00
|
|
|
}
|
|
|
|
);
|
2014-03-18 14:52:28 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-04-07 14:11:31 -07:00
|
|
|
* Uninitialize this instance.
|
|
|
|
*
|
|
|
|
* This function is susceptible to race conditions. If it is called multiple
|
|
|
|
* times before the previous uninit() has completed or if it is called while
|
|
|
|
* an init() operation is being performed, the object may get in bad state
|
|
|
|
* and/or deadlock could occur.
|
|
|
|
*
|
2014-03-18 14:52:28 -07:00
|
|
|
* @return Promise<>
|
|
|
|
* The promise is fulfilled when all pending tasks are finished.
|
|
|
|
*/
|
2014-04-07 14:11:31 -07:00
|
|
|
uninit: Task.async(function* () {
|
2014-04-17 07:52:23 -07:00
|
|
|
this._log.trace("uninit: started");
|
2014-04-07 14:11:31 -07:00
|
|
|
yield this._loadTask;
|
2014-04-17 07:52:23 -07:00
|
|
|
this._log.trace("uninit: finished with _loadTask");
|
2014-04-07 14:11:31 -07:00
|
|
|
|
2014-03-18 14:52:30 -07:00
|
|
|
if (!this._shutdown) {
|
2014-04-17 07:52:23 -07:00
|
|
|
this._log.trace("uninit: no previous shutdown");
|
2014-04-17 06:47:37 -07:00
|
|
|
this._unregisterWithAddonManager();
|
2014-03-18 14:52:28 -07:00
|
|
|
|
2014-03-18 14:52:30 -07:00
|
|
|
gPrefs.ignore(PREF_LOGGING, configureLogging);
|
|
|
|
gPrefs.ignore(PREF_MANIFEST_URI, this.updateManifest, this);
|
|
|
|
gPrefs.ignore(PREF_ENABLED, this._toggleExperimentsEnabled, this);
|
2014-03-18 14:52:28 -07:00
|
|
|
|
2014-03-18 14:52:30 -07:00
|
|
|
gPrefsTelemetry.ignore(PREF_TELEMETRY_ENABLED, this._telemetryStatusChanged, this);
|
2014-03-18 14:52:28 -07:00
|
|
|
|
2014-03-18 14:52:30 -07:00
|
|
|
if (this._timer) {
|
|
|
|
this._timer.clear();
|
|
|
|
}
|
2014-03-18 14:52:28 -07:00
|
|
|
}
|
|
|
|
|
2014-03-18 14:52:30 -07:00
|
|
|
this._shutdown = true;
|
2014-03-25 10:50:34 -07:00
|
|
|
if (this._mainTask) {
|
2014-09-01 06:26:07 -07:00
|
|
|
if (this._networkRequest) {
|
|
|
|
try {
|
|
|
|
this._log.trace("Aborting pending network request: " + this._networkRequest);
|
|
|
|
this._networkRequest.abort();
|
|
|
|
} catch (e) {
|
|
|
|
// pass
|
|
|
|
}
|
|
|
|
}
|
2014-04-17 06:47:37 -07:00
|
|
|
try {
|
2014-04-17 07:52:23 -07:00
|
|
|
this._log.trace("uninit: waiting on _mainTask");
|
2014-04-17 06:47:37 -07:00
|
|
|
yield this._mainTask;
|
2014-07-25 04:40:17 -07:00
|
|
|
} catch (e) {
|
2015-12-17 11:42:24 -08:00
|
|
|
// We error out of tasks after shutdown via this exception.
|
|
|
|
if (!(e instanceof AlreadyShutdownError)) {
|
|
|
|
this._latestError = e;
|
|
|
|
throw e;
|
|
|
|
}
|
2014-04-17 06:47:37 -07:00
|
|
|
}
|
2014-03-18 14:52:30 -07:00
|
|
|
}
|
2014-04-07 14:11:31 -07:00
|
|
|
|
|
|
|
this._log.info("Completed uninitialization.");
|
|
|
|
}),
|
2014-03-18 14:52:30 -07:00
|
|
|
|
2014-07-25 04:40:17 -07:00
|
|
|
// Return state information, for debugging purposes.
|
|
|
|
_getState: function() {
|
|
|
|
let state = {
|
|
|
|
isShutdown: this._shutdown,
|
|
|
|
isEnabled: gExperimentsEnabled,
|
|
|
|
isRefresh: this._refresh,
|
|
|
|
isDirty: this._dirty,
|
|
|
|
isFirstEvaluate: this._firstEvaluate,
|
|
|
|
hasLoadTask: !!this._loadTask,
|
|
|
|
hasMainTask: !!this._mainTask,
|
|
|
|
hasTimer: !!this._hasTimer,
|
|
|
|
hasAddonProvider: !!gAddonProvider,
|
|
|
|
latestLogs: this._forensicsLogs,
|
2014-08-01 05:38:45 -07:00
|
|
|
experiments: this._experiments ? this._experiments.keys() : null,
|
2014-07-25 04:40:17 -07:00
|
|
|
terminateReason: this._terminateReason,
|
|
|
|
};
|
|
|
|
if (this._latestError) {
|
|
|
|
if (typeof this._latestError == "object") {
|
|
|
|
state.latestError = {
|
|
|
|
message: this._latestError.message,
|
|
|
|
stack: this._latestError.stack
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
state.latestError = "" + this._latestError;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return state;
|
|
|
|
},
|
|
|
|
|
2014-05-06 03:18:10 -07:00
|
|
|
_registerWithAddonManager: function (previousExperimentsProvider) {
|
2014-04-17 06:47:37 -07:00
|
|
|
this._log.trace("Registering instance with Addon Manager.");
|
|
|
|
|
2014-04-04 15:58:26 -07:00
|
|
|
AddonManager.addAddonListener(this);
|
|
|
|
AddonManager.addInstallListener(this);
|
2014-04-23 05:34:48 -07:00
|
|
|
|
|
|
|
if (!gAddonProvider) {
|
|
|
|
// The properties of this AddonType should be kept in sync with the
|
|
|
|
// experiment AddonType registered in XPIProvider.
|
|
|
|
this._log.trace("Registering previous experiment add-on provider.");
|
2014-05-06 03:18:10 -07:00
|
|
|
gAddonProvider = previousExperimentsProvider || new Experiments.PreviousExperimentProvider(this);
|
|
|
|
AddonManagerPrivate.registerProvider(gAddonProvider, [
|
2014-04-23 05:34:48 -07:00
|
|
|
new AddonManagerPrivate.AddonType("experiment",
|
|
|
|
URI_EXTENSION_STRINGS,
|
|
|
|
STRING_TYPE_NAME,
|
|
|
|
AddonManager.VIEW_TYPE_LIST,
|
|
|
|
11000,
|
|
|
|
AddonManager.TYPE_UI_HIDE_EMPTY),
|
2014-05-06 03:18:10 -07:00
|
|
|
]);
|
2014-04-23 05:34:48 -07:00
|
|
|
}
|
|
|
|
|
2014-04-04 15:58:26 -07:00
|
|
|
},
|
|
|
|
|
2014-04-17 06:47:37 -07:00
|
|
|
_unregisterWithAddonManager: function () {
|
2014-04-23 05:34:48 -07:00
|
|
|
this._log.trace("Unregistering instance with Addon Manager.");
|
2014-07-25 04:40:17 -07:00
|
|
|
|
|
|
|
this._log.trace("Removing install listener from add-on manager.");
|
2014-07-09 06:17:36 -07:00
|
|
|
AddonManager.removeInstallListener(this);
|
2014-07-25 04:40:17 -07:00
|
|
|
this._log.trace("Removing addon listener from add-on manager.");
|
2014-07-09 06:17:36 -07:00
|
|
|
AddonManager.removeAddonListener(this);
|
2014-07-25 04:40:17 -07:00
|
|
|
this._log.trace("Finished unregistering with addon manager.");
|
|
|
|
|
2014-04-23 05:34:48 -07:00
|
|
|
if (gAddonProvider) {
|
2014-07-25 04:40:17 -07:00
|
|
|
this._log.trace("Unregistering previous experiment add-on provider.");
|
2014-04-23 05:34:48 -07:00
|
|
|
AddonManagerPrivate.unregisterProvider(gAddonProvider);
|
|
|
|
gAddonProvider = null;
|
|
|
|
}
|
2014-04-04 15:58:26 -07:00
|
|
|
},
|
|
|
|
|
2014-05-06 03:18:10 -07:00
|
|
|
/*
|
|
|
|
* Change the PreviousExperimentsProvider that this instance uses.
|
|
|
|
* For testing only.
|
|
|
|
*/
|
|
|
|
_setPreviousExperimentsProvider: function (provider) {
|
|
|
|
this._unregisterWithAddonManager();
|
|
|
|
this._registerWithAddonManager(provider);
|
|
|
|
},
|
|
|
|
|
2014-03-18 14:52:30 -07:00
|
|
|
/**
|
|
|
|
* Throws an exception if we've already shut down.
|
|
|
|
*/
|
|
|
|
_checkForShutdown: function() {
|
|
|
|
if (this._shutdown) {
|
2014-04-17 06:47:37 -07:00
|
|
|
throw new AlreadyShutdownError("uninit() already called");
|
2014-03-18 14:52:30 -07:00
|
|
|
}
|
2014-03-18 14:52:28 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether the experiments feature is enabled.
|
|
|
|
*/
|
|
|
|
get enabled() {
|
|
|
|
return gExperimentsEnabled;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Toggle whether the experiments feature is enabled or not.
|
|
|
|
*/
|
|
|
|
set enabled(enabled) {
|
2014-03-28 12:57:43 -07:00
|
|
|
this._log.trace("set enabled(" + enabled + ")");
|
2014-03-18 14:52:28 -07:00
|
|
|
gPrefs.set(PREF_ENABLED, enabled);
|
|
|
|
},
|
|
|
|
|
2014-04-17 06:47:36 -07:00
|
|
|
_toggleExperimentsEnabled: Task.async(function* (enabled) {
|
2014-03-28 12:57:43 -07:00
|
|
|
this._log.trace("_toggleExperimentsEnabled(" + enabled + ")");
|
2014-03-18 14:52:28 -07:00
|
|
|
let wasEnabled = gExperimentsEnabled;
|
2015-12-04 00:49:00 -08:00
|
|
|
gExperimentsEnabled = enabled && TelemetryUtils.isTelemetryEnabled;
|
2014-03-18 14:52:28 -07:00
|
|
|
|
|
|
|
if (wasEnabled == gExperimentsEnabled) {
|
2014-03-25 10:50:34 -07:00
|
|
|
return;
|
2014-03-18 14:52:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (gExperimentsEnabled) {
|
2014-04-17 06:47:36 -07:00
|
|
|
yield this.updateManifest();
|
2014-03-25 10:50:34 -07:00
|
|
|
} else {
|
2014-04-17 06:47:36 -07:00
|
|
|
yield this.disableExperiment(TELEMETRY_LOG.TERMINATION.SERVICE_DISABLED);
|
2014-03-25 10:50:34 -07:00
|
|
|
if (this._timer) {
|
|
|
|
this._timer.clear();
|
|
|
|
}
|
2014-03-18 14:52:28 -07:00
|
|
|
}
|
2014-04-17 06:47:36 -07:00
|
|
|
}),
|
2014-03-18 14:52:28 -07:00
|
|
|
|
|
|
|
_telemetryStatusChanged: function () {
|
2014-03-27 06:30:25 -07:00
|
|
|
this._toggleExperimentsEnabled(gExperimentsEnabled);
|
2014-03-18 14:52:28 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a promise that is resolved with an array of `ExperimentInfo` objects,
|
|
|
|
* which provide info on the currently and recently active experiments.
|
|
|
|
* The array is in chronological order.
|
|
|
|
*
|
|
|
|
* The experiment info is of the form:
|
|
|
|
* {
|
|
|
|
* id: <string>,
|
|
|
|
* name: <string>,
|
|
|
|
* description: <string>,
|
|
|
|
* active: <boolean>,
|
|
|
|
* endDate: <integer>, // epoch ms
|
|
|
|
* detailURL: <string>,
|
|
|
|
* ... // possibly extended later
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* @return Promise<Array<ExperimentInfo>> Array of experiment info objects.
|
|
|
|
*/
|
|
|
|
getExperiments: function () {
|
2014-03-25 10:50:34 -07:00
|
|
|
return Task.spawn(function*() {
|
|
|
|
yield this._loadTask;
|
|
|
|
let list = [];
|
2014-03-18 14:52:28 -07:00
|
|
|
|
2014-03-25 10:50:34 -07:00
|
|
|
for (let [id, experiment] of this._experiments) {
|
|
|
|
if (!experiment.startDate) {
|
|
|
|
// We only collect experiments that are or were active.
|
|
|
|
continue;
|
2014-03-18 14:52:28 -07:00
|
|
|
}
|
|
|
|
|
2014-03-25 10:50:34 -07:00
|
|
|
list.push({
|
|
|
|
id: id,
|
|
|
|
name: experiment._name,
|
|
|
|
description: experiment._description,
|
|
|
|
active: experiment.enabled,
|
|
|
|
endDate: experiment.endDate.getTime(),
|
|
|
|
detailURL: experiment._homepageURL,
|
2014-06-25 15:11:55 -07:00
|
|
|
branch: experiment.branch,
|
2014-03-25 10:50:34 -07:00
|
|
|
});
|
|
|
|
}
|
2014-03-18 14:52:28 -07:00
|
|
|
|
2014-03-25 10:50:34 -07:00
|
|
|
// Sort chronologically, descending.
|
|
|
|
list.sort((a, b) => b.endDate - a.endDate);
|
|
|
|
return list;
|
|
|
|
}.bind(this));
|
2014-03-18 14:52:28 -07:00
|
|
|
},
|
|
|
|
|
2014-04-23 05:34:49 -07:00
|
|
|
/**
|
|
|
|
* Returns the ExperimentInfo for the active experiment, or null
|
|
|
|
* if there is none.
|
|
|
|
*/
|
|
|
|
getActiveExperiment: function () {
|
|
|
|
let experiment = this._getActiveExperiment();
|
|
|
|
if (!experiment) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
let info = {
|
|
|
|
id: experiment.id,
|
|
|
|
name: experiment._name,
|
|
|
|
description: experiment._description,
|
|
|
|
active: experiment.enabled,
|
|
|
|
endDate: experiment.endDate.getTime(),
|
|
|
|
detailURL: experiment._homepageURL,
|
|
|
|
};
|
|
|
|
|
|
|
|
return info;
|
|
|
|
},
|
|
|
|
|
2014-06-25 12:16:13 -07:00
|
|
|
/**
|
|
|
|
* Experiment "branch" support. If an experiment has multiple branches, it
|
|
|
|
* can record the branch with the experiment system and it will
|
|
|
|
* automatically be included in data reporting (FHR/telemetry payloads).
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the experiment branch for the specified experiment ID.
|
|
|
|
* @returns Promise<>
|
|
|
|
*/
|
|
|
|
setExperimentBranch: Task.async(function*(id, branchstr) {
|
|
|
|
yield this._loadTask;
|
|
|
|
let e = this._experiments.get(id);
|
|
|
|
if (!e) {
|
|
|
|
throw new Error("Experiment not found");
|
|
|
|
}
|
|
|
|
e.branch = String(branchstr);
|
2014-08-14 13:21:15 -07:00
|
|
|
this._log.trace("setExperimentBranch(" + id + ", " + e.branch + ") _dirty=" + this._dirty);
|
2014-06-25 12:16:13 -07:00
|
|
|
this._dirty = true;
|
|
|
|
Services.obs.notifyObservers(null, EXPERIMENTS_CHANGED_TOPIC, null);
|
|
|
|
yield this._run();
|
|
|
|
}),
|
|
|
|
/**
|
|
|
|
* Get the branch of the specified experiment. If the experiment is unknown,
|
|
|
|
* throws an error.
|
|
|
|
*
|
|
|
|
* @param id The ID of the experiment. Pass null for the currently running
|
|
|
|
* experiment.
|
|
|
|
* @returns Promise<string|null>
|
|
|
|
* @throws Error if the specified experiment ID is unknown, or if there is no
|
|
|
|
* current experiment.
|
|
|
|
*/
|
|
|
|
getExperimentBranch: Task.async(function*(id=null) {
|
|
|
|
yield this._loadTask;
|
|
|
|
let e;
|
|
|
|
if (id) {
|
|
|
|
e = this._experiments.get(id);
|
|
|
|
if (!e) {
|
|
|
|
throw new Error("Experiment not found");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
e = this._getActiveExperiment();
|
|
|
|
if (e === null) {
|
|
|
|
throw new Error("No active experiment");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return e.branch;
|
|
|
|
}),
|
|
|
|
|
2014-03-20 14:16:00 -07:00
|
|
|
/**
|
|
|
|
* Determine whether another date has the same UTC day as now().
|
|
|
|
*/
|
|
|
|
_dateIsTodayUTC: function (d) {
|
|
|
|
let now = this._policy.now();
|
|
|
|
|
|
|
|
return stripDateToMidnight(now).getTime() == stripDateToMidnight(d).getTime();
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Obtain the entry of the most recent active experiment that was active
|
|
|
|
* today.
|
|
|
|
*
|
|
|
|
* If no experiment was active today, this resolves to nothing.
|
|
|
|
*
|
|
|
|
* Assumption: Only a single experiment can be active at a time.
|
|
|
|
*
|
|
|
|
* @return Promise<object>
|
|
|
|
*/
|
|
|
|
lastActiveToday: function () {
|
|
|
|
return Task.spawn(function* getMostRecentActiveExperimentTask() {
|
|
|
|
let experiments = yield this.getExperiments();
|
|
|
|
|
|
|
|
// Assumption: Ordered chronologically, descending, with active always
|
|
|
|
// first.
|
|
|
|
for (let experiment of experiments) {
|
|
|
|
if (experiment.active) {
|
|
|
|
return experiment;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (experiment.endDate && this._dateIsTodayUTC(experiment.endDate)) {
|
|
|
|
return experiment;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}.bind(this));
|
|
|
|
},
|
|
|
|
|
2014-03-25 10:50:34 -07:00
|
|
|
_run: function() {
|
2014-03-28 12:57:43 -07:00
|
|
|
this._log.trace("_run");
|
2014-03-25 10:50:34 -07:00
|
|
|
this._checkForShutdown();
|
|
|
|
if (!this._mainTask) {
|
2014-05-14 18:12:00 -07:00
|
|
|
this._mainTask = Task.spawn(function*() {
|
|
|
|
try {
|
|
|
|
yield this._main();
|
|
|
|
} catch (e) {
|
2015-12-17 11:42:24 -08:00
|
|
|
// In the CacheWriteError case we want to reschedule
|
|
|
|
if (!(e instanceof CacheWriteError)) {
|
|
|
|
this._log.error("_main caught error: " + e);
|
|
|
|
return;
|
|
|
|
}
|
2014-05-14 18:12:00 -07:00
|
|
|
} finally {
|
2014-03-25 10:50:34 -07:00
|
|
|
this._mainTask = null;
|
|
|
|
}
|
2014-05-14 18:12:00 -07:00
|
|
|
this._log.trace("_main finished, scheduling next run");
|
|
|
|
try {
|
|
|
|
yield this._scheduleNextRun();
|
2015-12-17 11:42:24 -08:00
|
|
|
} catch (ex) {
|
|
|
|
// We error out of tasks after shutdown via this exception.
|
|
|
|
if (!(ex instanceof AlreadyShutdownError)) {
|
|
|
|
throw ex;
|
|
|
|
}
|
2014-05-14 18:12:00 -07:00
|
|
|
}
|
|
|
|
}.bind(this));
|
2014-03-25 10:50:34 -07:00
|
|
|
}
|
|
|
|
return this._mainTask;
|
|
|
|
},
|
|
|
|
|
|
|
|
_main: function*() {
|
|
|
|
do {
|
2014-03-28 12:57:43 -07:00
|
|
|
this._log.trace("_main iteration");
|
2014-03-25 10:50:34 -07:00
|
|
|
yield this._loadTask;
|
2014-04-11 07:29:29 -07:00
|
|
|
if (!gExperimentsEnabled) {
|
|
|
|
this._refresh = false;
|
|
|
|
}
|
|
|
|
|
2014-03-25 10:50:34 -07:00
|
|
|
if (this._refresh) {
|
|
|
|
yield this._loadManifest();
|
|
|
|
}
|
|
|
|
yield this._evaluateExperiments();
|
|
|
|
if (this._dirty) {
|
|
|
|
yield this._saveToCache();
|
|
|
|
}
|
|
|
|
// If somebody called .updateManifest() or disableExperiment()
|
|
|
|
// while we were running, go again right now.
|
|
|
|
}
|
2014-08-14 13:21:15 -07:00
|
|
|
while (this._refresh || this._terminateReason || this._dirty);
|
2014-03-25 10:50:34 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
_loadManifest: function*() {
|
2014-03-28 12:57:43 -07:00
|
|
|
this._log.trace("_loadManifest");
|
2014-03-25 10:50:34 -07:00
|
|
|
let uri = Services.urlFormatter.formatURLPref(PREF_BRANCH + PREF_MANIFEST_URI);
|
|
|
|
|
|
|
|
this._checkForShutdown();
|
|
|
|
|
|
|
|
this._refresh = false;
|
|
|
|
try {
|
|
|
|
let responseText = yield this._httpGetRequest(uri);
|
2014-03-28 12:57:43 -07:00
|
|
|
this._log.trace("_loadManifest() - responseText=\"" + responseText + "\"");
|
2014-03-25 10:50:34 -07:00
|
|
|
|
|
|
|
if (this._shutdown) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let data = JSON.parse(responseText);
|
|
|
|
this._updateExperiments(data);
|
|
|
|
} catch (e) {
|
2014-03-28 12:57:43 -07:00
|
|
|
this._log.error("_loadManifest - failure to fetch/parse manifest (continuing anyway): " + e);
|
2014-03-25 10:50:34 -07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-03-18 14:52:28 -07:00
|
|
|
/**
|
|
|
|
* Fetch an updated list of experiments and trigger experiment updates.
|
|
|
|
* Do only use when experiments are enabled.
|
|
|
|
*
|
|
|
|
* @return Promise<>
|
|
|
|
* The promise is resolved when the manifest and experiment list is updated.
|
|
|
|
*/
|
|
|
|
updateManifest: function () {
|
2014-03-28 12:57:43 -07:00
|
|
|
this._log.trace("updateManifest()");
|
2014-03-18 14:52:28 -07:00
|
|
|
|
|
|
|
if (!gExperimentsEnabled) {
|
|
|
|
return Promise.reject(new Error("experiments are disabled"));
|
|
|
|
}
|
|
|
|
|
2014-03-18 14:52:30 -07:00
|
|
|
if (this._shutdown) {
|
|
|
|
return Promise.reject(Error("uninit() alrady called"));
|
|
|
|
}
|
|
|
|
|
2014-03-25 10:50:34 -07:00
|
|
|
this._refresh = true;
|
|
|
|
return this._run();
|
2014-03-18 14:52:28 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
notify: function (timer) {
|
2014-03-28 12:57:43 -07:00
|
|
|
this._log.trace("notify()");
|
2014-03-18 14:52:30 -07:00
|
|
|
this._checkForShutdown();
|
2014-03-25 10:50:34 -07:00
|
|
|
return this._run();
|
2014-03-18 14:52:28 -07:00
|
|
|
},
|
|
|
|
|
2014-03-21 16:05:29 -07:00
|
|
|
// START OF ADD-ON LISTENERS
|
|
|
|
|
2014-03-18 14:52:28 -07:00
|
|
|
onUninstalled: function (addon) {
|
2014-03-28 12:57:43 -07:00
|
|
|
this._log.trace("onUninstalled() - addon id: " + addon.id);
|
2014-04-17 06:47:36 -07:00
|
|
|
if (gActiveUninstallAddonIDs.has(addon.id)) {
|
2014-03-28 12:57:43 -07:00
|
|
|
this._log.trace("matches pending uninstall");
|
2014-03-25 10:50:34 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
let activeExperiment = this._getActiveExperiment();
|
|
|
|
if (!activeExperiment || activeExperiment._addonId != addon.id) {
|
|
|
|
return;
|
|
|
|
}
|
2014-04-17 06:47:36 -07:00
|
|
|
|
|
|
|
this.disableExperiment(TELEMETRY_LOG.TERMINATION.ADDON_UNINSTALLED);
|
2014-03-24 01:58:57 -07:00
|
|
|
},
|
|
|
|
|
2015-12-17 11:42:24 -08:00
|
|
|
/**
|
|
|
|
* @returns {Boolean} returns false when we cancel the install.
|
|
|
|
*/
|
2014-04-04 15:58:26 -07:00
|
|
|
onInstallStarted: function (install) {
|
|
|
|
if (install.addon.type != "experiment") {
|
2015-12-17 11:42:24 -08:00
|
|
|
return true;
|
2014-04-04 15:58:26 -07:00
|
|
|
}
|
|
|
|
|
2014-04-23 05:34:49 -07:00
|
|
|
this._log.trace("onInstallStarted() - " + install.addon.id);
|
2014-04-23 05:34:48 -07:00
|
|
|
if (install.addon.appDisabled) {
|
|
|
|
// This is a PreviousExperiment
|
2015-12-17 11:42:24 -08:00
|
|
|
return true;
|
2014-04-23 05:34:48 -07:00
|
|
|
}
|
|
|
|
|
2014-04-04 15:58:26 -07:00
|
|
|
// We want to be in control of all experiment add-ons: reject installs
|
|
|
|
// for add-ons that we don't know about.
|
|
|
|
|
|
|
|
// We have a race condition of sorts to worry about here. We have 2
|
|
|
|
// onInstallStarted listeners. This one (the global one) and the one
|
|
|
|
// created as part of ExperimentEntry._installAddon. Because of the order
|
|
|
|
// they are registered in, this one likely executes first. Unfortunately,
|
|
|
|
// this means that the add-on ID is not yet set on the ExperimentEntry.
|
|
|
|
// So, we can't just look at this._trackedAddonIds because the new experiment
|
|
|
|
// will have its add-on ID set to null. We work around this by storing a
|
|
|
|
// identifying field - the source URL of the install - in a module-level
|
|
|
|
// variable (so multiple Experiments instances doesn't cancel each other
|
|
|
|
// out).
|
|
|
|
|
|
|
|
if (this._trackedAddonIds.has(install.addon.id)) {
|
|
|
|
this._log.info("onInstallStarted allowing install because add-on ID " +
|
|
|
|
"tracked by us.");
|
2015-12-17 11:42:24 -08:00
|
|
|
return true;
|
2014-04-04 15:58:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (gActiveInstallURLs.has(install.sourceURI.spec)) {
|
|
|
|
this._log.info("onInstallStarted allowing install because install " +
|
|
|
|
"tracked by us.");
|
2015-12-17 11:42:24 -08:00
|
|
|
return true;
|
2014-04-04 15:58:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
this._log.warn("onInstallStarted cancelling install of unknown " +
|
|
|
|
"experiment add-on: " + install.addon.id);
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
2014-03-21 16:05:29 -07:00
|
|
|
// END OF ADD-ON LISTENERS.
|
|
|
|
|
2014-03-24 01:58:57 -07:00
|
|
|
_getExperimentByAddonId: function (addonId) {
|
|
|
|
for (let [, entry] of this._experiments) {
|
|
|
|
if (entry._addonId === addonId) {
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
2014-03-18 14:52:28 -07:00
|
|
|
/*
|
|
|
|
* Helper function to make HTTP GET requests. Returns a promise that is resolved with
|
|
|
|
* the responseText when the request is complete.
|
|
|
|
*/
|
|
|
|
_httpGetRequest: function (url) {
|
2014-03-28 12:57:43 -07:00
|
|
|
this._log.trace("httpGetRequest(" + url + ")");
|
2014-03-18 14:52:28 -07:00
|
|
|
let xhr = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Ci.nsIXMLHttpRequest);
|
|
|
|
try {
|
|
|
|
xhr.open("GET", url);
|
|
|
|
} catch (e) {
|
2014-03-28 12:57:43 -07:00
|
|
|
this._log.error("httpGetRequest() - Error opening request to " + url + ": " + e);
|
2014-03-18 14:52:28 -07:00
|
|
|
return Promise.reject(new Error("Experiments - Error opening XHR for " + url));
|
|
|
|
}
|
|
|
|
|
2014-09-01 06:26:07 -07:00
|
|
|
this._networkRequest = xhr;
|
2014-03-18 14:52:28 -07:00
|
|
|
let deferred = Promise.defer();
|
|
|
|
|
2014-03-28 11:20:31 -07:00
|
|
|
let log = this._log;
|
2014-09-01 06:26:07 -07:00
|
|
|
let errorhandler = (evt) => {
|
|
|
|
log.error("httpGetRequest::onError() - Error making request to " + url + ": " + evt.type);
|
|
|
|
deferred.reject(new Error("Experiments - XHR error for " + url + " - " + evt.type));
|
|
|
|
this._networkRequest = null;
|
2014-03-18 14:52:29 -07:00
|
|
|
};
|
2014-09-01 06:26:07 -07:00
|
|
|
xhr.onerror = errorhandler;
|
|
|
|
xhr.ontimeout = errorhandler;
|
|
|
|
xhr.onabort = errorhandler;
|
2014-03-18 14:52:28 -07:00
|
|
|
|
2014-09-01 06:26:07 -07:00
|
|
|
xhr.onload = (event) => {
|
2014-03-18 14:52:28 -07:00
|
|
|
if (xhr.status !== 200 && xhr.state !== 0) {
|
2014-03-28 12:57:43 -07:00
|
|
|
log.error("httpGetRequest::onLoad() - Request to " + url + " returned status " + xhr.status);
|
2014-03-18 14:52:28 -07:00
|
|
|
deferred.reject(new Error("Experiments - XHR status for " + url + " is " + xhr.status));
|
2014-09-01 06:26:07 -07:00
|
|
|
this._networkRequest = null;
|
2014-03-18 14:52:28 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
deferred.resolve(xhr.responseText);
|
2014-09-01 06:26:07 -07:00
|
|
|
this._networkRequest = null;
|
2014-03-18 14:52:29 -07:00
|
|
|
};
|
2014-03-18 14:52:28 -07:00
|
|
|
|
|
|
|
if (xhr.channel instanceof Ci.nsISupportsPriority) {
|
|
|
|
xhr.channel.priority = Ci.nsISupportsPriority.PRIORITY_LOWEST;
|
|
|
|
}
|
|
|
|
|
2014-09-01 06:26:07 -07:00
|
|
|
xhr.timeout = MANIFEST_FETCH_TIMEOUT_MSEC;
|
2014-03-18 14:52:28 -07:00
|
|
|
xhr.send(null);
|
|
|
|
return deferred.promise;
|
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Path of the cache file we use in the profile.
|
|
|
|
*/
|
|
|
|
get _cacheFilePath() {
|
|
|
|
return OS.Path.join(OS.Constants.Path.profileDir, FILE_CACHE);
|
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
2014-03-25 10:50:34 -07:00
|
|
|
* Part of the main task to save the cache to disk, called from _main.
|
2014-03-18 14:52:28 -07:00
|
|
|
*/
|
2014-03-25 10:50:34 -07:00
|
|
|
_saveToCache: function* () {
|
2014-03-28 12:57:43 -07:00
|
|
|
this._log.trace("_saveToCache");
|
2014-03-18 14:52:28 -07:00
|
|
|
let path = this._cacheFilePath;
|
2014-03-25 10:50:34 -07:00
|
|
|
this._dirty = false;
|
2014-07-24 14:21:03 -07:00
|
|
|
try {
|
|
|
|
let textData = JSON.stringify({
|
|
|
|
version: CACHE_VERSION,
|
2015-12-17 11:42:24 -08:00
|
|
|
data: [...this._experiments.values()].map(e => e.toJSON()),
|
2014-07-24 14:21:03 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
let encoder = new TextEncoder();
|
|
|
|
let data = encoder.encode(textData);
|
|
|
|
let options = { tmpPath: path + ".tmp", compression: "lz4" };
|
2014-08-28 07:25:05 -07:00
|
|
|
yield this._policy.delayCacheWrite(OS.File.writeAtomic(path, data, options));
|
2014-07-24 14:21:03 -07:00
|
|
|
} catch (e) {
|
|
|
|
// We failed to write the cache, it's still dirty.
|
|
|
|
this._dirty = true;
|
|
|
|
this._log.error("_saveToCache failed and caught error: " + e);
|
2014-08-14 13:21:15 -07:00
|
|
|
throw new CacheWriteError();
|
2014-07-24 14:21:03 -07:00
|
|
|
}
|
|
|
|
|
2014-03-28 12:57:43 -07:00
|
|
|
this._log.debug("_saveToCache saved to " + path);
|
2014-03-18 14:52:28 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
2014-03-25 10:50:34 -07:00
|
|
|
* Task function, load the cached experiments manifest file from disk.
|
2014-03-18 14:52:28 -07:00
|
|
|
*/
|
2014-04-17 06:47:37 -07:00
|
|
|
_loadFromCache: Task.async(function* () {
|
2014-03-28 12:57:43 -07:00
|
|
|
this._log.trace("_loadFromCache");
|
2014-03-18 14:52:28 -07:00
|
|
|
let path = this._cacheFilePath;
|
2014-03-25 10:50:34 -07:00
|
|
|
try {
|
|
|
|
let result = yield loadJSONAsync(path, { compression: "lz4" });
|
|
|
|
this._populateFromCache(result);
|
2015-12-17 11:42:24 -08:00
|
|
|
} catch (e) {
|
|
|
|
if (e instanceof OS.File.Error && e.becauseNoSuchFile) {
|
|
|
|
// No cached manifest yet.
|
|
|
|
this._experiments = new Map();
|
|
|
|
} else {
|
|
|
|
throw e;
|
|
|
|
}
|
2014-03-25 10:50:34 -07:00
|
|
|
}
|
2014-04-17 06:47:37 -07:00
|
|
|
}),
|
2014-03-18 14:52:28 -07:00
|
|
|
|
|
|
|
_populateFromCache: function (data) {
|
2014-03-28 12:57:43 -07:00
|
|
|
this._log.trace("populateFromCache() - data: " + JSON.stringify(data));
|
2014-03-18 14:52:28 -07:00
|
|
|
|
2014-03-25 10:50:34 -07:00
|
|
|
// If the user has a newer cache version than we can understand, we fail
|
|
|
|
// hard; no experiments should be active in this older client.
|
2014-03-18 14:52:28 -07:00
|
|
|
if (CACHE_VERSION !== data.version) {
|
2014-03-25 10:50:34 -07:00
|
|
|
throw new Error("Experiments::_populateFromCache() - invalid cache version");
|
2014-03-18 14:52:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
let experiments = new Map();
|
|
|
|
for (let item of data.data) {
|
|
|
|
let entry = new Experiments.ExperimentEntry(this._policy);
|
|
|
|
if (!entry.initFromCacheData(item)) {
|
|
|
|
continue;
|
|
|
|
}
|
2014-03-20 08:19:19 -07:00
|
|
|
experiments.set(entry.id, entry);
|
2014-03-18 14:52:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
this._experiments = experiments;
|
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Update the experiment entries from the experiments
|
|
|
|
* array in the manifest
|
|
|
|
*/
|
|
|
|
_updateExperiments: function (manifestObject) {
|
2014-03-28 12:57:43 -07:00
|
|
|
this._log.trace("_updateExperiments() - experiments: " + JSON.stringify(manifestObject));
|
2014-03-18 14:52:28 -07:00
|
|
|
|
|
|
|
if (manifestObject.version !== MANIFEST_VERSION) {
|
2014-03-28 12:57:43 -07:00
|
|
|
this._log.warning("updateExperiments() - unsupported version " + manifestObject.version);
|
2014-03-18 14:52:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
let experiments = new Map(); // The new experiments map
|
|
|
|
|
|
|
|
// Collect new and updated experiments.
|
|
|
|
for (let data of manifestObject.experiments) {
|
|
|
|
let entry = this._experiments.get(data.id);
|
|
|
|
|
|
|
|
if (entry) {
|
|
|
|
if (!entry.updateFromManifestData(data)) {
|
2014-03-28 12:57:43 -07:00
|
|
|
this._log.error("updateExperiments() - Invalid manifest data for " + data.id);
|
2014-03-18 14:52:28 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
entry = new Experiments.ExperimentEntry(this._policy);
|
|
|
|
if (!entry.initFromManifestData(data)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entry.shouldDiscard()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-03-20 08:19:19 -07:00
|
|
|
experiments.set(entry.id, entry);
|
2014-03-18 14:52:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we keep experiments that are or were running.
|
|
|
|
// We remove them after KEEP_HISTORY_N_DAYS.
|
|
|
|
for (let [id, entry] of this._experiments) {
|
2014-04-11 07:29:29 -07:00
|
|
|
if (experiments.has(id)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!entry.startDate || entry.shouldDiscard()) {
|
2014-03-28 12:57:43 -07:00
|
|
|
this._log.trace("updateExperiments() - discarding entry for " + id);
|
2014-03-18 14:52:28 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-03-24 01:58:57 -07:00
|
|
|
experiments.set(id, entry);
|
2014-03-18 14:52:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
this._experiments = experiments;
|
2014-03-25 10:50:34 -07:00
|
|
|
this._dirty = true;
|
2014-03-18 14:52:28 -07:00
|
|
|
},
|
|
|
|
|
2014-05-15 06:11:01 -07:00
|
|
|
getActiveExperimentID: function() {
|
|
|
|
if (!this._experiments) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
let e = this._getActiveExperiment();
|
|
|
|
if (!e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return e.id;
|
|
|
|
},
|
|
|
|
|
2014-06-25 12:16:13 -07:00
|
|
|
getActiveExperimentBranch: function() {
|
|
|
|
if (!this._experiments) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
let e = this._getActiveExperiment();
|
|
|
|
if (!e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return e.branch;
|
|
|
|
},
|
|
|
|
|
2014-03-18 14:52:28 -07:00
|
|
|
_getActiveExperiment: function () {
|
2015-12-17 11:42:24 -08:00
|
|
|
let enabled = [...this._experiments.values()].filter(experiment => experiment._enabled);
|
2014-03-18 14:52:28 -07:00
|
|
|
|
|
|
|
if (enabled.length == 1) {
|
|
|
|
return enabled[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (enabled.length > 1) {
|
2014-03-28 12:57:43 -07:00
|
|
|
this._log.error("getActiveExperimentId() - should not have more than 1 active experiment");
|
2014-03-18 14:52:28 -07:00
|
|
|
throw new Error("have more than 1 active experiment");
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-04-17 06:47:36 -07:00
|
|
|
* Disables all active experiments.
|
|
|
|
*
|
2014-03-18 14:52:28 -07:00
|
|
|
* @return Promise<> Promise that will get resolved once the task is done or failed.
|
|
|
|
*/
|
2014-04-17 06:47:36 -07:00
|
|
|
disableExperiment: function (reason) {
|
|
|
|
if (!reason) {
|
|
|
|
throw new Error("Must specify a termination reason.");
|
|
|
|
}
|
2014-03-18 14:52:28 -07:00
|
|
|
|
2014-04-17 06:47:36 -07:00
|
|
|
this._log.trace("disableExperiment()");
|
|
|
|
this._terminateReason = reason;
|
2014-03-25 10:50:34 -07:00
|
|
|
return this._run();
|
2014-03-18 14:52:28 -07:00
|
|
|
},
|
|
|
|
|
2014-04-04 15:58:26 -07:00
|
|
|
/**
|
|
|
|
* The Set of add-on IDs that we know about from manifests.
|
|
|
|
*/
|
|
|
|
get _trackedAddonIds() {
|
|
|
|
if (!this._experiments) {
|
|
|
|
return new Set();
|
|
|
|
}
|
|
|
|
|
2015-12-17 11:42:24 -08:00
|
|
|
return new Set([...this._experiments.values()].map(e => e._addonId));
|
2014-04-04 15:58:26 -07:00
|
|
|
},
|
|
|
|
|
2014-03-18 14:52:28 -07:00
|
|
|
/*
|
2014-03-25 10:50:34 -07:00
|
|
|
* Task function to check applicability of experiments, disable the active
|
|
|
|
* experiment if needed and activate the first applicable candidate.
|
2014-03-18 14:52:28 -07:00
|
|
|
*/
|
2014-03-25 10:50:34 -07:00
|
|
|
_evaluateExperiments: function*() {
|
2014-03-28 12:57:43 -07:00
|
|
|
this._log.trace("_evaluateExperiments");
|
2014-03-18 14:52:28 -07:00
|
|
|
|
2014-03-18 14:52:30 -07:00
|
|
|
this._checkForShutdown();
|
|
|
|
|
2014-03-26 22:20:23 -07:00
|
|
|
// The first thing we do is reconcile our state against what's in the
|
|
|
|
// Addon Manager. It's possible that the Addon Manager knows of experiment
|
|
|
|
// add-ons that we don't. This could happen if an experiment gets installed
|
|
|
|
// when we're not listening or if there is a bug in our synchronization
|
|
|
|
// code.
|
|
|
|
//
|
|
|
|
// We have a few options of what to do with unknown experiment add-ons
|
|
|
|
// coming from the Addon Manager. Ideally, we'd convert these to
|
|
|
|
// ExperimentEntry instances and stuff them inside this._experiments.
|
|
|
|
// However, since ExperimentEntry contain lots of metadata from the
|
|
|
|
// manifest and trying to make up data could be error prone, it's safer
|
|
|
|
// to not try. Furthermore, if an experiment really did come from us, we
|
|
|
|
// should have some record of it. In the end, we decide to discard all
|
|
|
|
// knowledge for these unknown experiment add-ons.
|
|
|
|
let installedExperiments = yield installedExperimentAddons();
|
2014-04-04 15:58:26 -07:00
|
|
|
let expectedAddonIds = this._trackedAddonIds;
|
2015-12-17 11:42:24 -08:00
|
|
|
let unknownAddons = installedExperiments.filter(a => !expectedAddonIds.has(a.id));
|
2014-03-26 22:20:23 -07:00
|
|
|
if (unknownAddons.length) {
|
2014-03-28 12:57:43 -07:00
|
|
|
this._log.warn("_evaluateExperiments() - unknown add-ons in AddonManager: " +
|
2015-12-17 11:42:24 -08:00
|
|
|
unknownAddons.map(a => a.id).join(", "));
|
2014-03-26 22:20:23 -07:00
|
|
|
|
|
|
|
yield uninstallAddons(unknownAddons);
|
|
|
|
}
|
|
|
|
|
2014-03-25 10:50:34 -07:00
|
|
|
let activeExperiment = this._getActiveExperiment();
|
|
|
|
let activeChanged = false;
|
|
|
|
let now = this._policy.now();
|
2014-03-18 14:52:28 -07:00
|
|
|
|
2014-04-15 09:12:26 -07:00
|
|
|
if (!activeExperiment) {
|
|
|
|
// Avoid this pref staying out of sync if there were e.g. crashes.
|
|
|
|
gPrefs.set(PREF_ACTIVE_EXPERIMENT, false);
|
|
|
|
}
|
|
|
|
|
2014-04-17 06:47:36 -07:00
|
|
|
// Ensure the active experiment is in the proper state. This may install,
|
|
|
|
// uninstall, upgrade, or enable the experiment add-on. What exactly is
|
|
|
|
// abstracted away from us by design.
|
2014-03-25 10:50:34 -07:00
|
|
|
if (activeExperiment) {
|
2014-04-17 06:47:36 -07:00
|
|
|
let changes;
|
|
|
|
let shouldStopResult = yield activeExperiment.shouldStop();
|
|
|
|
if (shouldStopResult.shouldStop) {
|
|
|
|
let expireReasons = ["endTime", "maxActiveSeconds"];
|
|
|
|
let kind, reason;
|
|
|
|
|
|
|
|
if (expireReasons.indexOf(shouldStopResult.reason[0]) != -1) {
|
|
|
|
kind = TELEMETRY_LOG.TERMINATION.EXPIRED;
|
|
|
|
reason = null;
|
2014-04-04 15:58:27 -07:00
|
|
|
} else {
|
2014-04-17 06:47:36 -07:00
|
|
|
kind = TELEMETRY_LOG.TERMINATION.RECHECK;
|
|
|
|
reason = shouldStopResult.reason;
|
2014-03-18 14:52:28 -07:00
|
|
|
}
|
2014-04-17 06:47:36 -07:00
|
|
|
changes = yield activeExperiment.stop(kind, reason);
|
|
|
|
}
|
|
|
|
else if (this._terminateReason) {
|
|
|
|
changes = yield activeExperiment.stop(this._terminateReason);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
changes = yield activeExperiment.reconcileAddonState();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (changes) {
|
|
|
|
this._dirty = true;
|
|
|
|
activeChanged = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!activeExperiment._enabled) {
|
|
|
|
activeExperiment = null;
|
|
|
|
activeChanged = true;
|
2014-03-18 14:52:28 -07:00
|
|
|
}
|
2014-03-25 10:50:34 -07:00
|
|
|
}
|
2014-04-17 06:47:36 -07:00
|
|
|
|
2014-03-25 10:50:34 -07:00
|
|
|
this._terminateReason = null;
|
|
|
|
|
2014-04-11 07:29:29 -07:00
|
|
|
if (!activeExperiment && gExperimentsEnabled) {
|
2014-03-25 10:50:34 -07:00
|
|
|
for (let [id, experiment] of this._experiments) {
|
|
|
|
let applicable;
|
|
|
|
let reason = null;
|
|
|
|
try {
|
|
|
|
applicable = yield experiment.isApplicable();
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
applicable = false;
|
|
|
|
reason = e;
|
|
|
|
}
|
2014-03-18 14:52:28 -07:00
|
|
|
|
2014-03-25 10:50:34 -07:00
|
|
|
if (!applicable && reason && reason[0] != "was-active") {
|
|
|
|
// Report this from here to avoid over-reporting.
|
|
|
|
let desc = TELEMETRY_LOG.ACTIVATION;
|
|
|
|
let data = [TELEMETRY_LOG.ACTIVATION.REJECTED, id];
|
|
|
|
data = data.concat(reason);
|
2014-04-28 07:11:26 -07:00
|
|
|
const key = TELEMETRY_LOG.ACTIVATION_KEY;
|
|
|
|
TelemetryLog.log(key, data);
|
|
|
|
this._log.trace("evaluateExperiments() - added " + key + " to TelemetryLog: " + JSON.stringify(data));
|
2014-03-25 10:50:34 -07:00
|
|
|
}
|
2014-03-24 01:58:57 -07:00
|
|
|
|
2014-04-17 06:47:36 -07:00
|
|
|
if (!applicable) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._log.debug("evaluateExperiments() - activating experiment " + id);
|
|
|
|
try {
|
|
|
|
yield experiment.start();
|
|
|
|
activeChanged = true;
|
|
|
|
activeExperiment = experiment;
|
|
|
|
this._dirty = true;
|
|
|
|
break;
|
|
|
|
} catch (e) {
|
|
|
|
// On failure, clean up the best we can and try the next experiment.
|
|
|
|
this._log.error("evaluateExperiments() - Unable to start experiment: " + e.message);
|
|
|
|
experiment._enabled = false;
|
|
|
|
yield experiment.reconcileAddonState();
|
2014-03-18 14:52:28 -07:00
|
|
|
}
|
|
|
|
}
|
2014-03-25 10:50:34 -07:00
|
|
|
}
|
2014-03-18 14:52:28 -07:00
|
|
|
|
2014-04-17 06:47:36 -07:00
|
|
|
gPrefs.set(PREF_ACTIVE_EXPERIMENT, activeExperiment != null);
|
|
|
|
|
2014-05-16 03:56:25 -07:00
|
|
|
if (activeChanged || this._firstEvaluate) {
|
2014-05-06 03:18:10 -07:00
|
|
|
Services.obs.notifyObservers(null, EXPERIMENTS_CHANGED_TOPIC, null);
|
2014-05-16 03:56:25 -07:00
|
|
|
this._firstEvaluate = false;
|
2014-03-25 10:50:34 -07:00
|
|
|
}
|
2014-03-18 14:52:28 -07:00
|
|
|
|
2014-04-17 06:47:36 -07:00
|
|
|
if ("@mozilla.org/toolkit/crash-reporter;1" in Cc && activeExperiment) {
|
2014-05-05 07:31:26 -07:00
|
|
|
try {
|
|
|
|
gCrashReporter.annotateCrashReport("ActiveExperiment", activeExperiment.id);
|
2014-12-23 09:21:14 -08:00
|
|
|
gCrashReporter.annotateCrashReport("ActiveExperimentBranch", activeExperiment.branch);
|
2014-05-05 07:31:26 -07:00
|
|
|
} catch (e) {
|
|
|
|
// It's ok if crash reporting is disabled.
|
|
|
|
}
|
2014-03-25 10:50:34 -07:00
|
|
|
}
|
2014-03-18 14:52:28 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Schedule the soonest re-check of experiment applicability that is needed.
|
|
|
|
*/
|
2014-03-25 10:50:34 -07:00
|
|
|
_scheduleNextRun: function () {
|
2014-03-18 14:52:30 -07:00
|
|
|
this._checkForShutdown();
|
2014-03-24 01:58:57 -07:00
|
|
|
|
|
|
|
if (this._timer) {
|
|
|
|
this._timer.clear();
|
|
|
|
}
|
|
|
|
|
2014-03-18 14:52:28 -07:00
|
|
|
if (!gExperimentsEnabled || this._experiments.length == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-24 01:58:57 -07:00
|
|
|
let time = null;
|
2014-03-18 14:52:28 -07:00
|
|
|
let now = this._policy.now().getTime();
|
2014-08-14 13:21:15 -07:00
|
|
|
if (this._dirty) {
|
|
|
|
// If we failed to write the cache, we should try again periodically
|
|
|
|
time = now + 1000 * CACHE_WRITE_RETRY_DELAY_SEC;
|
|
|
|
}
|
2014-03-18 14:52:28 -07:00
|
|
|
|
|
|
|
for (let [id, experiment] of this._experiments) {
|
|
|
|
let scheduleTime = experiment.getScheduleTime();
|
|
|
|
if (scheduleTime > now) {
|
2014-03-24 01:58:57 -07:00
|
|
|
if (time !== null) {
|
|
|
|
time = Math.min(time, scheduleTime);
|
|
|
|
} else {
|
|
|
|
time = scheduleTime;
|
|
|
|
}
|
2014-03-18 14:52:28 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-24 01:58:57 -07:00
|
|
|
if (time === null) {
|
|
|
|
// No schedule time found.
|
|
|
|
return;
|
2014-03-18 14:52:28 -07:00
|
|
|
}
|
|
|
|
|
2014-03-28 12:57:43 -07:00
|
|
|
this._log.trace("scheduleExperimentEvaluation() - scheduling for "+time+", now: "+now);
|
2014-03-18 14:52:28 -07:00
|
|
|
this._policy.oneshotTimer(this.notify, time - now, this, "_timer");
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Represents a single experiment.
|
|
|
|
*/
|
|
|
|
|
|
|
|
Experiments.ExperimentEntry = function (policy) {
|
|
|
|
this._policy = policy || new Experiments.Policy();
|
2014-03-28 12:57:43 -07:00
|
|
|
this._log = Log.repository.getLoggerWithMessagePrefix(
|
2014-03-28 13:04:09 -07:00
|
|
|
"Browser.Experiments.Experiments",
|
|
|
|
"ExperimentEntry #" + gExperimentEntryCounter++ + "::");
|
2014-03-18 14:52:28 -07:00
|
|
|
|
2014-04-17 06:47:36 -07:00
|
|
|
// Is the experiment supposed to be running.
|
2014-03-18 14:52:28 -07:00
|
|
|
this._enabled = false;
|
|
|
|
// When this experiment was started, if ever.
|
|
|
|
this._startDate = null;
|
|
|
|
// When this experiment was ended, if ever.
|
|
|
|
this._endDate = null;
|
|
|
|
// The condition data from the manifest.
|
|
|
|
this._manifestData = null;
|
|
|
|
// For an active experiment, signifies whether we need to update the xpi.
|
|
|
|
this._needsUpdate = false;
|
|
|
|
// A random sample value for comparison against the manifest conditions.
|
|
|
|
this._randomValue = null;
|
|
|
|
// When this entry was last changed for respecting history retention duration.
|
|
|
|
this._lastChangedDate = null;
|
|
|
|
// Has this experiment failed to activate before?
|
|
|
|
this._failedStart = false;
|
2014-06-25 12:16:13 -07:00
|
|
|
// The experiment branch
|
|
|
|
this._branch = null;
|
2014-03-18 14:52:28 -07:00
|
|
|
|
|
|
|
// We grab these from the addon after download.
|
|
|
|
this._name = null;
|
|
|
|
this._description = null;
|
|
|
|
this._homepageURL = null;
|
2014-03-24 01:58:57 -07:00
|
|
|
this._addonId = null;
|
2014-03-18 14:52:28 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
Experiments.ExperimentEntry.prototype = {
|
|
|
|
MANIFEST_REQUIRED_FIELDS: new Set([
|
|
|
|
"id",
|
|
|
|
"xpiURL",
|
|
|
|
"xpiHash",
|
|
|
|
"startTime",
|
|
|
|
"endTime",
|
|
|
|
"maxActiveSeconds",
|
|
|
|
"appName",
|
|
|
|
"channel",
|
|
|
|
]),
|
|
|
|
|
|
|
|
MANIFEST_OPTIONAL_FIELDS: new Set([
|
|
|
|
"maxStartTime",
|
|
|
|
"minVersion",
|
|
|
|
"maxVersion",
|
|
|
|
"version",
|
|
|
|
"minBuildID",
|
|
|
|
"maxBuildID",
|
|
|
|
"buildIDs",
|
|
|
|
"os",
|
|
|
|
"locale",
|
|
|
|
"sample",
|
|
|
|
"disabled",
|
|
|
|
"frozen",
|
|
|
|
"jsfilter",
|
|
|
|
]),
|
|
|
|
|
|
|
|
SERIALIZE_KEYS: new Set([
|
|
|
|
"_enabled",
|
|
|
|
"_manifestData",
|
|
|
|
"_needsUpdate",
|
|
|
|
"_randomValue",
|
|
|
|
"_failedStart",
|
|
|
|
"_name",
|
|
|
|
"_description",
|
|
|
|
"_homepageURL",
|
2014-03-25 09:50:24 -07:00
|
|
|
"_addonId",
|
2014-03-18 14:52:28 -07:00
|
|
|
"_startDate",
|
|
|
|
"_endDate",
|
2014-06-25 12:16:13 -07:00
|
|
|
"_branch",
|
2014-03-18 14:52:28 -07:00
|
|
|
]),
|
|
|
|
|
|
|
|
DATE_KEYS: new Set([
|
|
|
|
"_startDate",
|
|
|
|
"_endDate",
|
|
|
|
]),
|
|
|
|
|
2014-06-25 12:16:13 -07:00
|
|
|
UPGRADE_KEYS: new Map([
|
|
|
|
["_branch", null],
|
|
|
|
]),
|
|
|
|
|
2014-04-17 06:47:36 -07:00
|
|
|
ADDON_CHANGE_NONE: 0,
|
|
|
|
ADDON_CHANGE_INSTALL: 1,
|
|
|
|
ADDON_CHANGE_UNINSTALL: 2,
|
|
|
|
ADDON_CHANGE_ENABLE: 4,
|
|
|
|
|
2014-03-18 14:52:28 -07:00
|
|
|
/*
|
|
|
|
* Initialize entry from the manifest.
|
|
|
|
* @param data The experiment data from the manifest.
|
|
|
|
* @return boolean Whether initialization succeeded.
|
|
|
|
*/
|
|
|
|
initFromManifestData: function (data) {
|
|
|
|
if (!this._isManifestDataValid(data)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._manifestData = data;
|
|
|
|
|
|
|
|
this._randomValue = this._policy.random();
|
|
|
|
this._lastChangedDate = this._policy.now();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
|
|
|
get enabled() {
|
|
|
|
return this._enabled;
|
|
|
|
},
|
|
|
|
|
|
|
|
get id() {
|
|
|
|
return this._manifestData.id;
|
|
|
|
},
|
|
|
|
|
2014-06-25 12:16:13 -07:00
|
|
|
get branch() {
|
|
|
|
return this._branch;
|
|
|
|
},
|
|
|
|
|
|
|
|
set branch(v) {
|
|
|
|
this._branch = v;
|
|
|
|
},
|
|
|
|
|
2014-03-18 14:52:28 -07:00
|
|
|
get startDate() {
|
|
|
|
return this._startDate;
|
|
|
|
},
|
|
|
|
|
|
|
|
get endDate() {
|
|
|
|
if (!this._startDate) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
let endTime = 0;
|
|
|
|
|
|
|
|
if (!this._enabled) {
|
|
|
|
return this._endDate;
|
|
|
|
}
|
|
|
|
|
|
|
|
let maxActiveMs = 1000 * this._manifestData.maxActiveSeconds;
|
|
|
|
endTime = Math.min(1000 * this._manifestData.endTime,
|
|
|
|
this._startDate.getTime() + maxActiveMs);
|
|
|
|
|
|
|
|
return new Date(endTime);
|
|
|
|
},
|
|
|
|
|
|
|
|
get needsUpdate() {
|
|
|
|
return this._needsUpdate;
|
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize entry from the cache.
|
|
|
|
* @param data The entry data from the cache.
|
|
|
|
* @return boolean Whether initialization succeeded.
|
|
|
|
*/
|
|
|
|
initFromCacheData: function (data) {
|
2014-06-25 12:16:13 -07:00
|
|
|
for (let [key, dval] of this.UPGRADE_KEYS) {
|
|
|
|
if (!(key in data)) {
|
2014-06-27 10:44:21 -07:00
|
|
|
data[key] = dval;
|
2014-06-25 12:16:13 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-18 14:52:28 -07:00
|
|
|
for (let key of this.SERIALIZE_KEYS) {
|
2014-03-25 09:50:24 -07:00
|
|
|
if (!(key in data) && !this.DATE_KEYS.has(key)) {
|
2014-03-28 12:57:43 -07:00
|
|
|
this._log.error("initFromCacheData() - missing required key " + key);
|
2014-03-18 14:52:28 -07:00
|
|
|
return false;
|
|
|
|
}
|
2016-02-03 17:27:36 -08:00
|
|
|
}
|
2014-03-18 14:52:28 -07:00
|
|
|
|
|
|
|
if (!this._isManifestDataValid(data._manifestData)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-03-25 09:50:24 -07:00
|
|
|
// Dates are restored separately from epoch ms, everything else is just
|
|
|
|
// copied in.
|
|
|
|
|
2014-03-18 14:52:28 -07:00
|
|
|
this.SERIALIZE_KEYS.forEach(key => {
|
2014-03-25 09:50:24 -07:00
|
|
|
if (!this.DATE_KEYS.has(key)) {
|
|
|
|
this[key] = data[key];
|
|
|
|
}
|
2014-03-18 14:52:28 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
this.DATE_KEYS.forEach(key => {
|
2014-03-25 09:50:24 -07:00
|
|
|
if (key in data) {
|
2014-03-18 14:52:28 -07:00
|
|
|
let date = new Date();
|
2014-03-25 09:50:24 -07:00
|
|
|
date.setTime(data[key]);
|
2014-03-18 14:52:28 -07:00
|
|
|
this[key] = date;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-03-25 09:50:24 -07:00
|
|
|
this._lastChangedDate = this._policy.now();
|
|
|
|
|
2014-03-18 14:52:28 -07:00
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns a JSON representation of this object.
|
|
|
|
*/
|
|
|
|
toJSON: function () {
|
|
|
|
let obj = {};
|
|
|
|
|
2014-03-25 09:50:24 -07:00
|
|
|
// Dates are serialized separately as epoch ms.
|
|
|
|
|
2014-03-18 14:52:28 -07:00
|
|
|
this.SERIALIZE_KEYS.forEach(key => {
|
|
|
|
if (!this.DATE_KEYS.has(key)) {
|
|
|
|
obj[key] = this[key];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.DATE_KEYS.forEach(key => {
|
2014-03-25 09:50:24 -07:00
|
|
|
if (this[key]) {
|
|
|
|
obj[key] = this[key].getTime();
|
|
|
|
}
|
2014-03-18 14:52:28 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Update from the experiment data from the manifest.
|
|
|
|
* @param data The experiment data from the manifest.
|
|
|
|
* @return boolean Whether updating succeeded.
|
|
|
|
*/
|
|
|
|
updateFromManifestData: function (data) {
|
|
|
|
let old = this._manifestData;
|
|
|
|
|
|
|
|
if (!this._isManifestDataValid(data)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._enabled) {
|
|
|
|
if (old.xpiHash !== data.xpiHash) {
|
|
|
|
// A changed hash means we need to update active experiments.
|
|
|
|
this._needsUpdate = true;
|
|
|
|
}
|
|
|
|
} else if (this._failedStart &&
|
|
|
|
(old.xpiHash !== data.xpiHash) ||
|
|
|
|
(old.xpiURL !== data.xpiURL)) {
|
|
|
|
// Retry installation of previously invalid experiments
|
|
|
|
// if hash or url changed.
|
|
|
|
this._failedStart = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._manifestData = data;
|
|
|
|
this._lastChangedDate = this._policy.now();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Is this experiment applicable?
|
|
|
|
* @return Promise<> Resolved if the experiment is applicable.
|
|
|
|
* If it is not applicable it is rejected with
|
|
|
|
* a Promise<string> which contains the reason.
|
|
|
|
*/
|
|
|
|
isApplicable: function () {
|
|
|
|
let versionCmp = Cc["@mozilla.org/xpcom/version-comparator;1"]
|
|
|
|
.getService(Ci.nsIVersionComparator);
|
|
|
|
let app = Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULAppInfo);
|
|
|
|
let runtime = Cc["@mozilla.org/xre/app-info;1"]
|
|
|
|
.getService(Ci.nsIXULRuntime);
|
|
|
|
|
2014-03-26 08:35:59 -07:00
|
|
|
let locale = this._policy.locale();
|
2014-03-18 14:52:28 -07:00
|
|
|
let channel = this._policy.updatechannel();
|
|
|
|
let data = this._manifestData;
|
|
|
|
|
|
|
|
let now = this._policy.now() / 1000; // The manifest times are in seconds.
|
|
|
|
let minActive = MIN_EXPERIMENT_ACTIVE_SECONDS;
|
|
|
|
let maxActive = data.maxActiveSeconds || 0;
|
|
|
|
let startSec = (this.startDate || 0) / 1000;
|
|
|
|
|
2014-03-28 12:57:43 -07:00
|
|
|
this._log.trace("isApplicable() - now=" + now
|
2014-03-28 11:20:31 -07:00
|
|
|
+ ", randomValue=" + this._randomValue
|
|
|
|
+ ", data=" + JSON.stringify(this._manifestData));
|
2014-03-18 14:52:28 -07:00
|
|
|
|
|
|
|
// Not applicable if it already ran.
|
|
|
|
|
|
|
|
if (!this.enabled && this._endDate) {
|
2014-03-24 01:58:57 -07:00
|
|
|
return Promise.reject(["was-active"]);
|
2014-03-18 14:52:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Define and run the condition checks.
|
|
|
|
|
|
|
|
let simpleChecks = [
|
|
|
|
{ name: "failedStart",
|
|
|
|
condition: () => !this._failedStart },
|
|
|
|
{ name: "disabled",
|
|
|
|
condition: () => !data.disabled },
|
|
|
|
{ name: "frozen",
|
|
|
|
condition: () => !data.frozen || this._enabled },
|
|
|
|
{ name: "startTime",
|
|
|
|
condition: () => now >= data.startTime },
|
|
|
|
{ name: "endTime",
|
|
|
|
condition: () => now < data.endTime },
|
|
|
|
{ name: "maxStartTime",
|
2014-07-18 07:53:20 -07:00
|
|
|
condition: () => this._startDate || !data.maxStartTime || now <= data.maxStartTime },
|
2014-03-18 14:52:28 -07:00
|
|
|
{ name: "maxActiveSeconds",
|
|
|
|
condition: () => !this._startDate || now <= (startSec + maxActive) },
|
|
|
|
{ name: "appName",
|
2014-03-26 08:35:59 -07:00
|
|
|
condition: () => !data.appName || data.appName.indexOf(app.name) != -1 },
|
2014-03-18 14:52:28 -07:00
|
|
|
{ name: "minBuildID",
|
|
|
|
condition: () => !data.minBuildID || app.platformBuildID >= data.minBuildID },
|
|
|
|
{ name: "maxBuildID",
|
|
|
|
condition: () => !data.maxBuildID || app.platformBuildID <= data.maxBuildID },
|
|
|
|
{ name: "buildIDs",
|
|
|
|
condition: () => !data.buildIDs || data.buildIDs.indexOf(app.platformBuildID) != -1 },
|
|
|
|
{ name: "os",
|
|
|
|
condition: () => !data.os || data.os.indexOf(runtime.OS) != -1 },
|
|
|
|
{ name: "channel",
|
|
|
|
condition: () => !data.channel || data.channel.indexOf(channel) != -1 },
|
|
|
|
{ name: "locale",
|
|
|
|
condition: () => !data.locale || data.locale.indexOf(locale) != -1 },
|
|
|
|
{ name: "sample",
|
2014-03-26 08:35:59 -07:00
|
|
|
condition: () => data.sample === undefined || this._randomValue <= data.sample },
|
2014-03-18 14:52:28 -07:00
|
|
|
{ name: "version",
|
2014-03-26 08:35:59 -07:00
|
|
|
condition: () => !data.version || data.version.indexOf(app.version) != -1 },
|
2014-03-18 14:52:28 -07:00
|
|
|
{ name: "minVersion",
|
|
|
|
condition: () => !data.minVersion || versionCmp.compare(app.version, data.minVersion) >= 0 },
|
|
|
|
{ name: "maxVersion",
|
|
|
|
condition: () => !data.maxVersion || versionCmp.compare(app.version, data.maxVersion) <= 0 },
|
|
|
|
];
|
|
|
|
|
|
|
|
for (let check of simpleChecks) {
|
|
|
|
let result = check.condition();
|
|
|
|
if (!result) {
|
2014-03-28 12:57:43 -07:00
|
|
|
this._log.debug("isApplicable() - id="
|
2014-03-28 11:20:31 -07:00
|
|
|
+ data.id + " - test '" + check.name + "' failed");
|
2014-03-24 01:58:57 -07:00
|
|
|
return Promise.reject([check.name]);
|
2014-03-18 14:52:28 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data.jsfilter) {
|
|
|
|
return this._runFilterFunction(data.jsfilter);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.resolve(true);
|
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Run the jsfilter function from the manifest in a sandbox and return the
|
|
|
|
* result (forced to boolean).
|
|
|
|
*/
|
2015-06-23 11:56:11 -07:00
|
|
|
_runFilterFunction: Task.async(function* (jsfilter) {
|
2014-03-28 12:57:43 -07:00
|
|
|
this._log.trace("runFilterFunction() - filter: " + jsfilter);
|
2014-03-18 14:52:28 -07:00
|
|
|
|
2015-06-23 11:56:11 -07:00
|
|
|
const nullprincipal = Cc["@mozilla.org/nullprincipal;1"].createInstance(Ci.nsIPrincipal);
|
|
|
|
let options = {
|
|
|
|
sandboxName: "telemetry experiments jsfilter sandbox",
|
|
|
|
wantComponents: false,
|
|
|
|
};
|
2014-03-18 14:52:28 -07:00
|
|
|
|
2015-06-23 11:56:11 -07:00
|
|
|
let sandbox = Cu.Sandbox(nullprincipal, options);
|
|
|
|
try {
|
|
|
|
Cu.evalInSandbox(jsfilter, sandbox);
|
|
|
|
} catch (e) {
|
|
|
|
this._log.error("runFilterFunction() - failed to eval jsfilter: " + e.message);
|
|
|
|
throw ["jsfilter-evalfailed"];
|
|
|
|
}
|
2014-03-18 14:52:28 -07:00
|
|
|
|
2015-06-23 11:56:11 -07:00
|
|
|
let currentEnvironment = yield TelemetryEnvironment.onInitialized();
|
2014-03-18 14:52:29 -07:00
|
|
|
|
2015-06-23 11:56:11 -07:00
|
|
|
Object.defineProperty(sandbox, "_e",
|
|
|
|
{ get: () => Cu.cloneInto(currentEnvironment, sandbox) });
|
|
|
|
|
|
|
|
let result = false;
|
|
|
|
try {
|
|
|
|
result = !!Cu.evalInSandbox("filter({get telemetryEnvironment() { return _e; } })", sandbox);
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
this._log.debug("runFilterFunction() - filter function failed: "
|
2014-03-18 14:52:29 -07:00
|
|
|
+ e.message + ", " + e.stack);
|
2015-06-23 11:56:11 -07:00
|
|
|
throw ["jsfilter-threw", e.message];
|
|
|
|
}
|
|
|
|
finally {
|
|
|
|
Cu.nukeSandbox(sandbox);
|
|
|
|
}
|
2014-03-18 14:52:28 -07:00
|
|
|
|
2015-06-23 11:56:11 -07:00
|
|
|
if (!result) {
|
|
|
|
throw ["jsfilter-false"];
|
|
|
|
}
|
2014-03-24 01:58:57 -07:00
|
|
|
|
2015-06-23 11:56:11 -07:00
|
|
|
return true;
|
|
|
|
}),
|
2014-03-18 14:52:28 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Start running the experiment.
|
2014-04-17 06:47:36 -07:00
|
|
|
*
|
2014-03-18 14:52:28 -07:00
|
|
|
* @return Promise<> Resolved when the operation is complete.
|
|
|
|
*/
|
2014-04-17 06:47:36 -07:00
|
|
|
start: Task.async(function* () {
|
2014-03-28 12:57:43 -07:00
|
|
|
this._log.trace("start() for " + this.id);
|
2014-03-27 14:26:59 -07:00
|
|
|
|
2014-04-17 06:47:36 -07:00
|
|
|
this._enabled = true;
|
|
|
|
return yield this.reconcileAddonState();
|
|
|
|
}),
|
2014-03-27 14:26:59 -07:00
|
|
|
|
|
|
|
// Async install of the addon for this experiment, part of the start task above.
|
2014-04-17 06:47:36 -07:00
|
|
|
_installAddon: Task.async(function* () {
|
2014-03-18 14:52:28 -07:00
|
|
|
let deferred = Promise.defer();
|
|
|
|
|
2014-04-17 06:47:36 -07:00
|
|
|
let hash = this._policy.ignoreHashes ? null : this._manifestData.xpiHash;
|
|
|
|
|
|
|
|
let install = yield addonInstallForURL(this._manifestData.xpiURL, hash);
|
2014-04-04 15:58:26 -07:00
|
|
|
gActiveInstallURLs.add(install.sourceURI.spec);
|
|
|
|
|
2014-03-27 14:26:59 -07:00
|
|
|
let failureHandler = (install, handler) => {
|
|
|
|
let message = "AddonInstall " + handler + " for " + this.id + ", state=" +
|
|
|
|
(install.state || "?") + ", error=" + install.error;
|
2014-03-28 12:57:43 -07:00
|
|
|
this._log.error("_installAddon() - " + message);
|
2014-03-27 14:26:59 -07:00
|
|
|
this._failedStart = true;
|
2014-04-04 15:58:26 -07:00
|
|
|
gActiveInstallURLs.delete(install.sourceURI.spec);
|
2014-03-18 14:52:28 -07:00
|
|
|
|
2014-03-27 14:26:59 -07:00
|
|
|
TelemetryLog.log(TELEMETRY_LOG.ACTIVATION_KEY,
|
|
|
|
[TELEMETRY_LOG.ACTIVATION.INSTALL_FAILURE, this.id]);
|
2014-03-18 14:52:28 -07:00
|
|
|
|
2014-03-27 14:26:59 -07:00
|
|
|
deferred.reject(new Error(message));
|
|
|
|
};
|
2014-03-18 14:52:28 -07:00
|
|
|
|
2014-03-27 14:26:59 -07:00
|
|
|
let listener = {
|
2014-04-04 15:58:27 -07:00
|
|
|
_expectedID: null,
|
|
|
|
|
2014-03-28 21:45:10 -07:00
|
|
|
onDownloadEnded: install => {
|
2014-03-28 12:57:43 -07:00
|
|
|
this._log.trace("_installAddon() - onDownloadEnded for " + this.id);
|
2014-03-18 14:52:28 -07:00
|
|
|
|
2014-03-27 14:26:59 -07:00
|
|
|
if (install.existingAddon) {
|
2014-03-28 12:57:43 -07:00
|
|
|
this._log.warn("_installAddon() - onDownloadEnded, addon already installed");
|
2014-03-27 14:26:59 -07:00
|
|
|
}
|
2014-03-18 14:52:28 -07:00
|
|
|
|
2014-03-27 14:26:59 -07:00
|
|
|
if (install.addon.type !== "experiment") {
|
2014-03-28 12:57:43 -07:00
|
|
|
this._log.error("_installAddon() - onDownloadEnded, wrong addon type");
|
2014-03-27 14:26:59 -07:00
|
|
|
install.cancel();
|
|
|
|
}
|
2014-03-28 21:45:10 -07:00
|
|
|
},
|
2014-03-18 14:52:28 -07:00
|
|
|
|
2014-03-28 21:45:10 -07:00
|
|
|
onInstallStarted: install => {
|
2014-03-28 12:57:43 -07:00
|
|
|
this._log.trace("_installAddon() - onInstallStarted for " + this.id);
|
2014-03-18 14:52:28 -07:00
|
|
|
|
2014-03-27 14:26:59 -07:00
|
|
|
if (install.existingAddon) {
|
2014-03-28 12:57:43 -07:00
|
|
|
this._log.warn("_installAddon() - onInstallStarted, addon already installed");
|
2014-03-27 14:26:59 -07:00
|
|
|
}
|
2014-03-18 14:52:28 -07:00
|
|
|
|
2014-03-27 14:26:59 -07:00
|
|
|
if (install.addon.type !== "experiment") {
|
2014-03-28 12:57:43 -07:00
|
|
|
this._log.error("_installAddon() - onInstallStarted, wrong addon type");
|
2014-03-27 14:26:59 -07:00
|
|
|
return false;
|
|
|
|
}
|
2014-03-28 21:45:10 -07:00
|
|
|
},
|
2014-03-21 16:05:29 -07:00
|
|
|
|
2014-03-28 21:45:10 -07:00
|
|
|
onInstallEnded: install => {
|
2014-03-28 12:57:43 -07:00
|
|
|
this._log.trace("_installAddon() - install ended for " + this.id);
|
2014-04-04 15:58:26 -07:00
|
|
|
gActiveInstallURLs.delete(install.sourceURI.spec);
|
|
|
|
|
2014-03-27 14:26:59 -07:00
|
|
|
this._lastChangedDate = this._policy.now();
|
|
|
|
this._startDate = this._policy.now();
|
|
|
|
this._enabled = true;
|
|
|
|
|
|
|
|
TelemetryLog.log(TELEMETRY_LOG.ACTIVATION_KEY,
|
|
|
|
[TELEMETRY_LOG.ACTIVATION.ACTIVATED, this.id]);
|
2014-03-18 14:52:28 -07:00
|
|
|
|
2014-03-27 14:26:59 -07:00
|
|
|
let addon = install.addon;
|
|
|
|
this._name = addon.name;
|
|
|
|
this._addonId = addon.id;
|
|
|
|
this._description = addon.description || "";
|
|
|
|
this._homepageURL = addon.homepageURL || "";
|
|
|
|
|
2014-04-04 15:58:27 -07:00
|
|
|
// Experiment add-ons default to userDisabled=true. Enable if needed.
|
|
|
|
if (addon.userDisabled) {
|
|
|
|
this._log.trace("Add-on is disabled. Enabling.");
|
|
|
|
listener._expectedID = addon.id;
|
|
|
|
AddonManager.addAddonListener(listener);
|
|
|
|
addon.userDisabled = false;
|
|
|
|
} else {
|
|
|
|
this._log.trace("Add-on is enabled. start() completed.");
|
|
|
|
deferred.resolve();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
onEnabled: addon => {
|
|
|
|
this._log.info("onEnabled() for " + addon.id);
|
|
|
|
|
|
|
|
if (addon.id != listener._expectedID) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
AddonManager.removeAddonListener(listener);
|
2014-03-27 14:26:59 -07:00
|
|
|
deferred.resolve();
|
2014-03-28 21:45:10 -07:00
|
|
|
},
|
2014-03-28 12:17:04 -07:00
|
|
|
};
|
2014-03-27 14:26:59 -07:00
|
|
|
|
2014-03-28 21:45:10 -07:00
|
|
|
["onDownloadCancelled", "onDownloadFailed", "onInstallCancelled", "onInstallFailed"]
|
|
|
|
.forEach(what => {
|
|
|
|
listener[what] = install => failureHandler(install, what)
|
|
|
|
});
|
|
|
|
|
2014-03-27 14:26:59 -07:00
|
|
|
install.addListener(listener);
|
|
|
|
install.install();
|
|
|
|
|
2014-04-17 06:47:36 -07:00
|
|
|
return yield deferred.promise;
|
|
|
|
}),
|
2014-03-18 14:52:28 -07:00
|
|
|
|
2014-04-17 06:47:36 -07:00
|
|
|
/**
|
2014-03-18 14:52:28 -07:00
|
|
|
* Stop running the experiment if it is active.
|
2014-04-17 06:47:36 -07:00
|
|
|
*
|
|
|
|
* @param terminationKind (optional)
|
|
|
|
* The termination kind, e.g. ADDON_UNINSTALLED or EXPIRED.
|
|
|
|
* @param terminationReason (optional)
|
|
|
|
* The termination reason details for termination kind RECHECK.
|
2014-03-18 14:52:28 -07:00
|
|
|
* @return Promise<> Resolved when the operation is complete.
|
|
|
|
*/
|
2014-04-17 06:47:36 -07:00
|
|
|
stop: Task.async(function* (terminationKind, terminationReason) {
|
2014-03-28 12:57:43 -07:00
|
|
|
this._log.trace("stop() - id=" + this.id + ", terminationKind=" + terminationKind);
|
2014-03-18 14:52:28 -07:00
|
|
|
if (!this._enabled) {
|
2014-04-17 06:47:36 -07:00
|
|
|
throw new Error("Must not call stop() on an inactive experiment.");
|
2014-03-18 14:52:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
this._enabled = false;
|
2014-04-17 06:47:36 -07:00
|
|
|
let now = this._policy.now();
|
|
|
|
this._lastChangedDate = now;
|
|
|
|
this._endDate = now;
|
2014-04-29 09:20:56 -07:00
|
|
|
|
2014-04-29 09:21:06 -07:00
|
|
|
let changes = yield this.reconcileAddonState();
|
|
|
|
this._logTermination(terminationKind, terminationReason);
|
|
|
|
|
2014-04-29 09:20:56 -07:00
|
|
|
if (terminationKind == TELEMETRY_LOG.TERMINATION.ADDON_UNINSTALLED) {
|
|
|
|
changes |= this.ADDON_CHANGE_UNINSTALL;
|
|
|
|
}
|
2014-03-18 14:52:28 -07:00
|
|
|
|
2014-04-17 06:47:36 -07:00
|
|
|
return changes;
|
|
|
|
}),
|
2014-03-18 14:52:28 -07:00
|
|
|
|
2014-04-04 15:58:27 -07:00
|
|
|
/**
|
2014-04-17 06:47:36 -07:00
|
|
|
* Reconcile the state of the add-on against what it's supposed to be.
|
2014-04-04 15:58:27 -07:00
|
|
|
*
|
2014-04-17 06:47:36 -07:00
|
|
|
* If we are active, ensure the add-on is enabled and up to date.
|
2014-04-04 15:58:27 -07:00
|
|
|
*
|
2014-04-17 06:47:36 -07:00
|
|
|
* If we are inactive, ensure the add-on is not installed.
|
2014-04-04 15:58:27 -07:00
|
|
|
*/
|
2014-04-17 06:47:36 -07:00
|
|
|
reconcileAddonState: Task.async(function* () {
|
|
|
|
this._log.trace("reconcileAddonState()");
|
|
|
|
|
|
|
|
if (!this._enabled) {
|
|
|
|
if (!this._addonId) {
|
|
|
|
this._log.trace("reconcileAddonState() - Experiment is not enabled and " +
|
|
|
|
"has no add-on. Doing nothing.");
|
|
|
|
return this.ADDON_CHANGE_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
let addon = yield this._getAddon();
|
|
|
|
if (!addon) {
|
|
|
|
this._log.trace("reconcileAddonState() - Inactive experiment has no " +
|
|
|
|
"add-on. Doing nothing.");
|
|
|
|
return this.ADDON_CHANGE_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._log.info("reconcileAddonState() - Uninstalling add-on for inactive " +
|
|
|
|
"experiment: " + addon.id);
|
|
|
|
gActiveUninstallAddonIDs.add(addon.id);
|
|
|
|
yield uninstallAddons([addon]);
|
|
|
|
gActiveUninstallAddonIDs.delete(addon.id);
|
|
|
|
return this.ADDON_CHANGE_UNINSTALL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we get here, we're supposed to be active.
|
|
|
|
|
|
|
|
let changes = 0;
|
|
|
|
|
|
|
|
// That requires an add-on.
|
|
|
|
let currentAddon = yield this._getAddon();
|
|
|
|
|
|
|
|
// If we have an add-on but it isn't up to date, uninstall it
|
|
|
|
// (to prepare for reinstall).
|
|
|
|
if (currentAddon && this._needsUpdate) {
|
|
|
|
this._log.info("reconcileAddonState() - Uninstalling add-on because update " +
|
|
|
|
"needed: " + currentAddon.id);
|
|
|
|
gActiveUninstallAddonIDs.add(currentAddon.id);
|
|
|
|
yield uninstallAddons([currentAddon]);
|
|
|
|
gActiveUninstallAddonIDs.delete(currentAddon.id);
|
|
|
|
changes |= this.ADDON_CHANGE_UNINSTALL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!currentAddon || this._needsUpdate) {
|
|
|
|
this._log.info("reconcileAddonState() - Installing add-on.");
|
|
|
|
yield this._installAddon();
|
|
|
|
changes |= this.ADDON_CHANGE_INSTALL;
|
|
|
|
}
|
2014-04-04 15:58:27 -07:00
|
|
|
|
|
|
|
let addon = yield this._getAddon();
|
|
|
|
if (!addon) {
|
2014-04-17 06:47:36 -07:00
|
|
|
throw new Error("Could not obtain add-on for experiment that should be " +
|
|
|
|
"enabled.");
|
2014-04-04 15:58:27 -07:00
|
|
|
}
|
|
|
|
|
2014-04-17 06:47:36 -07:00
|
|
|
// If we have the add-on and it is enabled, we are done.
|
2014-04-04 15:58:27 -07:00
|
|
|
if (!addon.userDisabled) {
|
2014-04-17 06:47:36 -07:00
|
|
|
return changes;
|
2014-04-04 15:58:27 -07:00
|
|
|
}
|
|
|
|
|
2015-11-03 05:26:13 -08:00
|
|
|
// Check permissions to see if we can enable the addon.
|
|
|
|
if (!(addon.permissions & AddonManager.PERM_CAN_ENABLE)) {
|
|
|
|
throw new Error("Don't have permission to enable addon " + addon.id + ", perm=" + addon.permission);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Experiment addons should not require a restart.
|
|
|
|
if (!!(addon.operationsRequiringRestart & AddonManager.OP_NEEDS_RESTART_ENABLE)) {
|
|
|
|
throw new Error("Experiment addon requires a restart: " + addon.id);
|
|
|
|
}
|
|
|
|
|
2014-04-04 15:58:27 -07:00
|
|
|
let deferred = Promise.defer();
|
|
|
|
|
2014-04-17 06:47:36 -07:00
|
|
|
// Else we need to enable it.
|
2014-04-04 15:58:27 -07:00
|
|
|
let listener = {
|
|
|
|
onEnabled: enabledAddon => {
|
|
|
|
if (enabledAddon.id != addon.id) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
AddonManager.removeAddonListener(listener);
|
|
|
|
deferred.resolve();
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2015-11-03 05:26:13 -08:00
|
|
|
for (let handler of ["onDisabled", "onOperationCancelled", "onUninstalled"]) {
|
|
|
|
listener[handler] = (evtAddon) => {
|
|
|
|
if (evtAddon.id != addon.id) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
AddonManager.removeAddonListener(listener);
|
|
|
|
deferred.reject("Failed to enable addon " + addon.id + " due to: " + handler);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
this._log.info("reconcileAddonState() - Activating add-on: " + addon.id);
|
2014-04-04 15:58:27 -07:00
|
|
|
AddonManager.addAddonListener(listener);
|
|
|
|
addon.userDisabled = false;
|
|
|
|
yield deferred.promise;
|
2014-04-17 06:47:36 -07:00
|
|
|
changes |= this.ADDON_CHANGE_ENABLE;
|
|
|
|
|
2015-11-03 05:26:13 -08:00
|
|
|
this._log.info("reconcileAddonState() - Add-on has been enabled: " + addon.id);
|
2014-04-17 06:47:36 -07:00
|
|
|
return changes;
|
|
|
|
}),
|
2014-04-04 15:58:27 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Obtain the underlying Addon from the Addon Manager.
|
|
|
|
*
|
|
|
|
* @return Promise<Addon|null>
|
|
|
|
*/
|
|
|
|
_getAddon: function () {
|
2014-04-17 06:47:36 -07:00
|
|
|
if (!this._addonId) {
|
|
|
|
return Promise.resolve(null);
|
|
|
|
}
|
|
|
|
|
2014-04-04 15:58:27 -07:00
|
|
|
let deferred = Promise.defer();
|
|
|
|
|
2014-04-23 05:34:48 -07:00
|
|
|
AddonManager.getAddonByID(this._addonId, (addon) => {
|
|
|
|
if (addon && addon.appDisabled) {
|
|
|
|
// Don't return PreviousExperiments.
|
|
|
|
addon = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
deferred.resolve(addon);
|
|
|
|
});
|
2014-04-04 15:58:27 -07:00
|
|
|
|
|
|
|
return deferred.promise;
|
|
|
|
},
|
|
|
|
|
2014-03-24 01:58:57 -07:00
|
|
|
_logTermination: function (terminationKind, terminationReason) {
|
|
|
|
if (terminationKind === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(terminationKind in TELEMETRY_LOG.TERMINATION)) {
|
2014-03-28 12:57:43 -07:00
|
|
|
this._log.warn("stop() - unknown terminationKind " + terminationKind);
|
2014-03-24 01:58:57 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let data = [terminationKind, this.id];
|
|
|
|
if (terminationReason) {
|
|
|
|
data = data.concat(terminationReason);
|
|
|
|
}
|
|
|
|
|
|
|
|
TelemetryLog.log(TELEMETRY_LOG.TERMINATION_KEY, data);
|
|
|
|
},
|
|
|
|
|
2014-04-17 06:47:36 -07:00
|
|
|
/**
|
|
|
|
* Determine whether an active experiment should be stopped.
|
2014-03-18 14:52:28 -07:00
|
|
|
*/
|
2014-04-17 06:47:36 -07:00
|
|
|
shouldStop: function () {
|
|
|
|
if (!this._enabled) {
|
|
|
|
throw new Error("shouldStop must not be called on disabled experiments.");
|
|
|
|
}
|
2014-03-18 14:52:28 -07:00
|
|
|
|
|
|
|
let data = this._manifestData;
|
|
|
|
let now = this._policy.now() / 1000; // The manifest times are in seconds.
|
|
|
|
let maxActiveSec = data.maxActiveSeconds || 0;
|
|
|
|
|
|
|
|
let deferred = Promise.defer();
|
|
|
|
this.isApplicable().then(
|
2014-03-24 01:58:57 -07:00
|
|
|
() => deferred.resolve({shouldStop: false}),
|
|
|
|
reason => deferred.resolve({shouldStop: true, reason: reason})
|
2014-03-18 14:52:28 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
return deferred.promise;
|
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Should this be discarded from the cache due to age?
|
|
|
|
*/
|
|
|
|
shouldDiscard: function () {
|
|
|
|
let limit = this._policy.now();
|
|
|
|
limit.setDate(limit.getDate() - KEEP_HISTORY_N_DAYS);
|
|
|
|
return (this._lastChangedDate < limit);
|
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get next date (in epoch-ms) to schedule a re-evaluation for this.
|
|
|
|
* Returns 0 if it doesn't need one.
|
|
|
|
*/
|
|
|
|
getScheduleTime: function () {
|
|
|
|
if (this._enabled) {
|
|
|
|
let now = this._policy.now();
|
|
|
|
let startTime = this._startDate.getTime();
|
|
|
|
let maxActiveTime = startTime + 1000 * this._manifestData.maxActiveSeconds;
|
|
|
|
return Math.min(1000 * this._manifestData.endTime, maxActiveTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._endDate) {
|
|
|
|
return this._endDate.getTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1000 * this._manifestData.startTime;
|
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Perform sanity checks on the experiment data.
|
|
|
|
*/
|
|
|
|
_isManifestDataValid: function (data) {
|
2014-03-28 12:57:43 -07:00
|
|
|
this._log.trace("isManifestDataValid() - data: " + JSON.stringify(data));
|
2014-03-18 14:52:28 -07:00
|
|
|
|
|
|
|
for (let key of this.MANIFEST_REQUIRED_FIELDS) {
|
|
|
|
if (!(key in data)) {
|
2014-03-28 12:57:43 -07:00
|
|
|
this._log.error("isManifestDataValid() - missing required key: " + key);
|
2014-03-18 14:52:28 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let key in data) {
|
|
|
|
if (!this.MANIFEST_OPTIONAL_FIELDS.has(key) &&
|
|
|
|
!this.MANIFEST_REQUIRED_FIELDS.has(key)) {
|
2014-03-28 12:57:43 -07:00
|
|
|
this._log.error("isManifestDataValid() - unknown key: " + key);
|
2014-03-18 14:52:28 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
};
|
2014-03-20 14:16:00 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Strip a Date down to its UTC midnight.
|
|
|
|
*
|
|
|
|
* This will return a cloned Date object. The original is unchanged.
|
|
|
|
*/
|
2015-09-15 11:19:45 -07:00
|
|
|
var stripDateToMidnight = function (d) {
|
2014-03-20 14:16:00 -07:00
|
|
|
let m = new Date(d);
|
|
|
|
m.setUTCHours(0, 0, 0, 0);
|
|
|
|
|
|
|
|
return m;
|
|
|
|
};
|
|
|
|
|
2014-04-23 05:34:48 -07:00
|
|
|
/**
|
|
|
|
* An Add-ons Manager provider that knows about old experiments.
|
|
|
|
*
|
|
|
|
* This provider exposes read-only add-ons corresponding to previously-active
|
|
|
|
* experiments. The existence of this provider (and the add-ons it knows about)
|
|
|
|
* facilitates the display of old experiments in the Add-ons Manager UI with
|
|
|
|
* very little custom code in that component.
|
|
|
|
*/
|
|
|
|
this.Experiments.PreviousExperimentProvider = function (experiments) {
|
|
|
|
this._experiments = experiments;
|
2014-05-06 03:18:10 -07:00
|
|
|
this._experimentList = [];
|
|
|
|
this._log = Log.repository.getLoggerWithMessagePrefix(
|
|
|
|
"Browser.Experiments.Experiments",
|
|
|
|
"PreviousExperimentProvider #" + gPreviousProviderCounter++ + "::");
|
2014-04-23 05:34:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
this.Experiments.PreviousExperimentProvider.prototype = Object.freeze({
|
2015-09-20 10:26:17 -07:00
|
|
|
name: "PreviousExperimentProvider",
|
2014-09-27 13:25:47 -07:00
|
|
|
|
2014-05-06 03:18:10 -07:00
|
|
|
startup: function () {
|
|
|
|
this._log.trace("startup()");
|
|
|
|
Services.obs.addObserver(this, EXPERIMENTS_CHANGED_TOPIC, false);
|
|
|
|
},
|
|
|
|
|
|
|
|
shutdown: function () {
|
|
|
|
this._log.trace("shutdown()");
|
2014-07-02 15:40:56 -07:00
|
|
|
try {
|
|
|
|
Services.obs.removeObserver(this, EXPERIMENTS_CHANGED_TOPIC);
|
|
|
|
} catch(e) {
|
|
|
|
// Prevent crash in mochitest-browser3 on Mulet
|
|
|
|
}
|
2014-05-06 03:18:10 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
observe: function (subject, topic, data) {
|
|
|
|
switch (topic) {
|
|
|
|
case EXPERIMENTS_CHANGED_TOPIC:
|
|
|
|
this._updateExperimentList();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
2014-04-23 05:34:48 -07:00
|
|
|
|
|
|
|
getAddonByID: function (id, cb) {
|
2014-05-06 03:18:10 -07:00
|
|
|
for (let experiment of this._experimentList) {
|
|
|
|
if (experiment.id == id) {
|
|
|
|
cb(new PreviousExperimentAddon(experiment));
|
|
|
|
return;
|
2014-04-23 05:34:48 -07:00
|
|
|
}
|
2014-05-06 03:18:10 -07:00
|
|
|
}
|
2014-04-23 05:34:48 -07:00
|
|
|
|
2014-05-06 03:18:10 -07:00
|
|
|
cb(null);
|
2014-04-23 05:34:48 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
getAddonsByTypes: function (types, cb) {
|
|
|
|
if (types && types.length > 0 && types.indexOf("experiment") == -1) {
|
|
|
|
cb([]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-12-17 11:42:24 -08:00
|
|
|
cb(this._experimentList.map(e => new PreviousExperimentAddon(e)));
|
2014-04-23 05:34:48 -07:00
|
|
|
},
|
|
|
|
|
2014-05-06 03:18:10 -07:00
|
|
|
_updateExperimentList: function () {
|
2014-04-23 05:34:48 -07:00
|
|
|
return this._experiments.getExperiments().then((experiments) => {
|
2015-12-17 11:42:24 -08:00
|
|
|
let list = experiments.filter(e => !e.active);
|
2014-05-06 03:18:10 -07:00
|
|
|
|
2015-12-17 11:42:24 -08:00
|
|
|
let newMap = new Map(list.map(e => [e.id, e]));
|
|
|
|
let oldMap = new Map(this._experimentList.map(e => [e.id, e]));
|
2014-05-06 03:18:10 -07:00
|
|
|
|
2015-12-17 11:42:24 -08:00
|
|
|
let added = [...newMap.keys()].filter(id => !oldMap.has(id));
|
|
|
|
let removed = [...oldMap.keys()].filter(id => !newMap.has(id));
|
2014-05-06 03:18:10 -07:00
|
|
|
|
|
|
|
for (let id of added) {
|
|
|
|
this._log.trace("updateExperimentList() - adding " + id);
|
|
|
|
let wrapper = new PreviousExperimentAddon(newMap.get(id));
|
|
|
|
AddonManagerPrivate.callInstallListeners("onExternalInstall", null, wrapper, null, false);
|
|
|
|
AddonManagerPrivate.callAddonListeners("onInstalling", wrapper, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let id of removed) {
|
|
|
|
this._log.trace("updateExperimentList() - removing " + id);
|
|
|
|
let wrapper = new PreviousExperimentAddon(oldMap.get(id));
|
|
|
|
AddonManagerPrivate.callAddonListeners("onUninstalling", plugin, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
this._experimentList = list;
|
|
|
|
|
|
|
|
for (let id of added) {
|
|
|
|
let wrapper = new PreviousExperimentAddon(newMap.get(id));
|
|
|
|
AddonManagerPrivate.callAddonListeners("onInstalled", wrapper);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let id of removed) {
|
|
|
|
let wrapper = new PreviousExperimentAddon(oldMap.get(id));
|
|
|
|
AddonManagerPrivate.callAddonListeners("onUninstalled", wrapper);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this._experimentList;
|
2014-04-23 05:34:48 -07:00
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An add-on that represents a previously-installed experiment.
|
|
|
|
*/
|
|
|
|
function PreviousExperimentAddon(experiment) {
|
|
|
|
this._id = experiment.id;
|
|
|
|
this._name = experiment.name;
|
|
|
|
this._endDate = experiment.endDate;
|
2014-04-23 05:34:49 -07:00
|
|
|
this._description = experiment.description;
|
2014-04-23 05:34:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
PreviousExperimentAddon.prototype = Object.freeze({
|
|
|
|
// BEGIN REQUIRED ADDON PROPERTIES
|
|
|
|
|
|
|
|
get appDisabled() {
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
|
|
|
get blocklistState() {
|
|
|
|
Ci.nsIBlocklistService.STATE_NOT_BLOCKED
|
|
|
|
},
|
|
|
|
|
|
|
|
get creator() {
|
|
|
|
return new AddonManagerPrivate.AddonAuthor("");
|
|
|
|
},
|
|
|
|
|
|
|
|
get foreignInstall() {
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
|
|
|
get id() {
|
|
|
|
return this._id;
|
|
|
|
},
|
|
|
|
|
|
|
|
get isActive() {
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
|
|
|
get isCompatible() {
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
|
|
|
get isPlatformCompatible() {
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
|
|
|
get name() {
|
|
|
|
return this._name;
|
|
|
|
},
|
|
|
|
|
|
|
|
get pendingOperations() {
|
|
|
|
return AddonManager.PENDING_NONE;
|
|
|
|
},
|
|
|
|
|
|
|
|
get permissions() {
|
|
|
|
return 0;
|
|
|
|
},
|
|
|
|
|
|
|
|
get providesUpdatesSecurely() {
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
|
|
|
get scope() {
|
|
|
|
return AddonManager.SCOPE_PROFILE;
|
|
|
|
},
|
|
|
|
|
|
|
|
get type() {
|
|
|
|
return "experiment";
|
|
|
|
},
|
|
|
|
|
|
|
|
get userDisabled() {
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
|
|
|
get version() {
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
|
|
|
// END REQUIRED PROPERTIES
|
|
|
|
|
|
|
|
// BEGIN OPTIONAL PROPERTIES
|
|
|
|
|
2014-04-23 05:34:49 -07:00
|
|
|
get description() {
|
|
|
|
return this._description;
|
|
|
|
},
|
2014-04-23 05:34:48 -07:00
|
|
|
|
|
|
|
get updateDate() {
|
|
|
|
return new Date(this._endDate);
|
|
|
|
},
|
|
|
|
|
|
|
|
// END OPTIONAL PROPERTIES
|
|
|
|
|
|
|
|
// BEGIN REQUIRED METHODS
|
|
|
|
|
|
|
|
isCompatibleWith: function (appVersion, platformVersion) {
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
|
|
|
findUpdates: function (listener, reason, appVersion, platformVersion) {
|
|
|
|
AddonManagerPrivate.callNoUpdateListeners(this, listener, reason,
|
|
|
|
appVersion, platformVersion);
|
|
|
|
},
|
|
|
|
|
|
|
|
// END REQUIRED METHODS
|
|
|
|
|
2014-04-23 05:34:49 -07:00
|
|
|
/**
|
|
|
|
* The end-date of the experiment, required for the Addon Manager UI.
|
|
|
|
*/
|
|
|
|
|
|
|
|
get endDate() {
|
|
|
|
return this._endDate;
|
|
|
|
},
|
|
|
|
|
2014-04-23 05:34:48 -07:00
|
|
|
});
|