forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug 1801337 - Fix uninstall on sitepermission AddonCard created on th…
…e AOM onInstalled event. r=rpl. There are 2 folds to this patch. First, the `SitePermsAddonInstalling` was calling its parent constructor (`SitePermsAddonWrapper`), without the expected permissions parameter. This would then lead to the `uninstall` method to not do anything as there were no permissions. This is fixed by passing the permission for which we want to install the addon. Second, if multiple addons were installed for the same origin (e.g. "midi" and "midi-sysex") while about:addons was open, removing the addon would only revoke the "first" permission, as `SitePermsAddonInstalling` only holds one permission. To fix this, we define an `SitePermsAddonInstalling#uninstall` method that checks if we registered a `SitePermsAddonWrapper` instance for this origin, and in such case, uninstall the addon from this instance instead of the `SitePermsAddonInstalling`, one, as the `SitePermsAddonWrapper` instance has the whole set of permissions granted for a given origin. Test cases are added to ensure we don't regress this. Differential Revision: https://phabricator.services.mozilla.com/D162431
- Loading branch information
Showing
4 changed files
with
227 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
183 changes: 183 additions & 0 deletions
183
toolkit/mozapps/extensions/test/browser/browser_html_sitepermission_addons.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
/* Any copyright is dedicated to the Public Domain. | ||
* http://creativecommons.org/publicdomain/zero/1.0/ */ | ||
|
||
const { AddonTestUtils } = ChromeUtils.import( | ||
"resource://testing-common/AddonTestUtils.jsm" | ||
); | ||
const { | ||
SITEPERMS_ADDON_PROVIDER_PREF, | ||
SITEPERMS_ADDON_TYPE, | ||
} = ChromeUtils.importESModule( | ||
"resource://gre/modules/addons/siteperms-addon-utils.sys.mjs" | ||
); | ||
|
||
const html = `<!DOCTYPE html><h1>Test midi permission with synthetic site permission addon</h1>`; | ||
const EXAMPLE_COM_URL = `https://example.com/document-builder.sjs?html=${html}`; | ||
const EXAMPLE_ORG_URL = `https://example.org/document-builder.sjs?html=${html}`; | ||
|
||
async function uninstallAllSitePermissionAddons() { | ||
const sitepermAddons = await AddonManager.getAddonsByTypes([ | ||
SITEPERMS_ADDON_TYPE, | ||
]); | ||
for (const addon of sitepermAddons) { | ||
await addon.uninstall(); | ||
} | ||
} | ||
|
||
add_setup(async () => { | ||
await SpecialPowers.pushPrefEnv({ | ||
set: [["midi.prompt.testing", false]], | ||
}); | ||
await SpecialPowers.pushPrefEnv({ | ||
set: [["midi.testing", true]], | ||
}); | ||
|
||
registerCleanupFunction(uninstallAllSitePermissionAddons); | ||
}); | ||
|
||
add_task(async function testAboutAddonUninstall() { | ||
if (!AddonManager.hasProvider("SitePermsAddonProvider")) { | ||
ok( | ||
!Services.prefs.getBoolPref(SITEPERMS_ADDON_PROVIDER_PREF), | ||
"Expect SitePermsAddonProvider to be disabled by prefs" | ||
); | ||
ok(true, "Skip test on SitePermsAddonProvider disabled"); | ||
return; | ||
} | ||
|
||
// Grant midi permission on example.com so about:addons does have a Site Permissions section | ||
await SpecialPowers.addPermission("midi-sysex", true, EXAMPLE_COM_URL); | ||
|
||
info("Open an about:addon tab so AMO event listeners are registered"); | ||
const aboutAddonWin = await loadInitialView("sitepermission"); | ||
// loadInitialView sets the about:addon as the active one, so we can grab it here. | ||
const aboutAddonTab = gBrowser.selectedTab; | ||
|
||
const addonList = aboutAddonWin.document.querySelector("addon-list"); | ||
let addonCards = aboutAddonWin.document.querySelectorAll("addon-card"); | ||
is( | ||
addonCards.length, | ||
1, | ||
"There's a card displayed for the example.com addon" | ||
); | ||
|
||
info("Open an example.org tab and install the midi site permission addon"); | ||
const exampleOrgTab = await BrowserTestUtils.openNewForegroundTab( | ||
gBrowser, | ||
EXAMPLE_ORG_URL, | ||
true /* waitForLoad */ | ||
); | ||
|
||
let promiseAddonCardAdded = BrowserTestUtils.waitForEvent(addonList, "add"); | ||
|
||
info("Install midi"); | ||
await testInstallGatedPermission( | ||
exampleOrgTab, | ||
() => { | ||
content.navigator.requestMIDIAccess(); | ||
}, | ||
"midi" | ||
); | ||
|
||
info("Install midi-sysex as well"); | ||
const newAddon = await testInstallGatedPermission( | ||
exampleOrgTab, | ||
() => { | ||
content.navigator.requestMIDIAccess({ sysex: true }); | ||
}, | ||
"midi-sysex" | ||
); | ||
|
||
const newAddonId = newAddon.id; | ||
ok( | ||
newAddonId, | ||
"Got the addon id for the newly installed sitepermission add-on" | ||
); | ||
|
||
info("Switch back to about:addon"); | ||
gBrowser.selectedTab = aboutAddonTab; | ||
|
||
await promiseAddonCardAdded; | ||
|
||
is( | ||
aboutAddonWin.document.querySelectorAll("addon-card").length, | ||
2, | ||
"A new addon card has been added as expected" | ||
); | ||
|
||
const exampleOrgAddonCard = getAddonCard(aboutAddonWin, newAddonId); | ||
|
||
info("Remove the example.org addon"); | ||
const promptService = mockPromptService(); | ||
promptService._response = 0; | ||
|
||
let promiseRemoved = BrowserTestUtils.waitForEvent(addonList, "remove"); | ||
exampleOrgAddonCard.querySelector("[action=remove]").click(); | ||
await promiseRemoved; | ||
|
||
is( | ||
aboutAddonWin.document.querySelectorAll("addon-card").length, | ||
1, | ||
"addon card has been removed as expected" | ||
); | ||
|
||
ok( | ||
await SpecialPowers.testPermission( | ||
"midi", | ||
SpecialPowers.Services.perms.UNKNOWN_ACTION, | ||
{ url: EXAMPLE_ORG_URL } | ||
), | ||
"midi permission was revoked" | ||
); | ||
ok( | ||
await SpecialPowers.testPermission( | ||
"midi-sysex", | ||
SpecialPowers.Services.perms.UNKNOWN_ACTION, | ||
{ url: EXAMPLE_ORG_URL } | ||
), | ||
"midi-sysex permission was revoked as well" | ||
); | ||
|
||
await BrowserTestUtils.removeTab(exampleOrgTab); | ||
await close_manager(aboutAddonWin); | ||
await uninstallAllSitePermissionAddons(); | ||
}); | ||
|
||
/** | ||
* | ||
* Execute a function in the tab content page and check that the expected gated permission | ||
* is set | ||
* | ||
* @param {Tab} tab: The tab in which we want to install the gated permission | ||
* @param {Function} spawnCallback: function used in `SpecialPowers.spawn` that will trigger | ||
* the install | ||
* @param {String} expectedPermType: The name of the permission that should be granted | ||
* @returns {Promise<Addon>} The installed addon instance | ||
*/ | ||
async function testInstallGatedPermission( | ||
tab, | ||
spawnCallback, | ||
expectedPermType | ||
) { | ||
let onInstallEnded = AddonTestUtils.promiseInstallEvent("onInstallEnded"); | ||
let onAddonInstallBlockedNotification = promisePopupNotificationShown( | ||
"addon-install-blocked" | ||
); | ||
await SpecialPowers.spawn(tab.linkedBrowser, [], spawnCallback); | ||
|
||
let addonInstallPanel = await onAddonInstallBlockedNotification; | ||
let dialogPromise = promisePopupNotificationShown("addon-webext-permissions"); | ||
addonInstallPanel.button.click(); | ||
let installPermsDialog = await dialogPromise; | ||
installPermsDialog.button.click(); | ||
|
||
ok( | ||
await SpecialPowers.testPermission( | ||
expectedPermType, | ||
SpecialPowers.Services.perms.ALLOW_ACTION, | ||
{ url: EXAMPLE_ORG_URL } | ||
), | ||
`"${expectedPermType}" permission was granted` | ||
); | ||
return onInstallEnded.then(install => install[0].addon); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters