Bug 1485114 - Avoid creating nsPermission objects in the permission manager needlessly; r=nika
authorEhsan Akhgari <ehsan@mozilla.com>
Tue, 21 Aug 2018 15:00:26 -0400
changeset 487787 9b2e26f618a443989ab88e724ecc52d160bd093d
parent 487786 f9417ef4b9568f7edf1fc6854b38ef75a13db086
child 487788 4841b3ddbacbb80dfd48c5c0b9737d6c33c7bcaa
push id9719
push userffxbld-merge
push dateFri, 24 Aug 2018 17:49:46 +0000
treeherdermozilla-beta@719ec98fba77 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersnika
bugs1485114
milestone63.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1485114 - Avoid creating nsPermission objects in the permission manager needlessly; r=nika
extensions/cookie/nsPermissionManager.cpp
--- a/extensions/cookie/nsPermissionManager.cpp
+++ b/extensions/cookie/nsPermissionManager.cpp
@@ -6,16 +6,17 @@
 
 #include "mozilla/Attributes.h"
 #include "mozilla/DebugOnly.h"
 
 #include "mozilla/dom/ContentParent.h"
 #include "mozilla/dom/ContentChild.h"
 #include "mozilla/BasePrincipal.h"
 #include "mozilla/ContentPrincipal.h"
+#include "mozilla/Pair.h"
 #include "mozilla/Services.h"
 #include "mozilla/SystemGroup.h"
 #include "mozilla/Unused.h"
 #include "nsPermissionManager.h"
 #include "nsPermission.h"
 #include "nsCRT.h"
 #include "nsNetUtil.h"
 #include "nsCOMArray.h"
@@ -2100,64 +2101,41 @@ nsPermissionManager::RemoveAllSince(int6
   ENSURE_NOT_CHILD_PROCESS;
   return RemoveAllModifiedSince(aSince);
 }
 
 template<class T>
 nsresult
 nsPermissionManager::RemovePermissionEntries(T aCondition)
 {
-  nsCOMArray<nsIPermission> array;
+  AutoTArray<Pair<nsCOMPtr<nsIPrincipal>, nsCString>, 10> array;
   for (auto iter = mPermissionTable.Iter(); !iter.Done(); iter.Next()) {
     PermissionHashKey* entry = iter.Get();
     for (const auto& permEntry : entry->GetPermissions()) {
       if (!aCondition(permEntry)) {
         continue;
       }
 
       nsCOMPtr<nsIPrincipal> principal;
       nsresult rv = GetPrincipalFromOrigin(entry->GetKey()->mOrigin,
                                            getter_AddRefs(principal));
       if (NS_FAILED(rv)) {
         continue;
       }
 
-      nsCOMPtr<nsIPermission> permission =
-        nsPermission::Create(principal,
-                             mTypeArray.ElementAt(permEntry.mType),
-                             permEntry.mPermission,
-                             permEntry.mExpireType,
-                             permEntry.mExpireTime);
-      if (NS_WARN_IF(!permission)) {
-        continue;
-      }
-      array.AppendObject(permission);
+      array.AppendElement(MakePair(principal,
+                                   mTypeArray.ElementAt(permEntry.mType)));
     }
   }
 
-  for (int32_t i = 0; i<array.Count(); ++i) {
-    nsCOMPtr<nsIPrincipal> principal;
-    nsAutoCString type;
-
-    nsresult rv = array[i]->GetPrincipal(getter_AddRefs(principal));
-    if (NS_FAILED(rv)) {
-      NS_ERROR("GetPrincipal() failed!");
-      continue;
-    }
-
-    rv = array[i]->GetType(type);
-    if (NS_FAILED(rv)) {
-      NS_ERROR("GetType() failed!");
-      continue;
-    }
-
+  for (size_t i = 0; i < array.Length(); ++i) {
     // AddInternal handles removal, so let it do the work...
     AddInternal(
-      principal,
-      type,
+      array[i].first(),
+      array[i].second(),
       nsIPermissionManager::UNKNOWN_ACTION,
       0,
       nsIPermissionManager::EXPIRE_NEVER, 0, 0,
       nsPermissionManager::eNotify,
       nsPermissionManager::eWriteToDB);
   }
   // now re-import any defaults as they may now be required if we just deleted
   // an override.
@@ -2723,54 +2701,40 @@ nsPermissionManager::RemovePermissionsWi
   }
 
   return RemovePermissionsWithAttributes(pattern);
 }
 
 nsresult
 nsPermissionManager::RemovePermissionsWithAttributes(mozilla::OriginAttributesPattern& aPattern)
 {
-  nsCOMArray<nsIPermission> permissions;
+  AutoTArray<Pair<nsCOMPtr<nsIPrincipal>, nsCString>, 10> permissions;
   for (auto iter = mPermissionTable.Iter(); !iter.Done(); iter.Next()) {
     PermissionHashKey* entry = iter.Get();
 
     nsCOMPtr<nsIPrincipal> principal;
     nsresult rv = GetPrincipalFromOrigin(entry->GetKey()->mOrigin,
                                          getter_AddRefs(principal));
     if (NS_FAILED(rv)) {
       continue;
     }
 
     if (!aPattern.Matches(principal->OriginAttributesRef())) {
       continue;
     }
 
     for (const auto& permEntry : entry->GetPermissions()) {
-      nsCOMPtr<nsIPermission> permission =
-        nsPermission::Create(principal,
-                             mTypeArray.ElementAt(permEntry.mType),
-                             permEntry.mPermission,
-                             permEntry.mExpireType,
-                             permEntry.mExpireTime);
-      if (NS_WARN_IF(!permission)) {
-        continue;
-      }
-      permissions.AppendObject(permission);
+      permissions.AppendElement(MakePair(principal,
+                                         mTypeArray.ElementAt(permEntry.mType)));
     }
   }
 
-  for (int32_t i = 0; i < permissions.Count(); ++i) {
-    nsCOMPtr<nsIPrincipal> principal;
-    nsAutoCString type;
-
-    permissions[i]->GetPrincipal(getter_AddRefs(principal));
-    permissions[i]->GetType(type);
-
-    AddInternal(principal,
-                type,
+  for (size_t i = 0; i < permissions.Length(); ++i) {
+    AddInternal(permissions[i].first(),
+                permissions[i].second(),
                 nsIPermissionManager::UNKNOWN_ACTION,
                 0,
                 nsIPermissionManager::EXPIRE_NEVER,
                 0,
                 0,
                 nsPermissionManager::eNotify,
                 nsPermissionManager::eWriteToDB);
   }