netwerk/cache2/CacheStorage.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/cache2/CacheStorage.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,168 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +#include "CacheLog.h"
     1.9 +#include "CacheStorage.h"
    1.10 +#include "CacheStorageService.h"
    1.11 +#include "CacheEntry.h"
    1.12 +#include "CacheObserver.h"
    1.13 +
    1.14 +#include "OldWrappers.h"
    1.15 +
    1.16 +#include "nsICacheEntryDoomCallback.h"
    1.17 +
    1.18 +#include "nsIApplicationCache.h"
    1.19 +#include "nsIApplicationCacheService.h"
    1.20 +#include "nsIURI.h"
    1.21 +#include "nsNetCID.h"
    1.22 +#include "nsServiceManagerUtils.h"
    1.23 +
    1.24 +namespace mozilla {
    1.25 +namespace net {
    1.26 +
    1.27 +NS_IMPL_ISUPPORTS(CacheStorage, nsICacheStorage)
    1.28 +
    1.29 +CacheStorage::CacheStorage(nsILoadContextInfo* aInfo,
    1.30 +                           bool aAllowDisk,
    1.31 +                           bool aLookupAppCache)
    1.32 +: mLoadContextInfo(GetLoadContextInfo(aInfo))
    1.33 +, mWriteToDisk(aAllowDisk)
    1.34 +, mLookupAppCache(aLookupAppCache)
    1.35 +{
    1.36 +}
    1.37 +
    1.38 +CacheStorage::~CacheStorage()
    1.39 +{
    1.40 +}
    1.41 +
    1.42 +NS_IMETHODIMP CacheStorage::AsyncOpenURI(nsIURI *aURI,
    1.43 +                                         const nsACString & aIdExtension,
    1.44 +                                         uint32_t aFlags,
    1.45 +                                         nsICacheEntryOpenCallback *aCallback)
    1.46 +{
    1.47 +  if (!CacheStorageService::Self())
    1.48 +    return NS_ERROR_NOT_INITIALIZED;
    1.49 +
    1.50 +  if (MOZ_UNLIKELY(!CacheObserver::UseDiskCache()) && mWriteToDisk) {
    1.51 +    aCallback->OnCacheEntryAvailable(nullptr, false, nullptr, NS_ERROR_NOT_AVAILABLE);
    1.52 +    return NS_OK;
    1.53 +  }
    1.54 +
    1.55 +  if (MOZ_UNLIKELY(!CacheObserver::UseMemoryCache()) && !mWriteToDisk) {
    1.56 +    aCallback->OnCacheEntryAvailable(nullptr, false, nullptr, NS_ERROR_NOT_AVAILABLE);
    1.57 +    return NS_OK;
    1.58 +  }
    1.59 +
    1.60 +  NS_ENSURE_ARG(aURI);
    1.61 +  NS_ENSURE_ARG(aCallback);
    1.62 +
    1.63 +  nsresult rv;
    1.64 +
    1.65 +  bool truncate = aFlags & nsICacheStorage::OPEN_TRUNCATE;
    1.66 +
    1.67 +  nsCOMPtr<nsIURI> noRefURI;
    1.68 +  rv = aURI->CloneIgnoringRef(getter_AddRefs(noRefURI));
    1.69 +  NS_ENSURE_SUCCESS(rv, rv);
    1.70 +
    1.71 +  nsCOMPtr<nsIApplicationCache> appCache;
    1.72 +  if (LookupAppCache()) {
    1.73 +    rv = ChooseApplicationCache(noRefURI, getter_AddRefs(appCache));
    1.74 +    NS_ENSURE_SUCCESS(rv, rv);
    1.75 +  }
    1.76 +
    1.77 +  if (appCache) {
    1.78 +    nsAutoCString cacheKey;
    1.79 +    rv = noRefURI->GetAsciiSpec(cacheKey);
    1.80 +    NS_ENSURE_SUCCESS(rv, rv);
    1.81 +
    1.82 +    nsAutoCString scheme;
    1.83 +    rv = noRefURI->GetScheme(scheme);
    1.84 +    NS_ENSURE_SUCCESS(rv, rv);
    1.85 +
    1.86 +    nsRefPtr<_OldCacheLoad> appCacheLoad =
    1.87 +      new _OldCacheLoad(scheme, cacheKey, aCallback, appCache,
    1.88 +                        LoadInfo(), WriteToDisk(), aFlags);
    1.89 +    rv = appCacheLoad->Start();
    1.90 +    NS_ENSURE_SUCCESS(rv, rv);
    1.91 +
    1.92 +    LOG(("CacheStorage::AsyncOpenURI loading from appcache"));
    1.93 +    return NS_OK;
    1.94 +  }
    1.95 +
    1.96 +  nsRefPtr<CacheEntryHandle> entry;
    1.97 +  rv = CacheStorageService::Self()->AddStorageEntry(
    1.98 +    this, noRefURI, aIdExtension,
    1.99 +    true, // create always
   1.100 +    truncate, // replace any existing one?
   1.101 +    getter_AddRefs(entry));
   1.102 +  NS_ENSURE_SUCCESS(rv, rv);
   1.103 +
   1.104 +  // May invoke the callback synchronously
   1.105 +  entry->Entry()->AsyncOpen(aCallback, aFlags);
   1.106 +
   1.107 +  return NS_OK;
   1.108 +}
   1.109 +
   1.110 +NS_IMETHODIMP CacheStorage::AsyncDoomURI(nsIURI *aURI, const nsACString & aIdExtension,
   1.111 +                                         nsICacheEntryDoomCallback* aCallback)
   1.112 +{
   1.113 +  if (!CacheStorageService::Self())
   1.114 +    return NS_ERROR_NOT_INITIALIZED;
   1.115 +
   1.116 +  nsresult rv = CacheStorageService::Self()->DoomStorageEntry(
   1.117 +    this, aURI, aIdExtension, aCallback);
   1.118 +  NS_ENSURE_SUCCESS(rv, rv);
   1.119 +
   1.120 +  return NS_OK;
   1.121 +}
   1.122 +
   1.123 +NS_IMETHODIMP CacheStorage::AsyncEvictStorage(nsICacheEntryDoomCallback* aCallback)
   1.124 +{
   1.125 +  if (!CacheStorageService::Self())
   1.126 +    return NS_ERROR_NOT_INITIALIZED;
   1.127 +
   1.128 +  nsresult rv = CacheStorageService::Self()->DoomStorageEntries(
   1.129 +    this, aCallback);
   1.130 +  NS_ENSURE_SUCCESS(rv, rv);
   1.131 +
   1.132 +  return NS_OK;
   1.133 +}
   1.134 +
   1.135 +NS_IMETHODIMP CacheStorage::AsyncVisitStorage(nsICacheStorageVisitor* aVisitor,
   1.136 +                                              bool aVisitEntries)
   1.137 +{
   1.138 +  LOG(("CacheStorage::AsyncVisitStorage [this=%p, cb=%p, disk=%d]", this, aVisitor, (bool)mWriteToDisk));
   1.139 +  if (!CacheStorageService::Self())
   1.140 +    return NS_ERROR_NOT_INITIALIZED;
   1.141 +
   1.142 +  nsresult rv = CacheStorageService::Self()->WalkStorageEntries(
   1.143 +    this, aVisitEntries, aVisitor);
   1.144 +  NS_ENSURE_SUCCESS(rv, rv);
   1.145 +
   1.146 +  return NS_OK;
   1.147 +}
   1.148 +
   1.149 +// Internal
   1.150 +
   1.151 +nsresult CacheStorage::ChooseApplicationCache(nsIURI* aURI,
   1.152 +                                              nsIApplicationCache** aCache)
   1.153 +{
   1.154 +  nsresult rv;
   1.155 +
   1.156 +  nsCOMPtr<nsIApplicationCacheService> appCacheService =
   1.157 +    do_GetService(NS_APPLICATIONCACHESERVICE_CONTRACTID, &rv);
   1.158 +  NS_ENSURE_SUCCESS(rv, rv);
   1.159 +
   1.160 +  nsAutoCString cacheKey;
   1.161 +  rv = aURI->GetAsciiSpec(cacheKey);
   1.162 +  NS_ENSURE_SUCCESS(rv, rv);
   1.163 +
   1.164 +  rv = appCacheService->ChooseApplicationCache(cacheKey, LoadInfo(), aCache);
   1.165 +  NS_ENSURE_SUCCESS(rv, rv);
   1.166 +
   1.167 +  return NS_OK;
   1.168 +}
   1.169 +
   1.170 +} // net
   1.171 +} // mozilla

mercurial