Bug 783408 - 2/4 - Add a function to remove app's cookies in nsICookieManager2. r=jduell sr=mconnor

This commit is contained in:
Mounir Lamouri 2012-09-27 22:37:02 +01:00
Родитель 1034c194b7
Коммит b8dffb5266
3 изменённых файлов: 79 добавлений и 7 удалений

Просмотреть файл

@ -1800,11 +1800,11 @@ nsCookieService::Add(const nsACString &aHost,
return NS_OK; return NS_OK;
} }
NS_IMETHODIMP
nsCookieService::Remove(const nsACString &aHost, nsresult
const nsACString &aName, nsCookieService::Remove(const nsACString& aHost, uint32_t aAppId,
const nsACString &aPath, bool aInBrowserElement, const nsACString& aName,
bool aBlocked) const nsACString& aPath, bool aBlocked)
{ {
if (!mDBState) { if (!mDBState) {
NS_WARNING("No DBState! Profile already closed?"); NS_WARNING("No DBState! Profile already closed?");
@ -1822,7 +1822,7 @@ nsCookieService::Remove(const nsACString &aHost,
nsListIter matchIter; nsListIter matchIter;
nsRefPtr<nsCookie> cookie; nsRefPtr<nsCookie> cookie;
if (FindCookie(DEFAULT_APP_KEY(baseDomain), if (FindCookie(nsCookieKey(baseDomain, aAppId, aInBrowserElement),
host, host,
PromiseFlatCString(aName), PromiseFlatCString(aName),
PromiseFlatCString(aPath), PromiseFlatCString(aPath),
@ -1854,6 +1854,15 @@ nsCookieService::Remove(const nsACString &aHost,
return NS_OK; return NS_OK;
} }
NS_IMETHODIMP
nsCookieService::Remove(const nsACString &aHost,
const nsACString &aName,
const nsACString &aPath,
bool aBlocked)
{
return Remove(aHost, NECKO_NO_APP_ID, false, aName, aPath, aBlocked);
}
/****************************************************************************** /******************************************************************************
* nsCookieService impl: * nsCookieService impl:
* private file I/O functions * private file I/O functions
@ -3776,6 +3785,50 @@ nsCookieService::GetCookiesForApp(uint32_t aAppId, bool aOnlyBrowserElement,
return NS_NewArrayEnumerator(aEnumerator, data.cookies); return NS_NewArrayEnumerator(aEnumerator, data.cookies);
} }
NS_IMETHODIMP
nsCookieService::RemoveCookiesForApp(uint32_t aAppId, bool aOnlyBrowserElement)
{
nsCOMPtr<nsISimpleEnumerator> enumerator;
nsresult rv = GetCookiesForApp(aAppId, aOnlyBrowserElement,
getter_AddRefs(enumerator));
NS_ENSURE_SUCCESS(rv, rv);
bool hasMore;
while (NS_SUCCEEDED(enumerator->HasMoreElements(&hasMore)) && hasMore) {
nsCOMPtr<nsICookie> cookie;
rv = enumerator->GetNext(getter_AddRefs(cookie));
NS_ENSURE_SUCCESS(rv, rv);
nsAutoCString host;
cookie->GetHost(host);
nsAutoCString name;
cookie->GetName(name);
nsAutoCString path;
cookie->GetPath(path);
// nsICookie do not carry the appId/inBrowserElement information.
// That means we have to guess. This is easy for appId but not for
// inBrowserElement flag.
// A simple solution is to always ask to remove the cookie with
// inBrowserElement = true and only ask for the other one to be removed if
// we happen to be in the case of !aOnlyBrowserElement.
// Anyway, with this solution, we will likely be looking for unexistant
// cookies.
//
// NOTE: we could make this better by getting nsCookieEntry objects instead
// of plain nsICookie.
Remove(host, aAppId, true, name, path, false);
if (!aOnlyBrowserElement) {
Remove(host, aAppId, false, name, path, false);
}
}
return NS_OK;
}
// find an exact cookie specified by host, name, and path that hasn't expired. // find an exact cookie specified by host, name, and path that hasn't expired.
bool bool
nsCookieService::FindCookie(const nsCookieKey &aKey, nsCookieService::FindCookie(const nsCookieKey &aKey,

Просмотреть файл

@ -289,6 +289,15 @@ class nsCookieService : public nsICookieService
*/ */
static PLDHashOperator GetCookiesForApp(nsCookieEntry* entry, void* arg); static PLDHashOperator GetCookiesForApp(nsCookieEntry* entry, void* arg);
/**
* This method is a helper that allows calling nsICookieManager::Remove()
* with appId/inBrowserElement parameters.
* NOTE: this could be added to a public interface if we happen to need it.
*/
nsresult Remove(const nsACString& aHost, uint32_t aAppId,
bool aInBrowserElement, const nsACString& aName,
const nsACString& aPath, bool aBlocked);
protected: protected:
// cached members. // cached members.
nsCOMPtr<nsIObserverService> mObserverService; nsCOMPtr<nsIObserverService> mObserverService;

Просмотреть файл

@ -12,7 +12,7 @@ interface nsIFile;
* Additions to the frozen nsICookieManager * Additions to the frozen nsICookieManager
*/ */
[scriptable, uuid(c0058e11-8ac3-4349-b87c-7332d0bb542c)] [scriptable, uuid(daf0caa7-b431-4b4d-ba51-08c179bb9dfe)]
interface nsICookieManager2 : nsICookieManager interface nsICookieManager2 : nsICookieManager
{ {
/** /**
@ -119,4 +119,14 @@ interface nsICookieManager2 : nsICookieManager
* special value like UNKNOWN_APP_ID or NO_APP_ID. * special value like UNKNOWN_APP_ID or NO_APP_ID.
*/ */
nsISimpleEnumerator getCookiesForApp(in unsigned long appId, in boolean onlyBrowserElement); nsISimpleEnumerator getCookiesForApp(in unsigned long appId, in boolean onlyBrowserElement);
/**
* Remove all the cookies associated with the app with the id aAppId.
*
* If onlyBrowserElement is set to true, the method will only remove the
* cookies marked as part of a browser element inside the app.
*
* Special app id values are not allowed (NO_APP_ID or UNKNOWN_APP_ID for example).
*/
void removeCookiesForApp(in unsigned long appId, in boolean onlyBrowserElement);
}; };