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 "mozIStorageConnection.h"
12 #include "mozIStorageFunction.h"
15 #include "nsCOMArray.h"
16 #include "nsInterfaceHashtable.h"
17 #include "nsClassHashtable.h"
18 #include "nsIWeakReference.h"
19 #include "mozilla/Attributes.h"
20 #include "mozilla/Mutex.h"
23 class nsOfflineCacheDevice
;
24 class mozIStorageService
;
25 class nsILoadContextInfo
;
27 class OriginAttributesPattern
;
30 class nsApplicationCacheNamespace final
: public nsIApplicationCacheNamespace
{
33 NS_DECL_NSIAPPLICATIONCACHENAMESPACE
35 nsApplicationCacheNamespace() : mItemType(0) {}
38 ~nsApplicationCacheNamespace() = default;
41 nsCString mNamespaceSpec
;
45 class nsOfflineCacheEvictionFunction final
: public mozIStorageFunction
{
47 NS_DECL_THREADSAFE_ISUPPORTS
48 NS_DECL_MOZISTORAGEFUNCTION
50 explicit nsOfflineCacheEvictionFunction(nsOfflineCacheDevice
* device
);
57 ~nsOfflineCacheEvictionFunction() = default;
59 nsOfflineCacheDevice
* mDevice
;
63 class nsOfflineCacheDevice final
: public nsCacheDevice
, public nsISupports
{
65 nsOfflineCacheDevice();
67 NS_DECL_THREADSAFE_ISUPPORTS
70 * nsCacheDevice methods
73 virtual nsresult
Init() override
;
74 nsresult
InitWithSqlite(mozIStorageService
* ss
);
75 virtual nsresult
Shutdown() override
;
77 virtual const char* GetDeviceID(void) override
;
78 virtual nsCacheEntry
* FindEntry(nsCString
* key
, bool* collision
) override
;
79 virtual nsresult
DeactivateEntry(nsCacheEntry
* entry
) override
;
80 virtual nsresult
BindEntry(nsCacheEntry
* entry
) override
;
81 virtual void DoomEntry(nsCacheEntry
* entry
) override
;
83 virtual nsresult
OpenInputStreamForEntry(nsCacheEntry
* entry
,
84 nsCacheAccessMode mode
,
86 nsIInputStream
** result
) override
;
88 virtual nsresult
OpenOutputStreamForEntry(nsCacheEntry
* entry
,
89 nsCacheAccessMode mode
,
91 nsIOutputStream
** result
) override
;
93 virtual nsresult
GetFileForEntry(nsCacheEntry
* entry
,
94 nsIFile
** result
) override
;
96 virtual nsresult
OnDataSizeChange(nsCacheEntry
* entry
,
97 int32_t deltaSize
) override
;
99 virtual nsresult
Visit(nsICacheVisitor
* visitor
) override
;
101 virtual nsresult
EvictEntries(const char* clientID
) override
;
103 /* Entry ownership */
104 nsresult
GetOwnerDomains(const char* clientID
, uint32_t* count
,
106 nsresult
GetOwnerURIs(const char* clientID
, const nsACString
& ownerDomain
,
107 uint32_t* count
, char*** uris
);
108 nsresult
SetOwnedKeys(const char* clientID
, const nsACString
& ownerDomain
,
109 const nsACString
& ownerUrl
, uint32_t count
,
111 nsresult
GetOwnedKeys(const char* clientID
, const nsACString
& ownerDomain
,
112 const nsACString
& ownerUrl
, uint32_t* count
,
114 nsresult
AddOwnedKey(const char* clientID
, const nsACString
& ownerDomain
,
115 const nsACString
& ownerURI
, const nsACString
& key
);
116 nsresult
RemoveOwnedKey(const char* clientID
, const nsACString
& ownerDomain
,
117 const nsACString
& ownerURI
, const nsACString
& key
);
118 nsresult
KeyIsOwned(const char* clientID
, const nsACString
& ownerDomain
,
119 const nsACString
& ownerURI
, const nsACString
& key
,
122 nsresult
ClearKeysOwnedByDomain(const char* clientID
,
123 const nsACString
& ownerDomain
);
124 nsresult
EvictUnownedEntries(const char* clientID
);
126 static nsresult
BuildApplicationCacheGroupID(nsIURI
* aManifestURL
,
127 nsACString
const& aOriginSuffix
,
128 nsACString
& _result
);
130 nsresult
ActivateCache(const nsACString
& group
, const nsACString
& clientID
);
131 bool IsActiveCache(const nsACString
& group
, const nsACString
& clientID
);
132 nsresult
CreateApplicationCache(const nsACString
& group
,
133 nsIApplicationCache
** out
);
135 nsresult
GetApplicationCache(const nsACString
& clientID
,
136 nsIApplicationCache
** out
);
137 nsresult
GetApplicationCache_Unlocked(const nsACString
& clientID
,
138 nsIApplicationCache
** out
);
140 nsresult
GetActiveCache(const nsACString
& group
, nsIApplicationCache
** out
);
142 nsresult
DeactivateGroup(const nsACString
& group
);
144 nsresult
ChooseApplicationCache(const nsACString
& key
,
145 nsILoadContextInfo
* loadContext
,
146 nsIApplicationCache
** out
);
148 nsresult
CacheOpportunistically(nsIApplicationCache
* cache
,
149 const nsACString
& key
);
151 nsresult
Evict(nsILoadContextInfo
* aInfo
);
152 nsresult
Evict(mozilla::OriginAttributesPattern
const& aPattern
);
154 nsresult
GetGroups(nsTArray
<nsCString
>& keys
);
156 nsresult
GetGroupsTimeOrdered(nsTArray
<nsCString
>& keys
);
158 bool IsLocked(const nsACString
& key
);
159 void Lock(const nsACString
& key
);
160 void Unlock(const nsACString
& key
);
163 * Preference accessors
166 void SetCacheParentDirectory(nsIFile
* parentDir
);
167 void SetCapacity(uint32_t capacity
);
168 void SetAutoShutdown() { mAutoShutdown
= true; }
169 bool AutoShutdown(nsIApplicationCache
* aAppCache
);
171 nsIFile
* BaseDirectory() { return mBaseDirectory
; }
172 nsIFile
* CacheDirectory() { return mCacheDirectory
; }
173 uint32_t CacheCapacity() { return mCacheCapacity
; }
174 uint32_t CacheSize();
175 uint32_t EntryCount();
178 ~nsOfflineCacheDevice() = default;
180 friend class nsApplicationCache
;
182 static bool GetStrictFileOriginPolicy();
184 bool Initialized() { return mDB
!= nullptr; }
186 nsresult
InitActiveCaches();
187 nsresult
UpdateEntry(nsCacheEntry
* entry
);
188 nsresult
UpdateEntrySize(nsCacheEntry
* entry
, uint32_t newSize
);
189 nsresult
DeleteEntry(nsCacheEntry
* entry
, bool deleteData
);
190 nsresult
DeleteData(nsCacheEntry
* entry
);
191 nsresult
EnableEvictionObserver();
192 nsresult
DisableEvictionObserver();
194 bool CanUseCache(nsIURI
* keyURI
, const nsACString
& clientID
,
195 nsILoadContextInfo
* loadContext
);
197 nsresult
MarkEntry(const nsCString
& clientID
, const nsACString
& key
,
199 nsresult
UnmarkEntry(const nsCString
& clientID
, const nsACString
& key
,
202 nsresult
CacheOpportunistically(const nsCString
& clientID
,
203 const nsACString
& key
);
204 nsresult
GetTypes(const nsCString
& clientID
, const nsACString
& key
,
207 nsresult
GetMatchingNamespace(const nsCString
& clientID
,
208 const nsACString
& key
,
209 nsIApplicationCacheNamespace
** out
);
210 nsresult
GatherEntries(const nsCString
& clientID
, uint32_t typeBits
,
211 nsTArray
<nsCString
>& keys
);
212 nsresult
AddNamespace(const nsCString
& clientID
,
213 nsIApplicationCacheNamespace
* ns
);
215 nsresult
GetUsage(const nsACString
& clientID
, uint32_t* usage
);
217 nsresult
RunSimpleQuery(mozIStorageStatement
* statment
, uint32_t resultIndex
,
218 nsTArray
<nsCString
>& values
);
220 nsCOMPtr
<mozIStorageConnection
> mDB
;
221 RefPtr
<nsOfflineCacheEvictionFunction
> mEvictionFunction
;
223 nsCOMPtr
<mozIStorageStatement
> mStatement_CacheSize
;
224 nsCOMPtr
<mozIStorageStatement
> mStatement_ApplicationCacheSize
;
225 nsCOMPtr
<mozIStorageStatement
> mStatement_EntryCount
;
226 nsCOMPtr
<mozIStorageStatement
> mStatement_UpdateEntry
;
227 nsCOMPtr
<mozIStorageStatement
> mStatement_UpdateEntrySize
;
228 nsCOMPtr
<mozIStorageStatement
> mStatement_DeleteEntry
;
229 nsCOMPtr
<mozIStorageStatement
> mStatement_FindEntry
;
230 nsCOMPtr
<mozIStorageStatement
> mStatement_BindEntry
;
231 nsCOMPtr
<mozIStorageStatement
> mStatement_ClearDomain
;
232 nsCOMPtr
<mozIStorageStatement
> mStatement_MarkEntry
;
233 nsCOMPtr
<mozIStorageStatement
> mStatement_UnmarkEntry
;
234 nsCOMPtr
<mozIStorageStatement
> mStatement_GetTypes
;
235 nsCOMPtr
<mozIStorageStatement
> mStatement_FindNamespaceEntry
;
236 nsCOMPtr
<mozIStorageStatement
> mStatement_InsertNamespaceEntry
;
237 nsCOMPtr
<mozIStorageStatement
> mStatement_CleanupUnmarked
;
238 nsCOMPtr
<mozIStorageStatement
> mStatement_GatherEntries
;
239 nsCOMPtr
<mozIStorageStatement
> mStatement_ActivateClient
;
240 nsCOMPtr
<mozIStorageStatement
> mStatement_DeactivateGroup
;
241 nsCOMPtr
<mozIStorageStatement
> mStatement_FindClient
;
242 nsCOMPtr
<mozIStorageStatement
> mStatement_FindClientByNamespace
;
243 nsCOMPtr
<mozIStorageStatement
> mStatement_EnumerateApps
;
244 nsCOMPtr
<mozIStorageStatement
> mStatement_EnumerateGroups
;
245 nsCOMPtr
<mozIStorageStatement
> mStatement_EnumerateGroupsTimeOrder
;
247 nsCOMPtr
<nsIFile
> mBaseDirectory
;
248 nsCOMPtr
<nsIFile
> mCacheDirectory
;
249 uint32_t mCacheCapacity
; // in bytes
250 int32_t mDeltaCounter
;
253 mozilla::Mutex mLock
;
255 nsInterfaceHashtable
<nsCStringHashKey
, nsIWeakReference
> mCaches
;
256 nsClassHashtable
<nsCStringHashKey
, nsCString
> mActiveCachesByGroup
;
257 nsTHashtable
<nsCStringHashKey
> mActiveCaches
;
258 nsTHashtable
<nsCStringHashKey
> mLockedEntries
;
260 nsCOMPtr
<nsIEventTarget
> mInitEventTarget
;
263 #endif // nsOfflineCacheDevice_h__