netwerk/cache/nsCacheSession.cpp
author Honza Bambas <honzab.moz@firemni.cz>
Mon, 04 Jun 2012 16:12:24 +0200
changeset 100212 59e7730d780ba305dbee82fa4cf6260dec010414
parent 99365 d76b9c64e4575758fd44599c79cff98a960ca8bf
child 101024 37b83cdbc68e7f13d1d7089ee1068badbebec37e
child 101111 a15d75939cd5d820507656f64edae9a18e10918e
child 113135 56ba58a77e8f11cff724a9ddb50839a0c5663e2c
permissions -rw-r--r--
Bug 753990 - Allow appcache to work with a custom cache (profile) folder within a single application, r=michal.novotny

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "nsCacheSession.h"
#include "nsCacheService.h"
#include "nsCRT.h"

NS_IMPL_ISUPPORTS1(nsCacheSession, nsICacheSession)

nsCacheSession::nsCacheSession(const char *         clientID,
                               nsCacheStoragePolicy storagePolicy,
                               bool                 streamBased)
    : mClientID(clientID),
      mInfo(0)
{
  SetStoragePolicy(storagePolicy);

  if (streamBased) MarkStreamBased();
  else SetStoragePolicy(nsICache::STORE_IN_MEMORY);

  MarkPublic();

  MarkDoomEntriesIfExpired();
}

nsCacheSession::~nsCacheSession()
{
  /* destructor code */
    // notify service we are going away?
}


NS_IMETHODIMP nsCacheSession::GetDoomEntriesIfExpired(bool *result)
{
    NS_ENSURE_ARG_POINTER(result);
    *result = WillDoomEntriesIfExpired();
    return NS_OK;
}


NS_IMETHODIMP nsCacheSession::SetProfileDirectory(nsILocalFile *profileDir)
{
  if (StoragePolicy() != nsICache::STORE_OFFLINE && profileDir) {
        // Profile directory override is currently implemented only for
        // offline cache.  This is an early failure to prevent the request
        // being processed before it would fail later because of inability
        // to assign a cache base dir.
        return NS_ERROR_UNEXPECTED;
    }

    mProfileDir = profileDir;
    return NS_OK;
}

NS_IMETHODIMP nsCacheSession::GetProfileDirectory(nsILocalFile **profileDir)
{
    if (mProfileDir)
        NS_ADDREF(*profileDir = mProfileDir);
    else
        *profileDir = nsnull;

    return NS_OK;
}


NS_IMETHODIMP nsCacheSession::SetDoomEntriesIfExpired(bool doomEntriesIfExpired)
{
    if (doomEntriesIfExpired)  MarkDoomEntriesIfExpired();
    else                       ClearDoomEntriesIfExpired();
    return NS_OK;
}


NS_IMETHODIMP
nsCacheSession::OpenCacheEntry(const nsACString &         key, 
                               nsCacheAccessMode          accessRequested,
                               bool                       blockingMode,
                               nsICacheEntryDescriptor ** result)
{
    nsresult rv;
    rv =  nsCacheService::OpenCacheEntry(this,
                                         key,
                                         accessRequested,
                                         blockingMode,
                                         nsnull, // no listener
                                         result);
    return rv;
}


NS_IMETHODIMP nsCacheSession::AsyncOpenCacheEntry(const nsACString & key,
                                                  nsCacheAccessMode accessRequested,
                                                  nsICacheListener *listener,
                                                  bool              noWait)
{
    nsresult rv;
    rv = nsCacheService::OpenCacheEntry(this,
                                        key,
                                        accessRequested,
                                        !noWait,
                                        listener,
                                        nsnull); // no result

    if (rv == NS_ERROR_CACHE_WAIT_FOR_VALIDATION) rv = NS_OK;
    return rv;
}

NS_IMETHODIMP nsCacheSession::EvictEntries()
{
    return nsCacheService::EvictEntriesForSession(this);
}


NS_IMETHODIMP nsCacheSession::IsStorageEnabled(bool *result)
{

    return nsCacheService::IsStorageEnabledForPolicy(StoragePolicy(), result);
}

NS_IMETHODIMP nsCacheSession::DoomEntry(const nsACString &key,
                                        nsICacheListener *listener)
{
    return nsCacheService::DoomEntry(this, key, listener);
}

NS_IMETHODIMP nsCacheSession::GetIsPrivate(bool* aPrivate)
{
    *aPrivate = IsPrivate();
    return NS_OK;
}

NS_IMETHODIMP nsCacheSession::SetIsPrivate(bool aPrivate)
{
    if (aPrivate)
        MarkPrivate();
    else
        MarkPublic();
    return NS_OK;
}