Bug 961024 - Move HTTP cache on Android to application's cache/profile leaf name, r=michal
authorHonza Bambas <honzab.moz@firemni.cz>
Tue, 03 Jun 2014 14:35:17 +0200
changeset 206649 757c578c35742b97640aebaa7042438935f40749
parent 206648 f094c23e88e9d4efb1229f2ef26989ad633eafd7
child 206650 7a460ea73edac3bd75d9399912418d4b87fe5c83
push id494
push userraliiev@mozilla.com
push dateMon, 25 Aug 2014 18:42:16 +0000
treeherdermozilla-release@a3cc3e46b571 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmichal
bugs961024
milestone32.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 961024 - Move HTTP cache on Android to application's cache/profile leaf name, r=michal
netwerk/cache2/CacheFileIOManager.cpp
netwerk/cache2/CacheFileIOManager.h
netwerk/cache2/CacheStorageService.cpp
--- a/netwerk/cache2/CacheFileIOManager.cpp
+++ b/netwerk/cache2/CacheFileIOManager.cpp
@@ -1337,20 +1337,43 @@ CacheFileIOManager::OnProfile()
 
   nsresult rv;
 
   nsCOMPtr<nsIFile> directory;
 
   CacheObserver::ParentDirOverride(getter_AddRefs(directory));
 
 #if defined(MOZ_WIDGET_ANDROID)
+  nsCOMPtr<nsIFile> profilelessDirectory;
   char* cachePath = getenv("CACHE_DIRECTORY");
   if (!directory && cachePath && *cachePath) {
     rv = NS_NewNativeLocalFile(nsDependentCString(cachePath),
                                true, getter_AddRefs(directory));
+    if (NS_SUCCEEDED(rv)) {
+      // Save this directory as the profileless path.
+      rv = directory->Clone(getter_AddRefs(profilelessDirectory));
+      NS_ENSURE_SUCCESS(rv, rv);
+
+      // Add profile leaf name to the directory name to distinguish
+      // multiple profiles Fennec supports.
+      nsCOMPtr<nsIFile> profD;
+      rv = NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR,
+                                  getter_AddRefs(profD));
+
+      nsAutoCString leafName;
+      if (NS_SUCCEEDED(rv)) {
+        rv = profD->GetNativeLeafName(leafName);
+      }
+      if (NS_SUCCEEDED(rv)) {
+        rv = directory->AppendNative(leafName);
+      }
+      if (NS_FAILED(rv)) {
+        directory = nullptr;
+      }
+    }
   }
 #endif
 
   if (!directory) {
     rv = NS_GetSpecialDirectory(NS_APP_CACHE_PARENT_DIR,
                                 getter_AddRefs(directory));
   }
 
@@ -1362,16 +1385,25 @@ CacheFileIOManager::OnProfile()
   if (directory) {
     rv = directory->Append(NS_LITERAL_STRING("cache2"));
     NS_ENSURE_SUCCESS(rv, rv);
   }
 
   // All functions return a clone.
   ioMan->mCacheDirectory.swap(directory);
 
+#if defined(MOZ_WIDGET_ANDROID)
+  if (profilelessDirectory) {
+    rv = profilelessDirectory->Append(NS_LITERAL_STRING("cache2"));
+    NS_ENSURE_SUCCESS(rv, rv);
+  }
+
+  ioMan->mCacheProfilelessDirectory.swap(profilelessDirectory);
+#endif
+
   if (ioMan->mCacheDirectory) {
     CacheIndex::Init(ioMan->mCacheDirectory);
   }
 
   return NS_OK;
 }
 
 // static
@@ -2216,20 +2248,36 @@ void CacheFileIOManager::GetCacheDirecto
 {
   *result = nullptr;
 
   nsRefPtr<CacheFileIOManager> ioMan = gInstance;
   if (!ioMan) {
     return;
   }
 
-  nsCOMPtr<nsIFile> file = ioMan->mCacheDirectory;
-  file.forget(result);
+  ioMan->mCacheDirectory->Clone(result);
 }
 
