Skip to content
This repository has been archived by the owner on Aug 4, 2022. It is now read-only.

Commit

Permalink
Bug 1363243 - Avoid checking nsPermissionManager in nsContentBlocker …
Browse files Browse the repository at this point in the history
…when no preload permissions are set, r=ehsan

MozReview-Commit-ID: B8A8QXie8SX
  • Loading branch information
mystor committed May 11, 2017
1 parent 9f5c691 commit 2ab9f38
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
29 changes: 26 additions & 3 deletions extensions/cookie/nsPermissionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,15 @@ LogToConsole(const nsAString& aMsg)

namespace {

// The number of permissions from the kPreloadPermissions list which are present
// in the permission manager. Used to determine if the permission manager should
// be checked for one of these preload permissions in nsContentBlocker.
static int32_t sPreloadPermissionCount = 0;

// These permissions are special permissions which must be transmitted to the
// content process before documents with their principals have loaded within
// that process. For example, the permissions which are used for content
// blocking are sent using this mechanism.
// that process. This is because these permissions are used for content
// blocking in nsContentBlocker.
//
// Permissions which are in this list are considered to have a "" permission
// key, even if their principal would not normally have that key.
Expand Down Expand Up @@ -118,7 +123,6 @@ static const char* kPreloadPermissions[] = {
"fetch",
"image",
"manifest"
// ------------------------------------------
};

// NOTE: nullptr can be passed as aType - if it is this function will return
Expand Down Expand Up @@ -1781,6 +1785,12 @@ nsPermissionManager::AddInternal(nsIPrincipal* aPrincipal,
aExpireType, aExpireTime,
aModificationTime));

// Record a count of the number of preload permissions present in the
// content process.
if (IsPreloadPermission(mTypeArray[typeIndex].get())) {
sPreloadPermissionCount++;
}

if (aDBOperation == eWriteToDB && aExpireType != nsIPermissionManager::EXPIRE_SESSION) {
UpdateDB(op, mStmtInsert, id, origin, aType, aPermission, aExpireType, aExpireTime, aModificationTime);
}
Expand All @@ -1803,6 +1813,12 @@ nsPermissionManager::AddInternal(nsIPrincipal* aPrincipal,
id = oldPermissionEntry.mID;
entry->GetPermissions().RemoveElementAt(index);

// Record a count of the number of preload permissions present in the
// content process.
if (IsPreloadPermission(mTypeArray[typeIndex].get())) {
sPreloadPermissionCount--;
}

if (aDBOperation == eWriteToDB)
// We care only about the id here so we pass dummy values for all other
// parameters.
Expand Down Expand Up @@ -3248,3 +3264,10 @@ nsPermissionManager::WhenPermissionsAvailable(nsIPrincipal* aPrincipal,
});
return NS_OK;
}

NS_IMETHODIMP
nsPermissionManager::GetHasPreloadPermissions(bool* aResult)
{
*aResult = sPreloadPermissionCount > 0;
return NS_OK;
}
16 changes: 11 additions & 5 deletions extensions/permissions/nsContentBlocker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ nsContentBlocker::TestPermission(nsIURI *aCurrentURI,
bool *aFromPrefs)
{
*aFromPrefs = false;
nsresult rv;

if (!*kTypeString[aContentType - 1]) {
// Disallow internal content policy types, they should not be used here.
Expand All @@ -282,11 +283,16 @@ nsContentBlocker::TestPermission(nsIURI *aCurrentURI,
// default prefs.
// Don't forget the aContentType ranges from 1..8, while the
// array is indexed 0..7
uint32_t permission;
nsresult rv = mPermissionManager->TestPermission(aCurrentURI,
kTypeString[aContentType - 1],
&permission);
NS_ENSURE_SUCCESS(rv, rv);
// All permissions tested by this method are preload permissions, so don't
// bother actually checking with the permission manager unless we have a
// preload permission.
uint32_t permission = nsIPermissionManager::UNKNOWN_ACTION;
if (mPermissionManager->GetHasPreloadPermissions()) {
rv = mPermissionManager->TestPermission(aCurrentURI,
kTypeString[aContentType - 1],
&permission);
NS_ENSURE_SUCCESS(rv, rv);
}

// If there is nothing on the list, use the default.
if (!permission) {
Expand Down
6 changes: 6 additions & 0 deletions netwerk/base/nsIPermissionManager.idl
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,12 @@ interface nsIPermissionManager : nsISupports
*/
void whenPermissionsAvailable(in nsIPrincipal aPrincipal,
in nsIRunnable aRunnable);

/**
* True if any "preload" permissions are present. This is used to avoid making
* potentially expensive permissions checks in nsContentBlocker.
*/
[infallible] readonly attribute boolean hasPreloadPermissions;
};

%{ C++
Expand Down

0 comments on commit 2ab9f38

Please sign in to comment.