Bug 1888033 - [Menu Redesign] Add a secret setting and feature flag for the menu...
[gecko.git] / modules / libjar / nsJAR.h
blob01332b8616d9ed64a50c8593aed66904437aab40
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/. */
6 #ifndef nsJAR_h_
7 #define nsJAR_h_
9 #include "nscore.h"
10 #include "prio.h"
11 #include "mozilla/Logging.h"
12 #include "prinrval.h"
14 #include "mozilla/Atomics.h"
15 #include "mozilla/RecursiveMutex.h"
16 #include "nsCOMPtr.h"
17 #include "nsClassHashtable.h"
18 #include "nsString.h"
19 #include "nsIFile.h"
20 #include "nsStringEnumerator.h"
21 #include "nsHashKeys.h"
22 #include "nsRefPtrHashtable.h"
23 #include "nsTHashtable.h"
24 #include "nsIZipReader.h"
25 #include "nsZipArchive.h"
26 #include "nsWeakReference.h"
27 #include "nsIObserver.h"
28 #include "mozilla/Attributes.h"
30 class nsZipReaderCache;
32 /*-------------------------------------------------------------------------
33 * Class nsJAR declaration.
34 * nsJAR serves as an XPCOM wrapper for nsZipArchive with the addition of
35 * JAR manifest file parsing.
36 *------------------------------------------------------------------------*/
37 class nsJAR final : public nsIZipReader {
38 // Allows nsJARInputStream to call the verification functions
39 friend class nsJARInputStream;
40 // Allows nsZipReaderCache to access mOuterZipEntry
41 friend class nsZipReaderCache;
43 private:
44 virtual ~nsJAR();
46 public:
47 nsJAR();
49 NS_DEFINE_STATIC_CID_ACCESSOR(NS_ZIPREADER_CID)
51 NS_DECL_THREADSAFE_ISUPPORTS
53 NS_DECL_NSIZIPREADER
55 nsresult GetFullJarPath(nsACString& aResult);
57 // These access to mReleaseTime, which is locked by nsZipReaderCache's
58 // mLock, not nsJAR's mLock
59 PRIntervalTime GetReleaseTime() { return mReleaseTime; }
61 bool IsReleased() { return mReleaseTime != PR_INTERVAL_NO_TIMEOUT; }
63 void SetReleaseTime() { mReleaseTime = PR_IntervalNow(); }
65 void ClearReleaseTime() { mReleaseTime = PR_INTERVAL_NO_TIMEOUT; }
67 void SetZipReaderCache(nsZipReaderCache* aCache) {
68 mozilla::RecursiveMutexAutoLock lock(mLock);
69 mCache = aCache;
72 nsresult GetNSPRFileDesc(PRFileDesc** aNSPRFileDesc);
74 protected:
75 nsresult LoadEntry(const nsACString& aFilename, nsCString& aBuf);
76 int32_t ReadLine(const char** src);
78 // used by nsZipReaderCache for flushing entries; access is locked by
79 // nsZipReaderCache's mLock
80 PRIntervalTime mReleaseTime;
82 //-- Private data members, protected by mLock
83 mozilla::RecursiveMutex mLock;
84 // The entry in the zip this zip is reading from
85 nsCString mOuterZipEntry MOZ_GUARDED_BY(mLock);
86 // The zip/jar file on disk
87 nsCOMPtr<nsIFile> mZipFile MOZ_GUARDED_BY(mLock);
88 // The underlying zip archive
89 RefPtr<nsZipArchive> mZip MOZ_GUARDED_BY(mLock);
90 // if cached, this points to the cache it's contained in
91 nsZipReaderCache* mCache MOZ_GUARDED_BY(mLock);
94 /**
95 * nsJARItem
97 * An individual JAR entry. A set of nsJARItems matching a
98 * supplied pattern are returned in a nsJAREnumerator.
100 class nsJARItem : public nsIZipEntry {
101 public:
102 NS_DECL_THREADSAFE_ISUPPORTS
103 NS_DECL_NSIZIPENTRY
105 explicit nsJARItem(nsZipItem* aZipItem);
107 private:
108 virtual ~nsJARItem() {}
110 const uint32_t mSize; /* size in original file */
111 const uint32_t mRealsize; /* inflated size */
112 const uint32_t mCrc32;
113 const PRTime mLastModTime;
114 const uint16_t mCompression;
115 const uint32_t mPermissions;
116 const bool mIsDirectory;
117 const bool mIsSynthetic;
121 * nsJAREnumerator
123 * Enumerates a list of files in a zip archive
124 * (based on a pattern match in its member nsZipFind).
126 class nsJAREnumerator final : public nsStringEnumeratorBase {
127 public:
128 NS_DECL_THREADSAFE_ISUPPORTS
129 NS_DECL_NSIUTF8STRINGENUMERATOR
131 using nsStringEnumeratorBase::GetNext;
133 explicit nsJAREnumerator(nsZipFind* aFind)
134 : mFind(aFind), mName(nullptr), mNameLen(0) {
135 NS_ASSERTION(mFind, "nsJAREnumerator: Missing zipFind.");
138 private:
139 nsZipFind* mFind;
140 const char* mName; // pointer to an name owned by mArchive -- DON'T delete
141 uint16_t mNameLen;
143 ~nsJAREnumerator() { delete mFind; }
146 ////////////////////////////////////////////////////////////////////////////////
148 #if defined(DEBUG_warren) || defined(DEBUG_jband)
149 # define ZIP_CACHE_HIT_RATE
150 #endif
152 class nsZipReaderCache : public nsIZipReaderCache,
153 public nsIObserver,
154 public nsSupportsWeakReference {
155 friend class nsJAR;
157 public:
158 NS_DECL_THREADSAFE_ISUPPORTS
159 NS_DECL_NSIZIPREADERCACHE
160 NS_DECL_NSIOBSERVER
162 nsZipReaderCache();
164 nsresult ReleaseZip(nsJAR* reader);
166 typedef nsRefPtrHashtable<nsCStringHashKey, nsJAR> ZipsHashtable;
168 protected:
169 void AssertLockOwned() { mLock.AssertCurrentThreadOwns(); }
171 virtual ~nsZipReaderCache();
173 mozilla::Mutex mLock;
174 uint32_t mCacheSize MOZ_GUARDED_BY(mLock);
175 ZipsHashtable mZips MOZ_GUARDED_BY(mLock);
177 #ifdef ZIP_CACHE_HIT_RATE
178 uint32_t mZipCacheLookups MOZ_GUARDED_BY(mLock);
179 uint32_t mZipCacheHits MOZ_GUARDED_BY(mLock);
180 uint32_t mZipCacheFlushes MOZ_GUARDED_BY(mLock);
181 uint32_t mZipSyncMisses MOZ_GUARDED_BY(mLock);
182 #endif
184 private:
185 nsresult GetZip(nsIFile* zipFile, nsIZipReader** result, bool failOnMiss);
188 ////////////////////////////////////////////////////////////////////////////////
190 #endif /* nsJAR_h_ */