2015-04-03 08:53:00 +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/. */
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2018-02-23 22:50:01 +03:00
|
|
|
var EXPORTED_SYMBOLS = ["ClientID"];
|
2015-04-03 08:53:00 +03:00
|
|
|
|
2019-01-17 21:18:31 +03:00
|
|
|
const {XPCOMUtils} = ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
|
|
|
|
const {Log} = ChromeUtils.import("resource://gre/modules/Log.jsm");
|
|
|
|
const {AppConstants} = ChromeUtils.import("resource://gre/modules/AppConstants.jsm");
|
2015-09-21 10:49:49 +03:00
|
|
|
|
|
|
|
const LOGGER_NAME = "Toolkit.Telemetry";
|
|
|
|
const LOGGER_PREFIX = "ClientID::";
|
2018-10-26 21:01:13 +03:00
|
|
|
// Must match ID in TelemetryUtils
|
|
|
|
const CANARY_CLIENT_ID = "c0ffeec0-ffee-c0ff-eec0-ffeec0ffeec0";
|
2015-04-03 08:53:00 +03:00
|
|
|
|
2018-01-30 02:20:18 +03:00
|
|
|
ChromeUtils.defineModuleGetter(this, "CommonUtils",
|
|
|
|
"resource://services-common/utils.js");
|
|
|
|
ChromeUtils.defineModuleGetter(this, "OS",
|
|
|
|
"resource://gre/modules/osfile.jsm");
|
2015-04-03 08:53:00 +03:00
|
|
|
|
2018-11-26 18:26:39 +03:00
|
|
|
XPCOMUtils.defineLazyGetter(this, "CryptoHash", () => {
|
|
|
|
return Components.Constructor("@mozilla.org/security/hash;1", "nsICryptoHash", "initWithString");
|
|
|
|
});
|
|
|
|
|
2015-04-03 08:53:00 +03:00
|
|
|
XPCOMUtils.defineLazyGetter(this, "gDatareportingPath", () => {
|
|
|
|
return OS.Path.join(OS.Constants.Path.profileDir, "datareporting");
|
|
|
|
});
|
|
|
|
|
|
|
|
XPCOMUtils.defineLazyGetter(this, "gStateFilePath", () => {
|
|
|
|
return OS.Path.join(gDatareportingPath, "state.json");
|
|
|
|
});
|
|
|
|
|
2015-08-11 13:17:29 +03:00
|
|
|
const PREF_CACHED_CLIENTID = "toolkit.telemetry.cachedClientID";
|
|
|
|
|
2015-09-21 10:49:49 +03:00
|
|
|
/**
|
|
|
|
* Checks if client ID has a valid format.
|
|
|
|
*
|
|
|
|
* @param {String} id A string containing the client ID.
|
|
|
|
* @return {Boolean} True when the client ID has valid format, or False
|
|
|
|
* otherwise.
|
|
|
|
*/
|
|
|
|
function isValidClientID(id) {
|
|
|
|
const UUID_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
|
|
return UUID_REGEX.test(id);
|
|
|
|
}
|
|
|
|
|
2018-02-23 22:50:01 +03:00
|
|
|
var ClientID = Object.freeze({
|
2015-04-03 08:53:00 +03:00
|
|
|
/**
|
|
|
|
* This returns a promise resolving to the the stable client ID we use for
|
2018-01-19 01:51:24 +03:00
|
|
|
* data reporting (FHR & Telemetry).
|
2015-04-03 08:53:00 +03:00
|
|
|
*
|
2016-02-18 02:31:26 +03:00
|
|
|
* WARNING: This functionality is duplicated for Android (see GeckoProfile.getClientId
|
|
|
|
* for more). There are Java tests (TestGeckoProfile) to ensure the functionality is
|
|
|
|
* consistent and Gecko tests to come (bug 1249156). However, THIS IS NOT FOOLPROOF.
|
|
|
|
* Be careful when changing this code and, in particular, the underlying file format.
|
|
|
|
*
|
2015-04-03 08:53:00 +03:00
|
|
|
* @return {Promise<string>} The stable client ID.
|
|
|
|
*/
|
2016-12-30 02:34:54 +03:00
|
|
|
getClientID() {
|
2015-04-03 08:53:00 +03:00
|
|
|
return ClientIDImpl.getClientID();
|
|
|
|
},
|
|
|
|
|
2018-10-26 21:01:13 +03:00
|
|
|
/**
|
|
|
|
* This returns true if the client ID prior to the last client ID reset was a canary client ID.
|
|
|
|
* Android only. Always returns null on Desktop.
|
|
|
|
*/
|
|
|
|
wasCanaryClientID() {
|
|
|
|
if (AppConstants.platform == "android") {
|
|
|
|
return ClientIDImpl.wasCanaryClientID();
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
2018-07-10 15:51:01 +03:00
|
|
|
/**
|
2015-08-11 13:17:29 +03:00
|
|
|
* Get the client id synchronously without hitting the disk.
|
|
|
|
* This returns:
|
|
|
|
* - the current on-disk client id if it was already loaded
|
|
|
|
* - the client id that we cached into preferences (if any)
|
|
|
|
* - null otherwise
|
|
|
|
*/
|
2016-12-30 02:34:54 +03:00
|
|
|
getCachedClientID() {
|
2015-08-11 13:17:29 +03:00
|
|
|
return ClientIDImpl.getCachedClientID();
|
|
|
|
},
|
|
|
|
|
2018-11-26 18:26:39 +03:00
|
|
|
async getClientIdHash() {
|
|
|
|
return ClientIDImpl.getClientIdHash();
|
|
|
|
},
|
|
|
|
|
2018-07-10 15:51:01 +03:00
|
|
|
/**
|
|
|
|
* Set a specific client id asynchronously, writing it to disk
|
|
|
|
* and updating the cached version.
|
|
|
|
*
|
|
|
|
* Should only ever be used when a known client ID value should be set.
|
|
|
|
* Use `resetClientID` to generate a new random one if required.
|
|
|
|
*
|
|
|
|
* @return {Promise<string>} The stable client ID.
|
|
|
|
*/
|
|
|
|
setClientID(id) {
|
|
|
|
return ClientIDImpl.setClientID(id);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset the client id asynchronously, writing it to disk
|
|
|
|
* and updating the cached version.
|
|
|
|
*
|
|
|
|
* Should only be used if a reset is explicitely requested by the user.
|
|
|
|
*
|
|
|
|
* @return {Promise<string>} A new stable client ID.
|
|
|
|
*/
|
|
|
|
resetClientID() {
|
|
|
|
return ClientIDImpl.resetClientID();
|
|
|
|
},
|
|
|
|
|
2015-04-03 08:53:00 +03:00
|
|
|
/**
|
|
|
|
* Only used for testing. Invalidates the client ID so that it gets read
|
|
|
|
* again from file.
|
|
|
|
*/
|
2016-12-30 02:34:54 +03:00
|
|
|
_reset() {
|
2015-04-03 08:53:00 +03:00
|
|
|
return ClientIDImpl._reset();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2015-09-15 21:19:45 +03:00
|
|
|
var ClientIDImpl = {
|
2015-04-03 08:53:00 +03:00
|
|
|
_clientID: null,
|
2018-11-26 18:26:39 +03:00
|
|
|
_clientIDHash: null,
|
2015-04-03 08:53:00 +03:00
|
|
|
_loadClientIdTask: null,
|
|
|
|
_saveClientIdTask: null,
|
2018-07-10 15:51:01 +03:00
|
|
|
_removeClientIdTask: null,
|
2015-09-21 10:49:49 +03:00
|
|
|
_logger: null,
|
2018-10-26 21:01:13 +03:00
|
|
|
_wasCanary: null,
|
2015-04-03 08:53:00 +03:00
|
|
|
|
2016-12-30 02:34:54 +03:00
|
|
|
_loadClientID() {
|
2015-04-03 08:53:00 +03:00
|
|
|
if (this._loadClientIdTask) {
|
|
|
|
return this._loadClientIdTask;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._loadClientIdTask = this._doLoadClientID();
|
|
|
|
let clear = () => this._loadClientIdTask = null;
|
|
|
|
this._loadClientIdTask.then(clear, clear);
|
|
|
|
return this._loadClientIdTask;
|
|
|
|
},
|
|
|
|
|
2018-01-19 01:51:24 +03:00
|
|
|
/**
|
|
|
|
* Load the Client ID from the DataReporting Service state file.
|
|
|
|
* If no Client ID is found, we generate a new one.
|
|
|
|
*/
|
2017-05-12 15:42:39 +03:00
|
|
|
async _doLoadClientID() {
|
2018-07-10 15:51:01 +03:00
|
|
|
// If there's a removal in progress, let's wait for it
|
|
|
|
await this._removeClientIdTask;
|
|
|
|
|
2018-01-19 01:51:24 +03:00
|
|
|
// Try to load the client id from the DRS state file.
|
2015-04-03 08:53:00 +03:00
|
|
|
try {
|
2017-05-12 15:42:39 +03:00
|
|
|
let state = await CommonUtils.readJSON(gStateFilePath);
|
2018-10-26 21:01:13 +03:00
|
|
|
if (AppConstants.platform == "android" && state && "wasCanary" in state) {
|
|
|
|
this._wasCanary = state.wasCanary;
|
|
|
|
}
|
2015-09-21 10:49:49 +03:00
|
|
|
if (state && this.updateClientID(state.clientID)) {
|
2015-04-03 08:53:00 +03:00
|
|
|
return this._clientID;
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
// fall through to next option
|
|
|
|
}
|
|
|
|
|
2018-01-19 01:51:24 +03:00
|
|
|
// We dont have an id from the DRS state file yet, generate a new ID.
|
2015-09-21 10:49:49 +03:00
|
|
|
this.updateClientID(CommonUtils.generateUUID());
|
2015-04-03 08:53:00 +03:00
|
|
|
this._saveClientIdTask = this._saveClientID();
|
|
|
|
|
|
|
|
// Wait on persisting the id. Otherwise failure to save the ID would result in
|
|
|
|
// the client creating and subsequently sending multiple IDs to the server.
|
|
|
|
// This would appear as multiple clients submitting similar data, which would
|
|
|
|
// result in orphaning.
|
2017-05-12 15:42:39 +03:00
|
|
|
await this._saveClientIdTask;
|
2015-04-03 08:53:00 +03:00
|
|
|
|
|
|
|
return this._clientID;
|
2017-05-12 15:42:39 +03:00
|
|
|
},
|
2015-04-03 08:53:00 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Save the client ID to the client ID file.
|
|
|
|
*
|
|
|
|
* @return {Promise} A promise resolved when the client ID is saved to disk.
|
|
|
|
*/
|
2017-05-12 15:42:39 +03:00
|
|
|
async _saveClientID() {
|
2015-04-03 08:53:00 +03:00
|
|
|
let obj = { clientID: this._clientID };
|
2018-10-26 21:01:13 +03:00
|
|
|
// We detected a canary client ID when resetting, storing this as a flag
|
|
|
|
if (AppConstants.platform == "android" && this._wasCanary) {
|
|
|
|
obj.wasCanary = true;
|
|
|
|
}
|
2017-05-12 15:42:39 +03:00
|
|
|
await OS.File.makeDir(gDatareportingPath);
|
|
|
|
await CommonUtils.writeJSON(obj, gStateFilePath);
|
2015-04-03 08:53:00 +03:00
|
|
|
this._saveClientIdTask = null;
|
2017-05-12 15:42:39 +03:00
|
|
|
},
|
2015-04-03 08:53:00 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This returns a promise resolving to the the stable client ID we use for
|
2018-01-19 01:51:24 +03:00
|
|
|
* data reporting (FHR & Telemetry).
|
2015-04-03 08:53:00 +03:00
|
|
|
*
|
|
|
|
* @return {Promise<string>} The stable client ID.
|
|
|
|
*/
|
2016-12-30 02:34:54 +03:00
|
|
|
getClientID() {
|
2015-04-03 08:53:00 +03:00
|
|
|
if (!this._clientID) {
|
|
|
|
return this._loadClientID();
|
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.resolve(this._clientID);
|
|
|
|
},
|
|
|
|
|
2018-10-26 21:01:13 +03:00
|
|
|
/**
|
|
|
|
* This returns true if the client ID prior to the last client ID reset was a canary client ID.
|
|
|
|
* Android only. Always returns null on Desktop.
|
|
|
|
*/
|
|
|
|
wasCanaryClientID() {
|
|
|
|
return this._wasCanary;
|
|
|
|
},
|
|
|
|
|
2015-08-11 13:17:29 +03:00
|
|
|
/**
|
|
|
|
* Get the client id synchronously without hitting the disk.
|
|
|
|
* This returns:
|
|
|
|
* - the current on-disk client id if it was already loaded
|
|
|
|
* - the client id that we cached into preferences (if any)
|
|
|
|
* - null otherwise
|
|
|
|
*/
|
2016-12-30 02:34:54 +03:00
|
|
|
getCachedClientID() {
|
2015-08-11 13:17:29 +03:00
|
|
|
if (this._clientID) {
|
|
|
|
// Already loaded the client id from disk.
|
|
|
|
return this._clientID;
|
|
|
|
}
|
|
|
|
|
2017-10-11 17:10:29 +03:00
|
|
|
// If the client id cache contains a value of the wrong type,
|
|
|
|
// reset the pref. We need to do this before |getStringPref| since
|
|
|
|
// it will just return |null| in that case and we won't be able
|
|
|
|
// to distinguish between the missing pref and wrong type cases.
|
|
|
|
if (Services.prefs.prefHasUserValue(PREF_CACHED_CLIENTID) &&
|
|
|
|
Services.prefs.getPrefType(PREF_CACHED_CLIENTID) != Ci.nsIPrefBranch.PREF_STRING) {
|
2017-07-29 01:55:58 +03:00
|
|
|
this._log.error("getCachedClientID - invalid client id type in preferences, resetting");
|
|
|
|
Services.prefs.clearUserPref(PREF_CACHED_CLIENTID);
|
|
|
|
}
|
|
|
|
|
2017-10-11 17:10:29 +03:00
|
|
|
// Not yet loaded, return the cached client id if we have one.
|
2017-07-29 01:55:58 +03:00
|
|
|
let id = Services.prefs.getStringPref(PREF_CACHED_CLIENTID, null);
|
2015-11-09 18:20:00 +03:00
|
|
|
if (id === null) {
|
|
|
|
return null;
|
|
|
|
}
|
2015-09-21 10:49:49 +03:00
|
|
|
if (!isValidClientID(id)) {
|
|
|
|
this._log.error("getCachedClientID - invalid client id in preferences, resetting", id);
|
2017-07-29 01:55:58 +03:00
|
|
|
Services.prefs.clearUserPref(PREF_CACHED_CLIENTID);
|
2015-09-21 10:49:49 +03:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return id;
|
2015-08-11 13:17:29 +03:00
|
|
|
},
|
|
|
|
|
2018-11-26 18:26:39 +03:00
|
|
|
async getClientIdHash() {
|
|
|
|
if (!this._clientIDHash) {
|
|
|
|
let byteArr = new TextEncoder().encode(await this.getClientID());
|
|
|
|
let hash = new CryptoHash("sha256");
|
|
|
|
hash.update(byteArr, byteArr.length);
|
|
|
|
this._clientIDHash = CommonUtils.bytesAsHex(hash.finish(false));
|
|
|
|
}
|
|
|
|
return this._clientIDHash;
|
|
|
|
},
|
|
|
|
|
2015-04-03 08:53:00 +03:00
|
|
|
/*
|
|
|
|
* Resets the provider. This is for testing only.
|
|
|
|
*/
|
2017-05-12 15:42:39 +03:00
|
|
|
async _reset() {
|
|
|
|
await this._loadClientIdTask;
|
|
|
|
await this._saveClientIdTask;
|
2015-04-03 08:53:00 +03:00
|
|
|
this._clientID = null;
|
2018-11-26 18:26:39 +03:00
|
|
|
this._clientIDHash = null;
|
2017-05-12 15:42:39 +03:00
|
|
|
},
|
2015-09-21 10:49:49 +03:00
|
|
|
|
2018-07-10 15:51:01 +03:00
|
|
|
async setClientID(id) {
|
|
|
|
if (!this.updateClientID(id)) {
|
|
|
|
throw ("Invalid client ID: " + id);
|
|
|
|
}
|
|
|
|
|
|
|
|
this._saveClientIdTask = this._saveClientID();
|
|
|
|
await this._saveClientIdTask;
|
|
|
|
return this._clientID;
|
|
|
|
},
|
|
|
|
|
|
|
|
async _doRemoveClientID() {
|
|
|
|
// Reset stored id.
|
|
|
|
this._clientID = null;
|
2018-11-26 18:26:39 +03:00
|
|
|
this._clientIDHash = null;
|
2018-07-10 15:51:01 +03:00
|
|
|
|
|
|
|
// Clear the client id from the preference cache.
|
|
|
|
Services.prefs.clearUserPref(PREF_CACHED_CLIENTID);
|
|
|
|
|
|
|
|
// Remove the client id from disk
|
|
|
|
await OS.File.remove(gStateFilePath, {ignoreAbsent: true});
|
|
|
|
},
|
|
|
|
|
|
|
|
async resetClientID() {
|
2018-10-26 21:01:13 +03:00
|
|
|
let oldClientId = this._clientID;
|
|
|
|
|
2018-07-10 15:51:01 +03:00
|
|
|
// Wait for the removal.
|
|
|
|
// Asynchronous calls to getClientID will also be blocked on this.
|
|
|
|
this._removeClientIdTask = this._doRemoveClientID();
|
|
|
|
let clear = () => this._removeClientIdTask = null;
|
|
|
|
this._removeClientIdTask.then(clear, clear);
|
|
|
|
|
|
|
|
await this._removeClientIdTask;
|
|
|
|
|
2018-10-26 21:01:13 +03:00
|
|
|
// On Android we detect resets after a canary client ID.
|
|
|
|
if (AppConstants.platform == "android" ) {
|
|
|
|
this._wasCanary = oldClientId == CANARY_CLIENT_ID;
|
|
|
|
}
|
|
|
|
|
2018-07-10 15:51:01 +03:00
|
|
|
// Generate a new id.
|
|
|
|
return this.getClientID();
|
|
|
|
},
|
|
|
|
|
2015-09-21 10:49:49 +03:00
|
|
|
/**
|
|
|
|
* Sets the client id to the given value and updates the value cached in
|
|
|
|
* preferences only if the given id is a valid.
|
|
|
|
*
|
|
|
|
* @param {String} id A string containing the client ID.
|
|
|
|
* @return {Boolean} True when the client ID has valid format, or False
|
|
|
|
* otherwise.
|
|
|
|
*/
|
2016-12-30 02:34:54 +03:00
|
|
|
updateClientID(id) {
|
2015-09-21 10:49:49 +03:00
|
|
|
if (!isValidClientID(id)) {
|
|
|
|
this._log.error("updateClientID - invalid client ID", id);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._clientID = id;
|
2018-11-26 18:26:39 +03:00
|
|
|
this._clientIDHash = null;
|
2017-07-29 01:55:58 +03:00
|
|
|
Services.prefs.setStringPref(PREF_CACHED_CLIENTID, this._clientID);
|
2015-09-21 10:49:49 +03:00
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A helper for getting access to telemetry logger.
|
|
|
|
*/
|
|
|
|
get _log() {
|
|
|
|
if (!this._logger) {
|
|
|
|
this._logger = Log.repository.getLoggerWithMessagePrefix(LOGGER_NAME, LOGGER_PREFIX);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this._logger;
|
|
|
|
},
|
2015-04-03 08:53:00 +03:00
|
|
|
};
|