From 1ea5f174c2094abfe5fa6876d58bd56c28d5336b Mon Sep 17 00:00:00 2001 From: Nick Alexander Date: Fri, 12 Mar 2021 04:08:42 +0000 Subject: [PATCH] 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 --- .../backgroundtasks/BackgroundTasksUtils.jsm | 33 ++++++++++++++++ .../xpcshell/test_backgroundtasksutils.js | 38 +++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/toolkit/components/backgroundtasks/BackgroundTasksUtils.jsm b/toolkit/components/backgroundtasks/BackgroundTasksUtils.jsm index 5ba0877030d0..28a7bdfdb826 100644 --- a/toolkit/components/backgroundtasks/BackgroundTasksUtils.jsm +++ b/toolkit/components/backgroundtasks/BackgroundTasksUtils.jsm @@ -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; + }, }; diff --git a/toolkit/components/backgroundtasks/tests/xpcshell/test_backgroundtasksutils.js b/toolkit/components/backgroundtasks/tests/xpcshell/test_backgroundtasksutils.js index d62916c51fba..dcabf3af57f5 100644 --- a/toolkit/components/backgroundtasks/tests/xpcshell/test_backgroundtasksutils.js +++ b/toolkit/components/backgroundtasks/tests/xpcshell/test_backgroundtasksutils.js @@ -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"); +});