From 57fe00ab9aa6c522c56834615448aac52a476764 Mon Sep 17 00:00:00 2001 From: Bobby Holley Date: Sun, 11 Jul 2010 21:01:53 -0400 Subject: [PATCH] Bug 576621 - clearing cache does NOT clear cached images.r=joe,gavin --- browser/base/content/sanitize.js | 6 ++++ .../src/nsPrivateBrowsingService.js | 11 +++++++ modules/libpr0n/src/imgLoader.cpp | 30 +++++++++++++++---- 3 files changed, 41 insertions(+), 6 deletions(-) diff --git a/browser/base/content/sanitize.js b/browser/base/content/sanitize.js index 9128fb122dd9..3834c697e11a 100644 --- a/browser/base/content/sanitize.js +++ b/browser/base/content/sanitize.js @@ -122,6 +122,12 @@ Sanitizer.prototype = { // facility for timespan-based eviction. Wipe it. cacheService.evictEntries(Ci.nsICache.STORE_ANYWHERE); } catch(er) {} + + var imageCache = Cc["@mozilla.org/image/cache;1"]. + getService(Ci.imgICache); + try { + imageCache.clearCache(false); // true=chrome, false=content + } catch(er) {} }, get canClear() diff --git a/browser/components/privatebrowsing/src/nsPrivateBrowsingService.js b/browser/components/privatebrowsing/src/nsPrivateBrowsingService.js index 43295df0050a..e0981f07d900 100644 --- a/browser/components/privatebrowsing/src/nsPrivateBrowsingService.js +++ b/browser/components/privatebrowsing/src/nsPrivateBrowsingService.js @@ -571,6 +571,17 @@ PrivateBrowsingService.prototype = { } } + // Image Cache + let (imageCache = Cc["@mozilla.org/image/cache;1"]. + getService(Ci.imgICache)) { + try { + imageCache.clearCache(false); // true=chrome, false=content + } catch (ex) { + Cu.reportError("Exception thrown while clearing the image cache: " + + ex.toString()); + } + } + // Cookies let (cm = Cc["@mozilla.org/cookiemanager;1"]. getService(Ci.nsICookieManager2)) { diff --git a/modules/libpr0n/src/imgLoader.cpp b/modules/libpr0n/src/imgLoader.cpp index 47360493f84f..10b81b37c32d 100644 --- a/modules/libpr0n/src/imgLoader.cpp +++ b/modules/libpr0n/src/imgLoader.cpp @@ -78,6 +78,7 @@ #include "nsIApplicationCacheContainer.h" #include "nsIMemoryReporter.h" +#include "nsIPrivateBrowsingService.h" // we want to explore making the document own the load group // so we can associate the document URI with the load group. @@ -874,19 +875,36 @@ nsresult imgLoader::Init() prefs->AddObserver("image.http.accept", this, PR_TRUE); + // Listen for when we leave private browsing mode + nsCOMPtr obService = mozilla::services::GetObserverService(); + if (obService) + obService->AddObserver(this, NS_PRIVATE_BROWSING_SWITCH_TOPIC, PR_TRUE); + return NS_OK; } NS_IMETHODIMP imgLoader::Observe(nsISupports* aSubject, const char* aTopic, const PRUnichar* aData) { - NS_ASSERTION(strcmp(aTopic, NS_PREFBRANCH_PREFCHANGE_TOPIC_ID) == 0, - "invalid topic received"); - - if (strcmp(NS_ConvertUTF16toUTF8(aData).get(), "image.http.accept") == 0) { - nsCOMPtr prefs = do_QueryInterface(aSubject); - ReadAcceptHeaderPref(prefs); + // We listen for pref change notifications... + if (!strcmp(aTopic, NS_PREFBRANCH_PREFCHANGE_TOPIC_ID)) { + if (!strcmp(NS_ConvertUTF16toUTF8(aData).get(), "image.http.accept")) { + nsCOMPtr prefs = do_QueryInterface(aSubject); + ReadAcceptHeaderPref(prefs); + } } + + // ...and exits from private browsing. + else if (!strcmp(aTopic, NS_PRIVATE_BROWSING_SWITCH_TOPIC)) { + if (NS_LITERAL_STRING(NS_PRIVATE_BROWSING_LEAVE).Equals(aData)) + ClearImageCache(); + } + + // (Nothing else should bring us here) + else { + NS_ABORT_IF_FALSE(0, "Invalid topic received"); + } + return NS_OK; }