Bug 594821 - Trigger a sync paint on intial window show. r=roc, a=final.
[mozilla-central.git] / netwerk / cache / nsCacheService.h
blob80d9f5450aeb9beb499b8edab24102c4b5e4297c
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 nsresult
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);
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();
167 nsresult Init();
168 void Shutdown();
169 private:
170 friend class nsCacheServiceAutoLock;
171 friend class nsOfflineCacheDevice;
172 friend class nsProcessRequestEvent;
173 friend class nsSetSmartSizeEvent;
176 * Internal Methods
179 static void Lock();
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,
189 PRBool blockingMode,
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,
204 nsresult error);
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);
225 static
226 PLDHashOperator DeactivateAndClearEntry(PLDHashTable * table,
227 PLDHashEntryHdr * hdr,
228 PRUint32 number,
229 void * arg);
230 static
231 PLDHashOperator RemoveActiveEntry(PLDHashTable * table,
232 PLDHashEntryHdr * hdr,
233 PRUint32 number,
234 void * arg);
235 #if defined(PR_LOGGING)
236 void LogCacheStatistics();
237 #endif
240 * Data Members
243 static nsCacheService * gService; // there can be only one...
245 nsCacheProfilePrefObserver * mObserver;
247 PRLock * mLock;
249 #if defined(DEBUG)
250 PRThread * mLockedThread; // The thread holding mLock
251 #endif
253 nsCOMPtr<nsIThread> mCacheIOThread;
255 nsTArray<nsISupports*> mDoomedObjects;
257 PRBool mInitialized;
259 PRBool mEnableMemoryDevice;
260 PRBool mEnableDiskDevice;
261 PRBool mEnableOfflineDevice;
263 nsMemoryCacheDevice * mMemoryDevice;
264 nsDiskCacheDevice * mDiskDevice;
265 nsOfflineCacheDevice * mOfflineDevice;
267 nsCacheEntryHashTable mActiveEntries;
268 PRCList mDoomedEntries;
270 // stats
272 PRUint32 mTotalEntries;
273 PRUint32 mCacheHits;
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
289 // execution scope.
290 class nsCacheServiceAutoLock {
291 public:
292 nsCacheServiceAutoLock() {
293 nsCacheService::Lock();
295 ~nsCacheServiceAutoLock() {
296 nsCacheService::Unlock();
300 #endif // _nsCacheService_h_