2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
*
|
2012-05-21 04:12:37 -07:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#ifndef _nsCacheEntry_h_
|
|
|
|
#define _nsCacheEntry_h_
|
|
|
|
|
|
|
|
#include "nsICache.h"
|
|
|
|
#include "nsICacheEntryDescriptor.h"
|
|
|
|
#include "nsIThread.h"
|
|
|
|
#include "nsCacheMetaData.h"
|
|
|
|
|
|
|
|
#include "nspr.h"
|
|
|
|
#include "pldhash.h"
|
|
|
|
#include "nscore.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsAString.h"
|
|
|
|
|
|
|
|
class nsCacheDevice;
|
|
|
|
class nsCacheMetaData;
|
|
|
|
class nsCacheRequest;
|
|
|
|
class nsCacheEntryDescriptor;
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* nsCacheEntry
|
|
|
|
*******************************************************************************/
|
|
|
|
class nsCacheEntry : public PRCList
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2012-08-07 17:06:06 -07:00
|
|
|
nsCacheEntry(const nsACString & key,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool streamBased,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCacheStoragePolicy storagePolicy);
|
|
|
|
~nsCacheEntry();
|
|
|
|
|
|
|
|
|
|
|
|
static nsresult Create( const char * key,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool streamBased,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCacheStoragePolicy storagePolicy,
|
|
|
|
nsCacheDevice * device,
|
|
|
|
nsCacheEntry ** result);
|
|
|
|
|
2012-08-07 17:06:06 -07:00
|
|
|
nsCString * Key() { return &mKey; }
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t FetchCount() { return mFetchCount; }
|
|
|
|
void SetFetchCount( int32_t count) { mFetchCount = count; }
|
2007-03-22 10:30:00 -07:00
|
|
|
void Fetched();
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t LastFetched() { return mLastFetched; }
|
|
|
|
void SetLastFetched( uint32_t lastFetched) { mLastFetched = lastFetched; }
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t LastModified() { return mLastModified; }
|
|
|
|
void SetLastModified( uint32_t lastModified) { mLastModified = lastModified; }
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t ExpirationTime() { return mExpirationTime; }
|
|
|
|
void SetExpirationTime( uint32_t expires) { mExpirationTime = expires; }
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t Size()
|
2012-08-07 17:06:06 -07:00
|
|
|
{ return mDataSize + mMetaData.Size() + mKey.Length() ; }
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsCacheDevice * CacheDevice() { return mCacheDevice; }
|
|
|
|
void SetCacheDevice( nsCacheDevice * device) { mCacheDevice = device; }
|
2012-06-04 07:12:24 -07:00
|
|
|
void SetCustomCacheDevice( nsCacheDevice * device )
|
|
|
|
{ mCustomDevice = device; }
|
|
|
|
nsCacheDevice * CustomCacheDevice() { return mCustomDevice; }
|
2007-03-22 10:30:00 -07:00
|
|
|
const char * GetDeviceID();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Data accessors
|
|
|
|
*/
|
|
|
|
nsISupports *Data() { return mData; }
|
|
|
|
void SetData( nsISupports * data);
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t PredictedDataSize() { return mPredictedDataSize; }
|
|
|
|
void SetPredictedDataSize(int64_t size) { mPredictedDataSize = size; }
|
2010-09-07 15:39:28 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t DataSize() { return mDataSize; }
|
|
|
|
void SetDataSize( uint32_t size) { mDataSize = size; }
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
void TouchData();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Meta data accessors
|
|
|
|
*/
|
|
|
|
const char * GetMetaDataElement( const char * key) { return mMetaData.GetElement(key); }
|
|
|
|
nsresult SetMetaDataElement( const char * key,
|
|
|
|
const char * value) { return mMetaData.SetElement(key, value); }
|
|
|
|
nsresult VisitMetaDataElements( nsICacheMetaDataVisitor * visitor) { return mMetaData.VisitElements(visitor); }
|
2012-08-22 08:56:38 -07:00
|
|
|
nsresult FlattenMetaData(char * buffer, uint32_t bufSize) { return mMetaData.FlattenMetaData(buffer, bufSize); }
|
|
|
|
nsresult UnflattenMetaData(const char * buffer, uint32_t bufSize) { return mMetaData.UnflattenMetaData(buffer, bufSize); }
|
|
|
|
uint32_t MetaDataSize() { return mMetaData.Size(); }
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
void TouchMetaData();
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Security Info accessors
|
|
|
|
*/
|
2007-11-30 10:05:54 -08:00
|
|
|
nsISupports* SecurityInfo() { return mSecurityInfo; }
|
2007-03-22 10:30:00 -07:00
|
|
|
void SetSecurityInfo( nsISupports * info) { mSecurityInfo = info; }
|
|
|
|
|
|
|
|
|
|
|
|
// XXX enumerate MetaData method
|
|
|
|
|
|
|
|
|
|
|
|
enum CacheEntryFlags {
|
|
|
|
eStoragePolicyMask = 0x000000FF,
|
|
|
|
eDoomedMask = 0x00000100,
|
|
|
|
eEntryDirtyMask = 0x00000200,
|
|
|
|
eDataDirtyMask = 0x00000400,
|
|
|
|
eMetaDataDirtyMask = 0x00000800,
|
|
|
|
eStreamDataMask = 0x00001000,
|
|
|
|
eActiveMask = 0x00002000,
|
|
|
|
eInitializedMask = 0x00004000,
|
|
|
|
eValidMask = 0x00008000,
|
2012-05-24 08:31:53 -07:00
|
|
|
eBindingMask = 0x00010000,
|
|
|
|
ePrivateMask = 0x00020000
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
void MarkBinding() { mFlags |= eBindingMask; }
|
|
|
|
void ClearBinding() { mFlags &= ~eBindingMask; }
|
2011-09-28 23:19:26 -07:00
|
|
|
bool IsBinding() { return (mFlags & eBindingMask) != 0; }
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
void MarkEntryDirty() { mFlags |= eEntryDirtyMask; }
|
|
|
|
void MarkEntryClean() { mFlags &= ~eEntryDirtyMask; }
|
|
|
|
void MarkDataDirty() { mFlags |= eDataDirtyMask; }
|
|
|
|
void MarkDataClean() { mFlags &= ~eDataDirtyMask; }
|
|
|
|
void MarkMetaDataDirty() { mFlags |= eMetaDataDirtyMask; }
|
|
|
|
void MarkMetaDataClean() { mFlags &= ~eMetaDataDirtyMask; }
|
|
|
|
void MarkStreamData() { mFlags |= eStreamDataMask; }
|
|
|
|
void MarkValid() { mFlags |= eValidMask; }
|
|
|
|
void MarkInvalid() { mFlags &= ~eValidMask; }
|
2012-05-24 08:31:53 -07:00
|
|
|
void MarkPrivate() { mFlags |= ePrivateMask; }
|
|
|
|
void MarkPublic() { mFlags &= ~ePrivateMask; }
|
2007-03-22 10:30:00 -07:00
|
|
|
// void MarkAllowedInMemory() { mFlags |= eAllowedInMemoryMask; }
|
|
|
|
// void MarkAllowedOnDisk() { mFlags |= eAllowedOnDiskMask; }
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool IsDoomed() { return (mFlags & eDoomedMask) != 0; }
|
|
|
|
bool IsEntryDirty() { return (mFlags & eEntryDirtyMask) != 0; }
|
|
|
|
bool IsDataDirty() { return (mFlags & eDataDirtyMask) != 0; }
|
|
|
|
bool IsMetaDataDirty() { return (mFlags & eMetaDataDirtyMask) != 0; }
|
|
|
|
bool IsStreamData() { return (mFlags & eStreamDataMask) != 0; }
|
|
|
|
bool IsActive() { return (mFlags & eActiveMask) != 0; }
|
|
|
|
bool IsInitialized() { return (mFlags & eInitializedMask) != 0; }
|
|
|
|
bool IsValid() { return (mFlags & eValidMask) != 0; }
|
|
|
|
bool IsInvalid() { return (mFlags & eValidMask) == 0; }
|
|
|
|
bool IsInUse() { return IsBinding() ||
|
2007-03-22 10:30:00 -07:00
|
|
|
!(PR_CLIST_IS_EMPTY(&mRequestQ) &&
|
|
|
|
PR_CLIST_IS_EMPTY(&mDescriptorQ)); }
|
2011-09-28 23:19:26 -07:00
|
|
|
bool IsNotInUse() { return !IsInUse(); }
|
2012-05-24 08:31:53 -07:00
|
|
|
bool IsPrivate() { return (mFlags & ePrivateMask) != 0; }
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool IsAllowedInMemory()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
return (StoragePolicy() == nsICache::STORE_ANYWHERE) ||
|
|
|
|
(StoragePolicy() == nsICache::STORE_IN_MEMORY);
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool IsAllowedOnDisk()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-05-24 08:31:53 -07:00
|
|
|
return !IsPrivate() && ((StoragePolicy() == nsICache::STORE_ANYWHERE) ||
|
2007-03-22 10:30:00 -07:00
|
|
|
(StoragePolicy() == nsICache::STORE_ON_DISK) ||
|
2012-05-24 08:31:53 -07:00
|
|
|
(StoragePolicy() == nsICache::STORE_ON_DISK_AS_FILE));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool IsAllowedOffline()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
return (StoragePolicy() == nsICache::STORE_OFFLINE);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCacheStoragePolicy StoragePolicy()
|
|
|
|
{
|
|
|
|
return (nsCacheStoragePolicy)(mFlags & eStoragePolicyMask);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetStoragePolicy(nsCacheStoragePolicy policy)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(policy <= 0xFF, "too many bits in nsCacheStoragePolicy");
|
|
|
|
mFlags &= ~eStoragePolicyMask; // clear storage policy bits
|
|
|
|
mFlags |= policy;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// methods for nsCacheService
|
|
|
|
nsresult RequestAccess( nsCacheRequest * request, nsCacheAccessMode *accessGranted);
|
|
|
|
nsresult CreateDescriptor( nsCacheRequest * request,
|
|
|
|
nsCacheAccessMode accessGranted,
|
|
|
|
nsICacheEntryDescriptor ** result);
|
|
|
|
|
|
|
|
// nsresult Open(nsCacheRequest *request, nsICacheEntryDescriptor ** result);
|
|
|
|
// nsresult AsyncOpen(nsCacheRequest *request);
|
2011-09-28 23:19:26 -07:00
|
|
|
bool RemoveRequest( nsCacheRequest * request);
|
|
|
|
bool RemoveDescriptor( nsCacheEntryDescriptor * descriptor);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
private:
|
|
|
|
friend class nsCacheEntryHashTable;
|
|
|
|
friend class nsCacheService;
|
|
|
|
|
|
|
|
void DetachDescriptors(void);
|
|
|
|
|
|
|
|
// internal methods
|
|
|
|
void MarkDoomed() { mFlags |= eDoomedMask; }
|
|
|
|
void MarkStreamBased() { mFlags |= eStreamDataMask; }
|
|
|
|
void MarkInitialized() { mFlags |= eInitializedMask; }
|
|
|
|
void MarkActive() { mFlags |= eActiveMask; }
|
|
|
|
void MarkInactive() { mFlags &= ~eActiveMask; }
|
|
|
|
|
2012-08-07 17:06:06 -07:00
|
|
|
nsCString mKey;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t mFetchCount; // 4
|
|
|
|
uint32_t mLastFetched; // 4
|
|
|
|
uint32_t mLastModified; // 4
|
|
|
|
uint32_t mLastValidated; // 4
|
|
|
|
uint32_t mExpirationTime; // 4
|
|
|
|
uint32_t mFlags; // 4
|
|
|
|
int64_t mPredictedDataSize; // Size given by ContentLength.
|
|
|
|
uint32_t mDataSize; // 4
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCacheDevice * mCacheDevice; // 4
|
2012-06-04 07:12:24 -07:00
|
|
|
nsCacheDevice * mCustomDevice; // 4
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsISupports> mSecurityInfo; //
|
|
|
|
nsISupports * mData; // strong ref
|
|
|
|
nsCOMPtr<nsIThread> mThread;
|
|
|
|
nsCacheMetaData mMetaData; // 4
|
|
|
|
PRCList mRequestQ; // 8
|
|
|
|
PRCList mDescriptorQ; // 8
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* nsCacheEntryInfo
|
|
|
|
*******************************************************************************/
|
|
|
|
class nsCacheEntryInfo : public nsICacheEntryInfo {
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSICACHEENTRYINFO
|
|
|
|
|
|
|
|
nsCacheEntryInfo(nsCacheEntry* entry)
|
|
|
|
: mCacheEntry(entry)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~nsCacheEntryInfo() {}
|
2012-07-30 07:20:58 -07:00
|
|
|
void DetachEntry() { mCacheEntry = nullptr; }
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
private:
|
|
|
|
nsCacheEntry * mCacheEntry;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* nsCacheEntryHashTable
|
|
|
|
*******************************************************************************/
|
|
|
|
typedef struct {
|
|
|
|
PLDHashNumber keyHash;
|
|
|
|
nsCacheEntry *cacheEntry;
|
|
|
|
} nsCacheEntryHashTableEntry;
|
|
|
|
|
|
|
|
|
|
|
|
class nsCacheEntryHashTable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
nsCacheEntryHashTable();
|
|
|
|
~nsCacheEntryHashTable();
|
|
|
|
|
|
|
|
nsresult Init();
|
|
|
|
void Shutdown();
|
|
|
|
|
|
|
|
nsCacheEntry *GetEntry( const nsCString * key);
|
|
|
|
nsresult AddEntry( nsCacheEntry *entry);
|
|
|
|
void RemoveEntry( nsCacheEntry *entry);
|
|
|
|
|
2008-03-11 01:08:05 -07:00
|
|
|
void VisitEntries( PLDHashEnumerator etor, void *arg);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-03-11 01:08:05 -07:00
|
|
|
private:
|
2007-03-22 10:30:00 -07:00
|
|
|
// PLDHashTable operation callbacks
|
2008-10-10 08:04:34 -07:00
|
|
|
static PLDHashNumber HashKey( PLDHashTable *table, const void *key);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool MatchEntry( PLDHashTable * table,
|
2008-10-10 08:04:34 -07:00
|
|
|
const PLDHashEntryHdr * entry,
|
|
|
|
const void * key);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-10-10 08:04:34 -07:00
|
|
|
static void MoveEntry( PLDHashTable *table,
|
|
|
|
const PLDHashEntryHdr *from,
|
|
|
|
PLDHashEntryHdr *to);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-10-10 08:04:34 -07:00
|
|
|
static void ClearEntry( PLDHashTable *table, PLDHashEntryHdr *entry);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-10-10 08:04:34 -07:00
|
|
|
static void Finalize( PLDHashTable *table);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
static
|
2008-10-10 08:04:34 -07:00
|
|
|
PLDHashOperator FreeCacheEntries(PLDHashTable * table,
|
|
|
|
PLDHashEntryHdr * hdr,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t number,
|
2008-10-10 08:04:34 -07:00
|
|
|
void * arg);
|
2007-03-22 10:30:00 -07:00
|
|
|
static
|
2008-10-10 08:04:34 -07:00
|
|
|
PLDHashOperator VisitEntry(PLDHashTable * table,
|
|
|
|
PLDHashEntryHdr * hdr,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t number,
|
2008-10-10 08:04:34 -07:00
|
|
|
void * arg);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// member variables
|
|
|
|
static PLDHashTableOps ops;
|
|
|
|
PLDHashTable table;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool initialized;
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // _nsCacheEntry_h_
|