Bug 972606, part 1 - Convert nsJAR::mManifestData to nsClassHashtable. r=aklotz
authorAndrew McCreight <continuation@gmail.com>
Tue, 25 Feb 2014 20:32:48 -0800
changeset 170971 165e6579508a2bf2661418e4a6dbb988de4aa896
parent 170970 fd6f16c2f588a1934ef79c1ae126fd7518b867df
child 170972 4f7be2b146f51589cc6a9956cb37d6568f5149f1
push id270
push userpvanderbeken@mozilla.com
push dateThu, 06 Mar 2014 09:24:21 +0000
reviewersaklotz
bugs972606
milestone30.0a1
Bug 972606, part 1 - Convert nsJAR::mManifestData to nsClassHashtable. r=aklotz
modules/libjar/nsJAR.cpp
modules/libjar/nsJAR.h
--- a/modules/libjar/nsJAR.cpp
+++ b/modules/libjar/nsJAR.cpp
@@ -67,27 +67,20 @@ nsJARManifestItem::nsJARManifestItem(): 
 
 nsJARManifestItem::~nsJARManifestItem()
 {
 }
 
 //----------------------------------------------
 // nsJAR constructor/destructor
 //----------------------------------------------
-static bool
-DeleteManifestEntry(nsHashKey* aKey, void* aData, void* closure)
-{
-//-- deletes an entry in  mManifestData.
-  delete (nsJARManifestItem*)aData;
-  return true;
-}
 
 // The following initialization makes a guess of 10 entries per jarfile.
 nsJAR::nsJAR(): mZip(new nsZipArchive()),
