Bumping gaia.json for 2 gaia revision(s) a=gaia-bump
[gecko.git] / netwerk / cache / nsCacheEntryDescriptor.h
blobc443f728c69393919ead1aee4713d341f79d1772
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
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 MOZ_FINAL :
24 public PRCList,
25 public nsICacheEntryDescriptor
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);
37 /**
38 * utility method to attempt changing data size of associated entry
40 nsresult RequestDataSizeChange(int32_t deltaSize);
42 /**
43 * methods callbacks for nsCacheService
45 nsCacheEntry * CacheEntry(void) { return mCacheEntry; }
46 bool ClearCacheEntry(void)
48 NS_ASSERTION(mInputWrappers.Count() == 0, "Bad state");
49 NS_ASSERTION(!mOutputWrapper, "Bad state");
51 bool doomEntry = false;
52 bool asyncDoomPending;
54 mozilla::MutexAutoLock lock(mLock);
55 asyncDoomPending = mAsyncDoomPending;
58 if (asyncDoomPending && mCacheEntry) {
59 doomEntry = true;
60 mDoomedOnClose = true;
62 mCacheEntry = nullptr;
64 return doomEntry;
67 private:
68 virtual ~nsCacheEntryDescriptor();
70 /*************************************************************************
71 * input stream wrapper class -
73 * The input stream wrapper references the descriptor, but the descriptor
74 * doesn't need any references to the stream wrapper.
75 *************************************************************************/
76 class nsInputStreamWrapper : public nsIInputStream {
77 friend class nsCacheEntryDescriptor;
79 private:
80 nsCacheEntryDescriptor * mDescriptor;
81 nsCOMPtr<nsIInputStream> mInput;
82 uint32_t mStartOffset;
83 bool mInitialized;
84 mozilla::Mutex mLock;
85 public:
86 NS_DECL_THREADSAFE_ISUPPORTS
87 NS_DECL_NSIINPUTSTREAM
89 nsInputStreamWrapper(nsCacheEntryDescriptor * desc, uint32_t off)
90 : mDescriptor(desc)
91 , mStartOffset(off)
92 , mInitialized(false)
93 , mLock("nsInputStreamWrapper.mLock")
95 NS_ADDREF(mDescriptor);
98 private:
99 virtual ~nsInputStreamWrapper()
101 NS_IF_RELEASE(mDescriptor);
104 nsresult LazyInit();
105 nsresult EnsureInit();
106 nsresult Read_Locked(char *buf, uint32_t count, uint32_t *countRead);
107 nsresult Close_Locked();
108 void CloseInternal();
112 class nsDecompressInputStreamWrapper : public nsInputStreamWrapper {
113 private:
114 unsigned char* mReadBuffer;
115 uint32_t mReadBufferLen;
116 z_stream mZstream;
117 bool mStreamInitialized;
118 bool mStreamEnded;
119 public:
120 NS_DECL_ISUPPORTS_INHERITED
122 nsDecompressInputStreamWrapper(nsCacheEntryDescriptor * desc,
123 uint32_t off)
124 : nsInputStreamWrapper(desc, off)
125 , mReadBuffer(0)
126 , mReadBufferLen(0)
127 , mStreamInitialized(false)
128 , mStreamEnded(false)
131 NS_IMETHOD Read(char* buf, uint32_t count, uint32_t * result) MOZ_OVERRIDE;
132 NS_IMETHOD Close() MOZ_OVERRIDE;
133 private:
134 virtual ~nsDecompressInputStreamWrapper()
136 Close();
138 nsresult InitZstream();
139 nsresult EndZstream();
143 /*************************************************************************
144 * output stream wrapper class -
146 * The output stream wrapper references the descriptor, but the descriptor
147 * doesn't need any references to the stream wrapper.
148 *************************************************************************/
149 class nsOutputStreamWrapper : public nsIOutputStream {
150 friend class nsCacheEntryDescriptor;
152 protected:
153 nsCacheEntryDescriptor * mDescriptor;
154 nsCOMPtr<nsIOutputStream> mOutput;
155 uint32_t mStartOffset;
156 bool mInitialized;
157 mozilla::Mutex mLock;
158 public:
159 NS_DECL_THREADSAFE_ISUPPORTS
160 NS_DECL_NSIOUTPUTSTREAM
162 nsOutputStreamWrapper(nsCacheEntryDescriptor * desc, uint32_t off)
163 : mDescriptor(desc)
164 , mStartOffset(off)
165 , mInitialized(false)
166 , mLock("nsOutputStreamWrapper.mLock")
168 NS_ADDREF(mDescriptor); // owning ref
171 private:
172 virtual ~nsOutputStreamWrapper()
174 Close();
176 NS_ASSERTION(!mOutput, "Bad state");
177 NS_ASSERTION(!mDescriptor, "Bad state");
180 nsresult LazyInit();
181 nsresult EnsureInit();
182 nsresult OnWrite(uint32_t count);
183 nsresult Write_Locked(const char * buf,
184 uint32_t count,
185 uint32_t * result);
186 nsresult Close_Locked();
187 void CloseInternal();
191 class nsCompressOutputStreamWrapper : public nsOutputStreamWrapper {
192 private:
193 unsigned char* mWriteBuffer;
194 uint32_t mWriteBufferLen;
195 z_stream mZstream;
196 bool mStreamInitialized;
197 bool mStreamEnded;
198 uint32_t mUncompressedCount;
199 public:
200 NS_DECL_ISUPPORTS_INHERITED
202 nsCompressOutputStreamWrapper(nsCacheEntryDescriptor * desc,
203 uint32_t off)
204 : nsOutputStreamWrapper(desc, off)
205 , mWriteBuffer(0)
206 , mWriteBufferLen(0)
207 , mStreamInitialized(false)
208 , mStreamEnded(false)
209 , mUncompressedCount(0)
212 NS_IMETHOD Write(const char* buf, uint32_t count, uint32_t * result) MOZ_OVERRIDE;
213 NS_IMETHOD Close() MOZ_OVERRIDE;
214 private:
215 virtual ~nsCompressOutputStreamWrapper()
217 Close();
219 nsresult InitZstream();
220 nsresult WriteBuffer();
223 private:
225 * nsCacheEntryDescriptor data members
227 nsCacheEntry * mCacheEntry; // we are a child of the entry
228 nsCacheAccessMode mAccessGranted;
229 nsVoidArray mInputWrappers;
230 nsOutputStreamWrapper * mOutputWrapper;
231 mozilla::Mutex mLock;
232 bool mAsyncDoomPending;
233 bool mDoomedOnClose;
234 bool mClosingDescriptor;
238 #endif // _nsCacheEntryDescriptor_h_