2001-02-23 13:18:01 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
*
|
2004-04-18 22:01:16 +00: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 nsCacheService.h, released
|
|
|
|
* February 10, 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>
|
|
|
|
* Patrick C. Beard <beard@netscape.com>
|
|
|
|
* Darin Fisher <darin@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 13:18:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
#ifndef _nsCacheService_h_
|
|
|
|
#define _nsCacheService_h_
|
|
|
|
|
|
|
|
#include "nsICacheService.h"
|
2001-02-26 14:55:58 +00:00
|
|
|
#include "nsCacheSession.h"
|
2001-02-23 13:18:01 +00:00
|
|
|
#include "nsCacheDevice.h"
|
|
|
|
#include "nsCacheEntry.h"
|
2001-05-03 02:35:26 +00:00
|
|
|
|
|
|
|
#include "nspr.h"
|
2001-03-13 01:59:43 +00:00
|
|
|
#include "nsIObserver.h"
|
2001-03-20 22:42:03 +00:00
|
|
|
#include "nsString.h"
|
2001-05-03 02:35:26 +00:00
|
|
|
#include "nsIEventQueueService.h"
|
|
|
|
#include "nsProxiedService.h"
|
2001-02-23 13:18:01 +00:00
|
|
|
|
2001-02-26 15:53:31 +00:00
|
|
|
class nsCacheRequest;
|
2001-06-28 01:30:26 +00:00
|
|
|
class nsCacheProfilePrefObserver;
|
|
|
|
class nsDiskCacheDevice;
|
|
|
|
class nsMemoryCacheDevice;
|
2001-02-23 13:18:01 +00:00
|
|
|
|
|
|
|
|
2001-03-20 22:42:03 +00:00
|
|
|
/******************************************************************************
|
2001-02-23 13:18:01 +00:00
|
|
|
* nsCacheService
|
2001-03-20 22:42:03 +00:00
|
|
|
******************************************************************************/
|
2001-02-23 13:18:01 +00:00
|
|
|
|
2001-06-28 01:30:26 +00:00
|
|
|
class nsCacheService : public nsICacheService
|
2001-02-23 13:18:01 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSICACHESERVICE
|
|
|
|
|
|
|
|
nsCacheService();
|
|
|
|
virtual ~nsCacheService();
|
|
|
|
|
|
|
|
// Define a Create method to be used with a factory:
|
|
|
|
static NS_METHOD
|
|
|
|
Create(nsISupports* outer, const nsIID& iid, void* *result);
|
|
|
|
|
2001-02-26 14:55:58 +00:00
|
|
|
|
2001-02-28 04:10:43 +00:00
|
|
|
/**
|
|
|
|
* Methods called by nsCacheSession
|
|
|
|
*/
|
2002-08-07 01:13:29 +00:00
|
|
|
static nsresult OpenCacheEntry(nsCacheSession * session,
|
2001-03-05 10:45:33 +00:00
|
|
|
const char * key,
|
|
|
|
nsCacheAccessMode accessRequested,
|
2001-05-09 03:36:00 +00:00
|
|
|
PRBool blockingMode,
|
2001-03-05 10:45:33 +00:00
|
|
|
nsICacheListener * listener,
|
|
|
|
nsICacheEntryDescriptor ** result);
|
2001-03-05 07:17:58 +00:00
|
|
|
|
2002-08-07 01:13:29 +00:00
|
|
|
static nsresult EvictEntriesForSession(nsCacheSession * session);
|
2001-04-09 08:14:08 +00:00
|
|
|
|
2002-01-24 01:25:25 +00:00
|
|
|
static nsresult IsStorageEnabledForPolicy(nsCacheStoragePolicy storagePolicy,
|
|
|
|
PRBool * result);
|
|
|
|
|
2001-02-28 04:10:43 +00:00
|
|
|
/**
|
|
|
|
* Methods called by nsCacheEntryDescriptor
|
|
|
|
*/
|
|
|
|
|
2002-08-07 01:13:29 +00:00
|
|
|
static void CloseDescriptor(nsCacheEntryDescriptor * descriptor);
|
2001-03-01 05:01:43 +00:00
|
|
|
|
2002-08-07 01:13:29 +00:00
|
|
|
static nsresult GetFileForEntry(nsCacheEntry * entry,
|
|
|
|
nsIFile ** result);
|
2001-03-02 01:51:41 +00:00
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
static nsresult OpenInputStreamForEntry(nsCacheEntry * entry,
|
|
|
|
nsCacheAccessMode mode,
|
|
|
|
PRUint32 offset,
|
|
|
|
nsIInputStream ** result);
|
|
|
|
|
|
|
|
static nsresult OpenOutputStreamForEntry(nsCacheEntry * entry,
|
|
|
|
nsCacheAccessMode mode,
|
|
|
|
PRUint32 offset,
|
|
|
|
nsIOutputStream ** result);
|
2001-03-01 05:01:43 +00:00
|
|
|
|
2002-08-07 01:13:29 +00:00
|
|
|
static nsresult OnDataSizeChange(nsCacheEntry * entry, PRInt32 deltaSize);
|
|
|
|
|
|
|
|
static PRLock * ServiceLock();
|
|
|
|
|
|
|
|
static nsresult SetCacheElement(nsCacheEntry * entry, nsISupports * element);
|
|
|
|
|
|
|
|
static nsresult ValidateEntry(nsCacheEntry * entry);
|
2001-02-26 14:55:58 +00:00
|
|
|
|
2001-03-08 21:22:18 +00:00
|
|
|
|
2001-02-28 04:10:43 +00:00
|
|
|
/**
|
|
|
|
* Methods called by any cache classes
|
|
|
|
*/
|
|
|
|
|
|
|
|
static
|
2002-08-07 01:13:29 +00:00
|
|
|
nsCacheService * GlobalInstance() { return gService; }
|
|
|
|
|
|
|
|
static nsresult DoomEntry(nsCacheEntry * entry);
|
2001-02-28 04:10:43 +00:00
|
|
|
|
2002-08-07 01:13:29 +00:00
|
|
|
static void ProxyObjectRelease(nsISupports * object, PRThread * thread);
|
2001-03-10 00:42:19 +00:00
|
|
|
|
2002-08-07 01:13:29 +00:00
|
|
|
static PRBool IsStorageEnabledForPolicy_Locked(nsCacheStoragePolicy policy);
|
2001-06-06 02:30:07 +00:00
|
|
|
|
2001-03-20 22:42:03 +00:00
|
|
|
/**
|
2001-06-28 01:30:26 +00:00
|
|
|
* Methods called by nsCacheProfilePrefObserver
|
2001-03-20 22:42:03 +00:00
|
|
|
*/
|
2001-08-17 00:01:01 +00:00
|
|
|
static void OnProfileShutdown(PRBool cleanse);
|
|
|
|
static void OnProfileChanged();
|
2001-06-28 01:30:26 +00:00
|
|
|
|
|
|
|
static void SetDiskCacheEnabled(PRBool enabled);
|
|
|
|
static void SetDiskCacheCapacity(PRInt32 capacity);
|
|
|
|
|
|
|
|
static void SetMemoryCacheEnabled(PRBool enabled);
|
|
|
|
static void SetMemoryCacheCapacity(PRInt32 capacity);
|
2001-03-20 22:42:03 +00:00
|
|
|
|
2004-10-31 21:58:51 +00:00
|
|
|
nsresult Init();
|
|
|
|
void Shutdown();
|
2001-02-23 13:18:01 +00:00
|
|
|
private:
|
|
|
|
|
2001-02-28 04:10:43 +00:00
|
|
|
/**
|
|
|
|
* Internal Methods
|
|
|
|
*/
|
2001-03-23 21:06:39 +00:00
|
|
|
|
|
|
|
nsresult CreateDiskDevice();
|
2001-03-29 05:54:58 +00:00
|
|
|
nsresult CreateMemoryDevice();
|
2001-03-23 21:06:39 +00:00
|
|
|
|
2001-03-05 10:45:33 +00:00
|
|
|
nsresult CreateRequest(nsCacheSession * session,
|
|
|
|
const char * clientKey,
|
|
|
|
nsCacheAccessMode accessRequested,
|
2001-05-09 03:36:00 +00:00
|
|
|
PRBool blockingMode,
|
2001-03-05 10:45:33 +00:00
|
|
|
nsICacheListener * listener,
|
|
|
|
nsCacheRequest ** request);
|
|
|
|
|
2002-08-07 01:13:29 +00:00
|
|
|
nsresult DoomEntry_Internal(nsCacheEntry * entry);
|
|
|
|
|
|
|
|
nsresult EvictEntriesForClient(const char * clientID,
|
|
|
|
nsCacheStoragePolicy storagePolicy);
|
|
|
|
|
2001-03-05 10:45:33 +00:00
|
|
|
nsresult NotifyListener(nsCacheRequest * request,
|
|
|
|
nsICacheEntryDescriptor * descriptor,
|
|
|
|
nsCacheAccessMode accessGranted,
|
|
|
|
nsresult error);
|
|
|
|
|
|
|
|
nsresult ActivateEntry(nsCacheRequest * request, nsCacheEntry ** entry);
|
|
|
|
|
|
|
|
nsCacheDevice * EnsureEntryHasDevice(nsCacheEntry * entry);
|
|
|
|
|
|
|
|
nsCacheEntry * SearchCacheDevices(nsCString * key, nsCacheStoragePolicy policy);
|
2001-02-26 14:55:58 +00:00
|
|
|
|
2001-03-05 10:45:33 +00:00
|
|
|
void DeactivateEntry(nsCacheEntry * entry);
|
2001-02-23 13:18:01 +00:00
|
|
|
|
2001-05-09 03:36:00 +00:00
|
|
|
nsresult ProcessRequest(nsCacheRequest * request,
|
|
|
|
PRBool calledFromOpenCacheEntry,
|
2001-03-05 10:45:33 +00:00
|
|
|
nsICacheEntryDescriptor ** result);
|
2001-02-24 01:02:37 +00:00
|
|
|
|
2001-03-05 10:45:33 +00:00
|
|
|
nsresult ProcessPendingRequests(nsCacheEntry * entry);
|
2001-02-24 01:02:37 +00:00
|
|
|
|
2001-03-13 15:43:17 +00:00
|
|
|
void ClearPendingRequests(nsCacheEntry * entry);
|
|
|
|
void ClearDoomList(void);
|
|
|
|
void ClearActiveEntries(void);
|
2001-09-17 20:47:09 +00:00
|
|
|
void DoomActiveEntries(void);
|
2001-03-13 15:43:17 +00:00
|
|
|
|
2003-04-23 05:15:48 +00:00
|
|
|
PRInt32 CacheMemoryAvailable();
|
2002-06-03 20:05:53 +00:00
|
|
|
|
2001-03-13 15:43:17 +00:00
|
|
|
static
|
2001-03-29 05:54:58 +00:00
|
|
|
PLDHashOperator PR_CALLBACK DeactivateAndClearEntry(PLDHashTable * table,
|
2001-03-26 23:21:53 +00:00
|
|
|
PLDHashEntryHdr * hdr,
|
|
|
|
PRUint32 number,
|
|
|
|
void * arg);
|
2001-09-17 20:47:09 +00:00
|
|
|
static
|
|
|
|
PLDHashOperator PR_CALLBACK RemoveActiveEntry(PLDHashTable * table,
|
|
|
|
PLDHashEntryHdr * hdr,
|
|
|
|
PRUint32 number,
|
|
|
|
void * arg);
|
2001-04-04 03:30:45 +00:00
|
|
|
#if defined(PR_LOGGING)
|
|
|
|
void LogCacheStatistics();
|
|
|
|
#endif
|
2001-05-08 00:13:21 +00:00
|
|
|
|
2001-02-23 13:18:01 +00:00
|
|
|
/**
|
|
|
|
* Data Members
|
|
|
|
*/
|
|
|
|
|
2001-06-28 01:30:26 +00:00
|
|
|
static nsCacheService * gService; // there can be only one...
|
|
|
|
nsCOMPtr<nsIEventQueueService> mEventQService;
|
|
|
|
nsCOMPtr<nsIProxyObjectManager> mProxyObjectManager;
|
|
|
|
|
|
|
|
nsCacheProfilePrefObserver * mObserver;
|
2001-05-03 02:35:26 +00:00
|
|
|
|
2001-06-28 01:30:26 +00:00
|
|
|
PRLock * mCacheServiceLock;
|
2001-02-27 05:35:53 +00:00
|
|
|
|
2002-08-07 01:13:29 +00:00
|
|
|
PRBool mInitialized;
|
|
|
|
|
2001-06-28 01:30:26 +00:00
|
|
|
PRBool mEnableMemoryDevice;
|
|
|
|
PRBool mEnableDiskDevice;
|
2001-03-29 05:54:58 +00:00
|
|
|
|
2001-06-28 01:30:26 +00:00
|
|
|
nsMemoryCacheDevice * mMemoryDevice;
|
|
|
|
nsDiskCacheDevice * mDiskDevice;
|
2001-02-27 05:35:53 +00:00
|
|
|
|
2001-06-28 01:30:26 +00:00
|
|
|
nsCacheEntryHashTable mActiveEntries;
|
|
|
|
PRCList mDoomedEntries;
|
2001-03-02 01:51:41 +00:00
|
|
|
|
2001-03-13 15:43:17 +00:00
|
|
|
// stats
|
2001-04-04 03:30:45 +00:00
|
|
|
|
2001-06-28 01:30:26 +00:00
|
|
|
PRUint32 mTotalEntries;
|
|
|
|
PRUint32 mCacheHits;
|
|
|
|
PRUint32 mCacheMisses;
|
|
|
|
PRUint32 mMaxKeyLength;
|
|
|
|
PRUint32 mMaxDataSize;
|
|
|
|
PRUint32 mMaxMetaSize;
|
2001-03-13 15:43:17 +00:00
|
|
|
|
2001-03-02 01:51:41 +00:00
|
|
|
// Unexpected error totals
|
2001-06-28 01:30:26 +00:00
|
|
|
PRUint32 mDeactivateFailures;
|
|
|
|
PRUint32 mDeactivatedUnboundEntries;
|
2001-02-23 13:18:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif // _nsCacheService_h_
|