Bug 1882817 - Export some mmultiscripts reftests to WPT. r=emilio
[gecko.git] / toolkit / components / resistfingerprinting / nsUserCharacteristics.cpp
blobb3e826a6f55d40af0d88eb71bb75d2852c027976
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "nsUserCharacteristics.h"
8 #include "nsID.h"
9 #include "nsIUUIDGenerator.h"
11 #include "mozilla/Logging.h"
12 #include "mozilla/glean/GleanPings.h"
13 #include "mozilla/glean/GleanMetrics.h"
15 static mozilla::LazyLogModule gUserCharacteristicsLog("UserCharacteristics");
17 // The current schema of the data. Anytime you add a metric, or change how a
18 // metric is set, this variable should be incremented. It'll be a lot. It's
19 // okay. We're going to need it to know (including during development) what is
20 // the source of the data we are looking at.
21 const int kSubmissionSchema = 0;
23 /* static */
24 void nsUserCharacteristics::MaybeSubmitPing() {
25 MOZ_LOG(gUserCharacteristicsLog, LogLevel::Debug, ("In MaybeSubmitPing()"));
27 /**
28 * There are two preferences at play here:
29 * - Last Version Sent - preference containing the last version sent by the
30 * user to Mozilla
31 * - Current Version - preference containing the version Mozilla would like
32 * the user to send
34 * A point of complexity arises in that these two values _may_ be changed
35 * by the user, even though neither is intended to be.
37 * When Current Version > Last Version Sent, we intend for the user to submit
38 * a new ping, which will include the schema version. Then update Last Version
39 * Sent = Current Version.
42 const auto* const kLastVersionPref =
43 "toolkit.telemetry.user_characteristics_ping.last_version_sent";
44 const auto* const kCurrentVersionPref =
45 "toolkit.telemetry.user_characteristics_ping.current_version";
47 auto lastSubmissionVersion = Preferences::GetInt(kLastVersionPref, 0);
48 auto currentVersion = Preferences::GetInt(kCurrentVersionPref, 0);
50 MOZ_ASSERT(currentVersion == -1 || lastSubmissionVersion <= currentVersion,
51 "lastSubmissionVersion is somehow greater than currentVersion "
52 "- did you edit prefs improperly?");
54 if (lastSubmissionVersion < 0) {
55 // This is a way for users to opt out of this ping specifically.
56 MOZ_LOG(gUserCharacteristicsLog, LogLevel::Debug,
57 ("Returning, User Opt-out"));
58 return;
60 if (currentVersion == 0) {
61 // Do nothing. We do not want any pings.
62 MOZ_LOG(gUserCharacteristicsLog, LogLevel::Debug,
63 ("Returning, currentVersion == 0"));
64 return;
66 if (currentVersion == -1) {
67 // currentVersion = -1 is a development value to force a ping submission
68 MOZ_LOG(gUserCharacteristicsLog, LogLevel::Debug,
69 ("Force-Submitting Ping"));
70 if (NS_SUCCEEDED(PopulateData())) {
71 SubmitPing();
73 return;
75 if (lastSubmissionVersion > currentVersion) {
76 // This is an unexpected scneario that indicates something is wrong. We
77 // asserted against it (in debug, above) We will try to sanity-correct
78 // ourselves by setting it to the current version.
79 Preferences::SetInt(kLastVersionPref, currentVersion);
80 MOZ_LOG(gUserCharacteristicsLog, LogLevel::Warning,
81 ("Returning, lastSubmissionVersion > currentVersion"));
82 return;
84 if (lastSubmissionVersion == currentVersion) {
85 // We are okay, we've already submitted the most recent ping
86 MOZ_LOG(gUserCharacteristicsLog, LogLevel::Warning,
87 ("Returning, lastSubmissionVersion == currentVersion"));
88 return;
90 if (lastSubmissionVersion < currentVersion) {
91 if (NS_SUCCEEDED(PopulateData())) {
92 if (NS_SUCCEEDED(SubmitPing())) {
93 Preferences::SetInt(kLastVersionPref, currentVersion);
96 } else {
97 MOZ_ASSERT_UNREACHABLE("Should never reach here");
101 const auto* const kUUIDPref =
102 "toolkit.telemetry.user_characteristics_ping.uuid";
104 /* static */
105 nsresult nsUserCharacteristics::PopulateData() {
106 MOZ_LOG(gUserCharacteristicsLog, LogLevel::Warning, ("Populating Data"));
107 mozilla::glean::characteristics::submission_schema.Set(kSubmissionSchema);
109 nsAutoCString uuidString;
110 nsresult rv = Preferences::GetCString(kUUIDPref, uuidString);
111 if (NS_FAILED(rv) || uuidString.Length() == 0) {
112 nsCOMPtr<nsIUUIDGenerator> uuidgen =
113 do_GetService("@mozilla.org/uuid-generator;1", &rv);
114 NS_ENSURE_SUCCESS(rv, rv);
116 nsIDToCString id(nsID::GenerateUUID());
117 uuidString = id.get();
118 Preferences::SetCString(kUUIDPref, uuidString);
120 mozilla::glean::characteristics::client_identifier.Set(uuidString);
122 return NS_OK;
125 /* static */
126 nsresult nsUserCharacteristics::SubmitPing() {
127 MOZ_LOG(gUserCharacteristicsLog, LogLevel::Warning, ("Submitting Ping"));
128 mozilla::glean_pings::UserCharacteristics.Submit();
130 return NS_OK;