2017-08-08 21:41:15 +03: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/. */
|
|
|
|
|
2019-01-17 21:18:31 +03:00
|
|
|
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
|
2017-08-08 21:41:15 +03:00
|
|
|
|
2018-01-30 08:17:48 +03:00
|
|
|
ChromeUtils.defineModuleGetter(
|
|
|
|
this,
|
|
|
|
"AppConstants",
|
2017-08-08 21:41:15 +03:00
|
|
|
"resource://gre/modules/AppConstants.jsm"
|
|
|
|
);
|
2018-09-25 18:01:42 +03:00
|
|
|
ChromeUtils.defineModuleGetter(
|
|
|
|
this,
|
|
|
|
"UpdateUtils",
|
|
|
|
"resource://gre/modules/UpdateUtils.jsm"
|
|
|
|
);
|
2018-01-30 08:17:48 +03:00
|
|
|
ChromeUtils.defineModuleGetter(
|
|
|
|
this,
|
|
|
|
"TelemetryEnvironment",
|
2017-09-08 17:43:37 +03:00
|
|
|
"resource://gre/modules/TelemetryEnvironment.jsm"
|
|
|
|
);
|
2019-09-26 23:01:28 +03:00
|
|
|
ChromeUtils.defineModuleGetter(
|
|
|
|
this,
|
|
|
|
"ServiceRequest",
|
|
|
|
"resource://gre/modules/ServiceRequest.jsm"
|
|
|
|
);
|
2017-08-08 21:41:15 +03:00
|
|
|
|
|
|
|
const PREF_BRANCH = "browser.ping-centre.";
|
|
|
|
|
|
|
|
const TELEMETRY_PREF = `${PREF_BRANCH}telemetry`;
|
|
|
|
const LOGGING_PREF = `${PREF_BRANCH}log`;
|
2019-09-26 23:01:28 +03:00
|
|
|
const STRUCTURED_INGESTION_SEND_TIMEOUT = 30 * 1000; // 30 seconds
|
2017-08-08 21:41:15 +03:00
|
|
|
|
|
|
|
const FHR_UPLOAD_ENABLED_PREF = "datareporting.healthreport.uploadEnabled";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Observe various notifications and send them to a telemetry endpoint.
|
|
|
|
*
|
|
|
|
* @param {Object} options
|
|
|
|
* @param {string} options.topic - a unique ID for users of PingCentre to distinguish
|
|
|
|
* their data on the server side.
|
|
|
|
*/
|
|
|
|
class PingCentre {
|
|
|
|
constructor(options) {
|
|
|
|
if (!options.topic) {
|
|
|
|
throw new Error("Must specify topic.");
|
|
|
|
}
|
|
|
|
|
|
|
|
this._topic = options.topic;
|
|
|
|
this._prefs = Services.prefs.getBranch("");
|
|
|
|
|
|
|
|
this._enabled = this._prefs.getBoolPref(TELEMETRY_PREF);
|
|
|
|
this._onTelemetryPrefChange = this._onTelemetryPrefChange.bind(this);
|
|
|
|
this._prefs.addObserver(TELEMETRY_PREF, this._onTelemetryPrefChange);
|
|
|
|
|
|
|
|
this._fhrEnabled = this._prefs.getBoolPref(FHR_UPLOAD_ENABLED_PREF);
|
|
|
|
this._onFhrPrefChange = this._onFhrPrefChange.bind(this);
|
|
|
|
this._prefs.addObserver(FHR_UPLOAD_ENABLED_PREF, this._onFhrPrefChange);
|
|
|
|
|
|
|
|
this.logging = this._prefs.getBoolPref(LOGGING_PREF);
|
|
|
|
this._onLoggingPrefChange = this._onLoggingPrefChange.bind(this);
|
|
|
|
this._prefs.addObserver(LOGGING_PREF, this._onLoggingPrefChange);
|
|
|
|
}
|
|
|
|
|
|
|
|
get enabled() {
|
|
|
|
return this._enabled && this._fhrEnabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
_onLoggingPrefChange(aSubject, aTopic, prefKey) {
|
|
|
|
this.logging = this._prefs.getBoolPref(prefKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
_onTelemetryPrefChange(aSubject, aTopic, prefKey) {
|
|
|
|
this._enabled = this._prefs.getBoolPref(prefKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
_onFhrPrefChange(aSubject, aTopic, prefKey) {
|
|
|
|
this._fhrEnabled = this._prefs.getBoolPref(prefKey);
|
|
|
|
}
|
|
|
|
|
2020-03-25 23:05:43 +03:00
|
|
|
_createExperimentsPayload() {
|
2020-11-30 22:47:32 +03:00
|
|
|
let activeExperiments = TelemetryEnvironment.getActiveExperiments();
|
2020-12-10 22:19:45 +03:00
|
|
|
let experiments = {};
|
2017-09-08 17:43:37 +03:00
|
|
|
for (let experimentID in activeExperiments) {
|
|
|
|
if (
|
2020-03-25 23:05:43 +03:00
|
|
|
activeExperiments[experimentID] &&
|
|
|
|
activeExperiments[experimentID].branch
|
2017-09-27 15:28:06 +03:00
|
|
|
) {
|
2020-03-25 23:05:43 +03:00
|
|
|
experiments[experimentID] = {
|
|
|
|
branch: activeExperiments[experimentID].branch,
|
|
|
|
};
|
2018-01-13 01:12:57 +03:00
|
|
|
}
|
|
|
|
}
|
2020-03-25 23:05:43 +03:00
|
|
|
return experiments;
|
2018-01-13 01:12:57 +03:00
|
|
|
}
|
|
|
|
|
2020-03-25 23:05:43 +03:00
|
|
|
_createStructuredIngestionPing(data) {
|
|
|
|
let experiments = this._createExperimentsPayload();
|
2020-01-17 22:29:09 +03:00
|
|
|
let locale = data.locale || Services.locale.appLocaleAsBCP47;
|
2020-03-25 23:05:43 +03:00
|
|
|
const payload = {
|
|
|
|
experiments,
|
|
|
|
locale,
|
|
|
|
version: AppConstants.MOZ_APP_VERSION,
|
|
|
|
release_channel: UpdateUtils.getUpdateChannel(false),
|
|
|
|
...data,
|
|
|
|
};
|
2019-05-02 01:55:10 +03:00
|
|
|
|
|
|
|
return payload;
|
|
|
|
}
|
|
|
|
|
2019-09-26 23:01:28 +03:00
|
|
|
static _gzipCompressString(string) {
|
|
|
|
let observer = {
|
|
|
|
buffer: "",
|
|
|
|
onStreamComplete(loader, context, status, length, result) {
|
|
|
|
this.buffer = String.fromCharCode(...result);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
let scs = Cc["@mozilla.org/streamConverters;1"].getService(
|
|
|
|
Ci.nsIStreamConverterService
|
|
|
|
);
|
|
|
|
let listener = Cc["@mozilla.org/network/stream-loader;1"].createInstance(
|
|
|
|
Ci.nsIStreamLoader
|
|
|
|
);
|
|
|
|
listener.init(observer);
|
|
|
|
let converter = scs.asyncConvertData(
|
|
|
|
"uncompressed",
|
|
|
|
"gzip",
|
|
|
|
listener,
|
|
|
|
null
|
|
|
|
);
|
|
|
|
let stringStream = Cc[
|
|
|
|
"@mozilla.org/io/string-input-stream;1"
|
|
|
|
].createInstance(Ci.nsIStringInputStream);
|
|
|
|
stringStream.data = string;
|
|
|
|
converter.onStartRequest(null, null);
|
|
|
|
converter.onDataAvailable(null, stringStream, 0, string.length);
|
|
|
|
converter.onStopRequest(null, null, null);
|
|
|
|
return observer.buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
static _sendInGzip(endpoint, payload) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let request = new ServiceRequest({ mozAnon: true });
|
|
|
|
request.mozBackgroundRequest = true;
|
|
|
|
request.timeout = STRUCTURED_INGESTION_SEND_TIMEOUT;
|
|
|
|
|
|
|
|
request.open("POST", endpoint, true);
|
|
|
|
request.overrideMimeType("text/plain");
|
|
|
|
request.setRequestHeader(
|
|
|
|
"Content-Type",
|
|
|
|
"application/json; charset=UTF-8"
|
|
|
|
);
|
|
|
|
request.setRequestHeader("Content-Encoding", "gzip");
|
|
|
|
request.setRequestHeader("Date", new Date().toUTCString());
|
|
|
|
|
|
|
|
request.onload = event => {
|
|
|
|
if (request.status !== 200) {
|
|
|
|
reject(event);
|
|
|
|
} else {
|
|
|
|
resolve(event);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
request.onerror = reject;
|
|
|
|
request.onabort = reject;
|
|
|
|
request.ontimeout = reject;
|
|
|
|
|
|
|
|
let payloadStream = Cc[
|
|
|
|
"@mozilla.org/io/string-input-stream;1"
|
|
|
|
].createInstance(Ci.nsIStringInputStream);
|
|
|
|
payloadStream.data = PingCentre._gzipCompressString(payload);
|
|
|
|
request.sendInputStream(payloadStream);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-03-04 22:15:21 +03:00
|
|
|
/**
|
|
|
|
* Sends a ping to the Structured Ingestion telemetry pipeline.
|
|
|
|
*
|
2019-09-26 23:01:28 +03:00
|
|
|
* The payload would be compressed using gzip.
|
|
|
|
*
|
2019-03-04 22:15:21 +03:00
|
|
|
* @param {Object} data The payload to be sent.
|
|
|
|
* @param {String} endpoint The destination endpoint. Note that Structured Ingestion
|
|
|
|
* requires a different endpoint for each ping. It's up to the
|
|
|
|
* caller to provide that. See more details at
|
|
|
|
* https://github.com/mozilla/gcp-ingestion/blob/master/docs/edge.md#postput-request
|
|
|
|
*/
|
2020-03-25 23:05:43 +03:00
|
|
|
sendStructuredIngestionPing(data, endpoint) {
|
2019-03-04 22:15:21 +03:00
|
|
|
if (!this.enabled) {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
2020-03-25 23:05:43 +03:00
|
|
|
const ping = this._createStructuredIngestionPing(data);
|
2019-09-26 23:01:28 +03:00
|
|
|
const payload = JSON.stringify(ping);
|
2019-05-02 01:55:10 +03:00
|
|
|
|
|
|
|
if (this.logging) {
|
|
|
|
Services.console.logStringMessage(
|
2020-03-25 23:05:43 +03:00
|
|
|
`TELEMETRY PING (${this._topic}): ${payload}\n`
|
2019-05-02 01:55:10 +03:00
|
|
|
);
|
|
|
|
}
|
2019-03-04 22:15:21 +03:00
|
|
|
|
2019-09-26 23:01:28 +03:00
|
|
|
return PingCentre._sendInGzip(endpoint, payload).catch(event => {
|
|
|
|
Cu.reportError(
|
|
|
|
`Structured Ingestion ping failure with error: ${event.type}`
|
|
|
|
);
|
|
|
|
});
|
2019-03-04 22:15:21 +03:00
|
|
|
}
|
|
|
|
|
2017-08-08 21:41:15 +03:00
|
|
|
uninit() {
|
|
|
|
try {
|
|
|
|
this._prefs.removeObserver(TELEMETRY_PREF, this._onTelemetryPrefChange);
|
|
|
|
this._prefs.removeObserver(LOGGING_PREF, this._onLoggingPrefChange);
|
|
|
|
this._prefs.removeObserver(
|
|
|
|
FHR_UPLOAD_ENABLED_PREF,
|
|
|
|
this._onFhrPrefChange
|
|
|
|
);
|
|
|
|
} catch (e) {
|
|
|
|
Cu.reportError(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.PingCentre = PingCentre;
|
2018-03-01 22:21:00 +03:00
|
|
|
this.PingCentreConstants = {
|
2017-08-08 21:41:15 +03:00
|
|
|
FHR_UPLOAD_ENABLED_PREF,
|
|
|
|
TELEMETRY_PREF,
|
|
|
|
LOGGING_PREF,
|
|
|
|
};
|
2018-03-01 22:21:00 +03:00
|
|
|
const EXPORTED_SYMBOLS = ["PingCentre", "PingCentreConstants"];
|