From 4937793ee039ab70bc54e465f8be829fc9ab941a Mon Sep 17 00:00:00 2001 From: Andrea Marchesini Date: Wed, 8 Apr 2020 15:59:29 +0000 Subject: [PATCH] Bug 1610373 - Introduce nsICookieManager::getCookieSince(), r=ewright Differential Revision: https://phabricator.services.mozilla.com/D70055 --HG-- extra : moz-landing-system : lando --- netwerk/cookie/CookieService.cpp | 36 +++++++++++++++++++++++++++++ netwerk/cookie/nsICookieManager.idl | 5 ++++ 2 files changed, 41 insertions(+) diff --git a/netwerk/cookie/CookieService.cpp b/netwerk/cookie/CookieService.cpp index 3658cc0c6f26..d7b37e34e8aa 100644 --- a/netwerk/cookie/CookieService.cpp +++ b/netwerk/cookie/CookieService.cpp @@ -2064,6 +2064,42 @@ CookieService::RemoveAllSince(int64_t aSinceWhen, JSContext* aCx, return runMe->Run(); } +namespace { + +class CompareCookiesCreationTime { + public: + static bool Equals(const nsICookie* aCookie1, const nsICookie* aCookie2) { + return static_cast(aCookie1)->CreationTime() == + static_cast(aCookie2)->CreationTime(); + } + + static bool LessThan(const nsICookie* aCookie1, const nsICookie* aCookie2) { + return static_cast(aCookie1)->CreationTime() < + static_cast(aCookie2)->CreationTime(); + } +}; + +} // namespace + +NS_IMETHODIMP +CookieService::GetCookiesSince(int64_t aSinceWhen, + nsTArray>& aResult) { + mPersistentStorage->EnsureReadComplete(); + + // We expose only non-private cookies. + nsTArray> cookieList; + mPersistentStorage->GetAll(cookieList); + + for (RefPtr& cookie : cookieList) { + if (static_cast(cookie.get())->CreationTime() >= aSinceWhen) { + aResult.AppendElement(cookie); + } + } + + aResult.Sort(CompareCookiesCreationTime()); + return NS_OK; +} + size_t CookieService::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const { size_t n = aMallocSizeOf(this); diff --git a/netwerk/cookie/nsICookieManager.idl b/netwerk/cookie/nsICookieManager.idl index e757fbfd6811..00ac3991b5a3 100644 --- a/netwerk/cookie/nsICookieManager.idl +++ b/netwerk/cookie/nsICookieManager.idl @@ -247,4 +247,9 @@ interface nsICookieManager : nsISupports */ [implicit_jscontext] Promise removeAllSince(in int64_t aSinceWhen); + + /** + * Retrieves all the cookies that were created on or after aSinceWhen, order + * by creation time */ + Array getCookiesSince(in int64_t aSinceWhen); };