1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/netwerk/cache/nsDiskCacheDeviceSQL.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,287 @@ 1.4 +/* vim:set ts=2 sw=2 sts=2 et cin: */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#ifndef nsOfflineCacheDevice_h__ 1.10 +#define nsOfflineCacheDevice_h__ 1.11 + 1.12 +#include "nsCacheDevice.h" 1.13 +#include "nsIApplicationCache.h" 1.14 +#include "nsIApplicationCacheService.h" 1.15 +#include "nsIObserver.h" 1.16 +#include "mozIStorageConnection.h" 1.17 +#include "mozIStorageFunction.h" 1.18 +#include "nsIFile.h" 1.19 +#include "nsAutoPtr.h" 1.20 +#include "nsCOMPtr.h" 1.21 +#include "nsCOMArray.h" 1.22 +#include "nsInterfaceHashtable.h" 1.23 +#include "nsClassHashtable.h" 1.24 +#include "nsWeakReference.h" 1.25 +#include "mozilla/Attributes.h" 1.26 +#include "mozilla/Mutex.h" 1.27 + 1.28 +class nsIURI; 1.29 +class nsOfflineCacheDevice; 1.30 +class mozIStorageService; 1.31 + 1.32 +class nsApplicationCacheNamespace MOZ_FINAL : public nsIApplicationCacheNamespace 1.33 +{ 1.34 +public: 1.35 + NS_DECL_ISUPPORTS 1.36 + NS_DECL_NSIAPPLICATIONCACHENAMESPACE 1.37 + 1.38 + nsApplicationCacheNamespace() : mItemType(0) {} 1.39 + 1.40 +private: 1.41 + uint32_t mItemType; 1.42 + nsCString mNamespaceSpec; 1.43 + nsCString mData; 1.44 +}; 1.45 + 1.46 +class nsOfflineCacheEvictionFunction MOZ_FINAL : public mozIStorageFunction { 1.47 +public: 1.48 + NS_DECL_THREADSAFE_ISUPPORTS 1.49 + NS_DECL_MOZISTORAGEFUNCTION 1.50 + 1.51 + nsOfflineCacheEvictionFunction(nsOfflineCacheDevice *device) 1.52 + : mDevice(device) 1.53 + {} 1.54 + 1.55 + void Reset() { mItems.Clear(); } 1.56 + void Apply(); 1.57 + 1.58 +private: 1.59 + nsOfflineCacheDevice *mDevice; 1.60 + nsCOMArray<nsIFile> mItems; 1.61 + 1.62 +}; 1.63 + 1.64 +class nsOfflineCacheDevice : public nsCacheDevice 1.65 + , public nsISupports 1.66 +{ 1.67 +public: 1.68 + nsOfflineCacheDevice(); 1.69 + 1.70 + NS_DECL_THREADSAFE_ISUPPORTS 1.71 + 1.72 + /** 1.73 + * nsCacheDevice methods 1.74 + */ 1.75 + 1.76 + virtual nsresult Init(); 1.77 + nsresult InitWithSqlite(mozIStorageService * ss); 1.78 + virtual nsresult Shutdown(); 1.79 + 1.80 + virtual const char * GetDeviceID(void); 1.81 + virtual nsCacheEntry * FindEntry(nsCString * key, bool *collision); 1.82 + virtual nsresult DeactivateEntry(nsCacheEntry * entry); 1.83 + virtual nsresult BindEntry(nsCacheEntry * entry); 1.84 + virtual void DoomEntry( nsCacheEntry * entry ); 1.85 + 1.86 + virtual nsresult OpenInputStreamForEntry(nsCacheEntry * entry, 1.87 + nsCacheAccessMode mode, 1.88 + uint32_t offset, 1.89 + nsIInputStream ** result); 1.90 + 1.91 + virtual nsresult OpenOutputStreamForEntry(nsCacheEntry * entry, 1.92 + nsCacheAccessMode mode, 1.93 + uint32_t offset, 1.94 + nsIOutputStream ** result); 1.95 + 1.96 + virtual nsresult GetFileForEntry(nsCacheEntry * entry, 1.97 + nsIFile ** result); 1.98 + 1.99 + virtual nsresult OnDataSizeChange(nsCacheEntry * entry, int32_t deltaSize); 1.100 + 1.101 + virtual nsresult Visit(nsICacheVisitor * visitor); 1.102 + 1.103 + virtual nsresult EvictEntries(const char * clientID); 1.104 + 1.105 + /* Entry ownership */ 1.106 + nsresult GetOwnerDomains(const char * clientID, 1.107 + uint32_t * count, 1.108 + char *** domains); 1.109 + nsresult GetOwnerURIs(const char * clientID, 1.110 + const nsACString & ownerDomain, 1.111 + uint32_t * count, 1.112 + char *** uris); 1.113 + nsresult SetOwnedKeys(const char * clientID, 1.114 + const nsACString & ownerDomain, 1.115 + const nsACString & ownerUrl, 1.116 + uint32_t count, 1.117 + const char ** keys); 1.118 + nsresult GetOwnedKeys(const char * clientID, 1.119 + const nsACString & ownerDomain, 1.120 + const nsACString & ownerUrl, 1.121 + uint32_t * count, 1.122 + char *** keys); 1.123 + nsresult AddOwnedKey(const char * clientID, 1.124 + const nsACString & ownerDomain, 1.125 + const nsACString & ownerURI, 1.126 + const nsACString & key); 1.127 + nsresult RemoveOwnedKey(const char * clientID, 1.128 + const nsACString & ownerDomain, 1.129 + const nsACString & ownerURI, 1.130 + const nsACString & key); 1.131 + nsresult KeyIsOwned(const char * clientID, 1.132 + const nsACString & ownerDomain, 1.133 + const nsACString & ownerURI, 1.134 + const nsACString & key, 1.135 + bool * isOwned); 1.136 + 1.137 + nsresult ClearKeysOwnedByDomain(const char *clientID, 1.138 + const nsACString &ownerDomain); 1.139 + nsresult EvictUnownedEntries(const char *clientID); 1.140 + 1.141 + static nsresult BuildApplicationCacheGroupID(nsIURI *aManifestURL, 1.142 + uint32_t appId, bool isInBrowserElement, 1.143 + nsACString &_result); 1.144 + 1.145 + nsresult ActivateCache(const nsCSubstring &group, 1.146 + const nsCSubstring &clientID); 1.147 + bool IsActiveCache(const nsCSubstring &group, 1.148 + const nsCSubstring &clientID); 1.149 + nsresult CreateApplicationCache(const nsACString &group, 1.150 + nsIApplicationCache **out); 1.151 + 1.152 + nsresult GetApplicationCache(const nsACString &clientID, 1.153 + nsIApplicationCache **out); 1.154 + nsresult GetApplicationCache_Unlocked(const nsACString &clientID, 1.155 + nsIApplicationCache **out); 1.156 + 1.157 + nsresult GetActiveCache(const nsACString &group, 1.158 + nsIApplicationCache **out); 1.159 + 1.160 + nsresult DeactivateGroup(const nsACString &group); 1.161 + 1.162 + nsresult ChooseApplicationCache(const nsACString &key, 1.163 + nsILoadContextInfo *loadContext, 1.164 + nsIApplicationCache **out); 1.165 + 1.166 + nsresult CacheOpportunistically(nsIApplicationCache* cache, 1.167 + const nsACString &key); 1.168 + 1.169 + nsresult DiscardByAppId(int32_t appID, bool isInBrowser); 1.170 + 1.171 + nsresult GetGroups(uint32_t *count,char ***keys); 1.172 + 1.173 + nsresult GetGroupsTimeOrdered(uint32_t *count, 1.174 + char ***keys); 1.175 + 1.176 + bool IsLocked(const nsACString &key); 1.177 + void Lock(const nsACString &key); 1.178 + void Unlock(const nsACString &key); 1.179 + 1.180 + /** 1.181 + * Preference accessors 1.182 + */ 1.183 + 1.184 + void SetCacheParentDirectory(nsIFile * parentDir); 1.185 + void SetCapacity(uint32_t capacity); 1.186 + void SetAutoShutdown() { mAutoShutdown = true; } 1.187 + bool AutoShutdown(nsIApplicationCache * aAppCache); 1.188 + 1.189 + nsIFile * BaseDirectory() { return mBaseDirectory; } 1.190 + nsIFile * CacheDirectory() { return mCacheDirectory; } 1.191 + uint32_t CacheCapacity() { return mCacheCapacity; } 1.192 + uint32_t CacheSize(); 1.193 + uint32_t EntryCount(); 1.194 + 1.195 +private: 1.196 + friend class nsApplicationCache; 1.197 + 1.198 + static PLDHashOperator ShutdownApplicationCache(const nsACString &key, 1.199 + nsIWeakReference *weakRef, 1.200 + void *ctx); 1.201 + 1.202 + static bool GetStrictFileOriginPolicy(); 1.203 + 1.204 + bool Initialized() { return mDB != nullptr; } 1.205 + 1.206 + nsresult InitActiveCaches(); 1.207 + nsresult UpdateEntry(nsCacheEntry *entry); 1.208 + nsresult UpdateEntrySize(nsCacheEntry *entry, uint32_t newSize); 1.209 + nsresult DeleteEntry(nsCacheEntry *entry, bool deleteData); 1.210 + nsresult DeleteData(nsCacheEntry *entry); 1.211 + nsresult EnableEvictionObserver(); 1.212 + nsresult DisableEvictionObserver(); 1.213 + 1.214 + bool CanUseCache(nsIURI *keyURI, const nsACString &clientID, nsILoadContextInfo *loadContext); 1.215 + 1.216 + nsresult MarkEntry(const nsCString &clientID, 1.217 + const nsACString &key, 1.218 + uint32_t typeBits); 1.219 + nsresult UnmarkEntry(const nsCString &clientID, 1.220 + const nsACString &key, 1.221 + uint32_t typeBits); 1.222 + 1.223 + nsresult CacheOpportunistically(const nsCString &clientID, 1.224 + const nsACString &key); 1.225 + nsresult GetTypes(const nsCString &clientID, 1.226 + const nsACString &key, 1.227 + uint32_t *typeBits); 1.228 + 1.229 + nsresult GetMatchingNamespace(const nsCString &clientID, 1.230 + const nsACString &key, 1.231 + nsIApplicationCacheNamespace **out); 1.232 + nsresult GatherEntries(const nsCString &clientID, 1.233 + uint32_t typeBits, 1.234 + uint32_t *count, 1.235 + char *** values); 1.236 + nsresult AddNamespace(const nsCString &clientID, 1.237 + nsIApplicationCacheNamespace *ns); 1.238 + 1.239 + nsresult GetUsage(const nsACString &clientID, 1.240 + uint32_t *usage); 1.241 + 1.242 + nsresult RunSimpleQuery(mozIStorageStatement *statment, 1.243 + uint32_t resultIndex, 1.244 + uint32_t * count, 1.245 + char *** values); 1.246 + 1.247 + nsCOMPtr<mozIStorageConnection> mDB; 1.248 + nsRefPtr<nsOfflineCacheEvictionFunction> mEvictionFunction; 1.249 + 1.250 + nsCOMPtr<mozIStorageStatement> mStatement_CacheSize; 1.251 + nsCOMPtr<mozIStorageStatement> mStatement_ApplicationCacheSize; 1.252 + nsCOMPtr<mozIStorageStatement> mStatement_EntryCount; 1.253 + nsCOMPtr<mozIStorageStatement> mStatement_UpdateEntry; 1.254 + nsCOMPtr<mozIStorageStatement> mStatement_UpdateEntrySize; 1.255 + nsCOMPtr<mozIStorageStatement> mStatement_DeleteEntry; 1.256 + nsCOMPtr<mozIStorageStatement> mStatement_FindEntry; 1.257 + nsCOMPtr<mozIStorageStatement> mStatement_BindEntry; 1.258 + nsCOMPtr<mozIStorageStatement> mStatement_ClearDomain; 1.259 + nsCOMPtr<mozIStorageStatement> mStatement_MarkEntry; 1.260 + nsCOMPtr<mozIStorageStatement> mStatement_UnmarkEntry; 1.261 + nsCOMPtr<mozIStorageStatement> mStatement_GetTypes; 1.262 + nsCOMPtr<mozIStorageStatement> mStatement_FindNamespaceEntry; 1.263 + nsCOMPtr<mozIStorageStatement> mStatement_InsertNamespaceEntry; 1.264 + nsCOMPtr<mozIStorageStatement> mStatement_CleanupUnmarked; 1.265 + nsCOMPtr<mozIStorageStatement> mStatement_GatherEntries; 1.266 + nsCOMPtr<mozIStorageStatement> mStatement_ActivateClient; 1.267 + nsCOMPtr<mozIStorageStatement> mStatement_DeactivateGroup; 1.268 + nsCOMPtr<mozIStorageStatement> mStatement_FindClient; 1.269 + nsCOMPtr<mozIStorageStatement> mStatement_FindClientByNamespace; 1.270 + nsCOMPtr<mozIStorageStatement> mStatement_EnumerateApps; 1.271 + nsCOMPtr<mozIStorageStatement> mStatement_EnumerateGroups; 1.272 + nsCOMPtr<mozIStorageStatement> mStatement_EnumerateGroupsTimeOrder; 1.273 + 1.274 + nsCOMPtr<nsIFile> mBaseDirectory; 1.275 + nsCOMPtr<nsIFile> mCacheDirectory; 1.276 + uint32_t mCacheCapacity; // in bytes 1.277 + int32_t mDeltaCounter; 1.278 + bool mAutoShutdown; 1.279 + 1.280 + mozilla::Mutex mLock; 1.281 + 1.282 + nsInterfaceHashtable<nsCStringHashKey, nsIWeakReference> mCaches; 1.283 + nsClassHashtable<nsCStringHashKey, nsCString> mActiveCachesByGroup; 1.284 + nsTHashtable<nsCStringHashKey> mActiveCaches; 1.285 + nsTHashtable<nsCStringHashKey> mLockedEntries; 1.286 + 1.287 + nsCOMPtr<nsIThread> mInitThread; 1.288 +}; 1.289 + 1.290 +#endif // nsOfflineCacheDevice_h__