Bug 1694515 - Part 3: Add BackgroundTasksUtils method for reading Telemetry client ID. r=bytesized,chutten

This will be used from the `backgroundupdate` task to fish the default
profile's Telemetry client ID in order to correlate the task's Glean
ping with regular main pings.

Differential Revision: https://phabricator.services.mozilla.com/D107712
This commit is contained in:
Nick Alexander 2021-03-12 04:08:42 +00:00
Родитель d60f45d4c0
Коммит 1ea5f174c2
2 изменённых файлов: 71 добавлений и 0 удалений

Просмотреть файл

@ -154,4 +154,37 @@ var BackgroundTasksUtils = {
log.debug(`readPreferences: parsed prefs from buffer`, prefs);
return prefs;
},
/**
* Reads the Telemetry Client ID out of a profile.
*
* If no `lock` is given, the default profile is locked and the preferences
* read from it. If `lock` is given, read from the given lock's directory.
*
* @param {nsIProfileLock} [lock] optional lock to use
* @returns {string}
*/
async readTelemetryClientID(lock = null) {
if (!lock) {
return this.withProfileLock(profileLock =>
this.readTelemetryClientID(profileLock)
);
}
this._throwIfNotLocked(lock);
let stateFile = lock.directory.clone();
stateFile.append("datareporting");
stateFile.append("state.json");
log.info(
`readPreferences: will read Telemetry client ID from ${stateFile.path}`
);
// This JSON is always UTF-8.
let data = await IOUtils.readUTF8(stateFile.path);
let state = JSON.parse(data);
return state.clientID;
},
};

Просмотреть файл

@ -97,3 +97,41 @@ add_task(async function test_readPreferences() {
};
Assert.deepEqual(prefs, expected, "Filtered prefs read are correct");
});
add_task(async function test_readTelemetryClientID() {
let profileService = Cc["@mozilla.org/toolkit/profile-service;1"].getService(
Ci.nsIToolkitProfileService
);
let profilePath = do_get_profile();
profilePath.append(`test_readTelemetryClientID`);
let profile = profileService.createUniqueProfile(
profilePath,
"test_readTelemetryClientID"
);
// Before we write any state, we fail to read.
await Assert.rejects(
BackgroundTasksUtils.withProfileLock(
lock => BackgroundTasksUtils.readTelemetryClientID(lock),
profile
),
/NotFoundError/
);
let expected = {
clientID: "9cc75672-6830-4cb6-a7fd-089d6c7ce34c",
ecosystemClientID: "752f9d53-73fc-4006-a93c-d3e2e427f238",
};
let prefsFile = profile.rootDir.clone();
prefsFile.append("datareporting");
prefsFile.append("state.json");
await IOUtils.writeUTF8(prefsFile.path, JSON.stringify(expected));
// Now we can read the state.
let state = await BackgroundTasksUtils.withProfileLock(
lock => BackgroundTasksUtils.readTelemetryClientID(lock),
profile
);
Assert.deepEqual(state, expected.clientID, "State read is correct");
});