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/. */
11 #include "mozilla/Logging.h"
14 #include "mozilla/Atomics.h"
15 #include "mozilla/RecursiveMutex.h"
17 #include "nsClassHashtable.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
;
49 NS_DEFINE_STATIC_CID_ACCESSOR(NS_ZIPREADER_CID
)
51 NS_DECL_THREADSAFE_ISUPPORTS
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
);
72 nsresult
GetNSPRFileDesc(PRFileDesc
** aNSPRFileDesc
);
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
);
97 * An individual JAR entry. A set of nsJARItems matching a
98 * supplied pattern are returned in a nsJAREnumerator.
100 class nsJARItem
: public nsIZipEntry
{
102 NS_DECL_THREADSAFE_ISUPPORTS
105 explicit nsJARItem(nsZipItem
* aZipItem
);
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
;
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
{
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.");
140 const char* mName
; // pointer to an name owned by mArchive -- DON'T delete
143 ~nsJAREnumerator() { delete mFind
; }
146 ////////////////////////////////////////////////////////////////////////////////
148 #if defined(DEBUG_warren) || defined(DEBUG_jband)
149 # define ZIP_CACHE_HIT_RATE
152 class nsZipReaderCache
: public nsIZipReaderCache
,
154 public nsSupportsWeakReference
{
158 NS_DECL_THREADSAFE_ISUPPORTS
159 NS_DECL_NSIZIPREADERCACHE
164 nsresult
ReleaseZip(nsJAR
* reader
);
166 typedef nsRefPtrHashtable
<nsCStringHashKey
, nsJAR
> ZipsHashtable
;
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
);
185 nsresult
GetZip(nsIFile
* zipFile
, nsIZipReader
** result
, bool failOnMiss
);
188 ////////////////////////////////////////////////////////////////////////////////
190 #endif /* nsJAR_h_ */