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.

     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/. */
     5 #include "CacheLog.h"
     6 #include "CacheStorage.h"
     7 #include "CacheStorageService.h"
     8 #include "CacheEntry.h"
     9 #include "CacheObserver.h"
    11 #include "OldWrappers.h"
    13 #include "nsICacheEntryDoomCallback.h"
    15 #include "nsIApplicationCache.h"
    16 #include "nsIApplicationCacheService.h"
    17 #include "nsIURI.h"
    18 #include "nsNetCID.h"
    19 #include "nsServiceManagerUtils.h"
    21 namespace mozilla {
    22 namespace net {
    24 NS_IMPL_ISUPPORTS(CacheStorage, nsICacheStorage)
    26 CacheStorage::CacheStorage(nsILoadContextInfo* aInfo,
    27                            bool aAllowDisk,
    28                            bool aLookupAppCache)
    29 : mLoadContextInfo(GetLoadContextInfo(aInfo))
    30 , mWriteToDisk(aAllowDisk)
    31 , mLookupAppCache(aLookupAppCache)
    32 {
    33 }
    35 CacheStorage::~CacheStorage()
    36 {
    37 }
    39 NS_IMETHODIMP CacheStorage::AsyncOpenURI(nsIURI *aURI,
    40                                          const nsACString & aIdExtension,
    41                                          uint32_t aFlags,
    42                                          nsICacheEntryOpenCallback *aCallback)
    43 {
    44   if (!CacheStorageService::Self())
    45     return NS_ERROR_NOT_INITIALIZED;
    47   if (MOZ_UNLIKELY(!CacheObserver::UseDiskCache()) && mWriteToDisk) {
    48     aCallback->OnCacheEntryAvailable(nullptr, false, nullptr, NS_ERROR_NOT_AVAILABLE);
    49     return NS_OK;
    50   }
    52   if (MOZ_UNLIKELY(!CacheObserver::UseMemoryCache()) && !mWriteToDisk) {
    53     aCallback->OnCacheEntryAvailable(nullptr, false, nullptr, NS_ERROR_NOT_AVAILABLE);
    54     return NS_OK;
    55   }
    57   NS_ENSURE_ARG(aURI);
    58   NS_ENSURE_ARG(aCallback);
    60   nsresult rv;
    62   bool truncate = aFlags & nsICacheStorage::OPEN_TRUNCATE;
    64   nsCOMPtr<nsIURI> noRefURI;
    65   rv = aURI->CloneIgnoringRef(getter_AddRefs(noRefURI));
    66   NS_ENSURE_SUCCESS(rv, rv);
    68   nsCOMPtr<nsIApplicationCache> appCache;
    69   if (LookupAppCache()) {
    70     rv = ChooseApplicationCache(noRefURI, getter_AddRefs(appCache));
    71     NS_ENSURE_SUCCESS(rv, rv);
    72   }
    74   if (appCache) {
    75     nsAutoCString cacheKey;
    76     rv = noRefURI->GetAsciiSpec(cacheKey);
    77     NS_ENSURE_SUCCESS(rv, rv);
    79     nsAutoCString scheme;
    80     rv = noRefURI->GetScheme(scheme);
    81     NS_ENSURE_SUCCESS(rv, rv);
    83     nsRefPtr<_OldCacheLoad> appCacheLoad =
    84       new _OldCacheLoad(scheme, cacheKey, aCallback, appCache,
    85                         LoadInfo(), WriteToDisk(), aFlags);
    86     rv = appCacheLoad->Start();
    87     NS_ENSURE_SUCCESS(rv, rv);
    89     LOG(("CacheStorage::AsyncOpenURI loading from appcache"));
    90     return NS_OK;
    91   }
    93   nsRefPtr<CacheEntryHandle> entry;
    94   rv = CacheStorageService::Self()->AddStorageEntry(
    95     this, noRefURI, aIdExtension,
    96     true, // create always
    97     truncate, // replace any existing one?
    98     getter_AddRefs(entry));
    99   NS_ENSURE_SUCCESS(rv, rv);
   101   // May invoke the callback synchronously
   102   entry->Entry()->AsyncOpen(aCallback, aFlags);
   104   return NS_OK;
   105 }
   107 NS_IMETHODIMP CacheStorage::AsyncDoomURI(nsIURI *aURI, const nsACString & aIdExtension,
   108                                          nsICacheEntryDoomCallback* aCallback)
   109 {
   110   if (!CacheStorageService::Self())
   111     return NS_ERROR_NOT_INITIALIZED;
   113   nsresult rv = CacheStorageService::Self()->DoomStorageEntry(
   114     this, aURI, aIdExtension, aCallback);
   115   NS_ENSURE_SUCCESS(rv, rv);
   117   return NS_OK;
   118 }
   120 NS_IMETHODIMP CacheStorage::AsyncEvictStorage(nsICacheEntryDoomCallback* aCallback)
   121 {
   122   if (!CacheStorageService::Self())
   123     return NS_ERROR_NOT_INITIALIZED;
   125   nsresult rv = CacheStorageService::Self()->DoomStorageEntries(
   126     this, aCallback);
   127   NS_ENSURE_SUCCESS(rv, rv);
   129   return NS_OK;
   130 }
   132 NS_IMETHODIMP CacheStorage::AsyncVisitStorage(nsICacheStorageVisitor* aVisitor,
   133                                               bool aVisitEntries)
   134 {
   135   LOG(("CacheStorage::AsyncVisitStorage [this=%p, cb=%p, disk=%d]", this, aVisitor, (bool)mWriteToDisk));
   136   if (!CacheStorageService::Self())
   137     return NS_ERROR_NOT_INITIALIZED;
   139   nsresult rv = CacheStorageService::Self()->WalkStorageEntries(
   140     this, aVisitEntries, aVisitor);
   141   NS_ENSURE_SUCCESS(rv, rv);
   143   return NS_OK;
   144 }
   146 // Internal
   148 nsresult CacheStorage::ChooseApplicationCache(nsIURI* aURI,
   149                                               nsIApplicationCache** aCache)
   150 {
   151   nsresult rv;
   153   nsCOMPtr<nsIApplicationCacheService> appCacheService =
   154     do_GetService(NS_APPLICATIONCACHESERVICE_CONTRACTID, &rv);
   155   NS_ENSURE_SUCCESS(rv, rv);
   157   nsAutoCString cacheKey;
   158   rv = aURI->GetAsciiSpec(cacheKey);
   159   NS_ENSURE_SUCCESS(rv, rv);
   161   rv = appCacheService->ChooseApplicationCache(cacheKey, LoadInfo(), aCache);
   162   NS_ENSURE_SUCCESS(rv, rv);
   164   return NS_OK;
   165 }
   167 } // net
   168 } // mozilla

mercurial