blob: fa5e56d60b093cdf4ec5a116cb91fb0eee8b1b7c [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"
Alex Kalugin379f47d82022-03-08 13:11:5937#include "third_party/abseil-cpp/absl/types/optional.h"
ellyjones399e35a22014-10-27 11:09:5638#include "url/gurl.h"
[email protected]63ee33bd2012-03-15 09:29:5839
[email protected]63ee33bd2012-03-15 09:29:5840namespace net {
41
Victor Costan14f47c12018-03-01 08:02:2442class CookieChangeDispatcher;
[email protected]63ee33bd2012-03-15 09:29:5843
44// The cookie monster is the system for storing and retrieving cookies. It has
45// an in-memory list of all cookies, and synchronizes non-session cookies to an
46// optional permanent storage that implements the PersistentCookieStore
47// interface.
48//
mmenke96f3bab2016-01-22 17:34:0249// Tasks may be deferred if all affected cookies are not yet loaded from the
50// backing store. Otherwise, callbacks may be invoked immediately.
[email protected]63ee33bd2012-03-15 09:29:5851//
52// A cookie task is either pending loading of the entire cookie store, or
Maks Orlovich323efaf2018-03-06 02:56:3953// loading of cookies for a specific domain key (GetKey(), roughly eTLD+1). In
54// the former case, the cookie callback will be queued in tasks_pending_ while
55// PersistentCookieStore chain loads the cookie store on DB thread. In the
56// latter case, the cookie callback will be queued in tasks_pending_for_key_
57// while PermanentCookieStore loads cookies for the specified domain key on DB
58// thread.
[email protected]63ee33bd2012-03-15 09:29:5859class NET_EXPORT CookieMonster : public CookieStore {
60 public:
[email protected]63ee33bd2012-03-15 09:29:5861 class PersistentCookieStore;
62
63 // Terminology:
64 // * The 'top level domain' (TLD) of an internet domain name is
65 // the terminal "." free substring (e.g. "com" for google.com
66 // or world.std.com).
67 // * The 'effective top level domain' (eTLD) is the longest
68 // "." initiated terminal substring of an internet domain name
69 // that is controlled by a general domain registrar.
70 // (e.g. "co.uk" for news.bbc.co.uk).
71 // * The 'effective top level domain plus one' (eTLD+1) is the
72 // shortest "." delimited terminal substring of an internet
73 // domain name that is not controlled by a general domain
74 // registrar (e.g. "bbc.co.uk" for news.bbc.co.uk, or
75 // "google.com" for news.google.com). The general assumption
76 // is that all hosts and domains under an eTLD+1 share some
77 // administrative control.
78
79 // CookieMap is the central data structure of the CookieMonster. It
80 // is a map whose values are pointers to CanonicalCookie data
81 // structures (the data structures are owned by the CookieMonster
82 // and must be destroyed when removed from the map). The key is based on the
83 // effective domain of the cookies. If the domain of the cookie has an
84 // eTLD+1, that is the key for the map. If the domain of the cookie does not
85 // have an eTLD+1, the key of the map is the host the cookie applies to (it is
86 // not legal to have domain cookies without an eTLD+1). This rule
87 // excludes cookies for, e.g, ".com", ".co.uk", or ".internalnetwork".
88 // This behavior is the same as the behavior in Firefox v 3.6.10.
Dylan Cutler0b9a4e962021-09-13 17:34:2589 // CookieMap does not store cookies that were set with the Partitioned
90 // attribute, those are stored in PartitionedCookieMap.
[email protected]63ee33bd2012-03-15 09:29:5891
92 // NOTE(deanm):
93 // I benchmarked hash_multimap vs multimap. We're going to be query-heavy
94 // so it would seem like hashing would help. However they were very
95 // close, with multimap being a tiny bit faster. I think this is because
96 // our map is at max around 1000 entries, and the additional complexity
97 // for the hashing might not overcome the O(log(1000)) for querying
98 // a multimap. Also, multimap is standard, another reason to use it.
99 // TODO(rdsmith): This benchmark should be re-done now that we're allowing
avie7cd11a2016-10-11 02:00:35100 // substantially more entries in the map.
101 using CookieMap =
102 std::multimap<std::string, std::unique_ptr<CanonicalCookie>>;
103 using CookieMapItPair = std::pair<CookieMap::iterator, CookieMap::iterator>;
104 using CookieItVector = std::vector<CookieMap::iterator>;
[email protected]8ad5d462013-05-02 08:45:26105
Dylan Cutler0b9a4e962021-09-13 17:34:25106 // PartitionedCookieMap only stores cookies that were set with the Partitioned
107 // attribute. The map is double-keyed on cookie's partition key and
108 // the cookie's effective domain of the cookie (the key of CookieMap).
109 // We store partitioned cookies in a separate map so that the queries for a
110 // request's unpartitioned and partitioned cookies will both be more
111 // efficient (since querying two smaller maps is more efficient that querying
112 // one larger map twice).
113 using PartitionedCookieMap =
114 std::map<CookiePartitionKey, std::unique_ptr<CookieMap>>;
115 using PartitionedCookieMapIterators =
116 std::pair<PartitionedCookieMap::iterator, CookieMap::iterator>;
117
[email protected]8ad5d462013-05-02 08:45:26118 // Cookie garbage collection thresholds. Based off of the Mozilla defaults.
119 // When the number of cookies gets to k{Domain,}MaxCookies
120 // purge down to k{Domain,}MaxCookies - k{Domain,}PurgeCookies.
121 // It might seem scary to have a high purge value, but really it's not.
122 // You just make sure that you increase the max to cover the increase
123 // in purge, and we would have been purging the same number of cookies.
124 // We're just going through the garbage collection process less often.
125 // Note that the DOMAIN values are per eTLD+1; see comment for the
126 // CookieMap typedef. So, e.g., the maximum number of cookies allowed for
127 // google.com and all of its subdomains will be 150-180.
128 //
129 // Any cookies accessed more recently than kSafeFromGlobalPurgeDays will not
130 // be evicted by global garbage collection, even if we have more than
131 // kMaxCookies. This does not affect domain garbage collection.
132 static const size_t kDomainMaxCookies;
133 static const size_t kDomainPurgeCookies;
134 static const size_t kMaxCookies;
135 static const size_t kPurgeCookies;
136
Lily Chen229623d72020-06-01 17:20:14137 // Max number of keys to store for domains that have been purged.
138 static const size_t kMaxDomainPurgedKeys;
139
Dylan Cutler0b9a4e962021-09-13 17:34:25140 // Partitioned cookie garbage collection thresholds.
141 static const size_t kPerPartitionDomainMaxCookies;
142 // TODO(crbug.com/1225444): Add global limit to number of partitioned cookies.
143
[email protected]8ad5d462013-05-02 08:45:26144 // Quota for cookies with {low, medium, high} priorities within a domain.
mkwst87734352016-03-03 17:36:23145 static const size_t kDomainCookiesQuotaLow;
146 static const size_t kDomainCookiesQuotaMedium;
147 static const size_t kDomainCookiesQuotaHigh;
[email protected]63ee33bd2012-03-15 09:29:58148
Matt Menke477ab632019-06-27 23:12:17149 // The number of days since last access that cookies will not be subject
150 // to global garbage collection.
151 static const int kSafeFromGlobalPurgeDays;
152
[email protected]63ee33bd2012-03-15 09:29:58153 // The store passed in should not have had Init() called on it yet. This
154 // class will take care of initializing it. The backing store is NOT owned by
155 // this class, but it must remain valid for the duration of the cookie
156 // monster's existence. If |store| is NULL, then no backing store will be
Nick Harper57142b1c2019-03-14 21:03:59157 // updated. |net_log| must outlive the CookieMonster and can be null.
Pritam8354cf702018-03-10 08:55:41158 CookieMonster(scoped_refptr<PersistentCookieStore> store,
Kirubel Akliluc9b4e412022-01-12 01:00:01159 NetLog* net_log,
160 bool first_party_sets_enabled);
nharper2b0ad9a2017-05-22 18:33:45161
[email protected]63ee33bd2012-03-15 09:29:58162 // Only used during unit testing.
Helen Lifb313a92018-08-14 15:46:44163 // |net_log| must outlive the CookieMonster.
Pritam8354cf702018-03-10 08:55:41164 CookieMonster(scoped_refptr<PersistentCookieStore> store,
Helen Lifb313a92018-08-14 15:46:44165 base::TimeDelta last_access_threshold,
Kirubel Akliluc9b4e412022-01-12 01:00:01166 NetLog* net_log,
167 bool first_party_sets_enabled);
[email protected]63ee33bd2012-03-15 09:29:58168
Peter Boström293b1342021-09-22 17:31:43169 CookieMonster(const CookieMonster&) = delete;
170 CookieMonster& operator=(const CookieMonster&) = delete;
171
mmenke606c59c2016-03-07 18:20:55172 ~CookieMonster() override;
173
rdsmith0e84cea2017-07-13 03:09:53174 // Writes all the cookies in |list| into the store, replacing all cookies
175 // currently present in store.
rdsmith2709eee2017-06-20 22:43:27176 // This method does not flush the backend.
177 // TODO(rdsmith, mmenke): Do not use this function; it is deprecated
178 // and should be removed.
179 // See https://codereview.chromium.org/2882063002/#msg64.
rdsmith7ac81712017-06-22 17:09:54180 void SetAllCookiesAsync(const CookieList& list, SetCookiesCallback callback);
drogerd5d1278c2015-03-17 19:21:51181
[email protected]63ee33bd2012-03-15 09:29:58182 // CookieStore implementation.
Juba Borgohain9fa24142022-02-04 18:25:42183 void SetCanonicalCookieAsync(
184 std::unique_ptr<CanonicalCookie> cookie,
185 const GURL& source_url,
186 const CookieOptions& options,
187 SetCookiesCallback callback,
Alex Kalugin379f47d82022-03-08 13:11:59188 absl::optional<CookieAccessResult> cookie_access_result =
189 absl::nullopt) override;
Dylan Cutlercd2d8932021-10-05 19:03:43190 void GetCookieListWithOptionsAsync(const GURL& url,
191 const CookieOptions& options,
Aykut Bulut244341e2021-12-09 15:57:25192 const CookiePartitionKeyCollection& s,
Dylan Cutlercd2d8932021-10-05 19:03:43193 GetCookieListCallback callback) override;
Lily Chenf068a762019-08-21 21:10:50194 void GetAllCookiesAsync(GetAllCookiesCallback callback) override;
Lily Chene2e9ae012019-10-09 20:02:54195 void GetAllCookiesWithAccessSemanticsAsync(
196 GetAllCookiesWithAccessSemanticsCallback callback) override;
mmenke24379d52016-02-05 23:50:17197 void DeleteCanonicalCookieAsync(const CanonicalCookie& cookie,
rdsmith7ac81712017-06-22 17:09:54198 DeleteCallback callback) override;
Chris Mumfordd8ed9f82018-05-01 15:43:13199 void DeleteAllCreatedInTimeRangeAsync(
200 const CookieDeletionInfo::TimeRange& creation_range,
201 DeleteCallback callback) override;
Chris Mumford800caa62018-04-20 19:34:44202 void DeleteAllMatchingInfoAsync(CookieDeletionInfo delete_info,
203 DeleteCallback callback) override;
Christian Dullweberff11c452021-05-12 17:04:45204 void DeleteSessionCookiesAsync(DeleteCallback callback) override;
205 void DeleteMatchingCookiesAsync(DeletePredicate predicate,
206 DeleteCallback callback) override;
rdsmith7ac81712017-06-22 17:09:54207 void FlushStore(base::OnceClosure callback) override;
mmenkeded79da2016-02-06 08:28:51208 void SetForceKeepSessionState() override;
Victor Costan14f47c12018-03-01 08:02:24209 CookieChangeDispatcher& GetChangeDispatcher() override;
Nate Fischerc6fb6cf2019-03-27 00:39:49210 void SetCookieableSchemes(const std::vector<std::string>& schemes,
211 SetCookieableSchemesCallback callback) override;
mmenke74bcbd52016-01-21 17:17:56212
[email protected]63ee33bd2012-03-15 09:29:58213 // Enables writing session cookies into the cookie database. If this this
214 // method is called, it must be called before first use of the instance
215 // (i.e. as part of the instance initialization process).
216 void SetPersistSessionCookies(bool persist_session_cookies);
217
[email protected]63ee33bd2012-03-15 09:29:58218 // The default list of schemes the cookie monster can handle.
[email protected]5edff3c52014-06-23 20:27:48219 static const char* const kDefaultCookieableSchemes[];
[email protected]63ee33bd2012-03-15 09:29:58220 static const int kDefaultCookieableSchemesCount;
221
Maks Orlovich323efaf2018-03-06 02:56:39222 // Find a key based on the given domain, which will be used to find all
223 // cookies potentially relevant to it. This is used for lookup in cookies_ as
224 // well as for PersistentCookieStore::LoadCookiesForKey. See comment on keys
225 // before the CookieMap typedef.
226 static std::string GetKey(base::StringPiece domain);
227
cfredric59f8a8452021-06-08 15:27:11228 // Exposes the comparison function used when sorting cookies.
229 static bool CookieSorter(const CanonicalCookie* cc1,
230 const CanonicalCookie* cc2);
231
Lily Chen229623d72020-06-01 17:20:14232 // Triggers immediate recording of stats that are typically reported
233 // periodically.
234 bool DoRecordPeriodicStatsForTesting() { return DoRecordPeriodicStats(); }
235
Dylan Cutler03d2c76c2022-02-18 02:23:15236 // Will convert a site's partitioned cookies into unpartitioned cookies. This
237 // may result in multiple cookies which have the same (partition_key, name,
238 // host_key, path), which violates the database's unique constraint. The
239 // algorithm we use to coalesce the cookies into a single unpartitioned cookie
240 // is the following:
241 //
242 // 1. If one of the cookies has no partition key (i.e. it is unpartitioned)
243 // choose this cookie.
244 //
245 // 2. Choose the partitioned cookie with the most recent last_access_time.
246 //
247 // TODO(crbug.com/1296161): Delete this when the partitioned cookies Origin
248 // Trial ends.
249 void ConvertPartitionedCookiesToUnpartitioned(const GURL& url) override;
250
[email protected]63ee33bd2012-03-15 09:29:58251 private:
avie7cd11a2016-10-11 02:00:35252 // For garbage collection constants.
[email protected]63ee33bd2012-03-15 09:29:58253 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestHostGarbageCollection);
mmenkef4721d992017-06-07 17:13:59254 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest,
255 GarbageCollectWithSecureCookiesOnly);
[email protected]63ee33bd2012-03-15 09:29:58256 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestGCTimes);
257
258 // For validation of key values.
259 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestDomainTree);
260 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestImport);
261 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, GetKey);
262 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestGetKey);
263
264 // For FindCookiesForKey.
265 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, ShortLivedSessionCookies);
266
estark7feb65c2b2015-08-21 23:38:20267 // For CookieSource histogram enum.
268 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, CookieSourceHistogram);
269
jww31e32632015-12-16 23:38:34270 // For kSafeFromGlobalPurgeDays in CookieStore.
jwwa26e439d2017-01-27 18:17:27271 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, EvictSecureCookies);
jww82d99c12015-11-25 18:39:53272
jww31e32632015-12-16 23:38:34273 // For CookieDeleteEquivalent histogram enum.
274 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest,
275 CookieDeleteEquivalentHistogramTest);
jww31e32632015-12-16 23:38:34276
Steven Bingler0420a3752020-11-20 21:40:48277 // For CookieSentToSamePort enum.
278 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest,
279 CookiePortReadDiffersFromSetHistogram);
280 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, IsCookieSentToSamePortThatSetIt);
281
[email protected]63ee33bd2012-03-15 09:29:58282 // Internal reasons for deletion, used to populate informative histograms
283 // and to provide a public cause for onCookieChange notifications.
284 //
285 // If you add or remove causes from this list, please be sure to also update
Victor Costan14f47c12018-03-01 08:02:24286 // the CookieChangeCause mapping inside ChangeCauseMapping. New items (if
287 // necessary) should be added at the end of the list, just before
Nick Harper7a6683a2018-01-30 20:42:52288 // DELETE_COOKIE_LAST_ENTRY.
[email protected]63ee33bd2012-03-15 09:29:58289 enum DeletionCause {
290 DELETE_COOKIE_EXPLICIT = 0,
mkwstaa07ee82016-03-11 15:32:14291 DELETE_COOKIE_OVERWRITE = 1,
292 DELETE_COOKIE_EXPIRED = 2,
293 DELETE_COOKIE_EVICTED = 3,
294 DELETE_COOKIE_DUPLICATE_IN_BACKING_STORE = 4,
295 DELETE_COOKIE_DONT_RECORD = 5, // For final cleanup after flush to store.
[email protected]63ee33bd2012-03-15 09:29:58296
mkwstaa07ee82016-03-11 15:32:14297 // Cookies evicted during domain-level garbage collection.
298 DELETE_COOKIE_EVICTED_DOMAIN = 6,
[email protected]63ee33bd2012-03-15 09:29:58299
Dylan Cutler0b9a4e962021-09-13 17:34:25300 // Cookies evicted during global garbage collection, which takes place after
mkwstaa07ee82016-03-11 15:32:14301 // domain-level garbage collection fails to bring the cookie store under
302 // the overall quota.
303 DELETE_COOKIE_EVICTED_GLOBAL = 7,
304
305 // #8 was DELETE_COOKIE_EVICTED_DOMAIN_PRE_SAFE
306 // #9 was DELETE_COOKIE_EVICTED_DOMAIN_POST_SAFE
[email protected]63ee33bd2012-03-15 09:29:58307
308 // A common idiom is to remove a cookie by overwriting it with an
309 // already-expired expiration date. This captures that case.
mkwstaa07ee82016-03-11 15:32:14310 DELETE_COOKIE_EXPIRED_OVERWRITE = 10,
[email protected]63ee33bd2012-03-15 09:29:58311
[email protected]6210ce52013-09-20 03:33:14312 // Cookies are not allowed to contain control characters in the name or
313 // value. However, we used to allow them, so we are now evicting any such
314 // cookies as we load them. See http://crbug.com/238041.
mkwstaa07ee82016-03-11 15:32:14315 DELETE_COOKIE_CONTROL_CHAR = 11,
[email protected]6210ce52013-09-20 03:33:14316
jww82d99c12015-11-25 18:39:53317 // When strict secure cookies is enabled, non-secure cookies are evicted
318 // right after expired cookies.
mkwstaa07ee82016-03-11 15:32:14319 DELETE_COOKIE_NON_SECURE = 12,
jww82d99c12015-11-25 18:39:53320
Dylan Cutler0b9a4e962021-09-13 17:34:25321 // Partitioned cookies evicted during per-partition domain-level garbage
322 // collection.
323 DELETE_COOKIE_EVICTED_PER_PARTITION_DOMAIN = 13,
324
325 DELETE_COOKIE_LAST_ENTRY = 14,
[email protected]63ee33bd2012-03-15 09:29:58326 };
327
mkwstc1aa4cc2015-04-03 19:57:45328 // This enum is used to generate a histogramed bitmask measureing the types
329 // of stored cookies. Please do not reorder the list when adding new entries.
330 // New items MUST be added at the end of the list, just before
331 // COOKIE_TYPE_LAST_ENTRY;
Lily Chenda465cca2021-03-08 23:47:17332 // There will be 2^COOKIE_TYPE_LAST_ENTRY buckets in the linear histogram.
mkwstc1aa4cc2015-04-03 19:57:45333 enum CookieType {
mkwst46549412016-02-01 10:05:37334 COOKIE_TYPE_SAME_SITE = 0,
mkwstc1aa4cc2015-04-03 19:57:45335 COOKIE_TYPE_HTTPONLY,
336 COOKIE_TYPE_SECURE,
337 COOKIE_TYPE_LAST_ENTRY
338 };
339
estark7feb65c2b2015-08-21 23:38:20340 // Used to populate a histogram containing information about the
341 // sources of Secure and non-Secure cookies: that is, whether such
342 // cookies are set by origins with cryptographic or non-cryptographic
343 // schemes. Please do not reorder the list when adding new
Lily Chenda465cca2021-03-08 23:47:17344 // entries. New items MUST be added at the end of the list, and kMaxValue
345 // should be updated to the last value.
estark7feb65c2b2015-08-21 23:38:20346 //
Lei Zhangd9388332022-08-02 23:30:46347 // CookieSource::k(Non)SecureCookie(Non)CryptographicScheme means
estark7feb65c2b2015-08-21 23:38:20348 // that a cookie was set or overwritten from a URL with the given type
349 // of scheme. This enum should not be used when cookies are *cleared*,
350 // because its purpose is to understand if Chrome can deprecate the
351 // ability of HTTP urls to set/overwrite Secure cookies.
Lei Zhangd9388332022-08-02 23:30:46352 enum class CookieSource : uint8_t {
353 kSecureCookieCryptographicScheme = 0,
354 kSecureCookieNoncryptographicScheme,
355 kNonsecureCookieCryptographicScheme,
356 kNonsecureCookieNoncryptographicScheme,
357 kMaxValue = kNonsecureCookieNoncryptographicScheme
estark7feb65c2b2015-08-21 23:38:20358 };
359
Steven Bingler0420a3752020-11-20 21:40:48360 // Enum for collecting metrics on how frequently a cookie is sent to the same
361 // port it was set by.
362 //
363 // kNoButDefault exists because we expect for cookies being sent between
364 // schemes to have a port mismatch and want to separate those out from other,
365 // more interesting, cases.
366 //
367 // Do not reorder or renumber. Used for metrics.
368 enum class CookieSentToSamePort {
369 kSourcePortUnspecified = 0, // Cookie's source port is unspecified, we
370 // can't know if this is the same port or not.
371 kInvalid = 1, // The source port was corrupted to be PORT_INVALID, we
372 // can't know if this is the same port or not.
373 kNo = 2, // Source port and destination port are different.
374 kNoButDefault =
375 3, // Source and destination ports are different but they're
376 // the defaults for their scheme. This can mean that an http
377 // cookie was sent to a https origin or vice-versa.
378 kYes = 4, // They're the same.
379 kMaxValue = kYes
380 };
381
[email protected]63ee33bd2012-03-15 09:29:58382 // Record statistics every kRecordStatisticsIntervalSeconds of uptime.
383 static const int kRecordStatisticsIntervalSeconds = 10 * 60;
384
rdsmitha6ce4442017-06-21 17:11:05385 // Sets a canonical cookie, deletes equivalents and performs garbage
Lily Chen96f29a132020-04-15 17:59:36386 // collection. |source_url| indicates what URL the cookie is being set
Maks Orlovichfdbc8be2019-03-18 18:34:52387 // from; secure cookies cannot be altered from insecure schemes, and some
388 // schemes may not be authorized.
389 //
390 // |options| indicates if this setting operation is allowed
391 // to affect http_only or same-site cookies.
Juba Borgohain9fa24142022-02-04 18:25:42392 //
393 // |cookie_access_result| is an optional input status, to allow for status
394 // chaining from callers. It helps callers provide the status of a
395 // canonical cookie that may have warnings associated with it.
396 void SetCanonicalCookie(
397 std::unique_ptr<CanonicalCookie> cookie,
398 const GURL& source_url,
399 const CookieOptions& options,
400 SetCookiesCallback callback,
Alex Kalugin379f47d82022-03-08 13:11:59401 absl::optional<CookieAccessResult> cookie_access_result = absl::nullopt);
rdsmitha6ce4442017-06-21 17:11:05402
Lily Chenf068a762019-08-21 21:10:50403 void GetAllCookies(GetAllCookiesCallback callback);
[email protected]63ee33bd2012-03-15 09:29:58404
Lily Chene2e9ae012019-10-09 20:02:54405 void AttachAccessSemanticsListForCookieList(
406 GetAllCookiesWithAccessSemanticsCallback callback,
407 const CookieList& cookie_list);
408
Dylan Cutler0b9a4e962021-09-13 17:34:25409 void GetCookieListWithOptions(
410 const GURL& url,
411 const CookieOptions& options,
Aykut Bulut244341e2021-12-09 15:57:25412 const CookiePartitionKeyCollection& cookie_partition_key_collection,
Dylan Cutler0b9a4e962021-09-13 17:34:25413 GetCookieListCallback callback);
[email protected]63ee33bd2012-03-15 09:29:58414
Chris Mumfordd8ed9f82018-05-01 15:43:13415 void DeleteAllCreatedInTimeRange(
416 const CookieDeletionInfo::TimeRange& creation_range,
417 DeleteCallback callback);
[email protected]63ee33bd2012-03-15 09:29:58418
Christian Dullweberff11c452021-05-12 17:04:45419 // Returns whether |cookie| matches |delete_info|.
420 bool MatchCookieDeletionInfo(const CookieDeletionInfo& delete_info,
421 const net::CanonicalCookie& cookie);
[email protected]63ee33bd2012-03-15 09:29:58422
rdsmithe5c701d2017-07-12 21:50:00423 void DeleteCanonicalCookie(const CanonicalCookie& cookie,
424 DeleteCallback callback);
mmenke24379d52016-02-05 23:50:17425
Christian Dullweberff11c452021-05-12 17:04:45426 void DeleteMatchingCookies(DeletePredicate predicate,
427 DeletionCause cause,
428 DeleteCallback callback);
[email protected]264807b2012-04-25 14:49:37429
erikchen1dd72a72015-05-06 20:45:05430 // The first access to the cookie store initializes it. This method should be
431 // called before any access to the cookie store.
432 void MarkCookieStoreAsInitialized();
[email protected]63ee33bd2012-03-15 09:29:58433
erikchen1dd72a72015-05-06 20:45:05434 // Fetches all cookies if the backing store exists and they're not already
435 // being fetched.
erikchen1dd72a72015-05-06 20:45:05436 void FetchAllCookiesIfNecessary();
437
438 // Fetches all cookies from the backing store.
erikchen1dd72a72015-05-06 20:45:05439 void FetchAllCookies();
440
441 // Whether all cookies should be fetched as soon as any is requested.
442 bool ShouldFetchAllCookiesWhenFetchingAnyCookie();
[email protected]63ee33bd2012-03-15 09:29:58443
444 // Stores cookies loaded from the backing store and invokes any deferred
445 // calls. |beginning_time| should be the moment PersistentCookieStore::Load
446 // was invoked and is used for reporting histogram_time_blocked_on_load_.
447 // See PersistentCookieStore::Load for details on the contents of cookies.
448 void OnLoaded(base::TimeTicks beginning_time,
avie7cd11a2016-10-11 02:00:35449 std::vector<std::unique_ptr<CanonicalCookie>> cookies);
[email protected]63ee33bd2012-03-15 09:29:58450
451 // Stores cookies loaded from the backing store and invokes the deferred
452 // task(s) pending loading of cookies associated with the domain key
Maks Orlovich323efaf2018-03-06 02:56:39453 // (GetKey, roughly eTLD+1). Called when all cookies for the domain key have
454 // been loaded from DB. See PersistentCookieStore::Load for details on the
455 // contents of cookies.
mkwstbe84af312015-02-20 08:52:45456 void OnKeyLoaded(const std::string& key,
avie7cd11a2016-10-11 02:00:35457 std::vector<std::unique_ptr<CanonicalCookie>> cookies);
[email protected]63ee33bd2012-03-15 09:29:58458
459 // Stores the loaded cookies.
avie7cd11a2016-10-11 02:00:35460 void StoreLoadedCookies(
461 std::vector<std::unique_ptr<CanonicalCookie>> cookies);
[email protected]63ee33bd2012-03-15 09:29:58462
463 // Invokes deferred calls.
464 void InvokeQueue();
465
466 // Checks that |cookies_| matches our invariants, and tries to repair any
467 // inconsistencies. (In other words, it does not have duplicate cookies).
468 void EnsureCookiesMapIsValid();
469
470 // Checks for any duplicate cookies for CookieMap key |key| which lie between
471 // |begin| and |end|. If any are found, all but the most recent are deleted.
Dylan Cutler0b9a4e962021-09-13 17:34:25472 //
473 // If |cookie_partition_it| is not nullopt, then this function trims cookies
474 // from the CookieMap in |partitioned_cookies_| at |cookie_partition_it|
475 // instead of trimming cookies from |cookies_|.
476 void TrimDuplicateCookiesForKey(
477 const std::string& key,
478 CookieMap::iterator begin,
479 CookieMap::iterator end,
480 absl::optional<PartitionedCookieMap::iterator> cookie_partition_it);
[email protected]63ee33bd2012-03-15 09:29:58481
482 void SetDefaultCookieableSchemes();
483
cfredric09ba72fb2020-12-22 21:03:27484 std::vector<CanonicalCookie*> FindCookiesForRegistryControlledHost(
Dylan Cutler0b9a4e962021-09-13 17:34:25485 const GURL& url,
486 CookieMap* cookie_map = nullptr);
487
488 std::vector<CanonicalCookie*> FindPartitionedCookiesForRegistryControlledHost(
489 const CookiePartitionKey& cookie_partition_key,
cfredric09ba72fb2020-12-22 21:03:27490 const GURL& url);
[email protected]63ee33bd2012-03-15 09:29:58491
Lily Chenf068a762019-08-21 21:10:50492 void FilterCookiesWithOptions(const GURL url,
493 const CookieOptions options,
494 std::vector<CanonicalCookie*>* cookie_ptrs,
Ayu Ishiibc6fdb0a2020-06-08 22:59:19495 CookieAccessResultList* included_cookies,
496 CookieAccessResultList* excluded_cookies);
Lily Chenf068a762019-08-21 21:10:50497
Lily Chen4c5f8632019-10-30 18:11:51498 // Possibly delete an existing cookie equivalent to |cookie_being_set| (same
499 // path, domain, and name).
Mike Westc4a777b2017-10-06 14:04:20500 //
Maks Orlovichbd04d782020-11-17 21:23:34501 // |allowed_to_set_secure_cookie| indicates if the source may override
502 // existing secure cookies. If the source is not trustworthy, and there is an
503 // existing "equivalent" cookie that is Secure, that cookie will be preserved,
504 // under "Leave Secure Cookies Alone" (see
Lily Chen4c5f8632019-10-30 18:11:51505 // https://tools.ietf.org/html/draft-ietf-httpbis-cookie-alone-01).
506 // ("equivalent" here is in quotes because the equivalency check for the
507 // purposes of preserving existing Secure cookies is slightly more inclusive.)
508 //
509 // If |skip_httponly| is true, httponly cookies will not be deleted even if
510 // they are equivalent.
511 // |key| is the key to find the cookie in cookies_; see the comment before the
jwwa26e439d2017-01-27 18:17:27512 // CookieMap typedef for details.
Mike Westc4a777b2017-10-06 14:04:20513 //
Lily Chen4c5f8632019-10-30 18:11:51514 // If a cookie is deleted, and its value matches |cookie_being_set|'s value,
515 // then |creation_date_to_inherit| will be set to that cookie's creation date.
Mike Westc4a777b2017-10-06 14:04:20516 //
Lily Chenf53dfbcd2019-08-30 01:42:10517 // The cookie will not be deleted if |*status| is not "include" when calling
518 // the function. The function will update |*status| with exclusion reasons if
519 // a secure cookie was skipped or an httponly cookie was skipped.
520 //
Dylan Cutler0b9a4e962021-09-13 17:34:25521 // If |cookie_partition_it| is nullopt, it will search |cookies_| for
522 // duplicates of |cookie_being_set|. Otherwise, |cookie_partition_it|'s value
523 // is the iterator of the CookieMap in |partitioned_cookies_| we should search
524 // for duplicates.
525 //
[email protected]63ee33bd2012-03-15 09:29:58526 // NOTE: There should never be more than a single matching equivalent cookie.
Lily Chenf53dfbcd2019-08-30 01:42:10527 void MaybeDeleteEquivalentCookieAndUpdateStatus(
Aaron Tagliaboschi29764f52019-02-21 17:19:59528 const std::string& key,
Lily Chen4c5f8632019-10-30 18:11:51529 const CanonicalCookie& cookie_being_set,
Maks Orlovichbd04d782020-11-17 21:23:34530 bool allowed_to_set_secure_cookie,
Aaron Tagliaboschi29764f52019-02-21 17:19:59531 bool skip_httponly,
532 bool already_expired,
Lily Chenf53dfbcd2019-08-30 01:42:10533 base::Time* creation_date_to_inherit,
Dylan Cutler0b9a4e962021-09-13 17:34:25534 CookieInclusionStatus* status,
535 absl::optional<PartitionedCookieMap::iterator> cookie_partition_it);
[email protected]63ee33bd2012-03-15 09:29:58536
Lily Chend8d11db2021-02-25 19:50:52537 // Inserts `cc` into cookies_. Returns an iterator that points to the inserted
538 // cookie in `cookies_`. Guarantee: all iterators to `cookies_` remain valid.
539 // Dispatches the change to `change_dispatcher_` iff `dispatch_change` is
540 // true.
Ayu Ishii9f5e72dc2020-07-22 19:43:18541 CookieMap::iterator InternalInsertCookie(
542 const std::string& key,
543 std::unique_ptr<CanonicalCookie> cc,
544 bool sync_to_store,
Lily Chend8d11db2021-02-25 19:50:52545 const CookieAccessResult& access_result,
546 bool dispatch_change = true);
[email protected]63ee33bd2012-03-15 09:29:58547
Dylan Cutler0b9a4e962021-09-13 17:34:25548 // Returns true if the cookie should be (or is already) synced to the store.
549 // Used for cookies during insertion and deletion into the in-memory store.
550 bool ShouldUpdatePersistentStore(CanonicalCookie* cc);
551
552 void LogCookieTypeToUMA(CanonicalCookie* cc,
553 const CookieAccessResult& access_result);
554
555 // Inserts `cc` into partitioned_cookies_. Should only be used when
556 // cc->IsPartitioned() is true.
557 PartitionedCookieMapIterators InternalInsertPartitionedCookie(
558 std::string key,
559 std::unique_ptr<CanonicalCookie> cc,
560 bool sync_to_store,
561 const CookieAccessResult& access_result,
562 bool dispatch_change = true);
563
rdsmith2709eee2017-06-20 22:43:27564 // Sets all cookies from |list| after deleting any equivalent cookie.
565 // For data gathering purposes, this routine is treated as if it is
566 // restoring saved cookies; some statistics are not gathered in this case.
rdsmithe5c701d2017-07-12 21:50:00567 void SetAllCookies(CookieList list, SetCookiesCallback callback);
drogerd5d1278c2015-03-17 19:21:51568
[email protected]63ee33bd2012-03-15 09:29:58569 void InternalUpdateCookieAccessTime(CanonicalCookie* cc,
570 const base::Time& current_time);
571
572 // |deletion_cause| argument is used for collecting statistics and choosing
Victor Costan14f47c12018-03-01 08:02:24573 // the correct CookieChangeCause for OnCookieChange notifications. Guarantee:
574 // All iterators to cookies_, except for the deleted entry, remain valid.
mkwstbe84af312015-02-20 08:52:45575 void InternalDeleteCookie(CookieMap::iterator it,
576 bool sync_to_store,
[email protected]63ee33bd2012-03-15 09:29:58577 DeletionCause deletion_cause);
578
Dylan Cutler0b9a4e962021-09-13 17:34:25579 // Deletes a Partitioned cookie. Returns true if the deletion operation
580 // resulted in the CookieMap the cookie was stored in was deleted.
581 //
582 // If the CookieMap which contains the deleted cookie only has one entry, then
583 // this function will also delete the CookieMap from PartitionedCookieMap.
584 // This may invalidate the |cookie_partition_it| argument.
585 void InternalDeletePartitionedCookie(
586 PartitionedCookieMap::iterator partition_it,
587 CookieMap::iterator cookie_it,
588 bool sync_to_store,
589 DeletionCause deletion_cause);
590
[email protected]63ee33bd2012-03-15 09:29:58591 // If the number of cookies for CookieMap key |key|, or globally, are
592 // over the preset maximums above, garbage collect, first for the host and
593 // then globally. See comments above garbage collection threshold
Dylan Cutler0b9a4e962021-09-13 17:34:25594 // constants for details. Also removes expired cookies.
[email protected]63ee33bd2012-03-15 09:29:58595 //
596 // Returns the number of cookies deleted (useful for debugging).
jwwa26e439d2017-01-27 18:17:27597 size_t GarbageCollect(const base::Time& current, const std::string& key);
[email protected]63ee33bd2012-03-15 09:29:58598
Dylan Cutler0b9a4e962021-09-13 17:34:25599 // Run garbage collection for PartitionedCookieMap keys |cookie_partition_key|
600 // and |key|.
601 //
602 // Partitioned cookies are subject to different limits than unpartitioned
603 // cookies in order to prevent leaking entropy about user behavior across
604 // cookie partitions.
605 size_t GarbageCollectPartitionedCookies(
606 const base::Time& current,
607 const CookiePartitionKey& cookie_partition_key,
608 const std::string& key);
609
mkwste079ac412016-03-11 09:04:06610 // Helper for GarbageCollect(). Deletes up to |purge_goal| cookies with a
611 // priority less than or equal to |priority| from |cookies|, while ensuring
612 // that at least the |to_protect| most-recent cookies are retained.
jwwc00ac712016-05-05 22:21:44613 // |protected_secure_cookies| specifies whether or not secure cookies should
614 // be protected from deletion.
mkwste079ac412016-03-11 09:04:06615 //
616 // |cookies| must be sorted from least-recent to most-recent.
617 //
mkwste079ac412016-03-11 09:04:06618 // Returns the number of cookies deleted.
619 size_t PurgeLeastRecentMatches(CookieItVector* cookies,
620 CookiePriority priority,
621 size_t to_protect,
jwwc00ac712016-05-05 22:21:44622 size_t purge_goal,
623 bool protect_secure_cookies);
mkwste079ac412016-03-11 09:04:06624
jww82d99c12015-11-25 18:39:53625 // Helper for GarbageCollect(); can be called directly as well. Deletes all
626 // expired cookies in |itpair|. If |cookie_its| is non-NULL, all the
627 // non-expired cookies from |itpair| are appended to |cookie_its|.
[email protected]63ee33bd2012-03-15 09:29:58628 //
629 // Returns the number of cookies deleted.
jww82d99c12015-11-25 18:39:53630 size_t GarbageCollectExpired(const base::Time& current,
631 const CookieMapItPair& itpair,
632 CookieItVector* cookie_its);
633
Dylan Cutler0b9a4e962021-09-13 17:34:25634 // Deletes all expired cookies in the double-keyed PartitionedCookie map in
635 // the CookieMap at |cookie_partition_it|. It deletes all cookies in that
636 // CookieMap in |itpair|. If |cookie_its| is non-NULL, all non-expired cookies
637 // from |itpair| are appended to |cookie_its|.
638 //
639 // Returns the number of cookies deleted.
640 size_t GarbageCollectExpiredPartitionedCookies(
641 const base::Time& current,
642 const PartitionedCookieMap::iterator& cookie_partition_it,
643 const CookieMapItPair& itpair,
644 CookieItVector* cookie_its);
645
646 // Helper function to garbage collect all expired cookies in
647 // PartitionedCookieMap.
648 void GarbageCollectAllExpiredPartitionedCookies(const base::Time& current);
649
[email protected]8ad5d462013-05-02 08:45:26650 // Helper for GarbageCollect(). Deletes all cookies in the range specified by
651 // [|it_begin|, |it_end|). Returns the number of cookies deleted.
jww82d99c12015-11-25 18:39:53652 size_t GarbageCollectDeleteRange(const base::Time& current,
653 DeletionCause cause,
654 CookieItVector::iterator cookie_its_begin,
655 CookieItVector::iterator cookie_its_end);
656
657 // Helper for GarbageCollect(). Deletes cookies in |cookie_its| from least to
658 // most recently used, but only before |safe_date|. Also will stop deleting
659 // when the number of remaining cookies hits |purge_goal|.
mmenkef4721d992017-06-07 17:13:59660 //
661 // Sets |earliest_time| to be the earliest last access time of a cookie that
662 // was not deleted, or base::Time() if no such cookie exists.
jww82d99c12015-11-25 18:39:53663 size_t GarbageCollectLeastRecentlyAccessed(const base::Time& current,
664 const base::Time& safe_date,
665 size_t purge_goal,
mmenkef4721d992017-06-07 17:13:59666 CookieItVector cookie_its,
667 base::Time* earliest_time);
[email protected]63ee33bd2012-03-15 09:29:58668
[email protected]63ee33bd2012-03-15 09:29:58669 bool HasCookieableScheme(const GURL& url);
670
Lily Chenb0eedc22020-10-26 16:34:42671 // Get the cookie's access semantics (LEGACY or NONLEGACY), by checking for a
Lily Chenb0ca3f72019-12-05 18:06:29672 // value from the cookie access delegate, if it is non-null. Otherwise returns
673 // UNKNOWN.
Lily Chen70f997f2019-10-07 22:01:37674 CookieAccessSemantics GetAccessSemanticsForCookie(
675 const CanonicalCookie& cookie) const;
676
[email protected]63ee33bd2012-03-15 09:29:58677 // Statistics support
678
679 // This function should be called repeatedly, and will record
680 // statistics if a sufficient time period has passed.
681 void RecordPeriodicStats(const base::Time& current_time);
682
Lily Chen229623d72020-06-01 17:20:14683 // Records the aforementioned stats if we have already finished loading all
684 // cookies. Returns whether stats were recorded.
685 bool DoRecordPeriodicStats();
686
cfredric326a0bc2022-01-12 18:51:30687 // Records periodic stats related to First-Party Sets usage. Note that since
688 // First-Party Sets presents a potentially asynchronous interface, these stats
689 // may be collected asynchronously w.r.t. the rest of the stats collected by
690 // `RecordPeriodicStats`.
cfredric326a0bc2022-01-12 18:51:30691 void RecordPeriodicFirstPartySetsStats(
Chris Fredricksonc2efa96f2022-08-04 20:40:44692 base::flat_map<SchemefulSite, FirstPartySetEntry> sets) const;
cfredric326a0bc2022-01-12 18:51:30693
Maks Orlovich323efaf2018-03-06 02:56:39694 // Defers the callback until the full coookie database has been loaded. If
695 // it's already been loaded, runs the callback synchronously.
rdsmithe5c701d2017-07-12 21:50:00696 void DoCookieCallback(base::OnceClosure callback);
[email protected]63ee33bd2012-03-15 09:29:58697
Maks Orlovich323efaf2018-03-06 02:56:39698 // Defers the callback until the cookies relevant to given URL have been
699 // loaded. If they've already been loaded, runs the callback synchronously.
rdsmithe5c701d2017-07-12 21:50:00700 void DoCookieCallbackForURL(base::OnceClosure callback, const GURL& url);
[email protected]63ee33bd2012-03-15 09:29:58701
Maks Orlovich323efaf2018-03-06 02:56:39702 // Defers the callback until the cookies relevant to given host or domain
703 // have been loaded. If they've already been loaded, runs the callback
704 // synchronously.
705 void DoCookieCallbackForHostOrDomain(base::OnceClosure callback,
706 base::StringPiece host_or_domain);
707
Steven Bingler0420a3752020-11-20 21:40:48708 // Checks to see if a cookie is being sent to the same port it was set by. For
709 // metrics.
710 //
711 // This is in CookieMonster because only CookieMonster uses it. It's otherwise
712 // a standalone utility function.
713 static CookieSentToSamePort IsCookieSentToSamePortThatSetIt(
714 const GURL& destination,
715 int source_port,
716 CookieSourceScheme source_scheme);
717
Dylan Cutler03d2c76c2022-02-18 02:23:15718 // TODO(crbug.com/1296161): Delete this when the partitioned cookies Origin
719 // Trial ends.
720 void OnConvertPartitionedCookiesToUnpartitioned(const GURL& url);
721 void ConvertPartitionedCookie(const net::CanonicalCookie& cookie,
722 const GURL& url);
723
Lily Chen229623d72020-06-01 17:20:14724 // Set of keys (eTLD+1's) for which non-expired cookies have
725 // been evicted for hitting the per-domain max. The size of this set is
726 // histogrammed periodically. The size is limited to |kMaxDomainPurgedKeys|.
727 std::set<std::string> domain_purged_keys_;
728
729 // The number of distinct keys (eTLD+1's) currently present in the |cookies_|
730 // multimap. This is histogrammed periodically.
Tsuyoshi Horo432981d52022-06-09 09:50:13731 size_t num_keys_ = 0u;
Lily Chen229623d72020-06-01 17:20:14732
[email protected]63ee33bd2012-03-15 09:29:58733 CookieMap cookies_;
734
Dylan Cutler0b9a4e962021-09-13 17:34:25735 PartitionedCookieMap partitioned_cookies_;
736
737 // Number of distinct partitioned cookies globally. This is used to enforce a
738 // global maximum on the number of partitioned cookies.
Tsuyoshi Horo432981d52022-06-09 09:50:13739 size_t num_partitioned_cookies_ = 0u;
Dylan Cutler0b9a4e962021-09-13 17:34:25740
Victor Costan14f47c12018-03-01 08:02:24741 CookieMonsterChangeDispatcher change_dispatcher_;
742
erikchen1dd72a72015-05-06 20:45:05743 // Indicates whether the cookie store has been initialized.
Tsuyoshi Horo432981d52022-06-09 09:50:13744 bool initialized_ = false;
[email protected]63ee33bd2012-03-15 09:29:58745
erikchen1dd72a72015-05-06 20:45:05746 // Indicates whether the cookie store has started fetching all cookies.
Tsuyoshi Horo432981d52022-06-09 09:50:13747 bool started_fetching_all_cookies_ = false;
erikchen1dd72a72015-05-06 20:45:05748 // Indicates whether the cookie store has finished fetching all cookies.
Tsuyoshi Horo432981d52022-06-09 09:50:13749 bool finished_fetching_all_cookies_ = false;
[email protected]63ee33bd2012-03-15 09:29:58750
751 // List of domain keys that have been loaded from the DB.
752 std::set<std::string> keys_loaded_;
753
754 // Map of domain keys to their associated task queues. These tasks are blocked
755 // until all cookies for the associated domain key eTLD+1 are loaded from the
756 // backend store.
Brett Wilsonc6a0c822017-09-12 00:04:29757 std::map<std::string, base::circular_deque<base::OnceClosure>>
758 tasks_pending_for_key_;
[email protected]63ee33bd2012-03-15 09:29:58759
760 // Queues tasks that are blocked until all cookies are loaded from the backend
761 // store.
Brett Wilsonc6a0c822017-09-12 00:04:29762 base::circular_deque<base::OnceClosure> tasks_pending_;
mmenkef49fca0e2016-03-08 12:46:24763
764 // Once a global cookie task has been seen, all per-key tasks must be put in
765 // |tasks_pending_| instead of |tasks_pending_for_key_| to ensure a reasonable
rdsmithe5c701d2017-07-12 21:50:00766 // view of the cookie store. This is more to ensure fancy cookie export/import
mmenkef49fca0e2016-03-08 12:46:24767 // code has a consistent view of the CookieStore, rather than out of concern
768 // for typical use.
Tsuyoshi Horo432981d52022-06-09 09:50:13769 bool seen_global_task_ = false;
[email protected]63ee33bd2012-03-15 09:29:58770
Helen Licd0fab862018-08-13 16:07:53771 NetLogWithSource net_log_;
772
[email protected]63ee33bd2012-03-15 09:29:58773 scoped_refptr<PersistentCookieStore> store_;
774
[email protected]63ee33bd2012-03-15 09:29:58775 // Minimum delay after updating a cookie's LastAccessDate before we will
776 // update it again.
777 const base::TimeDelta last_access_threshold_;
778
779 // Approximate date of access time of least recently accessed cookie
780 // in |cookies_|. Note that this is not guaranteed to be accurate, only a)
781 // to be before or equal to the actual time, and b) to be accurate
mmenkef4721d992017-06-07 17:13:59782 // immediately after a garbage collection that scans through all the cookies
783 // (When garbage collection does not scan through all cookies, it may not be
784 // updated). This value is used to determine whether global garbage collection
785 // might find cookies to purge. Note: The default Time() constructor will
786 // create a value that compares earlier than any other time value, which is
787 // wanted. Thus this value is not initialized.
[email protected]63ee33bd2012-03-15 09:29:58788 base::Time earliest_access_time_;
789
[email protected]63ee33bd2012-03-15 09:29:58790 std::vector<std::string> cookieable_schemes_;
791
[email protected]63ee33bd2012-03-15 09:29:58792 base::Time last_statistic_record_time_;
793
Tsuyoshi Horo432981d52022-06-09 09:50:13794 bool persist_session_cookies_ = false;
[email protected]63ee33bd2012-03-15 09:29:58795
Kirubel Akliluc9b4e412022-01-12 01:00:01796 bool first_party_sets_enabled_;
797
mmenkebe0910d2016-03-01 19:09:09798 base::ThreadChecker thread_checker_;
799
Jeremy Romand54000b22019-07-08 18:40:16800 base::WeakPtrFactory<CookieMonster> weak_ptr_factory_{this};
[email protected]63ee33bd2012-03-15 09:29:58801};
802
[email protected]63ee33bd2012-03-15 09:29:58803typedef base::RefCountedThreadSafe<CookieMonster::PersistentCookieStore>
804 RefcountedPersistentCookieStore;
805
[email protected]c1b6e102013-04-10 20:54:49806class NET_EXPORT CookieMonster::PersistentCookieStore
[email protected]63ee33bd2012-03-15 09:29:58807 : public RefcountedPersistentCookieStore {
808 public:
Maks Orlovich108cb4c2019-03-26 20:24:57809 typedef base::OnceCallback<void(
810 std::vector<std::unique_ptr<CanonicalCookie>>)>
[email protected]5b9bc352012-07-18 13:13:34811 LoadedCallback;
[email protected]63ee33bd2012-03-15 09:29:58812
Peter Boström407869b2021-10-07 04:42:48813 PersistentCookieStore(const PersistentCookieStore&) = delete;
814 PersistentCookieStore& operator=(const PersistentCookieStore&) = delete;
815
[email protected]63ee33bd2012-03-15 09:29:58816 // Initializes the store and retrieves the existing cookies. This will be
817 // called only once at startup. The callback will return all the cookies
818 // that are not yet returned to CookieMonster by previous priority loads.
mmenkebe0910d2016-03-01 19:09:09819 //
820 // |loaded_callback| may not be NULL.
Helen Li92a29f102018-08-15 23:02:26821 // |net_log| is a NetLogWithSource that may be copied if the persistent
822 // store wishes to log NetLog events.
Maks Orlovich108cb4c2019-03-26 20:24:57823 virtual void Load(LoadedCallback loaded_callback,
Helen Li92a29f102018-08-15 23:02:26824 const NetLogWithSource& net_log) = 0;
[email protected]63ee33bd2012-03-15 09:29:58825
826 // Does a priority load of all cookies for the domain key (eTLD+1). The
827 // callback will return all the cookies that are not yet returned by previous
828 // loads, which includes cookies for the requested domain key if they are not
829 // already returned, plus all cookies that are chain-loaded and not yet
830 // returned to CookieMonster.
mmenkebe0910d2016-03-01 19:09:09831 //
832 // |loaded_callback| may not be NULL.
[email protected]63ee33bd2012-03-15 09:29:58833 virtual void LoadCookiesForKey(const std::string& key,
Maks Orlovich108cb4c2019-03-26 20:24:57834 LoadedCallback loaded_callback) = 0;
[email protected]63ee33bd2012-03-15 09:29:58835
836 virtual void AddCookie(const CanonicalCookie& cc) = 0;
837 virtual void UpdateCookieAccessTime(const CanonicalCookie& cc) = 0;
838 virtual void DeleteCookie(const CanonicalCookie& cc) = 0;
839
[email protected]bf510ed2012-06-05 08:31:43840 // Instructs the store to not discard session only cookies on shutdown.
841 virtual void SetForceKeepSessionState() = 0;
[email protected]63ee33bd2012-03-15 09:29:58842
Nick Harper14e23332017-07-28 00:27:23843 // Sets a callback that will be run before the store flushes. If |callback|
844 // performs any async operations, the store will not wait for those to finish
845 // before flushing.
Lily Chen9934f7e2019-03-13 19:16:55846 virtual void SetBeforeCommitCallback(base::RepeatingClosure callback) = 0;
Nick Harper14e23332017-07-28 00:27:23847
mmenkebe0910d2016-03-01 19:09:09848 // Flushes the store and posts |callback| when complete. |callback| may be
849 // NULL.
rdsmith7ac81712017-06-22 17:09:54850 virtual void Flush(base::OnceClosure callback) = 0;
[email protected]63ee33bd2012-03-15 09:29:58851
852 protected:
Christian Dullweberff11c452021-05-12 17:04:45853 PersistentCookieStore() = default;
854 virtual ~PersistentCookieStore() = default;
[email protected]63ee33bd2012-03-15 09:29:58855
856 private:
[email protected]a9813302012-04-28 09:29:28857 friend class base::RefCountedThreadSafe<PersistentCookieStore>;
[email protected]63ee33bd2012-03-15 09:29:58858};
859
[email protected]63ee33bd2012-03-15 09:29:58860} // namespace net
861
862#endif // NET_COOKIES_COOKIE_MONSTER_H_