Bug 1919558 - Part 5: Add AddDefaultFromPrincipal method to permission manager r=pbz,permissions-reviewers

This method will later be used to import default permissions from remote
settings through the remote permission manager.

Differential Revision: https://phabricator.services.mozilla.com/D222650
This commit is contained in:
Malte Jürgens 2024-11-05 10:17:42 +00:00
parent 4231a0c487
commit 7fa4b7041e
2 changed files with 76 additions and 0 deletions

View File

@ -1706,6 +1706,68 @@ PermissionManager::AddFromPrincipalAndPersistInPrivateBrowsing(
/* aAllowPersistInPrivateBrowsing */ true);
}
NS_IMETHODIMP
PermissionManager::AddDefaultFromPrincipal(nsIPrincipal* aPrincipal,
const nsACString& aType,
uint32_t aPermission) {
ENSURE_NOT_CHILD_PROCESS;
MOZ_ASSERT(mState == eReady);
bool isValidPermissionPrincipal = false;
nsresult rv = ShouldHandlePrincipalForPermission(aPrincipal,
isValidPermissionPrincipal);
NS_ENSURE_SUCCESS(rv, rv);
if (!isValidPermissionPrincipal) {
// return early if the principal is invalid for permissions
return rv;
}
nsCString origin;
rv = GetOriginFromPrincipal(aPrincipal, IsOAForceStripPermission(aType),
origin);
NS_ENSURE_SUCCESS(rv, rv);
DefaultEntry entry;
{
// Lock for mDefaultEntriesForImport
MonitorAutoLock lock(mMonitor);
// Try to update existing entry in mDefaultEntriesForImport, which will
// later be used to restore the default permissions when permissions are
// cleared
bool updatedExistingEntry = false;
nsTArray<DefaultEntry>::iterator defaultEntry =
mDefaultEntriesForImport.begin();
while (defaultEntry != mDefaultEntriesForImport.end()) {
if (defaultEntry->mType == aType && defaultEntry->mOrigin == origin) {
defaultEntry->mPermission = aPermission;
entry = *defaultEntry;
if (aPermission == nsIPermissionManager::UNKNOWN_ACTION) {
mDefaultEntriesForImport.RemoveElementAt(defaultEntry);
}
updatedExistingEntry = true;
break;
}
++defaultEntry;
}
// Or add a new entry if there wasn't already one and we aren't deleting the
// default permission
if (!updatedExistingEntry) {
entry.mOrigin = origin;
entry.mPermission = aPermission;
entry.mType = aType;
if (aPermission != nsIPermissionManager::UNKNOWN_ACTION) {
mDefaultEntriesForImport.AppendElement(entry);
}
}
}
// So far, we have only updated mDefaultEntriesForImport for later recovery.
// Now, we actually need to import this change into the permission manager.
return ImportDefaultEntry(entry);
}
NS_IMETHODIMP
PermissionManager::AddFromPrincipal(nsIPrincipal* aPrincipal,
const nsACString& aType,

View File

@ -155,6 +155,20 @@ interface nsIPermissionManager : nsISupports
in ACString type,
in uint32_t permission);
/**
* Add temporary default permission information for a given principal.
* This permission will be cleared at the end of the session, will not be
* stored on disk, and will not be set if a conflicting (non-default)
* permission already exists.
*
* This function shouldn't be used by regular permission manager consumers and
* is only expected to be called by the RemotePermissionService.sys.mjs for
* the purpose of importing default permissions from remote settings.
*/
void addDefaultFromPrincipal(in nsIPrincipal principal,
in ACString type,
in uint32_t permission);
/**
* Remove permission information for a given principal.
* This is internally calling remove() with the host from the principal's URI.