Thu, 15 Jan 2015 15:55:04 +0100
Back out 97036ab72558 which inappropriately compared turds to third parties.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 #ifndef _nsCacheEntryDescriptor_h_
9 #define _nsCacheEntryDescriptor_h_
11 #include "nsICacheEntryDescriptor.h"
12 #include "nsCacheEntry.h"
13 #include "nsIInputStream.h"
14 #include "nsIOutputStream.h"
15 #include "nsCacheService.h"
16 #include "zlib.h"
17 #include "mozilla/Mutex.h"
18 #include "nsVoidArray.h"
20 /******************************************************************************
21 * nsCacheEntryDescriptor
22 *******************************************************************************/
23 class nsCacheEntryDescriptor :
24 public PRCList,
25 public nsICacheEntryDescriptor
26 {
27 public:
28 NS_DECL_THREADSAFE_ISUPPORTS
29 NS_DECL_NSICACHEENTRYDESCRIPTOR
30 NS_DECL_NSICACHEENTRYINFO
32 friend class nsAsyncDoomEvent;
33 friend class nsCacheService;
35 nsCacheEntryDescriptor(nsCacheEntry * entry, nsCacheAccessMode mode);
36 virtual ~nsCacheEntryDescriptor();
38 /**
39 * utility method to attempt changing data size of associated entry
40 */
41 nsresult RequestDataSizeChange(int32_t deltaSize);
43 /**
44 * methods callbacks for nsCacheService
45 */
46 nsCacheEntry * CacheEntry(void) { return mCacheEntry; }
47 bool ClearCacheEntry(void)
48 {
49 NS_ASSERTION(mInputWrappers.Count() == 0, "Bad state");
50 NS_ASSERTION(!mOutputWrapper, "Bad state");
52 bool doomEntry = false;
53 bool asyncDoomPending;
54 {
55 mozilla::MutexAutoLock lock(mLock);
56 asyncDoomPending = mAsyncDoomPending;
57 }
59 if (asyncDoomPending && mCacheEntry) {
60 doomEntry = true;
61 mDoomedOnClose = true;
62 }
63 mCacheEntry = nullptr;
65 return doomEntry;
66 }
68 private:
69 /*************************************************************************
70 * input stream wrapper class -
71 *
72 * The input stream wrapper references the descriptor, but the descriptor
73 * doesn't need any references to the stream wrapper.
74 *************************************************************************/
75 class nsInputStreamWrapper : public nsIInputStream {
76 friend class nsCacheEntryDescriptor;
78 private:
79 nsCacheEntryDescriptor * mDescriptor;
80 nsCOMPtr<nsIInputStream> mInput;
81 uint32_t mStartOffset;
82 bool mInitialized;
83 mozilla::Mutex mLock;
84 public:
85 NS_DECL_THREADSAFE_ISUPPORTS
86 NS_DECL_NSIINPUTSTREAM
88 nsInputStreamWrapper(nsCacheEntryDescriptor * desc, uint32_t off)
89 : mDescriptor(desc)
90 , mStartOffset(off)
91 , mInitialized(false)
92 , mLock("nsInputStreamWrapper.mLock")
93 {
94 NS_ADDREF(mDescriptor);
95 }
96 virtual ~nsInputStreamWrapper()
97 {
98 NS_IF_RELEASE(mDescriptor);
99 }
101 private:
102 nsresult LazyInit();
103 nsresult EnsureInit();
104 nsresult Read_Locked(char *buf, uint32_t count, uint32_t *countRead);
105 nsresult Close_Locked();
106 void CloseInternal();
107 };
110 class nsDecompressInputStreamWrapper : public nsInputStreamWrapper {
111 private:
112 unsigned char* mReadBuffer;
113 uint32_t mReadBufferLen;
114 z_stream mZstream;
115 bool mStreamInitialized;
116 bool mStreamEnded;
117 public:
118 NS_DECL_THREADSAFE_ISUPPORTS
120 nsDecompressInputStreamWrapper(nsCacheEntryDescriptor * desc,
121 uint32_t off)
122 : nsInputStreamWrapper(desc, off)
123 , mReadBuffer(0)
124 , mReadBufferLen(0)
125 , mStreamInitialized(false)
126 , mStreamEnded(false)
127 {
128 }
129 virtual ~nsDecompressInputStreamWrapper()
130 {
131 Close();
132 }
133 NS_IMETHOD Read(char* buf, uint32_t count, uint32_t * result);
134 NS_IMETHOD Close();
135 private:
136 nsresult InitZstream();
137 nsresult EndZstream();
138 };
141 /*************************************************************************
142 * output stream wrapper class -
143 *
144 * The output stream wrapper references the descriptor, but the descriptor
145 * doesn't need any references to the stream wrapper.
146 *************************************************************************/
147 class nsOutputStreamWrapper : public nsIOutputStream {
148 friend class nsCacheEntryDescriptor;
150 protected:
151 nsCacheEntryDescriptor * mDescriptor;
152 nsCOMPtr<nsIOutputStream> mOutput;
153 uint32_t mStartOffset;
154 bool mInitialized;
155 mozilla::Mutex mLock;
156 public:
157 NS_DECL_THREADSAFE_ISUPPORTS
158 NS_DECL_NSIOUTPUTSTREAM
160 nsOutputStreamWrapper(nsCacheEntryDescriptor * desc, uint32_t off)
161 : mDescriptor(desc)
162 , mStartOffset(off)
163 , mInitialized(false)
164 , mLock("nsOutputStreamWrapper.mLock")
165 {
166 NS_ADDREF(mDescriptor); // owning ref
167 }
168 virtual ~nsOutputStreamWrapper()
169 {
170 Close();
172 NS_ASSERTION(!mOutput, "Bad state");
173 NS_ASSERTION(!mDescriptor, "Bad state");
174 }
176 private:
177 nsresult LazyInit();
178 nsresult EnsureInit();
179 nsresult OnWrite(uint32_t count);
180 nsresult Write_Locked(const char * buf,
181 uint32_t count,
182 uint32_t * result);
183 nsresult Close_Locked();
184 void CloseInternal();
185 };
188 class nsCompressOutputStreamWrapper : public nsOutputStreamWrapper {
189 private:
190 unsigned char* mWriteBuffer;
191 uint32_t mWriteBufferLen;
192 z_stream mZstream;
193 bool mStreamInitialized;
194 bool mStreamEnded;
195 uint32_t mUncompressedCount;
196 public:
197 NS_DECL_THREADSAFE_ISUPPORTS
199 nsCompressOutputStreamWrapper(nsCacheEntryDescriptor * desc,
200 uint32_t off)
201 : nsOutputStreamWrapper(desc, off)
202 , mWriteBuffer(0)
203 , mWriteBufferLen(0)
204 , mStreamInitialized(false)
205 , mStreamEnded(false)
206 , mUncompressedCount(0)
207 {
208 }
209 virtual ~nsCompressOutputStreamWrapper()
210 {
211 Close();
212 }
213 NS_IMETHOD Write(const char* buf, uint32_t count, uint32_t * result);
214 NS_IMETHOD Close();
215 private:
216 nsresult InitZstream();
217 nsresult WriteBuffer();
218 };
220 private:
221 /**
222 * nsCacheEntryDescriptor data members
223 */
224 nsCacheEntry * mCacheEntry; // we are a child of the entry
225 nsCacheAccessMode mAccessGranted;
226 nsVoidArray mInputWrappers;
227 nsOutputStreamWrapper * mOutputWrapper;
228 mozilla::Mutex mLock;
229 bool mAsyncDoomPending;
230 bool mDoomedOnClose;
231 bool mClosingDescriptor;
232 };
235 #endif // _nsCacheEntryDescriptor_h_