Open Bug 2098732 Opened 1 day ago Updated 1 day ago

Built-in widget prefs are left behind when an update changes the widget's pref branch

Categories

(Firefox :: Widgets, defect)

defect

Tracking

()

People

(Reporter: mconley, Unassigned)

References

(Depends on 1 open bug, Blocks 1 open bug)

Details

Follow-up from the review of bug 2096724 (D345895).

WidgetManager.register() records each widget's pref branch in extensions.widgets.<key>.prefBranch so cleanupPrefs() can clear the widget's prefs after it unregisters, even in a later session. When a built-in widget add-on is updated to a version with a different pref_branch (or none, falling back to the default extensions.widgets.<key> branch), register() overwrites the recorded branch. The .enabled and .size prefs on the old branch are then orphaned: cleanupPrefs() never clears them, and the updated widget loses its enabled state and chosen size.

This only affects built-in widgets, since generated widgets cannot set a custom pref_branch. Built-in widgets still need more work before they are usable, so this should block the MVP for built-in widgets rather than bug 2096724.

So something Luca suggested we could do is, in register(), before writing the recorded branch, migrate the prefs when the branch is detected to be changed:

if (this.hasWidgetPreferences(id) && prefBranchFor(id) !== prefBranch) {
  migrateWidgetPrefs(prefBranchFor(id), prefBranch);
}
Services.prefs.setStringPref(prefBranchPrefFor(id), prefBranch);

where migrateWidgetPrefs(oldBranch, newBranch) does the following for each of enabled and size:

  • if the pref is set on the old branch, and not yet set on the new branch, copy its value to the new branch;
  • then clear the pref on the old branch.

Suggested test for test_WidgetManager.js:

add_task(async function test_cleanup_after_pref_branch_changes_on_updates() {
  const manager = createManager();
  const id = "custom@mozilla.org";
  const sizes = ["small", "medium"];
  const defaultBranch = "extensions.widgets.custom";

  const widget = manager.register(
    makeOptions({ id, prefBranch: "test.old", sizes })
  );
  widget.enabled = true;
  widget.size = "small";
  manager.unregister(id);

  // The add-on is updated to a version using another pref branch.
  const updated = manager.register(
    makeOptions({ id, prefBranch: "test.new", sizes })
  );
  Assert.ok(updated.enabled, "Enabled state migrated to the new branch");
  Assert.equal(updated.size, "small", "Size migrated to the new branch");
  manager.unregister(id);

  // The add-on is updated again to a version without a custom pref branch.
  const updatedAgain = manager.register(makeOptions({ id, sizes }));
  Assert.ok(updatedAgain.enabled, "Enabled state migrated to default branch");
  Assert.equal(updatedAgain.size, "small", "Size migrated to default branch");
  manager.unregister(id);

  manager.cleanupPrefs(id);
  for (const branch of ["test.old", "test.new", defaultBranch]) {
    Assert.deepEqual(
      Services.prefs.getChildList(`${branch}.`),
      [],
      `Prefs of ${branch} cleared`
    );
  }
});
You need to log in before you can comment on or make changes to this bug.