2001-02-23 16:18:01 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
*
|
2004-04-19 02:01:16 +04:00
|
|
|
* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* 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 the Initial Developer are Copyright (C) 2001
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Gordon Sheridan <gordon@netscape.com>
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
2001-02-23 16:18:01 +03:00
|
|
|
|
|
|
|
#ifndef _nsCacheEntry_h_
|
|
|
|
#define _nsCacheEntry_h_
|
|
|
|
|
2001-05-03 06:35:26 +04:00
|
|
|
#include "nsICache.h"
|
|
|
|
#include "nsICacheEntryDescriptor.h"
|
2006-05-10 21:30:15 +04:00
|
|
|
#include "nsIThread.h"
|
2001-05-03 06:35:26 +04:00
|
|
|
#include "nsCacheMetaData.h"
|
|
|
|
|
2001-02-23 16:18:01 +03:00
|
|
|
#include "nspr.h"
|
|
|
|
#include "pldhash.h"
|
|
|
|
#include "nscore.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsString.h"
|
2002-03-24 00:35:01 +03:00
|
|
|
#include "nsAString.h"
|
2001-02-23 16:18:01 +03:00
|
|
|
|
|
|
|
class nsCacheDevice;
|
|
|
|
class nsCacheMetaData;
|
|
|
|
class nsCacheRequest;
|
2001-02-26 17:42:50 +03:00
|
|
|
class nsCacheEntryDescriptor;
|
2001-02-23 16:18:01 +03:00
|
|
|
|
2001-03-21 01:42:03 +03:00
|
|
|
/******************************************************************************
|
|
|
|
* nsCacheEntry
|
|
|
|
*******************************************************************************/
|
2001-03-05 10:17:58 +03:00
|
|
|
class nsCacheEntry : public PRCList
|
2001-02-23 16:18:01 +03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2001-02-28 06:54:16 +03:00
|
|
|
nsCacheEntry(nsCString * key,
|
|
|
|
PRBool streamBased,
|
|
|
|
nsCacheStoragePolicy storagePolicy);
|
2001-02-23 16:18:01 +03:00
|
|
|
~nsCacheEntry();
|
|
|
|
|
2001-03-03 07:42:21 +03:00
|
|
|
|
2001-05-19 03:24:09 +04:00
|
|
|
static nsresult Create( const char * key,
|
|
|
|
PRBool streamBased,
|
|
|
|
nsCacheStoragePolicy storagePolicy,
|
|
|
|
nsCacheDevice * device,
|
|
|
|
nsCacheEntry ** result);
|
|
|
|
|
2001-03-29 09:54:58 +04:00
|
|
|
nsCString * Key() { return mKey; }
|
2001-02-23 16:18:01 +03:00
|
|
|
|
2003-09-11 20:57:40 +04:00
|
|
|
PRInt32 FetchCount() { return mFetchCount; }
|
|
|
|
void SetFetchCount( PRInt32 count) { mFetchCount = count; }
|
2001-03-29 09:54:58 +04:00
|
|
|
void Fetched();
|
2001-02-23 16:18:01 +03:00
|
|
|
|
2003-09-11 20:57:40 +04:00
|
|
|
PRUint32 LastFetched() { return mLastFetched; }
|
|
|
|
void SetLastFetched( PRUint32 lastFetched) { mLastFetched = lastFetched; }
|
2001-02-23 16:18:01 +03:00
|
|
|
|
2003-09-11 20:57:40 +04:00
|
|
|
PRUint32 LastModified() { return mLastModified; }
|
|
|
|
void SetLastModified( PRUint32 lastModified) { mLastModified = lastModified; }
|
2001-03-08 08:37:00 +03:00
|
|
|
|
2003-09-11 20:57:40 +04:00
|
|
|
PRUint32 ExpirationTime() { return mExpirationTime; }
|
|
|
|
void SetExpirationTime( PRUint32 expires) { mExpirationTime = expires; }
|
2001-02-23 16:18:01 +03:00
|
|
|
|
2011-03-29 22:54:58 +04:00
|
|
|
PRUint32 Size()
|
|
|
|
{ return mDataSize + mMetaData.Size() + (mKey ? mKey->Length() : 0); }
|
2001-03-13 18:43:17 +03:00
|
|
|
|
2003-09-11 20:57:40 +04:00
|
|
|
nsCacheDevice * CacheDevice() { return mCacheDevice; }
|
|
|
|
void SetCacheDevice( nsCacheDevice * device) { mCacheDevice = device; }
|
2001-04-30 05:18:34 +04:00
|
|
|
const char * GetDeviceID();
|
2001-03-07 12:27:36 +03:00
|
|
|
|
2001-03-29 09:54:58 +04:00
|
|
|
/**
|
|
|
|
* Data accessors
|
|
|
|
*/
|
2004-10-20 09:00:23 +04:00
|
|
|
nsISupports *Data() { return mData; }
|
2006-06-28 03:13:48 +04:00
|
|
|
void SetData( nsISupports * data);
|
2001-03-29 09:54:58 +04:00
|
|
|
|
2010-09-08 02:39:28 +04:00
|
|
|
PRInt64 PredictedDataSize() { return mPredictedDataSize; }
|
|
|
|
void SetPredictedDataSize(PRInt64 size) { mPredictedDataSize = size; }
|
|
|
|
|
2003-09-11 20:57:40 +04:00
|
|
|
PRUint32 DataSize() { return mDataSize; }
|
|
|
|
void SetDataSize( PRUint32 size) { mDataSize = size; }
|
2001-03-29 09:54:58 +04:00
|
|
|
|
|
|
|
void TouchData();
|
2001-05-03 06:35:26 +04:00
|
|
|
|
2001-03-29 09:54:58 +04:00
|
|
|
/**
|
|
|
|
* Meta data accessors
|
|
|
|
*/
|
2003-03-25 18:20:10 +03:00
|
|
|
const char * GetMetaDataElement( const char * key) { return mMetaData.GetElement(key); }
|
|
|
|
nsresult SetMetaDataElement( const char * key,
|
2003-09-11 20:57:40 +04:00
|
|
|
const char * value) { return mMetaData.SetElement(key, value); }
|
2003-03-25 18:20:10 +03:00
|
|
|
nsresult VisitMetaDataElements( nsICacheMetaDataVisitor * visitor) { return mMetaData.VisitElements(visitor); }
|
2003-09-11 20:57:40 +04:00
|
|
|
nsresult FlattenMetaData(char * buffer, PRUint32 bufSize) { return mMetaData.FlattenMetaData(buffer, bufSize); }
|
2004-10-20 09:00:23 +04:00
|
|
|
nsresult UnflattenMetaData(const char * buffer, PRUint32 bufSize) { return mMetaData.UnflattenMetaData(buffer, bufSize); }
|
2003-09-11 20:57:40 +04:00
|
|
|
PRUint32 MetaDataSize() { return mMetaData.Size(); }
|
2001-03-29 09:54:58 +04:00
|
|
|
|
|
|
|
void TouchMetaData();
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Security Info accessors
|
|
|
|
*/
|
2007-11-30 21:05:54 +03:00
|
|
|
nsISupports* SecurityInfo() { return mSecurityInfo; }
|
2001-03-29 09:54:58 +04:00
|
|
|
void SetSecurityInfo( nsISupports * info) { mSecurityInfo = info; }
|
|
|
|
|
|
|
|
|
2001-03-04 03:11:30 +03:00
|
|
|
// XXX enumerate MetaData method
|
2001-02-23 16:18:01 +03:00
|
|
|
|
|
|
|
|
2001-02-26 17:42:50 +03:00
|
|
|
enum CacheEntryFlags {
|
2001-03-21 01:42:03 +03:00
|
|
|
eStoragePolicyMask = 0x000000FF,
|
|
|
|
eDoomedMask = 0x00000100,
|
|
|
|
eEntryDirtyMask = 0x00000200,
|
|
|
|
eDataDirtyMask = 0x00000400,
|
|
|
|
eMetaDataDirtyMask = 0x00000800,
|
|
|
|
eStreamDataMask = 0x00001000,
|
|
|
|
eActiveMask = 0x00002000,
|
|
|
|
eInitializedMask = 0x00004000,
|
2001-04-04 07:30:45 +04:00
|
|
|
eValidMask = 0x00008000,
|
|
|
|
eBindingMask = 0x00010000
|
2001-02-26 17:42:50 +03:00
|
|
|
};
|
2001-04-04 07:30:45 +04:00
|
|
|
|
|
|
|
void MarkBinding() { mFlags |= eBindingMask; }
|
|
|
|
void ClearBinding() { mFlags &= ~eBindingMask; }
|
|
|
|
PRBool IsBinding() { return (mFlags & eBindingMask) != 0; }
|
2001-02-23 16:18:01 +03:00
|
|
|
|
2001-03-02 04:51:41 +03: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; }
|
2001-03-21 01:42:03 +03:00
|
|
|
// void MarkAllowedInMemory() { mFlags |= eAllowedInMemoryMask; }
|
|
|
|
// void MarkAllowedOnDisk() { mFlags |= eAllowedOnDiskMask; }
|
2001-02-26 17:42:50 +03: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 13:45:33 +03:00
|
|
|
PRBool IsInvalid() { return (mFlags & eValidMask) == 0; }
|
2002-01-19 01:18:28 +03:00
|
|
|
PRBool IsInUse() { return IsBinding() ||
|
|
|
|
!(PR_CLIST_IS_EMPTY(&mRequestQ) &&
|
2001-03-03 05:27:49 +03:00
|
|
|
PR_CLIST_IS_EMPTY(&mDescriptorQ)); }
|
2002-01-19 01:18:28 +03:00
|
|
|
PRBool IsNotInUse() { return !IsInUse(); }
|
2001-02-23 16:18:01 +03:00
|
|
|
|
2001-03-21 01:42:03 +03:00
|
|
|
|
|
|
|
PRBool IsAllowedInMemory()
|
|
|
|
{
|
|
|
|
return (StoragePolicy() == nsICache::STORE_ANYWHERE) ||
|
|
|
|
(StoragePolicy() == nsICache::STORE_IN_MEMORY);
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool IsAllowedOnDisk()
|
|
|
|
{
|
|
|
|
return (StoragePolicy() == nsICache::STORE_ANYWHERE) ||
|
|
|
|
(StoragePolicy() == nsICache::STORE_ON_DISK) ||
|
|
|
|
(StoragePolicy() == nsICache::STORE_ON_DISK_AS_FILE);
|
|
|
|
}
|
|
|
|
|
2007-03-14 04:52:07 +03:00
|
|
|
PRBool IsAllowedOffline()
|
|
|
|
{
|
|
|
|
return (StoragePolicy() == nsICache::STORE_OFFLINE);
|
|
|
|
}
|
|
|
|
|
2001-03-21 01:42:03 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-02-23 16:18:01 +03:00
|
|
|
// methods for nsCacheService
|
2001-03-05 10:17:58 +03:00
|
|
|
nsresult RequestAccess( nsCacheRequest * request, nsCacheAccessMode *accessGranted);
|
|
|
|
nsresult CreateDescriptor( nsCacheRequest * request,
|
|
|
|
nsCacheAccessMode accessGranted,
|
|
|
|
nsICacheEntryDescriptor ** result);
|
|
|
|
|
2001-03-05 13:45:33 +03:00
|
|
|
// nsresult Open(nsCacheRequest *request, nsICacheEntryDescriptor ** result);
|
|
|
|
// nsresult AsyncOpen(nsCacheRequest *request);
|
2001-02-26 17:42:50 +03:00
|
|
|
PRBool RemoveRequest( nsCacheRequest * request);
|
|
|
|
PRBool RemoveDescriptor( nsCacheEntryDescriptor * descriptor);
|
2001-02-28 03:06:13 +03:00
|
|
|
|
2001-02-26 17:42:50 +03:00
|
|
|
private:
|
|
|
|
friend class nsCacheEntryHashTable;
|
|
|
|
friend class nsCacheService;
|
2001-02-23 16:18:01 +03:00
|
|
|
|
2001-03-13 18:43:17 +03:00
|
|
|
void DetachDescriptors(void);
|
|
|
|
|
2001-02-23 16:18:01 +03:00
|
|
|
// internal methods
|
2001-03-03 07:42:21 +03:00
|
|
|
void MarkDoomed() { mFlags |= eDoomedMask; }
|
2001-03-08 08:37:00 +03:00
|
|
|
void MarkStreamBased() { mFlags |= eStreamDataMask; }
|
|
|
|
void MarkInitialized() { mFlags |= eInitializedMask; }
|
2001-03-03 07:42:21 +03:00
|
|
|
void MarkActive() { mFlags |= eActiveMask; }
|
|
|
|
void MarkInactive() { mFlags &= ~eActiveMask; }
|
2001-02-23 16:18:01 +03:00
|
|
|
|
2001-05-03 06:35:26 +04:00
|
|
|
nsCString * mKey; // 4 // XXX ask scc about const'ness
|
|
|
|
PRUint32 mFetchCount; // 4
|
|
|
|
PRUint32 mLastFetched; // 4
|
|
|
|
PRUint32 mLastModified; // 4
|
|
|
|
PRUint32 mLastValidated; // 4
|
|
|
|
PRUint32 mExpirationTime; // 4
|
|
|
|
PRUint32 mFlags; // 4
|
2010-09-08 02:39:28 +04:00
|
|
|
PRInt64 mPredictedDataSize; // Size given by ContentLength.
|
2001-05-03 06:35:26 +04:00
|
|
|
PRUint32 mDataSize; // 4
|
|
|
|
nsCacheDevice * mCacheDevice; // 4
|
|
|
|
nsCOMPtr<nsISupports> mSecurityInfo; //
|
2006-06-28 03:13:48 +04:00
|
|
|
nsISupports * mData; // strong ref
|
2006-05-10 21:30:15 +04:00
|
|
|
nsCOMPtr<nsIThread> mThread;
|
2003-03-25 18:20:10 +03:00
|
|
|
nsCacheMetaData mMetaData; // 4
|
2001-05-03 06:35:26 +04:00
|
|
|
PRCList mRequestQ; // 8
|
|
|
|
PRCList mDescriptorQ; // 8
|
2001-02-23 16:18:01 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2001-03-21 01:42:03 +03:00
|
|
|
/******************************************************************************
|
|
|
|
* nsCacheEntryInfo
|
|
|
|
*******************************************************************************/
|
|
|
|
class nsCacheEntryInfo : public nsICacheEntryInfo {
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSICACHEENTRYINFO
|
|
|
|
|
|
|
|
nsCacheEntryInfo(nsCacheEntry* entry)
|
|
|
|
: mCacheEntry(entry)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~nsCacheEntryInfo() {}
|
2001-03-29 09:54:58 +04:00
|
|
|
void DetachEntry() { mCacheEntry = nsnull; }
|
2001-03-21 01:42:03 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
nsCacheEntry * mCacheEntry;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* nsCacheEntryHashTable
|
|
|
|
*******************************************************************************/
|
2001-02-23 16:18:01 +03:00
|
|
|
typedef struct {
|
|
|
|
PLDHashNumber keyHash;
|
|
|
|
nsCacheEntry *cacheEntry;
|
|
|
|
} nsCacheEntryHashTableEntry;
|
|
|
|
|
|
|
|
|
|
|
|
class nsCacheEntryHashTable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
nsCacheEntryHashTable();
|
|
|
|
~nsCacheEntryHashTable();
|
|
|
|
|
|
|
|
nsresult Init();
|
2001-05-03 06:35:26 +04:00
|
|
|
void Shutdown();
|
2001-02-23 16:18:01 +03:00
|
|
|
|
|
|
|
nsCacheEntry *GetEntry( const nsCString * key);
|
|
|
|
nsresult AddEntry( nsCacheEntry *entry);
|
2001-03-08 08:37:00 +03:00
|
|
|
void RemoveEntry( nsCacheEntry *entry);
|
2001-03-10 03:43:20 +03:00
|
|
|
|
2008-03-11 11:08:05 +03:00
|
|
|
void VisitEntries( PLDHashEnumerator etor, void *arg);
|
2007-03-23 02:01:14 +03:00
|
|
|
|
2008-03-11 11:08:05 +03:00
|
|
|
private:
|
2001-02-23 16:18:01 +03:00
|
|
|
// PLDHashTable operation callbacks
|
2008-10-10 19:04:34 +04:00
|
|
|
static PLDHashNumber HashKey( PLDHashTable *table, const void *key);
|
2001-02-23 16:18:01 +03:00
|
|
|
|
2008-10-10 19:04:34 +04:00
|
|
|
static PRBool MatchEntry( PLDHashTable * table,
|
|
|
|
const PLDHashEntryHdr * entry,
|
|
|
|
const void * key);
|
2001-02-23 16:18:01 +03:00
|
|
|
|
2008-10-10 19:04:34 +04:00
|
|
|
static void MoveEntry( PLDHashTable *table,
|
|
|
|
const PLDHashEntryHdr *from,
|
|
|
|
PLDHashEntryHdr *to);
|
2001-02-23 16:18:01 +03:00
|
|
|
|
2008-10-10 19:04:34 +04:00
|
|
|
static void ClearEntry( PLDHashTable *table, PLDHashEntryHdr *entry);
|
2001-02-23 16:18:01 +03:00
|
|
|
|
2008-10-10 19:04:34 +04:00
|
|
|
static void Finalize( PLDHashTable *table);
|
2001-02-23 16:18:01 +03:00
|
|
|
|
2001-03-01 05:46:12 +03:00
|
|
|
static
|
2008-10-10 19:04:34 +04:00
|
|
|
PLDHashOperator FreeCacheEntries(PLDHashTable * table,
|
|
|
|
PLDHashEntryHdr * hdr,
|
|
|
|
PRUint32 number,
|
|
|
|
void * arg);
|
2001-03-10 03:43:20 +03:00
|
|
|
static
|
2008-10-10 19:04:34 +04:00
|
|
|
PLDHashOperator VisitEntry(PLDHashTable * table,
|
|
|
|
PLDHashEntryHdr * hdr,
|
|
|
|
PRUint32 number,
|
|
|
|
void * arg);
|
2001-03-10 03:43:20 +03:00
|
|
|
|
2001-02-23 16:18:01 +03:00
|
|
|
// member variables
|
|
|
|
static PLDHashTableOps ops;
|
|
|
|
PLDHashTable table;
|
|
|
|
PRBool initialized;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // _nsCacheEntry_h_
|