netwerk/cache/nsCacheEntry.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
michael@0 2 *
michael@0 3 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #ifndef _nsCacheEntry_h_
michael@0 8 #define _nsCacheEntry_h_
michael@0 9
michael@0 10 #include "nsICache.h"
michael@0 11 #include "nsICacheEntryDescriptor.h"
michael@0 12 #include "nsIThread.h"
michael@0 13 #include "nsCacheMetaData.h"
michael@0 14
michael@0 15 #include "nspr.h"
michael@0 16 #include "pldhash.h"
michael@0 17 #include "nsAutoPtr.h"
michael@0 18 #include "nscore.h"
michael@0 19 #include "nsCOMPtr.h"
michael@0 20 #include "nsString.h"
michael@0 21 #include "nsAString.h"
michael@0 22
michael@0 23 class nsCacheDevice;
michael@0 24 class nsCacheMetaData;
michael@0 25 class nsCacheRequest;
michael@0 26 class nsCacheEntryDescriptor;
michael@0 27
michael@0 28 /******************************************************************************
michael@0 29 * nsCacheEntry
michael@0 30 *******************************************************************************/
michael@0 31 class nsCacheEntry : public PRCList
michael@0 32 {
michael@0 33 public:
michael@0 34
michael@0 35 nsCacheEntry(const nsACString & key,
michael@0 36 bool streamBased,
michael@0 37 nsCacheStoragePolicy storagePolicy);
michael@0 38 ~nsCacheEntry();
michael@0 39
michael@0 40
michael@0 41 static nsresult Create( const char * key,
michael@0 42 bool streamBased,
michael@0 43 nsCacheStoragePolicy storagePolicy,
michael@0 44 nsCacheDevice * device,
michael@0 45 nsCacheEntry ** result);
michael@0 46
michael@0 47 nsCString * Key() { return &mKey; }
michael@0 48
michael@0 49 int32_t FetchCount() { return mFetchCount; }
michael@0 50 void SetFetchCount( int32_t count) { mFetchCount = count; }
michael@0 51 void Fetched();
michael@0 52
michael@0 53 uint32_t LastFetched() { return mLastFetched; }
michael@0 54 void SetLastFetched( uint32_t lastFetched) { mLastFetched = lastFetched; }
michael@0 55
michael@0 56 uint32_t LastModified() { return mLastModified; }
michael@0 57 void SetLastModified( uint32_t lastModified) { mLastModified = lastModified; }
michael@0 58
michael@0 59 uint32_t ExpirationTime() { return mExpirationTime; }
michael@0 60 void SetExpirationTime( uint32_t expires) { mExpirationTime = expires; }
michael@0 61
michael@0 62 uint32_t Size()
michael@0 63 { return mDataSize + mMetaData.Size() + mKey.Length() ; }
michael@0 64
michael@0 65 nsCacheDevice * CacheDevice() { return mCacheDevice; }
michael@0 66 void SetCacheDevice( nsCacheDevice * device) { mCacheDevice = device; }
michael@0 67 void SetCustomCacheDevice( nsCacheDevice * device )
michael@0 68 { mCustomDevice = device; }
michael@0 69 nsCacheDevice * CustomCacheDevice() { return mCustomDevice; }
michael@0 70 const char * GetDeviceID();
michael@0 71
michael@0 72 /**
michael@0 73 * Data accessors
michael@0 74 */
michael@0 75 nsISupports *Data() { return mData; }
michael@0 76 void SetData( nsISupports * data);
michael@0 77
michael@0 78 int64_t PredictedDataSize() { return mPredictedDataSize; }
michael@0 79 void SetPredictedDataSize(int64_t size) { mPredictedDataSize = size; }
michael@0 80
michael@0 81 uint32_t DataSize() { return mDataSize; }
michael@0 82 void SetDataSize( uint32_t size) { mDataSize = size; }
michael@0 83
michael@0 84 void TouchData();
michael@0 85
michael@0 86 /**
michael@0 87 * Meta data accessors
michael@0 88 */
michael@0 89 const char * GetMetaDataElement( const char * key) { return mMetaData.GetElement(key); }
michael@0 90 nsresult SetMetaDataElement( const char * key,
michael@0 91 const char * value) { return mMetaData.SetElement(key, value); }
michael@0 92 nsresult VisitMetaDataElements( nsICacheMetaDataVisitor * visitor) { return mMetaData.VisitElements(visitor); }
michael@0 93 nsresult FlattenMetaData(char * buffer, uint32_t bufSize) { return mMetaData.FlattenMetaData(buffer, bufSize); }
michael@0 94 nsresult UnflattenMetaData(const char * buffer, uint32_t bufSize) { return mMetaData.UnflattenMetaData(buffer, bufSize); }
michael@0 95 uint32_t MetaDataSize() { return mMetaData.Size(); }
michael@0 96
michael@0 97 void TouchMetaData();
michael@0 98
michael@0 99
michael@0 100 /**
michael@0 101 * Security Info accessors
michael@0 102 */
michael@0 103 nsISupports* SecurityInfo() { return mSecurityInfo; }
michael@0 104 void SetSecurityInfo( nsISupports * info) { mSecurityInfo = info; }
michael@0 105
michael@0 106
michael@0 107 // XXX enumerate MetaData method
michael@0 108
michael@0 109
michael@0 110 enum CacheEntryFlags {
michael@0 111 eStoragePolicyMask = 0x000000FF,
michael@0 112 eDoomedMask = 0x00000100,
michael@0 113 eEntryDirtyMask = 0x00000200,
michael@0 114 eDataDirtyMask = 0x00000400,
michael@0 115 eMetaDataDirtyMask = 0x00000800,
michael@0 116 eStreamDataMask = 0x00001000,
michael@0 117 eActiveMask = 0x00002000,
michael@0 118 eInitializedMask = 0x00004000,
michael@0 119 eValidMask = 0x00008000,
michael@0 120 eBindingMask = 0x00010000,
michael@0 121 ePrivateMask = 0x00020000
michael@0 122 };
michael@0 123
michael@0 124 void MarkBinding() { mFlags |= eBindingMask; }
michael@0 125 void ClearBinding() { mFlags &= ~eBindingMask; }
michael@0 126 bool IsBinding() { return (mFlags & eBindingMask) != 0; }
michael@0 127
michael@0 128 void MarkEntryDirty() { mFlags |= eEntryDirtyMask; }
michael@0 129 void MarkEntryClean() { mFlags &= ~eEntryDirtyMask; }
michael@0 130 void MarkDataDirty() { mFlags |= eDataDirtyMask; }
michael@0 131 void MarkDataClean() { mFlags &= ~eDataDirtyMask; }
michael@0 132 void MarkMetaDataDirty() { mFlags |= eMetaDataDirtyMask; }
michael@0 133 void MarkMetaDataClean() { mFlags &= ~eMetaDataDirtyMask; }
michael@0 134 void MarkStreamData() { mFlags |= eStreamDataMask; }
michael@0 135 void MarkValid() { mFlags |= eValidMask; }
michael@0 136 void MarkInvalid() { mFlags &= ~eValidMask; }
michael@0 137 void MarkPrivate() { mFlags |= ePrivateMask; }
michael@0 138 void MarkPublic() { mFlags &= ~ePrivateMask; }
michael@0 139 // void MarkAllowedInMemory() { mFlags |= eAllowedInMemoryMask; }
michael@0 140 // void MarkAllowedOnDisk() { mFlags |= eAllowedOnDiskMask; }
michael@0 141
michael@0 142 bool IsDoomed() { return (mFlags & eDoomedMask) != 0; }
michael@0 143 bool IsEntryDirty() { return (mFlags & eEntryDirtyMask) != 0; }
michael@0 144 bool IsDataDirty() { return (mFlags & eDataDirtyMask) != 0; }
michael@0 145 bool IsMetaDataDirty() { return (mFlags & eMetaDataDirtyMask) != 0; }
michael@0 146 bool IsStreamData() { return (mFlags & eStreamDataMask) != 0; }
michael@0 147 bool IsActive() { return (mFlags & eActiveMask) != 0; }
michael@0 148 bool IsInitialized() { return (mFlags & eInitializedMask) != 0; }
michael@0 149 bool IsValid() { return (mFlags & eValidMask) != 0; }
michael@0 150 bool IsInvalid() { return (mFlags & eValidMask) == 0; }
michael@0 151 bool IsInUse() { return IsBinding() ||
michael@0 152 !(PR_CLIST_IS_EMPTY(&mRequestQ) &&
michael@0 153 PR_CLIST_IS_EMPTY(&mDescriptorQ)); }
michael@0 154 bool IsNotInUse() { return !IsInUse(); }
michael@0 155 bool IsPrivate() { return (mFlags & ePrivateMask) != 0; }
michael@0 156
michael@0 157
michael@0 158 bool IsAllowedInMemory()
michael@0 159 {
michael@0 160 return (StoragePolicy() == nsICache::STORE_ANYWHERE) ||
michael@0 161 (StoragePolicy() == nsICache::STORE_IN_MEMORY);
michael@0 162 }
michael@0 163
michael@0 164 bool IsAllowedOnDisk()
michael@0 165 {
michael@0 166 return !IsPrivate() && ((StoragePolicy() == nsICache::STORE_ANYWHERE) ||
michael@0 167 (StoragePolicy() == nsICache::STORE_ON_DISK));
michael@0 168 }
michael@0 169
michael@0 170 bool IsAllowedOffline()
michael@0 171 {
michael@0 172 return (StoragePolicy() == nsICache::STORE_OFFLINE);
michael@0 173 }
michael@0 174
michael@0 175 nsCacheStoragePolicy StoragePolicy()
michael@0 176 {
michael@0 177 return (nsCacheStoragePolicy)(mFlags & eStoragePolicyMask);
michael@0 178 }
michael@0 179
michael@0 180 void SetStoragePolicy(nsCacheStoragePolicy policy)
michael@0 181 {
michael@0 182 NS_ASSERTION(policy <= 0xFF, "too many bits in nsCacheStoragePolicy");
michael@0 183 mFlags &= ~eStoragePolicyMask; // clear storage policy bits
michael@0 184 mFlags |= policy;
michael@0 185 }
michael@0 186
michael@0 187
michael@0 188 // methods for nsCacheService
michael@0 189 nsresult RequestAccess( nsCacheRequest * request, nsCacheAccessMode *accessGranted);
michael@0 190 nsresult CreateDescriptor( nsCacheRequest * request,
michael@0 191 nsCacheAccessMode accessGranted,
michael@0 192 nsICacheEntryDescriptor ** result);
michael@0 193
michael@0 194 bool RemoveRequest( nsCacheRequest * request);
michael@0 195 bool RemoveDescriptor( nsCacheEntryDescriptor * descriptor,
michael@0 196 bool * doomEntry);
michael@0 197
michael@0 198 void GetDescriptors(nsTArray<nsRefPtr<nsCacheEntryDescriptor> > &outDescriptors);
michael@0 199
michael@0 200 private:
michael@0 201 friend class nsCacheEntryHashTable;
michael@0 202 friend class nsCacheService;
michael@0 203
michael@0 204 void DetachDescriptors();
michael@0 205
michael@0 206 // internal methods
michael@0 207 void MarkDoomed() { mFlags |= eDoomedMask; }
michael@0 208 void MarkStreamBased() { mFlags |= eStreamDataMask; }
michael@0 209 void MarkInitialized() { mFlags |= eInitializedMask; }
michael@0 210 void MarkActive() { mFlags |= eActiveMask; }
michael@0 211 void MarkInactive() { mFlags &= ~eActiveMask; }
michael@0 212
michael@0 213 nsCString mKey;
michael@0 214 uint32_t mFetchCount; // 4
michael@0 215 uint32_t mLastFetched; // 4
michael@0 216 uint32_t mLastModified; // 4
michael@0 217 uint32_t mLastValidated; // 4
michael@0 218 uint32_t mExpirationTime; // 4
michael@0 219 uint32_t mFlags; // 4
michael@0 220 int64_t mPredictedDataSize; // Size given by ContentLength.
michael@0 221 uint32_t mDataSize; // 4
michael@0 222 nsCacheDevice * mCacheDevice; // 4
michael@0 223 nsCacheDevice * mCustomDevice; // 4
michael@0 224 nsCOMPtr<nsISupports> mSecurityInfo; //
michael@0 225 nsISupports * mData; // strong ref
michael@0 226 nsCOMPtr<nsIThread> mThread;
michael@0 227 nsCacheMetaData mMetaData; // 4
michael@0 228 PRCList mRequestQ; // 8
michael@0 229 PRCList mDescriptorQ; // 8
michael@0 230 };
michael@0 231
michael@0 232
michael@0 233 /******************************************************************************
michael@0 234 * nsCacheEntryInfo
michael@0 235 *******************************************************************************/
michael@0 236 class nsCacheEntryInfo : public nsICacheEntryInfo {
michael@0 237 public:
michael@0 238 NS_DECL_ISUPPORTS
michael@0 239 NS_DECL_NSICACHEENTRYINFO
michael@0 240
michael@0 241 nsCacheEntryInfo(nsCacheEntry* entry)
michael@0 242 : mCacheEntry(entry)
michael@0 243 {
michael@0 244 }
michael@0 245
michael@0 246 virtual ~nsCacheEntryInfo() {}
michael@0 247 void DetachEntry() { mCacheEntry = nullptr; }
michael@0 248
michael@0 249 private:
michael@0 250 nsCacheEntry * mCacheEntry;
michael@0 251 };
michael@0 252
michael@0 253
michael@0 254 /******************************************************************************
michael@0 255 * nsCacheEntryHashTable
michael@0 256 *******************************************************************************/
michael@0 257 typedef struct {
michael@0 258 PLDHashNumber keyHash;
michael@0 259 nsCacheEntry *cacheEntry;
michael@0 260 } nsCacheEntryHashTableEntry;
michael@0 261
michael@0 262
michael@0 263 class nsCacheEntryHashTable
michael@0 264 {
michael@0 265 public:
michael@0 266 nsCacheEntryHashTable();
michael@0 267 ~nsCacheEntryHashTable();
michael@0 268
michael@0 269 nsresult Init();
michael@0 270 void Shutdown();
michael@0 271
michael@0 272 nsCacheEntry *GetEntry( const nsCString * key);
michael@0 273 nsresult AddEntry( nsCacheEntry *entry);
michael@0 274 void RemoveEntry( nsCacheEntry *entry);
michael@0 275
michael@0 276 void VisitEntries( PLDHashEnumerator etor, void *arg);
michael@0 277
michael@0 278 private:
michael@0 279 // PLDHashTable operation callbacks
michael@0 280 static PLDHashNumber HashKey( PLDHashTable *table, const void *key);
michael@0 281
michael@0 282 static bool MatchEntry( PLDHashTable * table,
michael@0 283 const PLDHashEntryHdr * entry,
michael@0 284 const void * key);
michael@0 285
michael@0 286 static void MoveEntry( PLDHashTable *table,
michael@0 287 const PLDHashEntryHdr *from,
michael@0 288 PLDHashEntryHdr *to);
michael@0 289
michael@0 290 static void ClearEntry( PLDHashTable *table, PLDHashEntryHdr *entry);
michael@0 291
michael@0 292 static void Finalize( PLDHashTable *table);
michael@0 293
michael@0 294 static
michael@0 295 PLDHashOperator FreeCacheEntries(PLDHashTable * table,
michael@0 296 PLDHashEntryHdr * hdr,
michael@0 297 uint32_t number,
michael@0 298 void * arg);
michael@0 299 static
michael@0 300 PLDHashOperator VisitEntry(PLDHashTable * table,
michael@0 301 PLDHashEntryHdr * hdr,
michael@0 302 uint32_t number,
michael@0 303 void * arg);
michael@0 304
michael@0 305 // member variables
michael@0 306 static const PLDHashTableOps ops;
michael@0 307 PLDHashTable table;
michael@0 308 bool initialized;
michael@0 309 };
michael@0 310
michael@0 311 #endif // _nsCacheEntry_h_

mercurial