1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
16 * The Original Code is nsCacheService.h, released
19 * The Initial Developer of the Original Code is
20 * Netscape Communications Corporation.
21 * Portions created by the Initial Developer are Copyright (C) 2001
22 * the Initial Developer. All Rights Reserved.
25 * Gordon Sheridan <gordon@netscape.com>
26 * Patrick C. Beard <beard@netscape.com>
27 * Darin Fisher <darin@netscape.com>
28 * Ehsan Akhgari <ehsan.akhgari@gmail.com>
30 * Alternatively, the contents of this file may be used under the terms of
31 * either the GNU General Public License Version 2 or later (the "GPL"), or
32 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
33 * in which case the provisions of the GPL or the LGPL are applicable instead
34 * of those above. If you wish to allow use of your version of this file only
35 * under the terms of either the GPL or the LGPL, and not to allow others to
36 * use your version of this file under the terms of the MPL, indicate your
37 * decision by deleting the provisions above and replace them with the notice
38 * and other provisions required by the GPL or the LGPL. If you do not delete
39 * the provisions above, a recipient may use your version of this file under
40 * the terms of any one of the MPL, the GPL or the LGPL.
42 * ***** END LICENSE BLOCK ***** */
45 #ifndef _nsCacheService_h_
46 #define _nsCacheService_h_
48 #include "nsICacheService.h"
49 #include "nsCacheSession.h"
50 #include "nsCacheDevice.h"
51 #include "nsCacheEntry.h"
55 #include "nsIObserver.h"
57 #include "nsProxiedService.h"
61 class nsCacheProfilePrefObserver
;
62 class nsDiskCacheDevice
;
63 class nsMemoryCacheDevice
;
64 class nsOfflineCacheDevice
;
65 class nsCacheServiceAutoLock
;
68 /******************************************************************************
70 ******************************************************************************/
72 class nsCacheService
: public nsICacheService
76 NS_DECL_NSICACHESERVICE
79 virtual ~nsCacheService();
81 // Define a Create method to be used with a factory:
83 Create(nsISupports
* outer
, const nsIID
& iid
, void* *result
);
87 * Methods called by nsCacheSession
89 static nsresult
OpenCacheEntry(nsCacheSession
* session
,
90 const nsACString
& key
,
91 nsCacheAccessMode accessRequested
,
93 nsICacheListener
* listener
,
94 nsICacheEntryDescriptor
** result
);
96 static nsresult
EvictEntriesForSession(nsCacheSession
* session
);
98 static nsresult
IsStorageEnabledForPolicy(nsCacheStoragePolicy storagePolicy
,
102 * Methods called by nsCacheEntryDescriptor
105 static void CloseDescriptor(nsCacheEntryDescriptor
* descriptor
);
107 static nsresult
GetFileForEntry(nsCacheEntry
* entry
,
110 static nsresult
OpenInputStreamForEntry(nsCacheEntry
* entry
,
111 nsCacheAccessMode mode
,
113 nsIInputStream
** result
);
115 static nsresult
OpenOutputStreamForEntry(nsCacheEntry
* entry
,
116 nsCacheAccessMode mode
,
118 nsIOutputStream
** result
);
120 static nsresult
OnDataSizeChange(nsCacheEntry
* entry
, PRInt32 deltaSize
);
122 static nsresult
SetCacheElement(nsCacheEntry
* entry
, nsISupports
* element
);
124 static nsresult
ValidateEntry(nsCacheEntry
* entry
);
128 * Methods called by any cache classes
132 nsCacheService
* GlobalInstance() { return gService
; }
134 static nsresult
DoomEntry(nsCacheEntry
* entry
);
136 static PRBool
IsStorageEnabledForPolicy_Locked(nsCacheStoragePolicy policy
);
138 // This method may be called to release an object while the cache service
139 // lock is being held. If a non-null target is specified and the target
140 // does not correspond to the current thread, then the release will be
141 // proxied to the specified target. Otherwise, the object will be added to
142 // the list of objects to be released when the cache service is unlocked.
143 static void ReleaseObject_Locked(nsISupports
* object
,
144 nsIEventTarget
* target
= nsnull
);
146 static nsresult
DispatchToCacheIOThread(nsIRunnable
* event
);
150 * Methods called by nsCacheProfilePrefObserver
152 static void OnProfileShutdown(PRBool cleanse
);
153 static void OnProfileChanged();
155 static void SetDiskCacheEnabled(PRBool enabled
);
156 // Sets the disk cache capacity (in kilobytes)
157 static void SetDiskCacheCapacity(PRInt32 capacity
);
159 static void SetOfflineCacheEnabled(PRBool enabled
);
160 // Sets the offline cache capacity (in kilobytes)
161 static void SetOfflineCacheCapacity(PRInt32 capacity
);
163 static void SetMemoryCache();
165 static void OnEnterExitPrivateBrowsing();
170 friend class nsCacheServiceAutoLock
;
171 friend class nsOfflineCacheDevice
;
172 friend class nsProcessRequestEvent
;
173 friend class nsSetSmartSizeEvent
;
180 static void Unlock();
182 nsresult
CreateDiskDevice();
183 nsresult
CreateOfflineDevice();
184 nsresult
CreateMemoryDevice();
186 nsresult
CreateRequest(nsCacheSession
* session
,
187 const nsACString
& clientKey
,
188 nsCacheAccessMode accessRequested
,
190 nsICacheListener
* listener
,
191 nsCacheRequest
** request
);
193 nsresult
DoomEntry_Internal(nsCacheEntry
* entry
);
195 nsresult
EvictEntriesForClient(const char * clientID
,
196 nsCacheStoragePolicy storagePolicy
);
198 // Notifies request listener asynchronously on the request's thread, and
199 // releases the descriptor on the request's thread. If this method fails,
200 // the descriptor is not released.
201 nsresult
NotifyListener(nsCacheRequest
* request
,
202 nsICacheEntryDescriptor
* descriptor
,
203 nsCacheAccessMode accessGranted
,
206 nsresult
ActivateEntry(nsCacheRequest
* request
, nsCacheEntry
** entry
);
208 nsCacheDevice
* EnsureEntryHasDevice(nsCacheEntry
* entry
);
210 nsCacheEntry
* SearchCacheDevices(nsCString
* key
, nsCacheStoragePolicy policy
, PRBool
*collision
);
212 void DeactivateEntry(nsCacheEntry
* entry
);
214 nsresult
ProcessRequest(nsCacheRequest
* request
,
215 PRBool calledFromOpenCacheEntry
,
216 nsICacheEntryDescriptor
** result
);
218 nsresult
ProcessPendingRequests(nsCacheEntry
* entry
);
220 void ClearPendingRequests(nsCacheEntry
* entry
);
221 void ClearDoomList(void);
222 void ClearActiveEntries(void);
223 void DoomActiveEntries(void);
226 PLDHashOperator
DeactivateAndClearEntry(PLDHashTable
* table
,
227 PLDHashEntryHdr
* hdr
,
231 PLDHashOperator
RemoveActiveEntry(PLDHashTable
* table
,
232 PLDHashEntryHdr
* hdr
,
235 #if defined(PR_LOGGING)
236 void LogCacheStatistics();
243 static nsCacheService
* gService
; // there can be only one...
245 nsCacheProfilePrefObserver
* mObserver
;
250 PRThread
* mLockedThread
; // The thread holding mLock
253 nsCOMPtr
<nsIThread
> mCacheIOThread
;
255 nsTArray
<nsISupports
*> mDoomedObjects
;
259 PRBool mEnableMemoryDevice
;
260 PRBool mEnableDiskDevice
;
261 PRBool mEnableOfflineDevice
;
263 nsMemoryCacheDevice
* mMemoryDevice
;
264 nsDiskCacheDevice
* mDiskDevice
;
265 nsOfflineCacheDevice
* mOfflineDevice
;
267 nsCacheEntryHashTable mActiveEntries
;
268 PRCList mDoomedEntries
;
272 PRUint32 mTotalEntries
;
274 PRUint32 mCacheMisses
;
275 PRUint32 mMaxKeyLength
;
276 PRUint32 mMaxDataSize
;
277 PRUint32 mMaxMetaSize
;
279 // Unexpected error totals
280 PRUint32 mDeactivateFailures
;
281 PRUint32 mDeactivatedUnboundEntries
;
284 /******************************************************************************
285 * nsCacheServiceAutoLock
286 ******************************************************************************/
288 // Instantiate this class to acquire the cache service lock for a particular
290 class nsCacheServiceAutoLock
{
292 nsCacheServiceAutoLock() {
293 nsCacheService::Lock();
295 ~nsCacheServiceAutoLock() {
296 nsCacheService::Unlock();
300 #endif // _nsCacheService_h_