Bug 1790017 - Add Services.perms.getAllByTypes(permissionTypes). r=permissions-reviewers,necko-reviewers,pbz

This new method return an array of `nsIPermission` whose type are included in the
array of permission types parameter.
This will help the implementation of Bug 1778959 where we need to get all the permission
of a given Set of (gated) permission types.

Differential Revision: https://phabricator.services.mozilla.com/D156951
This commit is contained in:
nchevobbe 2022-09-09 14:52:38 +00:00
parent 50310d33ee
commit 2479165e79
4 changed files with 147 additions and 0 deletions

View File

@ -2471,6 +2471,20 @@ NS_IMETHODIMP PermissionManager::GetAllWithTypePrefix(
aResult);
}
NS_IMETHODIMP PermissionManager::GetAllByTypes(
const nsTArray<nsCString>& aTypes,
nsTArray<RefPtr<nsIPermission>>& aResult) {
if (aTypes.IsEmpty()) {
return NS_OK;
}
return GetPermissionEntries(
[&](const PermissionEntry& aPermEntry) {
return aTypes.Contains(mTypeArray[aPermEntry.mType]);
},
aResult);
}
nsresult PermissionManager::GetAllForPrincipalHelper(
nsIPrincipal* aPrincipal, bool aSiteScopePermissions,
nsTArray<RefPtr<nsIPermission>>& aResult) {

View File

@ -0,0 +1,121 @@
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
function check_enumerator(permissionTypes, expectedPermissions) {
const permissions = Services.perms.getAllByTypes(permissionTypes);
Assert.equal(
permissions.length,
expectedPermissions.length,
`getAllByTypes returned the expected number of permissions for ${JSON.stringify(
permissionTypes
)}`
);
permissions.forEach((perm, i) => {
info(`Checking permission #${i}`);
const [
expectedPrincipal,
expectedType,
expectedCapability,
] = expectedPermissions[i];
Assert.ok(perm != null);
Assert.ok(perm.principal.equals(expectedPrincipal));
Assert.equal(perm.type, expectedType);
Assert.equal(perm.capability, expectedCapability);
Assert.equal(perm.expireType, Services.perms.EXPIRE_NEVER);
});
}
function run_test() {
let pm = Services.perms;
let principal = Services.scriptSecurityManager.createContentPrincipalFromOrigin(
"http://example.com"
);
let subPrincipal = Services.scriptSecurityManager.createContentPrincipalFromOrigin(
"http://sub.example.com"
);
const PERM_TYPE_1 = "test/getallbytypes_1";
const PERM_TYPE_2 = "test/getallbytypes_2";
info("check default state");
check_enumerator([], []);
check_enumerator([PERM_TYPE_1], []);
check_enumerator([PERM_TYPE_2], []);
check_enumerator([PERM_TYPE_1, PERM_TYPE_2], []);
info("check that expected permissions are retrieved");
pm.addFromPrincipal(principal, PERM_TYPE_1, pm.ALLOW_ACTION);
pm.addFromPrincipal(
subPrincipal,
"other-test/getallbytypes_1",
pm.PROMPT_ACTION
);
check_enumerator([PERM_TYPE_1], [[principal, PERM_TYPE_1, pm.ALLOW_ACTION]]);
check_enumerator(
[PERM_TYPE_1, PERM_TYPE_2],
[[principal, PERM_TYPE_1, pm.ALLOW_ACTION]]
);
check_enumerator([], []);
check_enumerator([PERM_TYPE_2], []);
pm.addFromPrincipal(subPrincipal, PERM_TYPE_1, pm.PROMPT_ACTION);
check_enumerator(
[PERM_TYPE_1],
[
[subPrincipal, PERM_TYPE_1, pm.PROMPT_ACTION],
[principal, PERM_TYPE_1, pm.ALLOW_ACTION],
]
);
check_enumerator(
[PERM_TYPE_1, PERM_TYPE_2],
[
[subPrincipal, PERM_TYPE_1, pm.PROMPT_ACTION],
[principal, PERM_TYPE_1, pm.ALLOW_ACTION],
]
);
check_enumerator([], []);
check_enumerator([PERM_TYPE_2], []);
pm.addFromPrincipal(principal, PERM_TYPE_2, pm.PROMPT_ACTION);
check_enumerator(
[PERM_TYPE_1, PERM_TYPE_2],
[
[subPrincipal, PERM_TYPE_1, pm.PROMPT_ACTION],
[principal, PERM_TYPE_1, pm.ALLOW_ACTION],
[principal, PERM_TYPE_2, pm.PROMPT_ACTION],
]
);
check_enumerator([], []);
check_enumerator([PERM_TYPE_2], [[principal, PERM_TYPE_2, pm.PROMPT_ACTION]]);
info("check that UNKNOWN_ACTION permissions are ignored");
pm.addFromPrincipal(subPrincipal, PERM_TYPE_2, pm.UNKNOWN_ACTION);
check_enumerator([PERM_TYPE_2], [[principal, PERM_TYPE_2, pm.PROMPT_ACTION]]);
info("check that permission updates are reflected");
pm.addFromPrincipal(subPrincipal, PERM_TYPE_2, pm.PROMPT_ACTION);
check_enumerator(
[PERM_TYPE_2],
[
[subPrincipal, PERM_TYPE_2, pm.PROMPT_ACTION],
[principal, PERM_TYPE_2, pm.PROMPT_ACTION],
]
);
info("check that permission removals are reflected");
pm.removeFromPrincipal(principal, PERM_TYPE_1);
check_enumerator(
[PERM_TYPE_1],
[[subPrincipal, PERM_TYPE_1, pm.PROMPT_ACTION]]
);
pm.removeAll();
check_enumerator([], []);
check_enumerator([PERM_TYPE_1], []);
check_enumerator([PERM_TYPE_2], []);
check_enumerator([PERM_TYPE_1, PERM_TYPE_2], []);
}

View File

@ -5,6 +5,7 @@ head = head.js
[test_permmanager_defaults.js]
[test_permmanager_expiration.js]
skip-if = win10_2004 # Bug 1718292
[test_permmanager_getAllByTypes.js]
[test_permmanager_getAllByTypeSince.js]
[test_permmanager_getAllForPrincipal.js]
[test_permmanager_getAllWithTypePrefix.js]

View File

@ -82,6 +82,17 @@ interface nsIPermissionManager : nsISupports
*/
Array<nsIPermission> getAllWithTypePrefix(in ACString prefix);
/**
* Get all custom permissions whose type exactly match one of the types defined
* in the passed array argument.
* This will return an array of all permissions which are not set to default.
*
* @param types an array of case-sensitive ASCII strings, identifying the
* permissions to be matched.
*/
Array<nsIPermission> getAllByTypes(in Array<ACString> types);
/**
* Get all custom permissions of a specific type and that were modified after
* the specified date. This will return an array of all permissions which are