Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* vim:set ts=2 sw=2 sts=2 et cin: */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #ifndef nsOfflineCacheDevice_h__ |
michael@0 | 7 | #define nsOfflineCacheDevice_h__ |
michael@0 | 8 | |
michael@0 | 9 | #include "nsCacheDevice.h" |
michael@0 | 10 | #include "nsIApplicationCache.h" |
michael@0 | 11 | #include "nsIApplicationCacheService.h" |
michael@0 | 12 | #include "nsIObserver.h" |
michael@0 | 13 | #include "mozIStorageConnection.h" |
michael@0 | 14 | #include "mozIStorageFunction.h" |
michael@0 | 15 | #include "nsIFile.h" |
michael@0 | 16 | #include "nsAutoPtr.h" |
michael@0 | 17 | #include "nsCOMPtr.h" |
michael@0 | 18 | #include "nsCOMArray.h" |
michael@0 | 19 | #include "nsInterfaceHashtable.h" |
michael@0 | 20 | #include "nsClassHashtable.h" |
michael@0 | 21 | #include "nsWeakReference.h" |
michael@0 | 22 | #include "mozilla/Attributes.h" |
michael@0 | 23 | #include "mozilla/Mutex.h" |
michael@0 | 24 | |
michael@0 | 25 | class nsIURI; |
michael@0 | 26 | class nsOfflineCacheDevice; |
michael@0 | 27 | class mozIStorageService; |
michael@0 | 28 | |
michael@0 | 29 | class nsApplicationCacheNamespace MOZ_FINAL : public nsIApplicationCacheNamespace |
michael@0 | 30 | { |
michael@0 | 31 | public: |
michael@0 | 32 | NS_DECL_ISUPPORTS |
michael@0 | 33 | NS_DECL_NSIAPPLICATIONCACHENAMESPACE |
michael@0 | 34 | |
michael@0 | 35 | nsApplicationCacheNamespace() : mItemType(0) {} |
michael@0 | 36 | |
michael@0 | 37 | private: |
michael@0 | 38 | uint32_t mItemType; |
michael@0 | 39 | nsCString mNamespaceSpec; |
michael@0 | 40 | nsCString mData; |
michael@0 | 41 | }; |
michael@0 | 42 | |
michael@0 | 43 | class nsOfflineCacheEvictionFunction MOZ_FINAL : public mozIStorageFunction { |
michael@0 | 44 | public: |
michael@0 | 45 | NS_DECL_THREADSAFE_ISUPPORTS |
michael@0 | 46 | NS_DECL_MOZISTORAGEFUNCTION |
michael@0 | 47 | |
michael@0 | 48 | nsOfflineCacheEvictionFunction(nsOfflineCacheDevice *device) |
michael@0 | 49 | : mDevice(device) |
michael@0 | 50 | {} |
michael@0 | 51 | |
michael@0 | 52 | void Reset() { mItems.Clear(); } |
michael@0 | 53 | void Apply(); |
michael@0 | 54 | |
michael@0 | 55 | private: |
michael@0 | 56 | nsOfflineCacheDevice *mDevice; |
michael@0 | 57 | nsCOMArray<nsIFile> mItems; |
michael@0 | 58 | |
michael@0 | 59 | }; |
michael@0 | 60 | |
michael@0 | 61 | class nsOfflineCacheDevice : public nsCacheDevice |
michael@0 | 62 | , public nsISupports |
michael@0 | 63 | { |
michael@0 | 64 | public: |
michael@0 | 65 | nsOfflineCacheDevice(); |
michael@0 | 66 | |
michael@0 | 67 | NS_DECL_THREADSAFE_ISUPPORTS |
michael@0 | 68 | |
michael@0 | 69 | /** |
michael@0 | 70 | * nsCacheDevice methods |
michael@0 | 71 | */ |
michael@0 | 72 | |
michael@0 | 73 | virtual nsresult Init(); |
michael@0 | 74 | nsresult InitWithSqlite(mozIStorageService * ss); |
michael@0 | 75 | virtual nsresult Shutdown(); |
michael@0 | 76 | |
michael@0 | 77 | virtual const char * GetDeviceID(void); |
michael@0 | 78 | virtual nsCacheEntry * FindEntry(nsCString * key, bool *collision); |
michael@0 | 79 | virtual nsresult DeactivateEntry(nsCacheEntry * entry); |
michael@0 | 80 | virtual nsresult BindEntry(nsCacheEntry * entry); |
michael@0 | 81 | virtual void DoomEntry( nsCacheEntry * entry ); |
michael@0 | 82 | |
michael@0 | 83 | virtual nsresult OpenInputStreamForEntry(nsCacheEntry * entry, |
michael@0 | 84 | nsCacheAccessMode mode, |
michael@0 | 85 | uint32_t offset, |
michael@0 | 86 | nsIInputStream ** result); |
michael@0 | 87 | |
michael@0 | 88 | virtual nsresult OpenOutputStreamForEntry(nsCacheEntry * entry, |
michael@0 | 89 | nsCacheAccessMode mode, |
michael@0 | 90 | uint32_t offset, |
michael@0 | 91 | nsIOutputStream ** result); |
michael@0 | 92 | |
michael@0 | 93 | virtual nsresult GetFileForEntry(nsCacheEntry * entry, |
michael@0 | 94 | nsIFile ** result); |
michael@0 | 95 | |
michael@0 | 96 | virtual nsresult OnDataSizeChange(nsCacheEntry * entry, int32_t deltaSize); |
michael@0 | 97 | |
michael@0 | 98 | virtual nsresult Visit(nsICacheVisitor * visitor); |
michael@0 | 99 | |
michael@0 | 100 | virtual nsresult EvictEntries(const char * clientID); |
michael@0 | 101 | |
michael@0 | 102 | /* Entry ownership */ |
michael@0 | 103 | nsresult GetOwnerDomains(const char * clientID, |
michael@0 | 104 | uint32_t * count, |
michael@0 | 105 | char *** domains); |
michael@0 | 106 | nsresult GetOwnerURIs(const char * clientID, |
michael@0 | 107 | const nsACString & ownerDomain, |
michael@0 | 108 | uint32_t * count, |
michael@0 | 109 | char *** uris); |
michael@0 | 110 | nsresult SetOwnedKeys(const char * clientID, |
michael@0 | 111 | const nsACString & ownerDomain, |
michael@0 | 112 | const nsACString & ownerUrl, |
michael@0 | 113 | uint32_t count, |
michael@0 | 114 | const char ** keys); |
michael@0 | 115 | nsresult GetOwnedKeys(const char * clientID, |
michael@0 | 116 | const nsACString & ownerDomain, |
michael@0 | 117 | const nsACString & ownerUrl, |
michael@0 | 118 | uint32_t * count, |
michael@0 | 119 | char *** keys); |
michael@0 | 120 | nsresult AddOwnedKey(const char * clientID, |
michael@0 | 121 | const nsACString & ownerDomain, |
michael@0 | 122 | const nsACString & ownerURI, |
michael@0 | 123 | const nsACString & key); |
michael@0 | 124 | nsresult RemoveOwnedKey(const char * clientID, |
michael@0 | 125 | const nsACString & ownerDomain, |
michael@0 | 126 | const nsACString & ownerURI, |
michael@0 | 127 | const nsACString & key); |
michael@0 | 128 | nsresult KeyIsOwned(const char * clientID, |
michael@0 | 129 | const nsACString & ownerDomain, |
michael@0 | 130 | const nsACString & ownerURI, |
michael@0 | 131 | const nsACString & key, |
michael@0 | 132 | bool * isOwned); |
michael@0 | 133 | |
michael@0 | 134 | nsresult ClearKeysOwnedByDomain(const char *clientID, |
michael@0 | 135 | const nsACString &ownerDomain); |
michael@0 | 136 | nsresult EvictUnownedEntries(const char *clientID); |
michael@0 | 137 | |
michael@0 | 138 | static nsresult BuildApplicationCacheGroupID(nsIURI *aManifestURL, |
michael@0 | 139 | uint32_t appId, bool isInBrowserElement, |
michael@0 | 140 | nsACString &_result); |
michael@0 | 141 | |
michael@0 | 142 | nsresult ActivateCache(const nsCSubstring &group, |
michael@0 | 143 | const nsCSubstring &clientID); |
michael@0 | 144 | bool IsActiveCache(const nsCSubstring &group, |
michael@0 | 145 | const nsCSubstring &clientID); |
michael@0 | 146 | nsresult CreateApplicationCache(const nsACString &group, |
michael@0 | 147 | nsIApplicationCache **out); |
michael@0 | 148 | |
michael@0 | 149 | nsresult GetApplicationCache(const nsACString &clientID, |
michael@0 | 150 | nsIApplicationCache **out); |
michael@0 | 151 | nsresult GetApplicationCache_Unlocked(const nsACString &clientID, |
michael@0 | 152 | nsIApplicationCache **out); |
michael@0 | 153 | |
michael@0 | 154 | nsresult GetActiveCache(const nsACString &group, |
michael@0 | 155 | nsIApplicationCache **out); |
michael@0 | 156 | |
michael@0 | 157 | nsresult DeactivateGroup(const nsACString &group); |
michael@0 | 158 | |
michael@0 | 159 | nsresult ChooseApplicationCache(const nsACString &key, |
michael@0 | 160 | nsILoadContextInfo *loadContext, |
michael@0 | 161 | nsIApplicationCache **out); |
michael@0 | 162 | |
michael@0 | 163 | nsresult CacheOpportunistically(nsIApplicationCache* cache, |
michael@0 | 164 | const nsACString &key); |
michael@0 | 165 | |
michael@0 | 166 | nsresult DiscardByAppId(int32_t appID, bool isInBrowser); |
michael@0 | 167 | |
michael@0 | 168 | nsresult GetGroups(uint32_t *count,char ***keys); |
michael@0 | 169 | |
michael@0 | 170 | nsresult GetGroupsTimeOrdered(uint32_t *count, |
michael@0 | 171 | char ***keys); |
michael@0 | 172 | |
michael@0 | 173 | bool IsLocked(const nsACString &key); |
michael@0 | 174 | void Lock(const nsACString &key); |
michael@0 | 175 | void Unlock(const nsACString &key); |
michael@0 | 176 | |
michael@0 | 177 | /** |
michael@0 | 178 | * Preference accessors |
michael@0 | 179 | */ |
michael@0 | 180 | |
michael@0 | 181 | void SetCacheParentDirectory(nsIFile * parentDir); |
michael@0 | 182 | void SetCapacity(uint32_t capacity); |
michael@0 | 183 | void SetAutoShutdown() { mAutoShutdown = true; } |
michael@0 | 184 | bool AutoShutdown(nsIApplicationCache * aAppCache); |
michael@0 | 185 | |
michael@0 | 186 | nsIFile * BaseDirectory() { return mBaseDirectory; } |
michael@0 | 187 | nsIFile * CacheDirectory() { return mCacheDirectory; } |
michael@0 | 188 | uint32_t CacheCapacity() { return mCacheCapacity; } |
michael@0 | 189 | uint32_t CacheSize(); |
michael@0 | 190 | uint32_t EntryCount(); |
michael@0 | 191 | |
michael@0 | 192 | private: |
michael@0 | 193 | friend class nsApplicationCache; |
michael@0 | 194 | |
michael@0 | 195 | static PLDHashOperator ShutdownApplicationCache(const nsACString &key, |
michael@0 | 196 | nsIWeakReference *weakRef, |
michael@0 | 197 | void *ctx); |
michael@0 | 198 | |
michael@0 | 199 | static bool GetStrictFileOriginPolicy(); |
michael@0 | 200 | |
michael@0 | 201 | bool Initialized() { return mDB != nullptr; } |
michael@0 | 202 | |
michael@0 | 203 | nsresult InitActiveCaches(); |
michael@0 | 204 | nsresult UpdateEntry(nsCacheEntry *entry); |
michael@0 | 205 | nsresult UpdateEntrySize(nsCacheEntry *entry, uint32_t newSize); |
michael@0 | 206 | nsresult DeleteEntry(nsCacheEntry *entry, bool deleteData); |
michael@0 | 207 | nsresult DeleteData(nsCacheEntry *entry); |
michael@0 | 208 | nsresult EnableEvictionObserver(); |
michael@0 | 209 | nsresult DisableEvictionObserver(); |
michael@0 | 210 | |
michael@0 | 211 | bool CanUseCache(nsIURI *keyURI, const nsACString &clientID, nsILoadContextInfo *loadContext); |
michael@0 | 212 | |
michael@0 | 213 | nsresult MarkEntry(const nsCString &clientID, |
michael@0 | 214 | const nsACString &key, |
michael@0 | 215 | uint32_t typeBits); |
michael@0 | 216 | nsresult UnmarkEntry(const nsCString &clientID, |
michael@0 | 217 | const nsACString &key, |
michael@0 | 218 | uint32_t typeBits); |
michael@0 | 219 | |
michael@0 | 220 | nsresult CacheOpportunistically(const nsCString &clientID, |
michael@0 | 221 | const nsACString &key); |
michael@0 | 222 | nsresult GetTypes(const nsCString &clientID, |
michael@0 | 223 | const nsACString &key, |
michael@0 | 224 | uint32_t *typeBits); |
michael@0 | 225 | |
michael@0 | 226 | nsresult GetMatchingNamespace(const nsCString &clientID, |
michael@0 | 227 | const nsACString &key, |
michael@0 | 228 | nsIApplicationCacheNamespace **out); |
michael@0 | 229 | nsresult GatherEntries(const nsCString &clientID, |
michael@0 | 230 | uint32_t typeBits, |
michael@0 | 231 | uint32_t *count, |
michael@0 | 232 | char *** values); |
michael@0 | 233 | nsresult AddNamespace(const nsCString &clientID, |
michael@0 | 234 | nsIApplicationCacheNamespace *ns); |
michael@0 | 235 | |
michael@0 | 236 | nsresult GetUsage(const nsACString &clientID, |
michael@0 | 237 | uint32_t *usage); |
michael@0 | 238 | |
michael@0 | 239 | nsresult RunSimpleQuery(mozIStorageStatement *statment, |
michael@0 | 240 | uint32_t resultIndex, |
michael@0 | 241 | uint32_t * count, |
michael@0 | 242 | char *** values); |
michael@0 | 243 | |
michael@0 | 244 | nsCOMPtr<mozIStorageConnection> mDB; |
michael@0 | 245 | nsRefPtr<nsOfflineCacheEvictionFunction> mEvictionFunction; |
michael@0 | 246 | |
michael@0 | 247 | nsCOMPtr<mozIStorageStatement> mStatement_CacheSize; |
michael@0 | 248 | nsCOMPtr<mozIStorageStatement> mStatement_ApplicationCacheSize; |
michael@0 | 249 | nsCOMPtr<mozIStorageStatement> mStatement_EntryCount; |
michael@0 | 250 | nsCOMPtr<mozIStorageStatement> mStatement_UpdateEntry; |
michael@0 | 251 | nsCOMPtr<mozIStorageStatement> mStatement_UpdateEntrySize; |
michael@0 | 252 | nsCOMPtr<mozIStorageStatement> mStatement_DeleteEntry; |
michael@0 | 253 | nsCOMPtr<mozIStorageStatement> mStatement_FindEntry; |
michael@0 | 254 | nsCOMPtr<mozIStorageStatement> mStatement_BindEntry; |
michael@0 | 255 | nsCOMPtr<mozIStorageStatement> mStatement_ClearDomain; |
michael@0 | 256 | nsCOMPtr<mozIStorageStatement> mStatement_MarkEntry; |
michael@0 | 257 | nsCOMPtr<mozIStorageStatement> mStatement_UnmarkEntry; |
michael@0 | 258 | nsCOMPtr<mozIStorageStatement> mStatement_GetTypes; |
michael@0 | 259 | nsCOMPtr<mozIStorageStatement> mStatement_FindNamespaceEntry; |
michael@0 | 260 | nsCOMPtr<mozIStorageStatement> mStatement_InsertNamespaceEntry; |
michael@0 | 261 | nsCOMPtr<mozIStorageStatement> mStatement_CleanupUnmarked; |
michael@0 | 262 | nsCOMPtr<mozIStorageStatement> mStatement_GatherEntries; |
michael@0 | 263 | nsCOMPtr<mozIStorageStatement> mStatement_ActivateClient; |
michael@0 | 264 | nsCOMPtr<mozIStorageStatement> mStatement_DeactivateGroup; |
michael@0 | 265 | nsCOMPtr<mozIStorageStatement> mStatement_FindClient; |
michael@0 | 266 | nsCOMPtr<mozIStorageStatement> mStatement_FindClientByNamespace; |
michael@0 | 267 | nsCOMPtr<mozIStorageStatement> mStatement_EnumerateApps; |
michael@0 | 268 | nsCOMPtr<mozIStorageStatement> mStatement_EnumerateGroups; |
michael@0 | 269 | nsCOMPtr<mozIStorageStatement> mStatement_EnumerateGroupsTimeOrder; |
michael@0 | 270 | |
michael@0 | 271 | nsCOMPtr<nsIFile> mBaseDirectory; |
michael@0 | 272 | nsCOMPtr<nsIFile> mCacheDirectory; |
michael@0 | 273 | uint32_t mCacheCapacity; // in bytes |
michael@0 | 274 | int32_t mDeltaCounter; |
michael@0 | 275 | bool mAutoShutdown; |
michael@0 | 276 | |
michael@0 | 277 | mozilla::Mutex mLock; |
michael@0 | 278 | |
michael@0 | 279 | nsInterfaceHashtable<nsCStringHashKey, nsIWeakReference> mCaches; |
michael@0 | 280 | nsClassHashtable<nsCStringHashKey, nsCString> mActiveCachesByGroup; |
michael@0 | 281 | nsTHashtable<nsCStringHashKey> mActiveCaches; |
michael@0 | 282 | nsTHashtable<nsCStringHashKey> mLockedEntries; |
michael@0 | 283 | |
michael@0 | 284 | nsCOMPtr<nsIThread> mInitThread; |
michael@0 | 285 | }; |
michael@0 | 286 | |
michael@0 | 287 | #endif // nsOfflineCacheDevice_h__ |