Bug 922081 - respect browser.cache.disk.parent_directory preference in the new cache, r=michal
authorHonza Bambas <honzab.moz@firemni.cz>
Thu, 27 Feb 2014 00:11:38 +0100
changeset 180051 c486ad9a89acd41b4261285b91762f9cc5a7abaa
parent 180050 8c60c6fa67c9323bb4476e46d8784218ee4dd2fd
child 180052 a8cb3454d250df05a7fa57d1eb131cd3c455c547
push idunknown
push userunknown
push dateunknown
reviewersmichal
bugs922081
milestone30.0a1
Bug 922081 - respect browser.cache.disk.parent_directory preference in the new cache, r=michal
netwerk/cache2/CacheFileIOManager.cpp
netwerk/cache2/CacheObserver.cpp
netwerk/cache2/CacheObserver.h
--- a/netwerk/cache2/CacheFileIOManager.cpp
+++ b/netwerk/cache2/CacheFileIOManager.cpp
@@ -1203,19 +1203,21 @@ CacheFileIOManager::OnProfile()
     // thread, just go with it...
     return NS_ERROR_NOT_INITIALIZED;
   }
 
   nsresult rv;
 
   nsCOMPtr<nsIFile> directory;
 
+  CacheObserver::ParentDirOverride(getter_AddRefs(directory));
+
 #if defined(MOZ_WIDGET_ANDROID)
   char* cachePath = getenv("CACHE_DIRECTORY");
-  if (cachePath && *cachePath) {
+  if (!directory && cachePath && *cachePath) {
     rv = NS_NewNativeLocalFile(nsDependentCString(cachePath),
                                true, getter_AddRefs(directory));
   }
 #endif
 
   if (!directory) {
     rv = NS_GetSpecialDirectory(NS_APP_CACHE_PARENT_DIR,
                                 getter_AddRefs(directory));
@@ -1224,21 +1226,23 @@ CacheFileIOManager::OnProfile()
   if (!directory) {
     rv = NS_GetSpecialDirectory(NS_APP_USER_PROFILE_LOCAL_50_DIR,
                                 getter_AddRefs(directory));
   }
 
   if (directory) {
     rv = directory->Append(NS_LITERAL_STRING("cache2"));
     NS_ENSURE_SUCCESS(rv, rv);
-
-    rv = directory->Clone(getter_AddRefs(ioMan->mCacheDirectory));
-    NS_ENSURE_SUCCESS(rv, rv);
-
-    CacheIndex::Init(directory);
+  }
+
+  // All functions return a clone.
+  ioMan->mCacheDirectory.swap(directory);
+
+  if (ioMan->mCacheDirectory) {
+    CacheIndex::Init(ioMan->mCacheDirectory);
   }
 
   return NS_OK;
 }
 
 // static
 already_AddRefed<nsIEventTarget>
 CacheFileIOManager::IOTarget()
--- a/netwerk/cache2/CacheObserver.cpp
+++ b/netwerk/cache2/CacheObserver.cpp
@@ -54,16 +54,17 @@ uint32_t CacheObserver::sMaxDiskEntrySiz
 
 static uint32_t const kDefaultCompressionLevel = 1;
 uint32_t CacheObserver::sCompressionLevel = kDefaultCompressionLevel;
 
 NS_IMPL_ISUPPORTS2(CacheObserver,
                    nsIObserver,
                    nsISupportsWeakReference)
 
+// static
 nsresult
 CacheObserver::Init()
 {
   if (sSelf) {
     return NS_OK;
   }
 
   nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
@@ -81,16 +82,17 @@ CacheObserver::Init()
   obs->AddObserver(sSelf, "xpcom-shutdown", true);
   obs->AddObserver(sSelf, "last-pb-context-exited", true);
   obs->AddObserver(sSelf, "webapps-clear-data", true);
   obs->AddObserver(sSelf, "memory-pressure", true);
 
   return NS_OK;
 }
 
+// static
 nsresult
 CacheObserver::Shutdown()
 {
   if (!sSelf) {
     return NS_ERROR_NOT_INITIALIZED;
   }
 
   NS_RELEASE(sSelf);
@@ -123,16 +125,20 @@ CacheObserver::AttachToPreferences()
     &sMaxMemoryEntrySize, "browser.cache.memory.max_entry_size", kDefaultMaxMemoryEntrySize);
   mozilla::Preferences::AddUintVarCache(
     &sMaxDiskEntrySize, "browser.cache.disk.max_entry_size", kDefaultMaxDiskEntrySize);
 
   // http://mxr.mozilla.org/mozilla-central/source/netwerk/cache/nsCacheEntryDescriptor.cpp#367
   mozilla::Preferences::AddUintVarCache(
     &sCompressionLevel, "browser.cache.compression_level", kDefaultCompressionLevel);
 
+  mozilla::Preferences::GetComplex(
+    "browser.cache.disk.parent_directory", NS_GET_IID(nsIFile),
+    getter_AddRefs(mCacheParentDirectoryOverride));
+
   sHalfLifeExperiment = mozilla::Preferences::GetInt(
     "browser.cache.frecency_experiment", kDefaultHalfLifeExperiment);
 
   if (sHalfLifeExperiment == 0) {
     // The experiment has not yet been initialized, do it now
     // Store the experiemnt value, since we need it not to change between
     // browser sessions.
     srand(time(NULL));
@@ -193,16 +199,32 @@ bool const CacheObserver::UseNewCache()
 
     case 1: // use the new cache backend
       return true;
   }
 
   return true;
 }
 
