[tor-commits] [tor-browser/tor-browser-17.0.9esr-1] fixup! Isolate the Image Cache per url bar domain.

mikeperry at torproject.org mikeperry at torproject.org
Thu Oct 3 02:40:30 UTC 2013


commit a7d5d182fe32eb7f5ae8d3149e5f0c7f3dbfb11b
Author: Mike Perry <mikeperry-git at torproject.org>
Date:   Wed Oct 2 19:30:06 2013 -0700

    fixup! Isolate the Image Cache per url bar domain.
    
    Make the image cache use the simplified GetFirstPartyURI API.
---
 image/src/imgLoader.cpp  |   80 ++++++++++++++++++++++------------------------
 image/src/imgLoader.h    |    5 ++-
 image/src/imgRequest.cpp |    2 +-
 3 files changed, 43 insertions(+), 44 deletions(-)

diff --git a/image/src/imgLoader.cpp b/image/src/imgLoader.cpp
index 8e52af8..3a26cf4 100644
--- a/image/src/imgLoader.cpp
+++ b/image/src/imgLoader.cpp
@@ -59,7 +59,6 @@
 #include "nsIHttpChannelInternal.h"  
 #include "nsIContentSecurityPolicy.h"
 #include "nsIChannelPolicy.h"
-#include "mozIThirdPartyUtil.h"
 
 #include "nsContentUtils.h"
 
@@ -779,6 +778,8 @@ imgCacheQueue imgLoader::sChromeCacheQueue;
 double imgLoader::sCacheTimeWeight;
 uint32_t imgLoader::sCacheMaxSize;
 
+nsCOMPtr<mozIThirdPartyUtil> imgLoader::sThirdPartyUtilSvc;
+
 NS_IMPL_ISUPPORTS5(imgLoader, imgILoader, nsIContentSniffer, imgICache, nsISupportsWeakReference, nsIObserver)
 
 imgLoader::imgLoader()
@@ -879,6 +880,8 @@ nsresult imgLoader::Init()
   if (obService)
     obService->AddObserver(this, NS_PRIVATE_BROWSING_SWITCH_TOPIC, true);
 
+  sThirdPartyUtilSvc = do_GetService(THIRDPARTYUTIL_CONTRACTID);
+
   return NS_OK;
 }
 
@@ -992,6 +995,7 @@ void imgLoader::Shutdown()
   NS_IF_RELEASE(gCacheObserver);
   delete gCacheTracker;
   gCacheTracker = nullptr;
+  sThirdPartyUtilSvc = nullptr;
 }
 
 nsresult imgLoader::ClearChromeImageCache()
@@ -1103,7 +1107,7 @@ bool imgLoader::SetHasProxies(nsIURI *firstPartyURI, nsIURI *imgURI)
 
   LOG_STATIC_FUNC_WITH_PARAM(gImgLog, "imgLoader::SetHasProxies", "uri", spec.get());
 
