2001-02-23 13:18:01 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public
|
|
|
|
* License Version 1.1 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS
|
|
|
|
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
|
|
|
* implied. See the License for the specific language governing
|
|
|
|
* rights and limitations under the License.
|
|
|
|
*
|
|
|
|
* The Original Code is nsCacheEntry.h, released February 22, 2001.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Netscape Communications
|
|
|
|
* Corporation. Portions created by Netscape are
|
|
|
|
* Copyright (C) 2001 Netscape Communications Corporation. All
|
|
|
|
* Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Gordon Sheridan, 22-February-2001
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _nsCacheEntry_h_
|
|
|
|
#define _nsCacheEntry_h_
|
|
|
|
|
|
|
|
#include "nspr.h"
|
|
|
|
#include "pldhash.h"
|
|
|
|
#include "nscore.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsAReadableString.h"
|
|
|
|
|
2001-02-26 14:42:50 +00:00
|
|
|
#include "nsICache.h"
|
2001-02-23 13:18:01 +00:00
|
|
|
#include "nsICacheEntryDescriptor.h"
|
2001-02-28 07:13:32 +00:00
|
|
|
#include "nsCacheMetaData.h"
|
2001-02-23 13:18:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
class nsCacheDevice;
|
|
|
|
class nsCacheMetaData;
|
|
|
|
class nsCacheRequest;
|
2001-02-26 14:42:50 +00:00
|
|
|
class nsCacheEntryDescriptor;
|
2001-02-23 13:18:01 +00:00
|
|
|
|
2001-02-28 07:13:32 +00:00
|
|
|
|
2001-03-05 07:17:58 +00:00
|
|
|
class nsCacheEntry : public PRCList
|
2001-02-23 13:18:01 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2001-02-28 03:54:16 +00:00
|
|
|
nsCacheEntry(nsCString * key,
|
|
|
|
PRBool streamBased,
|
|
|
|
nsCacheStoragePolicy storagePolicy);
|
2001-02-23 13:18:01 +00:00
|
|
|
~nsCacheEntry();
|
|
|
|
|
2001-03-03 04:42:21 +00:00
|
|
|
nsCacheEntry *
|
|
|
|
Create(nsCString * key,
|
|
|
|
PRBool streamBased,
|
|
|
|
nsCacheStoragePolicy storagePolicy,
|
|
|
|
nsCacheDevice * cacheDevice);
|
|
|
|
|
2001-02-28 00:06:13 +00:00
|
|
|
nsCString * Key(void) { return mKey; }
|
2001-02-23 13:18:01 +00:00
|
|
|
|
2001-03-04 00:11:30 +00:00
|
|
|
PRInt32 FetchCount(void) { return mFetchCount;}
|
|
|
|
void SetFetchCount( PRInt32 count) { mFetchCount = count;}
|
2001-02-23 13:18:01 +00:00
|
|
|
|
2001-03-04 00:11:30 +00:00
|
|
|
PRUint32 LastFetched(void) { return mLastFetched;}
|
|
|
|
void SetLastFetched( PRUint32 lastFetched) { mLastFetched = lastFetched;}
|
2001-02-23 13:18:01 +00:00
|
|
|
|
2001-03-04 00:11:30 +00:00
|
|
|
PRUint32 LastValidated(void) { return mLastValidated;}
|
|
|
|
void SetLastValidated( PRUint32 lastValidated) { mLastValidated = lastValidated;}
|
2001-02-23 13:18:01 +00:00
|
|
|
|
2001-03-04 00:11:30 +00:00
|
|
|
PRUint32 ExpirationTime(void) { return mExpirationTime;}
|
|
|
|
void SetExpirationTime( PRUint32 expires) { mExpirationTime = expires;}
|
2001-02-23 13:18:01 +00:00
|
|
|
|
2001-03-04 00:11:30 +00:00
|
|
|
PRUint32 DataSize(void) { return mDataSize;}
|
|
|
|
void SetDataSize( PRUint32 size) { mDataSize = size;}
|
2001-02-23 13:18:01 +00:00
|
|
|
|
2001-03-04 00:11:30 +00:00
|
|
|
PRUint32 MetaDataSize(void) { return mMetaSize;}
|
|
|
|
void SetMetaDataSize( PRUint32 size) { mMetaSize = size;}
|
2001-02-23 13:18:01 +00:00
|
|
|
|
2001-02-28 00:06:13 +00:00
|
|
|
nsCacheDevice * CacheDevice(void) { return mCacheDevice;}
|
|
|
|
void SetCacheDevice( nsCacheDevice * device) { mCacheDevice = device;}
|
2001-02-23 13:18:01 +00:00
|
|
|
|
|
|
|
nsresult GetData( nsISupports ** result);
|
|
|
|
nsresult SetData( nsISupports * data);
|
|
|
|
|
|
|
|
nsresult GetMetaDataElement( const nsAReadableCString& key,
|
|
|
|
nsAReadableCString ** value);
|
|
|
|
nsresult SetMetaDataElement( const nsAReadableCString& key,
|
|
|
|
const nsAReadableCString& value);
|
|
|
|
|
2001-03-02 04:09:01 +00:00
|
|
|
nsresult FlattenMetaData(char ** data, PRUint32 * size);
|
|
|
|
nsresult UnflattenMetaData(char * data, PRUint32 size);
|
2001-02-28 07:13:32 +00:00
|
|
|
|
2001-03-04 00:11:30 +00:00
|
|
|
// XXX enumerate MetaData method
|
2001-02-23 13:18:01 +00:00
|
|
|
|
|
|
|
|
2001-02-26 14:42:50 +00:00
|
|
|
enum CacheEntryFlags {
|
|
|
|
eDoomedMask = 0x00000001,
|
|
|
|
eEntryDirtyMask = 0x00000002,
|
|
|
|
eDataDirtyMask = 0x00000004,
|
|
|
|
eMetaDataDirtyMask = 0x00000008,
|
|
|
|
eStreamDataMask = 0x00000010,
|
|
|
|
eActiveMask = 0x00000020,
|
|
|
|
eInitializedMask = 0x00000040,
|
|
|
|
eValidMask = 0x00000080,
|
|
|
|
// eStoragePolicyMask = 0x00000300
|
|
|
|
eAllowedInMemoryMask = 0x00000100,
|
|
|
|
eAllowedOnDiskMask = 0x00000200
|
|
|
|
|
|
|
|
};
|
2001-02-23 13:18:01 +00:00
|
|
|
|
2001-03-02 01:51:41 +00: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; }
|
|
|
|
void MarkAllowedInMemory() { mFlags |= eAllowedInMemoryMask; }
|
|
|
|
void MarkAllowedOnDisk() { mFlags |= eAllowedOnDiskMask; }
|
2001-02-26 14:42:50 +00:00
|
|
|
|
|
|
|
PRBool IsDoomed() { return (mFlags & eDoomedMask) != 0; }
|
|
|
|
PRBool IsEntryDirty() { return (mFlags & eEntryDirtyMask) != 0; }
|
|
|
|
PRBool IsDataDirty() { return (mFlags & eDataDirtyMask) != 0; }
|
|
|
|
PRBool IsMetaDataDirty() { return (mFlags & eMetaDataDirtyMask) != 0; }
|
|
|
|
PRBool IsStreamData() { return (mFlags & eStreamDataMask) != 0; }
|
|
|
|
PRBool IsActive() { return (mFlags & eActiveMask) != 0; }
|
|
|
|
PRBool IsInitialized() { return (mFlags & eInitializedMask) != 0; }
|
|
|
|
PRBool IsValid() { return (mFlags & eValidMask) != 0; }
|
2001-03-05 10:45:33 +00:00
|
|
|
PRBool IsInvalid() { return (mFlags & eValidMask) == 0; }
|
2001-02-27 05:35:53 +00:00
|
|
|
PRBool IsAllowedInMemory() { return (mFlags & eAllowedInMemoryMask) != 0; }
|
2001-03-03 02:27:49 +00:00
|
|
|
PRBool IsAllowedOnDisk() { return (mFlags & eAllowedOnDiskMask) !=0; }
|
|
|
|
PRBool IsInUse() { return !(PR_CLIST_IS_EMPTY(&mRequestQ) &&
|
|
|
|
PR_CLIST_IS_EMPTY(&mDescriptorQ)); }
|
|
|
|
PRBool IsNotInUse() { return (PR_CLIST_IS_EMPTY(&mRequestQ) &&
|
|
|
|
PR_CLIST_IS_EMPTY(&mDescriptorQ)); }
|
2001-02-23 13:18:01 +00:00
|
|
|
|
|
|
|
// methods for nsCacheService
|
2001-03-05 07:17:58 +00:00
|
|
|
nsresult RequestAccess( nsCacheRequest * request, nsCacheAccessMode *accessGranted);
|
|
|
|
nsresult CreateDescriptor( nsCacheRequest * request,
|
|
|
|
nsCacheAccessMode accessGranted,
|
|
|
|
nsICacheEntryDescriptor ** result);
|
|
|
|
|
2001-03-05 10:45:33 +00:00
|
|
|
// nsresult Open(nsCacheRequest *request, nsICacheEntryDescriptor ** result);
|
|
|
|
// nsresult AsyncOpen(nsCacheRequest *request);
|
2001-02-26 14:42:50 +00:00
|
|
|
PRBool RemoveRequest( nsCacheRequest * request);
|
|
|
|
PRBool RemoveDescriptor( nsCacheEntryDescriptor * descriptor);
|
2001-02-28 00:06:13 +00:00
|
|
|
|
2001-02-26 14:42:50 +00:00
|
|
|
private:
|
|
|
|
friend class nsCacheEntryHashTable;
|
|
|
|
friend class nsCacheService;
|
2001-02-23 13:18:01 +00:00
|
|
|
|
|
|
|
// internal methods
|
2001-02-28 11:29:22 +00:00
|
|
|
nsresult CommonOpen(nsCacheRequest * request, nsCacheAccessMode *accessGranted);
|
2001-03-03 04:42:21 +00:00
|
|
|
void MarkDoomed() { mFlags |= eDoomedMask; }
|
|
|
|
void MarkStreamBased() { mFlags |= eStreamDataMask; }
|
|
|
|
void MarkInitialized() { mFlags |= eInitializedMask; }
|
|
|
|
void MarkActive() { mFlags |= eActiveMask; }
|
|
|
|
void MarkInactive() { mFlags &= ~eActiveMask; }
|
2001-02-23 13:18:01 +00:00
|
|
|
|
2001-03-04 00:11:30 +00:00
|
|
|
nsCString * mKey; // 4 // XXX ask scc about const'ness
|
2001-02-23 13:18:01 +00:00
|
|
|
PRUint32 mFetchCount; // 4
|
2001-03-04 00:11:30 +00:00
|
|
|
PRUint32 mLastFetched; // 8
|
|
|
|
PRUint32 mLastValidated; // 8
|
|
|
|
PRUint32 mExpirationTime; // 8
|
2001-02-23 13:18:01 +00:00
|
|
|
PRUint32 mFlags; // 4
|
|
|
|
PRUint32 mDataSize; // 4
|
|
|
|
PRUint32 mMetaSize; // 4
|
|
|
|
nsCacheDevice * mCacheDevice; // 4
|
|
|
|
nsCOMPtr<nsISupports> mData; // 4
|
|
|
|
nsCacheMetaData * mMetaData; // 4
|
|
|
|
PRCList mRequestQ; // 8 = 64
|
|
|
|
PRCList mDescriptorQ; // 8 = 72
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
PLDHashNumber keyHash;
|
|
|
|
nsCacheEntry *cacheEntry;
|
|
|
|
} nsCacheEntryHashTableEntry;
|
|
|
|
|
|
|
|
|
|
|
|
class nsCacheEntryHashTable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
nsCacheEntryHashTable();
|
|
|
|
~nsCacheEntryHashTable();
|
|
|
|
|
|
|
|
nsresult Init();
|
|
|
|
|
|
|
|
nsCacheEntry *GetEntry( const nsCString * key);
|
|
|
|
nsresult AddEntry( nsCacheEntry *entry);
|
|
|
|
nsresult RemoveEntry( nsCacheEntry *entry);
|
2001-03-04 00:11:30 +00:00
|
|
|
// XXX enumerate entries?
|
2001-02-23 13:18:01 +00:00
|
|
|
|
2001-03-05 07:17:58 +00:00
|
|
|
// XXX
|
2001-02-23 13:18:01 +00:00
|
|
|
private:
|
|
|
|
|
|
|
|
// PLDHashTable operation callbacks
|
|
|
|
static const void * GetKey( PLDHashTable *table, PLDHashEntryHdr *entry);
|
|
|
|
|
|
|
|
static PLDHashNumber HashKey( PLDHashTable *table, const void *key);
|
|
|
|
|
|
|
|
static PRBool MatchEntry( PLDHashTable * table,
|
|
|
|
const PLDHashEntryHdr * entry,
|
|
|
|
const void * key);
|
|
|
|
|
|
|
|
static void MoveEntry( PLDHashTable *table,
|
|
|
|
const PLDHashEntryHdr *from,
|
|
|
|
PLDHashEntryHdr *to);
|
|
|
|
|
|
|
|
static void ClearEntry( PLDHashTable *table, PLDHashEntryHdr *entry);
|
|
|
|
|
|
|
|
static void Finalize( PLDHashTable *table);
|
|
|
|
|
2001-03-01 02:46:12 +00:00
|
|
|
static
|
|
|
|
PLDHashOperator FreeCacheEntries(PLDHashTable * table,
|
|
|
|
PLDHashEntryHdr * hdr,
|
|
|
|
PRUint32 number,
|
|
|
|
void * arg);
|
|
|
|
|
2001-02-23 13:18:01 +00:00
|
|
|
// member variables
|
|
|
|
static PLDHashTableOps ops;
|
|
|
|
PLDHashTable table;
|
|
|
|
PRBool initialized;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // _nsCacheEntry_h_
|
|
|
|
|