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 MOZ_FINAL
:
25 public nsICacheEntryDescriptor
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
);
38 * utility method to attempt changing data size of associated entry
40 nsresult
RequestDataSizeChange(int32_t deltaSize
);
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
) {
60 mDoomedOnClose
= true;
62 mCacheEntry
= nullptr;
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
;
80 nsCacheEntryDescriptor
* mDescriptor
;
81 nsCOMPtr
<nsIInputStream
> mInput
;
82 uint32_t mStartOffset
;
86 NS_DECL_THREADSAFE_ISUPPORTS
87 NS_DECL_NSIINPUTSTREAM
89 nsInputStreamWrapper(nsCacheEntryDescriptor
* desc
, uint32_t off
)
93 , mLock("nsInputStreamWrapper.mLock")
95 NS_ADDREF(mDescriptor
);
99 virtual ~nsInputStreamWrapper()
101 NS_IF_RELEASE(mDescriptor
);
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
{
114 unsigned char* mReadBuffer
;
115 uint32_t mReadBufferLen
;
117 bool mStreamInitialized
;
120 NS_DECL_ISUPPORTS_INHERITED
122 nsDecompressInputStreamWrapper(nsCacheEntryDescriptor
* desc
,
124 : nsInputStreamWrapper(desc
, off
)
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
;
134 virtual ~nsDecompressInputStreamWrapper()
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
;
153 nsCacheEntryDescriptor
* mDescriptor
;
154 nsCOMPtr
<nsIOutputStream
> mOutput
;
155 uint32_t mStartOffset
;
157 mozilla::Mutex mLock
;
159 NS_DECL_THREADSAFE_ISUPPORTS
160 NS_DECL_NSIOUTPUTSTREAM
162 nsOutputStreamWrapper(nsCacheEntryDescriptor
* desc
, uint32_t off
)
165 , mInitialized(false)
166 , mLock("nsOutputStreamWrapper.mLock")
168 NS_ADDREF(mDescriptor
); // owning ref
172 virtual ~nsOutputStreamWrapper()
176 NS_ASSERTION(!mOutput
, "Bad state");
177 NS_ASSERTION(!mDescriptor
, "Bad state");
181 nsresult
EnsureInit();
182 nsresult
OnWrite(uint32_t count
);
183 nsresult
Write_Locked(const char * buf
,
186 nsresult
Close_Locked();
187 void CloseInternal();
191 class nsCompressOutputStreamWrapper
: public nsOutputStreamWrapper
{
193 unsigned char* mWriteBuffer
;
194 uint32_t mWriteBufferLen
;
196 bool mStreamInitialized
;
198 uint32_t mUncompressedCount
;
200 NS_DECL_ISUPPORTS_INHERITED
202 nsCompressOutputStreamWrapper(nsCacheEntryDescriptor
* desc
,
204 : nsOutputStreamWrapper(desc
, off
)
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
;
215 virtual ~nsCompressOutputStreamWrapper()
219 nsresult
InitZstream();
220 nsresult
WriteBuffer();
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
;
234 bool mClosingDescriptor
;
238 #endif // _nsCacheEntryDescriptor_h_