browser/experiments/ExperimentsService.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/experiments/ExperimentsService.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,110 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +"use strict";
     1.9 +
    1.10 +const {interfaces: Ci, utils: Cu} = Components;
    1.11 +
    1.12 +Cu.import("resource://gre/modules/XPCOMUtils.jsm");
    1.13 +Cu.import("resource://gre/modules/Services.jsm");
    1.14 +Cu.import("resource://gre/modules/Preferences.jsm");
    1.15 +
    1.16 +XPCOMUtils.defineLazyModuleGetter(this, "Experiments",
    1.17 +                                  "resource:///modules/experiments/Experiments.jsm");
    1.18 +XPCOMUtils.defineLazyModuleGetter(this, "OS",
    1.19 +                                  "resource://gre/modules/osfile.jsm");
    1.20 +XPCOMUtils.defineLazyModuleGetter(this, "CommonUtils",
    1.21 +                                  "resource://services-common/utils.js");
    1.22 +
    1.23 +const PREF_EXPERIMENTS_ENABLED  = "experiments.enabled";
    1.24 +const PREF_ACTIVE_EXPERIMENT    = "experiments.activeExperiment"; // whether we have an active experiment
    1.25 +const PREF_HEALTHREPORT_ENABLED = "datareporting.healthreport.service.enabled";
    1.26 +const PREF_TELEMETRY_ENABLED    = "toolkit.telemetry.enabled";
    1.27 +const DELAY_INIT_MS             = 30 * 1000;
    1.28 +
    1.29 +XPCOMUtils.defineLazyGetter(
    1.30 +  this, "gPrefs", () => {
    1.31 +    return new Preferences();
    1.32 +  });
    1.33 +
    1.34 +XPCOMUtils.defineLazyGetter(
    1.35 +  this, "gExperimentsEnabled", () => {
    1.36 +    return gPrefs.get(PREF_EXPERIMENTS_ENABLED, false) &&
    1.37 +           gPrefs.get(PREF_TELEMETRY_ENABLED, false) &&
    1.38 +           gPrefs.get(PREF_HEALTHREPORT_ENABLED, false);
    1.39 +  });
    1.40 +
    1.41 +XPCOMUtils.defineLazyGetter(
    1.42 +  this, "gActiveExperiment", () => {
    1.43 +    return gPrefs.get(PREF_ACTIVE_EXPERIMENT);
    1.44 +  });
    1.45 +
    1.46 +function ExperimentsService() {
    1.47 +  this._initialized = false;
    1.48 +  this._delayedInitTimer = null;
    1.49 +}
    1.50 +
    1.51 +ExperimentsService.prototype = {
    1.52 +  classID: Components.ID("{f7800463-3b97-47f9-9341-b7617e6d8d49}"),
    1.53 +  QueryInterface: XPCOMUtils.generateQI([Ci.nsITimerCallback, Ci.nsIObserver]),
    1.54 +
    1.55 +  notify: function (timer) {
    1.56 +    if (!gExperimentsEnabled) {
    1.57 +      return;
    1.58 +    }
    1.59 +    if (OS.Constants.Path.profileDir === undefined) {
    1.60 +      throw Error("Update timer fired before profile was initialized?");
    1.61 +    }
    1.62 +    Experiments.instance().updateManifest();
    1.63 +  },
    1.64 +
    1.65 +  _delayedInit: function () {
    1.66 +    if (!this._initialized) {
    1.67 +      this._initialized = true;
    1.68 +      Experiments.instance(); // for side effects
    1.69 +    }
    1.70 +  },
    1.71 +
    1.72 +  observe: function (subject, topic, data) {
    1.73 +    switch (topic) {
    1.74 +      case "profile-after-change":
    1.75 +        if (gExperimentsEnabled) {
    1.76 +          Services.obs.addObserver(this, "quit-application", false);
    1.77 +          Services.obs.addObserver(this, "sessionstore-state-finalized", false);
    1.78 +          Services.obs.addObserver(this, "EM-loaded", false);
    1.79 +
    1.80 +          if (gActiveExperiment) {
    1.81 +            this._initialized = true;
    1.82 +            Experiments.instance(); // for side effects
    1.83 +          }
    1.84 +        }
    1.85 +        break;
    1.86 +      case "sessionstore-state-finalized":
    1.87 +        if (!this._initialized) {
    1.88 +          CommonUtils.namedTimer(this._delayedInit, DELAY_INIT_MS, this, "_delayedInitTimer");
    1.89 +        }
    1.90 +        break;
    1.91 +      case "EM-loaded":
    1.92 +        if (!this._initialized) {
    1.93 +          Experiments.instance(); // for side effects
    1.94 +          this._initialized = true;
    1.95 +
    1.96 +          if (this._delayedInitTimer) {
    1.97 +            this._delayedInitTimer.clear();
    1.98 +          }
    1.99 +        }
   1.100 +        break;
   1.101 +      case "quit-application":
   1.102 +        Services.obs.removeObserver(this, "quit-application");
   1.103 +        Services.obs.removeObserver(this, "sessionstore-state-finalized");
   1.104 +        Services.obs.removeObserver(this, "EM-loaded");
   1.105 +        if (this._delayedInitTimer) {
   1.106 +          this._delayedInitTimer.clear();
   1.107 +        }
   1.108 +        break;
   1.109 +    }
   1.110 +  },
   1.111 +};
   1.112 +
   1.113 +this.NSGetFactory = XPCOMUtils.generateNSGetFactory([ExperimentsService]);

mercurial