tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

nsJAR.h (5235B)


      1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* This Source Code Form is subject to the terms of the Mozilla Public
      3 * License, v. 2.0. If a copy of the MPL was not distributed with this
      4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      5 
      6 #ifndef nsJAR_h_
      7 #define nsJAR_h_
      8 
      9 #include "nscore.h"
     10 #include "prio.h"
     11 #include "mozilla/Logging.h"
     12 #include "prinrval.h"
     13 
     14 #include "mozilla/RecursiveMutex.h"
     15 #include "nsCOMPtr.h"
     16 #include "nsClassHashtable.h"
     17 #include "nsString.h"
     18 #include "nsIFile.h"
     19 #include "nsStringEnumerator.h"
     20 #include "nsHashKeys.h"
     21 #include "nsRefPtrHashtable.h"
     22 #include "nsTHashtable.h"
     23 #include "nsIZipReader.h"
     24 #include "nsZipArchive.h"
     25 #include "nsWeakReference.h"
     26 #include "nsIObserver.h"
     27 
     28 class nsZipReaderCache;
     29 
     30 /*-------------------------------------------------------------------------
     31 * Class nsJAR declaration.
     32 * nsJAR serves as an XPCOM wrapper for nsZipArchive with the addition of
     33 * JAR manifest file parsing.
     34 *------------------------------------------------------------------------*/
     35 class nsJAR final : public nsIZipReader {
     36  // Allows nsJARInputStream to call the verification functions
     37  friend class nsJARInputStream;
     38  // Allows nsZipReaderCache to access mOuterZipEntry
     39  friend class nsZipReaderCache;
     40 
     41 private:
     42  virtual ~nsJAR();
     43 
     44 public:
     45  nsJAR();
     46 
     47  NS_DEFINE_STATIC_CID_ACCESSOR(NS_ZIPREADER_CID)
     48 
     49  NS_DECL_THREADSAFE_ISUPPORTS
     50 
     51  NS_DECL_NSIZIPREADER
     52 
     53  nsresult GetFullJarPath(nsACString& aResult);
     54 
     55  // These access to mReleaseTime, which is locked by nsZipReaderCache's
     56  // mLock, not nsJAR's mLock
     57  PRIntervalTime GetReleaseTime() { return mReleaseTime; }
     58 
     59  bool IsReleased() { return mReleaseTime != PR_INTERVAL_NO_TIMEOUT; }
     60 
     61  void SetReleaseTime() { mReleaseTime = PR_IntervalNow(); }
     62 
     63  void ClearReleaseTime() { mReleaseTime = PR_INTERVAL_NO_TIMEOUT; }
     64 
     65  void SetZipReaderCache(nsZipReaderCache* aCache) {
     66    mozilla::RecursiveMutexAutoLock lock(mLock);
     67    mCache = aCache;
     68  }
     69 
     70  nsresult GetNSPRFileDesc(PRFileDesc** aNSPRFileDesc);
     71 
     72 protected:
     73  nsresult LoadEntry(const nsACString& aFilename, nsCString& aBuf);
     74  int32_t ReadLine(const char** src);
     75 
     76  // used by nsZipReaderCache for flushing entries; access is locked by
     77  // nsZipReaderCache's mLock
     78  PRIntervalTime mReleaseTime;
     79 
     80  //-- Private data members, protected by mLock
     81  mozilla::RecursiveMutex mLock;
     82  // The entry in the zip this zip is reading from
     83  nsCString mOuterZipEntry MOZ_GUARDED_BY(mLock);
     84  // The zip/jar file on disk
     85  nsCOMPtr<nsIFile> mZipFile MOZ_GUARDED_BY(mLock);
     86  // The underlying zip archive
     87  RefPtr<nsZipArchive> mZip MOZ_GUARDED_BY(mLock);
     88  // if cached, this points to the cache it's contained in
     89  nsZipReaderCache* mCache MOZ_GUARDED_BY(mLock);
     90 };
     91 
     92 /**
     93 * nsJARItem
     94 *
     95 * An individual JAR entry. A set of nsJARItems matching a
     96 * supplied pattern are returned in a nsJAREnumerator.
     97 */
     98 class nsJARItem : public nsIZipEntry {
     99 public:
    100  NS_DECL_THREADSAFE_ISUPPORTS
    101  NS_DECL_NSIZIPENTRY
    102 
    103  explicit nsJARItem(nsZipItem* aZipItem);
    104 
    105 private:
    106  virtual ~nsJARItem() {}
    107 
    108  const uint32_t mSize;     /* size in original file */
    109  const uint32_t mRealsize; /* inflated size */
    110  const uint32_t mCrc32;
    111  const PRTime mLastModTime;
    112  const uint16_t mCompression;
    113  const uint32_t mPermissions;
    114  const bool mIsDirectory;
    115  const bool mIsSynthetic;
    116 };
    117 
    118 /**
    119 * nsJAREnumerator
    120 *
    121 * Enumerates a list of files in a zip archive
    122 * (based on a pattern match in its member nsZipFind).
    123 */
    124 class nsJAREnumerator final : public nsStringEnumeratorBase {
    125 public:
    126  NS_DECL_THREADSAFE_ISUPPORTS
    127  NS_DECL_NSIUTF8STRINGENUMERATOR
    128 
    129  using nsStringEnumeratorBase::GetNext;
    130 
    131  explicit nsJAREnumerator(nsZipFind* aFind)
    132      : mFind(aFind), mName(nullptr), mNameLen(0) {
    133    NS_ASSERTION(mFind, "nsJAREnumerator: Missing zipFind.");
    134  }
    135 
    136 private:
    137  nsZipFind* mFind;
    138  const char* mName;  // pointer to an name owned by mArchive -- DON'T delete
    139  uint16_t mNameLen;
    140 
    141  ~nsJAREnumerator() { delete mFind; }
    142 };
    143 
    144 ////////////////////////////////////////////////////////////////////////////////
    145 
    146 #if defined(DEBUG_warren) || defined(DEBUG_jband)
    147 #  define ZIP_CACHE_HIT_RATE
    148 #endif
    149 
    150 class nsZipReaderCache : public nsIZipReaderCache,
    151                         public nsIObserver,
    152                         public nsSupportsWeakReference {
    153  friend class nsJAR;
    154 
    155 public:
    156  NS_DECL_THREADSAFE_ISUPPORTS
    157  NS_DECL_NSIZIPREADERCACHE
    158  NS_DECL_NSIOBSERVER
    159 
    160  nsZipReaderCache();
    161 
    162  nsresult ReleaseZip(nsJAR* reader);
    163 
    164  typedef nsRefPtrHashtable<nsCStringHashKey, nsJAR> ZipsHashtable;
    165 
    166 protected:
    167  void AssertLockOwned() { mLock.AssertCurrentThreadOwns(); }
    168 
    169  virtual ~nsZipReaderCache();
    170 
    171  mozilla::Mutex mLock;
    172  uint32_t mCacheSize MOZ_GUARDED_BY(mLock);
    173  ZipsHashtable mZips MOZ_GUARDED_BY(mLock);
    174 
    175 #ifdef ZIP_CACHE_HIT_RATE
    176  uint32_t mZipCacheLookups MOZ_GUARDED_BY(mLock);
    177  uint32_t mZipCacheHits MOZ_GUARDED_BY(mLock);
    178  uint32_t mZipCacheFlushes MOZ_GUARDED_BY(mLock);
    179  uint32_t mZipSyncMisses MOZ_GUARDED_BY(mLock);
    180 #endif
    181 
    182 private:
    183  nsresult GetZip(nsIFile* zipFile, nsIZipReader** result, bool failOnMiss);
    184 };
    185 
    186 ////////////////////////////////////////////////////////////////////////////////
    187 
    188 #endif /* nsJAR_h_ */