1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef mozilla_net_CookieStorage_h
7 #define mozilla_net_CookieStorage_h
11 #include "nsICookieNotification.h"
12 #include "nsIObserver.h"
13 #include "nsTHashtable.h"
14 #include "nsWeakReference.h"
16 #include "CookieCommons.h"
20 class nsICookieTransactionCallback
;
28 // Inherit from CookieKey so this can be stored in nsTHashTable
29 // TODO: why aren't we using nsClassHashTable<CookieKey, ArrayType>?
30 class CookieEntry
: public CookieKey
{
33 using ArrayType
= nsTArray
<RefPtr
<Cookie
>>;
34 using IndexType
= ArrayType::index_type
;
36 explicit CookieEntry(KeyTypePointer aKey
) : CookieKey(aKey
) {}
38 CookieEntry(const CookieEntry
& toCopy
) {
39 // if we end up here, things will break. nsTHashtable shouldn't
40 // allow this, since we set ALLOW_MEMMOVE to true.
41 MOZ_ASSERT_UNREACHABLE("CookieEntry copy constructor is forbidden!");
44 ~CookieEntry() = default;
46 inline ArrayType
& GetCookies() { return mCookies
; }
47 inline const ArrayType
& GetCookies() const { return mCookies
; }
49 size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf
) const;
51 bool IsPartitioned() const;
57 // stores the CookieEntry entryclass and an index into the cookie array within
58 // that entryclass, for purposes of storing an iteration state that points to a
60 struct CookieListIter
{
61 // default (non-initializing) constructor.
62 CookieListIter() = default;
64 // explicit constructor to a given iterator state with entryclass 'aEntry'
65 // and index 'aIndex'.
66 explicit CookieListIter(CookieEntry
* aEntry
, CookieEntry::IndexType aIndex
)
67 : entry(aEntry
), index(aIndex
) {}
69 // get the Cookie * the iterator currently points to.
70 mozilla::net::Cookie
* Cookie() const { return entry
->GetCookies()[index
]; }
73 CookieEntry::IndexType index
;
76 class CookieStorage
: public nsIObserver
, public nsSupportsWeakReference
{
78 NS_DECL_THREADSAFE_ISUPPORTS
81 size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf
) const;
83 void GetCookies(nsTArray
<RefPtr
<nsICookie
>>& aCookies
) const;
85 void GetSessionCookies(nsTArray
<RefPtr
<nsICookie
>>& aCookies
) const;
87 bool FindCookie(const nsACString
& aBaseDomain
,
88 const OriginAttributes
& aOriginAttributes
,
89 const nsACString
& aHost
, const nsACString
& aName
,
90 const nsACString
& aPath
, CookieListIter
& aIter
);
92 uint32_t CountCookiesFromHost(const nsACString
& aBaseDomain
,
93 uint32_t aPrivateBrowsingId
);
95 void GetAll(nsTArray
<RefPtr
<nsICookie
>>& aResult
) const;
97 const nsTArray
<RefPtr
<Cookie
>>* GetCookiesFromHost(
98 const nsACString
& aBaseDomain
, const OriginAttributes
& aOriginAttributes
);
100 void GetCookiesWithOriginAttributes(const OriginAttributesPattern
& aPattern
,
101 const nsACString
& aBaseDomain
,
102 nsTArray
<RefPtr
<nsICookie
>>& aResult
);
104 void RemoveCookie(const nsACString
& aBaseDomain
,
105 const OriginAttributes
& aOriginAttributes
,
106 const nsACString
& aHost
, const nsACString
& aName
,
107 const nsACString
& aPath
);
109 virtual void RemoveCookiesWithOriginAttributes(
110 const OriginAttributesPattern
& aPattern
, const nsACString
& aBaseDomain
);
112 virtual void RemoveCookiesFromExactHost(
113 const nsACString
& aHost
, const nsACString
& aBaseDomain
,
114 const OriginAttributesPattern
& aPattern
);
118 void NotifyChanged(nsISupports
* aSubject
,
119 nsICookieNotification::Action aAction
,
120 const nsACString
& aBaseDomain
,
121 dom::BrowsingContext
* aBrowsingContext
= nullptr,
122 bool aOldCookieIsSession
= false);
124 void AddCookie(nsIConsoleReportCollector
* aCRC
, const nsACString
& aBaseDomain
,
125 const OriginAttributes
& aOriginAttributes
, Cookie
* aCookie
,
126 int64_t aCurrentTimeInUsec
, nsIURI
* aHostURI
,
127 const nsACString
& aCookieHeader
, bool aFromHttp
,
128 dom::BrowsingContext
* aBrowsingContext
);
130 static void CreateOrUpdatePurgeList(nsCOMPtr
<nsIArray
>& aPurgedList
,
133 virtual void StaleCookies(const nsTArray
<Cookie
*>& aCookieList
,
134 int64_t aCurrentTimeInUsec
) = 0;
136 virtual void Close() = 0;
138 virtual void EnsureInitialized() = 0;
140 virtual nsresult
RunInTransaction(
141 nsICookieTransactionCallback
* aCallback
) = 0;
144 CookieStorage() = default;
145 virtual ~CookieStorage() = default;
149 void AddCookieToList(const nsACString
& aBaseDomain
,
150 const OriginAttributes
& aOriginAttributes
,
153 virtual void StoreCookie(const nsACString
& aBaseDomain
,
154 const OriginAttributes
& aOriginAttributes
,
155 Cookie
* aCookie
) = 0;
157 virtual const char* NotificationTopic() const = 0;
159 virtual void NotifyChangedInternal(nsICookieNotification
* aSubject
,
160 bool aOldCookieIsSession
) = 0;
162 virtual void RemoveAllInternal() = 0;
164 // This method calls RemoveCookieFromDB + RemoveCookieFromListInternal.
165 void RemoveCookieFromList(const CookieListIter
& aIter
);
167 void RemoveCookieFromListInternal(const CookieListIter
& aIter
);
169 virtual void RemoveCookieFromDB(const Cookie
& aCookie
) = 0;
171 already_AddRefed
<nsIArray
> PurgeCookiesWithCallbacks(
172 int64_t aCurrentTimeInUsec
, uint16_t aMaxNumberOfCookies
,
173 int64_t aCookiePurgeAge
,
174 std::function
<void(const CookieListIter
&)>&& aRemoveCookieCallback
,
175 std::function
<void()>&& aFinalizeCallback
);
177 nsTHashtable
<CookieEntry
> mHostTable
;
179 uint32_t mCookieCount
{0};
182 void PrefChanged(nsIPrefBranch
* aPrefBranch
);
184 bool FindSecureCookie(const nsACString
& aBaseDomain
,
185 const OriginAttributes
& aOriginAttributes
,
188 static void FindStaleCookies(CookieEntry
* aEntry
, int64_t aCurrentTime
,
190 nsTArray
<CookieListIter
>& aOutput
,
193 void UpdateCookieOldestTime(Cookie
* aCookie
);
195 void MergeCookieSchemeMap(Cookie
* aOldCookie
, Cookie
* aNewCookie
);
197 static already_AddRefed
<nsIArray
> CreatePurgeList(nsICookie
* aCookie
);
199 virtual already_AddRefed
<nsIArray
> PurgeCookies(int64_t aCurrentTimeInUsec
,
200 uint16_t aMaxNumberOfCookies
,
201 int64_t aCookiePurgeAge
) = 0;
203 virtual void CollectCookieJarSizeData() = 0;
205 int64_t mCookieOldestTime
{INT64_MAX
};
207 uint16_t mMaxNumberOfCookies
{kMaxNumberOfCookies
};
208 uint16_t mMaxCookiesPerHost
{kMaxCookiesPerHost
};
209 uint16_t mCookieQuotaPerHost
{kCookieQuotaPerHost
};
210 int64_t mCookiePurgeAge
{kCookiePurgeAge
};
214 } // namespace mozilla
216 #endif // mozilla_net_CookieStorage_h