Bug 1958316 - Support setting a pref to a string of the current timestamp via Special Message Action r=omc-reviewers,hanna_a

- Allow for configuration of `SET_PREF` SMA value to include a `timestamp` property. When this has a truthy value, the pref is set to a string of the current timestamp in milliseconds.
  - Adds data reporting prefs to the allowed prefs to list

Differential Revision: https://phabricator.services.mozilla.com/D244650
This commit is contained in:
Meg Viar
2025-04-09 14:09:13 +00:00
parent 357d0e78d6
commit 20f3dafb0f
3 changed files with 45 additions and 2 deletions

View File

@@ -228,6 +228,10 @@ export const SpecialMessageActions = {
"sidebar.visibility",
"browser.crashReports.unsubmittedCheck.autoSubmit2",
"datareporting.healthreport.uploadEnabled",
"datareporting.policy.currentPolicyVersion",
"datareporting.policy.dataSubmissionPolicyAcceptedVersion",
"datareporting.policy.dataSubmissionPolicyNotifiedTime",
"datareporting.policy.minimumPolicyVersion",
];
if (
@@ -239,6 +243,12 @@ export const SpecialMessageActions = {
// If pref has no value, reset it, otherwise set it to desired value
switch (typeof pref.value) {
case "object":
if (pref.value.timestamp) {
Services.prefs.setStringPref(pref.name, Date.now().toString());
} else {
Services.prefs.clearUserPref(pref.name);
}
break;
case "undefined":
Services.prefs.clearUserPref(pref.name);
break;

View File

@@ -541,10 +541,10 @@
"type": "string"
},
"value": {
"type": ["boolean", "string", "number", "null"]
"type": ["boolean", "string", "number", "null", "object"]
}
},
"description": "An object representing a pref containing a name and a value."
"description": "An object representing a pref containing a name and a value. The value can be an object with the property `timestamp` with a truthy value to set the pref to a string of the current time in ms."
}
},
"required": ["pref"],

View File

@@ -6,10 +6,12 @@
const HOMEPAGE_PREF = "browser.startup.homepage";
const PRIVACY_SEGMENTATION_PREF = "browser.dataFeatureRecommendations.enabled";
const MESSAGING_ACTION_PREF = "special-message-testpref";
const TIMESTAMP_PREF = "browser.shopping.experience2023.survey.optedInTime";
const PREFS_TO_CLEAR = [
HOMEPAGE_PREF,
PRIVACY_SEGMENTATION_PREF,
TIMESTAMP_PREF,
`messaging-system-action.${MESSAGING_ACTION_PREF}`,
];
@@ -165,3 +167,34 @@ add_task(async function test_clear_messaging_system_pref() {
`messaging-system-action.${MESSAGING_ACTION_PREF} pref successfully cleared`
);
});
add_task(async function test_set_pref_to_timestamp_string() {
Assert.ok(!Services.prefs.prefHasUserValue(TIMESTAMP_PREF), "Test setup ok");
const action = {
type: "SET_PREF",
data: {
pref: {
name: TIMESTAMP_PREF,
value: { timestamp: true },
},
},
};
const before = Date.now();
await SMATestUtils.executeAndValidateAction(action);
const after = Date.now();
Assert.ok(
Services.prefs.prefHasUserValue(TIMESTAMP_PREF),
"Timestamp pref successfully set"
);
const value = Services.prefs.getStringPref(TIMESTAMP_PREF);
const timestamp = parseInt(value, 10);
Assert.ok(!isNaN(timestamp), "Timestamp value is a valid number");
Assert.ok(
timestamp >= before && timestamp <= after,
"Timestamp is within the expected time range"
);
});