|
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/. */ |
|
5 |
|
6 #ifndef nsOfflineCacheDevice_h__ |
|
7 #define nsOfflineCacheDevice_h__ |
|
8 |
|
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" |
|
23 #include "mozilla/Mutex.h" |
|
24 |
|
25 class nsIURI; |
|
26 class nsOfflineCacheDevice; |
|
27 class mozIStorageService; |
|
28 |
|
29 class nsApplicationCacheNamespace MOZ_FINAL : public nsIApplicationCacheNamespace |
|
30 { |
|
31 public: |
|
32 NS_DECL_ISUPPORTS |
|
33 NS_DECL_NSIAPPLICATIONCACHENAMESPACE |
|
34 |
|
35 nsApplicationCacheNamespace() : mItemType(0) {} |
|
36 |
|
37 private: |
|
38 uint32_t mItemType; |
|
39 nsCString mNamespaceSpec; |
|
40 nsCString mData; |
|
41 }; |
|
42 |
|
43 class nsOfflineCacheEvictionFunction MOZ_FINAL : public mozIStorageFunction { |
|
44 public: |
|
45 NS_DECL_THREADSAFE_ISUPPORTS |
|
46 NS_DECL_MOZISTORAGEFUNCTION |
|
47 |
|
48 nsOfflineCacheEvictionFunction(nsOfflineCacheDevice *device) |
|
49 : mDevice(device) |
|
50 {} |
|
51 |
|
52 void Reset() { mItems.Clear(); } |
|
53 void Apply(); |
|
54 |
|
55 private: |
|
56 nsOfflineCacheDevice *mDevice; |
|
57 nsCOMArray<nsIFile> mItems; |
|
58 |
|
59 }; |
|
60 |
|
61 class nsOfflineCacheDevice : public nsCacheDevice |
|
62 , public nsISupports |
|
63 { |
|
64 public: |
|
65 nsOfflineCacheDevice(); |
|
66 |
|
67 NS_DECL_THREADSAFE_ISUPPORTS |
|
68 |
|
69 /** |
|
70 * nsCacheDevice methods |
|
71 */ |
|
72 |
|
73 virtual nsresult Init(); |
|
74 nsresult InitWithSqlite(mozIStorageService * ss); |
|
75 virtual nsresult Shutdown(); |
|
76 |
|
77 virtual const char * GetDeviceID(void); |
|
78 virtual nsCacheEntry * FindEntry(nsCString * key, bool *collision); |
|
79 virtual nsresult DeactivateEntry(nsCacheEntry * entry); |
|
80 virtual nsresult BindEntry(nsCacheEntry * entry); |
|
81 virtual void DoomEntry( nsCacheEntry * entry ); |
|
82 |
|
83 virtual nsresult OpenInputStreamForEntry(nsCacheEntry * entry, |
|
84 nsCacheAccessMode mode, |
|
85 uint32_t offset, |
|
86 nsIInputStream ** result); |
|
87 |
|
88 virtual nsresult OpenOutputStreamForEntry(nsCacheEntry * entry, |
|
89 nsCacheAccessMode mode, |
|
90 uint32_t offset, |
|
91 nsIOutputStream ** result); |
|
92 |
|
93 virtual nsresult GetFileForEntry(nsCacheEntry * entry, |
|
94 nsIFile ** result); |
|
95 |
|
96 virtual nsresult OnDataSizeChange(nsCacheEntry * entry, int32_t deltaSize); |
|
97 |
|
98 virtual nsresult Visit(nsICacheVisitor * visitor); |
|
99 |
|
100 virtual nsresult EvictEntries(const char * clientID); |
|
101 |
|
102 /* Entry ownership */ |
|
103 nsresult GetOwnerDomains(const char * clientID, |
|
104 uint32_t * count, |
|
105 char *** domains); |
|
106 nsresult GetOwnerURIs(const char * clientID, |
|
107 const nsACString & ownerDomain, |
|
108 uint32_t * count, |
|
109 char *** uris); |
|
110 nsresult SetOwnedKeys(const char * clientID, |
|
111 const nsACString & ownerDomain, |
|
112 const nsACString & ownerUrl, |
|
113 uint32_t count, |
|
114 const char ** keys); |
|
115 nsresult GetOwnedKeys(const char * clientID, |
|
116 const nsACString & ownerDomain, |
|
117 const nsACString & ownerUrl, |
|
118 uint32_t * count, |
|
119 char *** keys); |
|
120 nsresult AddOwnedKey(const char * clientID, |
|
121 const nsACString & ownerDomain, |
|
122 const nsACString & ownerURI, |
|
123 const nsACString & key); |
|
124 nsresult RemoveOwnedKey(const char * clientID, |
|
125 const nsACString & ownerDomain, |
|
126 const nsACString & ownerURI, |
|
127 const nsACString & key); |
|
128 nsresult KeyIsOwned(const char * clientID, |
|
129 const nsACString & ownerDomain, |
|
130 const nsACString & ownerURI, |
|
131 const nsACString & key, |
|
132 bool * isOwned); |
|
133 |
|
134 nsresult ClearKeysOwnedByDomain(const char *clientID, |
|
135 const nsACString &ownerDomain); |
|
136 nsresult EvictUnownedEntries(const char *clientID); |
|
137 |
|
138 static nsresult BuildApplicationCacheGroupID(nsIURI *aManifestURL, |
|
139 uint32_t appId, bool isInBrowserElement, |
|
140 nsACString &_result); |
|
141 |
|
142 nsresult ActivateCache(const nsCSubstring &group, |
|
143 const nsCSubstring &clientID); |
|
144 bool IsActiveCache(const nsCSubstring &group, |
|
145 const nsCSubstring &clientID); |
|
146 nsresult CreateApplicationCache(const nsACString &group, |
|
147 nsIApplicationCache **out); |
|
148 |
|
149 nsresult GetApplicationCache(const nsACString &clientID, |
|
150 nsIApplicationCache **out); |
|
151 nsresult GetApplicationCache_Unlocked(const nsACString &clientID, |
|
152 nsIApplicationCache **out); |
|
153 |
|
154 nsresult GetActiveCache(const nsACString &group, |
|
155 nsIApplicationCache **out); |
|
156 |
|
157 nsresult DeactivateGroup(const nsACString &group); |
|
158 |
|
159 nsresult ChooseApplicationCache(const nsACString &key, |
|
160 nsILoadContextInfo *loadContext, |
|
161 nsIApplicationCache **out); |
|
162 |
|
163 nsresult CacheOpportunistically(nsIApplicationCache* cache, |
|
164 const nsACString &key); |
|
165 |
|
166 nsresult DiscardByAppId(int32_t appID, bool isInBrowser); |
|
167 |
|
168 nsresult GetGroups(uint32_t *count,char ***keys); |
|
169 |
|
170 nsresult GetGroupsTimeOrdered(uint32_t *count, |
|
171 char ***keys); |
|
172 |
|
173 bool IsLocked(const nsACString &key); |
|
174 void Lock(const nsACString &key); |
|
175 void Unlock(const nsACString &key); |
|
176 |
|
177 /** |
|
178 * Preference accessors |
|
179 */ |
|
180 |
|
181 void SetCacheParentDirectory(nsIFile * parentDir); |
|
182 void SetCapacity(uint32_t capacity); |
|
183 void SetAutoShutdown() { mAutoShutdown = true; } |
|
184 bool AutoShutdown(nsIApplicationCache * aAppCache); |
|
185 |
|
186 nsIFile * BaseDirectory() { return mBaseDirectory; } |
|
187 nsIFile * CacheDirectory() { return mCacheDirectory; } |
|
188 uint32_t CacheCapacity() { return mCacheCapacity; } |
|
189 uint32_t CacheSize(); |
|
190 uint32_t EntryCount(); |
|
191 |
|
192 private: |
|
193 friend class nsApplicationCache; |
|
194 |
|
195 static PLDHashOperator ShutdownApplicationCache(const nsACString &key, |
|
196 nsIWeakReference *weakRef, |
|
197 void *ctx); |
|
198 |
|
199 static bool GetStrictFileOriginPolicy(); |
|
200 |
|
201 bool Initialized() { return mDB != nullptr; } |
|
202 |
|
203 nsresult InitActiveCaches(); |
|
204 nsresult UpdateEntry(nsCacheEntry *entry); |
|
205 nsresult UpdateEntrySize(nsCacheEntry *entry, uint32_t newSize); |
|
206 nsresult DeleteEntry(nsCacheEntry *entry, bool deleteData); |
|
207 nsresult DeleteData(nsCacheEntry *entry); |
|
208 nsresult EnableEvictionObserver(); |
|
209 nsresult DisableEvictionObserver(); |
|
210 |
|
211 bool CanUseCache(nsIURI *keyURI, const nsACString &clientID, nsILoadContextInfo *loadContext); |
|
212 |
|
213 nsresult MarkEntry(const nsCString &clientID, |
|
214 const nsACString &key, |
|
215 uint32_t typeBits); |
|
216 nsresult UnmarkEntry(const nsCString &clientID, |
|
217 const nsACString &key, |
|
218 uint32_t typeBits); |
|
219 |
|
220 nsresult CacheOpportunistically(const nsCString &clientID, |
|
221 const nsACString &key); |
|
222 nsresult GetTypes(const nsCString &clientID, |
|
223 const nsACString &key, |
|
224 uint32_t *typeBits); |
|
225 |
|
226 nsresult GetMatchingNamespace(const nsCString &clientID, |
|
227 const nsACString &key, |
|
228 nsIApplicationCacheNamespace **out); |
|
229 nsresult GatherEntries(const nsCString &clientID, |
|
230 uint32_t typeBits, |
|
231 uint32_t *count, |
|
232 char *** values); |
|
233 nsresult AddNamespace(const nsCString &clientID, |
|
234 nsIApplicationCacheNamespace *ns); |
|
235 |
|
236 nsresult GetUsage(const nsACString &clientID, |
|
237 uint32_t *usage); |
|
238 |
|
239 nsresult RunSimpleQuery(mozIStorageStatement *statment, |
|
240 uint32_t resultIndex, |
|
241 uint32_t * count, |
|
242 char *** values); |
|
243 |
|
244 nsCOMPtr<mozIStorageConnection> mDB; |
|
245 nsRefPtr<nsOfflineCacheEvictionFunction> mEvictionFunction; |
|
246 |
|
247 nsCOMPtr<mozIStorageStatement> mStatement_CacheSize; |
|
248 nsCOMPtr<mozIStorageStatement> mStatement_ApplicationCacheSize; |
|
249 nsCOMPtr<mozIStorageStatement> mStatement_EntryCount; |
|
250 nsCOMPtr<mozIStorageStatement> mStatement_UpdateEntry; |
|
251 nsCOMPtr<mozIStorageStatement> mStatement_UpdateEntrySize; |
|
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; |
|
267 nsCOMPtr<mozIStorageStatement> mStatement_EnumerateApps; |
|
268 nsCOMPtr<mozIStorageStatement> mStatement_EnumerateGroups; |
|
269 nsCOMPtr<mozIStorageStatement> mStatement_EnumerateGroupsTimeOrder; |
|
270 |
|
271 nsCOMPtr<nsIFile> mBaseDirectory; |
|
272 nsCOMPtr<nsIFile> mCacheDirectory; |
|
273 uint32_t mCacheCapacity; // in bytes |
|
274 int32_t mDeltaCounter; |
|
275 bool mAutoShutdown; |
|
276 |
|
277 mozilla::Mutex mLock; |
|
278 |
|
279 nsInterfaceHashtable<nsCStringHashKey, nsIWeakReference> mCaches; |
|
280 nsClassHashtable<nsCStringHashKey, nsCString> mActiveCachesByGroup; |
|
281 nsTHashtable<nsCStringHashKey> mActiveCaches; |
|
282 nsTHashtable<nsCStringHashKey> mLockedEntries; |
|
283 |
|
284 nsCOMPtr<nsIThread> mInitThread; |
|
285 }; |
|
286 |
|
287 #endif // nsOfflineCacheDevice_h__ |