From 8347459dd826bd4fb4696cd8f43cf169c9dfa739 Mon Sep 17 00:00:00 2001
From: Mike Perry <mikeperry-git@torproject.org>
Date: Tue, 4 Dec 2012 16:25:52 -0800
Subject: [PATCH 14/41] Make nsICacheService.EvictEntries synchronous

This fixes a race condition that allows cache-based EverCookies to persist for
a brief time (on the order of minutes?) after cache clearing/"New Identity".

https://trac.torproject.org/projects/tor/ticket/5715
---
 netwerk/cache/nsCacheService.cpp | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/netwerk/cache/nsCacheService.cpp b/netwerk/cache/nsCacheService.cpp
index e88de402..5035f68 100644
--- a/netwerk/cache/nsCacheService.cpp
+++ b/netwerk/cache/nsCacheService.cpp
@@ -1555,7 +1555,21 @@ NS_IMETHODIMP nsCacheService::VisitEntries(nsICacheVisitor *visitor)
 
 NS_IMETHODIMP nsCacheService::EvictEntries(nsCacheStoragePolicy storagePolicy)
 {
-    return  EvictEntriesForClient(nullptr, storagePolicy);
+    NS_IMETHODIMP r;
+    r = EvictEntriesForClient(nullptr, storagePolicy);
+
+    // XXX: Bloody hack until we get this notifier in FF14.0:
+    // https://developer.mozilla.org/en/XPCOM_Interface_Reference/nsICacheListener#onCacheEntryDoomed%28%29
+    if (storagePolicy == nsICache::STORE_ANYWHERE &&
+            NS_IsMainThread() && gService && gService->mInitialized) {
+        nsCacheServiceAutoLock lock(LOCK_TELEM(NSCACHESERVICE_EVICTENTRIESFORCLIENT));
+        gService->mClearingEntries = true;
+        gService->DoomActiveEntries();
+        gService->ClearDoomList();
+        (void) SyncWithCacheIOThread();
+        gService->mClearingEntries = false;
+    }
+    return r; 
 }
 
 NS_IMETHODIMP nsCacheService::GetCacheIOTarget(nsIEventTarget * *aCacheIOTarget)
-- 
1.8.1.2

