2014-03-19 01:52:28 +04: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";
|
|
|
|
|
|
|
|
const {interfaces: Ci, utils: Cu} = Components;
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
2014-04-10 18:56:25 +04:00
|
|
|
|
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "Experiments",
|
|
|
|
"resource:///modules/experiments/Experiments.jsm");
|
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "OS",
|
|
|
|
"resource://gre/modules/osfile.jsm");
|
2014-04-15 20:12:26 +04:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "CommonUtils",
|
|
|
|
"resource://services-common/utils.js");
|
2017-04-19 14:06:16 +03:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "TelemetryUtils",
|
|
|
|
"resource://gre/modules/TelemetryUtils.jsm");
|
|
|
|
|
2014-04-10 18:56:25 +04:00
|
|
|
|
|
|
|
const PREF_EXPERIMENTS_ENABLED = "experiments.enabled";
|
2014-04-15 20:12:26 +04:00
|
|
|
const PREF_ACTIVE_EXPERIMENT = "experiments.activeExperiment"; // whether we have an active experiment
|
|
|
|
const DELAY_INIT_MS = 30 * 1000;
|
|
|
|
|
2014-03-19 01:52:28 +04:00
|
|
|
function ExperimentsService() {
|
2014-04-15 20:12:26 +04:00
|
|
|
this._initialized = false;
|
|
|
|
this._delayedInitTimer = null;
|
2014-03-19 01:52:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
ExperimentsService.prototype = {
|
|
|
|
classID: Components.ID("{f7800463-3b97-47f9-9341-b7617e6d8d49}"),
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsITimerCallback, Ci.nsIObserver]),
|
|
|
|
|
2017-04-19 14:06:16 +03:00
|
|
|
get _experimentsEnabled() {
|
|
|
|
// We can enable experiments if either unified Telemetry or FHR is on, and the user
|
|
|
|
// has opted into Telemetry.
|
2017-07-31 14:25:41 +03:00
|
|
|
return Services.prefs.getBoolPref(PREF_EXPERIMENTS_ENABLED, false) &&
|
2017-04-19 14:06:16 +03:00
|
|
|
TelemetryUtils.isTelemetryEnabled;
|
|
|
|
},
|
|
|
|
|
2016-12-30 02:34:54 +03:00
|
|
|
notify(timer) {
|
2017-04-19 14:06:16 +03:00
|
|
|
if (!this._experimentsEnabled) {
|
2014-04-10 18:56:25 +04:00
|
|
|
return;
|
|
|
|
}
|
2014-03-19 01:52:30 +04:00
|
|
|
if (OS.Constants.Path.profileDir === undefined) {
|
|
|
|
throw Error("Update timer fired before profile was initialized?");
|
2014-03-19 01:52:28 +04:00
|
|
|
}
|
2015-03-10 15:36:17 +03:00
|
|
|
let instance = Experiments.instance();
|
|
|
|
if (instance.isReady) {
|
2017-04-19 14:06:16 +03:00
|
|
|
instance.updateManifest().catch(error => {
|
|
|
|
// Don't throw, as this breaks tests. In any case the best we can do here
|
|
|
|
// is to log the failure.
|
|
|
|
Cu.reportError(error);
|
|
|
|
});
|
2015-03-10 15:36:17 +03:00
|
|
|
}
|
2014-03-19 01:52:28 +04:00
|
|
|
},
|
|
|
|
|
2016-12-30 02:34:54 +03:00
|
|
|
_delayedInit() {
|
2014-04-15 20:12:26 +04:00
|
|
|
if (!this._initialized) {
|
|
|
|
this._initialized = true;
|
|
|
|
Experiments.instance(); // for side effects
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-12-30 02:34:54 +03:00
|
|
|
observe(subject, topic, data) {
|
2014-03-19 01:52:30 +04:00
|
|
|
switch (topic) {
|
|
|
|
case "profile-after-change":
|
2017-04-19 14:06:16 +03:00
|
|
|
if (this._experimentsEnabled) {
|
2017-04-14 22:51:38 +03:00
|
|
|
Services.obs.addObserver(this, "quit-application");
|
|
|
|
Services.obs.addObserver(this, "sessionstore-state-finalized");
|
|
|
|
Services.obs.addObserver(this, "EM-loaded");
|
2014-04-15 20:12:26 +04:00
|
|
|
|
2017-07-31 14:25:41 +03:00
|
|
|
if (Services.prefs.getBoolPref(PREF_ACTIVE_EXPERIMENT, false)) {
|
2014-04-15 20:12:26 +04:00
|
|
|
this._initialized = true;
|
|
|
|
Experiments.instance(); // for side effects
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "sessionstore-state-finalized":
|
|
|
|
if (!this._initialized) {
|
|
|
|
CommonUtils.namedTimer(this._delayedInit, DELAY_INIT_MS, this, "_delayedInitTimer");
|
|
|
|
}
|
|
|
|
break;
|
2014-05-16 14:56:25 +04:00
|
|
|
case "EM-loaded":
|
|
|
|
if (!this._initialized) {
|
|
|
|
Experiments.instance(); // for side effects
|
|
|
|
this._initialized = true;
|
|
|
|
|
|
|
|
if (this._delayedInitTimer) {
|
|
|
|
this._delayedInitTimer.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2014-04-15 20:12:26 +04:00
|
|
|
case "quit-application":
|
|
|
|
Services.obs.removeObserver(this, "quit-application");
|
|
|
|
Services.obs.removeObserver(this, "sessionstore-state-finalized");
|
2014-05-16 14:56:25 +04:00
|
|
|
Services.obs.removeObserver(this, "EM-loaded");
|
2014-04-15 20:12:26 +04:00
|
|
|
if (this._delayedInitTimer) {
|
|
|
|
this._delayedInitTimer.clear();
|
2014-04-10 18:56:25 +04:00
|
|
|
}
|
2014-03-19 01:52:30 +04:00
|
|
|
break;
|
2014-03-19 01:52:28 +04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([ExperimentsService]);
|