blob: 5828ff23cdb25b684e79d2997ff48bff491410cf [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>
17#include <utility>
18#include <vector>
19
[email protected]63ee33bd2012-03-15 09:29:5820#include "base/callback_forward.h"
Brett Wilsonc6a0c822017-09-12 00:04:2921#include "base/containers/circular_deque.h"
[email protected]63ee33bd2012-03-15 09:29:5822#include "base/gtest_prod_util.h"
Avi Drissman13fc8932015-12-20 04:40:4623#include "base/macros.h"
[email protected]63ee33bd2012-03-15 09:29:5824#include "base/memory/ref_counted.h"
mmenkebe0910d2016-03-01 19:09:0925#include "base/memory/weak_ptr.h"
Maks Orlovich323efaf2018-03-06 02:56:3926#include "base/strings/string_piece.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"
[email protected]8da4b1812012-07-25 13:54:3830#include "net/cookies/canonical_cookie.h"
Lily Chenab36a112019-09-19 20:17:2831#include "net/cookies/cookie_access_delegate.h"
[email protected]ab2d75c82013-04-19 18:39:0432#include "net/cookies/cookie_constants.h"
Jihwan Marc Kim3e132f12020-05-20 17:33:1933#include "net/cookies/cookie_inclusion_status.h"
Victor Costan14f47c12018-03-01 08:02:2434#include "net/cookies/cookie_monster_change_dispatcher.h"
[email protected]63ee33bd2012-03-15 09:29:5835#include "net/cookies/cookie_store.h"
Helen Licd0fab862018-08-13 16:07:5336#include "net/log/net_log_with_source.h"
ellyjones399e35a22014-10-27 11:09:5637#include "url/gurl.h"
[email protected]63ee33bd2012-03-15 09:29:5838
[email protected]63ee33bd2012-03-15 09:29:5839namespace net {
40
Victor Costan14f47c12018-03-01 08:02:2441class CookieChangeDispatcher;
[email protected]63ee33bd2012-03-15 09:29:5842
43// The cookie monster is the system for storing and retrieving cookies. It has
44// an in-memory list of all cookies, and synchronizes non-session cookies to an
45// optional permanent storage that implements the PersistentCookieStore
46// interface.
47//
mmenke96f3bab2016-01-22 17:34:0248// Tasks may be deferred if all affected cookies are not yet loaded from the
49// backing store. Otherwise, callbacks may be invoked immediately.
[email protected]63ee33bd2012-03-15 09:29:5850//
51// A cookie task is either pending loading of the entire cookie store, or
Maks Orlovich323efaf2018-03-06 02:56:3952// loading of cookies for a specific domain key (GetKey(), roughly eTLD+1). In
53// the former case, the cookie callback will be queued in tasks_pending_ while
54// PersistentCookieStore chain loads the cookie store on DB thread. In the
55// latter case, the cookie callback will be queued in tasks_pending_for_key_
56// while PermanentCookieStore loads cookies for the specified domain key on DB
57// thread.
[email protected]63ee33bd2012-03-15 09:29:5858class NET_EXPORT CookieMonster : public CookieStore {
59 public:
[email protected]63ee33bd2012-03-15 09:29:5860 class PersistentCookieStore;
61
62 // Terminology:
63 // * The 'top level domain' (TLD) of an internet domain name is
64 // the terminal "." free substring (e.g. "com" for google.com
65 // or world.std.com).
66 // * The 'effective top level domain' (eTLD) is the longest
67 // "." initiated terminal substring of an internet domain name
68 // that is controlled by a general domain registrar.
69 // (e.g. "co.uk" for news.bbc.co.uk).
70 // * The 'effective top level domain plus one' (eTLD+1) is the
71 // shortest "." delimited terminal substring of an internet
72 // domain name that is not controlled by a general domain
73 // registrar (e.g. "bbc.co.uk" for news.bbc.co.uk, or
74 // "google.com" for news.google.com). The general assumption
75 // is that all hosts and domains under an eTLD+1 share some
76 // administrative control.
77
78 // CookieMap is the central data structure of the CookieMonster. It
79 // is a map whose values are pointers to CanonicalCookie data
80 // structures (the data structures are owned by the CookieMonster
81 // and must be destroyed when removed from the map). The key is based on the
82 // effective domain of the cookies. If the domain of the cookie has an
83 // eTLD+1, that is the key for the map. If the domain of the cookie does not
84 // have an eTLD+1, the key of the map is the host the cookie applies to (it is
85 // not legal to have domain cookies without an eTLD+1). This rule
86 // excludes cookies for, e.g, ".com", ".co.uk", or ".internalnetwork".
87 // This behavior is the same as the behavior in Firefox v 3.6.10.
Dylan Cutler0b9a4e962021-09-13 17:34:2588 // CookieMap does not store cookies that were set with the Partitioned
89 // attribute, those are stored in PartitionedCookieMap.
[email protected]63ee33bd2012-03-15 09:29:5890
91 // NOTE(deanm):
92 // I benchmarked hash_multimap vs multimap. We're going to be query-heavy
93 // so it would seem like hashing would help. However they were very
94 // close, with multimap being a tiny bit faster. I think this is because
95 // our map is at max around 1000 entries, and the additional complexity
96 // for the hashing might not overcome the O(log(1000)) for querying
97 // a multimap. Also, multimap is standard, another reason to use it.
98 // TODO(rdsmith): This benchmark should be re-done now that we're allowing
avie7cd11a2016-10-11 02:00:3599 // substantially more entries in the map.
100 using CookieMap =
101 std::multimap<std::string, std::unique_ptr<CanonicalCookie>>;
102 using CookieMapItPair = std::pair<CookieMap::iterator, CookieMap::iterator>;
103 using CookieItVector = std::vector<CookieMap::iterator>;
[email protected]8ad5d462013-05-02 08:45:26104
Dylan Cutler0b9a4e962021-09-13 17:34:25105 // PartitionedCookieMap only stores cookies that were set with the Partitioned
106 // attribute. The map is double-keyed on cookie's partition key and
107 // the cookie's effective domain of the cookie (the key of CookieMap).
108 // We store partitioned cookies in a separate map so that the queries for a
109 // request's unpartitioned and partitioned cookies will both be more
110 // efficient (since querying two smaller maps is more efficient that querying
111 // one larger map twice).
112 using PartitionedCookieMap =
113 std::map<CookiePartitionKey, std::unique_ptr<CookieMap>>;
114 using PartitionedCookieMapIterators =
115 std::pair<PartitionedCookieMap::iterator, CookieMap::iterator>;
116
[email protected]8ad5d462013-05-02 08:45:26117 // Cookie garbage collection thresholds. Based off of the Mozilla defaults.
118 // When the number of cookies gets to k{Domain,}MaxCookies
119 // purge down to k{Domain,}MaxCookies - k{Domain,}PurgeCookies.
120 // It might seem scary to have a high purge value, but really it's not.
121 // You just make sure that you increase the max to cover the increase
122 // in purge, and we would have been purging the same number of cookies.
123 // We're just going through the garbage collection process less often.
124 // Note that the DOMAIN values are per eTLD+1; see comment for the
125 // CookieMap typedef. So, e.g., the maximum number of cookies allowed for
126 // google.com and all of its subdomains will be 150-180.
127 //
128 // Any cookies accessed more recently than kSafeFromGlobalPurgeDays will not
129 // be evicted by global garbage collection, even if we have more than
130 // kMaxCookies. This does not affect domain garbage collection.
131 static const size_t kDomainMaxCookies;
132 static const size_t kDomainPurgeCookies;
133 static const size_t kMaxCookies;
134 static const size_t kPurgeCookies;
135
Lily Chen229623d72020-06-01 17:20:14136 // Max number of keys to store for domains that have been purged.
137 static const size_t kMaxDomainPurgedKeys;
138
Dylan Cutler0b9a4e962021-09-13 17:34:25139 // Partitioned cookie garbage collection thresholds.
140 static const size_t kPerPartitionDomainMaxCookies;
141 // TODO(crbug.com/1225444): Add global limit to number of partitioned cookies.
142
[email protected]8ad5d462013-05-02 08:45:26143 // Quota for cookies with {low, medium, high} priorities within a domain.
mkwst87734352016-03-03 17:36:23144 static const size_t kDomainCookiesQuotaLow;
145 static const size_t kDomainCookiesQuotaMedium;
146 static const size_t kDomainCookiesQuotaHigh;
[email protected]63ee33bd2012-03-15 09:29:58147
Matt Menke477ab632019-06-27 23:12:17148 // The number of days since last access that cookies will not be subject
149 // to global garbage collection.
150 static const int kSafeFromGlobalPurgeDays;
151
[email protected]63ee33bd2012-03-15 09:29:58152 // The store passed in should not have had Init() called on it yet. This
153 // class will take care of initializing it. The backing store is NOT owned by
154 // this class, but it must remain valid for the duration of the cookie
155 // monster's existence. If |store| is NULL, then no backing store will be
Nick Harper57142b1c2019-03-14 21:03:59156 // updated. |net_log| must outlive the CookieMonster and can be null.
Pritam8354cf702018-03-10 08:55:41157 CookieMonster(scoped_refptr<PersistentCookieStore> store,
Helen Lifb313a92018-08-14 15:46:44158 NetLog* net_log);
nharper2b0ad9a2017-05-22 18:33:45159
[email protected]63ee33bd2012-03-15 09:29:58160 // Only used during unit testing.
Helen Lifb313a92018-08-14 15:46:44161 // |net_log| must outlive the CookieMonster.
Pritam8354cf702018-03-10 08:55:41162 CookieMonster(scoped_refptr<PersistentCookieStore> store,
Helen Lifb313a92018-08-14 15:46:44163 base::TimeDelta last_access_threshold,
164 NetLog* net_log);
[email protected]63ee33bd2012-03-15 09:29:58165
Peter Boström293b1342021-09-22 17:31:43166 CookieMonster(const CookieMonster&) = delete;
167 CookieMonster& operator=(const CookieMonster&) = delete;
168
mmenke606c59c2016-03-07 18:20:55169 ~CookieMonster() override;
170
rdsmith0e84cea2017-07-13 03:09:53171 // Writes all the cookies in |list| into the store, replacing all cookies
172 // currently present in store.
rdsmith2709eee2017-06-20 22:43:27173 // This method does not flush the backend.
174 // TODO(rdsmith, mmenke): Do not use this function; it is deprecated
175 // and should be removed.
176 // See https://codereview.chromium.org/2882063002/#msg64.
rdsmith7ac81712017-06-22 17:09:54177 void SetAllCookiesAsync(const CookieList& list, SetCookiesCallback callback);
drogerd5d1278c2015-03-17 19:21:51178
[email protected]63ee33bd2012-03-15 09:29:58179 // CookieStore implementation.
rdsmitha6ce4442017-06-21 17:11:05180 void SetCanonicalCookieAsync(std::unique_ptr<CanonicalCookie> cookie,
Lily Chen96f29a132020-04-15 17:59:36181 const GURL& source_url,
Maks Orlovichfdbc8be2019-03-18 18:34:52182 const CookieOptions& options,
rdsmith7ac81712017-06-22 17:09:54183 SetCookiesCallback callback) override;
Dylan Cutlercd2d8932021-10-05 19:03:43184 void GetCookieListWithOptionsAsync(const GURL& url,
185 const CookieOptions& options,
186 const CookiePartitionKeychain& s,
187 GetCookieListCallback callback) override;
Lily Chenf068a762019-08-21 21:10:50188 void GetAllCookiesAsync(GetAllCookiesCallback callback) override;
Lily Chene2e9ae012019-10-09 20:02:54189 void GetAllCookiesWithAccessSemanticsAsync(
190 GetAllCookiesWithAccessSemanticsCallback callback) override;
mmenke24379d52016-02-05 23:50:17191 void DeleteCanonicalCookieAsync(const CanonicalCookie& cookie,
rdsmith7ac81712017-06-22 17:09:54192 DeleteCallback callback) override;
Chris Mumfordd8ed9f82018-05-01 15:43:13193 void DeleteAllCreatedInTimeRangeAsync(
194 const CookieDeletionInfo::TimeRange& creation_range,
195 DeleteCallback callback) override;
Chris Mumford800caa62018-04-20 19:34:44196 void DeleteAllMatchingInfoAsync(CookieDeletionInfo delete_info,
197 DeleteCallback callback) override;
Christian Dullweberff11c452021-05-12 17:04:45198 void DeleteSessionCookiesAsync(DeleteCallback callback) override;
199 void DeleteMatchingCookiesAsync(DeletePredicate predicate,
200 DeleteCallback callback) override;
rdsmith7ac81712017-06-22 17:09:54201 void FlushStore(base::OnceClosure callback) override;
mmenkeded79da2016-02-06 08:28:51202 void SetForceKeepSessionState() override;
Victor Costan14f47c12018-03-01 08:02:24203 CookieChangeDispatcher& GetChangeDispatcher() override;
Nate Fischerc6fb6cf2019-03-27 00:39:49204 void SetCookieableSchemes(const std::vector<std::string>& schemes,
205 SetCookieableSchemesCallback callback) override;
mmenke74bcbd52016-01-21 17:17:56206
[email protected]63ee33bd2012-03-15 09:29:58207 // Enables writing session cookies into the cookie database. If this this
208 // method is called, it must be called before first use of the instance
209 // (i.e. as part of the instance initialization process).
210 void SetPersistSessionCookies(bool persist_session_cookies);
211
[email protected]63ee33bd2012-03-15 09:29:58212 // The default list of schemes the cookie monster can handle.
[email protected]5edff3c52014-06-23 20:27:48213 static const char* const kDefaultCookieableSchemes[];
[email protected]63ee33bd2012-03-15 09:29:58214 static const int kDefaultCookieableSchemesCount;
215
Maks Orlovich323efaf2018-03-06 02:56:39216 // Find a key based on the given domain, which will be used to find all
217 // cookies potentially relevant to it. This is used for lookup in cookies_ as
218 // well as for PersistentCookieStore::LoadCookiesForKey. See comment on keys
219 // before the CookieMap typedef.
220 static std::string GetKey(base::StringPiece domain);
221
cfredric59f8a8452021-06-08 15:27:11222 // Exposes the comparison function used when sorting cookies.
223 static bool CookieSorter(const CanonicalCookie* cc1,
224 const CanonicalCookie* cc2);
225
Lily Chen229623d72020-06-01 17:20:14226 // Triggers immediate recording of stats that are typically reported
227 // periodically.
228 bool DoRecordPeriodicStatsForTesting() { return DoRecordPeriodicStats(); }
229
[email protected]63ee33bd2012-03-15 09:29:58230 private:
avie7cd11a2016-10-11 02:00:35231 // For garbage collection constants.
[email protected]63ee33bd2012-03-15 09:29:58232 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestHostGarbageCollection);
mmenkef4721d992017-06-07 17:13:59233 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest,
234 GarbageCollectWithSecureCookiesOnly);
[email protected]63ee33bd2012-03-15 09:29:58235 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestGCTimes);
236
237 // For validation of key values.
238 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestDomainTree);
239 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestImport);
240 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, GetKey);
241 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestGetKey);
242
243 // For FindCookiesForKey.
244 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, ShortLivedSessionCookies);
245
estark7feb65c2b2015-08-21 23:38:20246 // For CookieSource histogram enum.
247 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, CookieSourceHistogram);
248
jww31e32632015-12-16 23:38:34249 // For kSafeFromGlobalPurgeDays in CookieStore.
jwwa26e439d2017-01-27 18:17:27250 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, EvictSecureCookies);
jww82d99c12015-11-25 18:39:53251
jww31e32632015-12-16 23:38:34252 // For CookieDeleteEquivalent histogram enum.
253 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest,
254 CookieDeleteEquivalentHistogramTest);
jww31e32632015-12-16 23:38:34255
Steven Bingler0420a3752020-11-20 21:40:48256 // For CookieSentToSamePort enum.
257 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest,
258 CookiePortReadDiffersFromSetHistogram);
259 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, IsCookieSentToSamePortThatSetIt);
260
[email protected]63ee33bd2012-03-15 09:29:58261 // Internal reasons for deletion, used to populate informative histograms
262 // and to provide a public cause for onCookieChange notifications.
263 //
264 // If you add or remove causes from this list, please be sure to also update
Victor Costan14f47c12018-03-01 08:02:24265 // the CookieChangeCause mapping inside ChangeCauseMapping. New items (if
266 // necessary) should be added at the end of the list, just before
Nick Harper7a6683a2018-01-30 20:42:52267 // DELETE_COOKIE_LAST_ENTRY.
[email protected]63ee33bd2012-03-15 09:29:58268 enum DeletionCause {
269 DELETE_COOKIE_EXPLICIT = 0,
mkwstaa07ee82016-03-11 15:32:14270 DELETE_COOKIE_OVERWRITE = 1,
271 DELETE_COOKIE_EXPIRED = 2,
272 DELETE_COOKIE_EVICTED = 3,
273 DELETE_COOKIE_DUPLICATE_IN_BACKING_STORE = 4,
274 DELETE_COOKIE_DONT_RECORD = 5, // For final cleanup after flush to store.
[email protected]63ee33bd2012-03-15 09:29:58275
mkwstaa07ee82016-03-11 15:32:14276 // Cookies evicted during domain-level garbage collection.
277 DELETE_COOKIE_EVICTED_DOMAIN = 6,
[email protected]63ee33bd2012-03-15 09:29:58278
Dylan Cutler0b9a4e962021-09-13 17:34:25279 // Cookies evicted during global garbage collection, which takes place after
mkwstaa07ee82016-03-11 15:32:14280 // domain-level garbage collection fails to bring the cookie store under
281 // the overall quota.
282 DELETE_COOKIE_EVICTED_GLOBAL = 7,
283
284 // #8 was DELETE_COOKIE_EVICTED_DOMAIN_PRE_SAFE
285 // #9 was DELETE_COOKIE_EVICTED_DOMAIN_POST_SAFE
[email protected]63ee33bd2012-03-15 09:29:58286
287 // A common idiom is to remove a cookie by overwriting it with an
288 // already-expired expiration date. This captures that case.
mkwstaa07ee82016-03-11 15:32:14289 DELETE_COOKIE_EXPIRED_OVERWRITE = 10,
[email protected]63ee33bd2012-03-15 09:29:58290
[email protected]6210ce52013-09-20 03:33:14291 // Cookies are not allowed to contain control characters in the name or
292 // value. However, we used to allow them, so we are now evicting any such
293 // cookies as we load them. See http://crbug.com/238041.
mkwstaa07ee82016-03-11 15:32:14294 DELETE_COOKIE_CONTROL_CHAR = 11,
[email protected]6210ce52013-09-20 03:33:14295
jww82d99c12015-11-25 18:39:53296 // When strict secure cookies is enabled, non-secure cookies are evicted
297 // right after expired cookies.
mkwstaa07ee82016-03-11 15:32:14298 DELETE_COOKIE_NON_SECURE = 12,
jww82d99c12015-11-25 18:39:53299
Dylan Cutler0b9a4e962021-09-13 17:34:25300 // Partitioned cookies evicted during per-partition domain-level garbage
301 // collection.
302 DELETE_COOKIE_EVICTED_PER_PARTITION_DOMAIN = 13,
303
304 DELETE_COOKIE_LAST_ENTRY = 14,
[email protected]63ee33bd2012-03-15 09:29:58305 };
306
mkwstc1aa4cc2015-04-03 19:57:45307 // This enum is used to generate a histogramed bitmask measureing the types
308 // of stored cookies. Please do not reorder the list when adding new entries.
309 // New items MUST be added at the end of the list, just before
310 // COOKIE_TYPE_LAST_ENTRY;
Lily Chenda465cca2021-03-08 23:47:17311 // There will be 2^COOKIE_TYPE_LAST_ENTRY buckets in the linear histogram.
mkwstc1aa4cc2015-04-03 19:57:45312 enum CookieType {
mkwst46549412016-02-01 10:05:37313 COOKIE_TYPE_SAME_SITE = 0,
mkwstc1aa4cc2015-04-03 19:57:45314 COOKIE_TYPE_HTTPONLY,
315 COOKIE_TYPE_SECURE,
316 COOKIE_TYPE_LAST_ENTRY
317 };
318
estark7feb65c2b2015-08-21 23:38:20319 // Used to populate a histogram containing information about the
320 // sources of Secure and non-Secure cookies: that is, whether such
321 // cookies are set by origins with cryptographic or non-cryptographic
322 // schemes. Please do not reorder the list when adding new
Lily Chenda465cca2021-03-08 23:47:17323 // entries. New items MUST be added at the end of the list, and kMaxValue
324 // should be updated to the last value.
estark7feb65c2b2015-08-21 23:38:20325 //
326 // COOKIE_SOURCE_(NON)SECURE_COOKIE_(NON)CRYPTOGRAPHIC_SCHEME means
327 // that a cookie was set or overwritten from a URL with the given type
328 // of scheme. This enum should not be used when cookies are *cleared*,
329 // because its purpose is to understand if Chrome can deprecate the
330 // ability of HTTP urls to set/overwrite Secure cookies.
331 enum CookieSource {
332 COOKIE_SOURCE_SECURE_COOKIE_CRYPTOGRAPHIC_SCHEME = 0,
333 COOKIE_SOURCE_SECURE_COOKIE_NONCRYPTOGRAPHIC_SCHEME,
334 COOKIE_SOURCE_NONSECURE_COOKIE_CRYPTOGRAPHIC_SCHEME,
335 COOKIE_SOURCE_NONSECURE_COOKIE_NONCRYPTOGRAPHIC_SCHEME,
Lily Chenda465cca2021-03-08 23:47:17336 kMaxValue = COOKIE_SOURCE_NONSECURE_COOKIE_NONCRYPTOGRAPHIC_SCHEME
estark7feb65c2b2015-08-21 23:38:20337 };
338
Steven Bingler0420a3752020-11-20 21:40:48339 // Enum for collecting metrics on how frequently a cookie is sent to the same
340 // port it was set by.
341 //
342 // kNoButDefault exists because we expect for cookies being sent between
343 // schemes to have a port mismatch and want to separate those out from other,
344 // more interesting, cases.
345 //
346 // Do not reorder or renumber. Used for metrics.
347 enum class CookieSentToSamePort {
348 kSourcePortUnspecified = 0, // Cookie's source port is unspecified, we
349 // can't know if this is the same port or not.
350 kInvalid = 1, // The source port was corrupted to be PORT_INVALID, we
351 // can't know if this is the same port or not.
352 kNo = 2, // Source port and destination port are different.
353 kNoButDefault =
354 3, // Source and destination ports are different but they're
355 // the defaults for their scheme. This can mean that an http
356 // cookie was sent to a https origin or vice-versa.
357 kYes = 4, // They're the same.
358 kMaxValue = kYes
359 };
360
[email protected]63ee33bd2012-03-15 09:29:58361 // Record statistics every kRecordStatisticsIntervalSeconds of uptime.
362 static const int kRecordStatisticsIntervalSeconds = 10 * 60;
363
rdsmitha6ce4442017-06-21 17:11:05364 // Sets a canonical cookie, deletes equivalents and performs garbage
Lily Chen96f29a132020-04-15 17:59:36365 // collection. |source_url| indicates what URL the cookie is being set
Maks Orlovichfdbc8be2019-03-18 18:34:52366 // from; secure cookies cannot be altered from insecure schemes, and some
367 // schemes may not be authorized.
368 //
369 // |options| indicates if this setting operation is allowed
370 // to affect http_only or same-site cookies.
rdsmithe5c701d2017-07-12 21:50:00371 void SetCanonicalCookie(std::unique_ptr<CanonicalCookie> cookie,
Lily Chen96f29a132020-04-15 17:59:36372 const GURL& source_url,
Maks Orlovichfdbc8be2019-03-18 18:34:52373 const CookieOptions& options,
rdsmithe5c701d2017-07-12 21:50:00374 SetCookiesCallback callback);
rdsmitha6ce4442017-06-21 17:11:05375
Lily Chenf068a762019-08-21 21:10:50376 void GetAllCookies(GetAllCookiesCallback callback);
[email protected]63ee33bd2012-03-15 09:29:58377
Lily Chene2e9ae012019-10-09 20:02:54378 void AttachAccessSemanticsListForCookieList(
379 GetAllCookiesWithAccessSemanticsCallback callback,
380 const CookieList& cookie_list);
381
Dylan Cutler0b9a4e962021-09-13 17:34:25382 void GetCookieListWithOptions(
383 const GURL& url,
384 const CookieOptions& options,
Dylan Cutlercd2d8932021-10-05 19:03:43385 const CookiePartitionKeychain& cookie_partition_keychain,
Dylan Cutler0b9a4e962021-09-13 17:34:25386 GetCookieListCallback callback);
[email protected]63ee33bd2012-03-15 09:29:58387
Chris Mumfordd8ed9f82018-05-01 15:43:13388 void DeleteAllCreatedInTimeRange(
389 const CookieDeletionInfo::TimeRange& creation_range,
390 DeleteCallback callback);
[email protected]63ee33bd2012-03-15 09:29:58391
Christian Dullweberff11c452021-05-12 17:04:45392 // Returns whether |cookie| matches |delete_info|.
393 bool MatchCookieDeletionInfo(const CookieDeletionInfo& delete_info,
394 const net::CanonicalCookie& cookie);
[email protected]63ee33bd2012-03-15 09:29:58395
rdsmithe5c701d2017-07-12 21:50:00396 void DeleteCanonicalCookie(const CanonicalCookie& cookie,
397 DeleteCallback callback);
mmenke24379d52016-02-05 23:50:17398
Christian Dullweberff11c452021-05-12 17:04:45399 void DeleteMatchingCookies(DeletePredicate predicate,
400 DeletionCause cause,
401 DeleteCallback callback);
[email protected]264807b2012-04-25 14:49:37402
erikchen1dd72a72015-05-06 20:45:05403 // The first access to the cookie store initializes it. This method should be
404 // called before any access to the cookie store.
405 void MarkCookieStoreAsInitialized();
[email protected]63ee33bd2012-03-15 09:29:58406
erikchen1dd72a72015-05-06 20:45:05407 // Fetches all cookies if the backing store exists and they're not already
408 // being fetched.
erikchen1dd72a72015-05-06 20:45:05409 void FetchAllCookiesIfNecessary();
410
411 // Fetches all cookies from the backing store.
erikchen1dd72a72015-05-06 20:45:05412 void FetchAllCookies();
413
414 // Whether all cookies should be fetched as soon as any is requested.
415 bool ShouldFetchAllCookiesWhenFetchingAnyCookie();
[email protected]63ee33bd2012-03-15 09:29:58416
417 // Stores cookies loaded from the backing store and invokes any deferred
418 // calls. |beginning_time| should be the moment PersistentCookieStore::Load
419 // was invoked and is used for reporting histogram_time_blocked_on_load_.
420 // See PersistentCookieStore::Load for details on the contents of cookies.
421 void OnLoaded(base::TimeTicks beginning_time,
avie7cd11a2016-10-11 02:00:35422 std::vector<std::unique_ptr<CanonicalCookie>> cookies);
[email protected]63ee33bd2012-03-15 09:29:58423
424 // Stores cookies loaded from the backing store and invokes the deferred
425 // task(s) pending loading of cookies associated with the domain key
Maks Orlovich323efaf2018-03-06 02:56:39426 // (GetKey, roughly eTLD+1). Called when all cookies for the domain key have
427 // been loaded from DB. See PersistentCookieStore::Load for details on the
428 // contents of cookies.
mkwstbe84af312015-02-20 08:52:45429 void OnKeyLoaded(const std::string& key,
avie7cd11a2016-10-11 02:00:35430 std::vector<std::unique_ptr<CanonicalCookie>> cookies);
[email protected]63ee33bd2012-03-15 09:29:58431
432 // Stores the loaded cookies.
avie7cd11a2016-10-11 02:00:35433 void StoreLoadedCookies(
434 std::vector<std::unique_ptr<CanonicalCookie>> cookies);
[email protected]63ee33bd2012-03-15 09:29:58435
436 // Invokes deferred calls.
437 void InvokeQueue();
438
439 // Checks that |cookies_| matches our invariants, and tries to repair any
440 // inconsistencies. (In other words, it does not have duplicate cookies).
441 void EnsureCookiesMapIsValid();
442
443 // Checks for any duplicate cookies for CookieMap key |key| which lie between
444 // |begin| and |end|. If any are found, all but the most recent are deleted.
Dylan Cutler0b9a4e962021-09-13 17:34:25445 //
446 // If |cookie_partition_it| is not nullopt, then this function trims cookies
447 // from the CookieMap in |partitioned_cookies_| at |cookie_partition_it|
448 // instead of trimming cookies from |cookies_|.
449 void TrimDuplicateCookiesForKey(
450 const std::string& key,
451 CookieMap::iterator begin,
452 CookieMap::iterator end,
453 absl::optional<PartitionedCookieMap::iterator> cookie_partition_it);
[email protected]63ee33bd2012-03-15 09:29:58454
455 void SetDefaultCookieableSchemes();
456
cfredric09ba72fb2020-12-22 21:03:27457 std::vector<CanonicalCookie*> FindCookiesForRegistryControlledHost(
Dylan Cutler0b9a4e962021-09-13 17:34:25458 const GURL& url,
459 CookieMap* cookie_map = nullptr);
460
461 std::vector<CanonicalCookie*> FindPartitionedCookiesForRegistryControlledHost(
462 const CookiePartitionKey& cookie_partition_key,
cfredric09ba72fb2020-12-22 21:03:27463 const GURL& url);
[email protected]63ee33bd2012-03-15 09:29:58464
Lily Chenf068a762019-08-21 21:10:50465 void FilterCookiesWithOptions(const GURL url,
466 const CookieOptions options,
467 std::vector<CanonicalCookie*>* cookie_ptrs,
Ayu Ishiibc6fdb0a2020-06-08 22:59:19468 CookieAccessResultList* included_cookies,
469 CookieAccessResultList* excluded_cookies);
Lily Chenf068a762019-08-21 21:10:50470
Lily Chen4c5f8632019-10-30 18:11:51471 // Possibly delete an existing cookie equivalent to |cookie_being_set| (same
472 // path, domain, and name).
Mike Westc4a777b2017-10-06 14:04:20473 //
Maks Orlovichbd04d782020-11-17 21:23:34474 // |allowed_to_set_secure_cookie| indicates if the source may override
475 // existing secure cookies. If the source is not trustworthy, and there is an
476 // existing "equivalent" cookie that is Secure, that cookie will be preserved,
477 // under "Leave Secure Cookies Alone" (see
Lily Chen4c5f8632019-10-30 18:11:51478 // https://tools.ietf.org/html/draft-ietf-httpbis-cookie-alone-01).
479 // ("equivalent" here is in quotes because the equivalency check for the
480 // purposes of preserving existing Secure cookies is slightly more inclusive.)
481 //
482 // If |skip_httponly| is true, httponly cookies will not be deleted even if
483 // they are equivalent.
484 // |key| is the key to find the cookie in cookies_; see the comment before the
jwwa26e439d2017-01-27 18:17:27485 // CookieMap typedef for details.
Mike Westc4a777b2017-10-06 14:04:20486 //
Lily Chen4c5f8632019-10-30 18:11:51487 // If a cookie is deleted, and its value matches |cookie_being_set|'s value,
488 // then |creation_date_to_inherit| will be set to that cookie's creation date.
Mike Westc4a777b2017-10-06 14:04:20489 //
Lily Chenf53dfbcd2019-08-30 01:42:10490 // The cookie will not be deleted if |*status| is not "include" when calling
491 // the function. The function will update |*status| with exclusion reasons if
492 // a secure cookie was skipped or an httponly cookie was skipped.
493 //
Dylan Cutler0b9a4e962021-09-13 17:34:25494 // If |cookie_partition_it| is nullopt, it will search |cookies_| for
495 // duplicates of |cookie_being_set|. Otherwise, |cookie_partition_it|'s value
496 // is the iterator of the CookieMap in |partitioned_cookies_| we should search
497 // for duplicates.
498 //
[email protected]63ee33bd2012-03-15 09:29:58499 // NOTE: There should never be more than a single matching equivalent cookie.
Lily Chenf53dfbcd2019-08-30 01:42:10500 void MaybeDeleteEquivalentCookieAndUpdateStatus(
Aaron Tagliaboschi29764f52019-02-21 17:19:59501 const std::string& key,
Lily Chen4c5f8632019-10-30 18:11:51502 const CanonicalCookie& cookie_being_set,
Maks Orlovichbd04d782020-11-17 21:23:34503 bool allowed_to_set_secure_cookie,
Aaron Tagliaboschi29764f52019-02-21 17:19:59504 bool skip_httponly,
505 bool already_expired,
Lily Chenf53dfbcd2019-08-30 01:42:10506 base::Time* creation_date_to_inherit,
Dylan Cutler0b9a4e962021-09-13 17:34:25507 CookieInclusionStatus* status,
508 absl::optional<PartitionedCookieMap::iterator> cookie_partition_it);
[email protected]63ee33bd2012-03-15 09:29:58509
Lily Chend8d11db2021-02-25 19:50:52510 // Inserts `cc` into cookies_. Returns an iterator that points to the inserted
511 // cookie in `cookies_`. Guarantee: all iterators to `cookies_` remain valid.
512 // Dispatches the change to `change_dispatcher_` iff `dispatch_change` is
513 // true.
Ayu Ishii9f5e72dc2020-07-22 19:43:18514 CookieMap::iterator InternalInsertCookie(
515 const std::string& key,
516 std::unique_ptr<CanonicalCookie> cc,
517 bool sync_to_store,
Lily Chend8d11db2021-02-25 19:50:52518 const CookieAccessResult& access_result,
519 bool dispatch_change = true);
[email protected]63ee33bd2012-03-15 09:29:58520
Dylan Cutler0b9a4e962021-09-13 17:34:25521 // Returns true if the cookie should be (or is already) synced to the store.
522 // Used for cookies during insertion and deletion into the in-memory store.
523 bool ShouldUpdatePersistentStore(CanonicalCookie* cc);
524
525 void LogCookieTypeToUMA(CanonicalCookie* cc,
526 const CookieAccessResult& access_result);
527
528 // Inserts `cc` into partitioned_cookies_. Should only be used when
529 // cc->IsPartitioned() is true.
530 PartitionedCookieMapIterators InternalInsertPartitionedCookie(
531 std::string key,
532 std::unique_ptr<CanonicalCookie> cc,
533 bool sync_to_store,
534 const CookieAccessResult& access_result,
535 bool dispatch_change = true);
536
rdsmith2709eee2017-06-20 22:43:27537 // Sets all cookies from |list| after deleting any equivalent cookie.
538 // For data gathering purposes, this routine is treated as if it is
539 // restoring saved cookies; some statistics are not gathered in this case.
rdsmithe5c701d2017-07-12 21:50:00540 void SetAllCookies(CookieList list, SetCookiesCallback callback);
drogerd5d1278c2015-03-17 19:21:51541
[email protected]63ee33bd2012-03-15 09:29:58542 void InternalUpdateCookieAccessTime(CanonicalCookie* cc,
543 const base::Time& current_time);
544
545 // |deletion_cause| argument is used for collecting statistics and choosing
Victor Costan14f47c12018-03-01 08:02:24546 // the correct CookieChangeCause for OnCookieChange notifications. Guarantee:
547 // All iterators to cookies_, except for the deleted entry, remain valid.
mkwstbe84af312015-02-20 08:52:45548 void InternalDeleteCookie(CookieMap::iterator it,
549 bool sync_to_store,
[email protected]63ee33bd2012-03-15 09:29:58550 DeletionCause deletion_cause);
551
Dylan Cutler0b9a4e962021-09-13 17:34:25552 // Deletes a Partitioned cookie. Returns true if the deletion operation
553 // resulted in the CookieMap the cookie was stored in was deleted.
554 //
555 // If the CookieMap which contains the deleted cookie only has one entry, then
556 // this function will also delete the CookieMap from PartitionedCookieMap.
557 // This may invalidate the |cookie_partition_it| argument.
558 void InternalDeletePartitionedCookie(
559 PartitionedCookieMap::iterator partition_it,
560 CookieMap::iterator cookie_it,
561 bool sync_to_store,
562 DeletionCause deletion_cause);
563
[email protected]63ee33bd2012-03-15 09:29:58564 // If the number of cookies for CookieMap key |key|, or globally, are
565 // over the preset maximums above, garbage collect, first for the host and
566 // then globally. See comments above garbage collection threshold
Dylan Cutler0b9a4e962021-09-13 17:34:25567 // constants for details. Also removes expired cookies.
[email protected]63ee33bd2012-03-15 09:29:58568 //
569 // Returns the number of cookies deleted (useful for debugging).
jwwa26e439d2017-01-27 18:17:27570 size_t GarbageCollect(const base::Time& current, const std::string& key);
[email protected]63ee33bd2012-03-15 09:29:58571
Dylan Cutler0b9a4e962021-09-13 17:34:25572 // Run garbage collection for PartitionedCookieMap keys |cookie_partition_key|
573 // and |key|.
574 //
575 // Partitioned cookies are subject to different limits than unpartitioned
576 // cookies in order to prevent leaking entropy about user behavior across
577 // cookie partitions.
578 size_t GarbageCollectPartitionedCookies(
579 const base::Time& current,
580 const CookiePartitionKey& cookie_partition_key,
581 const std::string& key);
582
mkwste079ac412016-03-11 09:04:06583 // Helper for GarbageCollect(). Deletes up to |purge_goal| cookies with a
584 // priority less than or equal to |priority| from |cookies|, while ensuring
585 // that at least the |to_protect| most-recent cookies are retained.
jwwc00ac712016-05-05 22:21:44586 // |protected_secure_cookies| specifies whether or not secure cookies should
587 // be protected from deletion.
mkwste079ac412016-03-11 09:04:06588 //
589 // |cookies| must be sorted from least-recent to most-recent.
590 //
mkwste079ac412016-03-11 09:04:06591 // Returns the number of cookies deleted.
592 size_t PurgeLeastRecentMatches(CookieItVector* cookies,
593 CookiePriority priority,
594 size_t to_protect,
jwwc00ac712016-05-05 22:21:44595 size_t purge_goal,
596 bool protect_secure_cookies);
mkwste079ac412016-03-11 09:04:06597
jww82d99c12015-11-25 18:39:53598 // Helper for GarbageCollect(); can be called directly as well. Deletes all
599 // expired cookies in |itpair|. If |cookie_its| is non-NULL, all the
600 // non-expired cookies from |itpair| are appended to |cookie_its|.
[email protected]63ee33bd2012-03-15 09:29:58601 //
602 // Returns the number of cookies deleted.
jww82d99c12015-11-25 18:39:53603 size_t GarbageCollectExpired(const base::Time& current,
604 const CookieMapItPair& itpair,
605 CookieItVector* cookie_its);
606
Dylan Cutler0b9a4e962021-09-13 17:34:25607 // Deletes all expired cookies in the double-keyed PartitionedCookie map in
608 // the CookieMap at |cookie_partition_it|. It deletes all cookies in that
609 // CookieMap in |itpair|. If |cookie_its| is non-NULL, all non-expired cookies
610 // from |itpair| are appended to |cookie_its|.
611 //
612 // Returns the number of cookies deleted.
613 size_t GarbageCollectExpiredPartitionedCookies(
614 const base::Time& current,
615 const PartitionedCookieMap::iterator& cookie_partition_it,
616 const CookieMapItPair& itpair,
617 CookieItVector* cookie_its);
618
619 // Helper function to garbage collect all expired cookies in
620 // PartitionedCookieMap.
621 void GarbageCollectAllExpiredPartitionedCookies(const base::Time& current);
622
[email protected]8ad5d462013-05-02 08:45:26623 // Helper for GarbageCollect(). Deletes all cookies in the range specified by
624 // [|it_begin|, |it_end|). Returns the number of cookies deleted.
jww82d99c12015-11-25 18:39:53625 size_t GarbageCollectDeleteRange(const base::Time& current,
626 DeletionCause cause,
627 CookieItVector::iterator cookie_its_begin,
628 CookieItVector::iterator cookie_its_end);
629
630 // Helper for GarbageCollect(). Deletes cookies in |cookie_its| from least to
631 // most recently used, but only before |safe_date|. Also will stop deleting
632 // when the number of remaining cookies hits |purge_goal|.
mmenkef4721d992017-06-07 17:13:59633 //
634 // Sets |earliest_time| to be the earliest last access time of a cookie that
635 // was not deleted, or base::Time() if no such cookie exists.
jww82d99c12015-11-25 18:39:53636 size_t GarbageCollectLeastRecentlyAccessed(const base::Time& current,
637 const base::Time& safe_date,
638 size_t purge_goal,
mmenkef4721d992017-06-07 17:13:59639 CookieItVector cookie_its,
640 base::Time* earliest_time);
[email protected]63ee33bd2012-03-15 09:29:58641
[email protected]63ee33bd2012-03-15 09:29:58642 bool HasCookieableScheme(const GURL& url);
643
Lily Chenb0eedc22020-10-26 16:34:42644 // Get the cookie's access semantics (LEGACY or NONLEGACY), by checking for a
Lily Chenb0ca3f72019-12-05 18:06:29645 // value from the cookie access delegate, if it is non-null. Otherwise returns
646 // UNKNOWN.
Lily Chen70f997f2019-10-07 22:01:37647 CookieAccessSemantics GetAccessSemanticsForCookie(
648 const CanonicalCookie& cookie) const;
649
[email protected]63ee33bd2012-03-15 09:29:58650 // Statistics support
651
652 // This function should be called repeatedly, and will record
653 // statistics if a sufficient time period has passed.
654 void RecordPeriodicStats(const base::Time& current_time);
655
Lily Chen229623d72020-06-01 17:20:14656 // Records the aforementioned stats if we have already finished loading all
657 // cookies. Returns whether stats were recorded.
658 bool DoRecordPeriodicStats();
659
Maks Orlovich323efaf2018-03-06 02:56:39660 // Defers the callback until the full coookie database has been loaded. If
661 // it's already been loaded, runs the callback synchronously.
rdsmithe5c701d2017-07-12 21:50:00662 void DoCookieCallback(base::OnceClosure callback);
[email protected]63ee33bd2012-03-15 09:29:58663
Maks Orlovich323efaf2018-03-06 02:56:39664 // Defers the callback until the cookies relevant to given URL have been
665 // loaded. If they've already been loaded, runs the callback synchronously.
rdsmithe5c701d2017-07-12 21:50:00666 void DoCookieCallbackForURL(base::OnceClosure callback, const GURL& url);
[email protected]63ee33bd2012-03-15 09:29:58667
Maks Orlovich323efaf2018-03-06 02:56:39668 // Defers the callback until the cookies relevant to given host or domain
669 // have been loaded. If they've already been loaded, runs the callback
670 // synchronously.
671 void DoCookieCallbackForHostOrDomain(base::OnceClosure callback,
672 base::StringPiece host_or_domain);
673
Steven Bingler0420a3752020-11-20 21:40:48674 // Checks to see if a cookie is being sent to the same port it was set by. For
675 // metrics.
676 //
677 // This is in CookieMonster because only CookieMonster uses it. It's otherwise
678 // a standalone utility function.
679 static CookieSentToSamePort IsCookieSentToSamePortThatSetIt(
680 const GURL& destination,
681 int source_port,
682 CookieSourceScheme source_scheme);
683
Lily Chen229623d72020-06-01 17:20:14684 // Set of keys (eTLD+1's) for which non-expired cookies have
685 // been evicted for hitting the per-domain max. The size of this set is
686 // histogrammed periodically. The size is limited to |kMaxDomainPurgedKeys|.
687 std::set<std::string> domain_purged_keys_;
688
689 // The number of distinct keys (eTLD+1's) currently present in the |cookies_|
690 // multimap. This is histogrammed periodically.
691 size_t num_keys_;
692
[email protected]63ee33bd2012-03-15 09:29:58693 CookieMap cookies_;
694
Dylan Cutler0b9a4e962021-09-13 17:34:25695 PartitionedCookieMap partitioned_cookies_;
696
697 // Number of distinct partitioned cookies globally. This is used to enforce a
698 // global maximum on the number of partitioned cookies.
699 size_t num_partitioned_cookies_;
700
Victor Costan14f47c12018-03-01 08:02:24701 CookieMonsterChangeDispatcher change_dispatcher_;
702
erikchen1dd72a72015-05-06 20:45:05703 // Indicates whether the cookie store has been initialized.
[email protected]63ee33bd2012-03-15 09:29:58704 bool initialized_;
705
erikchen1dd72a72015-05-06 20:45:05706 // Indicates whether the cookie store has started fetching all cookies.
707 bool started_fetching_all_cookies_;
708 // Indicates whether the cookie store has finished fetching all cookies.
709 bool finished_fetching_all_cookies_;
[email protected]63ee33bd2012-03-15 09:29:58710
711 // List of domain keys that have been loaded from the DB.
712 std::set<std::string> keys_loaded_;
713
714 // Map of domain keys to their associated task queues. These tasks are blocked
715 // until all cookies for the associated domain key eTLD+1 are loaded from the
716 // backend store.
Brett Wilsonc6a0c822017-09-12 00:04:29717 std::map<std::string, base::circular_deque<base::OnceClosure>>
718 tasks_pending_for_key_;
[email protected]63ee33bd2012-03-15 09:29:58719
720 // Queues tasks that are blocked until all cookies are loaded from the backend
721 // store.
Brett Wilsonc6a0c822017-09-12 00:04:29722 base::circular_deque<base::OnceClosure> tasks_pending_;
mmenkef49fca0e2016-03-08 12:46:24723
724 // Once a global cookie task has been seen, all per-key tasks must be put in
725 // |tasks_pending_| instead of |tasks_pending_for_key_| to ensure a reasonable
rdsmithe5c701d2017-07-12 21:50:00726 // view of the cookie store. This is more to ensure fancy cookie export/import
mmenkef49fca0e2016-03-08 12:46:24727 // code has a consistent view of the CookieStore, rather than out of concern
728 // for typical use.
729 bool seen_global_task_;
[email protected]63ee33bd2012-03-15 09:29:58730
Helen Licd0fab862018-08-13 16:07:53731 NetLogWithSource net_log_;
732
[email protected]63ee33bd2012-03-15 09:29:58733 scoped_refptr<PersistentCookieStore> store_;
734
[email protected]63ee33bd2012-03-15 09:29:58735 // Minimum delay after updating a cookie's LastAccessDate before we will
736 // update it again.
737 const base::TimeDelta last_access_threshold_;
738
739 // Approximate date of access time of least recently accessed cookie
740 // in |cookies_|. Note that this is not guaranteed to be accurate, only a)
741 // to be before or equal to the actual time, and b) to be accurate
mmenkef4721d992017-06-07 17:13:59742 // immediately after a garbage collection that scans through all the cookies
743 // (When garbage collection does not scan through all cookies, it may not be
744 // updated). This value is used to determine whether global garbage collection
745 // might find cookies to purge. Note: The default Time() constructor will
746 // create a value that compares earlier than any other time value, which is
747 // wanted. Thus this value is not initialized.
[email protected]63ee33bd2012-03-15 09:29:58748 base::Time earliest_access_time_;
749
[email protected]63ee33bd2012-03-15 09:29:58750 std::vector<std::string> cookieable_schemes_;
751
[email protected]63ee33bd2012-03-15 09:29:58752 base::Time last_statistic_record_time_;
753
[email protected]63ee33bd2012-03-15 09:29:58754 bool persist_session_cookies_;
755
mmenkebe0910d2016-03-01 19:09:09756 base::ThreadChecker thread_checker_;
757
Jeremy Romand54000b22019-07-08 18:40:16758 base::WeakPtrFactory<CookieMonster> weak_ptr_factory_{this};
[email protected]63ee33bd2012-03-15 09:29:58759};
760
[email protected]63ee33bd2012-03-15 09:29:58761typedef base::RefCountedThreadSafe<CookieMonster::PersistentCookieStore>
762 RefcountedPersistentCookieStore;
763
[email protected]c1b6e102013-04-10 20:54:49764class NET_EXPORT CookieMonster::PersistentCookieStore
[email protected]63ee33bd2012-03-15 09:29:58765 : public RefcountedPersistentCookieStore {
766 public:
Maks Orlovich108cb4c2019-03-26 20:24:57767 typedef base::OnceCallback<void(
768 std::vector<std::unique_ptr<CanonicalCookie>>)>
[email protected]5b9bc352012-07-18 13:13:34769 LoadedCallback;
[email protected]63ee33bd2012-03-15 09:29:58770
Peter Boström407869b2021-10-07 04:42:48771 PersistentCookieStore(const PersistentCookieStore&) = delete;
772 PersistentCookieStore& operator=(const PersistentCookieStore&) = delete;
773
[email protected]63ee33bd2012-03-15 09:29:58774 // Initializes the store and retrieves the existing cookies. This will be
775 // called only once at startup. The callback will return all the cookies
776 // that are not yet returned to CookieMonster by previous priority loads.
mmenkebe0910d2016-03-01 19:09:09777 //
778 // |loaded_callback| may not be NULL.
Helen Li92a29f102018-08-15 23:02:26779 // |net_log| is a NetLogWithSource that may be copied if the persistent
780 // store wishes to log NetLog events.
Maks Orlovich108cb4c2019-03-26 20:24:57781 virtual void Load(LoadedCallback loaded_callback,
Helen Li92a29f102018-08-15 23:02:26782 const NetLogWithSource& net_log) = 0;
[email protected]63ee33bd2012-03-15 09:29:58783
784 // Does a priority load of all cookies for the domain key (eTLD+1). The
785 // callback will return all the cookies that are not yet returned by previous
786 // loads, which includes cookies for the requested domain key if they are not
787 // already returned, plus all cookies that are chain-loaded and not yet
788 // returned to CookieMonster.
mmenkebe0910d2016-03-01 19:09:09789 //
790 // |loaded_callback| may not be NULL.
[email protected]63ee33bd2012-03-15 09:29:58791 virtual void LoadCookiesForKey(const std::string& key,
Maks Orlovich108cb4c2019-03-26 20:24:57792 LoadedCallback loaded_callback) = 0;
[email protected]63ee33bd2012-03-15 09:29:58793
794 virtual void AddCookie(const CanonicalCookie& cc) = 0;
795 virtual void UpdateCookieAccessTime(const CanonicalCookie& cc) = 0;
796 virtual void DeleteCookie(const CanonicalCookie& cc) = 0;
797
[email protected]bf510ed2012-06-05 08:31:43798 // Instructs the store to not discard session only cookies on shutdown.
799 virtual void SetForceKeepSessionState() = 0;
[email protected]63ee33bd2012-03-15 09:29:58800
Nick Harper14e23332017-07-28 00:27:23801 // Sets a callback that will be run before the store flushes. If |callback|
802 // performs any async operations, the store will not wait for those to finish
803 // before flushing.
Lily Chen9934f7e2019-03-13 19:16:55804 virtual void SetBeforeCommitCallback(base::RepeatingClosure callback) = 0;
Nick Harper14e23332017-07-28 00:27:23805
mmenkebe0910d2016-03-01 19:09:09806 // Flushes the store and posts |callback| when complete. |callback| may be
807 // NULL.
rdsmith7ac81712017-06-22 17:09:54808 virtual void Flush(base::OnceClosure callback) = 0;
[email protected]63ee33bd2012-03-15 09:29:58809
810 protected:
Christian Dullweberff11c452021-05-12 17:04:45811 PersistentCookieStore() = default;
812 virtual ~PersistentCookieStore() = default;
[email protected]63ee33bd2012-03-15 09:29:58813
814 private:
[email protected]a9813302012-04-28 09:29:28815 friend class base::RefCountedThreadSafe<PersistentCookieStore>;
[email protected]63ee33bd2012-03-15 09:29:58816};
817
[email protected]63ee33bd2012-03-15 09:29:58818} // namespace net
819
820#endif // NET_COOKIES_COOKIE_MONSTER_H_