Follow-on fix for bug 457825. Use sheet principal for agent and user sheets. r=dbaron...
[wine-gecko.git] / netwerk / cache / src / nsCacheService.h
blobc277a45687d467a0350d6603524b8d557a4ab74e
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
14 * License.
16 * The Original Code is nsCacheService.h, released
17 * February 10, 2001.
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.
24 * Contributor(s):
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"
53 #include "prlock.h"
54 #include "prthread.h"
55 #include "nsIObserver.h"
56 #include "nsString.h"
57 #include "nsProxiedService.h"
58 #include "nsTArray.h"
60 class nsCacheRequest;
61 class nsCacheProfilePrefObserver;
62 class nsDiskCacheDevice;
63 class nsMemoryCacheDevice;
64 class nsOfflineCacheDevice;
65 class nsCacheServiceAutoLock;
68 /******************************************************************************
69 * nsCacheService
70 ******************************************************************************/
72 class nsCacheService : public nsICacheService
74 public:
75 NS_DECL_ISUPPORTS
76 NS_DECL_NSICACHESERVICE
78 nsCacheService();
79 virtual ~nsCacheService();
81 // Define a Create method to be used with a factory:
82 static NS_METHOD
83 Create(nsISupports* outer, const nsIID& iid, void* *result);
86 /**
87 * Methods called by nsCacheSession
89 static nsresult OpenCacheEntry(nsCacheSession * session,
90 const nsACString & key,
91 nsCacheAccessMode accessRequested,
92 PRBool blockingMode,
93 nsICacheListener * listener,
94 nsICacheEntryDescriptor ** result);
96 static nsresult EvictEntriesForSession(nsCacheSession * session);
98 static nsresult IsStorageEnabledForPolicy(nsCacheStoragePolicy storagePolicy,
99 PRBool * result);
102 * Methods called by nsCacheEntryDescriptor
105 static void CloseDescriptor(nsCacheEntryDescriptor * descriptor);
107 static nsresult GetFileForEntry(nsCacheEntry * entry,
108 nsIFile ** result);
110 static nsresult OpenInputStreamForEntry(nsCacheEntry * entry,
111 nsCacheAccessMode mode,
112 PRUint32 offset,
113 nsIInputStream ** result);
115 static nsresult OpenOutputStreamForEntry(nsCacheEntry * entry,
116 nsCacheAccessMode mode,
117 PRUint32 offset,
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
131 static
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);
147 * Methods called by nsCacheProfilePrefObserver
149 static void OnProfileShutdown(PRBool cleanse);
150 static void OnProfileChanged();
152 static void SetDiskCacheEnabled(PRBool enabled);
153 static void SetDiskCacheCapacity(PRInt32 capacity);
155 static void SetOfflineCacheEnabled(PRBool enabled);
156 static void SetOfflineCacheCapacity(PRInt32 capacity);
158 static void SetMemoryCache();
160 static void OnEnterExitPrivateBrowsing();
162 nsresult Init();
163 void Shutdown();
164 private:
165 friend class nsCacheServiceAutoLock;
166 friend class nsOfflineCacheDevice;
169 * Internal Methods
172 static void Lock();
173 static void Unlock();
175 nsresult CreateDiskDevice();
176 nsresult CreateOfflineDevice();
177 nsresult CreateMemoryDevice();
179 nsresult CreateRequest(nsCacheSession * session,
180 const nsACString & clientKey,
181 nsCacheAccessMode accessRequested,
182 PRBool blockingMode,
183 nsICacheListener * listener,
184 nsCacheRequest ** request);
186 nsresult DoomEntry_Internal(nsCacheEntry * entry);
188 nsresult EvictEntriesForClient(const char * clientID,
189 nsCacheStoragePolicy storagePolicy);
191 // Notifies request listener asynchronously on the request's thread, and
192 // releases the descriptor on the request's thread. If this method fails,
193 // the descriptor is not released.
194 nsresult NotifyListener(nsCacheRequest * request,
195 nsICacheEntryDescriptor * descriptor,
196 nsCacheAccessMode accessGranted,
197 nsresult error);
199 nsresult ActivateEntry(nsCacheRequest * request, nsCacheEntry ** entry);
201 nsCacheDevice * EnsureEntryHasDevice(nsCacheEntry * entry);
203 nsCacheEntry * SearchCacheDevices(nsCString * key, nsCacheStoragePolicy policy, PRBool *collision);
205 void DeactivateEntry(nsCacheEntry * entry);
207 nsresult ProcessRequest(nsCacheRequest * request,
208 PRBool calledFromOpenCacheEntry,
209 nsICacheEntryDescriptor ** result);
211 nsresult ProcessPendingRequests(nsCacheEntry * entry);
213 void ClearPendingRequests(nsCacheEntry * entry);
214 void ClearDoomList(void);
215 void ClearActiveEntries(void);
216 void DoomActiveEntries(void);
218 static
219 PLDHashOperator DeactivateAndClearEntry(PLDHashTable * table,
220 PLDHashEntryHdr * hdr,
221 PRUint32 number,
222 void * arg);
223 static
224 PLDHashOperator RemoveActiveEntry(PLDHashTable * table,
225 PLDHashEntryHdr * hdr,
226 PRUint32 number,
227 void * arg);
228 #if defined(PR_LOGGING)
229 void LogCacheStatistics();
230 #endif
233 * Data Members
236 static nsCacheService * gService; // there can be only one...
238 nsCacheProfilePrefObserver * mObserver;
240 PRLock * mLock;
242 #if defined(DEBUG)
243 PRThread * mLockedThread; // The thread holding mLock
244 #endif
246 nsTArray<nsISupports*> mDoomedObjects;
248 PRBool mInitialized;
250 PRBool mEnableMemoryDevice;
251 PRBool mEnableDiskDevice;
252 PRBool mEnableOfflineDevice;
254 nsMemoryCacheDevice * mMemoryDevice;
255 nsDiskCacheDevice * mDiskDevice;
256 nsOfflineCacheDevice * mOfflineDevice;
258 nsCacheEntryHashTable mActiveEntries;
259 PRCList mDoomedEntries;
261 // stats
263 PRUint32 mTotalEntries;
264 PRUint32 mCacheHits;
265 PRUint32 mCacheMisses;
266 PRUint32 mMaxKeyLength;
267 PRUint32 mMaxDataSize;
268 PRUint32 mMaxMetaSize;
270 // Unexpected error totals
271 PRUint32 mDeactivateFailures;
272 PRUint32 mDeactivatedUnboundEntries;
275 /******************************************************************************
276 * nsCacheServiceAutoLock
277 ******************************************************************************/
279 // Instantiate this class to acquire the cache service lock for a particular
280 // execution scope.
281 class nsCacheServiceAutoLock {
282 public:
283 nsCacheServiceAutoLock() {
284 nsCacheService::Lock();
286 ~nsCacheServiceAutoLock() {
287 nsCacheService::Unlock();
291 #endif // _nsCacheService_h_