|
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/. */ |
|
6 |
|
7 |
|
8 #ifndef _nsCacheEntryDescriptor_h_ |
|
9 #define _nsCacheEntryDescriptor_h_ |
|
10 |
|
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" |
|
19 |
|
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 |
|
31 |
|
32 friend class nsAsyncDoomEvent; |
|
33 friend class nsCacheService; |
|
34 |
|
35 nsCacheEntryDescriptor(nsCacheEntry * entry, nsCacheAccessMode mode); |
|
36 virtual ~nsCacheEntryDescriptor(); |
|
37 |
|
38 /** |
|
39 * utility method to attempt changing data size of associated entry |
|
40 */ |
|
41 nsresult RequestDataSizeChange(int32_t deltaSize); |
|
42 |
|
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"); |
|
51 |
|
52 bool doomEntry = false; |
|
53 bool asyncDoomPending; |
|
54 { |
|
55 mozilla::MutexAutoLock lock(mLock); |
|
56 asyncDoomPending = mAsyncDoomPending; |
|
57 } |
|
58 |
|
59 if (asyncDoomPending && mCacheEntry) { |
|
60 doomEntry = true; |
|
61 mDoomedOnClose = true; |
|
62 } |
|
63 mCacheEntry = nullptr; |
|
64 |
|
65 return doomEntry; |
|
66 } |
|
67 |
|
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; |
|
77 |
|
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 |
|
87 |
|
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 } |
|
100 |
|
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 }; |
|
108 |
|
109 |
|
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 |
|
119 |
|
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 }; |
|
139 |
|
140 |
|
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; |
|
149 |
|
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 |
|
159 |
|
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(); |
|
171 |
|
172 NS_ASSERTION(!mOutput, "Bad state"); |
|
173 NS_ASSERTION(!mDescriptor, "Bad state"); |
|
174 } |
|
175 |
|
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 }; |
|
186 |
|
187 |
|
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 |
|
198 |
|
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 }; |
|
219 |
|
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 }; |
|
233 |
|
234 |
|
235 #endif // _nsCacheEntryDescriptor_h_ |