michael@0: /* vim:set ts=2 sw=2 sts=2 et cin: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef nsOfflineCacheDevice_h__ michael@0: #define nsOfflineCacheDevice_h__ michael@0: michael@0: #include "nsCacheDevice.h" michael@0: #include "nsIApplicationCache.h" michael@0: #include "nsIApplicationCacheService.h" michael@0: #include "nsIObserver.h" michael@0: #include "mozIStorageConnection.h" michael@0: #include "mozIStorageFunction.h" michael@0: #include "nsIFile.h" michael@0: #include "nsAutoPtr.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "nsCOMArray.h" michael@0: #include "nsInterfaceHashtable.h" michael@0: #include "nsClassHashtable.h" michael@0: #include "nsWeakReference.h" michael@0: #include "mozilla/Attributes.h" michael@0: #include "mozilla/Mutex.h" michael@0: michael@0: class nsIURI; michael@0: class nsOfflineCacheDevice; michael@0: class mozIStorageService; michael@0: michael@0: class nsApplicationCacheNamespace MOZ_FINAL : public nsIApplicationCacheNamespace michael@0: { michael@0: public: michael@0: NS_DECL_ISUPPORTS michael@0: NS_DECL_NSIAPPLICATIONCACHENAMESPACE michael@0: michael@0: nsApplicationCacheNamespace() : mItemType(0) {} michael@0: michael@0: private: michael@0: uint32_t mItemType; michael@0: nsCString mNamespaceSpec; michael@0: nsCString mData; michael@0: }; michael@0: michael@0: class nsOfflineCacheEvictionFunction MOZ_FINAL : public mozIStorageFunction { michael@0: public: michael@0: NS_DECL_THREADSAFE_ISUPPORTS michael@0: NS_DECL_MOZISTORAGEFUNCTION michael@0: michael@0: nsOfflineCacheEvictionFunction(nsOfflineCacheDevice *device) michael@0: : mDevice(device) michael@0: {} michael@0: michael@0: void Reset() { mItems.Clear(); } michael@0: void Apply(); michael@0: michael@0: private: michael@0: nsOfflineCacheDevice *mDevice; michael@0: nsCOMArray mItems; michael@0: michael@0: }; michael@0: michael@0: class nsOfflineCacheDevice : public nsCacheDevice michael@0: , public nsISupports michael@0: { michael@0: public: michael@0: nsOfflineCacheDevice(); michael@0: michael@0: NS_DECL_THREADSAFE_ISUPPORTS michael@0: michael@0: /** michael@0: * nsCacheDevice methods michael@0: */ michael@0: michael@0: virtual nsresult Init(); michael@0: nsresult InitWithSqlite(mozIStorageService * ss); michael@0: virtual nsresult Shutdown(); michael@0: michael@0: virtual const char * GetDeviceID(void); michael@0: virtual nsCacheEntry * FindEntry(nsCString * key, bool *collision); michael@0: virtual nsresult DeactivateEntry(nsCacheEntry * entry); michael@0: virtual nsresult BindEntry(nsCacheEntry * entry); michael@0: virtual void DoomEntry( nsCacheEntry * entry ); michael@0: michael@0: virtual nsresult OpenInputStreamForEntry(nsCacheEntry * entry, michael@0: nsCacheAccessMode mode, michael@0: uint32_t offset, michael@0: nsIInputStream ** result); michael@0: michael@0: virtual nsresult OpenOutputStreamForEntry(nsCacheEntry * entry, michael@0: nsCacheAccessMode mode, michael@0: uint32_t offset, michael@0: nsIOutputStream ** result); michael@0: michael@0: virtual nsresult GetFileForEntry(nsCacheEntry * entry, michael@0: nsIFile ** result); michael@0: michael@0: virtual nsresult OnDataSizeChange(nsCacheEntry * entry, int32_t deltaSize); michael@0: michael@0: virtual nsresult Visit(nsICacheVisitor * visitor); michael@0: michael@0: virtual nsresult EvictEntries(const char * clientID); michael@0: michael@0: /* Entry ownership */ michael@0: nsresult GetOwnerDomains(const char * clientID, michael@0: uint32_t * count, michael@0: char *** domains); michael@0: nsresult GetOwnerURIs(const char * clientID, michael@0: const nsACString & ownerDomain, michael@0: uint32_t * count, michael@0: char *** uris); michael@0: nsresult SetOwnedKeys(const char * clientID, michael@0: const nsACString & ownerDomain, michael@0: const nsACString & ownerUrl, michael@0: uint32_t count, michael@0: const char ** keys); michael@0: nsresult GetOwnedKeys(const char * clientID, michael@0: const nsACString & ownerDomain, michael@0: const nsACString & ownerUrl, michael@0: uint32_t * count, michael@0: char *** keys); michael@0: nsresult AddOwnedKey(const char * clientID, michael@0: const nsACString & ownerDomain, michael@0: const nsACString & ownerURI, michael@0: const nsACString & key); michael@0: nsresult RemoveOwnedKey(const char * clientID, michael@0: const nsACString & ownerDomain, michael@0: const nsACString & ownerURI, michael@0: const nsACString & key); michael@0: nsresult KeyIsOwned(const char * clientID, michael@0: const nsACString & ownerDomain, michael@0: const nsACString & ownerURI, michael@0: const nsACString & key, michael@0: bool * isOwned); michael@0: michael@0: nsresult ClearKeysOwnedByDomain(const char *clientID, michael@0: const nsACString &ownerDomain); michael@0: nsresult EvictUnownedEntries(const char *clientID); michael@0: michael@0: static nsresult BuildApplicationCacheGroupID(nsIURI *aManifestURL, michael@0: uint32_t appId, bool isInBrowserElement, michael@0: nsACString &_result); michael@0: michael@0: nsresult ActivateCache(const nsCSubstring &group, michael@0: const nsCSubstring &clientID); michael@0: bool IsActiveCache(const nsCSubstring &group, michael@0: const nsCSubstring &clientID); michael@0: nsresult CreateApplicationCache(const nsACString &group, michael@0: nsIApplicationCache **out); michael@0: michael@0: nsresult GetApplicationCache(const nsACString &clientID, michael@0: nsIApplicationCache **out); michael@0: nsresult GetApplicationCache_Unlocked(const nsACString &clientID, michael@0: nsIApplicationCache **out); michael@0: michael@0: nsresult GetActiveCache(const nsACString &group, michael@0: nsIApplicationCache **out); michael@0: michael@0: nsresult DeactivateGroup(const nsACString &group); michael@0: michael@0: nsresult ChooseApplicationCache(const nsACString &key, michael@0: nsILoadContextInfo *loadContext, michael@0: nsIApplicationCache **out); michael@0: michael@0: nsresult CacheOpportunistically(nsIApplicationCache* cache, michael@0: const nsACString &key); michael@0: michael@0: nsresult DiscardByAppId(int32_t appID, bool isInBrowser); michael@0: michael@0: nsresult GetGroups(uint32_t *count,char ***keys); michael@0: michael@0: nsresult GetGroupsTimeOrdered(uint32_t *count, michael@0: char ***keys); michael@0: michael@0: bool IsLocked(const nsACString &key); michael@0: void Lock(const nsACString &key); michael@0: void Unlock(const nsACString &key); michael@0: michael@0: /** michael@0: * Preference accessors michael@0: */ michael@0: michael@0: void SetCacheParentDirectory(nsIFile * parentDir); michael@0: void SetCapacity(uint32_t capacity); michael@0: void SetAutoShutdown() { mAutoShutdown = true; } michael@0: bool AutoShutdown(nsIApplicationCache * aAppCache); michael@0: michael@0: nsIFile * BaseDirectory() { return mBaseDirectory; } michael@0: nsIFile * CacheDirectory() { return mCacheDirectory; } michael@0: uint32_t CacheCapacity() { return mCacheCapacity; } michael@0: uint32_t CacheSize(); michael@0: uint32_t EntryCount(); michael@0: michael@0: private: michael@0: friend class nsApplicationCache; michael@0: michael@0: static PLDHashOperator ShutdownApplicationCache(const nsACString &key, michael@0: nsIWeakReference *weakRef, michael@0: void *ctx); michael@0: michael@0: static bool GetStrictFileOriginPolicy(); michael@0: michael@0: bool Initialized() { return mDB != nullptr; } michael@0: michael@0: nsresult InitActiveCaches(); michael@0: nsresult UpdateEntry(nsCacheEntry *entry); michael@0: nsresult UpdateEntrySize(nsCacheEntry *entry, uint32_t newSize); michael@0: nsresult DeleteEntry(nsCacheEntry *entry, bool deleteData); michael@0: nsresult DeleteData(nsCacheEntry *entry); michael@0: nsresult EnableEvictionObserver(); michael@0: nsresult DisableEvictionObserver(); michael@0: michael@0: bool CanUseCache(nsIURI *keyURI, const nsACString &clientID, nsILoadContextInfo *loadContext); michael@0: michael@0: nsresult MarkEntry(const nsCString &clientID, michael@0: const nsACString &key, michael@0: uint32_t typeBits); michael@0: nsresult UnmarkEntry(const nsCString &clientID, michael@0: const nsACString &key, michael@0: uint32_t typeBits); michael@0: michael@0: nsresult CacheOpportunistically(const nsCString &clientID, michael@0: const nsACString &key); michael@0: nsresult GetTypes(const nsCString &clientID, michael@0: const nsACString &key, michael@0: uint32_t *typeBits); michael@0: michael@0: nsresult GetMatchingNamespace(const nsCString &clientID, michael@0: const nsACString &key, michael@0: nsIApplicationCacheNamespace **out); michael@0: nsresult GatherEntries(const nsCString &clientID, michael@0: uint32_t typeBits, michael@0: uint32_t *count, michael@0: char *** values); michael@0: nsresult AddNamespace(const nsCString &clientID, michael@0: nsIApplicationCacheNamespace *ns); michael@0: michael@0: nsresult GetUsage(const nsACString &clientID, michael@0: uint32_t *usage); michael@0: michael@0: nsresult RunSimpleQuery(mozIStorageStatement *statment, michael@0: uint32_t resultIndex, michael@0: uint32_t * count, michael@0: char *** values); michael@0: michael@0: nsCOMPtr mDB; michael@0: nsRefPtr mEvictionFunction; michael@0: michael@0: nsCOMPtr mStatement_CacheSize; michael@0: nsCOMPtr mStatement_ApplicationCacheSize; michael@0: nsCOMPtr mStatement_EntryCount; michael@0: nsCOMPtr mStatement_UpdateEntry; michael@0: nsCOMPtr mStatement_UpdateEntrySize; michael@0: nsCOMPtr mStatement_DeleteEntry; michael@0: nsCOMPtr mStatement_FindEntry; michael@0: nsCOMPtr mStatement_BindEntry; michael@0: nsCOMPtr mStatement_ClearDomain; michael@0: nsCOMPtr mStatement_MarkEntry; michael@0: nsCOMPtr mStatement_UnmarkEntry; michael@0: nsCOMPtr mStatement_GetTypes; michael@0: nsCOMPtr mStatement_FindNamespaceEntry; michael@0: nsCOMPtr mStatement_InsertNamespaceEntry; michael@0: nsCOMPtr mStatement_CleanupUnmarked; michael@0: nsCOMPtr mStatement_GatherEntries; michael@0: nsCOMPtr mStatement_ActivateClient; michael@0: nsCOMPtr mStatement_DeactivateGroup; michael@0: nsCOMPtr mStatement_FindClient; michael@0: nsCOMPtr mStatement_FindClientByNamespace; michael@0: nsCOMPtr mStatement_EnumerateApps; michael@0: nsCOMPtr mStatement_EnumerateGroups; michael@0: nsCOMPtr mStatement_EnumerateGroupsTimeOrder; michael@0: michael@0: nsCOMPtr mBaseDirectory; michael@0: nsCOMPtr mCacheDirectory; michael@0: uint32_t mCacheCapacity; // in bytes michael@0: int32_t mDeltaCounter; michael@0: bool mAutoShutdown; michael@0: michael@0: mozilla::Mutex mLock; michael@0: michael@0: nsInterfaceHashtable mCaches; michael@0: nsClassHashtable mActiveCachesByGroup; michael@0: nsTHashtable mActiveCaches; michael@0: nsTHashtable mLockedEntries; michael@0: michael@0: nsCOMPtr mInitThread; michael@0: }; michael@0: michael@0: #endif // nsOfflineCacheDevice_h__