Bug 746697 - Create nsApplicationCacheService to wrap nsOfflineCacheDevice. r=honzab
authorBrian Nicholson <bnicholson@mozilla.com>
Mon, 30 Apr 2012 10:57:09 -0700
changeset 92808 ecfa1fd40096332ef103a4d57eb7265fd5c2aaee
parent 92807 06091f9ed1ddd0a325dc00b06b1d3a13ec60f15f
child 92809 f5a301fe9ba5548c629389f45639f1d741355666
push id22595
push usereakhgari@mozilla.com
push dateThu, 03 May 2012 04:00:15 +0000
treeherdermozilla-central@807403a04a6a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewershonzab
bugs746697
milestone15.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 746697 - Create nsApplicationCacheService to wrap nsOfflineCacheDevice. r=honzab
netwerk/build/nsNetModule.cpp
netwerk/cache/Makefile.in
netwerk/cache/nsApplicationCacheService.cpp
netwerk/cache/nsApplicationCacheService.h
netwerk/cache/nsCacheService.cpp
netwerk/cache/nsCacheService.h
netwerk/cache/nsDiskCacheDeviceSQL.cpp
netwerk/cache/nsDiskCacheDeviceSQL.h
--- a/netwerk/build/nsNetModule.cpp
+++ b/netwerk/build/nsNetModule.cpp
@@ -55,16 +55,17 @@
 #include "nsUnicharStreamLoader.h"
 #include "nsFileStreams.h"
 #include "nsBufferedStreams.h"
 #include "nsMIMEInputStream.h"
 #include "nsSOCKSSocketProvider.h"
 #include "nsCacheService.h"
 #include "nsDiskCacheDeviceSQL.h"
 #include "nsApplicationCache.h"
+#include "nsApplicationCacheService.h"
 #include "nsMimeTypes.h"
 #include "nsNetStrings.h"
 #include "nsDNSPrefetch.h"
 #include "nsAboutProtocolHandler.h"
 #include "nsXULAppAPI.h"
 
 #include "nsNetCID.h"
 
@@ -210,17 +211,17 @@ NS_GENERIC_FACTORY_CONSTRUCTOR(nsNestedA
 #ifdef NS_BUILD_REFCNT_LOGGING
 #include "nsAboutBloat.h"
 #endif
 #include "nsAboutCache.h"
 #include "nsAboutCacheEntry.h"
 NS_GENERIC_FACTORY_CONSTRUCTOR(nsAboutCacheEntry)
 #endif
 
-NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(nsOfflineCacheDevice, nsOfflineCacheDevice::GetInstance)
+NS_GENERIC_FACTORY_CONSTRUCTOR(nsApplicationCacheService)
 NS_GENERIC_FACTORY_CONSTRUCTOR(nsApplicationCacheNamespace)
 NS_GENERIC_FACTORY_CONSTRUCTOR(nsApplicationCache)
 
 #ifdef NECKO_PROTOCOL_file
 // file
 #include "nsFileProtocolHandler.h"
 NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsFileProtocolHandler, Init)
 #endif
@@ -897,17 +898,17 @@ static const mozilla::Module::CIDEntry k
 #endif
     { &kNS_ABOUT_CACHE_MODULE_CID, false, NULL, nsAboutCache::Create },
     { &kNS_ABOUT_CACHE_ENTRY_MODULE_CID, false, NULL, nsAboutCacheEntryConstructor },
 #endif
     { &kNS_SOCKSSOCKETPROVIDER_CID, false, NULL, nsSOCKSSocketProvider::CreateV5 },
     { &kNS_SOCKS4SOCKETPROVIDER_CID, false, NULL, nsSOCKSSocketProvider::CreateV4 },
     { &kNS_UDPSOCKETPROVIDER_CID, false, NULL, nsUDPSocketProviderConstructor },
     { &kNS_CACHESERVICE_CID, false, NULL, nsCacheService::Create },
-    { &kNS_APPLICATIONCACHESERVICE_CID, false, NULL, nsOfflineCacheDeviceConstructor },
+    { &kNS_APPLICATIONCACHESERVICE_CID, false, NULL, nsApplicationCacheServiceConstructor },
     { &kNS_APPLICATIONCACHENAMESPACE_CID, false, NULL, nsApplicationCacheNamespaceConstructor },
     { &kNS_APPLICATIONCACHE_CID, false, NULL, nsApplicationCacheConstructor },
 #ifdef NECKO_COOKIES
     { &kNS_COOKIEMANAGER_CID, false, NULL, nsICookieServiceConstructor },
     { &kNS_COOKIESERVICE_CID, false, NULL, nsICookieServiceConstructor },
 #endif
 #ifdef NECKO_WIFI
     { &kNS_WIFI_MONITOR_COMPONENT_CID, false, NULL, nsWifiMonitorConstructor },
