nsCacheEntry.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2. *
  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/. */
  6. #ifndef _nsCacheEntry_h_
  7. #define _nsCacheEntry_h_
  8. #include "nsICache.h"
  9. #include "nsICacheEntryDescriptor.h"
  10. #include "nsIThread.h"
  11. #include "nsCacheMetaData.h"
  12. #include "nspr.h"
  13. #include "PLDHashTable.h"
  14. #include "nsAutoPtr.h"
  15. #include "nscore.h"
  16. #include "nsCOMPtr.h"
  17. #include "nsString.h"
  18. #include "nsAString.h"
  19. class nsCacheDevice;
  20. class nsCacheMetaData;
  21. class nsCacheRequest;
  22. class nsCacheEntryDescriptor;
  23. /******************************************************************************
  24. * nsCacheEntry
  25. *******************************************************************************/
  26. class nsCacheEntry : public PRCList
  27. {
  28. public:
  29. nsCacheEntry(const nsACString & key,
  30. bool streamBased,
  31. nsCacheStoragePolicy storagePolicy);
  32. ~nsCacheEntry();
  33. static nsresult Create( const char * key,
  34. bool streamBased,
  35. nsCacheStoragePolicy storagePolicy,
  36. nsCacheDevice * device,
  37. nsCacheEntry ** result);
  38. nsCString * Key() { return &mKey; }
  39. int32_t FetchCount() { return mFetchCount; }
  40. void SetFetchCount( int32_t count) { mFetchCount = count; }
  41. void Fetched();
  42. uint32_t LastFetched() { return mLastFetched; }
  43. void SetLastFetched( uint32_t lastFetched) { mLastFetched = lastFetched; }
  44. uint32_t LastModified() { return mLastModified; }
  45. void SetLastModified( uint32_t lastModified) { mLastModified = lastModified; }
  46. uint32_t ExpirationTime() { return mExpirationTime; }
  47. void SetExpirationTime( uint32_t expires) { mExpirationTime = expires; }
  48. uint32_t Size()
  49. { return mDataSize + mMetaData.Size() + mKey.Length() ; }
  50. nsCacheDevice * CacheDevice() { return mCacheDevice; }
  51. void SetCacheDevice( nsCacheDevice * device) { mCacheDevice = device; }
  52. void SetCustomCacheDevice( nsCacheDevice * device )
  53. { mCustomDevice = device; }
  54. nsCacheDevice * CustomCacheDevice() { return mCustomDevice; }
  55. const char * GetDeviceID();
  56. /**
  57. * Data accessors
  58. */
  59. nsISupports *Data() { return mData; }
  60. void SetData( nsISupports * data);
  61. int64_t PredictedDataSize() { return mPredictedDataSize; }
  62. void SetPredictedDataSize(int64_t size) { mPredictedDataSize = size; }
  63. uint32_t DataSize() { return mDataSize; }
  64. void SetDataSize( uint32_t size) { mDataSize = size; }
  65. void TouchData();
  66. /**
  67. * Meta data accessors
  68. */
  69. const char * GetMetaDataElement( const char * key) { return mMetaData.GetElement(key); }
  70. nsresult SetMetaDataElement( const char * key,
  71. const char * value) { return mMetaData.SetElement(key, value); }
  72. nsresult VisitMetaDataElements( nsICacheMetaDataVisitor * visitor) { return mMetaData.VisitElements(visitor); }
  73. nsresult FlattenMetaData(char * buffer, uint32_t bufSize) { return mMetaData.FlattenMetaData(buffer, bufSize); }
  74. nsresult UnflattenMetaData(const char * buffer, uint32_t bufSize) { return mMetaData.UnflattenMetaData(buffer, bufSize); }
  75. uint32_t MetaDataSize() { return mMetaData.Size(); }
  76. void TouchMetaData();
  77. /**
  78. * Security Info accessors
  79. */
  80. nsISupports* SecurityInfo() { return mSecurityInfo; }
  81. void SetSecurityInfo( nsISupports * info) { mSecurityInfo = info; }
  82. // XXX enumerate MetaData method
  83. enum CacheEntryFlags {
  84. eStoragePolicyMask = 0x000000FF,
  85. eDoomedMask = 0x00000100,
  86. eEntryDirtyMask = 0x00000200,
  87. eDataDirtyMask = 0x00000400,
  88. eMetaDataDirtyMask = 0x00000800,
  89. eStreamDataMask = 0x00001000,
  90. eActiveMask = 0x00002000,
  91. eInitializedMask = 0x00004000,
  92. eValidMask = 0x00008000,
  93. eBindingMask = 0x00010000,
  94. ePrivateMask = 0x00020000
  95. };
  96. void MarkBinding() { mFlags |= eBindingMask; }
  97. void ClearBinding() { mFlags &= ~eBindingMask; }
  98. bool IsBinding() { return (mFlags & eBindingMask) != 0; }
  99. void MarkEntryDirty() { mFlags |= eEntryDirtyMask; }
  100. void MarkEntryClean() { mFlags &= ~eEntryDirtyMask; }
  101. void MarkDataDirty() { mFlags |= eDataDirtyMask; }
  102. void MarkDataClean() { mFlags &= ~eDataDirtyMask; }
  103. void MarkMetaDataDirty() { mFlags |= eMetaDataDirtyMask; }
  104. void MarkMetaDataClean() { mFlags &= ~eMetaDataDirtyMask; }
  105. void MarkStreamData() { mFlags |= eStreamDataMask; }
  106. void MarkValid() { mFlags |= eValidMask; }
  107. void MarkInvalid() { mFlags &= ~eValidMask; }
  108. void MarkPrivate() { mFlags |= ePrivateMask; }
  109. void MarkPublic() { mFlags &= ~ePrivateMask; }
  110. // void MarkAllowedInMemory() { mFlags |= eAllowedInMemoryMask; }
  111. // void MarkAllowedOnDisk() { mFlags |= eAllowedOnDiskMask; }
  112. bool IsDoomed() { return (mFlags & eDoomedMask) != 0; }
  113. bool IsEntryDirty() { return (mFlags & eEntryDirtyMask) != 0; }
  114. bool IsDataDirty() { return (mFlags & eDataDirtyMask) != 0; }
  115. bool IsMetaDataDirty() { return (mFlags & eMetaDataDirtyMask) != 0; }
  116. bool IsStreamData() { return (mFlags & eStreamDataMask) != 0; }
  117. bool IsActive() { return (mFlags & eActiveMask) != 0; }
  118. bool IsInitialized() { return (mFlags & eInitializedMask) != 0; }
  119. bool IsValid() { return (mFlags & eValidMask) != 0; }
  120. bool IsInvalid() { return (mFlags & eValidMask) == 0; }
  121. bool IsInUse() { return IsBinding() ||
  122. !(PR_CLIST_IS_EMPTY(&mRequestQ) &&
  123. PR_CLIST_IS_EMPTY(&mDescriptorQ)); }
  124. bool IsNotInUse() { return !IsInUse(); }
  125. bool IsPrivate() { return (mFlags & ePrivateMask) != 0; }
  126. bool IsAllowedInMemory()
  127. {
  128. return (StoragePolicy() == nsICache::STORE_ANYWHERE) ||
  129. (StoragePolicy() == nsICache::STORE_IN_MEMORY);
  130. }
  131. bool IsAllowedOnDisk()
  132. {
  133. return !IsPrivate() && ((StoragePolicy() == nsICache::STORE_ANYWHERE) ||
  134. (StoragePolicy() == nsICache::STORE_ON_DISK));
  135. }
  136. bool IsAllowedOffline()
  137. {
  138. return (StoragePolicy() == nsICache::STORE_OFFLINE);
  139. }
  140. nsCacheStoragePolicy StoragePolicy()
  141. {
  142. return (nsCacheStoragePolicy)(mFlags & eStoragePolicyMask);
  143. }
  144. void SetStoragePolicy(nsCacheStoragePolicy policy)
  145. {
  146. NS_ASSERTION(policy <= 0xFF, "too many bits in nsCacheStoragePolicy");
  147. mFlags &= ~eStoragePolicyMask; // clear storage policy bits
  148. mFlags |= policy;
  149. }
  150. // methods for nsCacheService
  151. nsresult RequestAccess( nsCacheRequest * request, nsCacheAccessMode *accessGranted);
  152. nsresult CreateDescriptor( nsCacheRequest * request,
  153. nsCacheAccessMode accessGranted,
  154. nsICacheEntryDescriptor ** result);
  155. bool RemoveRequest( nsCacheRequest * request);
  156. bool RemoveDescriptor( nsCacheEntryDescriptor * descriptor,
  157. bool * doomEntry);
  158. void GetDescriptors(nsTArray<RefPtr<nsCacheEntryDescriptor> > &outDescriptors);
  159. private:
  160. friend class nsCacheEntryHashTable;
  161. friend class nsCacheService;
  162. void DetachDescriptors();
  163. // internal methods
  164. void MarkDoomed() { mFlags |= eDoomedMask; }
  165. void MarkStreamBased() { mFlags |= eStreamDataMask; }
  166. void MarkInitialized() { mFlags |= eInitializedMask; }
  167. void MarkActive() { mFlags |= eActiveMask; }
  168. void MarkInactive() { mFlags &= ~eActiveMask; }
  169. nsCString mKey;
  170. uint32_t mFetchCount; // 4
  171. uint32_t mLastFetched; // 4
  172. uint32_t mLastModified; // 4
  173. uint32_t mLastValidated; // 4
  174. uint32_t mExpirationTime; // 4
  175. uint32_t mFlags; // 4
  176. int64_t mPredictedDataSize; // Size given by ContentLength.
  177. uint32_t mDataSize; // 4
  178. nsCacheDevice * mCacheDevice; // 4
  179. nsCacheDevice * mCustomDevice; // 4
  180. nsCOMPtr<nsISupports> mSecurityInfo; //
  181. nsISupports * mData; // strong ref
  182. nsCOMPtr<nsIThread> mThread;
  183. nsCacheMetaData mMetaData; // 4
  184. PRCList mRequestQ; // 8
  185. PRCList mDescriptorQ; // 8
  186. };
  187. /******************************************************************************
  188. * nsCacheEntryInfo
  189. *******************************************************************************/
  190. class nsCacheEntryInfo : public nsICacheEntryInfo {
  191. public:
  192. NS_DECL_ISUPPORTS
  193. NS_DECL_NSICACHEENTRYINFO
  194. explicit nsCacheEntryInfo(nsCacheEntry* entry)
  195. : mCacheEntry(entry)
  196. {
  197. }
  198. void DetachEntry() { mCacheEntry = nullptr; }
  199. private:
  200. nsCacheEntry * mCacheEntry;
  201. virtual ~nsCacheEntryInfo() {}
  202. };
  203. /******************************************************************************
  204. * nsCacheEntryHashTable
  205. *******************************************************************************/
  206. struct nsCacheEntryHashTableEntry : public PLDHashEntryHdr
  207. {
  208. nsCacheEntry *cacheEntry;
  209. };
  210. class nsCacheEntryHashTable
  211. {
  212. public:
  213. nsCacheEntryHashTable();
  214. ~nsCacheEntryHashTable();
  215. void Init();
  216. void Shutdown();
  217. nsCacheEntry *GetEntry( const nsCString * key);
  218. nsresult AddEntry( nsCacheEntry *entry);
  219. void RemoveEntry( nsCacheEntry *entry);
  220. PLDHashTable::Iterator Iter();
  221. private:
  222. // PLDHashTable operation callbacks
  223. static PLDHashNumber HashKey(const void *key);
  224. static bool MatchEntry(const PLDHashEntryHdr * entry,
  225. const void * key);
  226. static void MoveEntry( PLDHashTable *table,
  227. const PLDHashEntryHdr *from,
  228. PLDHashEntryHdr *to);
  229. static void ClearEntry( PLDHashTable *table, PLDHashEntryHdr *entry);
  230. static void Finalize( PLDHashTable *table);
  231. // member variables
  232. static const PLDHashTableOps ops;
  233. PLDHashTable table;
  234. bool initialized;
  235. static const uint32_t kInitialTableLength = 256;
  236. };
  237. #endif // _nsCacheEntry_h_