-  nsCAutoString key = GetCacheKey(firstPartyURI, imgURI);
+  nsCAutoString key = GetCacheKey(firstPartyURI, imgURI, nullptr);
   nsRefPtr<imgCacheEntry> entry;
   if (cache.Get(key, getter_AddRefs(entry)) && entry && entry->HasNoProxies()) {
     imgCacheQueue &queue = GetCacheQueue(imgURI);
@@ -1462,7 +1466,7 @@ bool imgLoader::RemoveFromCache(imgCacheEntry *entry)
     if (imgURI) {
       imgCacheTable &cache = GetCache(imgURI);
       imgCacheQueue &queue = GetCacheQueue(imgURI);
-      nsCAutoString spec = GetCacheKey(firstPartyURI, imgURI);
+      nsCAutoString spec = GetCacheKey(firstPartyURI, imgURI, nullptr);
 
       LOG_STATIC_FUNC_WITH_PARAM(gImgLog, "imgLoader::RemoveFromCache", "entry's uri", spec.get());
 
@@ -1548,7 +1552,8 @@ NS_IMETHODIMP imgLoader::LoadImage(nsIURI *aURI,
   if (!aURI)
     return NS_ERROR_NULL_POINTER;
 
-  nsCAutoString spec = GetCacheKey(aFirstPartyURI, aURI);
+  bool isIsolated = false;
+  nsCAutoString spec = GetCacheKey(aFirstPartyURI, aURI, &isIsolated);
 
   LOG_SCOPE_WITH_PARAM(gImgLog, "imgLoader::LoadImage", "aURI", spec.get());
 
@@ -1710,8 +1715,8 @@ NS_IMETHODIMP imgLoader::LoadImage(nsIURI *aURI,
       return openRes;
     }
 
-    // Try to add the new request into the cache.
-    PutIntoCache(spec, entry);
+    if (isIsolated) // Try to add the new request into the cache.
+      PutIntoCache(spec, entry);
   } else {
     LOG_MSG_WITH_PARAM(gImgLog, 
                        "imgLoader::LoadImage |cache hit|", "request", request);
@@ -1771,41 +1776,26 @@ NS_IMETHODIMP imgLoader::LoadImage(nsIURI *aURI,
   return NS_OK;
 }
 
-nsCAutoString imgLoader::GetCacheKey(nsIURI *firstPartyURI, nsIURI *imgURI)
+nsCAutoString imgLoader::GetCacheKey(nsIURI *firstPartyURI, nsIURI *imgURI,
+                                     bool *isIsolated)
 {
   NS_ASSERTION(imgURI, "imgLoader::GetCacheKey -- NULL imgURI");
+  if (isIsolated)
+    *isIsolated = false;
 
-  nsCAutoString spec, hostKey;
+  nsCAutoString spec;
   if (imgURI)
     imgURI->GetSpec(spec);
 
-#if 0
-  bool isChrome = false;
-  if (imgURI)
-    imgURI->SchemeIs("chrome", &isChrome);
-  if (isChrome)
-    return spec;  // No partitioning needed for chrome; just use a simple key.
-#endif
+  nsCAutoString hostKey;
+  if (firstPartyURI && sThirdPartyUtilSvc)
+    sThirdPartyUtilSvc->GetFirstPartyHostForIsolation(firstPartyURI, hostKey);
 
-  // FIXME: Should we use mozIThirdPartyUtil to get a domain from this?
-  if (firstPartyURI)
-    firstPartyURI->GetHost(hostKey);
-  else {
+  if (hostKey.Length() > 0) {
+    if (isIsolated)
+      *isIsolated = true;
+  } else
     hostKey = "--NoFirstParty--";
-    nsCOMPtr<nsIConsoleService> consoleSvc =
-                                do_GetService(NS_CONSOLESERVICE_CONTRACTID);
-    if (consoleSvc) {
-      nsAutoString msg(NS_LITERAL_STRING(
-                       "imgLoader::GetCacheKey: NULL firstPartyURI for ")
-                       .get());
-      msg.AppendASCII(spec.get());
-      consoleSvc->LogStringMessage(msg.get());
-    }
-
-#ifdef DEBUG
-    printf("imgLoader::GetCacheKey: NULL firstPartyURI for %s\n", spec.get());
-#endif
-  }
 
   // Make a new key using host
   // FIXME: This might involve a couple more copies than necessary.. 
@@ -1819,23 +1809,24 @@ NS_IMETHODIMP imgLoader::LoadImageWithChannel(nsIChannel *channel, imgIDecoderOb
 {
   NS_ASSERTION(channel, "imgLoader::LoadImageWithChannel -- NULL channel pointer");
 
+  if (!sThirdPartyUtilSvc)
+    return NS_ERROR_FAILURE;
+   
   nsRefPtr<imgRequest> request;
 
   nsCOMPtr<nsIURI> uri;
   channel->GetURI(getter_AddRefs(uri));
 
   nsCOMPtr<nsIURI> firstPartyURI;
-  nsCOMPtr<mozIThirdPartyUtil> thirdPartySvc
-                               = do_GetService(THIRDPARTYUTIL_CONTRACTID);
-  thirdPartySvc->GetFirstPartyURI(channel, nullptr,
-                                  getter_AddRefs(firstPartyURI));
+  sThirdPartyUtilSvc->GetFirstPartyURI(channel, nullptr,
+                                       getter_AddRefs(firstPartyURI));
 
   nsLoadFlags requestFlags = nsIRequest::LOAD_NORMAL;
   channel->GetLoadFlags(&requestFlags);
 
   nsRefPtr<imgCacheEntry> entry;
   imgCacheTable &cache = GetCache(uri);
-  nsCAutoString spec = GetCacheKey(firstPartyURI, uri);
+  nsCAutoString spec = GetCacheKey(firstPartyURI, uri, nullptr);
 
   if (requestFlags & nsIRequest::LOAD_BYPASS_CACHE) {
     imgCacheQueue &queue = GetCacheQueue(uri);
@@ -1927,8 +1918,10 @@ NS_IMETHODIMP imgLoader::LoadImageWithChannel(nsIChannel *channel, imgIDecoderOb
 
     NS_RELEASE(pl);
 
-    // Try to add the new request into the cache.
-    PutIntoCache(GetCacheKey(firstPartyURI, originalURI), entry);
+    bool isIsolated = false;
+    nsCAutoString key = GetCacheKey(firstPartyURI, originalURI, &isIsolated);
+    if (isIsolated) // Try to add the new request into the cache.
+      PutIntoCache(key, entry);
 
     rv = CreateNewProxyForRequest(request, loadGroup, aObserver,
                                   requestFlags, nullptr, _retval);
@@ -2234,8 +2227,11 @@ NS_IMETHODIMP imgCacheValidator::OnStartRequest(nsIRequest *aRequest, nsISupport
   // Try to add the new request into the cache. Note that the entry must be in
   // the cache before the proxies' ownership changes, because adding a proxy
   // changes the caching behaviour for imgRequests.
-  sImgLoader.PutIntoCache(imgLoader::GetCacheKey(firstPartyURI, originalURI),
-                          mNewEntry);
+  bool isIsolated = false;
+  nsCAutoString key = imgLoader::GetCacheKey(firstPartyURI, originalURI,
+                                             &isIsolated);
+  if (isIsolated)
+    sImgLoader.PutIntoCache(key, mNewEntry);
 
   uint32_t count = mProxies.Count();
   for (int32_t i = count-1; i>=0; i--) {
diff --git a/image/src/imgLoader.h b/image/src/imgLoader.h
index c275d83..3e91edb 100644
--- a/image/src/imgLoader.h
+++ b/image/src/imgLoader.h
@@ -22,6 +22,7 @@
 #include "nsIChannelPolicy.h"
 #include "nsIProgressEventSink.h"
 #include "nsIChannel.h"
+#include "mozIThirdPartyUtil.h"
 
 #ifdef LOADER_THREADSAFE
 #include "prlock.h"
@@ -228,7 +229,8 @@ public:
   static nsresult InitCache();
 
   static nsCAutoString GetCacheKey(nsIURI *firstPartyURI,
-                                   nsIURI *imgURI);
+                                   nsIURI *imgURI,
+                                   bool *isIsolated);
   static bool RemoveFromCache(imgCacheEntry *entry);
   static bool PutIntoCache(nsCAutoString key, imgCacheEntry *entry);
   static bool RemoveMatchingUrlsFromCache(nsIURI *aKey);
@@ -333,6 +335,7 @@ private: // data
   static double sCacheTimeWeight;
   static uint32_t sCacheMaxSize;
 
+  static nsCOMPtr<mozIThirdPartyUtil> sThirdPartyUtilSvc;
   nsCString mAcceptHeader;
 };
 
diff --git a/image/src/imgRequest.cpp b/image/src/imgRequest.cpp
index 71afe2c..5b5232a 100644
--- a/image/src/imgRequest.cpp
+++ b/image/src/imgRequest.cpp
@@ -334,7 +334,7 @@ void imgRequest::RemoveFromCache()
     else {
       imgLoader::RemoveKeyFromCache(imgLoader::GetCache(mURI),
                                     imgLoader::GetCacheQueue(mURI),
-                                    imgLoader::GetCacheKey(mFirstPartyURI, mURI));
+                                    imgLoader::GetCacheKey(mFirstPartyURI, mURI, nullptr));
     }
   }
 



More information about the tor-commits mailing list