Bug 1730324 - [web-share] implement validate share and non-fully active check r=saschanaz
[gecko.git] / netwerk / cookie / CookieStorage.h
blob9a4abb2d7cb979ff1a90c83b82a8e03c0b957cfc
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
9 #include "CookieKey.h"
11 #include "nsIObserver.h"
12 #include "nsTHashtable.h"
13 #include "nsWeakReference.h"
14 #include <functional>
15 #include "CookieCommons.h"
17 class nsIArray;
18 class nsICookie;
19 class nsIPrefBranch;
21 namespace mozilla {
22 namespace net {
24 class Cookie;
26 // Inherit from CookieKey so this can be stored in nsTHashTable
27 // TODO: why aren't we using nsClassHashTable<CookieKey, ArrayType>?
28 class CookieEntry : public CookieKey {
29 public:
30 // Hash methods
31 using ArrayType = nsTArray<RefPtr<Cookie>>;
32 using IndexType = ArrayType::index_type;
34 explicit CookieEntry(KeyTypePointer aKey) : CookieKey(aKey) {}
36 CookieEntry(const CookieEntry& toCopy) {
37 // if we end up here, things will break. nsTHashtable shouldn't
38 // allow this, since we set ALLOW_MEMMOVE to true.
39 MOZ_ASSERT_UNREACHABLE("CookieEntry copy constructor is forbidden!");
42 ~CookieEntry() = default;
44 inline ArrayType& GetCookies() { return mCookies; }
45 inline const ArrayType& GetCookies() const { return mCookies; }
47 size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const;
49 private:
50 ArrayType mCookies;
53 // stores the CookieEntry entryclass and an index into the cookie array within
54 // that entryclass, for purposes of storing an iteration state that points to a
55 // certain cookie.
56 struct CookieListIter {
57 // default (non-initializing) constructor.
58 CookieListIter() = default;
60 // explicit constructor to a given iterator state with entryclass 'aEntry'
61 // and index 'aIndex'.
62 explicit CookieListIter(CookieEntry* aEntry, CookieEntry::IndexType aIndex)
63 : entry(aEntry), index(aIndex) {}
65 // get the Cookie * the iterator currently points to.
66 mozilla::net::Cookie* Cookie() const { return entry->GetCookies()[index]; }
68 CookieEntry* entry;
69 CookieEntry::IndexType index;
72 class CookieStorage : public nsIObserver, public nsSupportsWeakReference {
73 public:
74 NS_DECL_THREADSAFE_ISUPPORTS
75 NS_DECL_NSIOBSERVER
77 size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const;
79 void GetCookies(nsTArray<RefPtr<nsICookie>>& aCookies) const;
81 void GetSessionCookies(nsTArray<RefPtr<nsICookie>>& aCookies) const;
83 bool FindCookie(const nsACString& aBaseDomain,
84 const OriginAttributes& aOriginAttributes,
85 const nsACString& aHost, const nsACString& aName,
86 const nsACString& aPath, CookieListIter& aIter);
88 uint32_t CountCookiesFromHost(const nsACString& aBaseDomain,
89 uint32_t aPrivateBrowsingId);
91 void GetAll(nsTArray<RefPtr<nsICookie>>& aResult) const;
93 const nsTArray<RefPtr<Cookie>>* GetCookiesFromHost(
94 const nsACString& aBaseDomain, const OriginAttributes& aOriginAttributes);
96 void GetCookiesWithOriginAttributes(const OriginAttributesPattern& aPattern,
97 const nsACString& aBaseDomain,
98 nsTArray<RefPtr<nsICookie>>& aResult);
100 void RemoveCookie(const nsACString& aBaseDomain,
101 const OriginAttributes& aOriginAttributes,
102 const nsACString& aHost, const nsACString& aName,
103 const nsACString& aPath);
105 virtual void RemoveCookiesWithOriginAttributes(
106 const OriginAttributesPattern& aPattern, const nsACString& aBaseDomain);
108 virtual void RemoveCookiesFromExactHost(
109 const nsACString& aHost, const nsACString& aBaseDomain,
110 const OriginAttributesPattern& aPattern);
112 void RemoveAll();
114 void NotifyChanged(nsISupports* aSubject, const char16_t* aData,
115 bool aOldCookieIsSession = false);
117 void AddCookie(nsIConsoleReportCollector* aCRC, const nsACString& aBaseDomain,
118 const OriginAttributes& aOriginAttributes, Cookie* aCookie,
119 int64_t aCurrentTimeInUsec, nsIURI* aHostURI,
120 const nsACString& aCookieHeader, bool aFromHttp);
122 static void CreateOrUpdatePurgeList(nsIArray** aPurgedList,
123 nsICookie* aCookie);
125 virtual void StaleCookies(const nsTArray<Cookie*>& aCookieList,
126 int64_t aCurrentTimeInUsec) = 0;
128 virtual void Close() = 0;
130 protected:
131 CookieStorage() = default;
132 virtual ~CookieStorage() = default;
134 void Init();
136 void AddCookieToList(const nsACString& aBaseDomain,
137 const OriginAttributes& aOriginAttributes,
138 Cookie* aCookie);
140 virtual void StoreCookie(const nsACString& aBaseDomain,
141 const OriginAttributes& aOriginAttributes,
142 Cookie* aCookie) = 0;
144 virtual const char* NotificationTopic() const = 0;
146 virtual void NotifyChangedInternal(nsISupports* aSubject,
147 const char16_t* aData,
148 bool aOldCookieIsSession) = 0;
150 virtual void RemoveAllInternal() = 0;
152 // This method calls RemoveCookieFromDB + RemoveCookieFromListInternal.
153 void RemoveCookieFromList(const CookieListIter& aIter);
155 void RemoveCookieFromListInternal(const CookieListIter& aIter);
157 virtual void RemoveCookieFromDB(const CookieListIter& aIter) = 0;
159 already_AddRefed<nsIArray> PurgeCookiesWithCallbacks(
160 int64_t aCurrentTimeInUsec, uint16_t aMaxNumberOfCookies,
161 int64_t aCookiePurgeAge,
162 std::function<void(const CookieListIter&)>&& aRemoveCookieCallback,
163 std::function<void()>&& aFinalizeCallback);
165 nsTHashtable<CookieEntry> mHostTable;
167 uint32_t mCookieCount{0};
169 private:
170 void PrefChanged(nsIPrefBranch* aPrefBranch);
172 bool FindSecureCookie(const nsACString& aBaseDomain,
173 const OriginAttributes& aOriginAttributes,
174 Cookie* aCookie);
176 static void FindStaleCookies(CookieEntry* aEntry, int64_t aCurrentTime,
177 bool aIsSecure,
178 nsTArray<CookieListIter>& aOutput,
179 uint32_t aLimit);
181 void UpdateCookieOldestTime(Cookie* aCookie);
183 void MergeCookieSchemeMap(Cookie* aOldCookie, Cookie* aNewCookie);
185 static already_AddRefed<nsIArray> CreatePurgeList(nsICookie* aCookie);
187 virtual already_AddRefed<nsIArray> PurgeCookies(int64_t aCurrentTimeInUsec,
188 uint16_t aMaxNumberOfCookies,
189 int64_t aCookiePurgeAge) = 0;
191 int64_t mCookieOldestTime{INT64_MAX};
193 uint16_t mMaxNumberOfCookies{kMaxNumberOfCookies};
194 uint16_t mMaxCookiesPerHost{kMaxCookiesPerHost};
195 uint16_t mCookieQuotaPerHost{kCookieQuotaPerHost};
196 int64_t mCookiePurgeAge{kCookiePurgeAge};
199 } // namespace net
200 } // namespace mozilla
202 #endif // mozilla_net_CookieStorage_h