Bug 1703320 - Ensure Background Task is scheduled if and only if it ought to be scheduled r=nalexander,application-update-reviewers

Depends on D111014

Differential Revision: https://phabricator.services.mozilla.com/D111015
This commit is contained in:
Kirk Steuber 2021-04-07 17:19:22 +00:00
parent 812308277d
commit 1751681fd0

View File

@ -330,10 +330,15 @@ var BackgroundUpdate = {
`${SLUG}: checking eligibility before scheduling background update task` `${SLUG}: checking eligibility before scheduling background update task`
); );
const previousEnabled = Services.prefs.getBoolPref( let previousEnabled;
"app.update.background.previous.enabled", let successfullyReadPrevious;
false try {
); previousEnabled = await TaskScheduler.taskExists(this.taskId);
successfullyReadPrevious = true;
} catch (ex) {
successfullyReadPrevious = false;
}
const previousReasons = Services.prefs.getCharPref( const previousReasons = Services.prefs.getCharPref(
"app.update.background.previous.reasons", "app.update.background.previous.reasons",
null null
@ -404,12 +409,6 @@ var BackgroundUpdate = {
} }
let updatePreviousPrefs = () => { let updatePreviousPrefs = () => {
// Squirrel away our previous values: keeping them allows us to witness both the rising edge
// (disabled -> enabled) and the falling (enabled -> disabled) edge.
Services.prefs.setBoolPref(
"app.update.background.previous.enabled",
!reasons.length
);
if (reasons.length) { if (reasons.length) {
Services.prefs.setCharPref( Services.prefs.setCharPref(
"app.update.background.previous.reasons", "app.update.background.previous.reasons",
@ -429,7 +428,7 @@ var BackgroundUpdate = {
)}'` )}'`
); );
if (previousEnabled) { if (!successfullyReadPrevious || previousEnabled) {
await TaskScheduler.deleteTask(this.taskId); await TaskScheduler.deleteTask(this.taskId);
log.debug( log.debug(
`${SLUG}: witnessed falling (enabled -> disabled) edge; deleted task ${this.taskId}.` `${SLUG}: witnessed falling (enabled -> disabled) edge; deleted task ${this.taskId}.`
@ -441,7 +440,7 @@ var BackgroundUpdate = {
return false; return false;
} }
if (previousEnabled) { if (successfullyReadPrevious && previousEnabled) {
log.info( log.info(
`${SLUG}: background update was previously enabled; not registering task.` `${SLUG}: background update was previously enabled; not registering task.`
); );