netwerk/cache2/CacheStorage.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 #include "CacheLog.h"
michael@0 6 #include "CacheStorage.h"
michael@0 7 #include "CacheStorageService.h"
michael@0 8 #include "CacheEntry.h"
michael@0 9 #include "CacheObserver.h"
michael@0 10
michael@0 11 #include "OldWrappers.h"
michael@0 12
michael@0 13 #include "nsICacheEntryDoomCallback.h"
michael@0 14
michael@0 15 #include "nsIApplicationCache.h"
michael@0 16 #include "nsIApplicationCacheService.h"
michael@0 17 #include "nsIURI.h"
michael@0 18 #include "nsNetCID.h"
michael@0 19 #include "nsServiceManagerUtils.h"
michael@0 20
michael@0 21 namespace mozilla {
michael@0 22 namespace net {
michael@0 23
michael@0 24 NS_IMPL_ISUPPORTS(CacheStorage, nsICacheStorage)
michael@0 25
michael@0 26 CacheStorage::CacheStorage(nsILoadContextInfo* aInfo,
michael@0 27 bool aAllowDisk,
michael@0 28 bool aLookupAppCache)
michael@0 29 : mLoadContextInfo(GetLoadContextInfo(aInfo))
michael@0 30 , mWriteToDisk(aAllowDisk)
michael@0 31 , mLookupAppCache(aLookupAppCache)
michael@0 32 {
michael@0 33 }
michael@0 34
michael@0 35 CacheStorage::~CacheStorage()
michael@0 36 {
michael@0 37 }
michael@0 38
michael@0 39 NS_IMETHODIMP CacheStorage::AsyncOpenURI(nsIURI *aURI,
michael@0 40 const nsACString & aIdExtension,
michael@0 41 uint32_t aFlags,
michael@0 42 nsICacheEntryOpenCallback *aCallback)
michael@0 43 {
michael@0 44 if (!CacheStorageService::Self())
michael@0 45 return NS_ERROR_NOT_INITIALIZED;
michael@0 46
michael@0 47 if (MOZ_UNLIKELY(!CacheObserver::UseDiskCache()) && mWriteToDisk) {
michael@0 48 aCallback->OnCacheEntryAvailable(nullptr, false, nullptr, NS_ERROR_NOT_AVAILABLE);
michael@0 49 return NS_OK;
michael@0 50 }
michael@0 51
michael@0 52 if (MOZ_UNLIKELY(!CacheObserver::UseMemoryCache()) && !mWriteToDisk) {
michael@0 53 aCallback->OnCacheEntryAvailable(nullptr, false, nullptr, NS_ERROR_NOT_AVAILABLE);
michael@0 54 return NS_OK;
michael@0 55 }
michael@0 56
michael@0 57 NS_ENSURE_ARG(aURI);
michael@0 58 NS_ENSURE_ARG(aCallback);
michael@0 59
michael@0 60 nsresult rv;
michael@0 61
michael@0 62 bool truncate = aFlags & nsICacheStorage::OPEN_TRUNCATE;
michael@0 63
michael@0 64 nsCOMPtr<nsIURI> noRefURI;
michael@0 65 rv = aURI->CloneIgnoringRef(getter_AddRefs(noRefURI));
michael@0 66 NS_ENSURE_SUCCESS(rv, rv);
michael@0 67
michael@0 68 nsCOMPtr<nsIApplicationCache> appCache;
michael@0 69 if (LookupAppCache()) {
michael@0 70 rv = ChooseApplicationCache(noRefURI, getter_AddRefs(appCache));
michael@0 71 NS_ENSURE_SUCCESS(rv, rv);
michael@0 72 }
michael@0 73
michael@0 74 if (appCache) {
michael@0 75 nsAutoCString cacheKey;
michael@0 76 rv = noRefURI->GetAsciiSpec(cacheKey);
michael@0 77 NS_ENSURE_SUCCESS(rv, rv);
michael@0 78
michael@0 79 nsAutoCString scheme;
michael@0 80 rv = noRefURI->GetScheme(scheme);
michael@0 81 NS_ENSURE_SUCCESS(rv, rv);
michael@0 82
michael@0 83 nsRefPtr<_OldCacheLoad> appCacheLoad =
michael@0 84 new _OldCacheLoad(scheme, cacheKey, aCallback, appCache,
michael@0 85 LoadInfo(), WriteToDisk(), aFlags);
michael@0 86 rv = appCacheLoad->Start();
michael@0 87 NS_ENSURE_SUCCESS(rv, rv);
michael@0 88
michael@0 89 LOG(("CacheStorage::AsyncOpenURI loading from appcache"));
michael@0 90 return NS_OK;
michael@0 91 }
michael@0 92
michael@0 93 nsRefPtr<CacheEntryHandle> entry;
michael@0 94 rv = CacheStorageService::Self()->AddStorageEntry(
michael@0 95 this, noRefURI, aIdExtension,
michael@0 96 true, // create always
michael@0 97 truncate, // replace any existing one?
michael@0 98 getter_AddRefs(entry));
michael@0 99 NS_ENSURE_SUCCESS(rv, rv);
michael@0 100
michael@0 101 // May invoke the callback synchronously
michael@0 102 entry->Entry()->AsyncOpen(aCallback, aFlags);
michael@0 103
michael@0 104 return NS_OK;
michael@0 105 }
michael@0 106
michael@0 107 NS_IMETHODIMP CacheStorage::AsyncDoomURI(nsIURI *aURI, const nsACString & aIdExtension,
michael@0 108 nsICacheEntryDoomCallback* aCallback)
michael@0 109 {
michael@0 110 if (!CacheStorageService::Self())
michael@0 111 return NS_ERROR_NOT_INITIALIZED;
michael@0 112
michael@0 113 nsresult rv = CacheStorageService::Self()->DoomStorageEntry(
michael@0 114 this, aURI, aIdExtension, aCallback);
michael@0 115 NS_ENSURE_SUCCESS(rv, rv);
michael@0 116
michael@0 117 return NS_OK;
michael@0 118 }
michael@0 119
michael@0 120 NS_IMETHODIMP CacheStorage::AsyncEvictStorage(nsICacheEntryDoomCallback* aCallback)
michael@0 121 {
michael@0 122 if (!CacheStorageService::Self())
michael@0 123 return NS_ERROR_NOT_INITIALIZED;
michael@0 124
michael@0 125 nsresult rv = CacheStorageService::Self()->DoomStorageEntries(
michael@0 126 this, aCallback);
michael@0 127 NS_ENSURE_SUCCESS(rv, rv);
michael@0 128
michael@0 129 return NS_OK;
michael@0 130 }
michael@0 131
michael@0 132 NS_IMETHODIMP CacheStorage::AsyncVisitStorage(nsICacheStorageVisitor* aVisitor,
michael@0 133 bool aVisitEntries)
michael@0 134 {
michael@0 135 LOG(("CacheStorage::AsyncVisitStorage [this=%p, cb=%p, disk=%d]", this, aVisitor, (bool)mWriteToDisk));
michael@0 136 if (!CacheStorageService::Self())
michael@0 137 return NS_ERROR_NOT_INITIALIZED;
michael@0 138
michael@0 139 nsresult rv = CacheStorageService::Self()->WalkStorageEntries(
michael@0 140 this, aVisitEntries, aVisitor);
michael@0 141 NS_ENSURE_SUCCESS(rv, rv);
michael@0 142
michael@0 143 return NS_OK;
michael@0 144 }
michael@0 145
michael@0 146 // Internal
michael@0 147
michael@0 148 nsresult CacheStorage::ChooseApplicationCache(nsIURI* aURI,
michael@0 149 nsIApplicationCache** aCache)
michael@0 150 {
michael@0 151 nsresult rv;
michael@0 152
michael@0 153 nsCOMPtr<nsIApplicationCacheService> appCacheService =
michael@0 154 do_GetService(NS_APPLICATIONCACHESERVICE_CONTRACTID, &rv);
michael@0 155 NS_ENSURE_SUCCESS(rv, rv);
michael@0 156
michael@0 157 nsAutoCString cacheKey;
michael@0 158 rv = aURI->GetAsciiSpec(cacheKey);
michael@0 159 NS_ENSURE_SUCCESS(rv, rv);
michael@0 160
michael@0 161 rv = appCacheService->ChooseApplicationCache(cacheKey, LoadInfo(), aCache);
michael@0 162 NS_ENSURE_SUCCESS(rv, rv);
michael@0 163
michael@0 164 return NS_OK;
michael@0 165 }
michael@0 166
michael@0 167 } // net
michael@0 168 } // mozilla

mercurial