Bug 998445 - Unable to see disk cache size, 'Calculating web content cache size' stays forever, r=michal
authorHonza Bambas <honzab.moz@firemni.cz>
Thu, 24 Apr 2014 18:50:46 +0200
changeset 180353 6cea265963a6cdb001b97cf0fa61aef088d06b84
parent 180352 5a0136e2cc357a4002328f781b8dc8cf8e894a94
child 180354 f8f593eff939439e35431b03fcf0dcf83fbf94d7
push id272
push userpvanderbeken@mozilla.com
push dateMon, 05 May 2014 16:31:18 +0000
reviewersmichal
bugs998445
milestone31.0a1
Bug 998445 - Unable to see disk cache size, 'Calculating web content cache size' stays forever, r=michal
netwerk/cache2/CacheStorageService.cpp
netwerk/cache2/OldWrappers.cpp
netwerk/cache2/OldWrappers.h
--- a/netwerk/cache2/CacheStorageService.cpp
+++ b/netwerk/cache2/CacheStorageService.cpp
@@ -484,110 +484,30 @@ NS_IMETHODIMP CacheStorageService::Purge
   }
 
   nsCOMPtr<nsIRunnable> event =
     new PurgeFromMemoryRunnable(this, what);
 
   return Dispatch(event);
 }
 
-namespace { // anon
-
-class AsyncGetDiskConsumptionWrapper : public nsRunnable,
-                                       public nsICacheStorageVisitor
-{
-public:
-  AsyncGetDiskConsumptionWrapper(nsICacheStorageConsumptionObserver* aCallback);
-  virtual ~AsyncGetDiskConsumptionWrapper() {}
-  NS_DECL_ISUPPORTS_INHERITED
-  NS_DECL_NSICACHESTORAGEVISITOR
-  NS_DECL_NSIRUNNABLE
-
-private:
-  nsCOMPtr<nsICacheStorageConsumptionObserver> mCallback;
-  uint32_t mExpected;
-  uint64_t mSize;
-};
-
-NS_IMPL_ISUPPORTS_INHERITED1(AsyncGetDiskConsumptionWrapper,
-                             nsRunnable,
-                             nsICacheStorageVisitor)
-
-AsyncGetDiskConsumptionWrapper::AsyncGetDiskConsumptionWrapper(
-  nsICacheStorageConsumptionObserver* aCallback)
-  : mCallback(aCallback)
-  , mExpected(2) // expecting two callbacks, from non-anon and anon storage
-  , mSize(0)
-{
-}
-
-NS_IMETHODIMP
-AsyncGetDiskConsumptionWrapper::Run()
-{
-  mCallback->OnNetworkCacheDiskConsumption(mSize);
-  return NS_OK;
-}
-
-NS_IMETHODIMP
-AsyncGetDiskConsumptionWrapper::OnCacheStorageInfo(
-  uint32_t aEntryCount, uint64_t aConsumption)
-{
-  mSize += aConsumption;
-  if (--mExpected == 0)
-    NS_DispatchToMainThread(this);
-
-  return NS_OK;
-}
-
-NS_IMETHODIMP
-AsyncGetDiskConsumptionWrapper::OnCacheEntryInfo(nsICacheEntry *aEntry)
-{
-  MOZ_CRASH("Unexpected");
-  return NS_OK;
-}
-
-NS_IMETHODIMP
-AsyncGetDiskConsumptionWrapper::OnCacheEntryVisitCompleted()
-{
-  MOZ_CRASH("Unexpected");
-  return NS_OK;
-}
-
-} // anon
-
 NS_IMETHODIMP CacheStorageService::AsyncGetDiskConsumption(
   nsICacheStorageConsumptionObserver* aObserver)
 {
   NS_ENSURE_ARG(aObserver);
 
-  if (CacheObserver::UseNewCache()) {
-    return CacheIndex::AsyncGetDiskConsumption(aObserver);
-  }
-
   nsresult rv;
 
-  nsRefPtr<LoadContextInfo> def = GetLoadContextInfo(
-    false, nsILoadContextInfo::NO_APP_ID, false, false);
-  nsRefPtr<LoadContextInfo> anon = GetLoadContextInfo(
-    false, nsILoadContextInfo::NO_APP_ID, false, true);
-
-  nsCOMPtr<nsICacheStorage> defaultStorage;
-  rv = DiskCacheStorage(def, false, getter_AddRefs(defaultStorage));
-  NS_ENSURE_SUCCESS(rv, rv);
-  nsCOMPtr<nsICacheStorage> anonymousStorage;
-  rv = DiskCacheStorage(anon, false, getter_AddRefs(anonymousStorage));
-  NS_ENSURE_SUCCESS(rv, rv);
-
-  nsRefPtr<AsyncGetDiskConsumptionWrapper> visitor =
-    new AsyncGetDiskConsumptionWrapper(aObserver);
-
-  rv = defaultStorage->AsyncVisitStorage(visitor, false);
-  NS_ENSURE_SUCCESS(rv, rv);
-  rv = anonymousStorage->AsyncVisitStorage(visitor, false);
-  NS_ENSURE_SUCCESS(rv, rv);
+  if (CacheObserver::UseNewCache()) {
+    rv = CacheIndex::AsyncGetDiskConsumption(aObserver);
+    NS_ENSURE_SUCCESS(rv, rv);
+  } else {
+    rv = _OldGetDiskConsumption::Get(aObserver);
+    NS_ENSURE_SUCCESS(rv, rv);
+  }
 
   return NS_OK;
 }
 
 NS_IMETHODIMP CacheStorageService::GetIoTarget(nsIEventTarget** aEventTarget)
 {
   NS_ENSURE_ARG(aEventTarget);
 
--- a/netwerk/cache2/OldWrappers.cpp
+++ b/netwerk/cache2/OldWrappers.cpp
@@ -198,16 +198,86 @@ NS_IMETHODIMP VisitCallbackWrapper::Visi
   *_retval = NS_SUCCEEDED(rv);
 
   return NS_OK;
 }
 
 } // anon
 
 
