Coverage Report

Created: 2018-09-25 14:53

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