From b8dffb526665c7df786d7e69af560e4c75835953 Mon Sep 17 00:00:00 2001 From: Mounir Lamouri Date: Thu, 27 Sep 2012 22:37:02 +0100 Subject: [PATCH] Bug 783408 - 2/4 - Add a function to remove app's cookies in nsICookieManager2. r=jduell sr=mconnor --- netwerk/cookie/nsCookieService.cpp | 65 +++++++++++++++++++++++++--- netwerk/cookie/nsCookieService.h | 9 ++++ netwerk/cookie/nsICookieManager2.idl | 12 ++++- 3 files changed, 79 insertions(+), 7 deletions(-) diff --git a/netwerk/cookie/nsCookieService.cpp b/netwerk/cookie/nsCookieService.cpp index 3016b074db50..3ea54719121e 100644 --- a/netwerk/cookie/nsCookieService.cpp +++ b/netwerk/cookie/nsCookieService.cpp @@ -1800,11 +1800,11 @@ nsCookieService::Add(const nsACString &aHost, return NS_OK; } -NS_IMETHODIMP -nsCookieService::Remove(const nsACString &aHost, - const nsACString &aName, - const nsACString &aPath, - bool aBlocked) + +nsresult +nsCookieService::Remove(const nsACString& aHost, uint32_t aAppId, + bool aInBrowserElement, const nsACString& aName, + const nsACString& aPath, bool aBlocked) { if (!mDBState) { NS_WARNING("No DBState! Profile already closed?"); @@ -1822,7 +1822,7 @@ nsCookieService::Remove(const nsACString &aHost, nsListIter matchIter; nsRefPtr cookie; - if (FindCookie(DEFAULT_APP_KEY(baseDomain), + if (FindCookie(nsCookieKey(baseDomain, aAppId, aInBrowserElement), host, PromiseFlatCString(aName), PromiseFlatCString(aPath), @@ -1854,6 +1854,15 @@ nsCookieService::Remove(const nsACString &aHost, 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: * private file I/O functions @@ -3776,6 +3785,50 @@ nsCookieService::GetCookiesForApp(uint32_t aAppId, bool aOnlyBrowserElement, return NS_NewArrayEnumerator(aEnumerator, data.cookies); } +NS_IMETHODIMP +nsCookieService::RemoveCookiesForApp(uint32_t aAppId, bool aOnlyBrowserElement) +{ + nsCOMPtr enumerator; + nsresult rv = GetCookiesForApp(aAppId, aOnlyBrowserElement, + getter_AddRefs(enumerator)); + + NS_ENSURE_SUCCESS(rv, rv); + + bool hasMore; + while (NS_SUCCEEDED(enumerator->HasMoreElements(&hasMore)) && hasMore) { + nsCOMPtr 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. bool nsCookieService::FindCookie(const nsCookieKey &aKey, diff --git a/netwerk/cookie/nsCookieService.h b/netwerk/cookie/nsCookieService.h index e44759c74e10..ce648077f133 100644 --- a/netwerk/cookie/nsCookieService.h +++ b/netwerk/cookie/nsCookieService.h @@ -289,6 +289,15 @@ class nsCookieService : public nsICookieService */ 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: // cached members. nsCOMPtr mObserverService; diff --git a/netwerk/cookie/nsICookieManager2.idl b/netwerk/cookie/nsICookieManager2.idl index a1897cc2a505..b12d496c04f1 100644 --- a/netwerk/cookie/nsICookieManager2.idl +++ b/netwerk/cookie/nsICookieManager2.idl @@ -12,7 +12,7 @@ interface nsIFile; * Additions to the frozen nsICookieManager */ -[scriptable, uuid(c0058e11-8ac3-4349-b87c-7332d0bb542c)] +[scriptable, uuid(daf0caa7-b431-4b4d-ba51-08c179bb9dfe)] interface nsICookieManager2 : nsICookieManager { /** @@ -119,4 +119,14 @@ interface nsICookieManager2 : nsICookieManager * special value like UNKNOWN_APP_ID or NO_APP_ID. */ 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); };