+// static
+void CacheObserver::ParentDirOverride(nsIFile** aDir)
+{
+  if (NS_WARN_IF(!aDir))
+    return;
+
+  *aDir = nullptr;
+
+  if (!sSelf)
+    return;
+  if (!sSelf->mCacheParentDirectoryOverride)
+    return;
+
+  sSelf->mCacheParentDirectoryOverride->Clone(aDir);
+}
+
 namespace { // anon
 
 class CacheStorageEvictHelper
 {
 public:
   nsresult Run(mozIApplicationClearPrivateDataParams* aParams);
 
 private:
--- a/netwerk/cache2/CacheObserver.h
+++ b/netwerk/cache2/CacheObserver.h
@@ -1,16 +1,18 @@
 /* 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/. */
 
 #ifndef CacheObserver__h__
 #define CacheObserver__h__
 
 #include "nsIObserver.h"
+#include "nsIFile.h"
+#include "nsCOMPtr.h"
 #include "nsWeakReference.h"
 #include <algorithm>
 
 namespace mozilla {
 namespace net {
 
 class CacheObserver : public nsIObserver
                     , public nsSupportsWeakReference
@@ -39,16 +41,17 @@ class CacheObserver : public nsIObserver
   static uint32_t const MaxDiskEntrySize() // result in bytes.
     { return sMaxDiskEntrySize << 10; }
   static uint32_t const CompressionLevel()
     { return sCompressionLevel; }
   static uint32_t const HalfLifeSeconds()
     { return sHalfLifeHours * 60 * 60; }
   static int32_t const HalfLifeExperiment()
     { return sHalfLifeExperiment; }
+  static void ParentDirOverride(nsIFile ** aDir);
 
   static bool const EntryIsTooBig(int64_t aSize, bool aUsingDisk);
 
 private:
   static CacheObserver* sSelf;
 
   void AttachToPreferences();
   void SchduleAutoDelete();
@@ -58,14 +61,17 @@ private:
   static bool sUseMemoryCache;
   static uint32_t sMemoryLimit;
   static uint32_t sDiskCacheCapacity;
   static uint32_t sMaxMemoryEntrySize;
   static uint32_t sMaxDiskEntrySize;
   static uint32_t sCompressionLevel;
   static uint32_t sHalfLifeHours;
   static int32_t sHalfLifeExperiment;
+
+  // Non static properties, accessible via sSelf
+  nsCOMPtr<nsIFile> mCacheParentDirectoryOverride;
 };
 
 } // net
 } // mozilla
 
 #endif