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"
17 #include "mozilla/Mutex.h"
18 #include "nsVoidArray.h"
20 /******************************************************************************
21 * nsCacheEntryDescriptor
22 *******************************************************************************/
23 class nsCacheEntryDescriptor
:
25 public nsICacheEntryDescriptor
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();
39 * utility method to attempt changing data size of associated entry
41 nsresult
RequestDataSizeChange(int32_t deltaSize
);
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
) {
61 mDoomedOnClose
= true;
63 mCacheEntry
= nullptr;
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
;
79 nsCacheEntryDescriptor
* mDescriptor
;
80 nsCOMPtr
<nsIInputStream
> mInput
;
81 uint32_t mStartOffset
;
86 NS_DECL_NSIINPUTSTREAM
88 nsInputStreamWrapper(nsCacheEntryDescriptor
* desc
, uint32_t off
)
92 , mLock("nsInputStreamWrapper.mLock")
94 NS_ADDREF(mDescriptor
);
96 virtual ~nsInputStreamWrapper()
98 NS_IF_RELEASE(mDescriptor
);
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
{
112 unsigned char* mReadBuffer
;
113 uint32_t mReadBufferLen
;
115 bool mStreamInitialized
;
120 nsDecompressInputStreamWrapper(nsCacheEntryDescriptor
* desc
,
122 : nsInputStreamWrapper(desc
, off
)
125 , mStreamInitialized(false)
126 , mStreamEnded(false)
129 virtual ~nsDecompressInputStreamWrapper()
133 NS_IMETHOD
Read(char* buf
, uint32_t count
, uint32_t * result
);
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
;
151 nsCacheEntryDescriptor
* mDescriptor
;
152 nsCOMPtr
<nsIOutputStream
> mOutput
;
153 uint32_t mStartOffset
;
155 mozilla::Mutex mLock
;
158 NS_DECL_NSIOUTPUTSTREAM
160 nsOutputStreamWrapper(nsCacheEntryDescriptor
* desc
, uint32_t off
)
163 , mInitialized(false)
164 , mLock("nsOutputStreamWrapper.mLock")
166 NS_ADDREF(mDescriptor
); // owning ref
168 virtual ~nsOutputStreamWrapper()
172 NS_ASSERTION(!mOutput
, "Bad state");
173 NS_ASSERTION(!mDescriptor
, "Bad state");
178 nsresult
EnsureInit();
179 nsresult
OnWrite(uint32_t count
);
180 nsresult
Write_Locked(const char * buf
,
183 nsresult
Close_Locked();
184 void CloseInternal();
188 class nsCompressOutputStreamWrapper
: public nsOutputStreamWrapper
{
190 unsigned char* mWriteBuffer
;
191 uint32_t mWriteBufferLen
;
193 bool mStreamInitialized
;
195 uint32_t mUncompressedCount
;
199 nsCompressOutputStreamWrapper(nsCacheEntryDescriptor
* desc
,
201 : nsOutputStreamWrapper(desc
, off
)
204 , mStreamInitialized(false)
205 , mStreamEnded(false)
206 , mUncompressedCount(0)
209 virtual ~nsCompressOutputStreamWrapper()
213 NS_IMETHOD
Write(const char* buf
, uint32_t count
, uint32_t * result
);
216 nsresult
InitZstream();
217 nsresult
WriteBuffer();
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
;
231 bool mClosingDescriptor
;
235 #endif // _nsCacheEntryDescriptor_h_