blob: 227ca621fe05dfcb6bc9b107e2c037ea8dca0747 [file] [log] [blame]
Avi Drissman64595482022-09-14 20:52:291// Copyright 2012 The Chromium Authors
[email protected]63ee33bd2012-03-15 09:29:582// 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"
23#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"
Chris Fredrickson4f22b3262022-09-30 19:57:3026#include "base/thread_annotations.h"
mmenkebe0910d2016-03-01 19:09:0927#include "base/threading/thread_checker.h"
[email protected]9da992db2013-06-28 05:40:4728#include "base/time/time.h"
[email protected]565c3f42012-08-14 14:22:5829#include "net/base/net_export.h"
cfredric326a0bc2022-01-12 18:51:3030#include "net/base/schemeful_site.h"
[email protected]8da4b1812012-07-25 13:54:3831#include "net/cookies/canonical_cookie.h"
Lily Chenab36a112019-09-19 20:17:2832#include "net/cookies/cookie_access_delegate.h"
[email protected]ab2d75c82013-04-19 18:39:0433#include "net/cookies/cookie_constants.h"
Jihwan Marc Kim3e132f12020-05-20 17:33:1934#include "net/cookies/cookie_inclusion_status.h"
Victor Costan14f47c12018-03-01 08:02:2435#include "net/cookies/cookie_monster_change_dispatcher.h"
[email protected]63ee33bd2012-03-15 09:29:5836#include "net/cookies/cookie_store.h"
Helen Licd0fab862018-08-13 16:07:5337#include "net/log/net_log_with_source.h"
Alex Kalugin379f47d82022-03-08 13:11:5938#include "third_party/abseil-cpp/absl/types/optional.h"
ellyjones399e35a22014-10-27 11:09:5639#include "url/gurl.h"
[email protected]63ee33bd2012-03-15 09:29:5840
[email protected]63ee33bd2012-03-15 09:29:5841namespace net {
42
Victor Costan14f47c12018-03-01 08:02:2443class CookieChangeDispatcher;
[email protected]63ee33bd2012-03-15 09:29:5844
45// The cookie monster is the system for storing and retrieving cookies. It has
46// an in-memory list of all cookies, and synchronizes non-session cookies to an
47// optional permanent storage that implements the PersistentCookieStore
48// interface.
49//
mmenke96f3bab2016-01-22 17:34:0250// Tasks may be deferred if all affected cookies are not yet loaded from the
51// backing store. Otherwise, callbacks may be invoked immediately.
[email protected]63ee33bd2012-03-15 09:29:5852//
53// A cookie task is either pending loading of the entire cookie store, or
Maks Orlovich323efaf2018-03-06 02:56:3954// loading of cookies for a specific domain key (GetKey(), roughly eTLD+1). In
55// the former case, the cookie callback will be queued in tasks_pending_ while
56// PersistentCookieStore chain loads the cookie store on DB thread. In the
57// latter case, the cookie callback will be queued in tasks_pending_for_key_
58// while PermanentCookieStore loads cookies for the specified domain key on DB
59// thread.
[email protected]63ee33bd2012-03-15 09:29:5860class NET_EXPORT CookieMonster : public CookieStore {
61 public:
[email protected]63ee33bd2012-03-15 09:29:5862 class PersistentCookieStore;
63
64 // Terminology:
65 // * The 'top level domain' (TLD) of an internet domain name is
66 // the terminal "." free substring (e.g. "com" for google.com
67 // or world.std.com).
68 // * The 'effective top level domain' (eTLD) is the longest
69 // "." initiated terminal substring of an internet domain name
70 // that is controlled by a general domain registrar.
71 // (e.g. "co.uk" for news.bbc.co.uk).
72 // * The 'effective top level domain plus one' (eTLD+1) is the
73 // shortest "." delimited terminal substring of an internet
74 // domain name that is not controlled by a general domain
75 // registrar (e.g. "bbc.co.uk" for news.bbc.co.uk, or
76 // "google.com" for news.google.com). The general assumption
77 // is that all hosts and domains under an eTLD+1 share some
78 // administrative control.
79
80 // CookieMap is the central data structure of the CookieMonster. It
81 // is a map whose values are pointers to CanonicalCookie data
82 // structures (the data structures are owned by the CookieMonster
83 // and must be destroyed when removed from the map). The key is based on the
84 // effective domain of the cookies. If the domain of the cookie has an
85 // eTLD+1, that is the key for the map. If the domain of the cookie does not
86 // have an eTLD+1, the key of the map is the host the cookie applies to (it is
87 // not legal to have domain cookies without an eTLD+1). This rule
88 // excludes cookies for, e.g, ".com", ".co.uk", or ".internalnetwork".
89 // This behavior is the same as the behavior in Firefox v 3.6.10.
Dylan Cutler0b9a4e962021-09-13 17:34:2590 // CookieMap does not store cookies that were set with the Partitioned
91 // attribute, those are stored in PartitionedCookieMap.
[email protected]63ee33bd2012-03-15 09:29:5892
93 // NOTE(deanm):
94 // I benchmarked hash_multimap vs multimap. We're going to be query-heavy
95 // so it would seem like hashing would help. However they were very
96 // close, with multimap being a tiny bit faster. I think this is because
97 // our map is at max around 1000 entries, and the additional complexity
98 // for the hashing might not overcome the O(log(1000)) for querying
99 // a multimap. Also, multimap is standard, another reason to use it.
100 // TODO(rdsmith): This benchmark should be re-done now that we're allowing
avie7cd11a2016-10-11 02:00:35101 // substantially more entries in the map.
102 using CookieMap =
103 std::multimap<std::string, std::unique_ptr<CanonicalCookie>>;
104 using CookieMapItPair = std::pair<CookieMap::iterator, CookieMap::iterator>;
105 using CookieItVector = std::vector<CookieMap::iterator>;
[email protected]8ad5d462013-05-02 08:45:26106
Dylan Cutler0b9a4e962021-09-13 17:34:25107 // PartitionedCookieMap only stores cookies that were set with the Partitioned
108 // attribute. The map is double-keyed on cookie's partition key and
109 // the cookie's effective domain of the cookie (the key of CookieMap).
110 // We store partitioned cookies in a separate map so that the queries for a
111 // request's unpartitioned and partitioned cookies will both be more
112 // efficient (since querying two smaller maps is more efficient that querying
113 // one larger map twice).
114 using PartitionedCookieMap =
115 std::map<CookiePartitionKey, std::unique_ptr<CookieMap>>;
116 using PartitionedCookieMapIterators =
117 std::pair<PartitionedCookieMap::iterator, CookieMap::iterator>;
118
[email protected]8ad5d462013-05-02 08:45:26119 // Cookie garbage collection thresholds. Based off of the Mozilla defaults.
120 // When the number of cookies gets to k{Domain,}MaxCookies
121 // purge down to k{Domain,}MaxCookies - k{Domain,}PurgeCookies.
122 // It might seem scary to have a high purge value, but really it's not.
123 // You just make sure that you increase the max to cover the increase
124 // in purge, and we would have been purging the same number of cookies.
125 // We're just going through the garbage collection process less often.
126 // Note that the DOMAIN values are per eTLD+1; see comment for the
127 // CookieMap typedef. So, e.g., the maximum number of cookies allowed for
128 // google.com and all of its subdomains will be 150-180.
129 //
130 // Any cookies accessed more recently than kSafeFromGlobalPurgeDays will not
131 // be evicted by global garbage collection, even if we have more than
132 // kMaxCookies. This does not affect domain garbage collection.
133 static const size_t kDomainMaxCookies;
134 static const size_t kDomainPurgeCookies;
135 static const size_t kMaxCookies;
136 static const size_t kPurgeCookies;
137
Lily Chen229623d72020-06-01 17:20:14138 // Max number of keys to store for domains that have been purged.
139 static const size_t kMaxDomainPurgedKeys;
140
Dylan Cutler0b9a4e962021-09-13 17:34:25141 // Partitioned cookie garbage collection thresholds.
142 static const size_t kPerPartitionDomainMaxCookies;
143 // TODO(crbug.com/1225444): Add global limit to number of partitioned cookies.
144
[email protected]8ad5d462013-05-02 08:45:26145 // Quota for cookies with {low, medium, high} priorities within a domain.
mkwst87734352016-03-03 17:36:23146 static const size_t kDomainCookiesQuotaLow;
147 static const size_t kDomainCookiesQuotaMedium;
148 static const size_t kDomainCookiesQuotaHigh;
[email protected]63ee33bd2012-03-15 09:29:58149
Matt Menke477ab632019-06-27 23:12:17150 // The number of days since last access that cookies will not be subject
151 // to global garbage collection.
152 static const int kSafeFromGlobalPurgeDays;
153
[email protected]63ee33bd2012-03-15 09:29:58154 // The store passed in should not have had Init() called on it yet. This
155 // class will take care of initializing it. The backing store is NOT owned by
156 // this class, but it must remain valid for the duration of the cookie
157 // monster's existence. If |store| is NULL, then no backing store will be
Nick Harper57142b1c2019-03-14 21:03:59158 // updated. |net_log| must outlive the CookieMonster and can be null.
Chris Fredricksonbadf72dc2022-10-05 23:42:42159 CookieMonster(scoped_refptr<PersistentCookieStore> store, NetLog* net_log);
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,
Chris Fredricksonbadf72dc2022-10-05 23:42:42165 NetLog* net_log);
[email protected]63ee33bd2012-03-15 09:29:58166
Peter Boström293b1342021-09-22 17:31:43167 CookieMonster(const CookieMonster&) = delete;
168 CookieMonster& operator=(const CookieMonster&) = delete;
169
mmenke606c59c2016-03-07 18:20:55170 ~CookieMonster() override;
171
rdsmith0e84cea2017-07-13 03:09:53172 // Writes all the cookies in |list| into the store, replacing all cookies
173 // currently present in store.
rdsmith2709eee2017-06-20 22:43:27174 // This method does not flush the backend.
175 // TODO(rdsmith, mmenke): Do not use this function; it is deprecated
176 // and should be removed.
177 // See https://codereview.chromium.org/2882063002/#msg64.
rdsmith7ac81712017-06-22 17:09:54178 void SetAllCookiesAsync(const CookieList& list, SetCookiesCallback callback);
drogerd5d1278c2015-03-17 19:21:51179
[email protected]63ee33bd2012-03-15 09:29:58180 // CookieStore implementation.
Juba Borgohain9fa24142022-02-04 18:25:42181 void SetCanonicalCookieAsync(
182 std::unique_ptr<CanonicalCookie> cookie,
183 const GURL& source_url,
184 const CookieOptions& options,
185 SetCookiesCallback callback,
Alex Kalugin379f47d82022-03-08 13:11:59186 absl::optional<CookieAccessResult> cookie_access_result =
187 absl::nullopt) override;
Dylan Cutlercd2d8932021-10-05 19:03:43188 void GetCookieListWithOptionsAsync(const GURL& url,
189 const CookieOptions& options,
Aykut Bulut244341e2021-12-09 15:57:25190 const CookiePartitionKeyCollection& s,
Dylan Cutlercd2d8932021-10-05 19:03:43191 GetCookieListCallback callback) override;
Lily Chenf068a762019-08-21 21:10:50192 void GetAllCookiesAsync(GetAllCookiesCallback callback) override;
Lily Chene2e9ae012019-10-09 20:02:54193 void GetAllCookiesWithAccessSemanticsAsync(
194 GetAllCookiesWithAccessSemanticsCallback callback) override;
mmenke24379d52016-02-05 23:50:17195 void DeleteCanonicalCookieAsync(const CanonicalCookie& cookie,
rdsmith7ac81712017-06-22 17:09:54196 DeleteCallback callback) override;
Chris Mumfordd8ed9f82018-05-01 15:43:13197 void DeleteAllCreatedInTimeRangeAsync(
198 const CookieDeletionInfo::TimeRange& creation_range,
199 DeleteCallback callback) override;
Chris Mumford800caa62018-04-20 19:34:44200 void DeleteAllMatchingInfoAsync(CookieDeletionInfo delete_info,
201 DeleteCallback callback) override;
Christian Dullweberff11c452021-05-12 17:04:45202 void DeleteSessionCookiesAsync(DeleteCallback callback) override;
203 void DeleteMatchingCookiesAsync(DeletePredicate predicate,
204 DeleteCallback callback) override;
rdsmith7ac81712017-06-22 17:09:54205 void FlushStore(base::OnceClosure callback) override;
mmenkeded79da2016-02-06 08:28:51206 void SetForceKeepSessionState() override;
Victor Costan14f47c12018-03-01 08:02:24207 CookieChangeDispatcher& GetChangeDispatcher() override;
Nate Fischerc6fb6cf2019-03-27 00:39:49208 void SetCookieableSchemes(const std::vector<std::string>& schemes,
209 SetCookieableSchemesCallback callback) override;
mmenke74bcbd52016-01-21 17:17:56210
[email protected]63ee33bd2012-03-15 09:29:58211 // Enables writing session cookies into the cookie database. If this this
212 // method is called, it must be called before first use of the instance
213 // (i.e. as part of the instance initialization process).
214 void SetPersistSessionCookies(bool persist_session_cookies);
215
[email protected]63ee33bd2012-03-15 09:29:58216 // The default list of schemes the cookie monster can handle.
[email protected]5edff3c52014-06-23 20:27:48217 static const char* const kDefaultCookieableSchemes[];
[email protected]63ee33bd2012-03-15 09:29:58218 static const int kDefaultCookieableSchemesCount;
219
Maks Orlovich323efaf2018-03-06 02:56:39220 // Find a key based on the given domain, which will be used to find all
221 // cookies potentially relevant to it. This is used for lookup in cookies_ as
222 // well as for PersistentCookieStore::LoadCookiesForKey. See comment on keys
223 // before the CookieMap typedef.
224 static std::string GetKey(base::StringPiece domain);
225
cfredric59f8a8452021-06-08 15:27:11226 // Exposes the comparison function used when sorting cookies.
227 static bool CookieSorter(const CanonicalCookie* cc1,
228 const CanonicalCookie* cc2);
229
Lily Chen229623d72020-06-01 17:20:14230 // Triggers immediate recording of stats that are typically reported
231 // periodically.
232 bool DoRecordPeriodicStatsForTesting() { return DoRecordPeriodicStats(); }
233
Dylan Cutler03d2c76c2022-02-18 02:23:15234 // Will convert a site's partitioned cookies into unpartitioned cookies. This
235 // may result in multiple cookies which have the same (partition_key, name,
236 // host_key, path), which violates the database's unique constraint. The
237 // algorithm we use to coalesce the cookies into a single unpartitioned cookie
238 // is the following:
239 //
240 // 1. If one of the cookies has no partition key (i.e. it is unpartitioned)
241 // choose this cookie.
242 //
243 // 2. Choose the partitioned cookie with the most recent last_access_time.
244 //
245 // TODO(crbug.com/1296161): Delete this when the partitioned cookies Origin
246 // Trial ends.
247 void ConvertPartitionedCookiesToUnpartitioned(const GURL& url) override;
248
[email protected]63ee33bd2012-03-15 09:29:58249 private:
avie7cd11a2016-10-11 02:00:35250 // For garbage collection constants.
[email protected]63ee33bd2012-03-15 09:29:58251 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestHostGarbageCollection);
mmenkef4721d992017-06-07 17:13:59252 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest,
253 GarbageCollectWithSecureCookiesOnly);
[email protected]63ee33bd2012-03-15 09:29:58254 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestGCTimes);
255
256 // For validation of key values.
257 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestDomainTree);
258 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestImport);
259 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, GetKey);
260 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestGetKey);
261
262 // For FindCookiesForKey.
263 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, ShortLivedSessionCookies);
264
estark7feb65c2b2015-08-21 23:38:20265 // For CookieSource histogram enum.
266 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, CookieSourceHistogram);
267
jww31e32632015-12-16 23:38:34268 // For kSafeFromGlobalPurgeDays in CookieStore.
jwwa26e439d2017-01-27 18:17:27269 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, EvictSecureCookies);
jww82d99c12015-11-25 18:39:53270
jww31e32632015-12-16 23:38:34271 // For CookieDeleteEquivalent histogram enum.
272 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest,
273 CookieDeleteEquivalentHistogramTest);
jww31e32632015-12-16 23:38:34274
Steven Bingler0420a3752020-11-20 21:40:48275 // For CookieSentToSamePort enum.
276 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest,
277 CookiePortReadDiffersFromSetHistogram);
278 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, IsCookieSentToSamePortThatSetIt);
279
[email protected]63ee33bd2012-03-15 09:29:58280 // Internal reasons for deletion, used to populate informative histograms
281 // and to provide a public cause for onCookieChange notifications.
282 //
283 // If you add or remove causes from this list, please be sure to also update
Victor Costan14f47c12018-03-01 08:02:24284 // the CookieChangeCause mapping inside ChangeCauseMapping. New items (if
285 // necessary) should be added at the end of the list, just before
Nick Harper7a6683a2018-01-30 20:42:52286 // DELETE_COOKIE_LAST_ENTRY.
[email protected]63ee33bd2012-03-15 09:29:58287 enum DeletionCause {
288 DELETE_COOKIE_EXPLICIT = 0,
mkwstaa07ee82016-03-11 15:32:14289 DELETE_COOKIE_OVERWRITE = 1,
290 DELETE_COOKIE_EXPIRED = 2,
291 DELETE_COOKIE_EVICTED = 3,
292 DELETE_COOKIE_DUPLICATE_IN_BACKING_STORE = 4,
293 DELETE_COOKIE_DONT_RECORD = 5, // For final cleanup after flush to store.
[email protected]63ee33bd2012-03-15 09:29:58294
mkwstaa07ee82016-03-11 15:32:14295 // Cookies evicted during domain-level garbage collection.
296 DELETE_COOKIE_EVICTED_DOMAIN = 6,
[email protected]63ee33bd2012-03-15 09:29:58297
Dylan Cutler0b9a4e962021-09-13 17:34:25298 // Cookies evicted during global garbage collection, which takes place after
mkwstaa07ee82016-03-11 15:32:14299 // domain-level garbage collection fails to bring the cookie store under
300 // the overall quota.
301 DELETE_COOKIE_EVICTED_GLOBAL = 7,
302
303 // #8 was DELETE_COOKIE_EVICTED_DOMAIN_PRE_SAFE
304 // #9 was DELETE_COOKIE_EVICTED_DOMAIN_POST_SAFE
[email protected]63ee33bd2012-03-15 09:29:58305
306 // A common idiom is to remove a cookie by overwriting it with an
307 // already-expired expiration date. This captures that case.
mkwstaa07ee82016-03-11 15:32:14308 DELETE_COOKIE_EXPIRED_OVERWRITE = 10,
[email protected]63ee33bd2012-03-15 09:29:58309
[email protected]6210ce52013-09-20 03:33:14310 // Cookies are not allowed to contain control characters in the name or
311 // value. However, we used to allow them, so we are now evicting any such
312 // cookies as we load them. See http://crbug.com/238041.
mkwstaa07ee82016-03-11 15:32:14313 DELETE_COOKIE_CONTROL_CHAR = 11,
[email protected]6210ce52013-09-20 03:33:14314
jww82d99c12015-11-25 18:39:53315 // When strict secure cookies is enabled, non-secure cookies are evicted
316 // right after expired cookies.
mkwstaa07ee82016-03-11 15:32:14317 DELETE_COOKIE_NON_SECURE = 12,
jww82d99c12015-11-25 18:39:53318
Dylan Cutler0b9a4e962021-09-13 17:34:25319 // Partitioned cookies evicted during per-partition domain-level garbage
320 // collection.
321 DELETE_COOKIE_EVICTED_PER_PARTITION_DOMAIN = 13,
322
323 DELETE_COOKIE_LAST_ENTRY = 14,
[email protected]63ee33bd2012-03-15 09:29:58324 };
325
mkwstc1aa4cc2015-04-03 19:57:45326 // This enum is used to generate a histogramed bitmask measureing the types
327 // of stored cookies. Please do not reorder the list when adding new entries.
328 // New items MUST be added at the end of the list, just before
329 // COOKIE_TYPE_LAST_ENTRY;
Lily Chenda465cca2021-03-08 23:47:17330 // There will be 2^COOKIE_TYPE_LAST_ENTRY buckets in the linear histogram.
mkwstc1aa4cc2015-04-03 19:57:45331 enum CookieType {
mkwst46549412016-02-01 10:05:37332 COOKIE_TYPE_SAME_SITE = 0,
mkwstc1aa4cc2015-04-03 19:57:45333 COOKIE_TYPE_HTTPONLY,
334 COOKIE_TYPE_SECURE,
335 COOKIE_TYPE_LAST_ENTRY
336 };
337
estark7feb65c2b2015-08-21 23:38:20338 // Used to populate a histogram containing information about the
339 // sources of Secure and non-Secure cookies: that is, whether such
340 // cookies are set by origins with cryptographic or non-cryptographic
341 // schemes. Please do not reorder the list when adding new
Lily Chenda465cca2021-03-08 23:47:17342 // entries. New items MUST be added at the end of the list, and kMaxValue
343 // should be updated to the last value.
estark7feb65c2b2015-08-21 23:38:20344 //
Lei Zhangd9388332022-08-02 23:30:46345 // CookieSource::k(Non)SecureCookie(Non)CryptographicScheme means
estark7feb65c2b2015-08-21 23:38:20346 // that a cookie was set or overwritten from a URL with the given type
347 // of scheme. This enum should not be used when cookies are *cleared*,
348 // because its purpose is to understand if Chrome can deprecate the
349 // ability of HTTP urls to set/overwrite Secure cookies.
Lei Zhangd9388332022-08-02 23:30:46350 enum class CookieSource : uint8_t {
351 kSecureCookieCryptographicScheme = 0,
352 kSecureCookieNoncryptographicScheme,
353 kNonsecureCookieCryptographicScheme,
354 kNonsecureCookieNoncryptographicScheme,
355 kMaxValue = kNonsecureCookieNoncryptographicScheme
estark7feb65c2b2015-08-21 23:38:20356 };
357
Steven Bingler0420a3752020-11-20 21:40:48358 // Enum for collecting metrics on how frequently a cookie is sent to the same
359 // port it was set by.
360 //
361 // kNoButDefault exists because we expect for cookies being sent between
362 // schemes to have a port mismatch and want to separate those out from other,
363 // more interesting, cases.
364 //
365 // Do not reorder or renumber. Used for metrics.
366 enum class CookieSentToSamePort {
367 kSourcePortUnspecified = 0, // Cookie's source port is unspecified, we
368 // can't know if this is the same port or not.
369 kInvalid = 1, // The source port was corrupted to be PORT_INVALID, we
370 // can't know if this is the same port or not.
371 kNo = 2, // Source port and destination port are different.
372 kNoButDefault =
373 3, // Source and destination ports are different but they're
374 // the defaults for their scheme. This can mean that an http
375 // cookie was sent to a https origin or vice-versa.
376 kYes = 4, // They're the same.
377 kMaxValue = kYes
378 };
379
[email protected]63ee33bd2012-03-15 09:29:58380 // Record statistics every kRecordStatisticsIntervalSeconds of uptime.
381 static const int kRecordStatisticsIntervalSeconds = 10 * 60;
382
rdsmitha6ce4442017-06-21 17:11:05383 // Sets a canonical cookie, deletes equivalents and performs garbage
Lily Chen96f29a132020-04-15 17:59:36384 // collection. |source_url| indicates what URL the cookie is being set
Maks Orlovichfdbc8be2019-03-18 18:34:52385 // from; secure cookies cannot be altered from insecure schemes, and some
386 // schemes may not be authorized.
387 //
388 // |options| indicates if this setting operation is allowed
389 // to affect http_only or same-site cookies.
Juba Borgohain9fa24142022-02-04 18:25:42390 //
391 // |cookie_access_result| is an optional input status, to allow for status
392 // chaining from callers. It helps callers provide the status of a
393 // canonical cookie that may have warnings associated with it.
394 void SetCanonicalCookie(
395 std::unique_ptr<CanonicalCookie> cookie,
396 const GURL& source_url,
397 const CookieOptions& options,
398 SetCookiesCallback callback,
Alex Kalugin379f47d82022-03-08 13:11:59399 absl::optional<CookieAccessResult> cookie_access_result = absl::nullopt);
rdsmitha6ce4442017-06-21 17:11:05400
Lily Chenf068a762019-08-21 21:10:50401 void GetAllCookies(GetAllCookiesCallback callback);
[email protected]63ee33bd2012-03-15 09:29:58402
Lily Chene2e9ae012019-10-09 20:02:54403 void AttachAccessSemanticsListForCookieList(
404 GetAllCookiesWithAccessSemanticsCallback callback,
405 const CookieList& cookie_list);
406
Dylan Cutler0b9a4e962021-09-13 17:34:25407 void GetCookieListWithOptions(
408 const GURL& url,
409 const CookieOptions& options,
Aykut Bulut244341e2021-12-09 15:57:25410 const CookiePartitionKeyCollection& cookie_partition_key_collection,
Dylan Cutler0b9a4e962021-09-13 17:34:25411 GetCookieListCallback callback);
[email protected]63ee33bd2012-03-15 09:29:58412
Chris Mumfordd8ed9f82018-05-01 15:43:13413 void DeleteAllCreatedInTimeRange(
414 const CookieDeletionInfo::TimeRange& creation_range,
415 DeleteCallback callback);
[email protected]63ee33bd2012-03-15 09:29:58416
Christian Dullweberff11c452021-05-12 17:04:45417 // Returns whether |cookie| matches |delete_info|.
418 bool MatchCookieDeletionInfo(const CookieDeletionInfo& delete_info,
419 const net::CanonicalCookie& cookie);
[email protected]63ee33bd2012-03-15 09:29:58420
rdsmithe5c701d2017-07-12 21:50:00421 void DeleteCanonicalCookie(const CanonicalCookie& cookie,
422 DeleteCallback callback);
mmenke24379d52016-02-05 23:50:17423
Christian Dullweberff11c452021-05-12 17:04:45424 void DeleteMatchingCookies(DeletePredicate predicate,
425 DeletionCause cause,
426 DeleteCallback callback);
[email protected]264807b2012-04-25 14:49:37427
erikchen1dd72a72015-05-06 20:45:05428 // The first access to the cookie store initializes it. This method should be
429 // called before any access to the cookie store.
430 void MarkCookieStoreAsInitialized();
[email protected]63ee33bd2012-03-15 09:29:58431
erikchen1dd72a72015-05-06 20:45:05432 // Fetches all cookies if the backing store exists and they're not already
433 // being fetched.
erikchen1dd72a72015-05-06 20:45:05434 void FetchAllCookiesIfNecessary();
435
436 // Fetches all cookies from the backing store.
erikchen1dd72a72015-05-06 20:45:05437 void FetchAllCookies();
438
439 // Whether all cookies should be fetched as soon as any is requested.
440 bool ShouldFetchAllCookiesWhenFetchingAnyCookie();
[email protected]63ee33bd2012-03-15 09:29:58441
442 // Stores cookies loaded from the backing store and invokes any deferred
443 // calls. |beginning_time| should be the moment PersistentCookieStore::Load
444 // was invoked and is used for reporting histogram_time_blocked_on_load_.
445 // See PersistentCookieStore::Load for details on the contents of cookies.
446 void OnLoaded(base::TimeTicks beginning_time,
avie7cd11a2016-10-11 02:00:35447 std::vector<std::unique_ptr<CanonicalCookie>> cookies);
[email protected]63ee33bd2012-03-15 09:29:58448
449 // Stores cookies loaded from the backing store and invokes the deferred
450 // task(s) pending loading of cookies associated with the domain key
Maks Orlovich323efaf2018-03-06 02:56:39451 // (GetKey, roughly eTLD+1). Called when all cookies for the domain key have
452 // been loaded from DB. See PersistentCookieStore::Load for details on the
453 // contents of cookies.
mkwstbe84af312015-02-20 08:52:45454 void OnKeyLoaded(const std::string& key,
avie7cd11a2016-10-11 02:00:35455 std::vector<std::unique_ptr<CanonicalCookie>> cookies);
[email protected]63ee33bd2012-03-15 09:29:58456
457 // Stores the loaded cookies.
avie7cd11a2016-10-11 02:00:35458 void StoreLoadedCookies(
459 std::vector<std::unique_ptr<CanonicalCookie>> cookies);
[email protected]63ee33bd2012-03-15 09:29:58460
461 // Invokes deferred calls.
462 void InvokeQueue();
463
464 // Checks that |cookies_| matches our invariants, and tries to repair any
465 // inconsistencies. (In other words, it does not have duplicate cookies).
466 void EnsureCookiesMapIsValid();
467
468 // Checks for any duplicate cookies for CookieMap key |key| which lie between
469 // |begin| and |end|. If any are found, all but the most recent are deleted.
Dylan Cutler0b9a4e962021-09-13 17:34:25470 //
471 // If |cookie_partition_it| is not nullopt, then this function trims cookies
472 // from the CookieMap in |partitioned_cookies_| at |cookie_partition_it|
473 // instead of trimming cookies from |cookies_|.
474 void TrimDuplicateCookiesForKey(
475 const std::string& key,
476 CookieMap::iterator begin,
477 CookieMap::iterator end,
478 absl::optional<PartitionedCookieMap::iterator> cookie_partition_it);
[email protected]63ee33bd2012-03-15 09:29:58479
480 void SetDefaultCookieableSchemes();
481
cfredric09ba72fb2020-12-22 21:03:27482 std::vector<CanonicalCookie*> FindCookiesForRegistryControlledHost(
Dylan Cutler0b9a4e962021-09-13 17:34:25483 const GURL& url,
Dylan Cutlera7b5f822022-09-16 16:56:05484 CookieMap* cookie_map = nullptr,
485 PartitionedCookieMap::iterator* partition_it = nullptr);
Dylan Cutler0b9a4e962021-09-13 17:34:25486
487 std::vector<CanonicalCookie*> FindPartitionedCookiesForRegistryControlledHost(
488 const CookiePartitionKey& cookie_partition_key,
cfredric09ba72fb2020-12-22 21:03:27489 const GURL& url);
[email protected]63ee33bd2012-03-15 09:29:58490
Lily Chenf068a762019-08-21 21:10:50491 void FilterCookiesWithOptions(const GURL url,
492 const CookieOptions options,
493 std::vector<CanonicalCookie*>* cookie_ptrs,
Ayu Ishiibc6fdb0a2020-06-08 22:59:19494 CookieAccessResultList* included_cookies,
495 CookieAccessResultList* excluded_cookies);
Lily Chenf068a762019-08-21 21:10:50496
Lily Chen4c5f8632019-10-30 18:11:51497 // Possibly delete an existing cookie equivalent to |cookie_being_set| (same
498 // path, domain, and name).
Mike Westc4a777b2017-10-06 14:04:20499 //
Maks Orlovichbd04d782020-11-17 21:23:34500 // |allowed_to_set_secure_cookie| indicates if the source may override
501 // existing secure cookies. If the source is not trustworthy, and there is an
502 // existing "equivalent" cookie that is Secure, that cookie will be preserved,
503 // under "Leave Secure Cookies Alone" (see
Lily Chen4c5f8632019-10-30 18:11:51504 // https://tools.ietf.org/html/draft-ietf-httpbis-cookie-alone-01).
505 // ("equivalent" here is in quotes because the equivalency check for the
506 // purposes of preserving existing Secure cookies is slightly more inclusive.)
507 //
508 // If |skip_httponly| is true, httponly cookies will not be deleted even if
509 // they are equivalent.
510 // |key| is the key to find the cookie in cookies_; see the comment before the
jwwa26e439d2017-01-27 18:17:27511 // CookieMap typedef for details.
Mike Westc4a777b2017-10-06 14:04:20512 //
Lily Chen4c5f8632019-10-30 18:11:51513 // If a cookie is deleted, and its value matches |cookie_being_set|'s value,
514 // then |creation_date_to_inherit| will be set to that cookie's creation date.
Mike Westc4a777b2017-10-06 14:04:20515 //
Lily Chenf53dfbcd2019-08-30 01:42:10516 // The cookie will not be deleted if |*status| is not "include" when calling
517 // the function. The function will update |*status| with exclusion reasons if
518 // a secure cookie was skipped or an httponly cookie was skipped.
519 //
Dylan Cutler0b9a4e962021-09-13 17:34:25520 // If |cookie_partition_it| is nullopt, it will search |cookies_| for
521 // duplicates of |cookie_being_set|. Otherwise, |cookie_partition_it|'s value
522 // is the iterator of the CookieMap in |partitioned_cookies_| we should search
523 // for duplicates.
524 //
[email protected]63ee33bd2012-03-15 09:29:58525 // NOTE: There should never be more than a single matching equivalent cookie.
Lily Chenf53dfbcd2019-08-30 01:42:10526 void MaybeDeleteEquivalentCookieAndUpdateStatus(
Aaron Tagliaboschi29764f52019-02-21 17:19:59527 const std::string& key,
Lily Chen4c5f8632019-10-30 18:11:51528 const CanonicalCookie& cookie_being_set,
Maks Orlovichbd04d782020-11-17 21:23:34529 bool allowed_to_set_secure_cookie,
Aaron Tagliaboschi29764f52019-02-21 17:19:59530 bool skip_httponly,
531 bool already_expired,
Lily Chenf53dfbcd2019-08-30 01:42:10532 base::Time* creation_date_to_inherit,
Dylan Cutler0b9a4e962021-09-13 17:34:25533 CookieInclusionStatus* status,
534 absl::optional<PartitionedCookieMap::iterator> cookie_partition_it);
[email protected]63ee33bd2012-03-15 09:29:58535
Lily Chend8d11db2021-02-25 19:50:52536 // Inserts `cc` into cookies_. Returns an iterator that points to the inserted
537 // cookie in `cookies_`. Guarantee: all iterators to `cookies_` remain valid.
538 // Dispatches the change to `change_dispatcher_` iff `dispatch_change` is
539 // true.
Ayu Ishii9f5e72dc2020-07-22 19:43:18540 CookieMap::iterator InternalInsertCookie(
541 const std::string& key,
542 std::unique_ptr<CanonicalCookie> cc,
543 bool sync_to_store,
Lily Chend8d11db2021-02-25 19:50:52544 const CookieAccessResult& access_result,
545 bool dispatch_change = true);
[email protected]63ee33bd2012-03-15 09:29:58546
Dylan Cutler0b9a4e962021-09-13 17:34:25547 // Returns true if the cookie should be (or is already) synced to the store.
548 // Used for cookies during insertion and deletion into the in-memory store.
549 bool ShouldUpdatePersistentStore(CanonicalCookie* cc);
550
551 void LogCookieTypeToUMA(CanonicalCookie* cc,
552 const CookieAccessResult& access_result);
553
554 // Inserts `cc` into partitioned_cookies_. Should only be used when
555 // cc->IsPartitioned() is true.
556 PartitionedCookieMapIterators InternalInsertPartitionedCookie(
557 std::string key,
558 std::unique_ptr<CanonicalCookie> cc,
559 bool sync_to_store,
560 const CookieAccessResult& access_result,
561 bool dispatch_change = true);
562
rdsmith2709eee2017-06-20 22:43:27563 // Sets all cookies from |list| after deleting any equivalent cookie.
564 // For data gathering purposes, this routine is treated as if it is
565 // restoring saved cookies; some statistics are not gathered in this case.
rdsmithe5c701d2017-07-12 21:50:00566 void SetAllCookies(CookieList list, SetCookiesCallback callback);
drogerd5d1278c2015-03-17 19:21:51567
[email protected]63ee33bd2012-03-15 09:29:58568 void InternalUpdateCookieAccessTime(CanonicalCookie* cc,
569 const base::Time& current_time);
570
571 // |deletion_cause| argument is used for collecting statistics and choosing
Victor Costan14f47c12018-03-01 08:02:24572 // the correct CookieChangeCause for OnCookieChange notifications. Guarantee:
573 // All iterators to cookies_, except for the deleted entry, remain valid.
mkwstbe84af312015-02-20 08:52:45574 void InternalDeleteCookie(CookieMap::iterator it,
575 bool sync_to_store,
[email protected]63ee33bd2012-03-15 09:29:58576 DeletionCause deletion_cause);
577
Dylan Cutler0b9a4e962021-09-13 17:34:25578 // Deletes a Partitioned cookie. Returns true if the deletion operation
579 // resulted in the CookieMap the cookie was stored in was deleted.
580 //
581 // If the CookieMap which contains the deleted cookie only has one entry, then
582 // this function will also delete the CookieMap from PartitionedCookieMap.
583 // This may invalidate the |cookie_partition_it| argument.
584 void InternalDeletePartitionedCookie(
585 PartitionedCookieMap::iterator partition_it,
586 CookieMap::iterator cookie_it,
587 bool sync_to_store,
588 DeletionCause deletion_cause);
589
[email protected]63ee33bd2012-03-15 09:29:58590 // If the number of cookies for CookieMap key |key|, or globally, are
591 // over the preset maximums above, garbage collect, first for the host and
592 // then globally. See comments above garbage collection threshold
Dylan Cutler0b9a4e962021-09-13 17:34:25593 // constants for details. Also removes expired cookies.
[email protected]63ee33bd2012-03-15 09:29:58594 //
595 // Returns the number of cookies deleted (useful for debugging).
jwwa26e439d2017-01-27 18:17:27596 size_t GarbageCollect(const base::Time& current, const std::string& key);
[email protected]63ee33bd2012-03-15 09:29:58597
Dylan Cutler0b9a4e962021-09-13 17:34:25598 // Run garbage collection for PartitionedCookieMap keys |cookie_partition_key|
599 // and |key|.
600 //
601 // Partitioned cookies are subject to different limits than unpartitioned
602 // cookies in order to prevent leaking entropy about user behavior across
603 // cookie partitions.
604 size_t GarbageCollectPartitionedCookies(
605 const base::Time& current,
606 const CookiePartitionKey& cookie_partition_key,
607 const std::string& key);
608
mkwste079ac412016-03-11 09:04:06609 // Helper for GarbageCollect(). Deletes up to |purge_goal| cookies with a
610 // priority less than or equal to |priority| from |cookies|, while ensuring
611 // that at least the |to_protect| most-recent cookies are retained.
jwwc00ac712016-05-05 22:21:44612 // |protected_secure_cookies| specifies whether or not secure cookies should
613 // be protected from deletion.
mkwste079ac412016-03-11 09:04:06614 //
615 // |cookies| must be sorted from least-recent to most-recent.
616 //
mkwste079ac412016-03-11 09:04:06617 // Returns the number of cookies deleted.
618 size_t PurgeLeastRecentMatches(CookieItVector* cookies,
619 CookiePriority priority,
620 size_t to_protect,
jwwc00ac712016-05-05 22:21:44621 size_t purge_goal,
622 bool protect_secure_cookies);
mkwste079ac412016-03-11 09:04:06623
jww82d99c12015-11-25 18:39:53624 // Helper for GarbageCollect(); can be called directly as well. Deletes all
625 // expired cookies in |itpair|. If |cookie_its| is non-NULL, all the
626 // non-expired cookies from |itpair| are appended to |cookie_its|.
[email protected]63ee33bd2012-03-15 09:29:58627 //
628 // Returns the number of cookies deleted.
jww82d99c12015-11-25 18:39:53629 size_t GarbageCollectExpired(const base::Time& current,
630 const CookieMapItPair& itpair,
631 CookieItVector* cookie_its);
632
Dylan Cutler0b9a4e962021-09-13 17:34:25633 // Deletes all expired cookies in the double-keyed PartitionedCookie map in
634 // the CookieMap at |cookie_partition_it|. It deletes all cookies in that
635 // CookieMap in |itpair|. If |cookie_its| is non-NULL, all non-expired cookies
636 // from |itpair| are appended to |cookie_its|.
637 //
638 // Returns the number of cookies deleted.
639 size_t GarbageCollectExpiredPartitionedCookies(
640 const base::Time& current,
641 const PartitionedCookieMap::iterator& cookie_partition_it,
642 const CookieMapItPair& itpair,
643 CookieItVector* cookie_its);
644
645 // Helper function to garbage collect all expired cookies in
646 // PartitionedCookieMap.
647 void GarbageCollectAllExpiredPartitionedCookies(const base::Time& current);
648
[email protected]8ad5d462013-05-02 08:45:26649 // Helper for GarbageCollect(). Deletes all cookies in the range specified by
650 // [|it_begin|, |it_end|). Returns the number of cookies deleted.
jww82d99c12015-11-25 18:39:53651 size_t GarbageCollectDeleteRange(const base::Time& current,
652 DeletionCause cause,
653 CookieItVector::iterator cookie_its_begin,
654 CookieItVector::iterator cookie_its_end);
655
656 // Helper for GarbageCollect(). Deletes cookies in |cookie_its| from least to
657 // most recently used, but only before |safe_date|. Also will stop deleting
658 // when the number of remaining cookies hits |purge_goal|.
mmenkef4721d992017-06-07 17:13:59659 //
660 // Sets |earliest_time| to be the earliest last access time of a cookie that
661 // was not deleted, or base::Time() if no such cookie exists.
jww82d99c12015-11-25 18:39:53662 size_t GarbageCollectLeastRecentlyAccessed(const base::Time& current,
663 const base::Time& safe_date,
664 size_t purge_goal,
mmenkef4721d992017-06-07 17:13:59665 CookieItVector cookie_its,
666 base::Time* earliest_time);
[email protected]63ee33bd2012-03-15 09:29:58667
[email protected]63ee33bd2012-03-15 09:29:58668 bool HasCookieableScheme(const GURL& url);
669
Lily Chenb0eedc22020-10-26 16:34:42670 // Get the cookie's access semantics (LEGACY or NONLEGACY), by checking for a
Lily Chenb0ca3f72019-12-05 18:06:29671 // value from the cookie access delegate, if it is non-null. Otherwise returns
672 // UNKNOWN.
Lily Chen70f997f2019-10-07 22:01:37673 CookieAccessSemantics GetAccessSemanticsForCookie(
674 const CanonicalCookie& cookie) const;
675
[email protected]63ee33bd2012-03-15 09:29:58676 // Statistics support
677
678 // This function should be called repeatedly, and will record
679 // statistics if a sufficient time period has passed.
680 void RecordPeriodicStats(const base::Time& current_time);
681
Lily Chen229623d72020-06-01 17:20:14682 // Records the aforementioned stats if we have already finished loading all
683 // cookies. Returns whether stats were recorded.
684 bool DoRecordPeriodicStats();
685
cfredric326a0bc2022-01-12 18:51:30686 // Records periodic stats related to First-Party Sets usage. Note that since
687 // First-Party Sets presents a potentially asynchronous interface, these stats
688 // may be collected asynchronously w.r.t. the rest of the stats collected by
689 // `RecordPeriodicStats`.
cfredric326a0bc2022-01-12 18:51:30690 void RecordPeriodicFirstPartySetsStats(
Chris Fredricksonc2efa96f2022-08-04 20:40:44691 base::flat_map<SchemefulSite, FirstPartySetEntry> sets) const;
cfredric326a0bc2022-01-12 18:51:30692
Maks Orlovich323efaf2018-03-06 02:56:39693 // Defers the callback until the full coookie database has been loaded. If
694 // it's already been loaded, runs the callback synchronously.
rdsmithe5c701d2017-07-12 21:50:00695 void DoCookieCallback(base::OnceClosure callback);
[email protected]63ee33bd2012-03-15 09:29:58696
Maks Orlovich323efaf2018-03-06 02:56:39697 // Defers the callback until the cookies relevant to given URL have been
698 // loaded. If they've already been loaded, runs the callback synchronously.
rdsmithe5c701d2017-07-12 21:50:00699 void DoCookieCallbackForURL(base::OnceClosure callback, const GURL& url);
[email protected]63ee33bd2012-03-15 09:29:58700
Maks Orlovich323efaf2018-03-06 02:56:39701 // Defers the callback until the cookies relevant to given host or domain
702 // have been loaded. If they've already been loaded, runs the callback
703 // synchronously.
704 void DoCookieCallbackForHostOrDomain(base::OnceClosure callback,
705 base::StringPiece host_or_domain);
706
Steven Bingler0420a3752020-11-20 21:40:48707 // Checks to see if a cookie is being sent to the same port it was set by. For
708 // metrics.
709 //
710 // This is in CookieMonster because only CookieMonster uses it. It's otherwise
711 // a standalone utility function.
712 static CookieSentToSamePort IsCookieSentToSamePortThatSetIt(
713 const GURL& destination,
714 int source_port,
715 CookieSourceScheme source_scheme);
716
Dylan Cutler03d2c76c2022-02-18 02:23:15717 // TODO(crbug.com/1296161): Delete this when the partitioned cookies Origin
718 // Trial ends.
719 void OnConvertPartitionedCookiesToUnpartitioned(const GURL& url);
720 void ConvertPartitionedCookie(const net::CanonicalCookie& cookie,
721 const GURL& url);
722
Lily Chen229623d72020-06-01 17:20:14723 // Set of keys (eTLD+1's) for which non-expired cookies have
724 // been evicted for hitting the per-domain max. The size of this set is
725 // histogrammed periodically. The size is limited to |kMaxDomainPurgedKeys|.
Chris Fredrickson4f22b3262022-09-30 19:57:30726 std::set<std::string> domain_purged_keys_ GUARDED_BY_CONTEXT(thread_checker_);
Lily Chen229623d72020-06-01 17:20:14727
728 // The number of distinct keys (eTLD+1's) currently present in the |cookies_|
729 // multimap. This is histogrammed periodically.
Tsuyoshi Horo432981d52022-06-09 09:50:13730 size_t num_keys_ = 0u;
Lily Chen229623d72020-06-01 17:20:14731
Chris Fredrickson4f22b3262022-09-30 19:57:30732 CookieMap cookies_ GUARDED_BY_CONTEXT(thread_checker_);
[email protected]63ee33bd2012-03-15 09:29:58733
Chris Fredrickson4f22b3262022-09-30 19:57:30734 PartitionedCookieMap partitioned_cookies_ GUARDED_BY_CONTEXT(thread_checker_);
Dylan Cutler0b9a4e962021-09-13 17:34:25735
736 // Number of distinct partitioned cookies globally. This is used to enforce a
737 // global maximum on the number of partitioned cookies.
Tsuyoshi Horo432981d52022-06-09 09:50:13738 size_t num_partitioned_cookies_ = 0u;
Dylan Cutler0b9a4e962021-09-13 17:34:25739
Chris Fredricksonb4cf8402022-09-23 22:59:59740 bool same_party_attribute_enabled_ = false;
741
Victor Costan14f47c12018-03-01 08:02:24742 CookieMonsterChangeDispatcher change_dispatcher_;
743
erikchen1dd72a72015-05-06 20:45:05744 // Indicates whether the cookie store has been initialized.
Tsuyoshi Horo432981d52022-06-09 09:50:13745 bool initialized_ = false;
[email protected]63ee33bd2012-03-15 09:29:58746
erikchen1dd72a72015-05-06 20:45:05747 // Indicates whether the cookie store has started fetching all cookies.
Tsuyoshi Horo432981d52022-06-09 09:50:13748 bool started_fetching_all_cookies_ = false;
erikchen1dd72a72015-05-06 20:45:05749 // Indicates whether the cookie store has finished fetching all cookies.
Tsuyoshi Horo432981d52022-06-09 09:50:13750 bool finished_fetching_all_cookies_ = false;
[email protected]63ee33bd2012-03-15 09:29:58751
752 // List of domain keys that have been loaded from the DB.
753 std::set<std::string> keys_loaded_;
754
755 // Map of domain keys to their associated task queues. These tasks are blocked
756 // until all cookies for the associated domain key eTLD+1 are loaded from the
757 // backend store.
Brett Wilsonc6a0c822017-09-12 00:04:29758 std::map<std::string, base::circular_deque<base::OnceClosure>>
Chris Fredrickson4f22b3262022-09-30 19:57:30759 tasks_pending_for_key_ GUARDED_BY_CONTEXT(thread_checker_);
[email protected]63ee33bd2012-03-15 09:29:58760
761 // Queues tasks that are blocked until all cookies are loaded from the backend
762 // store.
Chris Fredrickson4f22b3262022-09-30 19:57:30763 base::circular_deque<base::OnceClosure> tasks_pending_
764 GUARDED_BY_CONTEXT(thread_checker_);
mmenkef49fca0e2016-03-08 12:46:24765
766 // Once a global cookie task has been seen, all per-key tasks must be put in
767 // |tasks_pending_| instead of |tasks_pending_for_key_| to ensure a reasonable
rdsmithe5c701d2017-07-12 21:50:00768 // view of the cookie store. This is more to ensure fancy cookie export/import
mmenkef49fca0e2016-03-08 12:46:24769 // code has a consistent view of the CookieStore, rather than out of concern
770 // for typical use.
Tsuyoshi Horo432981d52022-06-09 09:50:13771 bool seen_global_task_ = false;
[email protected]63ee33bd2012-03-15 09:29:58772
Helen Licd0fab862018-08-13 16:07:53773 NetLogWithSource net_log_;
774
[email protected]63ee33bd2012-03-15 09:29:58775 scoped_refptr<PersistentCookieStore> store_;
776
[email protected]63ee33bd2012-03-15 09:29:58777 // Minimum delay after updating a cookie's LastAccessDate before we will
778 // update it again.
779 const base::TimeDelta last_access_threshold_;
780
781 // Approximate date of access time of least recently accessed cookie
782 // in |cookies_|. Note that this is not guaranteed to be accurate, only a)
783 // to be before or equal to the actual time, and b) to be accurate
mmenkef4721d992017-06-07 17:13:59784 // immediately after a garbage collection that scans through all the cookies
785 // (When garbage collection does not scan through all cookies, it may not be
786 // updated). This value is used to determine whether global garbage collection
787 // might find cookies to purge. Note: The default Time() constructor will
788 // create a value that compares earlier than any other time value, which is
789 // wanted. Thus this value is not initialized.
[email protected]63ee33bd2012-03-15 09:29:58790 base::Time earliest_access_time_;
791
[email protected]63ee33bd2012-03-15 09:29:58792 std::vector<std::string> cookieable_schemes_;
793
[email protected]63ee33bd2012-03-15 09:29:58794 base::Time last_statistic_record_time_;
795
Tsuyoshi Horo432981d52022-06-09 09:50:13796 bool persist_session_cookies_ = false;
[email protected]63ee33bd2012-03-15 09:29:58797
Chris Fredrickson4f22b3262022-09-30 19:57:30798 THREAD_CHECKER(thread_checker_);
mmenkebe0910d2016-03-01 19:09:09799
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_