Bug 1312954 - part 2: Add a new interface nsICacheStorageService.asyncVisitAllStorages() to allow visiting every cache entry. r?mayhemer draft
authorTim Huang <tihuang@mozilla.com>
Thu, 19 Jan 2017 16:03:10 +0800
changeset 465996 c7c6452b312a9d0591cad8981936312ed61c346f
parent 465995 37d1e5ab846ef60883c5ede1e47ee1942d267d42
child 465997 4c0095a9055cb5b6c365b1a09712f8fc23ac03f5
push id42767
push userbmo:tihuang@mozilla.com
push dateWed, 25 Jan 2017 04:18:41 +0000
reviewersmayhemer
bugs1312954
milestone54.0a1
Bug 1312954 - part 2: Add a new interface nsICacheStorageService.asyncVisitAllStorages() to allow visiting every cache entry. r?mayhemer
netwerk/cache2/CacheIndex.cpp
netwerk/cache2/CacheIndex.h
netwerk/cache2/CacheStorageService.cpp
netwerk/cache2/nsICacheStorageService.idl
--- a/netwerk/cache2/CacheIndex.cpp
+++ b/netwerk/cache2/CacheIndex.cpp
@@ -1327,26 +1327,22 @@ CacheIndex::GetCacheStats(nsILoadContext
   if (!index) {
     return NS_ERROR_NOT_INITIALIZED;
   }
 
   if (!index->IsIndexUsable()) {
     return NS_ERROR_NOT_AVAILABLE;
   }
 
-  if (!aInfo) {
-    return NS_ERROR_INVALID_ARG;
-  }
-
   *aSize = 0;
   *aCount = 0;
 
   for (auto iter = index->mFrecencyArray.Iter(); !iter.Done(); iter.Next()) {
     CacheIndexRecord *record = iter.Get();
-    if (!CacheIndexEntry::RecordMatchesLoadContextInfo(record, aInfo))
+    if (aInfo && !CacheIndexEntry::RecordMatchesLoadContextInfo(record, aInfo))
       continue;
 
     *aSize += CacheIndexEntry::GetFileSize(record);
     ++*aCount;
   }
 
   return NS_OK;
 }
--- a/netwerk/cache2/CacheIndex.h
+++ b/netwerk/cache2/CacheIndex.h
@@ -259,16 +259,18 @@ public:
          this, LOGSHA1(mRec->mHash), IsFresh(), IsInitialized(), IsRemoved(),
          IsDirty(), Anonymous(), OriginAttrsHash(), GetFrecency(),
          GetExpirationTime(), GetFileSize()));
   }
 
   static bool RecordMatchesLoadContextInfo(CacheIndexRecord *aRec,
                                            nsILoadContextInfo *aInfo)
   {
+    MOZ_ASSERT(aInfo);
+
     if (!aInfo->IsPrivate() &&
         GetOriginAttrsHash(*aInfo->OriginAttributesPtr()) == aRec->mOriginAttrsHash &&
         aInfo->IsAnonymous() == !!(aRec->mFlags & kAnonymousMask)) {
       return true;
     }
 
     return false;
   }
@@ -661,17 +663,18 @@ public:
 
   // Returns cache size in kB.
   static nsresult GetCacheSize(uint32_t *_retval);
 
   // Returns number of entry files in the cache
   static nsresult GetEntryFileCount(uint32_t *_retval);
 
   // Synchronously returns the disk occupation and number of entries per-context.
-  // Callable on any thread.
+  // Callable on any thread. It will ignore loadContextInfo and get stats for
+  // all entries if the aInfo is a nullptr.
   static nsresult GetCacheStats(nsILoadContextInfo *aInfo, uint32_t *aSize, uint32_t *aCount);
 
   // Asynchronously gets the disk cache size, used for display in the UI.
   static nsresult AsyncGetDiskConsumption(nsICacheStorageConsumptionObserver* aObserver);
 
   // Returns an iterator that returns entries matching a given context that were
   // present in the index at the time this method was called. If aAddNew is true
   // then the iterator will also return entries created after this call.
--- a/netwerk/cache2/CacheStorageService.cpp
+++ b/netwerk/cache2/CacheStorageService.cpp
@@ -926,16 +926,31 @@ NS_IMETHODIMP CacheStorageService::GetIo
 
     rv = serv->GetCacheIOTarget(aEventTarget);
     NS_ENSURE_SUCCESS(rv, rv);
   }
 
   return NS_OK;
 }
 
+NS_IMETHODIMP CacheStorageService::AsyncVisitAllStorages(
+  nsICacheStorageVisitor* aVisitor,
+  bool aVisitEntries)
+{
+  LOG(("CacheStorageService::AsyncVisitAllStorages [cb=%p]", aVisitor));
+  NS_ENSURE_FALSE(mShutdown, NS_ERROR_NOT_INITIALIZED);
+
+  // Walking the disk cache also walks the memory cache.
+  RefPtr<WalkDiskCacheRunnable> event =
+    new WalkDiskCacheRunnable(nullptr, aVisitEntries, aVisitor);
+  return event->Walk();
+
+  return NS_OK;
+}
+
 // Methods used by CacheEntry for management of in-memory structures.
 
 namespace {
 
 class FrecencyComparator
 {
 public:
   bool Equals(CacheEntry* a, CacheEntry* b) const {
--- a/netwerk/cache2/nsICacheStorageService.idl
+++ b/netwerk/cache2/nsICacheStorageService.idl
@@ -4,16 +4,17 @@
 
 #include "nsISupports.idl"
 
 interface nsICacheStorage;
 interface nsILoadContextInfo;
 interface nsIApplicationCache;
 interface nsIEventTarget;
 interface nsICacheStorageConsumptionObserver;
+interface nsICacheStorageVisitor;
 
 /**
  * Provides access to particual cache storages of the network URI cache.
  */
 [scriptable, uuid(ae29c44b-fbc3-4552-afaf-0a157ce771e7)]
 interface nsICacheStorageService : nsISupports
 {
   /**
@@ -104,16 +105,27 @@ interface nsICacheStorageService : nsISu
    * Asynchronously determine how many bytes of the disk space the cache takes.
    * @see nsICacheStorageConsumptionObserver
    * @param aObserver
    *    A mandatory (weak referred) observer.  Documented at
    *    nsICacheStorageConsumptionObserver.
    *    NOTE: the observer MUST implement nsISupportsWeakReference.
    */
   void asyncGetDiskConsumption(in nsICacheStorageConsumptionObserver aObserver);
+
+  /**
+   * Asynchronously visits all storages of the disk cache and memory cache.
+   * @see nsICacheStorageVisitor
+   * @param aVisitor
+   *   A visitor callback.
+   * @param aVisitEntries
+   *   A boolean indicates whether visits entries.
+   */
+  void asyncVisitAllStorages(in nsICacheStorageVisitor aVisitor,
+                             in boolean aVisitEntries);
 };
 
 [scriptable, uuid(7728ab5b-4c01-4483-a606-32bf5b8136cb)]
 interface nsICacheStorageConsumptionObserver : nsISupports
 {
   /**
    * Callback invoked to answer asyncGetDiskConsumption call. Always triggered
    * on the main thread.