Commit d50b0587 authored by Nika Layzell's avatar Nika Layzell
Browse files

Bug 1363243 - Avoid checking nsPermissionManager in nsContentBlocker when no...

Bug 1363243 - Avoid checking nsPermissionManager in nsContentBlocker when no preload permissions are set, r=ehsan

MozReview-Commit-ID: B8A8QXie8SX
parent a1f1297c
Loading
Loading
Loading
Loading
+26 −3
Original line number Diff line number Diff line
@@ -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.
@@ -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
@@ -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);
      }
@@ -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.
@@ -3248,3 +3264,10 @@ nsPermissionManager::WhenPermissionsAvailable(nsIPrincipal* aPrincipal,
    });
  return NS_OK;
}

NS_IMETHODIMP
nsPermissionManager::GetHasPreloadPermissions(bool* aResult)
{
  *aResult = sPreloadPermissionCount > 0;
  return NS_OK;
}
+11 −5
Original line number Diff line number Diff line
@@ -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.
@@ -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, 
  // 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) {
+6 −0
Original line number Diff line number Diff line
@@ -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++