2012-11-13 20:22:09 -08: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";
|
|
|
|
|
2013-01-27 11:26:48 -08:00
|
|
|
#ifndef MERGED_COMPARTMENT
|
|
|
|
|
2012-11-13 20:22:09 -08:00
|
|
|
this.EXPORTED_SYMBOLS = ["HealthReporter"];
|
|
|
|
|
|
|
|
const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
|
|
|
|
|
2013-01-27 11:26:48 -08:00
|
|
|
const MILLISECONDS_PER_DAY = 24 * 60 * 60 * 1000;
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/Metrics.jsm");
|
2013-01-30 07:07:22 -08:00
|
|
|
Cu.import("resource://services-common/async.js");
|
2013-02-13 16:32:45 -08:00
|
|
|
|
|
|
|
Cu.import("resource://services-common/bagheeraclient.js");
|
2013-01-27 11:26:48 -08:00
|
|
|
#endif
|
|
|
|
|
2012-11-13 20:22:09 -08:00
|
|
|
Cu.import("resource://services-common/log4moz.js");
|
|
|
|
Cu.import("resource://services-common/preferences.js");
|
|
|
|
Cu.import("resource://services-common/utils.js");
|
2013-02-01 11:43:15 -08:00
|
|
|
Cu.import("resource://gre/modules/commonjs/sdk/core/promise.js");
|
2012-11-16 10:05:19 -08:00
|
|
|
Cu.import("resource://gre/modules/osfile.jsm");
|
2013-01-06 12:13:27 -08:00
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Task.jsm");
|
2013-02-01 13:28:02 -08:00
|
|
|
Cu.import("resource://gre/modules/TelemetryStopwatch.jsm");
|
2013-01-06 12:13:27 -08:00
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
2012-11-13 20:22:09 -08:00
|
|
|
|
|
|
|
|
|
|
|
// Oldest year to allow in date preferences. This module was implemented in
|
|
|
|
// 2012 and no dates older than that should be encountered.
|
|
|
|
const OLDEST_ALLOWED_YEAR = 2012;
|
|
|
|
|
2013-01-06 12:13:27 -08:00
|
|
|
const DAYS_IN_PAYLOAD = 180;
|
|
|
|
|
|
|
|
const DEFAULT_DATABASE_NAME = "healthreport.sqlite";
|
|
|
|
|
2013-02-01 13:28:02 -08:00
|
|
|
const TELEMETRY_INIT = "HEALTHREPORT_INIT_MS";
|
2013-02-18 12:45:53 -08:00
|
|
|
const TELEMETRY_INIT_FIRSTRUN = "HEALTHREPORT_INIT_FIRSTRUN_MS";
|
|
|
|
const TELEMETRY_DB_OPEN = "HEALTHREPORT_DB_OPEN_MS";
|
|
|
|
const TELEMETRY_DB_OPEN_FIRSTRUN = "HEALTHREPORT_DB_OPEN_FIRSTRUN_MS";
|
2013-02-01 13:28:02 -08:00
|
|
|
const TELEMETRY_GENERATE_PAYLOAD = "HEALTHREPORT_GENERATE_JSON_PAYLOAD_MS";
|
2013-02-18 12:45:53 -08:00
|
|
|
const TELEMETRY_JSON_PAYLOAD_SERIALIZE = "HEALTHREPORT_JSON_PAYLOAD_SERIALIZE_MS";
|
2013-02-01 13:28:02 -08:00
|
|
|
const TELEMETRY_PAYLOAD_SIZE = "HEALTHREPORT_PAYLOAD_UNCOMPRESSED_BYTES";
|
|
|
|
const TELEMETRY_SAVE_LAST_PAYLOAD = "HEALTHREPORT_SAVE_LAST_PAYLOAD_MS";
|
|
|
|
const TELEMETRY_UPLOAD = "HEALTHREPORT_UPLOAD_MS";
|
|
|
|
const TELEMETRY_SHUTDOWN_DELAY = "HEALTHREPORT_SHUTDOWN_DELAY_MS";
|
2013-02-18 12:45:53 -08:00
|
|
|
const TELEMETRY_COLLECT_CONSTANT = "HEALTHREPORT_COLLECT_CONSTANT_DATA_MS";
|
|
|
|
const TELEMETRY_COLLECT_DAILY = "HEALTHREPORT_COLLECT_DAILY_MS";
|
|
|
|
const TELEMETRY_SHUTDOWN = "HEALTHREPORT_SHUTDOWN_MS";
|
2012-11-13 20:22:09 -08:00
|
|
|
|
|
|
|
/**
|
2013-02-13 16:32:45 -08:00
|
|
|
* This is the abstract base class of `HealthReporter`. It exists so that
|
|
|
|
* we can sanely divide work on platforms where control of Firefox Health
|
|
|
|
* Report is outside of Gecko (e.g., Android).
|
2012-11-13 20:22:09 -08:00
|
|
|
*/
|
2013-02-13 16:32:45 -08:00
|
|
|
function AbstractHealthReporter(branch, policy, sessionRecorder) {
|
2012-11-13 20:22:09 -08:00
|
|
|
if (!branch.endsWith(".")) {
|
2013-01-06 12:13:27 -08:00
|
|
|
throw new Error("Branch must end with a period (.): " + branch);
|
2012-11-13 20:22:09 -08:00
|
|
|
}
|
|
|
|
|
2013-01-11 13:45:22 -08:00
|
|
|
if (!policy) {
|
|
|
|
throw new Error("Must provide policy to HealthReporter constructor.");
|
|
|
|
}
|
|
|
|
|
2012-11-13 20:22:09 -08:00
|
|
|
this._log = Log4Moz.repository.getLogger("Services.HealthReport.HealthReporter");
|
2013-01-06 12:13:27 -08:00
|
|
|
this._log.info("Initializing health reporter instance against " + branch);
|
2012-11-13 20:22:09 -08:00
|
|
|
|
2013-01-18 22:35:07 -08:00
|
|
|
this._branch = branch;
|
2012-11-13 20:22:09 -08:00
|
|
|
this._prefs = new Preferences(branch);
|
|
|
|
|
2013-01-11 13:45:22 -08:00
|
|
|
this._policy = policy;
|
2013-01-18 22:35:07 -08:00
|
|
|
this.sessionRecorder = sessionRecorder;
|
2013-01-06 12:13:27 -08:00
|
|
|
|
2013-01-11 13:45:22 -08:00
|
|
|
this._dbName = this._prefs.get("dbName") || DEFAULT_DATABASE_NAME;
|
2013-01-06 12:13:27 -08:00
|
|
|
|
|
|
|
this._storage = null;
|
|
|
|
this._storageInProgress = false;
|
|
|
|
this._collector = null;
|
2013-01-06 13:53:33 -08:00
|
|
|
this._collectorInProgress = false;
|
2013-01-06 12:13:27 -08:00
|
|
|
this._initialized = false;
|
|
|
|
this._initializeHadError = false;
|
|
|
|
this._initializedDeferred = Promise.defer();
|
|
|
|
this._shutdownRequested = false;
|
|
|
|
this._shutdownInitiated = false;
|
|
|
|
this._shutdownComplete = false;
|
|
|
|
this._shutdownCompleteCallback = null;
|
|
|
|
|
2013-02-27 16:52:26 -08:00
|
|
|
this._errors = [];
|
|
|
|
|
2013-02-18 13:05:07 -08:00
|
|
|
this._pullOnlyProviders = {};
|
|
|
|
this._pullOnlyProvidersRegistered = false;
|
2013-02-05 09:59:15 -08:00
|
|
|
this._lastDailyDate = null;
|
2013-01-31 08:58:19 -08:00
|
|
|
|
2013-02-13 16:32:45 -08:00
|
|
|
// Yes, this will probably run concurrently with remaining constructor work.
|
2013-02-18 12:45:53 -08:00
|
|
|
let hasFirstRun = this._prefs.get("service.firstRun", false);
|
|
|
|
this._initHistogram = hasFirstRun ? TELEMETRY_INIT : TELEMETRY_INIT_FIRSTRUN;
|
|
|
|
this._dbOpenHistogram = hasFirstRun ? TELEMETRY_DB_OPEN : TELEMETRY_DB_OPEN_FIRSTRUN;
|
|
|
|
|
|
|
|
TelemetryStopwatch.start(this._initHistogram, this);
|
2013-02-01 13:28:02 -08:00
|
|
|
|
2013-01-06 12:13:27 -08:00
|
|
|
this._ensureDirectoryExists(this._stateDir)
|
|
|
|
.then(this._onStateDirCreated.bind(this),
|
|
|
|
this._onInitError.bind(this));
|
2012-11-13 20:22:09 -08:00
|
|
|
}
|
|
|
|
|
2013-02-13 16:32:45 -08:00
|
|
|
AbstractHealthReporter.prototype = Object.freeze({
|
2013-01-06 12:13:27 -08:00
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver]),
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether the service is fully initialized and running.
|
|
|
|
*
|
|
|
|
* If this is false, it is not safe to call most functions.
|
|
|
|
*/
|
|
|
|
get initialized() {
|
|
|
|
return this._initialized;
|
|
|
|
},
|
|
|
|
|
|
|
|
//----------------------------------------------------
|
|
|
|
// SERVICE CONTROL FUNCTIONS
|
|
|
|
//
|
|
|
|
// You shouldn't need to call any of these externally.
|
|
|
|
//----------------------------------------------------
|
|
|
|
|
|
|
|
_onInitError: function (error) {
|
2013-02-18 12:45:53 -08:00
|
|
|
TelemetryStopwatch.cancel(this._initHistogram, this);
|
|
|
|
TelemetryStopwatch.cancel(this._dbOpenHistogram, this);
|
|
|
|
delete this._initHistogram;
|
|
|
|
delete this._dbOpenHistogram;
|
|
|
|
|
2013-02-27 16:52:26 -08:00
|
|
|
this._recordError("Error during initialization", error);
|
2013-01-06 12:13:27 -08:00
|
|
|
this._initializeHadError = true;
|
|
|
|
this._initiateShutdown();
|
|
|
|
this._initializedDeferred.reject(error);
|
|
|
|
|
|
|
|
// FUTURE consider poisoning prototype's functions so calls fail with a
|
|
|
|
// useful error message.
|
|
|
|
},
|
|
|
|
|
|
|
|
_onStateDirCreated: function () {
|
|
|
|
// As soon as we have could storage, we need to register cleanup or
|
|
|
|
// else bad things happen on shutdown.
|
|
|
|
Services.obs.addObserver(this, "quit-application", false);
|
|
|
|
Services.obs.addObserver(this, "profile-before-change", false);
|
|
|
|
|
|
|
|
this._storageInProgress = true;
|
2013-02-18 12:45:53 -08:00
|
|
|
TelemetryStopwatch.start(this._dbOpenHistogram, this);
|
2013-01-06 12:13:27 -08:00
|
|
|
Metrics.Storage(this._dbName).then(this._onStorageCreated.bind(this),
|
|
|
|
this._onInitError.bind(this));
|
|
|
|
},
|
|
|
|
|
|
|
|
// Called when storage has been opened.
|
|
|
|
_onStorageCreated: function (storage) {
|
2013-02-18 12:45:53 -08:00
|
|
|
TelemetryStopwatch.finish(this._dbOpenHistogram, this);
|
|
|
|
delete this._dbOpenHistogram;
|
2013-01-06 12:13:27 -08:00
|
|
|
this._log.info("Storage initialized.");
|
|
|
|
this._storage = storage;
|
|
|
|
this._storageInProgress = false;
|
|
|
|
|
|
|
|
if (this._shutdownRequested) {
|
|
|
|
this._initiateShutdown();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Task.spawn(this._initializeCollector.bind(this))
|
|
|
|
.then(this._onCollectorInitialized.bind(this),
|
|
|
|
this._onInitError.bind(this));
|
|
|
|
},
|
|
|
|
|
|
|
|
_initializeCollector: function () {
|
|
|
|
if (this._collector) {
|
|
|
|
throw new Error("Collector has already been initialized.");
|
|
|
|
}
|
|
|
|
|
|
|
|
this._log.info("Initializing collector.");
|
|
|
|
this._collector = new Metrics.Collector(this._storage);
|
2013-02-27 16:52:26 -08:00
|
|
|
this._collector.onProviderError = this._errors.push.bind(this._errors);
|
2013-01-06 13:53:33 -08:00
|
|
|
this._collectorInProgress = true;
|
2013-01-06 12:13:27 -08:00
|
|
|
|
|
|
|
let catString = this._prefs.get("service.providerCategories") || "";
|
|
|
|
if (catString.length) {
|
|
|
|
for (let category of catString.split(",")) {
|
|
|
|
yield this.registerProvidersFromCategoryManager(category);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_onCollectorInitialized: function () {
|
2013-02-18 12:45:53 -08:00
|
|
|
TelemetryStopwatch.finish(this._initHistogram, this);
|
|
|
|
delete this._initHistogram;
|
2013-01-06 13:53:33 -08:00
|
|
|
this._log.debug("Collector initialized.");
|
|
|
|
this._collectorInProgress = false;
|
|
|
|
|
2013-01-06 12:13:27 -08:00
|
|
|
if (this._shutdownRequested) {
|
|
|
|
this._initiateShutdown();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._log.info("HealthReporter started.");
|
|
|
|
this._initialized = true;
|
|
|
|
Services.obs.addObserver(this, "idle-daily", false);
|
2013-01-25 00:39:01 -08:00
|
|
|
|
|
|
|
// Clean up caches and reduce memory usage.
|
|
|
|
this._storage.compact();
|
2013-01-06 12:13:27 -08:00
|
|
|
this._initializedDeferred.resolve(this);
|
|
|
|
},
|
|
|
|
|
|
|
|
// nsIObserver to handle shutdown.
|
|
|
|
observe: function (subject, topic, data) {
|
|
|
|
switch (topic) {
|
|
|
|
case "quit-application":
|
|
|
|
Services.obs.removeObserver(this, "quit-application");
|
|
|
|
this._initiateShutdown();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "profile-before-change":
|
|
|
|
Services.obs.removeObserver(this, "profile-before-change");
|
|
|
|
this._waitForShutdown();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "idle-daily":
|
|
|
|
this._performDailyMaintenance();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_initiateShutdown: function () {
|
|
|
|
// Ensure we only begin the main shutdown sequence once.
|
|
|
|
if (this._shutdownInitiated) {
|
|
|
|
this._log.warn("Shutdown has already been initiated. No-op.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._log.info("Request to shut down.");
|
|
|
|
|
|
|
|
this._initialized = false;
|
|
|
|
this._shutdownRequested = true;
|
|
|
|
|
2013-02-27 16:52:29 -08:00
|
|
|
if (this._initializeHadError) {
|
|
|
|
this._log.warn("Initialization had error. Shutting down immediately.");
|
|
|
|
} else {
|
|
|
|
if (this._collectorInProgress) {
|
|
|
|
this._log.warn("Collector is in progress of initializing. Waiting to finish.");
|
|
|
|
return;
|
|
|
|
}
|
2013-01-06 13:53:33 -08:00
|
|
|
|
2013-02-27 16:52:29 -08:00
|
|
|
// If storage is in the process of initializing, we need to wait for it
|
|
|
|
// to finish before continuing. The initialization process will call us
|
|
|
|
// again once storage has initialized.
|
|
|
|
if (this._storageInProgress) {
|
|
|
|
this._log.warn("Storage is in progress of initializing. Waiting to finish.");
|
|
|
|
return;
|
|
|
|
}
|
2013-01-06 12:13:27 -08:00
|
|
|
}
|
|
|
|
|
2013-01-06 16:24:50 -08:00
|
|
|
this._log.warn("Initiating main shutdown procedure.");
|
|
|
|
|
2013-01-06 12:13:27 -08:00
|
|
|
// Everything from here must only be performed once or else race conditions
|
|
|
|
// could occur.
|
2013-02-18 12:45:53 -08:00
|
|
|
|
|
|
|
TelemetryStopwatch.start(TELEMETRY_SHUTDOWN, this);
|
2013-01-06 12:13:27 -08:00
|
|
|
this._shutdownInitiated = true;
|
|
|
|
|
2013-01-15 14:53:34 -08:00
|
|
|
// We may not have registered the observer yet. If not, this will
|
|
|
|
// throw.
|
|
|
|
try {
|
2013-01-06 16:24:50 -08:00
|
|
|
Services.obs.removeObserver(this, "idle-daily");
|
2013-01-15 14:53:34 -08:00
|
|
|
} catch (ex) { }
|
2013-01-06 12:13:27 -08:00
|
|
|
|
|
|
|
// If we have collectors, we need to shut down providers.
|
|
|
|
if (this._collector) {
|
|
|
|
let onShutdown = this._onCollectorShutdown.bind(this);
|
|
|
|
Task.spawn(this._shutdownCollector.bind(this))
|
|
|
|
.then(onShutdown, onShutdown);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-01-06 16:24:50 -08:00
|
|
|
this._log.warn("Don't have collector. Proceeding to storage shutdown.");
|
|
|
|
this._shutdownStorage();
|
2013-01-06 12:13:27 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
_shutdownCollector: function () {
|
2013-01-06 16:24:50 -08:00
|
|
|
this._log.info("Shutting down collector.");
|
2013-01-06 12:13:27 -08:00
|
|
|
for (let provider of this._collector.providers) {
|
|
|
|
try {
|
|
|
|
yield provider.shutdown();
|
|
|
|
} catch (ex) {
|
|
|
|
this._log.warn("Error when shutting down provider: " +
|
|
|
|
CommonUtils.exceptionStr(ex));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_onCollectorShutdown: function () {
|
2013-01-06 16:24:50 -08:00
|
|
|
this._log.info("Collector shut down.");
|
2013-01-06 12:13:27 -08:00
|
|
|
this._collector = null;
|
2013-01-06 16:24:50 -08:00
|
|
|
this._shutdownStorage();
|
|
|
|
},
|
2013-01-06 12:13:27 -08:00
|
|
|
|
2013-01-06 16:24:50 -08:00
|
|
|
_shutdownStorage: function () {
|
|
|
|
if (!this._storage) {
|
|
|
|
this._onShutdownComplete();
|
2013-01-06 12:13:27 -08:00
|
|
|
}
|
|
|
|
|
2013-01-06 16:24:50 -08:00
|
|
|
this._log.info("Shutting down storage.");
|
|
|
|
let onClose = this._onStorageClose.bind(this);
|
|
|
|
this._storage.close().then(onClose, onClose);
|
2013-01-06 12:13:27 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
_onStorageClose: function (error) {
|
2013-01-06 16:24:50 -08:00
|
|
|
this._log.info("Storage has been closed.");
|
|
|
|
|
2013-01-06 12:13:27 -08:00
|
|
|
if (error) {
|
|
|
|
this._log.warn("Error when closing storage: " +
|
|
|
|
CommonUtils.exceptionStr(error));
|
|
|
|
}
|
|
|
|
|
|
|
|
this._storage = null;
|
2013-01-06 16:24:50 -08:00
|
|
|
this._onShutdownComplete();
|
|
|
|
},
|
|
|
|
|
|
|
|
_onShutdownComplete: function () {
|
|
|
|
this._log.warn("Shutdown complete.");
|
2013-01-06 12:13:27 -08:00
|
|
|
this._shutdownComplete = true;
|
2013-02-18 12:45:53 -08:00
|
|
|
TelemetryStopwatch.finish(TELEMETRY_SHUTDOWN, this);
|
2013-01-06 12:13:27 -08:00
|
|
|
|
|
|
|
if (this._shutdownCompleteCallback) {
|
|
|
|
this._shutdownCompleteCallback();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_waitForShutdown: function () {
|
|
|
|
if (this._shutdownComplete) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-02-01 13:28:02 -08:00
|
|
|
TelemetryStopwatch.start(TELEMETRY_SHUTDOWN_DELAY, this);
|
|
|
|
try {
|
|
|
|
this._shutdownCompleteCallback = Async.makeSpinningCallback();
|
|
|
|
this._shutdownCompleteCallback.wait();
|
|
|
|
this._shutdownCompleteCallback = null;
|
|
|
|
} finally {
|
|
|
|
TelemetryStopwatch.finish(TELEMETRY_SHUTDOWN_DELAY, this);
|
|
|
|
}
|
2013-01-06 12:13:27 -08:00
|
|
|
},
|
|
|
|
|
2012-11-13 20:22:09 -08:00
|
|
|
/**
|
2013-01-06 12:13:27 -08:00
|
|
|
* Convenience method to shut down the instance.
|
2012-11-16 10:05:19 -08:00
|
|
|
*
|
2013-01-06 12:13:27 -08:00
|
|
|
* This should *not* be called outside of tests.
|
2012-11-13 20:22:09 -08:00
|
|
|
*/
|
2013-01-06 12:13:27 -08:00
|
|
|
_shutdown: function () {
|
|
|
|
this._initiateShutdown();
|
|
|
|
this._waitForShutdown();
|
2012-11-13 20:22:09 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-01-06 12:13:27 -08:00
|
|
|
* Return a promise that is resolved once the service has been initialized.
|
2012-11-13 20:22:09 -08:00
|
|
|
*/
|
2013-01-06 12:13:27 -08:00
|
|
|
onInit: function () {
|
|
|
|
if (this._initializeHadError) {
|
|
|
|
throw new Error("Service failed to initialize.");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._initialized) {
|
|
|
|
return Promise.resolve(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this._initializedDeferred.promise;
|
|
|
|
},
|
|
|
|
|
|
|
|
_performDailyMaintenance: function () {
|
|
|
|
this._log.info("Request to perform daily maintenance.");
|
|
|
|
|
|
|
|
if (!this._initialized) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let now = new Date();
|
|
|
|
let cutoff = new Date(now.getTime() - MILLISECONDS_PER_DAY * (DAYS_IN_PAYLOAD - 1));
|
|
|
|
|
|
|
|
// The operation is enqueued and put in a transaction by the storage module.
|
|
|
|
this._storage.pruneDataBefore(cutoff);
|
2012-11-13 20:22:09 -08:00
|
|
|
},
|
|
|
|
|
2013-01-06 12:13:27 -08:00
|
|
|
//--------------------
|
|
|
|
// Provider Management
|
|
|
|
//--------------------
|
|
|
|
|
2013-02-12 16:32:45 -08:00
|
|
|
/**
|
|
|
|
* Obtain a provider from its name.
|
|
|
|
*
|
|
|
|
* This will only return providers that are currently initialized. If
|
2013-02-18 13:05:07 -08:00
|
|
|
* a provider is lazy initialized (like pull-only providers) this
|
2013-02-12 16:32:45 -08:00
|
|
|
* will likely not return anything.
|
|
|
|
*/
|
|
|
|
getProvider: function (name) {
|
|
|
|
return this._collector.getProvider(name);
|
|
|
|
},
|
|
|
|
|
2012-11-13 20:22:09 -08:00
|
|
|
/**
|
2013-01-06 12:13:27 -08:00
|
|
|
* Register a `Metrics.Provider` with this instance.
|
2012-11-13 20:22:09 -08:00
|
|
|
*
|
|
|
|
* This needs to be called or no data will be collected. See also
|
2013-01-24 11:10:18 -08:00
|
|
|
* `registerProvidersFromCategoryManager`.
|
2012-11-13 20:22:09 -08:00
|
|
|
*
|
|
|
|
* @param provider
|
2013-01-06 12:13:27 -08:00
|
|
|
* (Metrics.Provider) The provider to register for collection.
|
2012-11-13 20:22:09 -08:00
|
|
|
*/
|
2013-01-06 12:13:27 -08:00
|
|
|
registerProvider: function (provider) {
|
2012-11-13 20:22:09 -08:00
|
|
|
return this._collector.registerProvider(provider);
|
|
|
|
},
|
|
|
|
|
2013-02-06 19:26:26 -08:00
|
|
|
/**
|
|
|
|
* Registers a provider from its constructor function.
|
|
|
|
*
|
2013-02-18 13:05:07 -08:00
|
|
|
* If the provider is pull-only, it will be stashed away and
|
2013-02-06 19:26:26 -08:00
|
|
|
* initialized later. Null will be returned.
|
|
|
|
*
|
2013-02-18 13:05:07 -08:00
|
|
|
* If it is not pull-only, it will be initialized immediately and a
|
2013-02-06 19:26:26 -08:00
|
|
|
* promise will be returned. The promise will be resolved when the
|
|
|
|
* provider has finished initializing.
|
|
|
|
*/
|
|
|
|
registerProviderFromType: function (type) {
|
|
|
|
let proto = type.prototype;
|
2013-02-18 13:05:07 -08:00
|
|
|
if (proto.pullOnly) {
|
|
|
|
this._log.info("Provider is pull-only. Deferring initialization: " +
|
2013-02-06 19:26:26 -08:00
|
|
|
proto.name);
|
2013-02-18 13:05:07 -08:00
|
|
|
this._pullOnlyProviders[proto.name] = type;
|
2013-02-06 19:26:26 -08:00
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
let provider = this.initProviderFromType(type);
|
|
|
|
return this.registerProvider(provider);
|
|
|
|
},
|
|
|
|
|
2012-11-13 20:22:09 -08:00
|
|
|
/**
|
|
|
|
* Registers providers from a category manager category.
|
|
|
|
*
|
|
|
|
* This examines the specified category entries and registers found
|
|
|
|
* providers.
|
|
|
|
*
|
|
|
|
* Category entries are essentially JS modules and the name of the symbol
|
2013-01-06 12:13:27 -08:00
|
|
|
* within that module that is a `Metrics.Provider` instance.
|
2012-11-13 20:22:09 -08:00
|
|
|
*
|
|
|
|
* The category entry name is the name of the JS type for the provider. The
|
|
|
|
* value is the resource:// URI to import which makes this type available.
|
|
|
|
*
|
|
|
|
* Example entry:
|
|
|
|
*
|
|
|
|
* FooProvider resource://gre/modules/foo.jsm
|
|
|
|
*
|
|
|
|
* One can register entries in the application's .manifest file. e.g.
|
|
|
|
*
|
|
|
|
* category healthreport-js-provider FooProvider resource://gre/modules/foo.jsm
|
|
|
|
*
|
|
|
|
* Then to load them:
|
|
|
|
*
|
2013-01-06 12:13:27 -08:00
|
|
|
* let reporter = getHealthReporter("healthreport.");
|
2012-11-13 20:22:09 -08:00
|
|
|
* reporter.registerProvidersFromCategoryManager("healthreport-js-provider");
|
|
|
|
*
|
|
|
|
* @param category
|
|
|
|
* (string) Name of category to query and load from.
|
|
|
|
*/
|
2013-01-06 12:13:27 -08:00
|
|
|
registerProvidersFromCategoryManager: function (category) {
|
|
|
|
this._log.info("Registering providers from category: " + category);
|
2012-11-13 20:22:09 -08:00
|
|
|
let cm = Cc["@mozilla.org/categorymanager;1"]
|
|
|
|
.getService(Ci.nsICategoryManager);
|
|
|
|
|
2013-01-06 12:13:27 -08:00
|
|
|
let promises = [];
|
2012-11-13 20:22:09 -08:00
|
|
|
let enumerator = cm.enumerateCategory(category);
|
|
|
|
while (enumerator.hasMoreElements()) {
|
|
|
|
let entry = enumerator.getNext()
|
|
|
|
.QueryInterface(Ci.nsISupportsCString)
|
|
|
|
.toString();
|
|
|
|
|
|
|
|
let uri = cm.getCategoryEntry(category, entry);
|
|
|
|
this._log.info("Attempting to load provider from category manager: " +
|
|
|
|
entry + " from " + uri);
|
|
|
|
|
|
|
|
try {
|
|
|
|
let ns = {};
|
|
|
|
Cu.import(uri, ns);
|
|
|
|
|
2013-02-06 19:26:26 -08:00
|
|
|
let promise = this.registerProviderFromType(ns[entry]);
|
|
|
|
if (promise) {
|
|
|
|
promises.push(promise);
|
2013-01-31 08:58:19 -08:00
|
|
|
}
|
2012-11-13 20:22:09 -08:00
|
|
|
} catch (ex) {
|
2013-02-27 16:52:26 -08:00
|
|
|
this._recordError("Error registering provider from category manager : " +
|
|
|
|
entry + ": ", ex);
|
2012-11-13 20:22:09 -08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2013-01-06 12:13:27 -08:00
|
|
|
|
|
|
|
return Task.spawn(function wait() {
|
|
|
|
for (let promise of promises) {
|
|
|
|
yield promise;
|
|
|
|
}
|
|
|
|
});
|
2012-11-13 20:22:09 -08:00
|
|
|
},
|
|
|
|
|
2013-01-31 08:58:19 -08:00
|
|
|
initProviderFromType: function (providerType) {
|
|
|
|
let provider = new providerType();
|
|
|
|
provider.initPreferences(this._branch + "provider.");
|
|
|
|
provider.healthReporter = this;
|
|
|
|
|
|
|
|
return provider;
|
|
|
|
},
|
|
|
|
|
2012-11-13 20:22:09 -08:00
|
|
|
/**
|
2013-02-18 13:05:07 -08:00
|
|
|
* Ensure that pull-only providers are registered.
|
2012-11-13 20:22:09 -08:00
|
|
|
*/
|
2013-02-18 13:05:07 -08:00
|
|
|
ensurePullOnlyProvidersRegistered: function () {
|
|
|
|
if (this._pullOnlyProvidersRegistered) {
|
2013-02-06 19:26:26 -08:00
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
let onFinished = function () {
|
2013-02-18 13:05:07 -08:00
|
|
|
this._pullOnlyProvidersRegistered = true;
|
2013-02-06 19:26:26 -08:00
|
|
|
|
|
|
|
return Promise.resolve();
|
|
|
|
}.bind(this);
|
|
|
|
|
2013-02-18 13:05:07 -08:00
|
|
|
return Task.spawn(function registerPullProviders() {
|
|
|
|
for each (let providerType in this._pullOnlyProviders) {
|
2013-01-31 08:58:19 -08:00
|
|
|
try {
|
|
|
|
let provider = this.initProviderFromType(providerType);
|
|
|
|
yield this.registerProvider(provider);
|
|
|
|
} catch (ex) {
|
2013-02-27 16:52:26 -08:00
|
|
|
this._recordError("Error registering pull-only provider", ex);
|
2013-01-31 08:58:19 -08:00
|
|
|
}
|
|
|
|
}
|
2013-02-06 19:26:26 -08:00
|
|
|
}.bind(this)).then(onFinished, onFinished);
|
|
|
|
},
|
|
|
|
|
2013-02-18 13:05:07 -08:00
|
|
|
ensurePullOnlyProvidersUnregistered: function () {
|
|
|
|
if (!this._pullOnlyProvidersRegistered) {
|
2013-02-06 19:26:26 -08:00
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
let onFinished = function () {
|
2013-02-18 13:05:07 -08:00
|
|
|
this._pullOnlyProvidersRegistered = false;
|
2013-02-06 19:26:26 -08:00
|
|
|
|
|
|
|
return Promise.resolve();
|
|
|
|
}.bind(this);
|
|
|
|
|
2013-02-18 13:05:07 -08:00
|
|
|
return Task.spawn(function unregisterPullProviders() {
|
2013-02-06 19:26:26 -08:00
|
|
|
for (let provider of this._collector.providers) {
|
2013-02-18 13:05:07 -08:00
|
|
|
if (!provider.pullOnly) {
|
2013-02-06 19:26:26 -08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-02-18 13:05:07 -08:00
|
|
|
this._log.info("Shutting down pull-only provider: " +
|
2013-02-06 19:26:26 -08:00
|
|
|
provider.name);
|
2013-01-31 08:58:19 -08:00
|
|
|
|
2013-02-06 19:26:26 -08:00
|
|
|
try {
|
|
|
|
yield provider.shutdown();
|
|
|
|
} catch (ex) {
|
2013-02-27 16:52:26 -08:00
|
|
|
this._recordError("Error when shutting down provider: " +
|
|
|
|
provider.name, ex);
|
2013-02-06 19:26:26 -08:00
|
|
|
} finally {
|
|
|
|
this._collector.unregisterProvider(provider.name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}.bind(this)).then(onFinished, onFinished);
|
|
|
|
},
|
|
|
|
|
2013-02-27 16:52:26 -08:00
|
|
|
/**
|
|
|
|
* Record an exception for reporting in the payload.
|
|
|
|
*
|
|
|
|
* A side effect is the exception is logged.
|
|
|
|
*
|
|
|
|
* Note that callers need to be extra sensitive about ensuring personal
|
|
|
|
* or otherwise private details do not leak into this. All of the user data
|
|
|
|
* on the stack in FHR code should be limited to data we were collecting with
|
|
|
|
* the intent to submit. So, it is covered under the user's consent to use
|
|
|
|
* the feature.
|
|
|
|
*
|
|
|
|
* @param message
|
|
|
|
* (string) Human readable message describing error.
|
|
|
|
* @param ex
|
|
|
|
* (Error) The error that should be captured.
|
|
|
|
*/
|
|
|
|
_recordError: function (message, ex) {
|
|
|
|
let recordMessage = message;
|
|
|
|
let logMessage = message;
|
|
|
|
|
|
|
|
if (ex) {
|
|
|
|
recordMessage += ": " + ex.message;
|
|
|
|
logMessage += ": " + CommonUtils.exceptionStr(ex);
|
|
|
|
}
|
|
|
|
|
|
|
|
this._log.warn(logMessage);
|
|
|
|
this._errors.push(recordMessage);
|
|
|
|
},
|
|
|
|
|
2013-02-06 19:26:26 -08:00
|
|
|
/**
|
|
|
|
* Collect all measurements for all registered providers.
|
|
|
|
*/
|
|
|
|
collectMeasurements: function () {
|
|
|
|
return Task.spawn(function doCollection() {
|
2013-01-31 08:58:19 -08:00
|
|
|
try {
|
2013-02-18 12:45:53 -08:00
|
|
|
TelemetryStopwatch.start(TELEMETRY_COLLECT_CONSTANT, this);
|
2013-02-05 09:59:15 -08:00
|
|
|
yield this._collector.collectConstantData();
|
2013-02-18 12:45:53 -08:00
|
|
|
TelemetryStopwatch.finish(TELEMETRY_COLLECT_CONSTANT, this);
|
2013-02-06 19:26:26 -08:00
|
|
|
} catch (ex) {
|
2013-02-18 12:45:53 -08:00
|
|
|
TelemetryStopwatch.cancel(TELEMETRY_COLLECT_CONSTANT, this);
|
2013-02-06 19:26:26 -08:00
|
|
|
this._log.warn("Error collecting constant data: " +
|
|
|
|
CommonUtils.exceptionStr(ex));
|
2013-01-31 08:58:19 -08:00
|
|
|
}
|
|
|
|
|
2013-02-05 09:59:15 -08:00
|
|
|
// Daily data is collected if it hasn't yet been collected this
|
|
|
|
// application session or if it has been more than a day since the
|
|
|
|
// last collection. This means that providers could see many calls to
|
|
|
|
// collectDailyData per calendar day. However, this collection API
|
|
|
|
// makes no guarantees about limits. The alternative would involve
|
|
|
|
// recording state. The simpler implementation prevails for now.
|
|
|
|
if (!this._lastDailyDate ||
|
|
|
|
Date.now() - this._lastDailyDate > MILLISECONDS_PER_DAY) {
|
|
|
|
|
|
|
|
try {
|
2013-02-18 12:45:53 -08:00
|
|
|
TelemetryStopwatch.start(TELEMETRY_COLLECT_DAILY, this);
|
2013-02-05 09:59:15 -08:00
|
|
|
this._lastDailyDate = new Date();
|
|
|
|
yield this._collector.collectDailyData();
|
2013-02-18 12:45:53 -08:00
|
|
|
TelemetryStopwatch.finish(TELEMETRY_COLLECT_DAILY, this);
|
2013-02-05 09:59:15 -08:00
|
|
|
} catch (ex) {
|
2013-02-18 12:45:53 -08:00
|
|
|
TelemetryStopwatch.cancel(TELEMETRY_COLLECT_DAILY, this);
|
2013-02-05 09:59:15 -08:00
|
|
|
this._log.warn("Error collecting daily data from providers: " +
|
|
|
|
CommonUtils.exceptionStr(ex));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Task.Result();
|
2013-01-31 08:58:19 -08:00
|
|
|
}.bind(this));
|
2012-11-13 20:22:09 -08:00
|
|
|
},
|
|
|
|
|
2013-02-05 20:31:48 -08:00
|
|
|
/**
|
|
|
|
* Helper function to perform data collection and obtain the JSON payload.
|
|
|
|
*
|
|
|
|
* If you are looking for an up-to-date snapshot of FHR data that pulls in
|
|
|
|
* new data since the last upload, this is how you should obtain it.
|
|
|
|
*
|
|
|
|
* @param asObject
|
|
|
|
* (bool) Whether to resolve an object or JSON-encoded string of that
|
|
|
|
* object (the default).
|
|
|
|
*
|
|
|
|
* @return Promise<Object | string>
|
|
|
|
*/
|
|
|
|
collectAndObtainJSONPayload: function (asObject=false) {
|
|
|
|
return Task.spawn(function collectAndObtain() {
|
2013-02-18 13:05:07 -08:00
|
|
|
yield this.ensurePullOnlyProvidersRegistered();
|
2013-02-05 20:31:48 -08:00
|
|
|
|
2013-02-06 19:26:26 -08:00
|
|
|
let payload;
|
|
|
|
let error;
|
2013-02-05 20:31:48 -08:00
|
|
|
|
2013-02-06 19:26:26 -08:00
|
|
|
try {
|
|
|
|
yield this.collectMeasurements();
|
|
|
|
payload = yield this.getJSONPayload(asObject);
|
|
|
|
} catch (ex) {
|
|
|
|
error = ex;
|
2013-02-27 16:52:26 -08:00
|
|
|
this._collectException("Error collecting and/or retrieving JSON payload",
|
|
|
|
ex);
|
2013-02-06 19:26:26 -08:00
|
|
|
} finally {
|
2013-02-18 13:05:07 -08:00
|
|
|
yield this.ensurePullOnlyProvidersUnregistered();
|
2013-02-06 19:26:26 -08:00
|
|
|
|
|
|
|
if (error) {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We hold off throwing to ensure that behavior between finally
|
|
|
|
// and generators and throwing is sane.
|
2013-02-05 20:31:48 -08:00
|
|
|
throw new Task.Result(payload);
|
|
|
|
}.bind(this));
|
|
|
|
},
|
|
|
|
|
2012-11-13 20:22:09 -08:00
|
|
|
|
2013-02-05 20:31:48 -08:00
|
|
|
/**
|
|
|
|
* Obtain the JSON payload for currently-collected data.
|
|
|
|
*
|
|
|
|
* The payload only contains data that has been recorded to FHR. Some
|
|
|
|
* providers may have newer data available. If you want to ensure you
|
|
|
|
* have all available data, call `collectAndObtainJSONPayload`
|
|
|
|
* instead.
|
|
|
|
*
|
|
|
|
* @param asObject
|
|
|
|
* (bool) Whether to return an object or JSON encoding of that
|
|
|
|
* object (the default).
|
2013-02-06 19:26:26 -08:00
|
|
|
*
|
|
|
|
* @return Promise<string|object>
|
2013-02-05 20:31:48 -08:00
|
|
|
*/
|
|
|
|
getJSONPayload: function (asObject=false) {
|
2013-02-01 13:28:02 -08:00
|
|
|
TelemetryStopwatch.start(TELEMETRY_GENERATE_PAYLOAD, this);
|
|
|
|
let deferred = Promise.defer();
|
|
|
|
|
2013-02-05 20:31:48 -08:00
|
|
|
Task.spawn(this._getJSONPayload.bind(this, this._now(), asObject)).then(
|
2013-02-01 13:28:02 -08:00
|
|
|
function onResult(result) {
|
|
|
|
TelemetryStopwatch.finish(TELEMETRY_GENERATE_PAYLOAD, this);
|
|
|
|
deferred.resolve(result);
|
|
|
|
}.bind(this),
|
|
|
|
function onError(error) {
|
|
|
|
TelemetryStopwatch.cancel(TELEMETRY_GENERATE_PAYLOAD, this);
|
|
|
|
deferred.reject(error);
|
|
|
|
}.bind(this)
|
|
|
|
);
|
|
|
|
|
|
|
|
return deferred.promise;
|
2013-01-06 12:13:27 -08:00
|
|
|
},
|
|
|
|
|
2013-02-05 20:31:48 -08:00
|
|
|
_getJSONPayload: function (now, asObject=false) {
|
2013-01-06 12:13:27 -08:00
|
|
|
let pingDateString = this._formatDate(now);
|
|
|
|
this._log.info("Producing JSON payload for " + pingDateString);
|
|
|
|
|
2012-11-13 20:22:09 -08:00
|
|
|
let o = {
|
|
|
|
version: 1,
|
2013-01-06 12:13:27 -08:00
|
|
|
thisPingDate: pingDateString,
|
|
|
|
data: {last: {}, days: {}},
|
2012-11-13 20:22:09 -08:00
|
|
|
};
|
|
|
|
|
2013-01-06 12:13:27 -08:00
|
|
|
let outputDataDays = o.data.days;
|
|
|
|
|
2013-02-13 16:32:45 -08:00
|
|
|
// Guard here in case we don't track this (e.g., on Android).
|
2012-11-13 20:22:09 -08:00
|
|
|
let lastPingDate = this.lastPingDate;
|
2013-02-13 16:32:45 -08:00
|
|
|
if (lastPingDate && lastPingDate.getTime() > 0) {
|
2012-11-13 20:22:09 -08:00
|
|
|
o.lastPingDate = this._formatDate(lastPingDate);
|
|
|
|
}
|
|
|
|
|
2013-01-06 12:13:27 -08:00
|
|
|
for (let provider of this._collector.providers) {
|
|
|
|
let providerName = provider.name;
|
|
|
|
|
|
|
|
let providerEntry = {
|
|
|
|
measurements: {},
|
|
|
|
};
|
|
|
|
|
|
|
|
for (let [measurementKey, measurement] of provider.measurements) {
|
2013-01-25 00:32:33 -08:00
|
|
|
let name = providerName + "." + measurement.name;
|
2013-01-06 12:13:27 -08:00
|
|
|
|
|
|
|
let serializer;
|
|
|
|
try {
|
2013-01-25 00:32:33 -08:00
|
|
|
// The measurement is responsible for returning a serializer which
|
|
|
|
// is aware of the measurement version.
|
2013-01-06 12:13:27 -08:00
|
|
|
serializer = measurement.serializer(measurement.SERIALIZE_JSON);
|
|
|
|
} catch (ex) {
|
2013-02-27 16:52:26 -08:00
|
|
|
this._recordError("Error obtaining serializer for measurement: " +
|
|
|
|
name, ex);
|
2013-01-06 12:13:27 -08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let data;
|
|
|
|
try {
|
2013-01-19 00:31:38 -08:00
|
|
|
data = yield measurement.getValues();
|
2013-01-06 12:13:27 -08:00
|
|
|
} catch (ex) {
|
2013-02-27 16:52:26 -08:00
|
|
|
this._recordError("Error obtaining data for measurement: " + name,
|
|
|
|
ex);
|
2013-01-06 12:13:27 -08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data.singular.size) {
|
|
|
|
try {
|
|
|
|
o.data.last[name] = serializer.singular(data.singular);
|
|
|
|
} catch (ex) {
|
2013-02-27 16:52:26 -08:00
|
|
|
this._recordError("Error serializing singular data: " + name,
|
|
|
|
ex);
|
2013-01-06 12:13:27 -08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let dataDays = data.days;
|
|
|
|
for (let i = 0; i < DAYS_IN_PAYLOAD; i++) {
|
|
|
|
let date = new Date(now.getTime() - i * MILLISECONDS_PER_DAY);
|
|
|
|
if (!dataDays.hasDay(date)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let dateFormatted = this._formatDate(date);
|
|
|
|
|
|
|
|
try {
|
|
|
|
let serialized = serializer.daily(dataDays.getDay(date));
|
|
|
|
if (!serialized) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(dateFormatted in outputDataDays)) {
|
|
|
|
outputDataDays[dateFormatted] = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
outputDataDays[dateFormatted][name] = serialized;
|
|
|
|
} catch (ex) {
|
2013-02-27 16:52:26 -08:00
|
|
|
this._recordError("Error populating data for day: " + name, ex);
|
2013-01-06 12:13:27 -08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-27 16:52:26 -08:00
|
|
|
if (this._errors.length) {
|
|
|
|
o.errors = this._errors.slice(0, 20);
|
2012-11-13 20:22:09 -08:00
|
|
|
}
|
|
|
|
|
2013-01-25 00:39:01 -08:00
|
|
|
this._storage.compact();
|
2013-02-05 20:31:48 -08:00
|
|
|
|
2013-02-18 12:45:53 -08:00
|
|
|
if (!asObject) {
|
|
|
|
TelemetryStopwatch.start(TELEMETRY_JSON_PAYLOAD_SERIALIZE, this);
|
|
|
|
o = JSON.stringify(o);
|
|
|
|
TelemetryStopwatch.finish(TELEMETRY_JSON_PAYLOAD_SERIALIZE, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Task.Result(o);
|
2012-11-13 20:22:09 -08:00
|
|
|
},
|
|
|
|
|
2013-02-13 16:32:45 -08:00
|
|
|
get _stateDir() {
|
|
|
|
let profD = OS.Constants.Path.profileDir;
|
2012-11-13 20:22:09 -08:00
|
|
|
|
2013-02-13 16:32:45 -08:00
|
|
|
// Work around bug 810543 until OS.File is more resilient.
|
|
|
|
if (!profD || !profD.length) {
|
|
|
|
throw new Error("Could not obtain profile directory. OS.File not " +
|
|
|
|
"initialized properly?");
|
2012-11-13 20:22:09 -08:00
|
|
|
}
|
|
|
|
|
2013-02-13 16:32:45 -08:00
|
|
|
return OS.Path.join(profD, "healthreport");
|
|
|
|
},
|
2012-11-13 20:22:09 -08:00
|
|
|
|
2013-02-13 16:32:45 -08:00
|
|
|
_ensureDirectoryExists: function (path) {
|
|
|
|
let deferred = Promise.defer();
|
2012-11-13 20:22:09 -08:00
|
|
|
|
2013-02-13 16:32:45 -08:00
|
|
|
OS.File.makeDir(path).then(
|
|
|
|
function onResult() {
|
|
|
|
deferred.resolve(true);
|
|
|
|
},
|
|
|
|
function onError(error) {
|
|
|
|
if (error.becauseExists) {
|
|
|
|
deferred.resolve(true);
|
|
|
|
return;
|
|
|
|
}
|
2012-11-13 20:22:09 -08:00
|
|
|
|
2013-02-13 16:32:45 -08:00
|
|
|
deferred.reject(error);
|
|
|
|
}
|
|
|
|
);
|
2012-11-13 20:22:09 -08:00
|
|
|
|
2013-02-13 16:32:45 -08:00
|
|
|
return deferred.promise;
|
2012-11-13 20:22:09 -08:00
|
|
|
},
|
|
|
|
|
2013-02-13 16:32:45 -08:00
|
|
|
get _lastPayloadPath() {
|
|
|
|
return OS.Path.join(this._stateDir, "lastpayload.json");
|
2012-11-13 20:22:09 -08:00
|
|
|
},
|
|
|
|
|
2013-02-13 16:32:45 -08:00
|
|
|
_saveLastPayload: function (payload) {
|
|
|
|
let path = this._lastPayloadPath;
|
|
|
|
let pathTmp = path + ".tmp";
|
2012-11-13 20:22:09 -08:00
|
|
|
|
2013-02-13 16:32:45 -08:00
|
|
|
let encoder = new TextEncoder();
|
|
|
|
let buffer = encoder.encode(payload);
|
2012-11-13 20:22:09 -08:00
|
|
|
|
2013-02-13 16:32:45 -08:00
|
|
|
return OS.File.writeAtomic(path, buffer, {tmpPath: pathTmp});
|
|
|
|
},
|
2012-11-13 20:22:09 -08:00
|
|
|
|
2013-02-13 16:32:45 -08:00
|
|
|
/**
|
|
|
|
* Obtain the last uploaded payload.
|
|
|
|
*
|
|
|
|
* The promise is resolved to a JSON-decoded object on success. The promise
|
|
|
|
* is rejected if the last uploaded payload could not be found or there was
|
|
|
|
* an error reading or parsing it.
|
|
|
|
*
|
|
|
|
* This reads the last payload from disk. If you are looking for a
|
|
|
|
* current snapshot of the data, see `getJSONPayload` and
|
|
|
|
* `collectAndObtainJSONPayload`.
|
|
|
|
*
|
|
|
|
* @return Promise<object>
|
|
|
|
*/
|
|
|
|
getLastPayload: function () {
|
|
|
|
let path = this._lastPayloadPath;
|
|
|
|
|
|
|
|
return OS.File.read(path).then(
|
|
|
|
function onData(buffer) {
|
|
|
|
let decoder = new TextDecoder();
|
|
|
|
let json = JSON.parse(decoder.decode(buffer));
|
|
|
|
|
|
|
|
return Promise.resolve(json);
|
|
|
|
},
|
|
|
|
function onError(error) {
|
|
|
|
return Promise.reject(error);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
_now: function _now() {
|
|
|
|
return new Date();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* HealthReporter and its abstract superclass coordinate collection and
|
|
|
|
* submission of health report metrics.
|
|
|
|
*
|
|
|
|
* This is the main type for Firefox Health Report on desktop. It glues all the
|
|
|
|
* lower-level components (such as collection and submission) together.
|
|
|
|
*
|
|
|
|
* An instance of this type is created as an XPCOM service. See
|
|
|
|
* DataReportingService.js and
|
|
|
|
* DataReporting.manifest/HealthReportComponents.manifest.
|
|
|
|
*
|
|
|
|
* It is theoretically possible to have multiple instances of this running
|
|
|
|
* in the application. For example, this type may one day handle submission
|
|
|
|
* of telemetry data as well. However, there is some moderate coupling between
|
|
|
|
* this type and *the* Firefox Health Report (e.g., the policy). This could
|
|
|
|
* be abstracted if needed.
|
|
|
|
*
|
|
|
|
* Note that `AbstractHealthReporter` exists to allow for Firefox Health Report
|
|
|
|
* to be more easily implemented on platforms where a separate controlling
|
|
|
|
* layer is responsible for payload upload and deletion.
|
|
|
|
*
|
|
|
|
* IMPLEMENTATION NOTES
|
|
|
|
* ====================
|
|
|
|
*
|
|
|
|
* These notes apply to the combination of `HealthReporter` and
|
|
|
|
* `AbstractHealthReporter`.
|
|
|
|
*
|
|
|
|
* Initialization and shutdown are somewhat complicated and worth explaining
|
|
|
|
* in extra detail.
|
|
|
|
*
|
|
|
|
* The complexity is driven by the requirements of SQLite connection management.
|
|
|
|
* Once you have a SQLite connection, it isn't enough to just let the
|
|
|
|
* application shut down. If there is an open connection or if there are
|
|
|
|
* outstanding SQL statements come XPCOM shutdown time, Storage will assert.
|
|
|
|
* On debug builds you will crash. On release builds you will get a shutdown
|
|
|
|
* hang. This must be avoided!
|
|
|
|
*
|
|
|
|
* During initialization, the second we create a SQLite connection (via
|
|
|
|
* Metrics.Storage) we register observers for application shutdown. The
|
|
|
|
* "quit-application" notification initiates our shutdown procedure. The
|
|
|
|
* subsequent "profile-do-change" notification ensures it has completed.
|
|
|
|
*
|
|
|
|
* The handler for "profile-do-change" may result in event loop spinning. This
|
|
|
|
* is because of race conditions between our shutdown code and application
|
|
|
|
* shutdown.
|
|
|
|
*
|
|
|
|
* All of our shutdown routines are async. There is the potential that these
|
|
|
|
* async functions will not complete before XPCOM shutdown. If they don't
|
|
|
|
* finish in time, we could get assertions in Storage. Our solution is to
|
|
|
|
* initiate storage early in the shutdown cycle ("quit-application").
|
|
|
|
* Hopefully all the async operations have completed by the time we reach
|
|
|
|
* "profile-do-change." If so, great. If not, we spin the event loop until
|
|
|
|
* they have completed, avoiding potential race conditions.
|
|
|
|
*
|
|
|
|
* @param branch
|
|
|
|
* (string) The preferences branch to use for state storage. The value
|
|
|
|
* must end with a period (.).
|
|
|
|
*
|
|
|
|
* @param policy
|
|
|
|
* (HealthReportPolicy) Policy driving execution of HealthReporter.
|
|
|
|
*/
|
|
|
|
function HealthReporter(branch, policy, sessionRecorder) {
|
|
|
|
AbstractHealthReporter.call(this, branch, policy, sessionRecorder);
|
|
|
|
|
|
|
|
if (!this.serverURI) {
|
|
|
|
throw new Error("No server URI defined. Did you forget to define the pref?");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.serverNamespace) {
|
|
|
|
throw new Error("No server namespace defined. Did you forget a pref?");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
HealthReporter.prototype = Object.freeze({
|
|
|
|
__proto__: AbstractHealthReporter.prototype,
|
|
|
|
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver]),
|
|
|
|
|
|
|
|
/**
|
|
|
|
* When we last successfully submitted data to the server.
|
|
|
|
*
|
|
|
|
* This is sent as part of the upload. This is redundant with similar data
|
|
|
|
* in the policy because we like the modules to be loosely coupled and the
|
|
|
|
* similar data in the policy is only used for forensic purposes.
|
|
|
|
*/
|
|
|
|
get lastPingDate() {
|
|
|
|
return CommonUtils.getDatePref(this._prefs, "lastPingTime", 0, this._log,
|
|
|
|
OLDEST_ALLOWED_YEAR);
|
|
|
|
},
|
|
|
|
|
|
|
|
set lastPingDate(value) {
|
|
|
|
CommonUtils.setDatePref(this._prefs, "lastPingTime", value,
|
|
|
|
OLDEST_ALLOWED_YEAR);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The base URI of the document server to which to submit data.
|
|
|
|
*
|
|
|
|
* This is typically a Bagheera server instance. It is the URI up to but not
|
|
|
|
* including the version prefix. e.g. https://data.metrics.mozilla.com/
|
|
|
|
*/
|
|
|
|
get serverURI() {
|
|
|
|
return this._prefs.get("documentServerURI", null);
|
|
|
|
},
|
|
|
|
|
|
|
|
set serverURI(value) {
|
|
|
|
if (!value) {
|
|
|
|
throw new Error("serverURI must have a value.");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof(value) != "string") {
|
|
|
|
throw new Error("serverURI must be a string: " + value);
|
|
|
|
}
|
|
|
|
|
|
|
|
this._prefs.set("documentServerURI", value);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The namespace on the document server to which we will be submitting data.
|
|
|
|
*/
|
|
|
|
get serverNamespace() {
|
|
|
|
return this._prefs.get("documentServerNamespace", "metrics");
|
|
|
|
},
|
|
|
|
|
|
|
|
set serverNamespace(value) {
|
|
|
|
if (!value) {
|
|
|
|
throw new Error("serverNamespace must have a value.");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof(value) != "string") {
|
|
|
|
throw new Error("serverNamespace must be a string: " + value);
|
|
|
|
}
|
|
|
|
|
|
|
|
this._prefs.set("documentServerNamespace", value);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The document ID for data to be submitted to the server.
|
|
|
|
*
|
|
|
|
* This should be a UUID.
|
|
|
|
*
|
|
|
|
* We generate a new UUID when we upload data to the server. When we get a
|
|
|
|
* successful response for that upload, we record that UUID in this value.
|
|
|
|
* On the subsequent upload, this ID will be deleted from the server.
|
|
|
|
*/
|
|
|
|
get lastSubmitID() {
|
|
|
|
return this._prefs.get("lastSubmitID", null) || null;
|
|
|
|
},
|
|
|
|
|
|
|
|
set lastSubmitID(value) {
|
|
|
|
this._prefs.set("lastSubmitID", value || "");
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether this instance will upload data to a server.
|
|
|
|
*/
|
|
|
|
get willUploadData() {
|
|
|
|
return this._policy.dataSubmissionPolicyAccepted &&
|
|
|
|
this._policy.healthReportUploadEnabled;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether remote data is currently stored.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
haveRemoteData: function () {
|
|
|
|
return !!this.lastSubmitID;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called to initiate a data upload.
|
|
|
|
*
|
|
|
|
* The passed argument is a `DataSubmissionRequest` from policy.jsm.
|
|
|
|
*/
|
|
|
|
requestDataUpload: function (request) {
|
|
|
|
return Task.spawn(function doUpload() {
|
2013-02-18 13:05:07 -08:00
|
|
|
yield this.ensurePullOnlyProvidersRegistered();
|
2013-02-13 16:32:45 -08:00
|
|
|
try {
|
|
|
|
yield this.collectMeasurements();
|
|
|
|
try {
|
|
|
|
yield this._uploadData(request);
|
|
|
|
} catch (ex) {
|
|
|
|
this._onSubmitDataRequestFailure(ex);
|
|
|
|
}
|
|
|
|
} finally {
|
2013-02-18 13:05:07 -08:00
|
|
|
yield this.ensurePullOnlyProvidersUnregistered();
|
2013-02-13 16:32:45 -08:00
|
|
|
}
|
|
|
|
}.bind(this));
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Request that server data be deleted.
|
|
|
|
*
|
|
|
|
* If deletion is scheduled to occur immediately, a promise will be returned
|
|
|
|
* that will be fulfilled when the deletion attempt finishes. Otherwise,
|
|
|
|
* callers should poll haveRemoteData() to determine when remote data is
|
|
|
|
* deleted.
|
|
|
|
*/
|
|
|
|
requestDeleteRemoteData: function (reason) {
|
|
|
|
if (!this.lastSubmitID) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this._policy.deleteRemoteData(reason);
|
|
|
|
},
|
|
|
|
|
|
|
|
_onBagheeraResult: function (request, isDelete, result) {
|
|
|
|
this._log.debug("Received Bagheera result.");
|
|
|
|
|
|
|
|
let promise = Promise.resolve(null);
|
|
|
|
|
|
|
|
if (!result.transportSuccess) {
|
|
|
|
request.onSubmissionFailureSoft("Network transport error.");
|
|
|
|
return promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!result.serverSuccess) {
|
|
|
|
request.onSubmissionFailureHard("Server failure.");
|
|
|
|
return promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
let now = this._now();
|
|
|
|
|
|
|
|
if (isDelete) {
|
|
|
|
this.lastSubmitID = null;
|
|
|
|
} else {
|
|
|
|
this.lastSubmitID = result.id;
|
|
|
|
this.lastPingDate = now;
|
|
|
|
}
|
|
|
|
|
|
|
|
request.onSubmissionSuccess(now);
|
|
|
|
|
|
|
|
return promise;
|
|
|
|
},
|
|
|
|
|
|
|
|
_onSubmitDataRequestFailure: function (error) {
|
|
|
|
this._log.error("Error processing request to submit data: " +
|
|
|
|
CommonUtils.exceptionStr(error));
|
|
|
|
},
|
|
|
|
|
|
|
|
_formatDate: function (date) {
|
|
|
|
// Why, oh, why doesn't JS have a strftime() equivalent?
|
|
|
|
return date.toISOString().substr(0, 10);
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
_uploadData: function (request) {
|
|
|
|
let id = CommonUtils.generateUUID();
|
|
|
|
|
|
|
|
this._log.info("Uploading data to server: " + this.serverURI + " " +
|
|
|
|
this.serverNamespace + ":" + id);
|
|
|
|
let client = new BagheeraClient(this.serverURI);
|
|
|
|
|
|
|
|
return Task.spawn(function doUpload() {
|
2013-01-06 12:13:27 -08:00
|
|
|
let payload = yield this.getJSONPayload();
|
2013-02-01 13:28:02 -08:00
|
|
|
|
|
|
|
let histogram = Services.telemetry.getHistogramById(TELEMETRY_PAYLOAD_SIZE);
|
|
|
|
histogram.add(payload.length);
|
|
|
|
|
|
|
|
TelemetryStopwatch.start(TELEMETRY_SAVE_LAST_PAYLOAD, this);
|
|
|
|
try {
|
|
|
|
yield this._saveLastPayload(payload);
|
|
|
|
TelemetryStopwatch.finish(TELEMETRY_SAVE_LAST_PAYLOAD, this);
|
|
|
|
} catch (ex) {
|
|
|
|
TelemetryStopwatch.cancel(TELEMETRY_SAVE_LAST_PAYLOAD, this);
|
|
|
|
throw ex;
|
|
|
|
}
|
|
|
|
|
|
|
|
TelemetryStopwatch.start(TELEMETRY_UPLOAD, this);
|
|
|
|
let result;
|
|
|
|
try {
|
|
|
|
result = yield client.uploadJSON(this.serverNamespace, id, payload,
|
|
|
|
this.lastSubmitID);
|
|
|
|
TelemetryStopwatch.finish(TELEMETRY_UPLOAD, this);
|
|
|
|
} catch (ex) {
|
|
|
|
TelemetryStopwatch.cancel(TELEMETRY_UPLOAD, this);
|
|
|
|
throw ex;
|
|
|
|
}
|
|
|
|
|
2013-01-06 12:13:27 -08:00
|
|
|
yield this._onBagheeraResult(request, false, result);
|
|
|
|
}.bind(this));
|
2012-11-13 20:22:09 -08:00
|
|
|
},
|
|
|
|
|
2013-02-05 20:31:48 -08:00
|
|
|
/**
|
|
|
|
* Request deletion of remote data.
|
|
|
|
*
|
|
|
|
* @param request
|
|
|
|
* (DataSubmissionRequest) Tracks progress of this request.
|
|
|
|
*/
|
2013-01-11 13:45:22 -08:00
|
|
|
deleteRemoteData: function (request) {
|
2012-11-13 20:22:09 -08:00
|
|
|
if (!this.lastSubmitID) {
|
|
|
|
this._log.info("Received request to delete remote data but no data stored.");
|
|
|
|
request.onNoDataAvailable();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._log.warn("Deleting remote data.");
|
|
|
|
let client = new BagheeraClient(this.serverURI);
|
|
|
|
|
|
|
|
return client.deleteDocument(this.serverNamespace, this.lastSubmitID)
|
|
|
|
.then(this._onBagheeraResult.bind(this, request, true),
|
|
|
|
this._onSubmitDataRequestFailure.bind(this));
|
2012-11-16 10:05:19 -08:00
|
|
|
},
|
2013-01-06 12:13:27 -08:00
|
|
|
});
|
2012-11-13 20:22:09 -08:00
|
|
|
|