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 nsCacheEntry.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 <gordon@netscape.com>
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 : #ifndef _nsCacheEntry_h_
42 : #define _nsCacheEntry_h_
43 :
44 : #include "nsICache.h"
45 : #include "nsICacheEntryDescriptor.h"
46 : #include "nsIThread.h"
47 : #include "nsCacheMetaData.h"
48 :
49 : #include "nspr.h"
50 : #include "pldhash.h"
51 : #include "nscore.h"
52 : #include "nsCOMPtr.h"
53 : #include "nsString.h"
54 : #include "nsAString.h"
55 :
56 : class nsCacheDevice;
57 : class nsCacheMetaData;
58 : class nsCacheRequest;
59 : class nsCacheEntryDescriptor;
60 :
61 : /******************************************************************************
62 : * nsCacheEntry
63 : *******************************************************************************/
64 : class nsCacheEntry : public PRCList
65 : {
66 : public:
67 :
68 : nsCacheEntry(nsCString * key,
69 : bool streamBased,
70 : nsCacheStoragePolicy storagePolicy);
71 : ~nsCacheEntry();
72 :
73 :
74 : static nsresult Create( const char * key,
75 : bool streamBased,
76 : nsCacheStoragePolicy storagePolicy,
77 : nsCacheDevice * device,
78 : nsCacheEntry ** result);
79 :
80 3002 : nsCString * Key() { return mKey; }
81 :
82 7100 : PRInt32 FetchCount() { return mFetchCount; }
83 285 : void SetFetchCount( PRInt32 count) { mFetchCount = count; }
84 : void Fetched();
85 :
86 716 : PRUint32 LastFetched() { return mLastFetched; }
87 285 : void SetLastFetched( PRUint32 lastFetched) { mLastFetched = lastFetched; }
88 :
89 1283 : PRUint32 LastModified() { return mLastModified; }
90 285 : void SetLastModified( PRUint32 lastModified) { mLastModified = lastModified; }
91 :
92 7837 : PRUint32 ExpirationTime() { return mExpirationTime; }
93 1642 : void SetExpirationTime( PRUint32 expires) { mExpirationTime = expires; }
94 :
95 7472 : PRUint32 Size()
96 7472 : { return mDataSize + mMetaData.Size() + (mKey ? mKey->Length() : 0); }
97 :
98 8227 : nsCacheDevice * CacheDevice() { return mCacheDevice; }
99 1376 : void SetCacheDevice( nsCacheDevice * device) { mCacheDevice = device; }
100 : const char * GetDeviceID();
101 :
102 : /**
103 : * Data accessors
104 : */
105 4546 : nsISupports *Data() { return mData; }
106 : void SetData( nsISupports * data);
107 :
108 851 : PRInt64 PredictedDataSize() { return mPredictedDataSize; }
109 1363 : void SetPredictedDataSize(PRInt64 size) { mPredictedDataSize = size; }
110 :
111 11837 : PRUint32 DataSize() { return mDataSize; }
112 2913 : void SetDataSize( PRUint32 size) { mDataSize = size; }
113 :
114 : void TouchData();
115 :
116 : /**
117 : * Meta data accessors
118 : */
119 3646 : const char * GetMetaDataElement( const char * key) { return mMetaData.GetElement(key); }
120 4848 : nsresult SetMetaDataElement( const char * key,
121 4848 : const char * value) { return mMetaData.SetElement(key, value); }
122 0 : nsresult VisitMetaDataElements( nsICacheMetaDataVisitor * visitor) { return mMetaData.VisitElements(visitor); }
123 691 : nsresult FlattenMetaData(char * buffer, PRUint32 bufSize) { return mMetaData.FlattenMetaData(buffer, bufSize); }
124 285 : nsresult UnflattenMetaData(const char * buffer, PRUint32 bufSize) { return mMetaData.UnflattenMetaData(buffer, bufSize); }
125 2553 : PRUint32 MetaDataSize() { return mMetaData.Size(); }
126 :
127 : void TouchMetaData();
128 :
129 :
130 : /**
131 : * Security Info accessors
132 : */
133 888 : nsISupports* SecurityInfo() { return mSecurityInfo; }
134 4 : void SetSecurityInfo( nsISupports * info) { mSecurityInfo = info; }
135 :
136 :
137 : // XXX enumerate MetaData method
138 :
139 :
140 : enum CacheEntryFlags {
141 : eStoragePolicyMask = 0x000000FF,
142 : eDoomedMask = 0x00000100,
143 : eEntryDirtyMask = 0x00000200,
144 : eDataDirtyMask = 0x00000400,
145 : eMetaDataDirtyMask = 0x00000800,
146 : eStreamDataMask = 0x00001000,
147 : eActiveMask = 0x00002000,
148 : eInitializedMask = 0x00004000,
149 : eValidMask = 0x00008000,
150 : eBindingMask = 0x00010000
151 : };
152 :
153 812 : void MarkBinding() { mFlags |= eBindingMask; }
154 812 : void ClearBinding() { mFlags &= ~eBindingMask; }
155 2770 : bool IsBinding() { return (mFlags & eBindingMask) != 0; }
156 :
157 4236 : void MarkEntryDirty() { mFlags |= eEntryDirtyMask; }
158 : void MarkEntryClean() { mFlags &= ~eEntryDirtyMask; }
159 1496 : void MarkDataDirty() { mFlags |= eDataDirtyMask; }
160 : void MarkDataClean() { mFlags &= ~eDataDirtyMask; }
161 3941 : void MarkMetaDataDirty() { mFlags |= eMetaDataDirtyMask; }
162 : void MarkMetaDataClean() { mFlags &= ~eMetaDataDirtyMask; }
163 : void MarkStreamData() { mFlags |= eStreamDataMask; }
164 286 : void MarkValid() { mFlags |= eValidMask; }
165 594 : void MarkInvalid() { mFlags &= ~eValidMask; }
166 : // void MarkAllowedInMemory() { mFlags |= eAllowedInMemoryMask; }
167 : // void MarkAllowedOnDisk() { mFlags |= eAllowedOnDiskMask; }
168 :
169 11336 : bool IsDoomed() { return (mFlags & eDoomedMask) != 0; }
170 : bool IsEntryDirty() { return (mFlags & eEntryDirtyMask) != 0; }
171 : bool IsDataDirty() { return (mFlags & eDataDirtyMask) != 0; }
172 : bool IsMetaDataDirty() { return (mFlags & eMetaDataDirtyMask) != 0; }
173 5330 : bool IsStreamData() { return (mFlags & eStreamDataMask) != 0; }
174 5466 : bool IsActive() { return (mFlags & eActiveMask) != 0; }
175 2890 : bool IsInitialized() { return (mFlags & eInitializedMask) != 0; }
176 3186 : bool IsValid() { return (mFlags & eValidMask) != 0; }
177 294 : bool IsInvalid() { return (mFlags & eValidMask) == 0; }
178 2282 : bool IsInUse() { return IsBinding() ||
179 : !(PR_CLIST_IS_EMPTY(&mRequestQ) &&
180 2282 : PR_CLIST_IS_EMPTY(&mDescriptorQ)); }
181 2080 : bool IsNotInUse() { return !IsInUse(); }
182 :
183 :
184 262 : bool IsAllowedInMemory()
185 : {
186 262 : return (StoragePolicy() == nsICache::STORE_ANYWHERE) ||
187 262 : (StoragePolicy() == nsICache::STORE_IN_MEMORY);
188 : }
189 :
190 814 : bool IsAllowedOnDisk()
191 : {
192 814 : return (StoragePolicy() == nsICache::STORE_ANYWHERE) ||
193 36 : (StoragePolicy() == nsICache::STORE_ON_DISK) ||
194 850 : (StoragePolicy() == nsICache::STORE_ON_DISK_AS_FILE);
195 : }
196 :
197 25 : bool IsAllowedOffline()
198 : {
199 25 : return (StoragePolicy() == nsICache::STORE_OFFLINE);
200 : }
201 :
202 4519 : nsCacheStoragePolicy StoragePolicy()
203 : {
204 4519 : return (nsCacheStoragePolicy)(mFlags & eStoragePolicyMask);
205 : }
206 :
207 1390 : void SetStoragePolicy(nsCacheStoragePolicy policy)
208 : {
209 1390 : NS_ASSERTION(policy <= 0xFF, "too many bits in nsCacheStoragePolicy");
210 1390 : mFlags &= ~eStoragePolicyMask; // clear storage policy bits
211 1390 : mFlags |= policy;
212 1390 : }
213 :
214 :
215 : // methods for nsCacheService
216 : nsresult RequestAccess( nsCacheRequest * request, nsCacheAccessMode *accessGranted);
217 : nsresult CreateDescriptor( nsCacheRequest * request,
218 : nsCacheAccessMode accessGranted,
219 : nsICacheEntryDescriptor ** result);
220 :
221 : // nsresult Open(nsCacheRequest *request, nsICacheEntryDescriptor ** result);
222 : // nsresult AsyncOpen(nsCacheRequest *request);
223 : bool RemoveRequest( nsCacheRequest * request);
224 : bool RemoveDescriptor( nsCacheEntryDescriptor * descriptor);
225 :
226 : private:
227 : friend class nsCacheEntryHashTable;
228 : friend class nsCacheService;
229 :
230 : void DetachDescriptors(void);
231 :
232 : // internal methods
233 488 : void MarkDoomed() { mFlags |= eDoomedMask; }
234 2485 : void MarkStreamBased() { mFlags |= eStreamDataMask; }
235 1592 : void MarkInitialized() { mFlags |= eInitializedMask; }
236 1474 : void MarkActive() { mFlags |= eActiveMask; }
237 1474 : void MarkInactive() { mFlags &= ~eActiveMask; }
238 :
239 : nsCString * mKey; // 4 // XXX ask scc about const'ness
240 : PRUint32 mFetchCount; // 4
241 : PRUint32 mLastFetched; // 4
242 : PRUint32 mLastModified; // 4
243 : PRUint32 mLastValidated; // 4
244 : PRUint32 mExpirationTime; // 4
245 : PRUint32 mFlags; // 4
246 : PRInt64 mPredictedDataSize; // Size given by ContentLength.
247 : PRUint32 mDataSize; // 4
248 : nsCacheDevice * mCacheDevice; // 4
249 : nsCOMPtr<nsISupports> mSecurityInfo; //
250 : nsISupports * mData; // strong ref
251 : nsCOMPtr<nsIThread> mThread;
252 : nsCacheMetaData mMetaData; // 4
253 : PRCList mRequestQ; // 8
254 : PRCList mDescriptorQ; // 8
255 : };
256 :
257 :
258 : /******************************************************************************
259 : * nsCacheEntryInfo
260 : *******************************************************************************/
261 : class nsCacheEntryInfo : public nsICacheEntryInfo {
262 : public:
263 : NS_DECL_ISUPPORTS
264 : NS_DECL_NSICACHEENTRYINFO
265 :
266 0 : nsCacheEntryInfo(nsCacheEntry* entry)
267 0 : : mCacheEntry(entry)
268 : {
269 0 : }
270 :
271 0 : virtual ~nsCacheEntryInfo() {}
272 0 : void DetachEntry() { mCacheEntry = nsnull; }
273 :
274 : private:
275 : nsCacheEntry * mCacheEntry;
276 : };
277 :
278 :
279 : /******************************************************************************
280 : * nsCacheEntryHashTable
281 : *******************************************************************************/
282 : typedef struct {
283 : PLDHashNumber keyHash;
284 : nsCacheEntry *cacheEntry;
285 : } nsCacheEntryHashTableEntry;
286 :
287 :
288 : class nsCacheEntryHashTable
289 : {
290 : public:
291 : nsCacheEntryHashTable();
292 : ~nsCacheEntryHashTable();
293 :
294 : nsresult Init();
295 : void Shutdown();
296 :
297 : nsCacheEntry *GetEntry( const nsCString * key);
298 : nsresult AddEntry( nsCacheEntry *entry);
299 : void RemoveEntry( nsCacheEntry *entry);
300 :
301 : void VisitEntries( PLDHashEnumerator etor, void *arg);
302 :
303 : private:
304 : // PLDHashTable operation callbacks
305 : static PLDHashNumber HashKey( PLDHashTable *table, const void *key);
306 :
307 : static bool MatchEntry( PLDHashTable * table,
308 : const PLDHashEntryHdr * entry,
309 : const void * key);
310 :
311 : static void MoveEntry( PLDHashTable *table,
312 : const PLDHashEntryHdr *from,
313 : PLDHashEntryHdr *to);
314 :
315 : static void ClearEntry( PLDHashTable *table, PLDHashEntryHdr *entry);
316 :
317 : static void Finalize( PLDHashTable *table);
318 :
319 : static
320 : PLDHashOperator FreeCacheEntries(PLDHashTable * table,
321 : PLDHashEntryHdr * hdr,
322 : PRUint32 number,
323 : void * arg);
324 : static
325 : PLDHashOperator VisitEntry(PLDHashTable * table,
326 : PLDHashEntryHdr * hdr,
327 : PRUint32 number,
328 : void * arg);
329 :
330 : // member variables
331 : static PLDHashTableOps ops;
332 : PLDHashTable table;
333 : bool initialized;
334 : };
335 :
336 : #endif // _nsCacheEntry_h_
|