nsDiskCacheDeviceSQL.h 12 KB

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