Bug 783551 - Get tooltool running on the b2g on OS X builds. r=respindola
[gecko.git] / netwerk / cache / nsCacheEntry.h
blob654491b68c3acbfde8f4996f3d992e42d9f582ae
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/. */
7 #ifndef _nsCacheEntry_h_
8 #define _nsCacheEntry_h_
10 #include "nsICache.h"
11 #include "nsICacheEntryDescriptor.h"
12 #include "nsIThread.h"
13 #include "nsCacheMetaData.h"
15 #include "nspr.h"
16 #include "pldhash.h"
17 #include "nscore.h"
18 #include "nsCOMPtr.h"
19 #include "nsString.h"
20 #include "nsAString.h"
22 class nsCacheDevice;
23 class nsCacheMetaData;
24 class nsCacheRequest;
25 class nsCacheEntryDescriptor;
27 /******************************************************************************
28 * nsCacheEntry
29 *******************************************************************************/
30 class nsCacheEntry : public PRCList
32 public:
34 nsCacheEntry(const nsACString & key,
35 bool streamBased,
36 nsCacheStoragePolicy storagePolicy);
37 ~nsCacheEntry();
40 static nsresult Create( const char * key,
41 bool streamBased,
42 nsCacheStoragePolicy storagePolicy,
43 nsCacheDevice * device,
44 nsCacheEntry ** result);
46 nsCString * Key() { return &mKey; }
48 PRInt32 FetchCount() { return mFetchCount; }
49 void SetFetchCount( PRInt32 count) { mFetchCount = count; }
50 void Fetched();
52 PRUint32 LastFetched() { return mLastFetched; }
53 void SetLastFetched( PRUint32 lastFetched) { mLastFetched = lastFetched; }
55 PRUint32 LastModified() { return mLastModified; }
56 void SetLastModified( PRUint32 lastModified) { mLastModified = lastModified; }
58 PRUint32 ExpirationTime() { return mExpirationTime; }
59 void SetExpirationTime( PRUint32 expires) { mExpirationTime = expires; }
61 PRUint32 Size()
62 { return mDataSize + mMetaData.Size() + mKey.Length() ; }
64 nsCacheDevice * CacheDevice() { return mCacheDevice; }
65 void SetCacheDevice( nsCacheDevice * device) { mCacheDevice = device; }
66 void SetCustomCacheDevice( nsCacheDevice * device )
67 { mCustomDevice = device; }
68 nsCacheDevice * CustomCacheDevice() { return mCustomDevice; }
69 const char * GetDeviceID();
71 /**
72 * Data accessors
74 nsISupports *Data() { return mData; }
75 void SetData( nsISupports * data);
77 PRInt64 PredictedDataSize() { return mPredictedDataSize; }
78 void SetPredictedDataSize(PRInt64 size) { mPredictedDataSize = size; }
80 PRUint32 DataSize() { return mDataSize; }
81 void SetDataSize( PRUint32 size) { mDataSize = size; }
83 void TouchData();
85 /**
86 * Meta data accessors
88 const char * GetMetaDataElement( const char * key) { return mMetaData.GetElement(key); }
89 nsresult SetMetaDataElement( const char * key,
90 const char * value) { return mMetaData.SetElement(key, value); }
91 nsresult VisitMetaDataElements( nsICacheMetaDataVisitor * visitor) { return mMetaData.VisitElements(visitor); }
92 nsresult FlattenMetaData(char * buffer, PRUint32 bufSize) { return mMetaData.FlattenMetaData(buffer, bufSize); }
93 nsresult UnflattenMetaData(const char * buffer, PRUint32 bufSize) { return mMetaData.UnflattenMetaData(buffer, bufSize); }
94 PRUint32 MetaDataSize() { return mMetaData.Size(); }
96 void TouchMetaData();
99 /**
100 * Security Info accessors
102 nsISupports* SecurityInfo() { return mSecurityInfo; }
103 void SetSecurityInfo( nsISupports * info) { mSecurityInfo = info; }
106 // XXX enumerate MetaData method
109 enum CacheEntryFlags {
110 eStoragePolicyMask = 0x000000FF,
111 eDoomedMask = 0x00000100,
112 eEntryDirtyMask = 0x00000200,
113 eDataDirtyMask = 0x00000400,
114 eMetaDataDirtyMask = 0x00000800,
115 eStreamDataMask = 0x00001000,
116 eActiveMask = 0x00002000,
117 eInitializedMask = 0x00004000,
118 eValidMask = 0x00008000,
119 eBindingMask = 0x00010000,
120 ePrivateMask = 0x00020000
123 void MarkBinding() { mFlags |= eBindingMask; }
124 void ClearBinding() { mFlags &= ~eBindingMask; }
125 bool IsBinding() { return (mFlags & eBindingMask) != 0; }
127 void MarkEntryDirty() { mFlags |= eEntryDirtyMask; }
128 void MarkEntryClean() { mFlags &= ~eEntryDirtyMask; }
129 void MarkDataDirty() { mFlags |= eDataDirtyMask; }
130 void MarkDataClean() { mFlags &= ~eDataDirtyMask; }
131 void MarkMetaDataDirty() { mFlags |= eMetaDataDirtyMask; }
132 void MarkMetaDataClean() { mFlags &= ~eMetaDataDirtyMask; }
133 void MarkStreamData() { mFlags |= eStreamDataMask; }
134 void MarkValid() { mFlags |= eValidMask; }
135 void MarkInvalid() { mFlags &= ~eValidMask; }
136 void MarkPrivate() { mFlags |= ePrivateMask; }
137 void MarkPublic() { mFlags &= ~ePrivateMask; }
138 // void MarkAllowedInMemory() { mFlags |= eAllowedInMemoryMask; }
139 // void MarkAllowedOnDisk() { mFlags |= eAllowedOnDiskMask; }
141 bool IsDoomed() { return (mFlags & eDoomedMask) != 0; }
142 bool IsEntryDirty() { return (mFlags & eEntryDirtyMask) != 0; }
143 bool IsDataDirty() { return (mFlags & eDataDirtyMask) != 0; }
144 bool IsMetaDataDirty() { return (mFlags & eMetaDataDirtyMask) != 0; }
145 bool IsStreamData() { return (mFlags & eStreamDataMask) != 0; }
146 bool IsActive() { return (mFlags & eActiveMask) != 0; }
147 bool IsInitialized() { return (mFlags & eInitializedMask) != 0; }
148 bool IsValid() { return (mFlags & eValidMask) != 0; }
149 bool IsInvalid() { return (mFlags & eValidMask) == 0; }
150 bool IsInUse() { return IsBinding() ||
151 !(PR_CLIST_IS_EMPTY(&mRequestQ) &&
152 PR_CLIST_IS_EMPTY(&mDescriptorQ)); }
153 bool IsNotInUse() { return !IsInUse(); }
154 bool IsPrivate() { return (mFlags & ePrivateMask) != 0; }
157 bool IsAllowedInMemory()
159 return (StoragePolicy() == nsICache::STORE_ANYWHERE) ||
160 (StoragePolicy() == nsICache::STORE_IN_MEMORY);
163 bool IsAllowedOnDisk()
165 return !IsPrivate() && ((StoragePolicy() == nsICache::STORE_ANYWHERE) ||
166 (StoragePolicy() == nsICache::STORE_ON_DISK) ||
167 (StoragePolicy() == nsICache::STORE_ON_DISK_AS_FILE));
170 bool IsAllowedOffline()
172 return (StoragePolicy() == nsICache::STORE_OFFLINE);
175 nsCacheStoragePolicy StoragePolicy()
177 return (nsCacheStoragePolicy)(mFlags & eStoragePolicyMask);
180 void SetStoragePolicy(nsCacheStoragePolicy policy)
182 NS_ASSERTION(policy <= 0xFF, "too many bits in nsCacheStoragePolicy");
183 mFlags &= ~eStoragePolicyMask; // clear storage policy bits
184 mFlags |= policy;
188 // methods for nsCacheService
189 nsresult RequestAccess( nsCacheRequest * request, nsCacheAccessMode *accessGranted);
190 nsresult CreateDescriptor( nsCacheRequest * request,
191 nsCacheAccessMode accessGranted,
192 nsICacheEntryDescriptor ** result);
194 // nsresult Open(nsCacheRequest *request, nsICacheEntryDescriptor ** result);
195 // nsresult AsyncOpen(nsCacheRequest *request);
196 bool RemoveRequest( nsCacheRequest * request);
197 bool RemoveDescriptor( nsCacheEntryDescriptor * descriptor);
199 private:
200 friend class nsCacheEntryHashTable;
201 friend class nsCacheService;
203 void DetachDescriptors(void);
205 // internal methods
206 void MarkDoomed() { mFlags |= eDoomedMask; }
207 void MarkStreamBased() { mFlags |= eStreamDataMask; }
208 void MarkInitialized() { mFlags |= eInitializedMask; }
209 void MarkActive() { mFlags |= eActiveMask; }
210 void MarkInactive() { mFlags &= ~eActiveMask; }
212 nsCString mKey;
213 PRUint32 mFetchCount; // 4
214 PRUint32 mLastFetched; // 4
215 PRUint32 mLastModified; // 4
216 PRUint32 mLastValidated; // 4
217 PRUint32 mExpirationTime; // 4
218 PRUint32 mFlags; // 4
219 PRInt64 mPredictedDataSize; // Size given by ContentLength.
220 PRUint32 mDataSize; // 4
221 nsCacheDevice * mCacheDevice; // 4
222 nsCacheDevice * mCustomDevice; // 4
223 nsCOMPtr<nsISupports> mSecurityInfo; //
224 nsISupports * mData; // strong ref
225 nsCOMPtr<nsIThread> mThread;
226 nsCacheMetaData mMetaData; // 4
227 PRCList mRequestQ; // 8
228 PRCList mDescriptorQ; // 8
232 /******************************************************************************
233 * nsCacheEntryInfo
234 *******************************************************************************/
235 class nsCacheEntryInfo : public nsICacheEntryInfo {
236 public:
237 NS_DECL_ISUPPORTS
238 NS_DECL_NSICACHEENTRYINFO
240 nsCacheEntryInfo(nsCacheEntry* entry)
241 : mCacheEntry(entry)
245 virtual ~nsCacheEntryInfo() {}
246 void DetachEntry() { mCacheEntry = nullptr; }
248 private:
249 nsCacheEntry * mCacheEntry;
253 /******************************************************************************
254 * nsCacheEntryHashTable
255 *******************************************************************************/
256 typedef struct {
257 PLDHashNumber keyHash;
258 nsCacheEntry *cacheEntry;
259 } nsCacheEntryHashTableEntry;
262 class nsCacheEntryHashTable
264 public:
265 nsCacheEntryHashTable();
266 ~nsCacheEntryHashTable();
268 nsresult Init();
269 void Shutdown();
271 nsCacheEntry *GetEntry( const nsCString * key);
272 nsresult AddEntry( nsCacheEntry *entry);
273 void RemoveEntry( nsCacheEntry *entry);
275 void VisitEntries( PLDHashEnumerator etor, void *arg);
277 private:
278 // PLDHashTable operation callbacks
279 static PLDHashNumber HashKey( PLDHashTable *table, const void *key);
281 static bool MatchEntry( PLDHashTable * table,
282 const PLDHashEntryHdr * entry,
283 const void * key);
285 static void MoveEntry( PLDHashTable *table,
286 const PLDHashEntryHdr *from,
287 PLDHashEntryHdr *to);
289 static void ClearEntry( PLDHashTable *table, PLDHashEntryHdr *entry);
291 static void Finalize( PLDHashTable *table);
293 static
294 PLDHashOperator FreeCacheEntries(PLDHashTable * table,
295 PLDHashEntryHdr * hdr,
296 PRUint32 number,
297 void * arg);
298 static
299 PLDHashOperator VisitEntry(PLDHashTable * table,
300 PLDHashEntryHdr * hdr,
301 PRUint32 number,
302 void * arg);
304 // member variables
305 static PLDHashTableOps ops;
306 PLDHashTable table;
307 bool initialized;
310 #endif // _nsCacheEntry_h_