1 : /* vim:set ts=2 sw=2 sts=2 et cin: */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is Mozilla.
16 : *
17 : * The Initial Developer of the Original Code is IBM Corporation.
18 : * Portions created by IBM Corporation are Copyright (C) 2004
19 : * IBM Corporation. All Rights Reserved.
20 : *
21 : * Contributor(s):
22 : * Darin Fisher <darin@meer.net>
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either the GNU General Public License Version 2 or later (the "GPL"), or
26 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #ifndef nsOfflineCacheDevice_h__
39 : #define nsOfflineCacheDevice_h__
40 :
41 : #include "nsCacheDevice.h"
42 : #include "nsIApplicationCache.h"
43 : #include "nsIApplicationCacheService.h"
44 : #include "nsILocalFile.h"
45 : #include "nsIObserver.h"
46 : #include "mozIStorageConnection.h"
47 : #include "mozIStorageFunction.h"
48 : #include "nsIFile.h"
49 : #include "nsAutoPtr.h"
50 : #include "nsCOMPtr.h"
51 : #include "nsCOMArray.h"
52 : #include "nsInterfaceHashtable.h"
53 : #include "nsClassHashtable.h"
54 : #include "nsWeakReference.h"
55 :
56 : class nsIURI;
57 : class nsOfflineCacheDevice;
58 :
59 : class nsApplicationCacheNamespace : public nsIApplicationCacheNamespace
60 16 : {
61 : public:
62 : NS_DECL_ISUPPORTS
63 : NS_DECL_NSIAPPLICATIONCACHENAMESPACE
64 :
65 16 : nsApplicationCacheNamespace() : mItemType(0) {}
66 :
67 : private:
68 : PRUint32 mItemType;
69 : nsCString mNamespaceSpec;
70 : nsCString mData;
71 : };
72 :
73 15 : class nsOfflineCacheEvictionFunction : public mozIStorageFunction {
74 : public:
75 : NS_DECL_ISUPPORTS
76 : NS_DECL_MOZISTORAGEFUNCTION
77 :
78 15 : nsOfflineCacheEvictionFunction(nsOfflineCacheDevice *device)
79 15 : : mDevice(device)
80 15 : {}
81 :
82 48 : void Reset() { mItems.Clear(); }
83 : void Apply();
84 :
85 : private:
86 : nsOfflineCacheDevice *mDevice;
87 : nsCOMArray<nsIFile> mItems;
88 :
89 : };
90 :
91 : class nsOfflineCacheDevice : public nsCacheDevice
92 : , public nsIApplicationCacheService
93 60 : {
94 : public:
95 : nsOfflineCacheDevice();
96 :
97 : NS_DECL_ISUPPORTS
98 : NS_DECL_NSIAPPLICATIONCACHESERVICE
99 :
100 : /**
101 : * nsCacheDevice methods
102 : */
103 :
104 : static nsOfflineCacheDevice *GetInstance();
105 :
106 : virtual nsresult Init();
107 : virtual nsresult Shutdown();
108 :
109 : virtual const char * GetDeviceID(void);
110 : virtual nsCacheEntry * FindEntry(nsCString * key, bool *collision);
111 : virtual nsresult DeactivateEntry(nsCacheEntry * entry);
112 : virtual nsresult BindEntry(nsCacheEntry * entry);
113 : virtual void DoomEntry( nsCacheEntry * entry );
114 :
115 : virtual nsresult OpenInputStreamForEntry(nsCacheEntry * entry,
116 : nsCacheAccessMode mode,
117 : PRUint32 offset,
118 : nsIInputStream ** result);
119 :
120 : virtual nsresult OpenOutputStreamForEntry(nsCacheEntry * entry,
121 : nsCacheAccessMode mode,
122 : PRUint32 offset,
123 : nsIOutputStream ** result);
124 :
125 : virtual nsresult GetFileForEntry(nsCacheEntry * entry,
126 : nsIFile ** result);
127 :
128 : virtual nsresult OnDataSizeChange(nsCacheEntry * entry, PRInt32 deltaSize);
129 :
130 : virtual nsresult Visit(nsICacheVisitor * visitor);
131 :
132 : virtual nsresult EvictEntries(const char * clientID);
133 :
134 : /* Entry ownership */
135 : nsresult GetOwnerDomains(const char * clientID,
136 : PRUint32 * count,
137 : char *** domains);
138 : nsresult GetOwnerURIs(const char * clientID,
139 : const nsACString & ownerDomain,
140 : PRUint32 * count,
141 : char *** uris);
142 : nsresult SetOwnedKeys(const char * clientID,
143 : const nsACString & ownerDomain,
144 : const nsACString & ownerUrl,
145 : PRUint32 count,
146 : const char ** keys);
147 : nsresult GetOwnedKeys(const char * clientID,
148 : const nsACString & ownerDomain,
149 : const nsACString & ownerUrl,
150 : PRUint32 * count,
151 : char *** keys);
152 : nsresult AddOwnedKey(const char * clientID,
153 : const nsACString & ownerDomain,
154 : const nsACString & ownerURI,
155 : const nsACString & key);
156 : nsresult RemoveOwnedKey(const char * clientID,
157 : const nsACString & ownerDomain,
158 : const nsACString & ownerURI,
159 : const nsACString & key);
160 : nsresult KeyIsOwned(const char * clientID,
161 : const nsACString & ownerDomain,
162 : const nsACString & ownerURI,
163 : const nsACString & key,
164 : bool * isOwned);
165 :
166 : nsresult ClearKeysOwnedByDomain(const char *clientID,
167 : const nsACString &ownerDomain);
168 : nsresult EvictUnownedEntries(const char *clientID);
169 :
170 : nsresult ActivateCache(const nsCSubstring &group,
171 : const nsCSubstring &clientID);
172 : bool IsActiveCache(const nsCSubstring &group,
173 : const nsCSubstring &clientID);
174 : nsresult GetGroupForCache(const nsCSubstring &clientID,
175 : nsCString &out);
176 :
177 : /**
178 : * Preference accessors
179 : */
180 :
181 : void SetCacheParentDirectory(nsILocalFile * parentDir);
182 : void SetCapacity(PRUint32 capacity);
183 :
184 7 : nsILocalFile * CacheDirectory() { return mCacheDirectory; }
185 0 : PRUint32 CacheCapacity() { return mCacheCapacity; }
186 : PRUint32 CacheSize();
187 : PRUint32 EntryCount();
188 :
189 : private:
190 : friend class nsApplicationCache;
191 :
192 : static PLDHashOperator ShutdownApplicationCache(const nsACString &key,
193 : nsIWeakReference *weakRef,
194 : void *ctx);
195 :
196 : static bool GetStrictFileOriginPolicy();
197 :
198 19 : bool Initialized() { return mDB != nsnull; }
199 :
200 : nsresult InitActiveCaches();
201 : nsresult UpdateEntry(nsCacheEntry *entry);
202 : nsresult UpdateEntrySize(nsCacheEntry *entry, PRUint32 newSize);
203 : nsresult DeleteEntry(nsCacheEntry *entry, bool deleteData);
204 : nsresult DeleteData(nsCacheEntry *entry);
205 : nsresult EnableEvictionObserver();
206 : nsresult DisableEvictionObserver();
207 :
208 : bool CanUseCache(nsIURI *keyURI, const nsCString &clientID);
209 :
210 : nsresult MarkEntry(const nsCString &clientID,
211 : const nsACString &key,
212 : PRUint32 typeBits);
213 : nsresult UnmarkEntry(const nsCString &clientID,
214 : const nsACString &key,
215 : PRUint32 typeBits);
216 :
217 : nsresult CacheOpportunistically(const nsCString &clientID,
218 : const nsACString &key);
219 : nsresult GetTypes(const nsCString &clientID,
220 : const nsACString &key,
221 : PRUint32 *typeBits);
222 :
223 : nsresult GetMatchingNamespace(const nsCString &clientID,
224 : const nsACString &key,
225 : nsIApplicationCacheNamespace **out);
226 : nsresult GatherEntries(const nsCString &clientID,
227 : PRUint32 typeBits,
228 : PRUint32 *count,
229 : char *** values);
230 : nsresult AddNamespace(const nsCString &clientID,
231 : nsIApplicationCacheNamespace *ns);
232 :
233 : nsresult GetUsage(const nsACString &clientID,
234 : PRUint32 *usage);
235 :
236 : nsresult RunSimpleQuery(mozIStorageStatement *statment,
237 : PRUint32 resultIndex,
238 : PRUint32 * count,
239 : char *** values);
240 :
241 : nsCOMPtr<mozIStorageConnection> mDB;
242 : nsRefPtr<nsOfflineCacheEvictionFunction> mEvictionFunction;
243 :
244 : nsCOMPtr<mozIStorageStatement> mStatement_CacheSize;
245 : nsCOMPtr<mozIStorageStatement> mStatement_ApplicationCacheSize;
246 : nsCOMPtr<mozIStorageStatement> mStatement_EntryCount;
247 : nsCOMPtr<mozIStorageStatement> mStatement_UpdateEntry;
248 : nsCOMPtr<mozIStorageStatement> mStatement_UpdateEntrySize;
249 : nsCOMPtr<mozIStorageStatement> mStatement_UpdateEntryFlags;
250 : nsCOMPtr<mozIStorageStatement> mStatement_DeleteEntry;
251 : nsCOMPtr<mozIStorageStatement> mStatement_FindEntry;
252 : nsCOMPtr<mozIStorageStatement> mStatement_BindEntry;
253 : nsCOMPtr<mozIStorageStatement> mStatement_ClearDomain;
254 : nsCOMPtr<mozIStorageStatement> mStatement_MarkEntry;
255 : nsCOMPtr<mozIStorageStatement> mStatement_UnmarkEntry;
256 : nsCOMPtr<mozIStorageStatement> mStatement_GetTypes;
257 : nsCOMPtr<mozIStorageStatement> mStatement_FindNamespaceEntry;
258 : nsCOMPtr<mozIStorageStatement> mStatement_InsertNamespaceEntry;
259 : nsCOMPtr<mozIStorageStatement> mStatement_CleanupUnmarked;
260 : nsCOMPtr<mozIStorageStatement> mStatement_GatherEntries;
261 : nsCOMPtr<mozIStorageStatement> mStatement_ActivateClient;
262 : nsCOMPtr<mozIStorageStatement> mStatement_DeactivateGroup;
263 : nsCOMPtr<mozIStorageStatement> mStatement_FindClient;
264 : nsCOMPtr<mozIStorageStatement> mStatement_FindClientByNamespace;
265 : nsCOMPtr<mozIStorageStatement> mStatement_EnumerateGroups;
266 :
267 : nsCOMPtr<nsILocalFile> mCacheDirectory;
268 : PRUint32 mCacheCapacity; // in bytes
269 : PRInt32 mDeltaCounter;
270 :
271 : nsInterfaceHashtable<nsCStringHashKey, nsIWeakReference> mCaches;
272 : nsClassHashtable<nsCStringHashKey, nsCString> mActiveCachesByGroup;
273 : nsTHashtable<nsCStringHashKey> mActiveCaches;
274 :
275 : nsCOMPtr<nsIThread> mInitThread;
276 : };
277 :
278 : #endif // nsOfflineCacheDevice_h__
|