Bug 786299 - Part 2: Delete app-cache related to an app when uninstalled. r=honza

This commit is contained in:
Jason Duell 2012-09-28 16:13:17 -07:00
parent 7babbef463
commit f2b6cb61ab
5 changed files with 106 additions and 1 deletions

View File

@ -101,6 +101,7 @@
#include "mozilla/dom/ipc/ProcessPriorityManager.h" #include "mozilla/dom/ipc/ProcessPriorityManager.h"
#include "nsPermissionManager.h" #include "nsPermissionManager.h"
#include "nsCookieService.h" #include "nsCookieService.h"
#include "nsApplicationCacheService.h"
extern void NS_ShutdownChainItemPool(); extern void NS_ShutdownChainItemPool();
@ -257,6 +258,7 @@ nsLayoutStatics::Initialize()
nsPermissionManager::AppUninstallObserverInit(); nsPermissionManager::AppUninstallObserverInit();
nsCookieService::AppUninstallObserverInit(); nsCookieService::AppUninstallObserverInit();
nsApplicationCacheService::AppClearDataObserverInit();
nsDOMStorageBaseDB::Init(); nsDOMStorageBaseDB::Init();

View File

@ -77,6 +77,7 @@
#include "nsILoadContext.h" #include "nsILoadContext.h"
#include "mozilla/Services.h" #include "mozilla/Services.h"
#include "nsIPrivateBrowsingChannel.h" #include "nsIPrivateBrowsingChannel.h"
#include "mozIApplicationClearPrivateDataParams.h"
#include <limits> #include <limits>
@ -1334,6 +1335,46 @@ NS_GetAppInfo(nsIChannel *aChannel, uint32_t *aAppID, bool *aIsInBrowserElement)
return true; 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 * 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. * method is provided mainly for use by other methods in this file.

View File

@ -30,6 +30,7 @@ XPIDLSRCS = \
EXPORTS = \ EXPORTS = \
nsCacheService.h \ nsCacheService.h \
nsApplicationCacheService.h \
$(NULL) $(NULL)
CPPSRCS = \ CPPSRCS = \

View File

@ -6,13 +6,18 @@
#include "nsDiskCacheDeviceSQL.h" #include "nsDiskCacheDeviceSQL.h"
#include "nsCacheService.h" #include "nsCacheService.h"
#include "nsApplicationCacheService.h" #include "nsApplicationCacheService.h"
#include "nsCRT.h"
#include "nsNetUtil.h" #include "nsNetUtil.h"
#include "nsIObserverService.h"
using namespace mozilla; using namespace mozilla;
static NS_DEFINE_CID(kCacheServiceCID, NS_CACHESERVICE_CID); static NS_DEFINE_CID(kCacheServiceCID, NS_CACHESERVICE_CID);
//-----------------------------------------------------------------------------
// nsApplicationCacheService
//-----------------------------------------------------------------------------
NS_IMPL_ISUPPORTS1(nsApplicationCacheService, nsIApplicationCacheService) NS_IMPL_ISUPPORTS1(nsApplicationCacheService, nsIApplicationCacheService)
nsApplicationCacheService::nsApplicationCacheService() nsApplicationCacheService::nsApplicationCacheService()
@ -166,3 +171,53 @@ nsApplicationCacheService::GetGroupsTimeOrdered(uint32_t *count,
NS_ENSURE_SUCCESS(rv, rv); NS_ENSURE_SUCCESS(rv, rv);
return device->GetGroupsTimeOrdered(count, keys); 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);
}
}

View File

@ -5,8 +5,11 @@
#ifndef _nsApplicationCacheService_h_ #ifndef _nsApplicationCacheService_h_
#define _nsApplicationCacheService_h_ #define _nsApplicationCacheService_h_
#include "nsIApplicationCacheService.h"
#include "mozilla/Attributes.h" #include "mozilla/Attributes.h"
class nsCacheService;
class nsApplicationCacheService MOZ_FINAL : public nsIApplicationCacheService class nsApplicationCacheService MOZ_FINAL : public nsIApplicationCacheService
{ {
public: public:
@ -14,6 +17,9 @@ public:
NS_DECL_ISUPPORTS NS_DECL_ISUPPORTS
NS_DECL_NSIAPPLICATIONCACHESERVICE NS_DECL_NSIAPPLICATIONCACHESERVICE
static void AppClearDataObserverInit();
private: private:
nsresult GetJARIdentifier(nsIURI *aURI, nsresult GetJARIdentifier(nsIURI *aURI,
nsILoadContext *aLoadContext, nsILoadContext *aLoadContext,