tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

CacheStorage.cpp (6450B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 #include "CacheLog.h"
      6 #include "CacheStorage.h"
      7 #include "CacheStorageService.h"
      8 #include "CacheEntry.h"
      9 #include "CacheObserver.h"
     10 
     11 #include "nsICacheEntryDoomCallback.h"
     12 
     13 #include "nsIURI.h"
     14 #include "nsNetUtil.h"
     15 
     16 namespace mozilla::net {
     17 
     18 NS_IMPL_ISUPPORTS(CacheStorage, nsICacheStorage)
     19 
     20 CacheStorage::CacheStorage(nsILoadContextInfo* aInfo, bool aAllowDisk,
     21                           bool aSkipSizeCheck, bool aPinning)
     22    : mLoadContextInfo(aInfo ? GetLoadContextInfo(aInfo) : nullptr),
     23      mWriteToDisk(aAllowDisk),
     24      mSkipSizeCheck(aSkipSizeCheck),
     25      mPinning(aPinning) {}
     26 
     27 NS_IMETHODIMP CacheStorage::AsyncOpenURI(nsIURI* aURI,
     28                                         const nsACString& aIdExtension,
     29                                         uint32_t aFlags,
     30                                         nsICacheEntryOpenCallback* aCallback) {
     31  NS_ENSURE_ARG(aURI);
     32 
     33  nsresult rv;
     34 
     35  nsCOMPtr<nsIURI> noRefURI;
     36  rv = NS_GetURIWithoutRef(aURI, getter_AddRefs(noRefURI));
     37  NS_ENSURE_SUCCESS(rv, rv);
     38 
     39  nsAutoCString asciiSpec;
     40  rv = noRefURI->GetAsciiSpec(asciiSpec);
     41  NS_ENSURE_SUCCESS(rv, rv);
     42 
     43  return AsyncOpenURIString(asciiSpec, aIdExtension, aFlags, aCallback);
     44 }
     45 
     46 NS_IMETHODIMP CacheStorage::AsyncOpenURIString(
     47    const nsACString& aURI, const nsACString& aIdExtension, uint32_t aFlags,
     48    nsICacheEntryOpenCallback* aCallback) {
     49  if (!CacheStorageService::Self()) return NS_ERROR_NOT_INITIALIZED;
     50 
     51  if (MOZ_UNLIKELY(!CacheObserver::UseDiskCache()) && mWriteToDisk &&
     52      !(aFlags & OPEN_INTERCEPTED)) {
     53    aCallback->OnCacheEntryAvailable(nullptr, false, NS_ERROR_NOT_AVAILABLE);
     54    return NS_OK;
     55  }
     56 
     57  if (MOZ_UNLIKELY(!CacheObserver::UseMemoryCache()) && !mWriteToDisk &&
     58      !(aFlags & OPEN_INTERCEPTED)) {
     59    aCallback->OnCacheEntryAvailable(nullptr, false, NS_ERROR_NOT_AVAILABLE);
     60    return NS_OK;
     61  }
     62 
     63  NS_ENSURE_ARG(aCallback);
     64 
     65  RefPtr<CacheEntryHandle> entry;
     66  nsresult rv = CacheStorageService::Self()->AddStorageEntry(
     67      this, aURI, aIdExtension, aFlags, getter_AddRefs(entry));
     68  if (NS_FAILED(rv)) {
     69    aCallback->OnCacheEntryAvailable(nullptr, false, rv);
     70    return NS_OK;
     71  }
     72 
     73  // May invoke the callback synchronously
     74  entry->Entry()->AsyncOpen(aCallback, aFlags);
     75 
     76  return NS_OK;
     77 }
     78 
     79 NS_IMETHODIMP CacheStorage::OpenTruncate(nsIURI* aURI,
     80                                         const nsACString& aIdExtension,
     81                                         nsICacheEntry** aCacheEntry) {
     82  if (!CacheStorageService::Self()) return NS_ERROR_NOT_INITIALIZED;
     83 
     84  nsresult rv;
     85 
     86  nsCOMPtr<nsIURI> noRefURI;
     87  rv = NS_GetURIWithoutRef(aURI, getter_AddRefs(noRefURI));
     88  NS_ENSURE_SUCCESS(rv, rv);
     89 
     90  nsAutoCString asciiSpec;
     91  rv = noRefURI->GetAsciiSpec(asciiSpec);
     92  NS_ENSURE_SUCCESS(rv, rv);
     93 
     94  RefPtr<CacheEntryHandle> handle;
     95  rv = CacheStorageService::Self()->AddStorageEntry(
     96      this, asciiSpec, aIdExtension,
     97      nsICacheStorage::OPEN_TRUNCATE,  // replace any existing one
     98      getter_AddRefs(handle));
     99  NS_ENSURE_SUCCESS(rv, rv);
    100 
    101  // Just open w/o callback, similar to nsICacheEntry.recreate().
    102  handle->Entry()->AsyncOpen(nullptr, OPEN_TRUNCATE);
    103 
    104  // Return a write handler, consumer is supposed to fill in the entry.
    105  RefPtr<CacheEntryHandle> writeHandle = handle->Entry()->NewWriteHandle();
    106  writeHandle.forget(aCacheEntry);
    107 
    108  return NS_OK;
    109 }
    110 
    111 NS_IMETHODIMP CacheStorage::Exists(nsIURI* aURI, const nsACString& aIdExtension,
    112                                   bool* aResult) {
    113  NS_ENSURE_ARG(aURI);
    114  NS_ENSURE_ARG(aResult);
    115 
    116  if (!CacheStorageService::Self()) return NS_ERROR_NOT_INITIALIZED;
    117 
    118  nsresult rv;
    119 
    120  nsCOMPtr<nsIURI> noRefURI;
    121  rv = NS_GetURIWithoutRef(aURI, getter_AddRefs(noRefURI));
    122  NS_ENSURE_SUCCESS(rv, rv);
    123 
    124  nsAutoCString asciiSpec;
    125  rv = noRefURI->GetAsciiSpec(asciiSpec);
    126  NS_ENSURE_SUCCESS(rv, rv);
    127 
    128  return CacheStorageService::Self()->CheckStorageEntry(this, asciiSpec,
    129                                                        aIdExtension, aResult);
    130 }
    131 
    132 NS_IMETHODIMP
    133 CacheStorage::GetCacheIndexEntryAttrs(nsIURI* aURI,
    134                                      const nsACString& aIdExtension,
    135                                      bool* aHasAltData, uint32_t* aSizeInKB) {
    136  NS_ENSURE_ARG(aURI);
    137  NS_ENSURE_ARG(aHasAltData);
    138  NS_ENSURE_ARG(aSizeInKB);
    139  if (!CacheStorageService::Self()) {
    140    return NS_ERROR_NOT_INITIALIZED;
    141  }
    142 
    143  nsresult rv;
    144 
    145  nsCOMPtr<nsIURI> noRefURI;
    146  rv = NS_GetURIWithoutRef(aURI, getter_AddRefs(noRefURI));
    147  NS_ENSURE_SUCCESS(rv, rv);
    148 
    149  nsAutoCString asciiSpec;
    150  rv = noRefURI->GetAsciiSpec(asciiSpec);
    151  NS_ENSURE_SUCCESS(rv, rv);
    152 
    153  return CacheStorageService::Self()->GetCacheIndexEntryAttrs(
    154      this, asciiSpec, aIdExtension, aHasAltData, aSizeInKB);
    155 }
    156 
    157 NS_IMETHODIMP CacheStorage::AsyncDoomURI(nsIURI* aURI,
    158                                         const nsACString& aIdExtension,
    159                                         nsICacheEntryDoomCallback* aCallback) {
    160  if (!CacheStorageService::Self()) return NS_ERROR_NOT_INITIALIZED;
    161 
    162  nsresult rv;
    163 
    164  nsCOMPtr<nsIURI> noRefURI;
    165  rv = NS_GetURIWithoutRef(aURI, getter_AddRefs(noRefURI));
    166  NS_ENSURE_SUCCESS(rv, rv);
    167 
    168  nsAutoCString asciiSpec;
    169  rv = noRefURI->GetAsciiSpec(asciiSpec);
    170  NS_ENSURE_SUCCESS(rv, rv);
    171 
    172  rv = CacheStorageService::Self()->DoomStorageEntry(this, asciiSpec,
    173                                                     aIdExtension, aCallback);
    174  NS_ENSURE_SUCCESS(rv, rv);
    175 
    176  return NS_OK;
    177 }
    178 
    179 NS_IMETHODIMP CacheStorage::AsyncEvictStorage(
    180    nsICacheEntryDoomCallback* aCallback) {
    181  if (!CacheStorageService::Self()) return NS_ERROR_NOT_INITIALIZED;
    182 
    183  nsresult rv =
    184      CacheStorageService::Self()->DoomStorageEntries(this, aCallback);
    185  NS_ENSURE_SUCCESS(rv, rv);
    186 
    187  return NS_OK;
    188 }
    189 
    190 NS_IMETHODIMP CacheStorage::AsyncVisitStorage(nsICacheStorageVisitor* aVisitor,
    191                                              bool aVisitEntries) {
    192  LOG(("CacheStorage::AsyncVisitStorage [this=%p, cb=%p, disk=%d]", this,
    193       aVisitor, (bool)mWriteToDisk));
    194  if (!CacheStorageService::Self()) return NS_ERROR_NOT_INITIALIZED;
    195 
    196  nsresult rv = CacheStorageService::Self()->WalkStorageEntries(
    197      this, aVisitEntries, aVisitor);
    198  NS_ENSURE_SUCCESS(rv, rv);
    199 
    200  return NS_OK;
    201 }
    202 
    203 }  // namespace mozilla::net