+// _OldGetDiskConsumption
+
+//static
+nsresult _OldGetDiskConsumption::Get(nsICacheStorageConsumptionObserver* aCallback)
+{
+  nsresult rv;
+
+  nsCOMPtr<nsICacheService> serv =
+      do_GetService(NS_CACHESERVICE_CONTRACTID, &rv);
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  nsRefPtr<_OldGetDiskConsumption> cb = new _OldGetDiskConsumption(aCallback);
+
+  // _OldGetDiskConsumption stores the found size value, but until dispatched
+  // to the main thread it doesn't call on the consupmtion observer. See bellow.
+  rv = serv->VisitEntries(cb);
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  // We are called from CacheStorageService::AsyncGetDiskConsumption whose IDL
+  // documentation claims the callback is always delievered asynchronously
+  // back to the main thread.  Despite we know the result synchronosusly when
+  // querying the old cache, we need to stand the word and dispatch the result
+  // to the main thread asynchronously.  Hence the dispatch here.
+  return NS_DispatchToMainThread(cb);
+}
+
+NS_IMPL_ISUPPORTS_INHERITED1(_OldGetDiskConsumption,
+                             nsRunnable,
+                             nsICacheVisitor)
+
+_OldGetDiskConsumption::_OldGetDiskConsumption(
+  nsICacheStorageConsumptionObserver* aCallback)
+  : mCallback(aCallback)
+  , mSize(0)
+{
+}
+
+NS_IMETHODIMP
+_OldGetDiskConsumption::Run()
+{
+  mCallback->OnNetworkCacheDiskConsumption(mSize);
+  return NS_OK;
+}
+
+NS_IMETHODIMP
+_OldGetDiskConsumption::VisitDevice(const char * deviceID,
+                                    nsICacheDeviceInfo *deviceInfo,
+                                    bool *_retval)
+{
+  if (!strcmp(deviceID, "disk")) {
+    uint32_t size;
+    nsresult rv = deviceInfo->GetTotalSize(&size);
+    if (NS_SUCCEEDED(rv))
+      mSize = (int64_t)size;
+  }
+
+  *_retval = false;
+  return NS_OK;
+}
+
+NS_IMETHODIMP
+_OldGetDiskConsumption::VisitEntry(const char * deviceID,
+                                   nsICacheEntryInfo *entryInfo,
+                                   bool *_retval)
+{
+  MOZ_CRASH("Unexpected");
+  return NS_OK;
+}
+
+
 // _OldCacheEntryWrapper
 
 _OldCacheEntryWrapper::_OldCacheEntryWrapper(nsICacheEntryDescriptor* desc)
 : mOldDesc(desc), mOldInfo(desc)
 {
   MOZ_COUNT_CTOR(_OldCacheEntryWrapper);
   LOG(("Creating _OldCacheEntryWrapper %p for descriptor %p", this, desc));
 }
--- a/netwerk/cache2/OldWrappers.h
+++ b/netwerk/cache2/OldWrappers.h
@@ -10,16 +10,17 @@
 #include "nsCOMPtr.h"
 #include "nsICacheEntryOpenCallback.h"
 #include "nsICacheEntryDescriptor.h"
 #include "nsThreadUtils.h"
 #include "mozilla/TimeStamp.h"
 
 class nsIURI;
 class nsICacheEntryOpenCallback;
+class nsICacheStorageConsumptionObserver;
 class nsIApplicationCache;
 class nsILoadContextInfo;
 
 namespace mozilla { namespace net {
 
 class CacheStorage;
 
 class _OldCacheEntryWrapper : public nsICacheEntry
@@ -116,11 +117,28 @@ private:
 
   nsCOMPtr<nsILoadContextInfo> mLoadInfo;
   nsCOMPtr<nsIApplicationCache> mAppCache;
   bool const mWriteToDisk : 1;
   bool const mLookupAppCache : 1;
   bool const mOfflineStorage : 1;
 };
 
+class _OldGetDiskConsumption : public nsRunnable,
+                               public nsICacheVisitor
+{
+public:
+  static nsresult Get(nsICacheStorageConsumptionObserver* aCallback);
+
+private:
+  _OldGetDiskConsumption(nsICacheStorageConsumptionObserver* aCallback);
+  virtual ~_OldGetDiskConsumption() {}
+  NS_DECL_ISUPPORTS_INHERITED
+  NS_DECL_NSICACHEVISITOR
+  NS_DECL_NSIRUNNABLE
+
+  nsCOMPtr<nsICacheStorageConsumptionObserver> mCallback;
+  int64_t mSize;
+};
+
 }}
 
 #endif