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