Bug 1406475 - Convert some sync modules to using Services.prefs. r=sync-reviewers,skhamis

Differential Revision: https://phabricator.services.mozilla.com/D180803
This commit is contained in:
Marco Castelluccio 2023-06-14 10:00:06 +00:00
parent 3588a7d013
commit 5fac802934
7 changed files with 74 additions and 41 deletions

View File

@ -9,7 +9,6 @@ const lazy = {};
ChromeUtils.defineESModuleGetters(lazy, {
CLIENT_NOT_CONFIGURED: "resource://services-sync/constants.sys.mjs",
Preferences: "resource://gre/modules/Preferences.sys.mjs",
Weave: "resource://services-sync/main.sys.mjs",
});
@ -118,7 +117,7 @@ let SyncedTabsInternal = {
}
// A boolean that controls whether we should show the icon from the remote tab.
const showRemoteIcons = lazy.Preferences.get(
const showRemoteIcons = Services.prefs.getBoolPref(
"services.sync.syncedTabs.showRemoteIcons",
true
);
@ -161,7 +160,10 @@ let SyncedTabsInternal = {
async syncTabs(force) {
if (!force) {
// Don't bother refetching tabs if we already did so recently
let lastFetch = lazy.Preferences.get("services.sync.lastTabFetch", 0);
let lastFetch = Services.prefs.getIntPref(
"services.sync.lastTabFetch",
0
);
let now = Math.floor(Date.now() / 1000);
if (now - lastFetch < TABS_FRESH_ENOUGH_INTERVAL_SECONDS) {
lazy.log.info("_refetchTabs was done recently, do not doing it again");
@ -206,7 +208,7 @@ let SyncedTabsInternal = {
// The tabs engine just finished syncing
// Set our lastTabFetch pref here so it tracks both explicit sync calls
// and normally scheduled ones.
lazy.Preferences.set(
Services.prefs.setIntPref(
"services.sync.lastTabFetch",
Math.floor(Date.now() / 1000)
);
@ -214,7 +216,7 @@ let SyncedTabsInternal = {
break;
case "weave:service:start-over":
// start-over needs to notify so consumers find no tabs.
lazy.Preferences.reset("services.sync.lastTabFetch");
Services.prefs.clearUserPref("services.sync.lastTabFetch");
Services.obs.notifyObservers(null, TOPIC_TABS_CHANGED);
break;
case "nsPref:changed":

View File

@ -15,7 +15,8 @@ ChromeUtils.defineESModuleGetters(lazy, {
function AddonUtilsInternal() {
this._log = Log.repository.getLogger("Sync.AddonUtils");
this._log.Level = Log.Level[Svc.Prefs.get("log.logger.addonutils")];
this._log.Level =
Log.Level[Svc.PrefBranch.getCharPref("log.logger.addonutils", null)];
}
AddonUtilsInternal.prototype = {
/**

View File

@ -33,13 +33,18 @@ export var Doctor = {
let result = {};
for (let e of recentlySyncedEngines) {
let prefPrefix = `engine.${e.name}.`;
if (!Svc.Prefs.get(prefPrefix + "validation.enabled", false)) {
if (
!Svc.PrefBranch.getBoolPref(prefPrefix + "validation.enabled", false)
) {
log.info(`Skipping check of ${e.name} - disabled via preferences`);
continue;
}
// Check the last validation time for the engine.
let lastValidation = Svc.Prefs.get(prefPrefix + "validation.lastTime", 0);
let validationInterval = Svc.Prefs.get(
let lastValidation = Svc.PrefBranch.getIntPref(
prefPrefix + "validation.lastTime",
0
);
let validationInterval = Svc.PrefBranch.getIntPref(
prefPrefix + "validation.interval"
);
let nowSeconds = this._now();
@ -53,11 +58,17 @@ export var Doctor = {
// Update the time now, even if we decline to actually perform a
// validation. We don't want to check the rest of these more frequently
// than once a day.
Svc.Prefs.set(prefPrefix + "validation.lastTime", Math.floor(nowSeconds));
Svc.PrefBranch.setIntPref(
prefPrefix + "validation.lastTime",
Math.floor(nowSeconds)
);
// Validation only occurs a certain percentage of the time.
let validationProbability =
Svc.Prefs.get(prefPrefix + "validation.percentageChance", 0) / 100.0;
Svc.PrefBranch.getIntPref(
prefPrefix + "validation.percentageChance",
0
) / 100.0;
if (validationProbability < Math.random()) {
log.info(
`Skipping validation of ${e.name}: Probability threshold not met`
@ -65,7 +76,9 @@ export var Doctor = {
continue;
}
let maxRecords = Svc.Prefs.get(prefPrefix + "validation.maxRecords");
let maxRecords = Svc.PrefBranch.getIntPref(
prefPrefix + "validation.maxRecords"
);
if (!maxRecords) {
log.info(`Skipping validation of ${e.name}: No maxRecords specified`);
continue;

View File

@ -71,7 +71,7 @@ XPCOMUtils.defineLazyServiceGetter(
// from themselves (and us from them!) the minimum time they can specify
// is 60s.
function getThrottledIntervalPreference(prefName) {
return Math.max(Svc.Prefs.get(prefName), 60) * 1000;
return Math.max(Svc.PrefBranch.getIntPref(prefName), 60) * 1000;
}
export function SyncScheduler(service) {
@ -126,10 +126,10 @@ SyncScheduler.prototype = {
// nextSync is in milliseconds, but prefs can't hold that much
get nextSync() {
return Svc.Prefs.get("nextSync", 0) * 1000;
return Svc.PrefBranch.getIntPref("nextSync", 0) * 1000;
},
set nextSync(value) {
Svc.Prefs.set("nextSync", Math.floor(value / 1000));
Svc.PrefBranch.setIntPref("nextSync", Math.floor(value / 1000));
},
get missedFxACommandsFetchInterval() {
@ -913,7 +913,7 @@ ErrorHandler.prototype = {
) {
// Great. Let's clear our mid-sync 401 note.
this._log.trace("Clearing lastSyncReassigned.");
Svc.Prefs.reset("lastSyncReassigned");
Svc.PrefBranch.clearUserPref("lastSyncReassigned");
}
if (lazy.Status.service == SYNC_FAILED_PARTIAL) {
@ -927,7 +927,7 @@ ErrorHandler.prototype = {
.then(() => {
// although for privacy reasons we also delete all logs (but we allow
// a preference to avoid this to help with debugging.)
if (!Svc.Prefs.get("log.keepLogsOnReset", false)) {
if (!Svc.PrefBranch.getBoolPref("log.keepLogsOnReset", false)) {
return logManager.removeAllLogs().then(() => {
Svc.Obs.notify("weave:service:remove-file-log");
});
@ -999,7 +999,7 @@ ErrorHandler.prototype = {
this.service.clusterURL = null;
let delay = 0;
if (Svc.Prefs.get("lastSyncReassigned")) {
if (Svc.PrefBranch.getBoolPref("lastSyncReassigned", false)) {
// We got a 401 in the middle of the previous sync, and we just got
// another. Login must have succeeded in order for us to get here, so
// the password should be correct.
@ -1009,7 +1009,7 @@ ErrorHandler.prototype = {
delay = MINIMUM_BACKOFF_INTERVAL;
} else {
this._log.debug("New mid-sync 401 failure. Making a note.");
Svc.Prefs.set("lastSyncReassigned", true);
Svc.PrefBranch.setBoolPref("lastSyncReassigned", true);
}
this._log.info("Attempting to schedule another sync.");
this.service.scheduler.scheduleNextSync(delay, { why: "reschedule" });

View File

@ -78,13 +78,13 @@ function getEngineModules() {
result.History = { module: "history.js", symbol: "HistoryEngine" };
result.Tab = { module: "tabs.js", symbol: "TabEngine" };
}
if (Svc.Prefs.get("engine.addresses.available", false)) {
if (Svc.PrefBranch.getBoolPref("engine.addresses.available", false)) {
result.Addresses = {
module: "resource://autofill/FormAutofillSync.jsm",
symbol: "AddressesEngine",
};
}
if (Svc.Prefs.get("engine.creditcards.available", false)) {
if (Svc.PrefBranch.getBoolPref("engine.creditcards.available", false)) {
result.CreditCards = {
module: "resource://autofill/FormAutofillSync.jsm",
symbol: "CreditCardsEngine",
@ -423,8 +423,11 @@ Sync11Service.prototype = {
let engines = [];
// We allow a pref, which has no default value, to limit the engines
// which are registered. We expect only tests will use this.
if (Svc.Prefs.has("registerEngines")) {
engines = Svc.Prefs.get("registerEngines").split(",");
if (
Svc.PrefBranch.getPrefType("registerEngines") !=
Ci.nsIPrefBranch.PREF_INVALID
) {
engines = Svc.PrefBranch.getCharPref("registerEngines").split(",");
this._log.info("Registering custom set of engines", engines);
} else {
// default is all engines.
@ -432,7 +435,7 @@ Sync11Service.prototype = {
}
let declined = [];
let pref = Svc.Prefs.get("declinedEngines");
let pref = Svc.PrefBranch.getCharPref("declinedEngines", null);
if (pref) {
declined = pref.split(",");
}
@ -497,7 +500,7 @@ Sync11Service.prototype = {
await this.promiseInitialized;
// Sanity check, this method is not meant to be run if Sync is enabled!
if (Svc.Prefs.get("username", "")) {
if (Svc.PrefBranch.getStringPref("username", "")) {
throw new Error("Sync is enabled!");
}
@ -524,7 +527,10 @@ Sync11Service.prototype = {
this._ignorePrefObserver = true;
try {
for (const engine of allEngines) {
Svc.Prefs.set(`engine.${engine}`, !declinedEngines.includes(engine));
Svc.PrefBranch.setBoolPref(
`engine.${engine}`,
!declinedEngines.includes(engine)
);
}
} finally {
this._ignorePrefObserver = false;
@ -544,7 +550,10 @@ Sync11Service.prototype = {
// We check if we're running TPS here to avoid TPS failing because it
// couldn't get to get the sync lock, due to us currently syncing the
// clients engine.
if (data.includes("clients") && !Svc.Prefs.get("testing.tps", false)) {
if (
data.includes("clients") &&
!Svc.PrefBranch.getBoolPref("testing.tps", false)
) {
// Sync in the background (it's fine not to wait on the returned promise
// because sync() has a lock).
// [] = clients collection only
@ -585,12 +594,12 @@ Sync11Service.prototype = {
_handleEngineStatusChanged(engine) {
this._log.trace("Status for " + engine + " engine changed.");
if (Svc.Prefs.get("engineStatusChanged." + engine, false)) {
if (Svc.PrefBranch.getBoolPref("engineStatusChanged." + engine, false)) {
// The enabled status being changed back to what it was before.
Svc.Prefs.reset("engineStatusChanged." + engine);
Svc.PrefBranch.clearUserPref("engineStatusChanged." + engine);
} else {
// Remember that the engine status changed locally until the next sync.
Svc.Prefs.set("engineStatusChanged." + engine, true);
Svc.PrefBranch.setBoolPref("engineStatusChanged." + engine, true);
}
},
@ -954,7 +963,7 @@ Sync11Service.prototype = {
throw new Error("No FxA user is signed in");
}
this._log.info("Configuring sync with current FxA user");
Svc.Prefs.set("username", user.email);
Svc.PrefBranch.setStringPref("username", user.email);
Svc.Obs.notify("weave:connected");
},
@ -992,11 +1001,13 @@ Sync11Service.prototype = {
// Reset Weave prefs.
this._ignorePrefObserver = true;
Svc.Prefs.resetBranch("");
for (const pref of Svc.PrefBranch.getChildList("")) {
Svc.PrefBranch.clearUserPref(pref);
}
this._ignorePrefObserver = false;
this.clusterURL = null;
Svc.Prefs.set("lastversion", WEAVE_VERSION);
Svc.PrefBranch.setCharPref("lastversion", WEAVE_VERSION);
try {
this.identity.finalize();
@ -1290,7 +1301,7 @@ Sync11Service.prototype = {
Utils.mpLocked()
) {
reason = kSyncMasterPasswordLocked;
} else if (Svc.Prefs.get("firstSync") == "notReady") {
} else if (Svc.PrefBranch.getCharPref("firstSync", null) == "notReady") {
reason = kFirstSyncChoiceNotMade;
} else if (!Async.isAppReady()) {
reason = kFirefoxShuttingDown;

View File

@ -196,15 +196,16 @@ SyncAuthManager.prototype = {
// We can use any pref that must be set if we've synced before, and check
// the sync lock state because we might already be doing that first sync.
let isFirstSync =
!lazy.Weave.Service.locked && !Svc.Prefs.get("client.syncID", null);
!lazy.Weave.Service.locked &&
!Svc.PrefBranch.getStringPref("client.syncID", null);
if (isFirstSync) {
this._log.info("Doing initial sync actions");
Svc.Prefs.set("firstSync", "resetClient");
Svc.PrefBranch.setCharPref("firstSync", "resetClient");
Services.obs.notifyObservers(null, "weave:service:setup-complete");
}
// There's no need to wait for sync to complete and it would deadlock
// our AsyncObserver.
if (!Svc.Prefs.get("testing.tps", false)) {
if (!Svc.PrefBranch.getBoolPref("testing.tps", false)) {
lazy.Weave.Service.sync({ why: "login" });
}
break;
@ -360,7 +361,7 @@ SyncAuthManager.prototype = {
// We used to support services.sync.tokenServerURI but this was a
// pain-point for people using non-default servers as Sync may auto-reset
// all services.sync prefs. So if that still exists, it wins.
let url = Svc.Prefs.get("tokenServerURI"); // Svc.Prefs "root" is services.sync
let url = Svc.PrefBranch.getStringPref("tokenServerURI", null); // Svc.PrefBranch "root" is services.sync
if (!url) {
url = Services.prefs.getCharPref("identity.sync.tokenserver.uri");
}

View File

@ -736,10 +736,15 @@ class SyncTelemetryImpl {
this.events = [];
this.histograms = {};
this.migrations = [];
this.maxEventsCount = lazy.Svc.Prefs.get("telemetry.maxEventsCount", 1000);
this.maxPayloadCount = lazy.Svc.Prefs.get("telemetry.maxPayloadCount");
this.maxEventsCount = lazy.Svc.PrefBranch.getIntPref(
"telemetry.maxEventsCount",
1000
);
this.maxPayloadCount = lazy.Svc.PrefBranch.getIntPref(
"telemetry.maxPayloadCount"
);
this.submissionInterval =
lazy.Svc.Prefs.get("telemetry.submissionInterval") * 1000;
lazy.Svc.PrefBranch.getIntPref("telemetry.submissionInterval") * 1000;
this.lastSubmissionTime = Services.telemetry.msSinceProcessStart();
this.lastUID = EMPTY_UID;
this.lastSyncNodeType = null;