Bug 1804828 - Handle permissions clearing in SitePermsAddonProvider. r=rpl.

`perm-changed` event can be called with a `"cleared"` `data` when
all permissions are cleared (e.g. using `Services.perms.removeAll()`).
In such case, we want to uninstall any `SitePermsAddon` that might exist.

Differential Revision: https://phabricator.services.mozilla.com/D164314
This commit is contained in:
Nicolas Chevobbe 2022-12-09 12:33:20 +00:00
parent d2f9ea2645
commit fce94306df
2 changed files with 43 additions and 1 deletions

View File

@ -507,7 +507,7 @@ const SitePermsAddonProvider = {
return;
}
// Pipe the change to the existing addon is there is one.
// Pipe the change to the existing addon if there is one.
if (this.wrappersMapByOrigin.has(siteOriginNoSuffix)) {
this.wrappersMapByOrigin
.get(siteOriginNoSuffix)
@ -637,6 +637,15 @@ const SitePermsAddonProvider = {
]);
Services.obs.notifyObservers(null, "sitepermsaddon-provider-registered");
} else if (topic === "perm-changed") {
if (data === "cleared") {
// In such case, `subject` is null, but we can simply uninstall all existing addons.
for (const addon of this.wrappersMapByOrigin.values()) {
addon.uninstall();
}
this.wrappersMapByOrigin.clear();
return;
}
const perm = subject.QueryInterface(Ci.nsIPermission);
this.handlePermissionChange(perm, data);
}

View File

@ -298,6 +298,39 @@ add_task(
"Permission was removed when the addon was uninstalled"
);
info("Add gated permissions");
PermissionTestUtils.add(
PRINCIPAL_COM,
GATED_SITE_PERM1,
Services.perms.ALLOW_ACTION
);
PermissionTestUtils.add(
PRINCIPAL_ORG,
GATED_SITE_PERM1,
Services.perms.ALLOW_ACTION
);
addons = await promiseAddonsByTypes([SITEPERMS_ADDON_TYPE]);
Assert.equal(addons.length, 2, "2 addons are now available");
info("Clear permissions");
const onAddon1Uninstall = AddonTestUtils.promiseAddonEvent(
"onUninstalled",
addon => addon.id === addons[0].id
);
const onAddon2Uninstall = AddonTestUtils.promiseAddonEvent(
"onUninstalled",
addon => addon.id === addons[1].id
);
Services.perms.removeAll();
await Promise.all([onAddon1Uninstall, onAddon2Uninstall]);
ok("Addons were properly uninstalled…");
Assert.equal(
(await promiseAddonsByTypes([SITEPERMS_ADDON_TYPE])).length,
0,
"… and getAddonsByTypes does not return them anymore"
);
info("Adding a permission to a public etld");
PermissionTestUtils.add(
PRINCIPAL_GITHUB,