Back to index

lightning-sunbird  0.9+nobinonly
nsCacheService.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is nsCacheService.h, released
00017  * February 10, 2001.
00018  *
00019  * The Initial Developer of the Original Code is
00020  * Netscape Communications Corporation.
00021  * Portions created by the Initial Developer are Copyright (C) 2001
00022  * the Initial Developer. All Rights Reserved.
00023  *
00024  * Contributor(s):
00025  *   Gordon Sheridan  <gordon@netscape.com>
00026  *   Patrick C. Beard <beard@netscape.com>
00027  *   Darin Fisher     <darin@netscape.com>
00028  *
00029  * Alternatively, the contents of this file may be used under the terms of
00030  * either the GNU General Public License Version 2 or later (the "GPL"), or
00031  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00032  * in which case the provisions of the GPL or the LGPL are applicable instead
00033  * of those above. If you wish to allow use of your version of this file only
00034  * under the terms of either the GPL or the LGPL, and not to allow others to
00035  * use your version of this file under the terms of the MPL, indicate your
00036  * decision by deleting the provisions above and replace them with the notice
00037  * and other provisions required by the GPL or the LGPL. If you do not delete
00038  * the provisions above, a recipient may use your version of this file under
00039  * the terms of any one of the MPL, the GPL or the LGPL.
00040  *
00041  * ***** END LICENSE BLOCK ***** */
00042 
00043 
00044 #ifndef _nsCacheService_h_
00045 #define _nsCacheService_h_
00046 
00047 #include "nsICacheService.h"
00048 #include "nsCacheSession.h"
00049 #include "nsCacheDevice.h"
00050 #include "nsCacheEntry.h"
00051 
00052 #include "nspr.h"
00053 #include "nsIObserver.h"
00054 #include "nsString.h"
00055 #include "nsIEventQueueService.h"
00056 #include "nsProxiedService.h"
00057 
00058 class nsCacheRequest;
00059 class nsCacheProfilePrefObserver;
00060 class nsDiskCacheDevice;
00061 class nsMemoryCacheDevice;
00062 
00063 
00064 /******************************************************************************
00065  *  nsCacheService
00066  ******************************************************************************/
00067 
00068 class nsCacheService : public nsICacheService
00069 {
00070 public:
00071     NS_DECL_ISUPPORTS
00072     NS_DECL_NSICACHESERVICE
00073     
00074     nsCacheService();
00075     virtual ~nsCacheService();
00076 
00077     // Define a Create method to be used with a factory:
00078     static NS_METHOD
00079     Create(nsISupports* outer, const nsIID& iid, void* *result);
00080 
00081 
00085     static nsresult  OpenCacheEntry(nsCacheSession *           session,
00086                                     const nsACString &         key,
00087                                     nsCacheAccessMode          accessRequested,
00088                                     PRBool                     blockingMode,
00089                                     nsICacheListener *         listener,
00090                                     nsICacheEntryDescriptor ** result);
00091 
00092     static nsresult  EvictEntriesForSession(nsCacheSession *   session);
00093 
00094     static nsresult  IsStorageEnabledForPolicy(nsCacheStoragePolicy  storagePolicy,
00095                                                PRBool *              result);
00096 
00101     static void      CloseDescriptor(nsCacheEntryDescriptor * descriptor);
00102 
00103     static nsresult  GetFileForEntry(nsCacheEntry *         entry,
00104                                      nsIFile **             result);
00105 
00106     static nsresult  OpenInputStreamForEntry(nsCacheEntry *     entry,
00107                                              nsCacheAccessMode  mode,
00108                                              PRUint32           offset,
00109                                              nsIInputStream **  result);
00110 
00111     static nsresult  OpenOutputStreamForEntry(nsCacheEntry *     entry,
00112                                               nsCacheAccessMode  mode,
00113                                               PRUint32           offset,
00114                                               nsIOutputStream ** result);
00115 
00116     static nsresult  OnDataSizeChange(nsCacheEntry * entry, PRInt32 deltaSize);
00117 
00118     static PRLock *  ServiceLock();
00119     
00120     static nsresult  SetCacheElement(nsCacheEntry * entry, nsISupports * element);
00121 
00122     static nsresult  ValidateEntry(nsCacheEntry * entry);
00123 
00124 
00129     static
00130     nsCacheService * GlobalInstance()   { return gService; }
00131     
00132     static nsresult  DoomEntry(nsCacheEntry * entry);
00133 
00134     static void      ProxyObjectRelease(nsISupports * object, PRThread * thread);
00135 
00136     static PRBool    IsStorageEnabledForPolicy_Locked(nsCacheStoragePolicy policy);
00137 
00141     static void      OnProfileShutdown(PRBool cleanse);
00142     static void      OnProfileChanged();
00143 
00144     static void      SetDiskCacheEnabled(PRBool  enabled);
00145     static void      SetDiskCacheCapacity(PRInt32  capacity);
00146 
00147     static void      SetMemoryCacheEnabled(PRBool  enabled);
00148     static void      SetMemoryCacheCapacity(PRInt32  capacity);
00149 
00150     nsresult         Init();
00151     void             Shutdown();
00152 private:
00153 
00158     nsresult         CreateDiskDevice();
00159     nsresult         CreateMemoryDevice();
00160 
00161     nsresult         CreateRequest(nsCacheSession *   session,
00162                                    const nsACString & clientKey,
00163                                    nsCacheAccessMode  accessRequested,
00164                                    PRBool             blockingMode,
00165                                    nsICacheListener * listener,
00166                                    nsCacheRequest **  request);
00167 
00168     nsresult         DoomEntry_Internal(nsCacheEntry * entry);
00169 
00170     nsresult         EvictEntriesForClient(const char *          clientID,
00171                                            nsCacheStoragePolicy  storagePolicy);
00172 
00173     nsresult         NotifyListener(nsCacheRequest *          request,
00174                                     nsICacheEntryDescriptor * descriptor,
00175                                     nsCacheAccessMode         accessGranted,
00176                                     nsresult                  error);
00177 
00178     nsresult         ActivateEntry(nsCacheRequest * request, nsCacheEntry ** entry);
00179 
00180     nsCacheDevice *  EnsureEntryHasDevice(nsCacheEntry * entry);
00181 
00182     nsCacheEntry *   SearchCacheDevices(nsCString * key, nsCacheStoragePolicy policy, PRBool *collision);
00183 
00184     void             DeactivateEntry(nsCacheEntry * entry);
00185 
00186     nsresult         ProcessRequest(nsCacheRequest *           request,
00187                                     PRBool                     calledFromOpenCacheEntry,
00188                                     nsICacheEntryDescriptor ** result);
00189 
00190     nsresult         ProcessPendingRequests(nsCacheEntry * entry);
00191 
00192     void             ClearPendingRequests(nsCacheEntry * entry);
00193     void             ClearDoomList(void);
00194     void             ClearActiveEntries(void);
00195     void             DoomActiveEntries(void);
00196 
00197     PRInt32         CacheMemoryAvailable();
00198 
00199     static
00200     PLDHashOperator PR_CALLBACK  DeactivateAndClearEntry(PLDHashTable *    table,
00201                                                          PLDHashEntryHdr * hdr,
00202                                                          PRUint32          number,
00203                                                          void *            arg);
00204     static
00205     PLDHashOperator PR_CALLBACK  RemoveActiveEntry(PLDHashTable *    table,
00206                                                    PLDHashEntryHdr * hdr,
00207                                                    PRUint32          number,
00208                                                    void *            arg);
00209 #if defined(PR_LOGGING)
00210     void LogCacheStatistics();
00211 #endif
00212 
00217     static nsCacheService *         gService;  // there can be only one...
00218     nsCOMPtr<nsIEventQueueService>  mEventQService;
00219     nsCOMPtr<nsIProxyObjectManager> mProxyObjectManager;
00220     
00221     nsCacheProfilePrefObserver *    mObserver;
00222     
00223     PRLock *                        mCacheServiceLock;
00224     
00225     PRBool                          mInitialized;
00226     
00227     PRBool                          mEnableMemoryDevice;
00228     PRBool                          mEnableDiskDevice;
00229 
00230     nsMemoryCacheDevice *           mMemoryDevice;
00231     nsDiskCacheDevice *             mDiskDevice;
00232 
00233     nsCacheEntryHashTable           mActiveEntries;
00234     PRCList                         mDoomedEntries;
00235 
00236     // stats
00237     
00238     PRUint32                        mTotalEntries;
00239     PRUint32                        mCacheHits;
00240     PRUint32                        mCacheMisses;
00241     PRUint32                        mMaxKeyLength;
00242     PRUint32                        mMaxDataSize;
00243     PRUint32                        mMaxMetaSize;
00244 
00245     // Unexpected error totals
00246     PRUint32                        mDeactivateFailures;
00247     PRUint32                        mDeactivatedUnboundEntries;
00248 };
00249 
00250 
00251 #endif // _nsCacheService_h_