--- a/netwerk/cache/Makefile.in
+++ b/netwerk/cache/Makefile.in
@@ -75,16 +75,17 @@ CPPSRCS = \
   nsDiskCacheBinding.cpp \
   nsDiskCacheBlockFile.cpp \
   nsDiskCacheDevice.cpp \
   nsDiskCacheDeviceSQL.cpp \
   nsDiskCacheEntry.cpp \
   nsDiskCacheMap.cpp \
   nsDiskCacheStreams.cpp \
   nsDeleteDir.cpp \
+  nsApplicationCacheService.cpp \
   $(NULL)
 
 LOCAL_INCLUDES = \
   -I$(srcdir)/../base/src \
   $(NULL)
 
 include $(topsrcdir)/config/rules.mk
 
new file mode 100644
--- /dev/null
+++ b/netwerk/cache/nsApplicationCacheService.cpp
@@ -0,0 +1,125 @@
+/* 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/. */
+
+#include "nsDiskCache.h"
+#include "nsDiskCacheDeviceSQL.h"
+#include "nsCacheService.h"
+#include "nsApplicationCacheService.h"
+
+#include "nsNetUtil.h"
+
+using namespace mozilla;
+
+static NS_DEFINE_CID(kCacheServiceCID, NS_CACHESERVICE_CID);
+
+NS_IMPL_ISUPPORTS1(nsApplicationCacheService, nsIApplicationCacheService)
+
+nsApplicationCacheService::nsApplicationCacheService()
+{
+    nsCOMPtr<nsICacheService> serv = do_GetService(kCacheServiceCID);
+    mCacheService = nsCacheService::GlobalInstance();
+}
+
+NS_IMETHODIMP
+nsApplicationCacheService::CreateApplicationCache(const nsACString &group,
+                                                  nsIApplicationCache **out)
+{
+    if (!mCacheService)
+        return NS_ERROR_UNEXPECTED;
+
+    nsRefPtr<nsOfflineCacheDevice> device;
+    nsresult rv = mCacheService->GetOfflineDevice(getter_AddRefs(device));
+    NS_ENSURE_SUCCESS(rv, rv);
+    return device->CreateApplicationCache(group, out);
+}
+
+NS_IMETHODIMP
+nsApplicationCacheService::GetApplicationCache(const nsACString &clientID,
+                                               nsIApplicationCache **out)
+{
+    if (!mCacheService)
+        return NS_ERROR_UNEXPECTED;
+
+    nsRefPtr<nsOfflineCacheDevice> device;
+    nsresult rv = mCacheService->GetOfflineDevice(getter_AddRefs(device));
+    NS_ENSURE_SUCCESS(rv, rv);
+    return device->GetApplicationCache(clientID, out);
+}
+
+NS_IMETHODIMP
+nsApplicationCacheService::GetActiveCache(const nsACString &group,
+                                          nsIApplicationCache **out)
+{
+    if (!mCacheService)
+        return NS_ERROR_UNEXPECTED;
+
+    nsRefPtr<nsOfflineCacheDevice> device;
+    nsresult rv = mCacheService->GetOfflineDevice(getter_AddRefs(device));
+    NS_ENSURE_SUCCESS(rv, rv);
+    return device->GetActiveCache(group, out);
+}
+
+NS_IMETHODIMP
+nsApplicationCacheService::DeactivateGroup(const nsACString &group)
+{
+    if (!mCacheService)
+        return NS_ERROR_UNEXPECTED;
+
+    nsRefPtr<nsOfflineCacheDevice> device;
+    nsresult rv = mCacheService->GetOfflineDevice(getter_AddRefs(device));
+    NS_ENSURE_SUCCESS(rv, rv);
+    return device->DeactivateGroup(group);
+}
+
+NS_IMETHODIMP
+nsApplicationCacheService::ChooseApplicationCache(const nsACString &key,
+                                                  nsIApplicationCache **out)
+{
+    if (!mCacheService)
+        return NS_ERROR_UNEXPECTED;
+
+    nsRefPtr<nsOfflineCacheDevice> device;
+    nsresult rv = mCacheService->GetOfflineDevice(getter_AddRefs(device));
+    NS_ENSURE_SUCCESS(rv, rv);
+    return device->ChooseApplicationCache(key, out);
+}
+
+NS_IMETHODIMP
+nsApplicationCacheService::CacheOpportunistically(nsIApplicationCache* cache,
+                                                  const nsACString &key)
+{
+    if (!mCacheService)
+        return NS_ERROR_UNEXPECTED;
+
+    nsRefPtr<nsOfflineCacheDevice> device;
+    nsresult rv = mCacheService->GetOfflineDevice(getter_AddRefs(device));
+    NS_ENSURE_SUCCESS(rv, rv);
+    return device->CacheOpportunistically(cache, key);
+}
+
+NS_IMETHODIMP
+nsApplicationCacheService::GetGroups(PRUint32 *count,
+                                     char ***keys)
+{
+    if (!mCacheService)
+        return NS_ERROR_UNEXPECTED;
+
+    nsRefPtr<nsOfflineCacheDevice> device;
+    nsresult rv = mCacheService->GetOfflineDevice(getter_AddRefs(device));
+    NS_ENSURE_SUCCESS(rv, rv);
+    return device->GetGroups(count, keys);
+}
+
+NS_IMETHODIMP
+nsApplicationCacheService::GetGroupsTimeOrdered(PRUint32 *count,
+                                                char ***keys)
+{
+    if (!mCacheService)
+        return NS_ERROR_UNEXPECTED;
+
+    nsRefPtr<nsOfflineCacheDevice> device;
+    nsresult rv = mCacheService->GetOfflineDevice(getter_AddRefs(device));
+    NS_ENSURE_SUCCESS(rv, rv);
+    return device->GetGroupsTimeOrdered(count, keys);
+}
new file mode 100644
--- /dev/null
+++ b/netwerk/cache/nsApplicationCacheService.h
@@ -0,0 +1,19 @@
+/* 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 _nsApplicationCacheService_h_
+#define _nsApplicationCacheService_h_
+
+class nsApplicationCacheService : public nsIApplicationCacheService
+{
+public:
+    nsApplicationCacheService();
+
+    NS_DECL_ISUPPORTS
+    NS_DECL_NSIAPPLICATIONCACHESERVICE
+private:
+    nsRefPtr<nsCacheService> mCacheService;
+};
+
+#endif // _nsApplicationCacheService_h_
--- a/netwerk/cache/nsCacheService.cpp
+++ b/netwerk/cache/nsCacheService.cpp
@@ -1585,16 +1585,28 @@ nsCacheService::CreateDiskDevice()
     }
     // Ignore state of the timer and return success since the purpose of the
     // method (create the disk-device) has been fulfilled
 
     return NS_OK;
 }
 
 nsresult
+nsCacheService::GetOfflineDevice(nsOfflineCacheDevice **aDevice)
+{
+    if (!mOfflineDevice) {
+        nsresult rv = CreateOfflineDevice();
+        NS_ENSURE_SUCCESS(rv, rv);
+    }
+
+    NS_ADDREF(*aDevice = mOfflineDevice);
+    return NS_OK;
+}
+
+nsresult
 nsCacheService::CreateOfflineDevice()
 {
     CACHE_LOG_ALWAYS(("Creating offline device"));
 
     if (!mInitialized)         return NS_ERROR_NOT_AVAILABLE;
     if (!mEnableOfflineDevice) return NS_ERROR_NOT_AVAILABLE;
     if (mOfflineDevice)        return NS_OK;
 
--- a/netwerk/cache/nsCacheService.h
+++ b/netwerk/cache/nsCacheService.h
@@ -138,16 +138,22 @@ public:
     nsCacheService * GlobalInstance()   { return gService; }
 
     static PRInt64   MemoryDeviceSize();
     
     static nsresult  DoomEntry(nsCacheEntry * entry);
 
     static bool      IsStorageEnabledForPolicy_Locked(nsCacheStoragePolicy policy);
 
+    /**
+     * Methods called by nsApplicationCacheService
+     */
+
+    nsresult GetOfflineDevice(nsOfflineCacheDevice ** aDevice);
+
     // This method may be called to release an object while the cache service
     // lock is being held.  If a non-null target is specified and the target
     // does not correspond to the current thread, then the release will be
     // proxied to the specified target.  Otherwise, the object will be added to
     // the list of objects to be released when the cache service is unlocked.
     static void      ReleaseObject_Locked(nsISupports *    object,
                                           nsIEventTarget * target = nsnull);
 
--- a/netwerk/cache/nsDiskCacheDeviceSQL.cpp
+++ b/netwerk/cache/nsDiskCacheDeviceSQL.cpp
@@ -68,17 +68,16 @@
 #include "nsISeekableStream.h"
 
 #include "mozilla/FunctionTimer.h"
 #include "mozilla/Telemetry.h"
 
 using namespace mozilla;
 
 static const char OFFLINE_CACHE_DEVICE_ID[] = { "offline" };
-static NS_DEFINE_CID(kCacheServiceCID, NS_CACHESERVICE_CID);
 
 #define LOG(args) CACHE_LOG_DEBUG(args)
 
 static PRUint32 gNextTemporaryClientID = 0;
 
 /*****************************************************************************
  * helpers
  */
@@ -816,17 +815,17 @@ private:
 };
 
 
 
 /******************************************************************************
  * nsOfflineCacheDevice
  */
 
-NS_IMPL_THREADSAFE_ISUPPORTS1(nsOfflineCacheDevice, nsIApplicationCacheService)
+NS_IMPL_THREADSAFE_ISUPPORTS0(nsOfflineCacheDevice)
 
 nsOfflineCacheDevice::nsOfflineCacheDevice()
   : mDB(nsnull)
   , mCacheCapacity(0)
   , mDeltaCounter(0)
 {
 }
 
@@ -980,33 +979,16 @@ nsOfflineCacheDevice::DeleteData(nsCache
 
   return binding->mDataFile->Remove(false);
 }
 
 /**
  * nsCacheDevice implementation
  */
 
-/* static */
-nsOfflineCacheDevice *
-nsOfflineCacheDevice::GetInstance()
-{
-  nsresult rv;
-  nsCOMPtr<nsICacheService> serv = do_GetService(kCacheServiceCID, &rv);
-  NS_ENSURE_SUCCESS(rv, nsnull);
-
-  nsICacheService *iservice = static_cast<nsICacheService*>(serv.get());
-  nsCacheService *cacheService = static_cast<nsCacheService*>(iservice);
-  rv = cacheService->CreateOfflineDevice();
-  NS_ENSURE_SUCCESS(rv, nsnull);
-
-  NS_IF_ADDREF(cacheService->mOfflineDevice);
-  return cacheService->mOfflineDevice;
-}
-
 // This struct is local to nsOfflineCacheDevice::Init, but ISO C++98 doesn't
 // allow a template (mozilla::ArrayLength) to be instantiated based on a local
 // type.  Boo-urns!
 struct StatementSql {
     nsCOMPtr<mozIStorageStatement> &statement;
     const char *sql;
     StatementSql (nsCOMPtr<mozIStorageStatement> &aStatement, const char *aSql):
       statement (aStatement), sql (aSql) {}
@@ -2050,27 +2032,27 @@ nsOfflineCacheDevice::GetUsage(const nsA
   if (!hasRows)
     return NS_OK;
 
   *usage = static_cast<PRUint32>(statement->AsInt32(0));
 
   return NS_OK;
 }
 
-NS_IMETHODIMP
+nsresult
 nsOfflineCacheDevice::GetGroups(PRUint32 *count,
                                  char ***keys)
 {
 
   LOG(("nsOfflineCacheDevice::GetGroups"));
 
   return RunSimpleQuery(mStatement_EnumerateGroups, 0, count, keys);
 }
 
-NS_IMETHODIMP
+nsresult
 nsOfflineCacheDevice::GetGroupsTimeOrdered(PRUint32 *count,
 					   char ***keys)
 {
 
   LOG(("nsOfflineCacheDevice::GetGroupsTimeOrder"));
 
   return RunSimpleQuery(mStatement_EnumerateGroupsTimeOrder, 0, count, keys);
 }
@@ -2108,17 +2090,17 @@ nsOfflineCacheDevice::RunSimpleQuery(moz
     }
   }
 
   *values = ret;
 
   return NS_OK;
 }
 
-NS_IMETHODIMP
+nsresult
 nsOfflineCacheDevice::CreateApplicationCache(const nsACString &group,
                                              nsIApplicationCache **out)
 {
   *out = nsnull;
 
   nsCString clientID;
   // Some characters are special in the clientID.  Escape the groupID
   // before putting it in to the client key.
@@ -2146,17 +2128,17 @@ nsOfflineCacheDevice::CreateApplicationC
 
   mCaches.Put(clientID, weak);
 
   cache.swap(*out);
 
   return NS_OK;
 }
 
-NS_IMETHODIMP
+nsresult
 nsOfflineCacheDevice::GetApplicationCache(const nsACString &clientID,
                                           nsIApplicationCache **out)
 {
   *out = nsnull;
 
   nsCOMPtr<nsIApplicationCache> cache;
 
   nsWeakPtr weak;
@@ -2181,30 +2163,30 @@ nsOfflineCacheDevice::GetApplicationCach
     mCaches.Put(clientID, weak);
   }
 
   cache.swap(*out);
 
   return NS_OK;
 }
 
-NS_IMETHODIMP
+nsresult
 nsOfflineCacheDevice::GetActiveCache(const nsACString &group,
                                      nsIApplicationCache **out)
 {
   *out = nsnull;
 
   nsCString *clientID;
   if (mActiveCachesByGroup.Get(group, &clientID))
     return GetApplicationCache(*clientID, out);
 
   return NS_OK;
 }
 
-NS_IMETHODIMP
+nsresult
 nsOfflineCacheDevice::DeactivateGroup(const nsACString &group)
 {
   nsCString *active = nsnull;
 
   AutoResetStatement statement(mStatement_DeactivateGroup);
   nsresult rv = statement->BindUTF8StringByIndex(0, group);
   NS_ENSURE_SUCCESS(rv, rv);
 
@@ -2243,17 +2225,17 @@ nsOfflineCacheDevice::CanUseCache(nsIURI
         return true;
     }
   }
 
   return false;
 }
 
 
-NS_IMETHODIMP
+nsresult
 nsOfflineCacheDevice::ChooseApplicationCache(const nsACString &key,
                                              nsIApplicationCache **out)
 {
   *out = nsnull;
 
   nsCOMPtr<nsIURI> keyURI;
   nsresult rv = NS_NewURI(getter_AddRefs(keyURI), key);
   NS_ENSURE_SUCCESS(rv, rv);
@@ -2316,17 +2298,17 @@ nsOfflineCacheDevice::ChooseApplicationC
 
     rv = nsstatement->ExecuteStep(&hasRows);
     NS_ENSURE_SUCCESS(rv, rv);
   }
 
   return NS_OK;
 }
 
-NS_IMETHODIMP
+nsresult
 nsOfflineCacheDevice::CacheOpportunistically(nsIApplicationCache* cache,
                                              const nsACString &key)
 {
   NS_ENSURE_ARG_POINTER(cache);
 
   nsresult rv;
 
   nsCAutoString clientID;
--- a/netwerk/cache/nsDiskCacheDeviceSQL.h
+++ b/netwerk/cache/nsDiskCacheDeviceSQL.h
@@ -84,30 +84,27 @@ public:
 
 private:
   nsOfflineCacheDevice *mDevice;
   nsCOMArray<nsIFile> mItems;
 
 };
 
 class nsOfflineCacheDevice : public nsCacheDevice
-                           , public nsIApplicationCacheService
+                           , public nsISupports
 {
 public:
   nsOfflineCacheDevice();
 
   NS_DECL_ISUPPORTS
-  NS_DECL_NSIAPPLICATIONCACHESERVICE
 
   /**
    * nsCacheDevice methods
    */
 
-  static nsOfflineCacheDevice *GetInstance();
-
   virtual nsresult        Init();
   virtual nsresult        Shutdown();
 
   virtual const char *    GetDeviceID(void);
   virtual nsCacheEntry *  FindEntry(nsCString * key, bool *collision);
   virtual nsresult        DeactivateEntry(nsCacheEntry * entry);
   virtual nsresult        BindEntry(nsCacheEntry * entry);
   virtual void            DoomEntry( nsCacheEntry * entry );
@@ -169,16 +166,38 @@ public:
 
   nsresult                ActivateCache(const nsCSubstring &group,
                                         const nsCSubstring &clientID);
   bool                    IsActiveCache(const nsCSubstring &group,
                                         const nsCSubstring &clientID);
   nsresult                GetGroupForCache(const nsCSubstring &clientID,
                                            nsCString &out);
 
+  nsresult                CreateApplicationCache(const nsACString &group,
+                                                 nsIApplicationCache **out);
+
+  nsresult                GetApplicationCache(const nsACString &clientID,
+                                              nsIApplicationCache **out);
+
+  nsresult                GetActiveCache(const nsACString &group,
+                                         nsIApplicationCache **out);
+
+  nsresult                DeactivateGroup(const nsACString &group);
+
+  nsresult                ChooseApplicationCache(const nsACString &key,
+                                                 nsIApplicationCache **out);
+
+  nsresult                CacheOpportunistically(nsIApplicationCache* cache,
+                                                 const nsACString &key);
+
+  nsresult                GetGroups(PRUint32 *count,char ***keys);
+
+  nsresult                GetGroupsTimeOrdered(PRUint32 *count,
+                                               char ***keys);
+
   /**
    * Preference accessors
    */
 
   void                    SetCacheParentDirectory(nsILocalFile * parentDir);
   void                    SetCapacity(PRUint32  capacity);
 
   nsILocalFile *          CacheDirectory() { return mCacheDirectory; }