-                mManifestData(nullptr, nullptr, DeleteManifestEntry, nullptr, 10),
+                mManifestData(10),
                 mParsedManifest(false),
                 mGlobalStatus(JAR_MANIFEST_NOT_PARSED),
                 mReleaseTime(PR_INTERVAL_NO_TIMEOUT),
                 mCache(nullptr),
                 mLock("nsJAR::mLock"),
                 mTotalItemsInManifest(0),
                 mOpened(false)
 {
@@ -184,17 +177,17 @@ nsJAR::GetFile(nsIFile* *result)
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsJAR::Close()
 {
   mOpened = false;
   mParsedManifest = false;
-  mManifestData.Reset();
+  mManifestData.Clear();
   mGlobalStatus = JAR_MANIFEST_NOT_PARSED;
   mTotalItemsInManifest = 0;
 
   nsRefPtr<nsZipArchive> greOmni = mozilla::Omnijar::GetReader(mozilla::Omnijar::GRE);
   nsRefPtr<nsZipArchive> appOmni = mozilla::Omnijar::GetReader(mozilla::Omnijar::APP);
 
   if (mZip == greOmni || mZip == appOmni) {
     mZip = new nsZipArchive();
@@ -354,18 +347,17 @@ nsJAR::GetCertificatePrincipal(const nsA
   if (NS_FAILED(rv)) return rv;
   if (mGlobalStatus == JAR_NO_MANIFEST)
     return NS_OK;
 
   int16_t requestedStatus;
   if (!aFilename.IsEmpty())
   {
     //-- Find the item
-    nsCStringKey key(aFilename);
-    nsJARManifestItem* manItem = static_cast<nsJARManifestItem*>(mManifestData.Get(&key));
+    nsJARManifestItem* manItem = mManifestData.Get(aFilename);
     if (!manItem)
       return NS_OK;
     //-- Verify the item against the manifest
     if (!manItem->entryVerified)
     {
       nsXPIDLCString entryData;
       uint32_t entryDataLen;
       rv = LoadEntry(aFilename, getter_Copies(entryData), &entryDataLen);
@@ -655,49 +647,46 @@ nsJAR::ParseOneFile(const char* filebuf,
             if (curItemMF->mType == JAR_INTERNAL)
             {
               bool exists;
               nsresult rv = HasEntry(curItemName, &exists);
               if (NS_FAILED(rv) || !exists)
                 curItemMF->mType = JAR_INVALID;
             }
             //-- Check for duplicates
-            nsCStringKey key(curItemName);
-            if (mManifestData.Exists(&key))
+            if (mManifestData.Contains(curItemName)) {
               curItemMF->mType = JAR_INVALID;
+            }
           }
         }
 
         if (curItemMF->mType == JAR_INVALID)
           delete curItemMF;
         else //-- calculate section digest
         {
           uint32_t sectionLength = curPos - sectionStart;
           CalculateDigest(sectionStart, sectionLength,
                           curItemMF->calculatedSectionDigest);
           //-- Save item in the hashtable
-          nsCStringKey itemKey(curItemName);
-          mManifestData.Put(&itemKey, (void*)curItemMF);
+          mManifestData.Put(curItemName, curItemMF);
         }
         if (nextLineStart == nullptr) // end-of-file
           break;
 
         sectionStart = nextLineStart;
         if (!(curItemMF = new nsJARManifestItem()))
           return NS_ERROR_OUT_OF_MEMORY;
       } // (aFileType == JAR_MF)
       else
         //-- file type is SF, compare digest with calculated
         //   section digests from MF file.
       {
         if (foundName)
         {
-          nsJARManifestItem* curItemSF;
-          nsCStringKey key(curItemName);
-          curItemSF = (nsJARManifestItem*)mManifestData.Get(&key);
+          nsJARManifestItem* curItemSF = mManifestData.Get(curItemName);
           if(curItemSF)
           {
             NS_ASSERTION(curItemSF->status == JAR_NOT_SIGNED,
                          "SECURITY ERROR: nsJARManifestItem not correctly initialized");
             curItemSF->status = mGlobalStatus;
             if (curItemSF->status == JAR_VALID_MANIFEST)
             { // Compare digests
               if (storedSectionDigest.IsEmpty())
--- a/modules/libjar/nsJAR.h
+++ b/modules/libjar/nsJAR.h
@@ -11,21 +11,21 @@
 #include "prio.h"
 #include "plstr.h"
 #include "prlog.h"
 #include "prinrval.h"
 
 #include "mozilla/Mutex.h"
 #include "nsIComponentManager.h"
 #include "nsCOMPtr.h"
+#include "nsClassHashtable.h"
 #include "nsString.h"
 #include "nsIFile.h"
 #include "nsStringEnumerator.h"
 #include "nsHashKeys.h"
-#include "nsHashtable.h"
 #include "nsRefPtrHashtable.h"
 #include "nsTHashtable.h"
 #include "nsIZipReader.h"
 #include "nsZipArchive.h"
 #include "nsICertificatePrincipal.h"
 #include "nsISignatureVerifier.h"
 #include "nsIObserverService.h"
 #include "nsWeakReference.h"
@@ -90,21 +90,23 @@ class nsJAR : public nsIZipReader
       mReleaseTime = PR_INTERVAL_NO_TIMEOUT;
     }
 
     void SetZipReaderCache(nsZipReaderCache* cache) {
       mCache = cache;
     }
 
   protected:
+    typedef nsClassHashtable<nsCStringHashKey, nsJARManifestItem> ManifestDataHashtable;
+
     //-- Private data members
     nsCOMPtr<nsIFile>        mZipFile;        // The zip/jar file on disk
     nsCString                mOuterZipEntry;  // The entry in the zip this zip is reading from
     nsRefPtr<nsZipArchive>   mZip;            // The underlying zip archive
-    nsObjectHashtable        mManifestData;   // Stores metadata for each entry
+    ManifestDataHashtable    mManifestData;   // Stores metadata for each entry
     bool                     mParsedManifest; // True if manifest has been parsed
     nsCOMPtr<nsICertificatePrincipal> mPrincipal; // The entity which signed this file
     int16_t                  mGlobalStatus;   // Global signature verification status
     PRIntervalTime           mReleaseTime;    // used by nsZipReaderCache for flushing entries
     nsZipReaderCache*        mCache;          // if cached, this points to the cache it's contained in
     mozilla::Mutex           mLock;
     int64_t                  mMtime;
     int32_t                  mTotalItemsInManifest;