Bug 1845134 - Part 4: Update existing ui-icons to use the latest source from acorn...
[gecko.git] / services / fxaccounts / FxAccountsTelemetry.sys.mjs
blob1d7b3d49544373bc8b22471d9696ca17dc7b34bc
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3  * You can obtain one at http://mozilla.org/MPL/2.0/. */
5 // FxA Telemetry support. For hysterical raisins, the actual implementation
6 // is inside "sync". We should move the core implementation somewhere that's
7 // sanely shared (eg, services-common?), but let's wait and see where we end up
8 // first...
10 import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs";
12 const lazy = {};
14 ChromeUtils.defineESModuleGetters(lazy, {
15   CryptoUtils: "resource://services-crypto/utils.sys.mjs",
17   // We use this observers module because we leverage its support for richer
18   // "subject" data.
19   Observers: "resource://services-common/observers.sys.mjs",
20 });
22 import {
23   PREF_ACCOUNT_ROOT,
24   log,
25 } from "resource://gre/modules/FxAccountsCommon.sys.mjs";
27 const PREF_SANITIZED_UID = PREF_ACCOUNT_ROOT + "telemetry.sanitized_uid";
28 XPCOMUtils.defineLazyPreferenceGetter(
29   lazy,
30   "pref_sanitizedUid",
31   PREF_SANITIZED_UID,
32   ""
35 export class FxAccountsTelemetry {
36   constructor(fxai) {
37     this._fxai = fxai;
38     Services.telemetry.setEventRecordingEnabled("fxa", true);
39   }
41   // Records an event *in the Fxa/Sync ping*.
42   recordEvent(object, method, value, extra = undefined) {
43     // We need to ensure the telemetry module is loaded.
44     ChromeUtils.importESModule("resource://services-sync/telemetry.sys.mjs");
45     // Now it will be listening for the notifications...
46     lazy.Observers.notify("fxa:telemetry:event", {
47       object,
48       method,
49       value,
50       extra,
51     });
52   }
54   generateUUID() {
55     return Services.uuid.generateUUID().toString().slice(1, -1);
56   }
58   // A flow ID can be anything that's "probably" unique, so for now use a UUID.
59   generateFlowID() {
60     return this.generateUUID();
61   }
63   // FxA- and Sync-related metrics are submitted in a special-purpose "sync ping". This ping
64   // identifies the user by a version of their FxA uid that is HMAC-ed with a server-side secret
65   // key, in an attempt to provide a bit of anonymity.
67   // Secret back-channel by which tokenserver client code can set the hashed UID.
68   // This value conceptually belongs to FxA, but we currently get it from tokenserver,
69   // so there's some light hackery to put it in the right place.
70   _setHashedUID(hashedUID) {
71     if (!hashedUID) {
72       Services.prefs.clearUserPref(PREF_SANITIZED_UID);
73     } else {
74       Services.prefs.setStringPref(PREF_SANITIZED_UID, hashedUID);
75     }
76   }
78   getSanitizedUID() {
79     // Sadly, we can only currently obtain this value if the user has enabled sync.
80     return lazy.pref_sanitizedUid || null;
81   }
83   // Sanitize the ID of a device into something suitable for including in the
84   // ping. Returns null if no transformation is possible.
85   sanitizeDeviceId(deviceId) {
86     const uid = this.getSanitizedUID();
87     if (!uid) {
88       // Sadly, we can only currently get this if the user has enabled sync.
89       return null;
90     }
91     // Combine the raw device id with the sanitized uid to create a stable
92     // unique identifier that can't be mapped back to the user's FxA
93     // identity without knowing the metrics HMAC key.
94     // The result is 64 bytes long, which in retrospect is probably excessive,
95     // but it's already shipping...
96     return lazy.CryptoUtils.sha256(deviceId + uid);
97   }
99   // Record the connection of FxA or one of its services.
100   // Note that you must call this before performing the actual connection
101   // or we may record incorrect data - for example, we will not be able to
102   // determine whether FxA itself was connected before this call.
103   //
104   // Currently sends an event in the main telemetry event ping rather than the
105   // FxA/Sync ping (although this might change in the future)
106   //
107   // @param services - An array of service names which should be recorded. FxA
108   //  itself is not counted as a "service" - ie, an empty array should be passed
109   //  if the account is connected without anything else .
110   //
111   // @param how - How the connection was done.
112   async recordConnection(services, how = null) {
113     try {
114       let extra = {};
115       // Record that fxa was connected if it isn't currently - it will be soon.
116       if (!(await this._fxai.getUserAccountData())) {
117         extra.fxa = "true";
118       }
119       // Events.yaml only declares "sync" as a valid service.
120       if (services.includes("sync")) {
121         extra.sync = "true";
122       }
123       Services.telemetry.recordEvent("fxa", "connect", "account", how, extra);
124     } catch (ex) {
125       log.error("Failed to record connection telemetry", ex);
126       console.error("Failed to record connection telemetry", ex);
127     }
128   }
130   // Record the disconnection of FxA or one of its services.
131   // Note that you must call this before performing the actual disconnection
132   // or we may record incomplete data - for example, if this is called after
133   // disconnection, we've almost certainly lost the ability to record what
134   // services were enabled prior to disconnection.
135   //
136   // Currently sends an event in the main telemetry event ping rather than the
137   // FxA/Sync ping (although this might change in the future)
138   //
139   // @param service - the service being disconnected. If null, the account
140   // itself is being disconnected, so all connected services are too.
141   //
142   // @param how - how the disconnection was done.
143   async recordDisconnection(service = null, how = null) {
144     try {
145       let extra = {};
146       if (!service) {
147         extra.fxa = "true";
148         // We need a way to enumerate all services - but for now we just hard-code
149         // all possibilities here.
150         if (Services.prefs.prefHasUserValue("services.sync.username")) {
151           extra.sync = "true";
152         }
153       } else if (service == "sync") {
154         extra[service] = "true";
155       } else {
156         // Events.yaml only declares "sync" as a valid service.
157         log.warn(
158           `recordDisconnection has invalid value for service: ${service}`
159         );
160       }
161       Services.telemetry.recordEvent(
162         "fxa",
163         "disconnect",
164         "account",
165         how,
166         extra
167       );
168     } catch (ex) {
169       log.error("Failed to record disconnection telemetry", ex);
170       console.error("Failed to record disconnection telemetry", ex);
171     }
172   }