1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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
16 * The Original Code is nsCacheEntry.h, released
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.
25 * Gordon Sheridan <gordon@netscape.com>
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.
39 * ***** END LICENSE BLOCK ***** */
41 #ifndef _nsCacheEntry_h_
42 #define _nsCacheEntry_h_
45 #include "nsICacheEntryDescriptor.h"
46 #include "nsIThread.h"
47 #include "nsCacheMetaData.h"
54 #include "nsAString.h"
57 class nsCacheMetaData
;
59 class nsCacheEntryDescriptor
;
61 #define NO_EXPIRATION_TIME 0xFFFFFFFF
63 /******************************************************************************
65 *******************************************************************************/
66 class nsCacheEntry
: public PRCList
70 nsCacheEntry(nsCString
* key
,
72 nsCacheStoragePolicy storagePolicy
);
76 static nsresult
Create( const char * key
,
78 nsCacheStoragePolicy storagePolicy
,
79 nsCacheDevice
* device
,
80 nsCacheEntry
** result
);
82 nsCString
* Key() { return mKey
; }
84 PRInt32
FetchCount() { return mFetchCount
; }
85 void SetFetchCount( PRInt32 count
) { mFetchCount
= count
; }
88 PRUint32
LastFetched() { return mLastFetched
; }
89 void SetLastFetched( PRUint32 lastFetched
) { mLastFetched
= lastFetched
; }
91 PRUint32
LastModified() { return mLastModified
; }
92 void SetLastModified( PRUint32 lastModified
) { mLastModified
= lastModified
; }
94 PRUint32
ExpirationTime() { return mExpirationTime
; }
95 void SetExpirationTime( PRUint32 expires
) { mExpirationTime
= expires
; }
97 PRUint32
Size() { return mDataSize
+ mMetaData
.Size(); }
99 nsCacheDevice
* CacheDevice() { return mCacheDevice
; }
100 void SetCacheDevice( nsCacheDevice
* device
) { mCacheDevice
= device
; }
101 const char * GetDeviceID();
106 nsISupports
*Data() { return mData
; }
107 void SetData( nsISupports
* data
);
109 PRUint32
DataSize() { return mDataSize
; }
110 void SetDataSize( PRUint32 size
) { mDataSize
= size
; }
115 * Meta data accessors
117 const char * GetMetaDataElement( const char * key
) { return mMetaData
.GetElement(key
); }
118 nsresult
SetMetaDataElement( const char * key
,
119 const char * value
) { return mMetaData
.SetElement(key
, value
); }
120 nsresult
VisitMetaDataElements( nsICacheMetaDataVisitor
* visitor
) { return mMetaData
.VisitElements(visitor
); }
121 nsresult
FlattenMetaData(char * buffer
, PRUint32 bufSize
) { return mMetaData
.FlattenMetaData(buffer
, bufSize
); }
122 nsresult
UnflattenMetaData(const char * buffer
, PRUint32 bufSize
) { return mMetaData
.UnflattenMetaData(buffer
, bufSize
); }
123 PRUint32
MetaDataSize() { return mMetaData
.Size(); }
125 void TouchMetaData();
129 * Security Info accessors
131 nsISupports
* SecurityInfo() { return mSecurityInfo
; }
132 void SetSecurityInfo( nsISupports
* info
) { mSecurityInfo
= info
; }
135 // XXX enumerate MetaData method
138 enum CacheEntryFlags
{
139 eStoragePolicyMask
= 0x000000FF,
140 eDoomedMask
= 0x00000100,
141 eEntryDirtyMask
= 0x00000200,
142 eDataDirtyMask
= 0x00000400,
143 eMetaDataDirtyMask
= 0x00000800,
144 eStreamDataMask
= 0x00001000,
145 eActiveMask
= 0x00002000,
146 eInitializedMask
= 0x00004000,
147 eValidMask
= 0x00008000,
148 eBindingMask
= 0x00010000
151 void MarkBinding() { mFlags
|= eBindingMask
; }
152 void ClearBinding() { mFlags
&= ~eBindingMask
; }
153 PRBool
IsBinding() { return (mFlags
& eBindingMask
) != 0; }
155 void MarkEntryDirty() { mFlags
|= eEntryDirtyMask
; }
156 void MarkEntryClean() { mFlags
&= ~eEntryDirtyMask
; }
157 void MarkDataDirty() { mFlags
|= eDataDirtyMask
; }
158 void MarkDataClean() { mFlags
&= ~eDataDirtyMask
; }
159 void MarkMetaDataDirty() { mFlags
|= eMetaDataDirtyMask
; }
160 void MarkMetaDataClean() { mFlags
&= ~eMetaDataDirtyMask
; }
161 void MarkStreamData() { mFlags
|= eStreamDataMask
; }
162 void MarkValid() { mFlags
|= eValidMask
; }
163 void MarkInvalid() { mFlags
&= ~eValidMask
; }
164 // void MarkAllowedInMemory() { mFlags |= eAllowedInMemoryMask; }
165 // void MarkAllowedOnDisk() { mFlags |= eAllowedOnDiskMask; }
167 PRBool
IsDoomed() { return (mFlags
& eDoomedMask
) != 0; }
168 PRBool
IsEntryDirty() { return (mFlags
& eEntryDirtyMask
) != 0; }
169 PRBool
IsDataDirty() { return (mFlags
& eDataDirtyMask
) != 0; }
170 PRBool
IsMetaDataDirty() { return (mFlags
& eMetaDataDirtyMask
) != 0; }
171 PRBool
IsStreamData() { return (mFlags
& eStreamDataMask
) != 0; }
172 PRBool
IsActive() { return (mFlags
& eActiveMask
) != 0; }
173 PRBool
IsInitialized() { return (mFlags
& eInitializedMask
) != 0; }
174 PRBool
IsValid() { return (mFlags
& eValidMask
) != 0; }
175 PRBool
IsInvalid() { return (mFlags
& eValidMask
) == 0; }
176 PRBool
IsInUse() { return IsBinding() ||
177 !(PR_CLIST_IS_EMPTY(&mRequestQ
) &&
178 PR_CLIST_IS_EMPTY(&mDescriptorQ
)); }
179 PRBool
IsNotInUse() { return !IsInUse(); }
182 PRBool
IsAllowedInMemory()
184 return (StoragePolicy() == nsICache::STORE_ANYWHERE
) ||
185 (StoragePolicy() == nsICache::STORE_IN_MEMORY
);
188 PRBool
IsAllowedOnDisk()
190 return (StoragePolicy() == nsICache::STORE_ANYWHERE
) ||
191 (StoragePolicy() == nsICache::STORE_ON_DISK
) ||
192 (StoragePolicy() == nsICache::STORE_ON_DISK_AS_FILE
);
195 PRBool
IsAllowedOffline()
197 return (StoragePolicy() == nsICache::STORE_OFFLINE
);
200 nsCacheStoragePolicy
StoragePolicy()
202 return (nsCacheStoragePolicy
)(mFlags
& eStoragePolicyMask
);
205 void SetStoragePolicy(nsCacheStoragePolicy policy
)
207 NS_ASSERTION(policy
<= 0xFF, "too many bits in nsCacheStoragePolicy");
208 mFlags
&= ~eStoragePolicyMask
; // clear storage policy bits
213 // methods for nsCacheService
214 nsresult
RequestAccess( nsCacheRequest
* request
, nsCacheAccessMode
*accessGranted
);
215 nsresult
CreateDescriptor( nsCacheRequest
* request
,
216 nsCacheAccessMode accessGranted
,
217 nsICacheEntryDescriptor
** result
);
219 // nsresult Open(nsCacheRequest *request, nsICacheEntryDescriptor ** result);
220 // nsresult AsyncOpen(nsCacheRequest *request);
221 PRBool
RemoveRequest( nsCacheRequest
* request
);
222 PRBool
RemoveDescriptor( nsCacheEntryDescriptor
* descriptor
);
225 friend class nsCacheEntryHashTable
;
226 friend class nsCacheService
;
228 void DetachDescriptors(void);
231 void MarkDoomed() { mFlags
|= eDoomedMask
; }
232 void MarkStreamBased() { mFlags
|= eStreamDataMask
; }
233 void MarkInitialized() { mFlags
|= eInitializedMask
; }
234 void MarkActive() { mFlags
|= eActiveMask
; }
235 void MarkInactive() { mFlags
&= ~eActiveMask
; }
237 nsCString
* mKey
; // 4 // XXX ask scc about const'ness
238 PRUint32 mFetchCount
; // 4
239 PRUint32 mLastFetched
; // 4
240 PRUint32 mLastModified
; // 4
241 PRUint32 mLastValidated
; // 4
242 PRUint32 mExpirationTime
; // 4
243 PRUint32 mFlags
; // 4
244 PRUint32 mDataSize
; // 4
245 nsCacheDevice
* mCacheDevice
; // 4
246 nsCOMPtr
<nsISupports
> mSecurityInfo
; //
247 nsISupports
* mData
; // strong ref
248 nsCOMPtr
<nsIThread
> mThread
;
249 nsCacheMetaData mMetaData
; // 4
250 PRCList mRequestQ
; // 8
251 PRCList mDescriptorQ
; // 8
255 /******************************************************************************
257 *******************************************************************************/
258 class nsCacheEntryInfo
: public nsICacheEntryInfo
{
261 NS_DECL_NSICACHEENTRYINFO
263 nsCacheEntryInfo(nsCacheEntry
* entry
)
268 virtual ~nsCacheEntryInfo() {}
269 void DetachEntry() { mCacheEntry
= nsnull
; }
272 nsCacheEntry
* mCacheEntry
;
276 /******************************************************************************
277 * nsCacheEntryHashTable
278 *******************************************************************************/
280 PLDHashNumber keyHash
;
281 nsCacheEntry
*cacheEntry
;
282 } nsCacheEntryHashTableEntry
;
285 class nsCacheEntryHashTable
288 nsCacheEntryHashTable();
289 ~nsCacheEntryHashTable();
294 nsCacheEntry
*GetEntry( const nsCString
* key
);
295 nsresult
AddEntry( nsCacheEntry
*entry
);
296 void RemoveEntry( nsCacheEntry
*entry
);
298 void VisitEntries( PLDHashEnumerator etor
, void *arg
);
301 // PLDHashTable operation callbacks
302 static PLDHashNumber
HashKey( PLDHashTable
*table
, const void *key
);
304 static PRBool
MatchEntry( PLDHashTable
* table
,
305 const PLDHashEntryHdr
* entry
,
308 static void MoveEntry( PLDHashTable
*table
,
309 const PLDHashEntryHdr
*from
,
310 PLDHashEntryHdr
*to
);
312 static void ClearEntry( PLDHashTable
*table
, PLDHashEntryHdr
*entry
);
314 static void Finalize( PLDHashTable
*table
);
317 PLDHashOperator
FreeCacheEntries(PLDHashTable
* table
,
318 PLDHashEntryHdr
* hdr
,
322 PLDHashOperator
VisitEntry(PLDHashTable
* table
,
323 PLDHashEntryHdr
* hdr
,
328 static PLDHashTableOps ops
;
333 #endif // _nsCacheEntry_h_