Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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"
23 #include "mozilla/Mutex.h"
25 class nsIURI;
26 class nsOfflineCacheDevice;
27 class mozIStorageService;
29 class nsApplicationCacheNamespace MOZ_FINAL : public nsIApplicationCacheNamespace
30 {
31 public:
32 NS_DECL_ISUPPORTS
33 NS_DECL_NSIAPPLICATIONCACHENAMESPACE
35 nsApplicationCacheNamespace() : mItemType(0) {}
37 private:
38 uint32_t mItemType;
39 nsCString mNamespaceSpec;
40 nsCString mData;
41 };
43 class nsOfflineCacheEvictionFunction MOZ_FINAL : public mozIStorageFunction {
44 public:
45 NS_DECL_THREADSAFE_ISUPPORTS
46 NS_DECL_MOZISTORAGEFUNCTION
48 nsOfflineCacheEvictionFunction(nsOfflineCacheDevice *device)
49 : mDevice(device)
50 {}
52 void Reset() { mItems.Clear(); }
53 void Apply();
55 private:
56 nsOfflineCacheDevice *mDevice;
57 nsCOMArray<nsIFile> mItems;
59 };
61 class nsOfflineCacheDevice : public nsCacheDevice
62 , public nsISupports
63 {
64 public:
65 nsOfflineCacheDevice();
67 NS_DECL_THREADSAFE_ISUPPORTS
69 /**
70 * nsCacheDevice methods
71 */
73 virtual nsresult Init();
74 nsresult InitWithSqlite(mozIStorageService * ss);
75 virtual nsresult Shutdown();
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 );
83 virtual nsresult OpenInputStreamForEntry(nsCacheEntry * entry,
84 nsCacheAccessMode mode,
85 uint32_t offset,
86 nsIInputStream ** result);
88 virtual nsresult OpenOutputStreamForEntry(nsCacheEntry * entry,
89 nsCacheAccessMode mode,
90 uint32_t offset,
91 nsIOutputStream ** result);
93 virtual nsresult GetFileForEntry(nsCacheEntry * entry,
94 nsIFile ** result);
96 virtual nsresult OnDataSizeChange(nsCacheEntry * entry, int32_t deltaSize);
98 virtual nsresult Visit(nsICacheVisitor * visitor);
100 virtual nsresult EvictEntries(const char * clientID);
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);
134 nsresult ClearKeysOwnedByDomain(const char *clientID,
135 const nsACString &ownerDomain);
136 nsresult EvictUnownedEntries(const char *clientID);
138 static nsresult BuildApplicationCacheGroupID(nsIURI *aManifestURL,
139 uint32_t appId, bool isInBrowserElement,
140 nsACString &_result);
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);
149 nsresult GetApplicationCache(const nsACString &clientID,
150 nsIApplicationCache **out);
151 nsresult GetApplicationCache_Unlocked(const nsACString &clientID,
152 nsIApplicationCache **out);
154 nsresult GetActiveCache(const nsACString &group,
155 nsIApplicationCache **out);
157 nsresult DeactivateGroup(const nsACString &group);
159 nsresult ChooseApplicationCache(const nsACString &key,
160 nsILoadContextInfo *loadContext,
161 nsIApplicationCache **out);
163 nsresult CacheOpportunistically(nsIApplicationCache* cache,
164 const nsACString &key);
166 nsresult DiscardByAppId(int32_t appID, bool isInBrowser);
168 nsresult GetGroups(uint32_t *count,char ***keys);
170 nsresult GetGroupsTimeOrdered(uint32_t *count,
171 char ***keys);
173 bool IsLocked(const nsACString &key);
174 void Lock(const nsACString &key);
175 void Unlock(const nsACString &key);
177 /**
178 * Preference accessors
179 */
181 void SetCacheParentDirectory(nsIFile * parentDir);
182 void SetCapacity(uint32_t capacity);
183 void SetAutoShutdown() { mAutoShutdown = true; }
184 bool AutoShutdown(nsIApplicationCache * aAppCache);
186 nsIFile * BaseDirectory() { return mBaseDirectory; }
187 nsIFile * CacheDirectory() { return mCacheDirectory; }
188 uint32_t CacheCapacity() { return mCacheCapacity; }
189 uint32_t CacheSize();
190 uint32_t EntryCount();
192 private:
193 friend class nsApplicationCache;
195 static PLDHashOperator ShutdownApplicationCache(const nsACString &key,
196 nsIWeakReference *weakRef,
197 void *ctx);
199 static bool GetStrictFileOriginPolicy();
201 bool Initialized() { return mDB != nullptr; }
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();
211 bool CanUseCache(nsIURI *keyURI, const nsACString &clientID, nsILoadContextInfo *loadContext);
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);
220 nsresult CacheOpportunistically(const nsCString &clientID,
221 const nsACString &key);
222 nsresult GetTypes(const nsCString &clientID,
223 const nsACString &key,
224 uint32_t *typeBits);
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);
236 nsresult GetUsage(const nsACString &clientID,
237 uint32_t *usage);
239 nsresult RunSimpleQuery(mozIStorageStatement *statment,
240 uint32_t resultIndex,
241 uint32_t * count,
242 char *** values);
244 nsCOMPtr<mozIStorageConnection> mDB;
245 nsRefPtr<nsOfflineCacheEvictionFunction> mEvictionFunction;
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;
271 nsCOMPtr<nsIFile> mBaseDirectory;
272 nsCOMPtr<nsIFile> mCacheDirectory;
273 uint32_t mCacheCapacity; // in bytes
274 int32_t mDeltaCounter;
275 bool mAutoShutdown;
277 mozilla::Mutex mLock;
279 nsInterfaceHashtable<nsCStringHashKey, nsIWeakReference> mCaches;
280 nsClassHashtable<nsCStringHashKey, nsCString> mActiveCachesByGroup;
281 nsTHashtable<nsCStringHashKey> mActiveCaches;
282 nsTHashtable<nsCStringHashKey> mLockedEntries;
284 nsCOMPtr<nsIThread> mInitThread;
285 };
287 #endif // nsOfflineCacheDevice_h__