+#if defined(MOZ_WIDGET_ANDROID)
+
+// static
+void CacheFileIOManager::GetProfilelessCacheDirectory(nsIFile** result)
+{
+  *result = nullptr;
+
+  nsRefPtr<CacheFileIOManager> ioMan = gInstance;
+  if (!ioMan) {
+    return;
+  }
+
+  ioMan->mCacheProfilelessDirectory->Clone(result);
+}
+
+#endif
+
 // static
 nsresult
 CacheFileIOManager::GetEntryInfo(const SHA1Sum::Hash *aHash,
                                  CacheStorageService::EntryInfoCallback *aCallback)
 {
   MOZ_ASSERT(CacheFileIOManager::IsOnIOThread());
 
   nsresult rv;
--- a/netwerk/cache2/CacheFileIOManager.h
+++ b/netwerk/cache2/CacheFileIOManager.h
@@ -275,16 +275,19 @@ public:
   static nsresult UpdateIndexEntry();
 
   enum EEnumerateMode {
     ENTRIES,
     DOOMED
   };
 
   static void GetCacheDirectory(nsIFile** result);
+#if defined(MOZ_WIDGET_ANDROID)
+  static void GetProfilelessCacheDirectory(nsIFile** result);
+#endif
 
   // Calls synchronously OnEntryInfo for an entry with the given hash.
   // Tries to find an existing entry in the service hashtables first, if not
   // found, loads synchronously from disk file.
   // Callable on the IO thread only.
   static nsresult GetEntryInfo(const SHA1Sum::Hash *aHash,
                                CacheStorageService::EntryInfoCallback *aCallback);
 
@@ -378,16 +381,23 @@ private:
   // Memory reporting (private part)
   size_t SizeOfExcludingThisInternal(mozilla::MallocSizeOf mallocSizeOf) const;
 
   static CacheFileIOManager           *gInstance;
   TimeStamp                            mStartTime;
   bool                                 mShuttingDown;
   nsRefPtr<CacheIOThread>              mIOThread;
   nsCOMPtr<nsIFile>                    mCacheDirectory;
+#if defined(MOZ_WIDGET_ANDROID)
+  // On Android we add the active profile directory name between the path
+  // and the 'cache2' leaf name.  However, to delete any leftover data from
+  // times before we were doing it, we still need to access the directory
+  // w/o the profile name in the path.  Here it is stored.
+  nsCOMPtr<nsIFile>                    mCacheProfilelessDirectory;
+#endif
   bool                                 mTreeCreated;
   CacheFileHandles                     mHandles;
   nsTArray<CacheFileHandle *>          mHandlesByLastUsed;
   nsTArray<CacheFileHandle *>          mSpecialHandles;
   nsTArray<nsRefPtr<CacheFile> >       mScheduledMetadataWrites;
   nsCOMPtr<nsITimer>                   mMetadataWritesTimer;
   bool                                 mOverLimitEvicting;
   bool                                 mRemovingTrashDirs;
--- a/netwerk/cache2/CacheStorageService.cpp
+++ b/netwerk/cache2/CacheStorageService.cpp
@@ -543,21 +543,27 @@ public:
   static bool Post(uint32_t aVersion, uint32_t aActive);
 
 private:
   CleaupCacheDirectoriesRunnable(uint32_t aVersion, uint32_t aActive)
     : mVersion(aVersion), mActive(aActive)
   {
     nsCacheService::GetDiskCacheDirectory(getter_AddRefs(mCache1Dir));
     CacheFileIOManager::GetCacheDirectory(getter_AddRefs(mCache2Dir));
+#if defined(MOZ_WIDGET_ANDROID)
+    CacheFileIOManager::GetProfilelessCacheDirectory(getter_AddRefs(mCache2Profileless));
+#endif
   }
 
   virtual ~CleaupCacheDirectoriesRunnable() {}
   uint32_t mVersion, mActive;
   nsCOMPtr<nsIFile> mCache1Dir, mCache2Dir;
+#if defined(MOZ_WIDGET_ANDROID)
+  nsCOMPtr<nsIFile> mCache2Profileless;
+#endif
 };
 
 // static
 bool CleaupCacheDirectoriesRunnable::Post(uint32_t aVersion, uint32_t aActive)
 {
   // CleaupCacheDirectories is called regardless what cache version is set up to use.
   // To obtain the cache1 directory we must unfortunatelly instantiate the old cache
   // service despite it may not be used at all...  This also initialize nsDeleteDir.
@@ -581,16 +587,22 @@ NS_IMETHODIMP CleaupCacheDirectoriesRunn
   MOZ_ASSERT(!NS_IsMainThread());
 
   if (mCache1Dir) {
     nsDeleteDir::RemoveOldTrashes(mCache1Dir);
   }
   if (mCache2Dir) {
     nsDeleteDir::RemoveOldTrashes(mCache2Dir);
   }
+#if defined(MOZ_WIDGET_ANDROID)
+  if (mCache2Profileless) {
+    // Always delete the profileless cache on Android
+    nsDeleteDir::DeleteDir(mCache2Profileless, true, 30000);
+  }
+#endif
 
   // Delete the non-active version cache data right now
   if (mVersion == mActive) {
     return NS_OK;
   }
 
   switch (mVersion) {
   case 0: