Bug 786299 - Part 2: Delete app-cache related to an app when uninstalled. r=honza
☠☠ backed out by 551152b8c271 ☠ ☠
authorJason Duell <jduell.mcbugs@gmail.com>
Fri, 28 Sep 2012 16:13:17 -0700
changeset 108687 bec7e68cad9af2ccf552f024d61c37352097890c
parent 108686 a6228bc289587667d4dc19792731c4fd311fa4a5
child 108688 189a1aaac3451b00d45445accd808b33add4d22f
push id82
push usershu@rfrn.org
push dateFri, 05 Oct 2012 13:20:22 +0000
reviewershonza
bugs786299
milestone18.0a1
Bug 786299 - Part 2: Delete app-cache related to an app when uninstalled. r=honza
layout/build/nsLayoutStatics.cpp
netwerk/base/public/nsNetUtil.h
netwerk/cache/Makefile.in
netwerk/cache/nsApplicationCacheService.cpp
netwerk/cache/nsApplicationCacheService.h
--- a/layout/build/nsLayoutStatics.cpp
+++ b/layout/build/nsLayoutStatics.cpp
@@ -96,16 +96,17 @@
 #include "nsDOMMutationObserver.h"
 #include "nsHyphenationManager.h"
 #include "nsEditorSpellCheck.h"
 #include "nsWindowMemoryReporter.h"
 #include "mozilla/dom/ContentParent.h"
 #include "mozilla/dom/ipc/ProcessPriorityManager.h"
 #include "nsPermissionManager.h"
 #include "nsCookieService.h"
+#include "nsApplicationCacheService.h"
 
 extern void NS_ShutdownChainItemPool();
 
 using namespace mozilla;
 using namespace mozilla::dom;
 using namespace mozilla::dom::ipc;
 
 nsrefcnt nsLayoutStatics::sLayoutStaticRefcnt = 0;
@@ -252,16 +253,17 @@ nsLayoutStatics::Initialize()
   nsWindowMemoryReporter::Init();
 
   nsSVGUtils::Init();
 
   InitProcessPriorityManager();
 
   nsPermissionManager::AppUninstallObserverInit();
   nsCookieService::AppUninstallObserverInit();
+  nsApplicationCacheService::AppClearDataObserverInit();
 
   nsDOMStorageBaseDB::Init();
 
   return NS_OK;
 }
 
 void
 nsLayoutStatics::Shutdown()
--- a/netwerk/base/public/nsNetUtil.h
+++ b/netwerk/base/public/nsNetUtil.h
@@ -72,16 +72,17 @@
 #include "nsIChannelPolicy.h"
 #include "nsISocketProviderService.h"
 #include "nsISocketProvider.h"
 #include "nsIRedirectChannelRegistrar.h"
 #include "nsIMIMEHeaderParam.h"
 #include "nsILoadContext.h"
 #include "mozilla/Services.h"
 #include "nsIPrivateBrowsingChannel.h"
+#include "mozIApplicationClearPrivateDataParams.h"
 
 #include <limits>
 
 #ifdef MOZILLA_INTERNAL_API
 
 inline already_AddRefed<nsIIOService>
 do_GetIOService(nsresult* error = 0)
 {
@@ -1329,16 +1330,56 @@ NS_GetAppInfo(nsIChannel *aChannel, uint
     NS_ENSURE_SUCCESS(rv, false);
 
     rv = loadContext->GetIsInBrowserElement(aIsInBrowserElement);
     NS_ENSURE_SUCCESS(rv, false);
 
     return true;
 }
 
+#define TOPIC_WEB_APP_CLEAR_DATA "webapps-clear-data"
+
+/**
+ *  Gets appId and browserOnly parameters from the TOPIC_WEB_APP_CLEAR_DATA
+ *  nsIObserverService notification.  Used when clearing user data or
+ *  uninstalling web apps.
+ */
+inline nsresult
+NS_GetAppInfoFromClearDataNotification(nsISupports *aSubject,
+                                       uint32_t *aAppID, bool* aBrowserOnly)
+{
+    nsresult rv;
+
+    nsCOMPtr<mozIApplicationClearPrivateDataParams>
+        clearParams(do_QueryInterface(aSubject));
+    MOZ_ASSERT(clearParams);
+    if (!clearParams) {
+        return NS_ERROR_UNEXPECTED;
+    }
+
+    uint32_t appId;
+    rv = clearParams->GetAppId(&appId);
+    MOZ_ASSERT(NS_SUCCEEDED(rv));
+    MOZ_ASSERT(appId != NECKO_NO_APP_ID);
+    MOZ_ASSERT(appId != NECKO_UNKNOWN_APP_ID);
+    NS_ENSURE_SUCCESS(rv, rv);
+    if (appId == NECKO_NO_APP_ID || appId == NECKO_UNKNOWN_APP_ID) {
+        return NS_ERROR_UNEXPECTED;
+    }
+
+    bool browserOnly = false;
+    rv = clearParams->GetBrowserOnly(&browserOnly);
+    MOZ_ASSERT(NS_SUCCEEDED(rv));
+    NS_ENSURE_SUCCESS(rv, rv);
+
+    *aAppID = appId;
+    *aBrowserOnly = browserOnly;
+    return NS_OK;
+}
+
 /**
  * Wraps an nsIAuthPrompt so that it can be used as an nsIAuthPrompt2. This
  * method is provided mainly for use by other methods in this file.
  *
  * *aAuthPrompt2 should be set to null before calling this function.
  */
 inline void
 NS_WrapAuthPrompt(nsIAuthPrompt *aAuthPrompt, nsIAuthPrompt2** aAuthPrompt2)
--- a/netwerk/cache/Makefile.in
+++ b/netwerk/cache/Makefile.in
@@ -25,16 +25,17 @@ XPIDLSRCS = \
   nsICacheService.idl \
   nsICacheSession.idl \
   nsICacheVisitor.idl \
   nsIDiskCacheStreamInternal.idl \
   $(NULL)
 
 EXPORTS = \
   nsCacheService.h \
+  nsApplicationCacheService.h \
   $(NULL)
 
 CPPSRCS = \
   nsCache.cpp \
   nsCacheEntry.cpp \
   nsCacheEntryDescriptor.cpp \
   nsCacheMetaData.cpp \
   nsCacheService.cpp \
--- a/netwerk/cache/nsApplicationCacheService.cpp
+++ b/netwerk/cache/nsApplicationCacheService.cpp
@@ -1,23 +1,28 @@
 /* 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 "nsCRT.h"
 #include "nsNetUtil.h"
+#include "nsIObserverService.h"
 
 using namespace mozilla;
 
 static NS_DEFINE_CID(kCacheServiceCID, NS_CACHESERVICE_CID);
 
+//-----------------------------------------------------------------------------
+// nsApplicationCacheService
+//-----------------------------------------------------------------------------
+
 NS_IMPL_ISUPPORTS1(nsApplicationCacheService, nsIApplicationCacheService)
 
 nsApplicationCacheService::nsApplicationCacheService()
 {
     nsCOMPtr<nsICacheService> serv = do_GetService(kCacheServiceCID);
     mCacheService = nsCacheService::GlobalInstance();
 }
 
@@ -161,8 +166,58 @@ nsApplicationCacheService::GetGroupsTime
     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);
 }
+
+//-----------------------------------------------------------------------------
+// AppCacheClearDataObserver: handles clearing appcache data for app uninstall
+// and clearing user data events.
+//-----------------------------------------------------------------------------
+
+namespace {
+
+class AppCacheClearDataObserver MOZ_FINAL : public nsIObserver {
+public:
+    NS_DECL_ISUPPORTS
+
+    // nsIObserver implementation.
+    NS_IMETHODIMP
+    Observe(nsISupports *aSubject, const char *aTopic, const PRUnichar *aData)
+    {
+        MOZ_ASSERT(!nsCRT::strcmp(aTopic, TOPIC_WEB_APP_CLEAR_DATA));
+
+        uint32_t appId = NECKO_UNKNOWN_APP_ID;
+        bool browserOnly = false;
+        nsresult rv = NS_GetAppInfoFromClearDataNotification(aSubject, &appId,
+                                                             &browserOnly);
+        NS_ENSURE_SUCCESS(rv, rv);
+
+        nsCOMPtr<nsIApplicationCacheService> cacheService =
+            do_GetService(NS_APPLICATIONCACHESERVICE_CONTRACTID, &rv);
+        NS_ENSURE_SUCCESS(rv, rv);
+
+        return cacheService->DiscardByAppId(appId, browserOnly);
+    }
+};
+
+NS_IMPL_ISUPPORTS1(AppCacheClearDataObserver, nsIObserver)
+
+} // anonymous namespace
+
+// Instantiates and registers AppCacheClearDataObserver for notifications
+void
+nsApplicationCacheService::AppClearDataObserverInit()
+{
+    nsCOMPtr<nsIObserverService> observerService =
+        do_GetService("@mozilla.org/observer-service;1");
+    if (observerService) {
+        nsRefPtr<AppCacheClearDataObserver> obs
+            = new AppCacheClearDataObserver();
+        observerService->AddObserver(obs, TOPIC_WEB_APP_CLEAR_DATA,
+                                     /*holdsWeak=*/ false);
+    }
+}
+
--- a/netwerk/cache/nsApplicationCacheService.h
+++ b/netwerk/cache/nsApplicationCacheService.h
@@ -1,24 +1,30 @@
 /* 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_
 
+#include "nsIApplicationCacheService.h"
 #include "mozilla/Attributes.h"
 
+class nsCacheService;
+
 class nsApplicationCacheService MOZ_FINAL : public nsIApplicationCacheService
 {
 public:
     nsApplicationCacheService();
 
     NS_DECL_ISUPPORTS
     NS_DECL_NSIAPPLICATIONCACHESERVICE
+
+    static void AppClearDataObserverInit();
+
 private:
     nsresult GetJARIdentifier(nsIURI *aURI,
                               nsILoadContext *aLoadContext,
                               nsACString &_result);
 
     nsRefPtr<nsCacheService> mCacheService;
 };