blob: 1ed421b60d5264453504189af0c1b378a03c97ad [file] [log] [blame]
[email protected]63ee33bd2012-03-15 09:29:581// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// Brought to you by the letter D and the number 2.
6
7#ifndef NET_COOKIES_COOKIE_MONSTER_H_
8#define NET_COOKIES_COOKIE_MONSTER_H_
[email protected]63ee33bd2012-03-15 09:29:589
Avi Drissman13fc8932015-12-20 04:40:4610#include <stddef.h>
11#include <stdint.h>
12
[email protected]63ee33bd2012-03-15 09:29:5813#include <map>
danakja9850e12016-04-18 22:28:0814#include <memory>
[email protected]63ee33bd2012-03-15 09:29:5815#include <set>
16#include <string>
[email protected]63ee33bd2012-03-15 09:29:5817#include <vector>
18
[email protected]63ee33bd2012-03-15 09:29:5819#include "base/callback_forward.h"
Brett Wilsonc6a0c822017-09-12 00:04:2920#include "base/containers/circular_deque.h"
cfredric326a0bc2022-01-12 18:51:3021#include "base/containers/flat_map.h"
[email protected]63ee33bd2012-03-15 09:29:5822#include "base/gtest_prod_util.h"
[email protected]63ee33bd2012-03-15 09:29:5823#include "base/memory/ref_counted.h"
mmenkebe0910d2016-03-01 19:09:0924#include "base/memory/weak_ptr.h"
Maks Orlovich323efaf2018-03-06 02:56:3925#include "base/strings/string_piece.h"
mmenkebe0910d2016-03-01 19:09:0926#include "base/threading/thread_checker.h"
[email protected]9da992db2013-06-28 05:40:4727#include "base/time/time.h"
[email protected]565c3f42012-08-14 14:22:5828#include "net/base/net_export.h"
cfredric326a0bc2022-01-12 18:51:3029#include "net/base/schemeful_site.h"
[email protected]8da4b1812012-07-25 13:54:3830#include "net/cookies/canonical_cookie.h"
Lily Chenab36a112019-09-19 20:17:2831#include "net/cookies/cookie_access_delegate.h"
[email protected]ab2d75c82013-04-19 18:39:0432#include "net/cookies/cookie_constants.h"
Jihwan Marc Kim3e132f12020-05-20 17:33:1933#include "net/cookies/cookie_inclusion_status.h"
Victor Costan14f47c12018-03-01 08:02:2434#include "net/cookies/cookie_monster_change_dispatcher.h"
[email protected]63ee33bd2012-03-15 09:29:5835#include "net/cookies/cookie_store.h"
Helen Licd0fab862018-08-13 16:07:5336#include "net/log/net_log_with_source.h"
ellyjones399e35a22014-10-27 11:09:5637#include "url/gurl.h"
[email protected]63ee33bd2012-03-15 09:29:5838
[email protected]63ee33bd2012-03-15 09:29:5839namespace net {
40
Victor Costan14f47c12018-03-01 08:02:2441class CookieChangeDispatcher;
[email protected]63ee33bd2012-03-15 09:29:5842
43// The cookie monster is the system for storing and retrieving cookies. It has
44// an in-memory list of all cookies, and synchronizes non-session cookies to an
45// optional permanent storage that implements the PersistentCookieStore
46// interface.
47//
mmenke96f3bab2016-01-22 17:34:0248// Tasks may be deferred if all affected cookies are not yet loaded from the
49// backing store. Otherwise, callbacks may be invoked immediately.
[email protected]63ee33bd2012-03-15 09:29:5850//
51// A cookie task is either pending loading of the entire cookie store, or
Maks Orlovich323efaf2018-03-06 02:56:3952// loading of cookies for a specific domain key (GetKey(), roughly eTLD+1). In
53// the former case, the cookie callback will be queued in tasks_pending_ while
54// PersistentCookieStore chain loads the cookie store on DB thread. In the
55// latter case, the cookie callback will be queued in tasks_pending_for_key_
56// while PermanentCookieStore loads cookies for the specified domain key on DB
57// thread.
[email protected]63ee33bd2012-03-15 09:29:5858class NET_EXPORT CookieMonster : public CookieStore {
59 public:
[email protected]63ee33bd2012-03-15 09:29:5860 class PersistentCookieStore;
61
62 // Terminology:
63 // * The 'top level domain' (TLD) of an internet domain name is
64 // the terminal "." free substring (e.g. "com" for google.com
65 // or world.std.com).
66 // * The 'effective top level domain' (eTLD) is the longest
67 // "." initiated terminal substring of an internet domain name
68 // that is controlled by a general domain registrar.
69 // (e.g. "co.uk" for news.bbc.co.uk).
70 // * The 'effective top level domain plus one' (eTLD+1) is the
71 // shortest "." delimited terminal substring of an internet
72 // domain name that is not controlled by a general domain
73 // registrar (e.g. "bbc.co.uk" for news.bbc.co.uk, or
74 // "google.com" for news.google.com). The general assumption
75 // is that all hosts and domains under an eTLD+1 share some
76 // administrative control.
77
78 // CookieMap is the central data structure of the CookieMonster. It
79 // is a map whose values are pointers to CanonicalCookie data
80 // structures (the data structures are owned by the CookieMonster
81 // and must be destroyed when removed from the map). The key is based on the
82 // effective domain of the cookies. If the domain of the cookie has an
83 // eTLD+1, that is the key for the map. If the domain of the cookie does not
84 // have an eTLD+1, the key of the map is the host the cookie applies to (it is
85 // not legal to have domain cookies without an eTLD+1). This rule
86 // excludes cookies for, e.g, ".com", ".co.uk", or ".internalnetwork".
87 // This behavior is the same as the behavior in Firefox v 3.6.10.
Dylan Cutler0b9a4e962021-09-13 17:34:2588 // CookieMap does not store cookies that were set with the Partitioned
89 // attribute, those are stored in PartitionedCookieMap.
[email protected]63ee33bd2012-03-15 09:29:5890
91 // NOTE(deanm):
92 // I benchmarked hash_multimap vs multimap. We're going to be query-heavy
93 // so it would seem like hashing would help. However they were very
94 // close, with multimap being a tiny bit faster. I think this is because
95 // our map is at max around 1000 entries, and the additional complexity
96 // for the hashing might not overcome the O(log(1000)) for querying
97 // a multimap. Also, multimap is standard, another reason to use it.
98 // TODO(rdsmith): This benchmark should be re-done now that we're allowing
avie7cd11a2016-10-11 02:00:3599 // substantially more entries in the map.
100 using CookieMap =
101 std::multimap<std::string, std::unique_ptr<CanonicalCookie>>;
102 using CookieMapItPair = std::pair<CookieMap::iterator, CookieMap::iterator>;
103 using CookieItVector = std::vector<CookieMap::iterator>;
[email protected]8ad5d462013-05-02 08:45:26104
Dylan Cutler0b9a4e962021-09-13 17:34:25105 // PartitionedCookieMap only stores cookies that were set with the Partitioned
106 // attribute. The map is double-keyed on cookie's partition key and
107 // the cookie's effective domain of the cookie (the key of CookieMap).
108 // We store partitioned cookies in a separate map so that the queries for a
109 // request's unpartitioned and partitioned cookies will both be more
110 // efficient (since querying two smaller maps is more efficient that querying
111 // one larger map twice).
112 using PartitionedCookieMap =
113 std::map<CookiePartitionKey, std::unique_ptr<CookieMap>>;
114 using PartitionedCookieMapIterators =
115 std::pair<PartitionedCookieMap::iterator, CookieMap::iterator>;
116
[email protected]8ad5d462013-05-02 08:45:26117 // Cookie garbage collection thresholds. Based off of the Mozilla defaults.
118 // When the number of cookies gets to k{Domain,}MaxCookies
119 // purge down to k{Domain,}MaxCookies - k{Domain,}PurgeCookies.
120 // It might seem scary to have a high purge value, but really it's not.
121 // You just make sure that you increase the max to cover the increase
122 // in purge, and we would have been purging the same number of cookies.
123 // We're just going through the garbage collection process less often.
124 // Note that the DOMAIN values are per eTLD+1; see comment for the
125 // CookieMap typedef. So, e.g., the maximum number of cookies allowed for
126 // google.com and all of its subdomains will be 150-180.
127 //
128 // Any cookies accessed more recently than kSafeFromGlobalPurgeDays will not
129 // be evicted by global garbage collection, even if we have more than
130 // kMaxCookies. This does not affect domain garbage collection.
131 static const size_t kDomainMaxCookies;
132 static const size_t kDomainPurgeCookies;
133 static const size_t kMaxCookies;
134 static const size_t kPurgeCookies;
135
Lily Chen229623d72020-06-01 17:20:14136 // Max number of keys to store for domains that have been purged.
137 static const size_t kMaxDomainPurgedKeys;
138
Dylan Cutler0b9a4e962021-09-13 17:34:25139 // Partitioned cookie garbage collection thresholds.
140 static const size_t kPerPartitionDomainMaxCookies;
141 // TODO(crbug.com/1225444): Add global limit to number of partitioned cookies.
142
[email protected]8ad5d462013-05-02 08:45:26143 // Quota for cookies with {low, medium, high} priorities within a domain.
mkwst87734352016-03-03 17:36:23144 static const size_t kDomainCookiesQuotaLow;
145 static const size_t kDomainCookiesQuotaMedium;
146 static const size_t kDomainCookiesQuotaHigh;
[email protected]63ee33bd2012-03-15 09:29:58147
Matt Menke477ab632019-06-27 23:12:17148 // The number of days since last access that cookies will not be subject
149 // to global garbage collection.
150 static const int kSafeFromGlobalPurgeDays;
151
[email protected]63ee33bd2012-03-15 09:29:58152 // The store passed in should not have had Init() called on it yet. This
153 // class will take care of initializing it. The backing store is NOT owned by
154 // this class, but it must remain valid for the duration of the cookie
155 // monster's existence. If |store| is NULL, then no backing store will be
Nick Harper57142b1c2019-03-14 21:03:59156 // updated. |net_log| must outlive the CookieMonster and can be null.
Pritam8354cf702018-03-10 08:55:41157 CookieMonster(scoped_refptr<PersistentCookieStore> store,
Kirubel Akliluc9b4e412022-01-12 01:00:01158 NetLog* net_log,
159 bool first_party_sets_enabled);
nharper2b0ad9a2017-05-22 18:33:45160
[email protected]63ee33bd2012-03-15 09:29:58161 // Only used during unit testing.
Helen Lifb313a92018-08-14 15:46:44162 // |net_log| must outlive the CookieMonster.
Pritam8354cf702018-03-10 08:55:41163 CookieMonster(scoped_refptr<PersistentCookieStore> store,
Helen Lifb313a92018-08-14 15:46:44164 base::TimeDelta last_access_threshold,
Kirubel Akliluc9b4e412022-01-12 01:00:01165 NetLog* net_log,
166 bool first_party_sets_enabled);
[email protected]63ee33bd2012-03-15 09:29:58167
Peter Boström293b1342021-09-22 17:31:43168 CookieMonster(const CookieMonster&) = delete;
169 CookieMonster& operator=(const CookieMonster&) = delete;
170
mmenke606c59c2016-03-07 18:20:55171 ~CookieMonster() override;
172
rdsmith0e84cea2017-07-13 03:09:53173 // Writes all the cookies in |list| into the store, replacing all cookies
174 // currently present in store.
rdsmith2709eee2017-06-20 22:43:27175 // This method does not flush the backend.
176 // TODO(rdsmith, mmenke): Do not use this function; it is deprecated
177 // and should be removed.
178 // See https://codereview.chromium.org/2882063002/#msg64.
rdsmith7ac81712017-06-22 17:09:54179 void SetAllCookiesAsync(const CookieList& list, SetCookiesCallback callback);
drogerd5d1278c2015-03-17 19:21:51180
[email protected]63ee33bd2012-03-15 09:29:58181 // CookieStore implementation.
rdsmitha6ce4442017-06-21 17:11:05182 void SetCanonicalCookieAsync(std::unique_ptr<CanonicalCookie> cookie,
Lily Chen96f29a132020-04-15 17:59:36183 const GURL& source_url,
Maks Orlovichfdbc8be2019-03-18 18:34:52184 const CookieOptions& options,
rdsmith7ac81712017-06-22 17:09:54185 SetCookiesCallback callback) override;
Dylan Cutlercd2d8932021-10-05 19:03:43186 void GetCookieListWithOptionsAsync(const GURL& url,
187 const CookieOptions& options,
Aykut Bulut244341e2021-12-09 15:57:25188 const CookiePartitionKeyCollection& s,
Dylan Cutlercd2d8932021-10-05 19:03:43189 GetCookieListCallback callback) override;
Lily Chenf068a762019-08-21 21:10:50190 void GetAllCookiesAsync(GetAllCookiesCallback callback) override;
Lily Chene2e9ae012019-10-09 20:02:54191 void GetAllCookiesWithAccessSemanticsAsync(
192 GetAllCookiesWithAccessSemanticsCallback callback) override;
mmenke24379d52016-02-05 23:50:17193 void DeleteCanonicalCookieAsync(const CanonicalCookie& cookie,
rdsmith7ac81712017-06-22 17:09:54194 DeleteCallback callback) override;
Chris Mumfordd8ed9f82018-05-01 15:43:13195 void DeleteAllCreatedInTimeRangeAsync(
196 const CookieDeletionInfo::TimeRange& creation_range,
197 DeleteCallback callback) override;
Chris Mumford800caa62018-04-20 19:34:44198 void DeleteAllMatchingInfoAsync(CookieDeletionInfo delete_info,
199 DeleteCallback callback) override;
Christian Dullweberff11c452021-05-12 17:04:45200 void DeleteSessionCookiesAsync(DeleteCallback callback) override;
201 void DeleteMatchingCookiesAsync(DeletePredicate predicate,
202 DeleteCallback callback) override;
rdsmith7ac81712017-06-22 17:09:54203 void FlushStore(base::OnceClosure callback) override;
mmenkeded79da2016-02-06 08:28:51204 void SetForceKeepSessionState() override;
Victor Costan14f47c12018-03-01 08:02:24205 CookieChangeDispatcher& GetChangeDispatcher() override;
Nate Fischerc6fb6cf2019-03-27 00:39:49206 void SetCookieableSchemes(const std::vector<std::string>& schemes,
207 SetCookieableSchemesCallback callback) override;
mmenke74bcbd52016-01-21 17:17:56208
[email protected]63ee33bd2012-03-15 09:29:58209 // Enables writing session cookies into the cookie database. If this this
210 // method is called, it must be called before first use of the instance
211 // (i.e. as part of the instance initialization process).
212 void SetPersistSessionCookies(bool persist_session_cookies);
213
[email protected]63ee33bd2012-03-15 09:29:58214 // The default list of schemes the cookie monster can handle.
[email protected]5edff3c52014-06-23 20:27:48215 static const char* const kDefaultCookieableSchemes[];
[email protected]63ee33bd2012-03-15 09:29:58216 static const int kDefaultCookieableSchemesCount;
217
Maks Orlovich323efaf2018-03-06 02:56:39218 // Find a key based on the given domain, which will be used to find all
219 // cookies potentially relevant to it. This is used for lookup in cookies_ as
220 // well as for PersistentCookieStore::LoadCookiesForKey. See comment on keys
221 // before the CookieMap typedef.
222 static std::string GetKey(base::StringPiece domain);
223
cfredric59f8a8452021-06-08 15:27:11224 // Exposes the comparison function used when sorting cookies.
225 static bool CookieSorter(const CanonicalCookie* cc1,
226 const CanonicalCookie* cc2);
227
Lily Chen229623d72020-06-01 17:20:14228 // Triggers immediate recording of stats that are typically reported
229 // periodically.
230 bool DoRecordPeriodicStatsForTesting() { return DoRecordPeriodicStats(); }
231
[email protected]63ee33bd2012-03-15 09:29:58232 private:
avie7cd11a2016-10-11 02:00:35233 // For garbage collection constants.
[email protected]63ee33bd2012-03-15 09:29:58234 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestHostGarbageCollection);
mmenkef4721d992017-06-07 17:13:59235 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest,
236 GarbageCollectWithSecureCookiesOnly);
[email protected]63ee33bd2012-03-15 09:29:58237 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestGCTimes);
238
239 // For validation of key values.
240 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestDomainTree);
241 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestImport);
242 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, GetKey);
243 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestGetKey);
244
245 // For FindCookiesForKey.
246 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, ShortLivedSessionCookies);
247
estark7feb65c2b2015-08-21 23:38:20248 // For CookieSource histogram enum.
249 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, CookieSourceHistogram);
250
jww31e32632015-12-16 23:38:34251 // For kSafeFromGlobalPurgeDays in CookieStore.
jwwa26e439d2017-01-27 18:17:27252 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, EvictSecureCookies);
jww82d99c12015-11-25 18:39:53253
jww31e32632015-12-16 23:38:34254 // For CookieDeleteEquivalent histogram enum.
255 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest,
256 CookieDeleteEquivalentHistogramTest);
jww31e32632015-12-16 23:38:34257
Steven Bingler0420a3752020-11-20 21:40:48258 // For CookieSentToSamePort enum.
259 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest,
260 CookiePortReadDiffersFromSetHistogram);
261 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, IsCookieSentToSamePortThatSetIt);
262
[email protected]63ee33bd2012-03-15 09:29:58263 // Internal reasons for deletion, used to populate informative histograms
264 // and to provide a public cause for onCookieChange notifications.
265 //
266 // If you add or remove causes from this list, please be sure to also update
Victor Costan14f47c12018-03-01 08:02:24267 // the CookieChangeCause mapping inside ChangeCauseMapping. New items (if
268 // necessary) should be added at the end of the list, just before
Nick Harper7a6683a2018-01-30 20:42:52269 // DELETE_COOKIE_LAST_ENTRY.
[email protected]63ee33bd2012-03-15 09:29:58270 enum DeletionCause {
271 DELETE_COOKIE_EXPLICIT = 0,
mkwstaa07ee82016-03-11 15:32:14272 DELETE_COOKIE_OVERWRITE = 1,
273 DELETE_COOKIE_EXPIRED = 2,
274 DELETE_COOKIE_EVICTED = 3,
275 DELETE_COOKIE_DUPLICATE_IN_BACKING_STORE = 4,
276 DELETE_COOKIE_DONT_RECORD = 5, // For final cleanup after flush to store.
[email protected]63ee33bd2012-03-15 09:29:58277
mkwstaa07ee82016-03-11 15:32:14278 // Cookies evicted during domain-level garbage collection.
279 DELETE_COOKIE_EVICTED_DOMAIN = 6,
[email protected]63ee33bd2012-03-15 09:29:58280
Dylan Cutler0b9a4e962021-09-13 17:34:25281 // Cookies evicted during global garbage collection, which takes place after
mkwstaa07ee82016-03-11 15:32:14282 // domain-level garbage collection fails to bring the cookie store under
283 // the overall quota.
284 DELETE_COOKIE_EVICTED_GLOBAL = 7,
285
286 // #8 was DELETE_COOKIE_EVICTED_DOMAIN_PRE_SAFE
287 // #9 was DELETE_COOKIE_EVICTED_DOMAIN_POST_SAFE
[email protected]63ee33bd2012-03-15 09:29:58288
289 // A common idiom is to remove a cookie by overwriting it with an
290 // already-expired expiration date. This captures that case.
mkwstaa07ee82016-03-11 15:32:14291 DELETE_COOKIE_EXPIRED_OVERWRITE = 10,
[email protected]63ee33bd2012-03-15 09:29:58292
[email protected]6210ce52013-09-20 03:33:14293 // Cookies are not allowed to contain control characters in the name or
294 // value. However, we used to allow them, so we are now evicting any such
295 // cookies as we load them. See http://crbug.com/238041.
mkwstaa07ee82016-03-11 15:32:14296 DELETE_COOKIE_CONTROL_CHAR = 11,
[email protected]6210ce52013-09-20 03:33:14297
jww82d99c12015-11-25 18:39:53298 // When strict secure cookies is enabled, non-secure cookies are evicted
299 // right after expired cookies.
mkwstaa07ee82016-03-11 15:32:14300 DELETE_COOKIE_NON_SECURE = 12,
jww82d99c12015-11-25 18:39:53301
Dylan Cutler0b9a4e962021-09-13 17:34:25302 // Partitioned cookies evicted during per-partition domain-level garbage
303 // collection.
304 DELETE_COOKIE_EVICTED_PER_PARTITION_DOMAIN = 13,
305
306 DELETE_COOKIE_LAST_ENTRY = 14,
[email protected]63ee33bd2012-03-15 09:29:58307 };
308
mkwstc1aa4cc2015-04-03 19:57:45309 // This enum is used to generate a histogramed bitmask measureing the types
310 // of stored cookies. Please do not reorder the list when adding new entries.
311 // New items MUST be added at the end of the list, just before
312 // COOKIE_TYPE_LAST_ENTRY;
Lily Chenda465cca2021-03-08 23:47:17313 // There will be 2^COOKIE_TYPE_LAST_ENTRY buckets in the linear histogram.
mkwstc1aa4cc2015-04-03 19:57:45314 enum CookieType {
mkwst46549412016-02-01 10:05:37315 COOKIE_TYPE_SAME_SITE = 0,
mkwstc1aa4cc2015-04-03 19:57:45316 COOKIE_TYPE_HTTPONLY,
317 COOKIE_TYPE_SECURE,
318 COOKIE_TYPE_LAST_ENTRY
319 };
320
estark7feb65c2b2015-08-21 23:38:20321 // Used to populate a histogram containing information about the
322 // sources of Secure and non-Secure cookies: that is, whether such
323 // cookies are set by origins with cryptographic or non-cryptographic
324 // schemes. Please do not reorder the list when adding new
Lily Chenda465cca2021-03-08 23:47:17325 // entries. New items MUST be added at the end of the list, and kMaxValue
326 // should be updated to the last value.
estark7feb65c2b2015-08-21 23:38:20327 //
328 // COOKIE_SOURCE_(NON)SECURE_COOKIE_(NON)CRYPTOGRAPHIC_SCHEME means
329 // that a cookie was set or overwritten from a URL with the given type
330 // of scheme. This enum should not be used when cookies are *cleared*,
331 // because its purpose is to understand if Chrome can deprecate the
332 // ability of HTTP urls to set/overwrite Secure cookies.
333 enum CookieSource {
334 COOKIE_SOURCE_SECURE_COOKIE_CRYPTOGRAPHIC_SCHEME = 0,
335 COOKIE_SOURCE_SECURE_COOKIE_NONCRYPTOGRAPHIC_SCHEME,
336 COOKIE_SOURCE_NONSECURE_COOKIE_CRYPTOGRAPHIC_SCHEME,
337 COOKIE_SOURCE_NONSECURE_COOKIE_NONCRYPTOGRAPHIC_SCHEME,
Lily Chenda465cca2021-03-08 23:47:17338 kMaxValue = COOKIE_SOURCE_NONSECURE_COOKIE_NONCRYPTOGRAPHIC_SCHEME
estark7feb65c2b2015-08-21 23:38:20339 };
340
Steven Bingler0420a3752020-11-20 21:40:48341 // Enum for collecting metrics on how frequently a cookie is sent to the same
342 // port it was set by.
343 //
344 // kNoButDefault exists because we expect for cookies being sent between
345 // schemes to have a port mismatch and want to separate those out from other,
346 // more interesting, cases.
347 //
348 // Do not reorder or renumber. Used for metrics.
349 enum class CookieSentToSamePort {
350 kSourcePortUnspecified = 0, // Cookie's source port is unspecified, we
351 // can't know if this is the same port or not.
352 kInvalid = 1, // The source port was corrupted to be PORT_INVALID, we
353 // can't know if this is the same port or not.
354 kNo = 2, // Source port and destination port are different.
355 kNoButDefault =
356 3, // Source and destination ports are different but they're
357 // the defaults for their scheme. This can mean that an http
358 // cookie was sent to a https origin or vice-versa.
359 kYes = 4, // They're the same.
360 kMaxValue = kYes
361 };
362
[email protected]63ee33bd2012-03-15 09:29:58363 // Record statistics every kRecordStatisticsIntervalSeconds of uptime.
364 static const int kRecordStatisticsIntervalSeconds = 10 * 60;
365
rdsmitha6ce4442017-06-21 17:11:05366 // Sets a canonical cookie, deletes equivalents and performs garbage
Lily Chen96f29a132020-04-15 17:59:36367 // collection. |source_url| indicates what URL the cookie is being set
Maks Orlovichfdbc8be2019-03-18 18:34:52368 // from; secure cookies cannot be altered from insecure schemes, and some
369 // schemes may not be authorized.
370 //
371 // |options| indicates if this setting operation is allowed
372 // to affect http_only or same-site cookies.
rdsmithe5c701d2017-07-12 21:50:00373 void SetCanonicalCookie(std::unique_ptr<CanonicalCookie> cookie,
Lily Chen96f29a132020-04-15 17:59:36374 const GURL& source_url,
Maks Orlovichfdbc8be2019-03-18 18:34:52375 const CookieOptions& options,
rdsmithe5c701d2017-07-12 21:50:00376 SetCookiesCallback callback);
rdsmitha6ce4442017-06-21 17:11:05377
Lily Chenf068a762019-08-21 21:10:50378 void GetAllCookies(GetAllCookiesCallback callback);
[email protected]63ee33bd2012-03-15 09:29:58379
Lily Chene2e9ae012019-10-09 20:02:54380 void AttachAccessSemanticsListForCookieList(
381 GetAllCookiesWithAccessSemanticsCallback callback,
382 const CookieList& cookie_list);
383
Dylan Cutler0b9a4e962021-09-13 17:34:25384 void GetCookieListWithOptions(
385 const GURL& url,
386 const CookieOptions& options,
Aykut Bulut244341e2021-12-09 15:57:25387 const CookiePartitionKeyCollection& cookie_partition_key_collection,
Dylan Cutler0b9a4e962021-09-13 17:34:25388 GetCookieListCallback callback);
[email protected]63ee33bd2012-03-15 09:29:58389
Chris Mumfordd8ed9f82018-05-01 15:43:13390 void DeleteAllCreatedInTimeRange(
391 const CookieDeletionInfo::TimeRange& creation_range,
392 DeleteCallback callback);
[email protected]63ee33bd2012-03-15 09:29:58393
Christian Dullweberff11c452021-05-12 17:04:45394 // Returns whether |cookie| matches |delete_info|.
395 bool MatchCookieDeletionInfo(const CookieDeletionInfo& delete_info,
396 const net::CanonicalCookie& cookie);
[email protected]63ee33bd2012-03-15 09:29:58397
rdsmithe5c701d2017-07-12 21:50:00398 void DeleteCanonicalCookie(const CanonicalCookie& cookie,
399 DeleteCallback callback);
mmenke24379d52016-02-05 23:50:17400
Christian Dullweberff11c452021-05-12 17:04:45401 void DeleteMatchingCookies(DeletePredicate predicate,
402 DeletionCause cause,
403 DeleteCallback callback);
[email protected]264807b2012-04-25 14:49:37404
erikchen1dd72a72015-05-06 20:45:05405 // The first access to the cookie store initializes it. This method should be
406 // called before any access to the cookie store.
407 void MarkCookieStoreAsInitialized();
[email protected]63ee33bd2012-03-15 09:29:58408
erikchen1dd72a72015-05-06 20:45:05409 // Fetches all cookies if the backing store exists and they're not already
410 // being fetched.
erikchen1dd72a72015-05-06 20:45:05411 void FetchAllCookiesIfNecessary();
412
413 // Fetches all cookies from the backing store.
erikchen1dd72a72015-05-06 20:45:05414 void FetchAllCookies();
415
416 // Whether all cookies should be fetched as soon as any is requested.
417 bool ShouldFetchAllCookiesWhenFetchingAnyCookie();
[email protected]63ee33bd2012-03-15 09:29:58418
419 // Stores cookies loaded from the backing store and invokes any deferred
420 // calls. |beginning_time| should be the moment PersistentCookieStore::Load
421 // was invoked and is used for reporting histogram_time_blocked_on_load_.
422 // See PersistentCookieStore::Load for details on the contents of cookies.
423 void OnLoaded(base::TimeTicks beginning_time,
avie7cd11a2016-10-11 02:00:35424 std::vector<std::unique_ptr<CanonicalCookie>> cookies);
[email protected]63ee33bd2012-03-15 09:29:58425
426 // Stores cookies loaded from the backing store and invokes the deferred
427 // task(s) pending loading of cookies associated with the domain key
Maks Orlovich323efaf2018-03-06 02:56:39428 // (GetKey, roughly eTLD+1). Called when all cookies for the domain key have
429 // been loaded from DB. See PersistentCookieStore::Load for details on the
430 // contents of cookies.
mkwstbe84af312015-02-20 08:52:45431 void OnKeyLoaded(const std::string& key,
avie7cd11a2016-10-11 02:00:35432 std::vector<std::unique_ptr<CanonicalCookie>> cookies);
[email protected]63ee33bd2012-03-15 09:29:58433
434 // Stores the loaded cookies.
avie7cd11a2016-10-11 02:00:35435 void StoreLoadedCookies(
436 std::vector<std::unique_ptr<CanonicalCookie>> cookies);
[email protected]63ee33bd2012-03-15 09:29:58437
438 // Invokes deferred calls.
439 void InvokeQueue();
440
441 // Checks that |cookies_| matches our invariants, and tries to repair any
442 // inconsistencies. (In other words, it does not have duplicate cookies).
443 void EnsureCookiesMapIsValid();
444
445 // Checks for any duplicate cookies for CookieMap key |key| which lie between
446 // |begin| and |end|. If any are found, all but the most recent are deleted.
Dylan Cutler0b9a4e962021-09-13 17:34:25447 //
448 // If |cookie_partition_it| is not nullopt, then this function trims cookies
449 // from the CookieMap in |partitioned_cookies_| at |cookie_partition_it|
450 // instead of trimming cookies from |cookies_|.
451 void TrimDuplicateCookiesForKey(
452 const std::string& key,
453 CookieMap::iterator begin,
454 CookieMap::iterator end,
455 absl::optional<PartitionedCookieMap::iterator> cookie_partition_it);
[email protected]63ee33bd2012-03-15 09:29:58456
457 void SetDefaultCookieableSchemes();
458
cfredric09ba72fb2020-12-22 21:03:27459 std::vector<CanonicalCookie*> FindCookiesForRegistryControlledHost(
Dylan Cutler0b9a4e962021-09-13 17:34:25460 const GURL& url,
461 CookieMap* cookie_map = nullptr);
462
463 std::vector<CanonicalCookie*> FindPartitionedCookiesForRegistryControlledHost(
464 const CookiePartitionKey& cookie_partition_key,
cfredric09ba72fb2020-12-22 21:03:27465 const GURL& url);
[email protected]63ee33bd2012-03-15 09:29:58466
Lily Chenf068a762019-08-21 21:10:50467 void FilterCookiesWithOptions(const GURL url,
468 const CookieOptions options,
469 std::vector<CanonicalCookie*>* cookie_ptrs,
Ayu Ishiibc6fdb0a2020-06-08 22:59:19470 CookieAccessResultList* included_cookies,
471 CookieAccessResultList* excluded_cookies);
Lily Chenf068a762019-08-21 21:10:50472
Lily Chen4c5f8632019-10-30 18:11:51473 // Possibly delete an existing cookie equivalent to |cookie_being_set| (same
474 // path, domain, and name).
Mike Westc4a777b2017-10-06 14:04:20475 //
Maks Orlovichbd04d782020-11-17 21:23:34476 // |allowed_to_set_secure_cookie| indicates if the source may override
477 // existing secure cookies. If the source is not trustworthy, and there is an
478 // existing "equivalent" cookie that is Secure, that cookie will be preserved,
479 // under "Leave Secure Cookies Alone" (see
Lily Chen4c5f8632019-10-30 18:11:51480 // https://tools.ietf.org/html/draft-ietf-httpbis-cookie-alone-01).
481 // ("equivalent" here is in quotes because the equivalency check for the
482 // purposes of preserving existing Secure cookies is slightly more inclusive.)
483 //
484 // If |skip_httponly| is true, httponly cookies will not be deleted even if
485 // they are equivalent.
486 // |key| is the key to find the cookie in cookies_; see the comment before the
jwwa26e439d2017-01-27 18:17:27487 // CookieMap typedef for details.
Mike Westc4a777b2017-10-06 14:04:20488 //
Lily Chen4c5f8632019-10-30 18:11:51489 // If a cookie is deleted, and its value matches |cookie_being_set|'s value,
490 // then |creation_date_to_inherit| will be set to that cookie's creation date.
Mike Westc4a777b2017-10-06 14:04:20491 //
Lily Chenf53dfbcd2019-08-30 01:42:10492 // The cookie will not be deleted if |*status| is not "include" when calling
493 // the function. The function will update |*status| with exclusion reasons if
494 // a secure cookie was skipped or an httponly cookie was skipped.
495 //
Dylan Cutler0b9a4e962021-09-13 17:34:25496 // If |cookie_partition_it| is nullopt, it will search |cookies_| for
497 // duplicates of |cookie_being_set|. Otherwise, |cookie_partition_it|'s value
498 // is the iterator of the CookieMap in |partitioned_cookies_| we should search
499 // for duplicates.
500 //
[email protected]63ee33bd2012-03-15 09:29:58501 // NOTE: There should never be more than a single matching equivalent cookie.
Lily Chenf53dfbcd2019-08-30 01:42:10502 void MaybeDeleteEquivalentCookieAndUpdateStatus(
Aaron Tagliaboschi29764f52019-02-21 17:19:59503 const std::string& key,
Lily Chen4c5f8632019-10-30 18:11:51504 const CanonicalCookie& cookie_being_set,
Maks Orlovichbd04d782020-11-17 21:23:34505 bool allowed_to_set_secure_cookie,
Aaron Tagliaboschi29764f52019-02-21 17:19:59506 bool skip_httponly,
507 bool already_expired,
Lily Chenf53dfbcd2019-08-30 01:42:10508 base::Time* creation_date_to_inherit,
Dylan Cutler0b9a4e962021-09-13 17:34:25509 CookieInclusionStatus* status,
510 absl::optional<PartitionedCookieMap::iterator> cookie_partition_it);
[email protected]63ee33bd2012-03-15 09:29:58511
Lily Chend8d11db2021-02-25 19:50:52512 // Inserts `cc` into cookies_. Returns an iterator that points to the inserted
513 // cookie in `cookies_`. Guarantee: all iterators to `cookies_` remain valid.
514 // Dispatches the change to `change_dispatcher_` iff `dispatch_change` is
515 // true.
Ayu Ishii9f5e72dc2020-07-22 19:43:18516 CookieMap::iterator InternalInsertCookie(
517 const std::string& key,
518 std::unique_ptr<CanonicalCookie> cc,
519 bool sync_to_store,
Lily Chend8d11db2021-02-25 19:50:52520 const CookieAccessResult& access_result,
521 bool dispatch_change = true);
[email protected]63ee33bd2012-03-15 09:29:58522
Dylan Cutler0b9a4e962021-09-13 17:34:25523 // Returns true if the cookie should be (or is already) synced to the store.
524 // Used for cookies during insertion and deletion into the in-memory store.
525 bool ShouldUpdatePersistentStore(CanonicalCookie* cc);
526
527 void LogCookieTypeToUMA(CanonicalCookie* cc,
528 const CookieAccessResult& access_result);
529
530 // Inserts `cc` into partitioned_cookies_. Should only be used when
531 // cc->IsPartitioned() is true.
532 PartitionedCookieMapIterators InternalInsertPartitionedCookie(
533 std::string key,
534 std::unique_ptr<CanonicalCookie> cc,
535 bool sync_to_store,
536 const CookieAccessResult& access_result,
537 bool dispatch_change = true);
538
rdsmith2709eee2017-06-20 22:43:27539 // Sets all cookies from |list| after deleting any equivalent cookie.
540 // For data gathering purposes, this routine is treated as if it is
541 // restoring saved cookies; some statistics are not gathered in this case.
rdsmithe5c701d2017-07-12 21:50:00542 void SetAllCookies(CookieList list, SetCookiesCallback callback);
drogerd5d1278c2015-03-17 19:21:51543
[email protected]63ee33bd2012-03-15 09:29:58544 void InternalUpdateCookieAccessTime(CanonicalCookie* cc,
545 const base::Time& current_time);
546
547 // |deletion_cause| argument is used for collecting statistics and choosing
Victor Costan14f47c12018-03-01 08:02:24548 // the correct CookieChangeCause for OnCookieChange notifications. Guarantee:
549 // All iterators to cookies_, except for the deleted entry, remain valid.
mkwstbe84af312015-02-20 08:52:45550 void InternalDeleteCookie(CookieMap::iterator it,
551 bool sync_to_store,
[email protected]63ee33bd2012-03-15 09:29:58552 DeletionCause deletion_cause);
553
Dylan Cutler0b9a4e962021-09-13 17:34:25554 // Deletes a Partitioned cookie. Returns true if the deletion operation
555 // resulted in the CookieMap the cookie was stored in was deleted.
556 //
557 // If the CookieMap which contains the deleted cookie only has one entry, then
558 // this function will also delete the CookieMap from PartitionedCookieMap.
559 // This may invalidate the |cookie_partition_it| argument.
560 void InternalDeletePartitionedCookie(
561 PartitionedCookieMap::iterator partition_it,
562 CookieMap::iterator cookie_it,
563 bool sync_to_store,
564 DeletionCause deletion_cause);
565
[email protected]63ee33bd2012-03-15 09:29:58566 // If the number of cookies for CookieMap key |key|, or globally, are
567 // over the preset maximums above, garbage collect, first for the host and
568 // then globally. See comments above garbage collection threshold
Dylan Cutler0b9a4e962021-09-13 17:34:25569 // constants for details. Also removes expired cookies.
[email protected]63ee33bd2012-03-15 09:29:58570 //
571 // Returns the number of cookies deleted (useful for debugging).
jwwa26e439d2017-01-27 18:17:27572 size_t GarbageCollect(const base::Time& current, const std::string& key);
[email protected]63ee33bd2012-03-15 09:29:58573
Dylan Cutler0b9a4e962021-09-13 17:34:25574 // Run garbage collection for PartitionedCookieMap keys |cookie_partition_key|
575 // and |key|.
576 //
577 // Partitioned cookies are subject to different limits than unpartitioned
578 // cookies in order to prevent leaking entropy about user behavior across
579 // cookie partitions.
580 size_t GarbageCollectPartitionedCookies(
581 const base::Time& current,
582 const CookiePartitionKey& cookie_partition_key,
583 const std::string& key);
584
mkwste079ac412016-03-11 09:04:06585 // Helper for GarbageCollect(). Deletes up to |purge_goal| cookies with a
586 // priority less than or equal to |priority| from |cookies|, while ensuring
587 // that at least the |to_protect| most-recent cookies are retained.
jwwc00ac712016-05-05 22:21:44588 // |protected_secure_cookies| specifies whether or not secure cookies should
589 // be protected from deletion.
mkwste079ac412016-03-11 09:04:06590 //
591 // |cookies| must be sorted from least-recent to most-recent.
592 //
mkwste079ac412016-03-11 09:04:06593 // Returns the number of cookies deleted.
594 size_t PurgeLeastRecentMatches(CookieItVector* cookies,
595 CookiePriority priority,
596 size_t to_protect,
jwwc00ac712016-05-05 22:21:44597 size_t purge_goal,
598 bool protect_secure_cookies);
mkwste079ac412016-03-11 09:04:06599
jww82d99c12015-11-25 18:39:53600 // Helper for GarbageCollect(); can be called directly as well. Deletes all
601 // expired cookies in |itpair|. If |cookie_its| is non-NULL, all the
602 // non-expired cookies from |itpair| are appended to |cookie_its|.
[email protected]63ee33bd2012-03-15 09:29:58603 //
604 // Returns the number of cookies deleted.
jww82d99c12015-11-25 18:39:53605 size_t GarbageCollectExpired(const base::Time& current,
606 const CookieMapItPair& itpair,
607 CookieItVector* cookie_its);
608
Dylan Cutler0b9a4e962021-09-13 17:34:25609 // Deletes all expired cookies in the double-keyed PartitionedCookie map in
610 // the CookieMap at |cookie_partition_it|. It deletes all cookies in that
611 // CookieMap in |itpair|. If |cookie_its| is non-NULL, all non-expired cookies
612 // from |itpair| are appended to |cookie_its|.
613 //
614 // Returns the number of cookies deleted.
615 size_t GarbageCollectExpiredPartitionedCookies(
616 const base::Time& current,
617 const PartitionedCookieMap::iterator& cookie_partition_it,
618 const CookieMapItPair& itpair,
619 CookieItVector* cookie_its);
620
621 // Helper function to garbage collect all expired cookies in
622 // PartitionedCookieMap.
623 void GarbageCollectAllExpiredPartitionedCookies(const base::Time& current);
624
[email protected]8ad5d462013-05-02 08:45:26625 // Helper for GarbageCollect(). Deletes all cookies in the range specified by
626 // [|it_begin|, |it_end|). Returns the number of cookies deleted.
jww82d99c12015-11-25 18:39:53627 size_t GarbageCollectDeleteRange(const base::Time& current,
628 DeletionCause cause,
629 CookieItVector::iterator cookie_its_begin,
630 CookieItVector::iterator cookie_its_end);
631
632 // Helper for GarbageCollect(). Deletes cookies in |cookie_its| from least to
633 // most recently used, but only before |safe_date|. Also will stop deleting
634 // when the number of remaining cookies hits |purge_goal|.
mmenkef4721d992017-06-07 17:13:59635 //
636 // Sets |earliest_time| to be the earliest last access time of a cookie that
637 // was not deleted, or base::Time() if no such cookie exists.
jww82d99c12015-11-25 18:39:53638 size_t GarbageCollectLeastRecentlyAccessed(const base::Time& current,
639 const base::Time& safe_date,
640 size_t purge_goal,
mmenkef4721d992017-06-07 17:13:59641 CookieItVector cookie_its,
642 base::Time* earliest_time);
[email protected]63ee33bd2012-03-15 09:29:58643
[email protected]63ee33bd2012-03-15 09:29:58644 bool HasCookieableScheme(const GURL& url);
645
Lily Chenb0eedc22020-10-26 16:34:42646 // Get the cookie's access semantics (LEGACY or NONLEGACY), by checking for a
Lily Chenb0ca3f72019-12-05 18:06:29647 // value from the cookie access delegate, if it is non-null. Otherwise returns
648 // UNKNOWN.
Lily Chen70f997f2019-10-07 22:01:37649 CookieAccessSemantics GetAccessSemanticsForCookie(
650 const CanonicalCookie& cookie) const;
651
[email protected]63ee33bd2012-03-15 09:29:58652 // Statistics support
653
654 // This function should be called repeatedly, and will record
655 // statistics if a sufficient time period has passed.
656 void RecordPeriodicStats(const base::Time& current_time);
657
Lily Chen229623d72020-06-01 17:20:14658 // Records the aforementioned stats if we have already finished loading all
659 // cookies. Returns whether stats were recorded.
660 bool DoRecordPeriodicStats();
661
cfredric326a0bc2022-01-12 18:51:30662 // Records periodic stats related to First-Party Sets usage. Note that since
663 // First-Party Sets presents a potentially asynchronous interface, these stats
664 // may be collected asynchronously w.r.t. the rest of the stats collected by
665 // `RecordPeriodicStats`.
666 // TODO(https://crbug.com/1266014): don't assume that the sets can all fit in
667 // memory at once.
668 void RecordPeriodicFirstPartySetsStats(
669 base::flat_map<SchemefulSite, std::set<SchemefulSite>> sets) const;
670
Maks Orlovich323efaf2018-03-06 02:56:39671 // Defers the callback until the full coookie database has been loaded. If
672 // it's already been loaded, runs the callback synchronously.
rdsmithe5c701d2017-07-12 21:50:00673 void DoCookieCallback(base::OnceClosure callback);
[email protected]63ee33bd2012-03-15 09:29:58674
Maks Orlovich323efaf2018-03-06 02:56:39675 // Defers the callback until the cookies relevant to given URL have been
676 // loaded. If they've already been loaded, runs the callback synchronously.
rdsmithe5c701d2017-07-12 21:50:00677 void DoCookieCallbackForURL(base::OnceClosure callback, const GURL& url);
[email protected]63ee33bd2012-03-15 09:29:58678
Maks Orlovich323efaf2018-03-06 02:56:39679 // Defers the callback until the cookies relevant to given host or domain
680 // have been loaded. If they've already been loaded, runs the callback
681 // synchronously.
682 void DoCookieCallbackForHostOrDomain(base::OnceClosure callback,
683 base::StringPiece host_or_domain);
684
Steven Bingler0420a3752020-11-20 21:40:48685 // Checks to see if a cookie is being sent to the same port it was set by. For
686 // metrics.
687 //
688 // This is in CookieMonster because only CookieMonster uses it. It's otherwise
689 // a standalone utility function.
690 static CookieSentToSamePort IsCookieSentToSamePortThatSetIt(
691 const GURL& destination,
692 int source_port,
693 CookieSourceScheme source_scheme);
694
Lily Chen229623d72020-06-01 17:20:14695 // Set of keys (eTLD+1's) for which non-expired cookies have
696 // been evicted for hitting the per-domain max. The size of this set is
697 // histogrammed periodically. The size is limited to |kMaxDomainPurgedKeys|.
698 std::set<std::string> domain_purged_keys_;
699
700 // The number of distinct keys (eTLD+1's) currently present in the |cookies_|
701 // multimap. This is histogrammed periodically.
702 size_t num_keys_;
703
[email protected]63ee33bd2012-03-15 09:29:58704 CookieMap cookies_;
705
Dylan Cutler0b9a4e962021-09-13 17:34:25706 PartitionedCookieMap partitioned_cookies_;
707
708 // Number of distinct partitioned cookies globally. This is used to enforce a
709 // global maximum on the number of partitioned cookies.
710 size_t num_partitioned_cookies_;
711
Victor Costan14f47c12018-03-01 08:02:24712 CookieMonsterChangeDispatcher change_dispatcher_;
713
erikchen1dd72a72015-05-06 20:45:05714 // Indicates whether the cookie store has been initialized.
[email protected]63ee33bd2012-03-15 09:29:58715 bool initialized_;
716
erikchen1dd72a72015-05-06 20:45:05717 // Indicates whether the cookie store has started fetching all cookies.
718 bool started_fetching_all_cookies_;
719 // Indicates whether the cookie store has finished fetching all cookies.
720 bool finished_fetching_all_cookies_;
[email protected]63ee33bd2012-03-15 09:29:58721
722 // List of domain keys that have been loaded from the DB.
723 std::set<std::string> keys_loaded_;
724
725 // Map of domain keys to their associated task queues. These tasks are blocked
726 // until all cookies for the associated domain key eTLD+1 are loaded from the
727 // backend store.
Brett Wilsonc6a0c822017-09-12 00:04:29728 std::map<std::string, base::circular_deque<base::OnceClosure>>
729 tasks_pending_for_key_;
[email protected]63ee33bd2012-03-15 09:29:58730
731 // Queues tasks that are blocked until all cookies are loaded from the backend
732 // store.
Brett Wilsonc6a0c822017-09-12 00:04:29733 base::circular_deque<base::OnceClosure> tasks_pending_;
mmenkef49fca0e2016-03-08 12:46:24734
735 // Once a global cookie task has been seen, all per-key tasks must be put in
736 // |tasks_pending_| instead of |tasks_pending_for_key_| to ensure a reasonable
rdsmithe5c701d2017-07-12 21:50:00737 // view of the cookie store. This is more to ensure fancy cookie export/import
mmenkef49fca0e2016-03-08 12:46:24738 // code has a consistent view of the CookieStore, rather than out of concern
739 // for typical use.
740 bool seen_global_task_;
[email protected]63ee33bd2012-03-15 09:29:58741
Helen Licd0fab862018-08-13 16:07:53742 NetLogWithSource net_log_;
743
[email protected]63ee33bd2012-03-15 09:29:58744 scoped_refptr<PersistentCookieStore> store_;
745
[email protected]63ee33bd2012-03-15 09:29:58746 // Minimum delay after updating a cookie's LastAccessDate before we will
747 // update it again.
748 const base::TimeDelta last_access_threshold_;
749
750 // Approximate date of access time of least recently accessed cookie
751 // in |cookies_|. Note that this is not guaranteed to be accurate, only a)
752 // to be before or equal to the actual time, and b) to be accurate
mmenkef4721d992017-06-07 17:13:59753 // immediately after a garbage collection that scans through all the cookies
754 // (When garbage collection does not scan through all cookies, it may not be
755 // updated). This value is used to determine whether global garbage collection
756 // might find cookies to purge. Note: The default Time() constructor will
757 // create a value that compares earlier than any other time value, which is
758 // wanted. Thus this value is not initialized.
[email protected]63ee33bd2012-03-15 09:29:58759 base::Time earliest_access_time_;
760
[email protected]63ee33bd2012-03-15 09:29:58761 std::vector<std::string> cookieable_schemes_;
762
[email protected]63ee33bd2012-03-15 09:29:58763 base::Time last_statistic_record_time_;
764
[email protected]63ee33bd2012-03-15 09:29:58765 bool persist_session_cookies_;
766
Kirubel Akliluc9b4e412022-01-12 01:00:01767 bool first_party_sets_enabled_;
768
mmenkebe0910d2016-03-01 19:09:09769 base::ThreadChecker thread_checker_;
770
Jeremy Romand54000b22019-07-08 18:40:16771 base::WeakPtrFactory<CookieMonster> weak_ptr_factory_{this};
[email protected]63ee33bd2012-03-15 09:29:58772};
773
[email protected]63ee33bd2012-03-15 09:29:58774typedef base::RefCountedThreadSafe<CookieMonster::PersistentCookieStore>
775 RefcountedPersistentCookieStore;
776
[email protected]c1b6e102013-04-10 20:54:49777class NET_EXPORT CookieMonster::PersistentCookieStore
[email protected]63ee33bd2012-03-15 09:29:58778 : public RefcountedPersistentCookieStore {
779 public:
Maks Orlovich108cb4c2019-03-26 20:24:57780 typedef base::OnceCallback<void(
781 std::vector<std::unique_ptr<CanonicalCookie>>)>
[email protected]5b9bc352012-07-18 13:13:34782 LoadedCallback;
[email protected]63ee33bd2012-03-15 09:29:58783
Peter Boström407869b2021-10-07 04:42:48784 PersistentCookieStore(const PersistentCookieStore&) = delete;
785 PersistentCookieStore& operator=(const PersistentCookieStore&) = delete;
786
[email protected]63ee33bd2012-03-15 09:29:58787 // Initializes the store and retrieves the existing cookies. This will be
788 // called only once at startup. The callback will return all the cookies
789 // that are not yet returned to CookieMonster by previous priority loads.
mmenkebe0910d2016-03-01 19:09:09790 //
791 // |loaded_callback| may not be NULL.
Helen Li92a29f102018-08-15 23:02:26792 // |net_log| is a NetLogWithSource that may be copied if the persistent
793 // store wishes to log NetLog events.
Maks Orlovich108cb4c2019-03-26 20:24:57794 virtual void Load(LoadedCallback loaded_callback,
Helen Li92a29f102018-08-15 23:02:26795 const NetLogWithSource& net_log) = 0;
[email protected]63ee33bd2012-03-15 09:29:58796
797 // Does a priority load of all cookies for the domain key (eTLD+1). The
798 // callback will return all the cookies that are not yet returned by previous
799 // loads, which includes cookies for the requested domain key if they are not
800 // already returned, plus all cookies that are chain-loaded and not yet
801 // returned to CookieMonster.
mmenkebe0910d2016-03-01 19:09:09802 //
803 // |loaded_callback| may not be NULL.
[email protected]63ee33bd2012-03-15 09:29:58804 virtual void LoadCookiesForKey(const std::string& key,
Maks Orlovich108cb4c2019-03-26 20:24:57805 LoadedCallback loaded_callback) = 0;
[email protected]63ee33bd2012-03-15 09:29:58806
807 virtual void AddCookie(const CanonicalCookie& cc) = 0;
808 virtual void UpdateCookieAccessTime(const CanonicalCookie& cc) = 0;
809 virtual void DeleteCookie(const CanonicalCookie& cc) = 0;
810
[email protected]bf510ed2012-06-05 08:31:43811 // Instructs the store to not discard session only cookies on shutdown.
812 virtual void SetForceKeepSessionState() = 0;
[email protected]63ee33bd2012-03-15 09:29:58813
Nick Harper14e23332017-07-28 00:27:23814 // Sets a callback that will be run before the store flushes. If |callback|
815 // performs any async operations, the store will not wait for those to finish
816 // before flushing.
Lily Chen9934f7e2019-03-13 19:16:55817 virtual void SetBeforeCommitCallback(base::RepeatingClosure callback) = 0;
Nick Harper14e23332017-07-28 00:27:23818
mmenkebe0910d2016-03-01 19:09:09819 // Flushes the store and posts |callback| when complete. |callback| may be
820 // NULL.
rdsmith7ac81712017-06-22 17:09:54821 virtual void Flush(base::OnceClosure callback) = 0;
[email protected]63ee33bd2012-03-15 09:29:58822
823 protected:
Christian Dullweberff11c452021-05-12 17:04:45824 PersistentCookieStore() = default;
825 virtual ~PersistentCookieStore() = default;
[email protected]63ee33bd2012-03-15 09:29:58826
827 private:
[email protected]a9813302012-04-28 09:29:28828 friend class base::RefCountedThreadSafe<PersistentCookieStore>;
[email protected]63ee33bd2012-03-15 09:29:58829};
830
[email protected]63ee33bd2012-03-15 09:29:58831} // namespace net
832
833#endif // NET_COOKIES_COOKIE_MONSTER_H_