1 /* vim:set ts=2 sw=2 sts=2 et cin: */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is Mozilla.
17 * The Initial Developer of the Original Code is IBM Corporation.
18 * Portions created by IBM Corporation are Copyright (C) 2004
19 * IBM Corporation. All Rights Reserved.
22 * Darin Fisher <darin@meer.net>
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #ifndef nsOfflineCacheDevice_h__
39 #define nsOfflineCacheDevice_h__
41 #include "nsCacheDevice.h"
42 #include "nsIApplicationCache.h"
43 #include "nsIApplicationCacheService.h"
44 #include "nsILocalFile.h"
45 #include "nsIObserver.h"
46 #include "mozIStorageConnection.h"
47 #include "mozIStorageFunction.h"
49 #include "nsAutoPtr.h"
51 #include "nsCOMArray.h"
52 #include "nsVoidArray.h"
53 #include "nsInterfaceHashtable.h"
54 #include "nsClassHashtable.h"
55 #include "nsHashSets.h"
56 #include "nsWeakReference.h"
59 class nsOfflineCacheDevice
;
61 class nsApplicationCacheNamespace
: public nsIApplicationCacheNamespace
65 NS_DECL_NSIAPPLICATIONCACHENAMESPACE
67 nsApplicationCacheNamespace() : mItemType(0) {}
71 nsCString mNamespaceSpec
;
75 class nsOfflineCacheEvictionFunction
: public mozIStorageFunction
{
78 NS_DECL_MOZISTORAGEFUNCTION
80 nsOfflineCacheEvictionFunction(nsOfflineCacheDevice
*device
)
84 void Reset() { mItems
.Clear(); }
88 nsOfflineCacheDevice
*mDevice
;
89 nsCOMArray
<nsIFile
> mItems
;
93 class nsOfflineCacheDevice
: public nsCacheDevice
94 , public nsIApplicationCacheService
97 nsOfflineCacheDevice();
100 NS_DECL_NSIAPPLICATIONCACHESERVICE
103 * nsCacheDevice methods
106 virtual ~nsOfflineCacheDevice();
108 static nsOfflineCacheDevice
*GetInstance();
110 virtual nsresult
Init();
111 virtual nsresult
Shutdown();
113 virtual const char * GetDeviceID(void);
114 virtual nsCacheEntry
* FindEntry(nsCString
* key
, PRBool
*collision
);
115 virtual nsresult
DeactivateEntry(nsCacheEntry
* entry
);
116 virtual nsresult
BindEntry(nsCacheEntry
* entry
);
117 virtual void DoomEntry( nsCacheEntry
* entry
);
119 virtual nsresult
OpenInputStreamForEntry(nsCacheEntry
* entry
,
120 nsCacheAccessMode mode
,
122 nsIInputStream
** result
);
124 virtual nsresult
OpenOutputStreamForEntry(nsCacheEntry
* entry
,
125 nsCacheAccessMode mode
,
127 nsIOutputStream
** result
);
129 virtual nsresult
GetFileForEntry(nsCacheEntry
* entry
,
132 virtual nsresult
OnDataSizeChange(nsCacheEntry
* entry
, PRInt32 deltaSize
);
134 virtual nsresult
Visit(nsICacheVisitor
* visitor
);
136 virtual nsresult
EvictEntries(const char * clientID
);
139 /* Entry ownership */
140 nsresult
GetOwnerDomains(const char * clientID
,
143 nsresult
GetOwnerURIs(const char * clientID
,
144 const nsACString
& ownerDomain
,
147 nsresult
SetOwnedKeys(const char * clientID
,
148 const nsACString
& ownerDomain
,
149 const nsACString
& ownerUrl
,
152 nsresult
GetOwnedKeys(const char * clientID
,
153 const nsACString
& ownerDomain
,
154 const nsACString
& ownerUrl
,
157 nsresult
AddOwnedKey(const char * clientID
,
158 const nsACString
& ownerDomain
,
159 const nsACString
& ownerURI
,
160 const nsACString
& key
);
161 nsresult
RemoveOwnedKey(const char * clientID
,
162 const nsACString
& ownerDomain
,
163 const nsACString
& ownerURI
,
164 const nsACString
& key
);
165 nsresult
KeyIsOwned(const char * clientID
,
166 const nsACString
& ownerDomain
,
167 const nsACString
& ownerURI
,
168 const nsACString
& key
,
171 nsresult
ClearKeysOwnedByDomain(const char *clientID
,
172 const nsACString
&ownerDomain
);
173 nsresult
EvictUnownedEntries(const char *clientID
);
175 nsresult
ActivateCache(const nsCSubstring
&group
,
176 const nsCSubstring
&clientID
);
177 PRBool
IsActiveCache(const nsCSubstring
&group
,
178 const nsCSubstring
&clientID
);
179 nsresult
GetGroupForCache(const nsCSubstring
&clientID
,
183 * Preference accessors
186 void SetCacheParentDirectory(nsILocalFile
* parentDir
);
187 void SetCapacity(PRUint32 capacity
);
189 nsILocalFile
* CacheDirectory() { return mCacheDirectory
; }
190 PRUint32
CacheCapacity() { return mCacheCapacity
; }
191 PRUint32
CacheSize();
192 PRUint32
EntryCount();
195 friend class nsApplicationCache
;
197 static PLDHashOperator
ShutdownApplicationCache(const nsACString
&key
,
198 nsIWeakReference
*weakRef
,
201 static PRBool
GetStrictFileOriginPolicy();
203 PRBool
Initialized() { return mDB
!= nsnull
; }
205 nsresult
InitActiveCaches();
206 nsresult
UpdateEntry(nsCacheEntry
*entry
);
207 nsresult
UpdateEntrySize(nsCacheEntry
*entry
, PRUint32 newSize
);
208 nsresult
DeleteEntry(nsCacheEntry
*entry
, PRBool deleteData
);
209 nsresult
DeleteData(nsCacheEntry
*entry
);
210 nsresult
EnableEvictionObserver();
211 nsresult
DisableEvictionObserver();
213 PRBool
CanUseCache(nsIURI
*keyURI
, const nsCString
&clientID
);
215 nsresult
MarkEntry(const nsCString
&clientID
,
216 const nsACString
&key
,
218 nsresult
UnmarkEntry(const nsCString
&clientID
,
219 const nsACString
&key
,
222 nsresult
CacheOpportunistically(const nsCString
&clientID
,
223 const nsACString
&key
);
224 nsresult
GetTypes(const nsCString
&clientID
,
225 const nsACString
&key
,
228 nsresult
GetMatchingNamespace(const nsCString
&clientID
,
229 const nsACString
&key
,
230 nsIApplicationCacheNamespace
**out
);
231 nsresult
GatherEntries(const nsCString
&clientID
,
235 nsresult
AddNamespace(const nsCString
&clientID
,
236 nsIApplicationCacheNamespace
*ns
);
238 nsresult
RunSimpleQuery(mozIStorageStatement
*statment
,
239 PRUint32 resultIndex
,
243 nsCOMPtr
<mozIStorageConnection
> mDB
;
244 nsRefPtr
<nsOfflineCacheEvictionFunction
> mEvictionFunction
;
246 nsCOMPtr
<mozIStorageStatement
> mStatement_CacheSize
;
247 nsCOMPtr
<mozIStorageStatement
> mStatement_DomainSize
;
248 nsCOMPtr
<mozIStorageStatement
> mStatement_EntryCount
;
249 nsCOMPtr
<mozIStorageStatement
> mStatement_UpdateEntry
;
250 nsCOMPtr
<mozIStorageStatement
> mStatement_UpdateEntrySize
;
251 nsCOMPtr
<mozIStorageStatement
> mStatement_UpdateEntryFlags
;
252 nsCOMPtr
<mozIStorageStatement
> mStatement_DeleteEntry
;
253 nsCOMPtr
<mozIStorageStatement
> mStatement_FindEntry
;
254 nsCOMPtr
<mozIStorageStatement
> mStatement_BindEntry
;
255 nsCOMPtr
<mozIStorageStatement
> mStatement_ClearDomain
;
256 nsCOMPtr
<mozIStorageStatement
> mStatement_MarkEntry
;
257 nsCOMPtr
<mozIStorageStatement
> mStatement_UnmarkEntry
;
258 nsCOMPtr
<mozIStorageStatement
> mStatement_GetTypes
;
259 nsCOMPtr
<mozIStorageStatement
> mStatement_FindNamespaceEntry
;
260 nsCOMPtr
<mozIStorageStatement
> mStatement_InsertNamespaceEntry
;
261 nsCOMPtr
<mozIStorageStatement
> mStatement_CleanupUnmarked
;
262 nsCOMPtr
<mozIStorageStatement
> mStatement_GatherEntries
;
263 nsCOMPtr
<mozIStorageStatement
> mStatement_ActivateClient
;
264 nsCOMPtr
<mozIStorageStatement
> mStatement_DeactivateGroup
;
265 nsCOMPtr
<mozIStorageStatement
> mStatement_FindClient
;
266 nsCOMPtr
<mozIStorageStatement
> mStatement_FindClientByNamespace
;
268 nsCOMPtr
<nsILocalFile
> mCacheDirectory
;
269 PRUint32 mCacheCapacity
;
270 PRInt32 mDeltaCounter
;
272 nsInterfaceHashtable
<nsCStringHashKey
, nsIWeakReference
> mCaches
;
273 nsClassHashtable
<nsCStringHashKey
, nsCString
> mActiveCachesByGroup
;
274 nsCStringHashSet mActiveCaches
;
277 #endif // nsOfflineCacheDevice_h__