Bug 783551 - Get tooltool running on the b2g on OS X builds. r=respindola
[gecko.git] / netwerk / cache / nsDiskCacheDeviceSQL.h
blobcf48204a279964640d0606090291a5e519b7618f
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"
15 #include "nsIFile.h"
16 #include "nsAutoPtr.h"
17 #include "nsCOMPtr.h"
18 #include "nsCOMArray.h"
19 #include "nsInterfaceHashtable.h"
20 #include "nsClassHashtable.h"
21 #include "nsWeakReference.h"
22 #include "mozilla/Attributes.h"
24 class nsIURI;
25 class nsOfflineCacheDevice;
27 class nsApplicationCacheNamespace MOZ_FINAL : public nsIApplicationCacheNamespace
29 public:
30 NS_DECL_ISUPPORTS
31 NS_DECL_NSIAPPLICATIONCACHENAMESPACE
33 nsApplicationCacheNamespace() : mItemType(0) {}
35 private:
36 PRUint32 mItemType;
37 nsCString mNamespaceSpec;
38 nsCString mData;
41 class nsOfflineCacheEvictionFunction MOZ_FINAL : public mozIStorageFunction {
42 public:
43 NS_DECL_ISUPPORTS
44 NS_DECL_MOZISTORAGEFUNCTION
46 nsOfflineCacheEvictionFunction(nsOfflineCacheDevice *device)
47 : mDevice(device)
50 void Reset() { mItems.Clear(); }
51 void Apply();
53 private:
54 nsOfflineCacheDevice *mDevice;
55 nsCOMArray<nsIFile> mItems;
59 class nsOfflineCacheDevice : public nsCacheDevice
60 , public nsISupports
62 public:
63 nsOfflineCacheDevice();
65 NS_DECL_ISUPPORTS
67 /**
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,
82 PRUint32 offset,
83 nsIInputStream ** result);
85 virtual nsresult OpenOutputStreamForEntry(nsCacheEntry * entry,
86 nsCacheAccessMode mode,
87 PRUint32 offset,
88 nsIOutputStream ** result);
90 virtual nsresult GetFileForEntry(nsCacheEntry * entry,
91 nsIFile ** result);
93 virtual nsresult OnDataSizeChange(nsCacheEntry * entry, PRInt32 deltaSize);
95 virtual nsresult Visit(nsICacheVisitor * visitor);
97 virtual nsresult EvictEntries(const char * clientID);
99 /* Entry ownership */
100 nsresult GetOwnerDomains(const char * clientID,
101 PRUint32 * count,
102 char *** domains);
103 nsresult GetOwnerURIs(const char * clientID,
104 const nsACString & ownerDomain,
105 PRUint32 * count,
106 char *** uris);
107 nsresult SetOwnedKeys(const char * clientID,
108 const nsACString & ownerDomain,
109 const nsACString & ownerUrl,
110 PRUint32 count,
111 const char ** keys);
112 nsresult GetOwnedKeys(const char * clientID,
113 const nsACString & ownerDomain,
114 const nsACString & ownerUrl,
115 PRUint32 * count,
116 char *** keys);
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,
129 bool * isOwned);
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,
140 nsCString &out);
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,
162 char ***keys);
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();
183 private:
184 friend class nsApplicationCache;
186 static PLDHashOperator ShutdownApplicationCache(const nsACString &key,
187 nsIWeakReference *weakRef,
188 void *ctx);
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,
206 PRUint32 typeBits);
207 nsresult UnmarkEntry(const nsCString &clientID,
208 const nsACString &key,
209 PRUint32 typeBits);
211 nsresult CacheOpportunistically(const nsCString &clientID,
212 const nsACString &key);
213 nsresult GetTypes(const nsCString &clientID,
214 const nsACString &key,
215 PRUint32 *typeBits);
217 nsresult GetMatchingNamespace(const nsCString &clientID,
218 const nsACString &key,
219 nsIApplicationCacheNamespace **out);
220 nsresult GatherEntries(const nsCString &clientID,
221 PRUint32 typeBits,
222 PRUint32 *count,
223 char *** values);
224 nsresult AddNamespace(const nsCString &clientID,
225 nsIApplicationCacheNamespace *ns);
227 nsresult GetUsage(const nsACString &clientID,
228 PRUint32 *usage);
230 nsresult RunSimpleQuery(mozIStorageStatement *statment,
231 PRUint32 resultIndex,
232 PRUint32 * count,
233 char *** values);
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;
265 bool mAutoShutdown;
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__