michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "CacheLog.h" michael@0: #include "AppCacheStorage.h" michael@0: #include "CacheStorageService.h" michael@0: michael@0: #include "OldWrappers.h" michael@0: michael@0: #include "nsICacheEntryDoomCallback.h" michael@0: michael@0: #include "nsICacheService.h" michael@0: #include "nsIApplicationCache.h" michael@0: #include "nsIApplicationCacheService.h" michael@0: #include "nsIURI.h" michael@0: #include "nsNetCID.h" michael@0: #include "nsServiceManagerUtils.h" michael@0: #include "nsThreadUtils.h" michael@0: michael@0: namespace mozilla { michael@0: namespace net { michael@0: michael@0: NS_IMPL_ISUPPORTS_INHERITED0(AppCacheStorage, CacheStorage) michael@0: michael@0: AppCacheStorage::AppCacheStorage(nsILoadContextInfo* aInfo, michael@0: nsIApplicationCache* aAppCache) michael@0: : CacheStorage(aInfo, true /* disk */, false /* lookup app cache */) michael@0: , mAppCache(aAppCache) michael@0: { michael@0: MOZ_COUNT_CTOR(AppCacheStorage); michael@0: } michael@0: michael@0: AppCacheStorage::~AppCacheStorage() michael@0: { michael@0: ProxyReleaseMainThread(mAppCache); michael@0: MOZ_COUNT_DTOR(AppCacheStorage); michael@0: } michael@0: michael@0: NS_IMETHODIMP AppCacheStorage::AsyncOpenURI(nsIURI *aURI, michael@0: const nsACString & aIdExtension, michael@0: uint32_t aFlags, michael@0: nsICacheEntryOpenCallback *aCallback) michael@0: { michael@0: if (!CacheStorageService::Self()) michael@0: return NS_ERROR_NOT_INITIALIZED; michael@0: michael@0: NS_ENSURE_ARG(aURI); michael@0: NS_ENSURE_ARG(aCallback); michael@0: michael@0: nsresult rv; michael@0: michael@0: nsCOMPtr appCache = mAppCache; michael@0: michael@0: if (!appCache) { michael@0: rv = ChooseApplicationCache(aURI, getter_AddRefs(appCache)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: } michael@0: michael@0: if (!appCache) { michael@0: LOG(("AppCacheStorage::AsyncOpenURI entry not found in any appcache, giving up")); michael@0: aCallback->OnCacheEntryAvailable(nullptr, false, nullptr, NS_ERROR_CACHE_KEY_NOT_FOUND); michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsCOMPtr noRefURI; michael@0: rv = aURI->CloneIgnoringRef(getter_AddRefs(noRefURI)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: nsAutoCString cacheKey; michael@0: rv = noRefURI->GetAsciiSpec(cacheKey); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: nsAutoCString scheme; michael@0: rv = noRefURI->GetScheme(scheme); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: nsRefPtr<_OldCacheLoad> appCacheLoad = michael@0: new _OldCacheLoad(scheme, cacheKey, aCallback, appCache, michael@0: LoadInfo(), WriteToDisk(), aFlags); michael@0: rv = appCacheLoad->Start(); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP AppCacheStorage::AsyncDoomURI(nsIURI *aURI, const nsACString & aIdExtension, michael@0: nsICacheEntryDoomCallback* aCallback) michael@0: { michael@0: if (!CacheStorageService::Self()) michael@0: return NS_ERROR_NOT_INITIALIZED; michael@0: michael@0: if (!mAppCache) { michael@0: return NS_ERROR_NOT_AVAILABLE; michael@0: } michael@0: michael@0: // TODO - remove entry from app cache michael@0: // I think no one is using this... michael@0: return NS_ERROR_NOT_IMPLEMENTED; michael@0: } michael@0: michael@0: NS_IMETHODIMP AppCacheStorage::AsyncEvictStorage(nsICacheEntryDoomCallback* aCallback) michael@0: { michael@0: if (!CacheStorageService::Self()) michael@0: return NS_ERROR_NOT_INITIALIZED; michael@0: michael@0: nsresult rv; michael@0: michael@0: nsCOMPtr appCacheService = michael@0: do_GetService(NS_APPLICATIONCACHESERVICE_CONTRACTID, &rv); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: if (!mAppCache) { michael@0: if (LoadInfo()->AppId() == nsILoadContextInfo::NO_APP_ID && michael@0: !LoadInfo()->IsInBrowserElement()) { michael@0: michael@0: // Clear everything. michael@0: nsCOMPtr serv = michael@0: do_GetService(NS_CACHESERVICE_CONTRACTID, &rv); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: rv = serv->EvictEntries(nsICache::STORE_OFFLINE); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: } michael@0: else { michael@0: // Clear app or inbrowser staff. michael@0: rv = appCacheService->DiscardByAppId(LoadInfo()->AppId(), michael@0: LoadInfo()->IsInBrowserElement()); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: } michael@0: } michael@0: else { michael@0: // Discard the group michael@0: nsAutoCString groupID; michael@0: rv = mAppCache->GetGroupID(groupID); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: rv = appCacheService->DeactivateGroup(groupID); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: } michael@0: michael@0: if (aCallback) michael@0: aCallback->OnCacheEntryDoomed(NS_OK); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP AppCacheStorage::AsyncVisitStorage(nsICacheStorageVisitor* aVisitor, michael@0: bool aVisitEntries) michael@0: { michael@0: if (!CacheStorageService::Self()) michael@0: return NS_ERROR_NOT_INITIALIZED; michael@0: michael@0: LOG(("AppCacheStorage::AsyncVisitStorage [this=%p, cb=%p]", this, aVisitor)); michael@0: michael@0: return NS_ERROR_NOT_IMPLEMENTED; michael@0: } michael@0: michael@0: } // net michael@0: } // mozilla