Bug 853360 - Implement the coneGain part of the AudioPannerNode. r=roc
[gecko.git] / netwerk / cache / nsCacheEntryDescriptor.h
blob2c59a0788cecde405d01eaa8b638355b47a900a6
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 :
24 public PRCList,
25 public nsICacheEntryDescriptor
27 public:
28 NS_DECL_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
41 nsresult RequestDataSizeChange(int32_t deltaSize);
43 /**
44 * methods callbacks for nsCacheService
46 nsCacheEntry * CacheEntry(void) { return mCacheEntry; }
47 bool ClearCacheEntry(void)
49 NS_ASSERTION(mInputWrappers.Count() == 0, "Bad state");
50 NS_ASSERTION(!mOutputWrapper, "Bad state");
52 bool doomEntry = false;
53 bool asyncDoomPending;
55 mozilla::MutexAutoLock lock(mLock);
56 asyncDoomPending = mAsyncDoomPending;
59 if (asyncDoomPending && mCacheEntry) {
60 doomEntry = true;
61 mDoomedOnClose = true;
63 mCacheEntry = nullptr;
65 return doomEntry;
68 private:
69 /*************************************************************************
70 * input stream wrapper class -
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_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")
94 NS_ADDREF(mDescriptor);
96 virtual ~nsInputStreamWrapper()
98 NS_IF_RELEASE(mDescriptor);
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();
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_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)
129 virtual ~nsDecompressInputStreamWrapper()
131 Close();
133 NS_IMETHOD Read(char* buf, uint32_t count, uint32_t * result);
134 NS_IMETHOD Close();
135 private:
136 nsresult InitZstream();
137 nsresult EndZstream();
141 /*************************************************************************
142 * output stream wrapper class -
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_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")
166 NS_ADDREF(mDescriptor); // owning ref
168 virtual ~nsOutputStreamWrapper()
170 Close();
172 NS_ASSERTION(!mOutput, "Bad state");
173 NS_ASSERTION(!mDescriptor, "Bad state");
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();
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_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)
209 virtual ~nsCompressOutputStreamWrapper()
211 Close();
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();
220 private:
222 * nsCacheEntryDescriptor data members
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;
235 #endif // _nsCacheEntryDescriptor_h_