1 /* vim:set ts=2 sw=2 sts=2 et cin: */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef nsOfflineCacheDevice_h__
7 #define nsOfflineCacheDevice_h__
9 #include "nsCacheDevice.h"
10 #include "nsIApplicationCache.h"
11 #include "nsIApplicationCacheService.h"
12 #include "nsIObserver.h"
13 #include "mozIStorageConnection.h"
14 #include "mozIStorageFunction.h"
16 #include "nsAutoPtr.h"
18 #include "nsCOMArray.h"
19 #include "nsInterfaceHashtable.h"
20 #include "nsClassHashtable.h"
21 #include "nsWeakReference.h"
22 #include "mozilla/Attributes.h"
25 class nsOfflineCacheDevice
;
27 class nsApplicationCacheNamespace MOZ_FINAL
: public nsIApplicationCacheNamespace
31 NS_DECL_NSIAPPLICATIONCACHENAMESPACE
33 nsApplicationCacheNamespace() : mItemType(0) {}
37 nsCString mNamespaceSpec
;
41 class nsOfflineCacheEvictionFunction MOZ_FINAL
: public mozIStorageFunction
{
44 NS_DECL_MOZISTORAGEFUNCTION
46 nsOfflineCacheEvictionFunction(nsOfflineCacheDevice
*device
)
50 void Reset() { mItems
.Clear(); }
54 nsOfflineCacheDevice
*mDevice
;
55 nsCOMArray
<nsIFile
> mItems
;
59 class nsOfflineCacheDevice
: public nsCacheDevice
63 nsOfflineCacheDevice();
68 * nsCacheDevice methods
71 virtual nsresult
Init();
72 virtual nsresult
Shutdown();
74 virtual const char * GetDeviceID(void);
75 virtual nsCacheEntry
* FindEntry(nsCString
* key
, bool *collision
);
76 virtual nsresult
DeactivateEntry(nsCacheEntry
* entry
);
77 virtual nsresult
BindEntry(nsCacheEntry
* entry
);
78 virtual void DoomEntry( nsCacheEntry
* entry
);
80 virtual nsresult
OpenInputStreamForEntry(nsCacheEntry
* entry
,
81 nsCacheAccessMode mode
,
83 nsIInputStream
** result
);
85 virtual nsresult
OpenOutputStreamForEntry(nsCacheEntry
* entry
,
86 nsCacheAccessMode mode
,
88 nsIOutputStream
** result
);
90 virtual nsresult
GetFileForEntry(nsCacheEntry
* entry
,
93 virtual nsresult
OnDataSizeChange(nsCacheEntry
* entry
, PRInt32 deltaSize
);
95 virtual nsresult
Visit(nsICacheVisitor
* visitor
);
97 virtual nsresult
EvictEntries(const char * clientID
);
100 nsresult
GetOwnerDomains(const char * clientID
,
103 nsresult
GetOwnerURIs(const char * clientID
,
104 const nsACString
& ownerDomain
,
107 nsresult
SetOwnedKeys(const char * clientID
,
108 const nsACString
& ownerDomain
,
109 const nsACString
& ownerUrl
,
112 nsresult
GetOwnedKeys(const char * clientID
,
113 const nsACString
& ownerDomain
,
114 const nsACString
& ownerUrl
,
117 nsresult
AddOwnedKey(const char * clientID
,
118 const nsACString
& ownerDomain
,
119 const nsACString
& ownerURI
,
120 const nsACString
& key
);
121 nsresult
RemoveOwnedKey(const char * clientID
,
122 const nsACString
& ownerDomain
,
123 const nsACString
& ownerURI
,
124 const nsACString
& key
);
125 nsresult
KeyIsOwned(const char * clientID
,
126 const nsACString
& ownerDomain
,
127 const nsACString
& ownerURI
,
128 const nsACString
& key
,
131 nsresult
ClearKeysOwnedByDomain(const char *clientID
,
132 const nsACString
&ownerDomain
);
133 nsresult
EvictUnownedEntries(const char *clientID
);
135 nsresult
ActivateCache(const nsCSubstring
&group
,
136 const nsCSubstring
&clientID
);
137 bool IsActiveCache(const nsCSubstring
&group
,
138 const nsCSubstring
&clientID
);
139 nsresult
GetGroupForCache(const nsCSubstring
&clientID
,
142 nsresult
CreateApplicationCache(const nsACString
&group
,
143 nsIApplicationCache
**out
);
145 nsresult
GetApplicationCache(const nsACString
&clientID
,
146 nsIApplicationCache
**out
);
148 nsresult
GetActiveCache(const nsACString
&group
,
149 nsIApplicationCache
**out
);
151 nsresult
DeactivateGroup(const nsACString
&group
);
153 nsresult
ChooseApplicationCache(const nsACString
&key
,
154 nsIApplicationCache
**out
);
156 nsresult
CacheOpportunistically(nsIApplicationCache
* cache
,
157 const nsACString
&key
);
159 nsresult
GetGroups(PRUint32
*count
,char ***keys
);
161 nsresult
GetGroupsTimeOrdered(PRUint32
*count
,
164 bool IsLocked(const nsACString
&key
);
165 void Lock(const nsACString
&key
);
166 void Unlock(const nsACString
&key
);
169 * Preference accessors
172 void SetCacheParentDirectory(nsIFile
* parentDir
);
173 void SetCapacity(PRUint32 capacity
);
174 void SetAutoShutdown() { mAutoShutdown
= true; }
175 bool AutoShutdown(nsIApplicationCache
* aAppCache
);
177 nsIFile
* BaseDirectory() { return mBaseDirectory
; }
178 nsIFile
* CacheDirectory() { return mCacheDirectory
; }
179 PRUint32
CacheCapacity() { return mCacheCapacity
; }
180 PRUint32
CacheSize();
181 PRUint32
EntryCount();
184 friend class nsApplicationCache
;
186 static PLDHashOperator
ShutdownApplicationCache(const nsACString
&key
,
187 nsIWeakReference
*weakRef
,
190 static bool GetStrictFileOriginPolicy();
192 bool Initialized() { return mDB
!= nullptr; }
194 nsresult
InitActiveCaches();
195 nsresult
UpdateEntry(nsCacheEntry
*entry
);
196 nsresult
UpdateEntrySize(nsCacheEntry
*entry
, PRUint32 newSize
);
197 nsresult
DeleteEntry(nsCacheEntry
*entry
, bool deleteData
);
198 nsresult
DeleteData(nsCacheEntry
*entry
);
199 nsresult
EnableEvictionObserver();
200 nsresult
DisableEvictionObserver();
202 bool CanUseCache(nsIURI
*keyURI
, const nsCString
&clientID
);
204 nsresult
MarkEntry(const nsCString
&clientID
,
205 const nsACString
&key
,
207 nsresult
UnmarkEntry(const nsCString
&clientID
,
208 const nsACString
&key
,
211 nsresult
CacheOpportunistically(const nsCString
&clientID
,
212 const nsACString
&key
);
213 nsresult
GetTypes(const nsCString
&clientID
,
214 const nsACString
&key
,
217 nsresult
GetMatchingNamespace(const nsCString
&clientID
,
218 const nsACString
&key
,
219 nsIApplicationCacheNamespace
**out
);
220 nsresult
GatherEntries(const nsCString
&clientID
,
224 nsresult
AddNamespace(const nsCString
&clientID
,
225 nsIApplicationCacheNamespace
*ns
);
227 nsresult
GetUsage(const nsACString
&clientID
,
230 nsresult
RunSimpleQuery(mozIStorageStatement
*statment
,
231 PRUint32 resultIndex
,
235 nsCOMPtr
<mozIStorageConnection
> mDB
;
236 nsRefPtr
<nsOfflineCacheEvictionFunction
> mEvictionFunction
;
238 nsCOMPtr
<mozIStorageStatement
> mStatement_CacheSize
;
239 nsCOMPtr
<mozIStorageStatement
> mStatement_ApplicationCacheSize
;
240 nsCOMPtr
<mozIStorageStatement
> mStatement_EntryCount
;
241 nsCOMPtr
<mozIStorageStatement
> mStatement_UpdateEntry
;
242 nsCOMPtr
<mozIStorageStatement
> mStatement_UpdateEntrySize
;
243 nsCOMPtr
<mozIStorageStatement
> mStatement_DeleteEntry
;
244 nsCOMPtr
<mozIStorageStatement
> mStatement_FindEntry
;
245 nsCOMPtr
<mozIStorageStatement
> mStatement_BindEntry
;
246 nsCOMPtr
<mozIStorageStatement
> mStatement_ClearDomain
;
247 nsCOMPtr
<mozIStorageStatement
> mStatement_MarkEntry
;
248 nsCOMPtr
<mozIStorageStatement
> mStatement_UnmarkEntry
;
249 nsCOMPtr
<mozIStorageStatement
> mStatement_GetTypes
;
250 nsCOMPtr
<mozIStorageStatement
> mStatement_FindNamespaceEntry
;
251 nsCOMPtr
<mozIStorageStatement
> mStatement_InsertNamespaceEntry
;
252 nsCOMPtr
<mozIStorageStatement
> mStatement_CleanupUnmarked
;
253 nsCOMPtr
<mozIStorageStatement
> mStatement_GatherEntries
;
254 nsCOMPtr
<mozIStorageStatement
> mStatement_ActivateClient
;
255 nsCOMPtr
<mozIStorageStatement
> mStatement_DeactivateGroup
;
256 nsCOMPtr
<mozIStorageStatement
> mStatement_FindClient
;
257 nsCOMPtr
<mozIStorageStatement
> mStatement_FindClientByNamespace
;
258 nsCOMPtr
<mozIStorageStatement
> mStatement_EnumerateGroups
;
259 nsCOMPtr
<mozIStorageStatement
> mStatement_EnumerateGroupsTimeOrder
;
261 nsCOMPtr
<nsIFile
> mBaseDirectory
;
262 nsCOMPtr
<nsIFile
> mCacheDirectory
;
263 PRUint32 mCacheCapacity
; // in bytes
264 PRInt32 mDeltaCounter
;
267 nsInterfaceHashtable
<nsCStringHashKey
, nsIWeakReference
> mCaches
;
268 nsClassHashtable
<nsCStringHashKey
, nsCString
> mActiveCachesByGroup
;
269 nsTHashtable
<nsCStringHashKey
> mActiveCaches
;
270 nsTHashtable
<nsCStringHashKey
> mLockedEntries
;
272 nsCOMPtr
<nsIThread
> mInitThread
;
275 #endif // nsOfflineCacheDevice_h__