netwerk/cache/nsCacheSession.cpp

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
michael@0 2 *
michael@0 3 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #include "nsCacheSession.h"
michael@0 8 #include "nsCacheService.h"
michael@0 9 #include "nsCRT.h"
michael@0 10 #include "nsThreadUtils.h"
michael@0 11
michael@0 12 NS_IMPL_ISUPPORTS(nsCacheSession, nsICacheSession)
michael@0 13
michael@0 14 nsCacheSession::nsCacheSession(const char * clientID,
michael@0 15 nsCacheStoragePolicy storagePolicy,
michael@0 16 bool streamBased)
michael@0 17 : mClientID(clientID),
michael@0 18 mInfo(0)
michael@0 19 {
michael@0 20 SetStoragePolicy(storagePolicy);
michael@0 21
michael@0 22 if (streamBased) MarkStreamBased();
michael@0 23 else SetStoragePolicy(nsICache::STORE_IN_MEMORY);
michael@0 24
michael@0 25 MarkPublic();
michael@0 26
michael@0 27 MarkDoomEntriesIfExpired();
michael@0 28 }
michael@0 29
michael@0 30 nsCacheSession::~nsCacheSession()
michael@0 31 {
michael@0 32 /* destructor code */
michael@0 33 // notify service we are going away?
michael@0 34 }
michael@0 35
michael@0 36
michael@0 37 NS_IMETHODIMP nsCacheSession::GetDoomEntriesIfExpired(bool *result)
michael@0 38 {
michael@0 39 NS_ENSURE_ARG_POINTER(result);
michael@0 40 *result = WillDoomEntriesIfExpired();
michael@0 41 return NS_OK;
michael@0 42 }
michael@0 43
michael@0 44
michael@0 45 NS_IMETHODIMP nsCacheSession::SetProfileDirectory(nsIFile *profileDir)
michael@0 46 {
michael@0 47 if (StoragePolicy() != nsICache::STORE_OFFLINE && profileDir) {
michael@0 48 // Profile directory override is currently implemented only for
michael@0 49 // offline cache. This is an early failure to prevent the request
michael@0 50 // being processed before it would fail later because of inability
michael@0 51 // to assign a cache base dir.
michael@0 52 return NS_ERROR_UNEXPECTED;
michael@0 53 }
michael@0 54
michael@0 55 mProfileDir = profileDir;
michael@0 56 return NS_OK;
michael@0 57 }
michael@0 58
michael@0 59 NS_IMETHODIMP nsCacheSession::GetProfileDirectory(nsIFile **profileDir)
michael@0 60 {
michael@0 61 if (mProfileDir)
michael@0 62 NS_ADDREF(*profileDir = mProfileDir);
michael@0 63 else
michael@0 64 *profileDir = nullptr;
michael@0 65
michael@0 66 return NS_OK;
michael@0 67 }
michael@0 68
michael@0 69
michael@0 70 NS_IMETHODIMP nsCacheSession::SetDoomEntriesIfExpired(bool doomEntriesIfExpired)
michael@0 71 {
michael@0 72 if (doomEntriesIfExpired) MarkDoomEntriesIfExpired();
michael@0 73 else ClearDoomEntriesIfExpired();
michael@0 74 return NS_OK;
michael@0 75 }
michael@0 76
michael@0 77
michael@0 78 NS_IMETHODIMP
michael@0 79 nsCacheSession::OpenCacheEntry(const nsACString & key,
michael@0 80 nsCacheAccessMode accessRequested,
michael@0 81 bool blockingMode,
michael@0 82 nsICacheEntryDescriptor ** result)
michael@0 83 {
michael@0 84 nsresult rv;
michael@0 85
michael@0 86 if (NS_IsMainThread())
michael@0 87 rv = NS_ERROR_NOT_AVAILABLE;
michael@0 88 else
michael@0 89 rv = nsCacheService::OpenCacheEntry(this,
michael@0 90 key,
michael@0 91 accessRequested,
michael@0 92 blockingMode,
michael@0 93 nullptr, // no listener
michael@0 94 result);
michael@0 95 return rv;
michael@0 96 }
michael@0 97
michael@0 98
michael@0 99 NS_IMETHODIMP nsCacheSession::AsyncOpenCacheEntry(const nsACString & key,
michael@0 100 nsCacheAccessMode accessRequested,
michael@0 101 nsICacheListener *listener,
michael@0 102 bool noWait)
michael@0 103 {
michael@0 104 nsresult rv;
michael@0 105 rv = nsCacheService::OpenCacheEntry(this,
michael@0 106 key,
michael@0 107 accessRequested,
michael@0 108 !noWait,
michael@0 109 listener,
michael@0 110 nullptr); // no result
michael@0 111
michael@0 112 if (rv == NS_ERROR_CACHE_WAIT_FOR_VALIDATION) rv = NS_OK;
michael@0 113 return rv;
michael@0 114 }
michael@0 115
michael@0 116 NS_IMETHODIMP nsCacheSession::EvictEntries()
michael@0 117 {
michael@0 118 return nsCacheService::EvictEntriesForSession(this);
michael@0 119 }
michael@0 120
michael@0 121
michael@0 122 NS_IMETHODIMP nsCacheSession::IsStorageEnabled(bool *result)
michael@0 123 {
michael@0 124
michael@0 125 return nsCacheService::IsStorageEnabledForPolicy(StoragePolicy(), result);
michael@0 126 }
michael@0 127
michael@0 128 NS_IMETHODIMP nsCacheSession::DoomEntry(const nsACString &key,
michael@0 129 nsICacheListener *listener)
michael@0 130 {
michael@0 131 return nsCacheService::DoomEntry(this, key, listener);
michael@0 132 }
michael@0 133
michael@0 134 NS_IMETHODIMP nsCacheSession::GetIsPrivate(bool* aPrivate)
michael@0 135 {
michael@0 136 *aPrivate = IsPrivate();
michael@0 137 return NS_OK;
michael@0 138 }
michael@0 139
michael@0 140 NS_IMETHODIMP nsCacheSession::SetIsPrivate(bool aPrivate)
michael@0 141 {
michael@0 142 if (aPrivate)
michael@0 143 MarkPrivate();
michael@0 144 else
michael@0 145 MarkPublic();
michael@0 146 return NS_OK;
michael@0 147 }

mercurial