michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- michael@0: * 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 "nsCacheSession.h" michael@0: #include "nsCacheService.h" michael@0: #include "nsCRT.h" michael@0: #include "nsThreadUtils.h" michael@0: michael@0: NS_IMPL_ISUPPORTS(nsCacheSession, nsICacheSession) michael@0: michael@0: nsCacheSession::nsCacheSession(const char * clientID, michael@0: nsCacheStoragePolicy storagePolicy, michael@0: bool streamBased) michael@0: : mClientID(clientID), michael@0: mInfo(0) michael@0: { michael@0: SetStoragePolicy(storagePolicy); michael@0: michael@0: if (streamBased) MarkStreamBased(); michael@0: else SetStoragePolicy(nsICache::STORE_IN_MEMORY); michael@0: michael@0: MarkPublic(); michael@0: michael@0: MarkDoomEntriesIfExpired(); michael@0: } michael@0: michael@0: nsCacheSession::~nsCacheSession() michael@0: { michael@0: /* destructor code */ michael@0: // notify service we are going away? michael@0: } michael@0: michael@0: michael@0: NS_IMETHODIMP nsCacheSession::GetDoomEntriesIfExpired(bool *result) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(result); michael@0: *result = WillDoomEntriesIfExpired(); michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: NS_IMETHODIMP nsCacheSession::SetProfileDirectory(nsIFile *profileDir) michael@0: { michael@0: if (StoragePolicy() != nsICache::STORE_OFFLINE && profileDir) { michael@0: // Profile directory override is currently implemented only for michael@0: // offline cache. This is an early failure to prevent the request michael@0: // being processed before it would fail later because of inability michael@0: // to assign a cache base dir. michael@0: return NS_ERROR_UNEXPECTED; michael@0: } michael@0: michael@0: mProfileDir = profileDir; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP nsCacheSession::GetProfileDirectory(nsIFile **profileDir) michael@0: { michael@0: if (mProfileDir) michael@0: NS_ADDREF(*profileDir = mProfileDir); michael@0: else michael@0: *profileDir = nullptr; michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: NS_IMETHODIMP nsCacheSession::SetDoomEntriesIfExpired(bool doomEntriesIfExpired) michael@0: { michael@0: if (doomEntriesIfExpired) MarkDoomEntriesIfExpired(); michael@0: else ClearDoomEntriesIfExpired(); michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: NS_IMETHODIMP michael@0: nsCacheSession::OpenCacheEntry(const nsACString & key, michael@0: nsCacheAccessMode accessRequested, michael@0: bool blockingMode, michael@0: nsICacheEntryDescriptor ** result) michael@0: { michael@0: nsresult rv; michael@0: michael@0: if (NS_IsMainThread()) michael@0: rv = NS_ERROR_NOT_AVAILABLE; michael@0: else michael@0: rv = nsCacheService::OpenCacheEntry(this, michael@0: key, michael@0: accessRequested, michael@0: blockingMode, michael@0: nullptr, // no listener michael@0: result); michael@0: return rv; michael@0: } michael@0: michael@0: michael@0: NS_IMETHODIMP nsCacheSession::AsyncOpenCacheEntry(const nsACString & key, michael@0: nsCacheAccessMode accessRequested, michael@0: nsICacheListener *listener, michael@0: bool noWait) michael@0: { michael@0: nsresult rv; michael@0: rv = nsCacheService::OpenCacheEntry(this, michael@0: key, michael@0: accessRequested, michael@0: !noWait, michael@0: listener, michael@0: nullptr); // no result michael@0: michael@0: if (rv == NS_ERROR_CACHE_WAIT_FOR_VALIDATION) rv = NS_OK; michael@0: return rv; michael@0: } michael@0: michael@0: NS_IMETHODIMP nsCacheSession::EvictEntries() michael@0: { michael@0: return nsCacheService::EvictEntriesForSession(this); michael@0: } michael@0: michael@0: michael@0: NS_IMETHODIMP nsCacheSession::IsStorageEnabled(bool *result) michael@0: { michael@0: michael@0: return nsCacheService::IsStorageEnabledForPolicy(StoragePolicy(), result); michael@0: } michael@0: michael@0: NS_IMETHODIMP nsCacheSession::DoomEntry(const nsACString &key, michael@0: nsICacheListener *listener) michael@0: { michael@0: return nsCacheService::DoomEntry(this, key, listener); michael@0: } michael@0: michael@0: NS_IMETHODIMP nsCacheSession::GetIsPrivate(bool* aPrivate) michael@0: { michael@0: *aPrivate = IsPrivate(); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP nsCacheSession::SetIsPrivate(bool aPrivate) michael@0: { michael@0: if (aPrivate) michael@0: MarkPrivate(); michael@0: else michael@0: MarkPublic(); michael@0: return NS_OK; michael@0: }