1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 : *
3 : * ***** BEGIN LICENSE BLOCK *****
4 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 : *
6 : * The contents of this file are subject to the Mozilla Public License Version
7 : * 1.1 (the "License"); you may not use this file except in compliance with
8 : * the License. You may obtain a copy of the License at
9 : * http://www.mozilla.org/MPL/
10 : *
11 : * Software distributed under the License is distributed on an "AS IS" basis,
12 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 : * for the specific language governing rights and limitations under the
14 : * License.
15 : *
16 : * The Original Code is nsCacheEntryDescriptor.h, released
17 : * February 22, 2001.
18 : *
19 : * The Initial Developer of the Original Code is
20 : * Netscape Communications Corporation.
21 : * Portions created by the Initial Developer are Copyright (C) 2001
22 : * the Initial Developer. All Rights Reserved.
23 : *
24 : * Contributor(s):
25 : * Gordon Sheridan, 22-February-2001
26 : *
27 : * Alternatively, the contents of this file may be used under the terms of
28 : * either the GNU General Public License Version 2 or later (the "GPL"), or
29 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 : * in which case the provisions of the GPL or the LGPL are applicable instead
31 : * of those above. If you wish to allow use of your version of this file only
32 : * under the terms of either the GPL or the LGPL, and not to allow others to
33 : * use your version of this file under the terms of the MPL, indicate your
34 : * decision by deleting the provisions above and replace them with the notice
35 : * and other provisions required by the GPL or the LGPL. If you do not delete
36 : * the provisions above, a recipient may use your version of this file under
37 : * the terms of any one of the MPL, the GPL or the LGPL.
38 : *
39 : * ***** END LICENSE BLOCK ***** */
40 :
41 :
42 : #ifndef _nsCacheEntryDescriptor_h_
43 : #define _nsCacheEntryDescriptor_h_
44 :
45 : #include "nsICacheEntryDescriptor.h"
46 : #include "nsCacheEntry.h"
47 : #include "nsIInputStream.h"
48 : #include "nsIOutputStream.h"
49 : #include "nsCacheService.h"
50 : #include "nsIDiskCacheStreamInternal.h"
51 : #include "zlib.h"
52 :
53 : /******************************************************************************
54 : * nsCacheEntryDescriptor
55 : *******************************************************************************/
56 : class nsCacheEntryDescriptor :
57 : public PRCList,
58 : public nsICacheEntryDescriptor
59 : {
60 : public:
61 : NS_DECL_ISUPPORTS
62 : NS_DECL_NSICACHEENTRYDESCRIPTOR
63 : NS_DECL_NSICACHEENTRYINFO
64 :
65 : nsCacheEntryDescriptor(nsCacheEntry * entry, nsCacheAccessMode mode);
66 : virtual ~nsCacheEntryDescriptor();
67 :
68 : /**
69 : * utility method to attempt changing data size of associated entry
70 : */
71 : nsresult RequestDataSizeChange(PRInt32 deltaSize);
72 :
73 : /**
74 : * methods callbacks for nsCacheService
75 : */
76 5036 : nsCacheEntry * CacheEntry(void) { return mCacheEntry; }
77 1778 : void ClearCacheEntry(void) { mCacheEntry = nsnull; }
78 :
79 1778 : nsresult CloseOutput(void)
80 : {
81 1778 : nsresult rv = InternalCleanup(mOutput);
82 1778 : mOutput = nsnull;
83 1778 : return rv;
84 : }
85 :
86 : private:
87 1780 : nsresult InternalCleanup(nsIOutputStream *stream)
88 : {
89 1780 : if (stream) {
90 74 : nsCOMPtr<nsIDiskCacheStreamInternal> tmp (do_QueryInterface(stream));
91 37 : if (tmp)
92 14 : return tmp->CloseInternal();
93 : else
94 23 : return stream->Close();
95 : }
96 1743 : return NS_OK;
97 : }
98 :
99 :
100 : /*************************************************************************
101 : * input stream wrapper class -
102 : *
103 : * The input stream wrapper references the descriptor, but the descriptor
104 : * doesn't need any references to the stream wrapper.
105 : *************************************************************************/
106 : class nsInputStreamWrapper : public nsIInputStream {
107 : private:
108 : nsCacheEntryDescriptor * mDescriptor;
109 : nsCOMPtr<nsIInputStream> mInput;
110 : PRUint32 mStartOffset;
111 : bool mInitialized;
112 : public:
113 : NS_DECL_ISUPPORTS
114 : NS_DECL_NSIINPUTSTREAM
115 :
116 242 : nsInputStreamWrapper(nsCacheEntryDescriptor * desc, PRUint32 off)
117 : : mDescriptor(desc)
118 : , mStartOffset(off)
119 242 : , mInitialized(false)
120 : {
121 242 : NS_ADDREF(mDescriptor);
122 242 : }
123 484 : virtual ~nsInputStreamWrapper()
124 484 : {
125 242 : NS_RELEASE(mDescriptor);
126 968 : }
127 :
128 : private:
129 : nsresult LazyInit();
130 734 : nsresult EnsureInit() { return mInitialized ? NS_OK : LazyInit(); }
131 : };
132 : friend class nsInputStreamWrapper;
133 :
134 :
135 : class nsDecompressInputStreamWrapper : public nsInputStreamWrapper {
136 : private:
137 : unsigned char* mReadBuffer;
138 : PRUint32 mReadBufferLen;
139 : z_stream mZstream;
140 : PRBool mStreamInitialized;
141 : PRBool mStreamEnded;
142 : public:
143 : NS_DECL_ISUPPORTS
144 :
145 0 : nsDecompressInputStreamWrapper(nsCacheEntryDescriptor * desc,
146 : PRUint32 off)
147 : : nsInputStreamWrapper(desc, off)
148 : , mReadBuffer(0)
149 : , mReadBufferLen(0)
150 : , mStreamInitialized(PR_FALSE)
151 0 : , mStreamEnded(PR_FALSE)
152 : {
153 0 : }
154 0 : virtual ~nsDecompressInputStreamWrapper()
155 0 : {
156 0 : Close();
157 0 : }
158 : NS_IMETHOD Read(char* buf, PRUint32 count, PRUint32 * result);
159 : NS_IMETHOD Close();
160 : private:
161 : nsresult InitZstream();
162 : nsresult EndZstream();
163 : };
164 :
165 :
166 : /*************************************************************************
167 : * output stream wrapper class -
168 : *
169 : * The output stream wrapper references the descriptor, but the descriptor
170 : * doesn't need any references to the stream wrapper.
171 : *************************************************************************/
172 : class nsOutputStreamWrapper : public nsIOutputStream {
173 : protected:
174 : nsCacheEntryDescriptor * mDescriptor;
175 : nsCOMPtr<nsIOutputStream> mOutput;
176 : PRUint32 mStartOffset;
177 : bool mInitialized;
178 : public:
179 : NS_DECL_ISUPPORTS
180 : NS_DECL_NSIOUTPUTSTREAM
181 :
182 1194 : nsOutputStreamWrapper(nsCacheEntryDescriptor * desc, PRUint32 off)
183 : : mDescriptor(desc)
184 : , mStartOffset(off)
185 1194 : , mInitialized(false)
186 : {
187 1194 : NS_ADDREF(mDescriptor); // owning ref
188 1194 : }
189 2388 : virtual ~nsOutputStreamWrapper()
190 2388 : {
191 : // XXX _HACK_ the storage stream needs this!
192 1194 : Close();
193 : {
194 2388 : nsCacheServiceAutoLock lock;
195 1194 : mDescriptor->mOutput = nsnull;
196 : }
197 1194 : NS_RELEASE(mDescriptor);
198 4776 : }
199 :
200 : private:
201 : nsresult LazyInit();
202 2785 : nsresult EnsureInit() { return mInitialized ? NS_OK : LazyInit(); }
203 : nsresult OnWrite(PRUint32 count);
204 : };
205 : friend class nsOutputStreamWrapper;
206 :
207 : class nsCompressOutputStreamWrapper : public nsOutputStreamWrapper {
208 : private:
209 : unsigned char* mWriteBuffer;
210 : PRUint32 mWriteBufferLen;
211 : z_stream mZstream;
212 : PRBool mStreamInitialized;
213 : PRUint32 mUncompressedCount;
214 : public:
215 : NS_DECL_ISUPPORTS
216 :
217 0 : nsCompressOutputStreamWrapper(nsCacheEntryDescriptor * desc,
218 : PRUint32 off)
219 : : nsOutputStreamWrapper(desc, off)
220 : , mWriteBuffer(0)
221 : , mWriteBufferLen(0)
222 : , mStreamInitialized(PR_FALSE)
223 0 : , mUncompressedCount(0)
224 : {
225 0 : }
226 0 : virtual ~nsCompressOutputStreamWrapper()
227 0 : {
228 0 : Close();
229 0 : }
230 : NS_IMETHOD Write(const char* buf, PRUint32 count, PRUint32 * result);
231 : NS_IMETHOD Close();
232 : private:
233 : nsresult InitZstream();
234 : nsresult WriteBuffer();
235 : };
236 :
237 : private:
238 : /**
239 : * nsCacheEntryDescriptor data members
240 : */
241 : nsCacheEntry * mCacheEntry; // we are a child of the entry
242 : nsCacheAccessMode mAccessGranted;
243 : nsIOutputStream * mOutput;
244 : };
245 :
246 :
247 : #endif // _nsCacheEntryDescriptor_h_
|