Merge mozilla-central to autoland. CLOSED TREE
[gecko.git] / netwerk / cookie / CookieStorage.h
blob2ab64156974564704d02ce9a8adc3c9e4fe28558
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 nsICookieTransactionCallback;
20 class nsIPrefBranch;
22 namespace mozilla {
23 namespace net {
25 class Cookie;
27 // Inherit from CookieKey so this can be stored in nsTHashTable
28 // TODO: why aren't we using nsClassHashTable<CookieKey, ArrayType>?
29 class CookieEntry : public CookieKey {
30 public:
31 // Hash methods
32 using ArrayType = nsTArray<RefPtr<Cookie>>;
33 using IndexType = ArrayType::index_type;
35 explicit CookieEntry(KeyTypePointer aKey) : CookieKey(aKey) {}
37 CookieEntry(const CookieEntry& toCopy) {
38 // if we end up here, things will break. nsTHashtable shouldn't
39 // allow this, since we set ALLOW_MEMMOVE to true.
40 MOZ_ASSERT_UNREACHABLE("CookieEntry copy constructor is forbidden!");
43 ~CookieEntry() = default;
45 inline ArrayType& GetCookies() { return mCookies; }
46 inline const ArrayType& GetCookies() const { return mCookies; }
48 size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const;
50 private:
51 ArrayType mCookies;
54 // stores the CookieEntry entryclass and an index into the cookie array within
55 // that entryclass, for purposes of storing an iteration state that points to a
56 // certain cookie.
57 struct CookieListIter {
58 // default (non-initializing) constructor.
59 CookieListIter() = default;
61 // explicit constructor to a given iterator state with entryclass 'aEntry'
62 // and index 'aIndex'.
63 explicit CookieListIter(CookieEntry* aEntry, CookieEntry::IndexType aIndex)
64 : entry(aEntry), index(aIndex) {}
66 // get the Cookie * the iterator currently points to.
67 mozilla::net::Cookie* Cookie() const { return entry->GetCookies()[index]; }
69 CookieEntry* entry;
70 CookieEntry::IndexType index;
73 class CookieStorage : public nsIObserver, public nsSupportsWeakReference {
74 public:
75 NS_DECL_THREADSAFE_ISUPPORTS
76 NS_DECL_NSIOBSERVER
78 size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const;
80 void GetCookies(nsTArray<RefPtr<nsICookie>>& aCookies) const;
82 void GetSessionCookies(nsTArray<RefPtr<nsICookie>>& aCookies) const;
84 bool FindCookie(const nsACString& aBaseDomain,
85 const OriginAttributes& aOriginAttributes,
86 const nsACString& aHost, const nsACString& aName,
87 const nsACString& aPath, CookieListIter& aIter);
89 uint32_t CountCookiesFromHost(const nsACString& aBaseDomain,
90 uint32_t aPrivateBrowsingId);
92 void GetAll(nsTArray<RefPtr<nsICookie>>& aResult) const;
94 const nsTArray<RefPtr<Cookie>>* GetCookiesFromHost(
95 const nsACString& aBaseDomain, const OriginAttributes& aOriginAttributes);
97 void GetCookiesWithOriginAttributes(const OriginAttributesPattern& aPattern,
98 const nsACString& aBaseDomain,
99 nsTArray<RefPtr<nsICookie>>& aResult);
101 void RemoveCookie(const nsACString& aBaseDomain,
102 const OriginAttributes& aOriginAttributes,
103 const nsACString& aHost, const nsACString& aName,
104 const nsACString& aPath);
106 virtual void RemoveCookiesWithOriginAttributes(
107 const OriginAttributesPattern& aPattern, const nsACString& aBaseDomain);
109 virtual void RemoveCookiesFromExactHost(
110 const nsACString& aHost, const nsACString& aBaseDomain,
111 const OriginAttributesPattern& aPattern);
113 void RemoveAll();
115 void NotifyChanged(nsISupports* aSubject, const char16_t* aData,
116 bool aOldCookieIsSession = false);
118 void AddCookie(nsIConsoleReportCollector* aCRC, const nsACString& aBaseDomain,
119 const OriginAttributes& aOriginAttributes, Cookie* aCookie,
120 int64_t aCurrentTimeInUsec, nsIURI* aHostURI,
121 const nsACString& aCookieHeader, bool aFromHttp);
123 static void CreateOrUpdatePurgeList(nsCOMPtr<nsIArray>& aPurgedList,
124 nsICookie* aCookie);
126 virtual void StaleCookies(const nsTArray<Cookie*>& aCookieList,
127 int64_t aCurrentTimeInUsec) = 0;
129 virtual void Close() = 0;
131 virtual void EnsureInitialized() = 0;
133 virtual nsresult RunInTransaction(
134 nsICookieTransactionCallback* aCallback) = 0;
136 protected:
137 CookieStorage() = default;
138 virtual ~CookieStorage() = default;
140 void Init();
142 void AddCookieToList(const nsACString& aBaseDomain,
143 const OriginAttributes& aOriginAttributes,
144 Cookie* aCookie);
146 virtual void StoreCookie(const nsACString& aBaseDomain,
147 const OriginAttributes& aOriginAttributes,
148 Cookie* aCookie) = 0;
150 virtual const char* NotificationTopic() const = 0;
152 virtual void NotifyChangedInternal(nsISupports* aSubject,
153 const char16_t* aData,
154 bool aOldCookieIsSession) = 0;
156 virtual void RemoveAllInternal() = 0;
158 // This method calls RemoveCookieFromDB + RemoveCookieFromListInternal.
159 void RemoveCookieFromList(const CookieListIter& aIter);
161 void RemoveCookieFromListInternal(const CookieListIter& aIter);
163 virtual void RemoveCookieFromDB(const CookieListIter& aIter) = 0;
165 already_AddRefed<nsIArray> PurgeCookiesWithCallbacks(
166 int64_t aCurrentTimeInUsec, uint16_t aMaxNumberOfCookies,
167 int64_t aCookiePurgeAge,
168 std::function<void(const CookieListIter&)>&& aRemoveCookieCallback,
169 std::function<void()>&& aFinalizeCallback);
171 nsTHashtable<CookieEntry> mHostTable;
173 uint32_t mCookieCount{0};
175 private:
176 void PrefChanged(nsIPrefBranch* aPrefBranch);
178 bool FindSecureCookie(const nsACString& aBaseDomain,
179 const OriginAttributes& aOriginAttributes,
180 Cookie* aCookie);
182 static void FindStaleCookies(CookieEntry* aEntry, int64_t aCurrentTime,
183 bool aIsSecure,
184 nsTArray<CookieListIter>& aOutput,
185 uint32_t aLimit);
187 void UpdateCookieOldestTime(Cookie* aCookie);
189 void MergeCookieSchemeMap(Cookie* aOldCookie, Cookie* aNewCookie);
191 static already_AddRefed<nsIArray> CreatePurgeList(nsICookie* aCookie);
193 virtual already_AddRefed<nsIArray> PurgeCookies(int64_t aCurrentTimeInUsec,
194 uint16_t aMaxNumberOfCookies,
195 int64_t aCookiePurgeAge) = 0;
197 int64_t mCookieOldestTime{INT64_MAX};
199 uint16_t mMaxNumberOfCookies{kMaxNumberOfCookies};
200 uint16_t mMaxCookiesPerHost{kMaxCookiesPerHost};
201 uint16_t mCookieQuotaPerHost{kCookieQuotaPerHost};
202 int64_t mCookiePurgeAge{kCookiePurgeAge};
205 } // namespace net
206 } // namespace mozilla
208 #endif // mozilla_net_CookieStorage_h