Avi Drissman | 6459548 | 2022-09-14 20:52:29 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 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] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 9 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 10 | #include <stddef.h> |
| 11 | #include <stdint.h> |
| 12 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 13 | #include <map> |
danakj | a9850e1 | 2016-04-18 22:28:08 | [diff] [blame] | 14 | #include <memory> |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 15 | #include <set> |
| 16 | #include <string> |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 17 | #include <vector> |
| 18 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 19 | #include "base/callback_forward.h" |
Brett Wilson | c6a0c82 | 2017-09-12 00:04:29 | [diff] [blame] | 20 | #include "base/containers/circular_deque.h" |
cfredric | 326a0bc | 2022-01-12 18:51:30 | [diff] [blame] | 21 | #include "base/containers/flat_map.h" |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 22 | #include "base/gtest_prod_util.h" |
| 23 | #include "base/memory/ref_counted.h" |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 24 | #include "base/memory/weak_ptr.h" |
Maks Orlovich | 323efaf | 2018-03-06 02:56:39 | [diff] [blame] | 25 | #include "base/strings/string_piece.h" |
Chris Fredrickson | 4f22b326 | 2022-09-30 19:57:30 | [diff] [blame^] | 26 | #include "base/thread_annotations.h" |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 27 | #include "base/threading/thread_checker.h" |
[email protected] | 9da992db | 2013-06-28 05:40:47 | [diff] [blame] | 28 | #include "base/time/time.h" |
[email protected] | 565c3f4 | 2012-08-14 14:22:58 | [diff] [blame] | 29 | #include "net/base/net_export.h" |
cfredric | 326a0bc | 2022-01-12 18:51:30 | [diff] [blame] | 30 | #include "net/base/schemeful_site.h" |
[email protected] | 8da4b181 | 2012-07-25 13:54:38 | [diff] [blame] | 31 | #include "net/cookies/canonical_cookie.h" |
Lily Chen | ab36a11 | 2019-09-19 20:17:28 | [diff] [blame] | 32 | #include "net/cookies/cookie_access_delegate.h" |
[email protected] | ab2d75c8 | 2013-04-19 18:39:04 | [diff] [blame] | 33 | #include "net/cookies/cookie_constants.h" |
Jihwan Marc Kim | 3e132f1 | 2020-05-20 17:33:19 | [diff] [blame] | 34 | #include "net/cookies/cookie_inclusion_status.h" |
Victor Costan | 14f47c1 | 2018-03-01 08:02:24 | [diff] [blame] | 35 | #include "net/cookies/cookie_monster_change_dispatcher.h" |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 36 | #include "net/cookies/cookie_store.h" |
Helen Li | cd0fab86 | 2018-08-13 16:07:53 | [diff] [blame] | 37 | #include "net/log/net_log_with_source.h" |
Alex Kalugin | 379f47d8 | 2022-03-08 13:11:59 | [diff] [blame] | 38 | #include "third_party/abseil-cpp/absl/types/optional.h" |
ellyjones | 399e35a2 | 2014-10-27 11:09:56 | [diff] [blame] | 39 | #include "url/gurl.h" |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 40 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 41 | namespace net { |
| 42 | |
Victor Costan | 14f47c1 | 2018-03-01 08:02:24 | [diff] [blame] | 43 | class CookieChangeDispatcher; |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 44 | |
| 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 | // |
mmenke | 96f3bab | 2016-01-22 17:34:02 | [diff] [blame] | 50 | // 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] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 52 | // |
| 53 | // A cookie task is either pending loading of the entire cookie store, or |
Maks Orlovich | 323efaf | 2018-03-06 02:56:39 | [diff] [blame] | 54 | // 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] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 60 | class NET_EXPORT CookieMonster : public CookieStore { |
| 61 | public: |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 62 | 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 Cutler | 0b9a4e96 | 2021-09-13 17:34:25 | [diff] [blame] | 90 | // CookieMap does not store cookies that were set with the Partitioned |
| 91 | // attribute, those are stored in PartitionedCookieMap. |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 92 | |
| 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 |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 101 | // 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] | 8ad5d46 | 2013-05-02 08:45:26 | [diff] [blame] | 106 | |
Dylan Cutler | 0b9a4e96 | 2021-09-13 17:34:25 | [diff] [blame] | 107 | // 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] | 8ad5d46 | 2013-05-02 08:45:26 | [diff] [blame] | 119 | // 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 Chen | 229623d7 | 2020-06-01 17:20:14 | [diff] [blame] | 138 | // Max number of keys to store for domains that have been purged. |
| 139 | static const size_t kMaxDomainPurgedKeys; |
| 140 | |
Dylan Cutler | 0b9a4e96 | 2021-09-13 17:34:25 | [diff] [blame] | 141 | // 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] | 8ad5d46 | 2013-05-02 08:45:26 | [diff] [blame] | 145 | // Quota for cookies with {low, medium, high} priorities within a domain. |
mkwst | 8773435 | 2016-03-03 17:36:23 | [diff] [blame] | 146 | static const size_t kDomainCookiesQuotaLow; |
| 147 | static const size_t kDomainCookiesQuotaMedium; |
| 148 | static const size_t kDomainCookiesQuotaHigh; |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 149 | |
Matt Menke | 477ab63 | 2019-06-27 23:12:17 | [diff] [blame] | 150 | // 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] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 154 | // 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 Harper | 57142b1c | 2019-03-14 21:03:59 | [diff] [blame] | 158 | // updated. |net_log| must outlive the CookieMonster and can be null. |
Pritam | 8354cf70 | 2018-03-10 08:55:41 | [diff] [blame] | 159 | CookieMonster(scoped_refptr<PersistentCookieStore> store, |
Kirubel Aklilu | c9b4e41 | 2022-01-12 01:00:01 | [diff] [blame] | 160 | NetLog* net_log, |
| 161 | bool first_party_sets_enabled); |
nharper | 2b0ad9a | 2017-05-22 18:33:45 | [diff] [blame] | 162 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 163 | // Only used during unit testing. |
Helen Li | fb313a9 | 2018-08-14 15:46:44 | [diff] [blame] | 164 | // |net_log| must outlive the CookieMonster. |
Pritam | 8354cf70 | 2018-03-10 08:55:41 | [diff] [blame] | 165 | CookieMonster(scoped_refptr<PersistentCookieStore> store, |
Helen Li | fb313a9 | 2018-08-14 15:46:44 | [diff] [blame] | 166 | base::TimeDelta last_access_threshold, |
Kirubel Aklilu | c9b4e41 | 2022-01-12 01:00:01 | [diff] [blame] | 167 | NetLog* net_log, |
| 168 | bool first_party_sets_enabled); |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 169 | |
Peter Boström | 293b134 | 2021-09-22 17:31:43 | [diff] [blame] | 170 | CookieMonster(const CookieMonster&) = delete; |
| 171 | CookieMonster& operator=(const CookieMonster&) = delete; |
| 172 | |
mmenke | 606c59c | 2016-03-07 18:20:55 | [diff] [blame] | 173 | ~CookieMonster() override; |
| 174 | |
rdsmith | 0e84cea | 2017-07-13 03:09:53 | [diff] [blame] | 175 | // Writes all the cookies in |list| into the store, replacing all cookies |
| 176 | // currently present in store. |
rdsmith | 2709eee | 2017-06-20 22:43:27 | [diff] [blame] | 177 | // This method does not flush the backend. |
| 178 | // TODO(rdsmith, mmenke): Do not use this function; it is deprecated |
| 179 | // and should be removed. |
| 180 | // See https://codereview.chromium.org/2882063002/#msg64. |
rdsmith | 7ac8171 | 2017-06-22 17:09:54 | [diff] [blame] | 181 | void SetAllCookiesAsync(const CookieList& list, SetCookiesCallback callback); |
droger | d5d1278c | 2015-03-17 19:21:51 | [diff] [blame] | 182 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 183 | // CookieStore implementation. |
Juba Borgohain | 9fa2414 | 2022-02-04 18:25:42 | [diff] [blame] | 184 | void SetCanonicalCookieAsync( |
| 185 | std::unique_ptr<CanonicalCookie> cookie, |
| 186 | const GURL& source_url, |
| 187 | const CookieOptions& options, |
| 188 | SetCookiesCallback callback, |
Alex Kalugin | 379f47d8 | 2022-03-08 13:11:59 | [diff] [blame] | 189 | absl::optional<CookieAccessResult> cookie_access_result = |
| 190 | absl::nullopt) override; |
Dylan Cutler | cd2d893 | 2021-10-05 19:03:43 | [diff] [blame] | 191 | void GetCookieListWithOptionsAsync(const GURL& url, |
| 192 | const CookieOptions& options, |
Aykut Bulut | 244341e | 2021-12-09 15:57:25 | [diff] [blame] | 193 | const CookiePartitionKeyCollection& s, |
Dylan Cutler | cd2d893 | 2021-10-05 19:03:43 | [diff] [blame] | 194 | GetCookieListCallback callback) override; |
Lily Chen | f068a76 | 2019-08-21 21:10:50 | [diff] [blame] | 195 | void GetAllCookiesAsync(GetAllCookiesCallback callback) override; |
Lily Chen | e2e9ae01 | 2019-10-09 20:02:54 | [diff] [blame] | 196 | void GetAllCookiesWithAccessSemanticsAsync( |
| 197 | GetAllCookiesWithAccessSemanticsCallback callback) override; |
mmenke | 24379d5 | 2016-02-05 23:50:17 | [diff] [blame] | 198 | void DeleteCanonicalCookieAsync(const CanonicalCookie& cookie, |
rdsmith | 7ac8171 | 2017-06-22 17:09:54 | [diff] [blame] | 199 | DeleteCallback callback) override; |
Chris Mumford | d8ed9f8 | 2018-05-01 15:43:13 | [diff] [blame] | 200 | void DeleteAllCreatedInTimeRangeAsync( |
| 201 | const CookieDeletionInfo::TimeRange& creation_range, |
| 202 | DeleteCallback callback) override; |
Chris Mumford | 800caa6 | 2018-04-20 19:34:44 | [diff] [blame] | 203 | void DeleteAllMatchingInfoAsync(CookieDeletionInfo delete_info, |
| 204 | DeleteCallback callback) override; |
Christian Dullweber | ff11c45 | 2021-05-12 17:04:45 | [diff] [blame] | 205 | void DeleteSessionCookiesAsync(DeleteCallback callback) override; |
| 206 | void DeleteMatchingCookiesAsync(DeletePredicate predicate, |
| 207 | DeleteCallback callback) override; |
rdsmith | 7ac8171 | 2017-06-22 17:09:54 | [diff] [blame] | 208 | void FlushStore(base::OnceClosure callback) override; |
mmenke | ded79da | 2016-02-06 08:28:51 | [diff] [blame] | 209 | void SetForceKeepSessionState() override; |
Victor Costan | 14f47c1 | 2018-03-01 08:02:24 | [diff] [blame] | 210 | CookieChangeDispatcher& GetChangeDispatcher() override; |
Nate Fischer | c6fb6cf | 2019-03-27 00:39:49 | [diff] [blame] | 211 | void SetCookieableSchemes(const std::vector<std::string>& schemes, |
| 212 | SetCookieableSchemesCallback callback) override; |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 213 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 214 | // Enables writing session cookies into the cookie database. If this this |
| 215 | // method is called, it must be called before first use of the instance |
| 216 | // (i.e. as part of the instance initialization process). |
| 217 | void SetPersistSessionCookies(bool persist_session_cookies); |
| 218 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 219 | // The default list of schemes the cookie monster can handle. |
[email protected] | 5edff3c5 | 2014-06-23 20:27:48 | [diff] [blame] | 220 | static const char* const kDefaultCookieableSchemes[]; |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 221 | static const int kDefaultCookieableSchemesCount; |
| 222 | |
Maks Orlovich | 323efaf | 2018-03-06 02:56:39 | [diff] [blame] | 223 | // Find a key based on the given domain, which will be used to find all |
| 224 | // cookies potentially relevant to it. This is used for lookup in cookies_ as |
| 225 | // well as for PersistentCookieStore::LoadCookiesForKey. See comment on keys |
| 226 | // before the CookieMap typedef. |
| 227 | static std::string GetKey(base::StringPiece domain); |
| 228 | |
cfredric | 59f8a845 | 2021-06-08 15:27:11 | [diff] [blame] | 229 | // Exposes the comparison function used when sorting cookies. |
| 230 | static bool CookieSorter(const CanonicalCookie* cc1, |
| 231 | const CanonicalCookie* cc2); |
| 232 | |
Lily Chen | 229623d7 | 2020-06-01 17:20:14 | [diff] [blame] | 233 | // Triggers immediate recording of stats that are typically reported |
| 234 | // periodically. |
| 235 | bool DoRecordPeriodicStatsForTesting() { return DoRecordPeriodicStats(); } |
| 236 | |
Dylan Cutler | 03d2c76c | 2022-02-18 02:23:15 | [diff] [blame] | 237 | // Will convert a site's partitioned cookies into unpartitioned cookies. This |
| 238 | // may result in multiple cookies which have the same (partition_key, name, |
| 239 | // host_key, path), which violates the database's unique constraint. The |
| 240 | // algorithm we use to coalesce the cookies into a single unpartitioned cookie |
| 241 | // is the following: |
| 242 | // |
| 243 | // 1. If one of the cookies has no partition key (i.e. it is unpartitioned) |
| 244 | // choose this cookie. |
| 245 | // |
| 246 | // 2. Choose the partitioned cookie with the most recent last_access_time. |
| 247 | // |
| 248 | // TODO(crbug.com/1296161): Delete this when the partitioned cookies Origin |
| 249 | // Trial ends. |
| 250 | void ConvertPartitionedCookiesToUnpartitioned(const GURL& url) override; |
| 251 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 252 | private: |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 253 | // For garbage collection constants. |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 254 | FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestHostGarbageCollection); |
mmenke | f4721d99 | 2017-06-07 17:13:59 | [diff] [blame] | 255 | FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, |
| 256 | GarbageCollectWithSecureCookiesOnly); |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 257 | FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestGCTimes); |
| 258 | |
| 259 | // For validation of key values. |
| 260 | FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestDomainTree); |
| 261 | FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestImport); |
| 262 | FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, GetKey); |
| 263 | FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestGetKey); |
| 264 | |
| 265 | // For FindCookiesForKey. |
| 266 | FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, ShortLivedSessionCookies); |
| 267 | |
estark | 7feb65c2b | 2015-08-21 23:38:20 | [diff] [blame] | 268 | // For CookieSource histogram enum. |
| 269 | FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, CookieSourceHistogram); |
| 270 | |
jww | 31e3263 | 2015-12-16 23:38:34 | [diff] [blame] | 271 | // For kSafeFromGlobalPurgeDays in CookieStore. |
jww | a26e439d | 2017-01-27 18:17:27 | [diff] [blame] | 272 | FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, EvictSecureCookies); |
jww | 82d99c1 | 2015-11-25 18:39:53 | [diff] [blame] | 273 | |
jww | 31e3263 | 2015-12-16 23:38:34 | [diff] [blame] | 274 | // For CookieDeleteEquivalent histogram enum. |
| 275 | FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, |
| 276 | CookieDeleteEquivalentHistogramTest); |
jww | 31e3263 | 2015-12-16 23:38:34 | [diff] [blame] | 277 | |
Steven Bingler | 0420a375 | 2020-11-20 21:40:48 | [diff] [blame] | 278 | // For CookieSentToSamePort enum. |
| 279 | FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, |
| 280 | CookiePortReadDiffersFromSetHistogram); |
| 281 | FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, IsCookieSentToSamePortThatSetIt); |
| 282 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 283 | // Internal reasons for deletion, used to populate informative histograms |
| 284 | // and to provide a public cause for onCookieChange notifications. |
| 285 | // |
| 286 | // If you add or remove causes from this list, please be sure to also update |
Victor Costan | 14f47c1 | 2018-03-01 08:02:24 | [diff] [blame] | 287 | // the CookieChangeCause mapping inside ChangeCauseMapping. New items (if |
| 288 | // necessary) should be added at the end of the list, just before |
Nick Harper | 7a6683a | 2018-01-30 20:42:52 | [diff] [blame] | 289 | // DELETE_COOKIE_LAST_ENTRY. |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 290 | enum DeletionCause { |
| 291 | DELETE_COOKIE_EXPLICIT = 0, |
mkwst | aa07ee8 | 2016-03-11 15:32:14 | [diff] [blame] | 292 | DELETE_COOKIE_OVERWRITE = 1, |
| 293 | DELETE_COOKIE_EXPIRED = 2, |
| 294 | DELETE_COOKIE_EVICTED = 3, |
| 295 | DELETE_COOKIE_DUPLICATE_IN_BACKING_STORE = 4, |
| 296 | DELETE_COOKIE_DONT_RECORD = 5, // For final cleanup after flush to store. |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 297 | |
mkwst | aa07ee8 | 2016-03-11 15:32:14 | [diff] [blame] | 298 | // Cookies evicted during domain-level garbage collection. |
| 299 | DELETE_COOKIE_EVICTED_DOMAIN = 6, |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 300 | |
Dylan Cutler | 0b9a4e96 | 2021-09-13 17:34:25 | [diff] [blame] | 301 | // Cookies evicted during global garbage collection, which takes place after |
mkwst | aa07ee8 | 2016-03-11 15:32:14 | [diff] [blame] | 302 | // domain-level garbage collection fails to bring the cookie store under |
| 303 | // the overall quota. |
| 304 | DELETE_COOKIE_EVICTED_GLOBAL = 7, |
| 305 | |
| 306 | // #8 was DELETE_COOKIE_EVICTED_DOMAIN_PRE_SAFE |
| 307 | // #9 was DELETE_COOKIE_EVICTED_DOMAIN_POST_SAFE |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 308 | |
| 309 | // A common idiom is to remove a cookie by overwriting it with an |
| 310 | // already-expired expiration date. This captures that case. |
mkwst | aa07ee8 | 2016-03-11 15:32:14 | [diff] [blame] | 311 | DELETE_COOKIE_EXPIRED_OVERWRITE = 10, |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 312 | |
[email protected] | 6210ce5 | 2013-09-20 03:33:14 | [diff] [blame] | 313 | // Cookies are not allowed to contain control characters in the name or |
| 314 | // value. However, we used to allow them, so we are now evicting any such |
| 315 | // cookies as we load them. See http://crbug.com/238041. |
mkwst | aa07ee8 | 2016-03-11 15:32:14 | [diff] [blame] | 316 | DELETE_COOKIE_CONTROL_CHAR = 11, |
[email protected] | 6210ce5 | 2013-09-20 03:33:14 | [diff] [blame] | 317 | |
jww | 82d99c1 | 2015-11-25 18:39:53 | [diff] [blame] | 318 | // When strict secure cookies is enabled, non-secure cookies are evicted |
| 319 | // right after expired cookies. |
mkwst | aa07ee8 | 2016-03-11 15:32:14 | [diff] [blame] | 320 | DELETE_COOKIE_NON_SECURE = 12, |
jww | 82d99c1 | 2015-11-25 18:39:53 | [diff] [blame] | 321 | |
Dylan Cutler | 0b9a4e96 | 2021-09-13 17:34:25 | [diff] [blame] | 322 | // Partitioned cookies evicted during per-partition domain-level garbage |
| 323 | // collection. |
| 324 | DELETE_COOKIE_EVICTED_PER_PARTITION_DOMAIN = 13, |
| 325 | |
| 326 | DELETE_COOKIE_LAST_ENTRY = 14, |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 327 | }; |
| 328 | |
mkwst | c1aa4cc | 2015-04-03 19:57:45 | [diff] [blame] | 329 | // This enum is used to generate a histogramed bitmask measureing the types |
| 330 | // of stored cookies. Please do not reorder the list when adding new entries. |
| 331 | // New items MUST be added at the end of the list, just before |
| 332 | // COOKIE_TYPE_LAST_ENTRY; |
Lily Chen | da465cca | 2021-03-08 23:47:17 | [diff] [blame] | 333 | // There will be 2^COOKIE_TYPE_LAST_ENTRY buckets in the linear histogram. |
mkwst | c1aa4cc | 2015-04-03 19:57:45 | [diff] [blame] | 334 | enum CookieType { |
mkwst | 4654941 | 2016-02-01 10:05:37 | [diff] [blame] | 335 | COOKIE_TYPE_SAME_SITE = 0, |
mkwst | c1aa4cc | 2015-04-03 19:57:45 | [diff] [blame] | 336 | COOKIE_TYPE_HTTPONLY, |
| 337 | COOKIE_TYPE_SECURE, |
| 338 | COOKIE_TYPE_LAST_ENTRY |
| 339 | }; |
| 340 | |
estark | 7feb65c2b | 2015-08-21 23:38:20 | [diff] [blame] | 341 | // Used to populate a histogram containing information about the |
| 342 | // sources of Secure and non-Secure cookies: that is, whether such |
| 343 | // cookies are set by origins with cryptographic or non-cryptographic |
| 344 | // schemes. Please do not reorder the list when adding new |
Lily Chen | da465cca | 2021-03-08 23:47:17 | [diff] [blame] | 345 | // entries. New items MUST be added at the end of the list, and kMaxValue |
| 346 | // should be updated to the last value. |
estark | 7feb65c2b | 2015-08-21 23:38:20 | [diff] [blame] | 347 | // |
Lei Zhang | d938833 | 2022-08-02 23:30:46 | [diff] [blame] | 348 | // CookieSource::k(Non)SecureCookie(Non)CryptographicScheme means |
estark | 7feb65c2b | 2015-08-21 23:38:20 | [diff] [blame] | 349 | // that a cookie was set or overwritten from a URL with the given type |
| 350 | // of scheme. This enum should not be used when cookies are *cleared*, |
| 351 | // because its purpose is to understand if Chrome can deprecate the |
| 352 | // ability of HTTP urls to set/overwrite Secure cookies. |
Lei Zhang | d938833 | 2022-08-02 23:30:46 | [diff] [blame] | 353 | enum class CookieSource : uint8_t { |
| 354 | kSecureCookieCryptographicScheme = 0, |
| 355 | kSecureCookieNoncryptographicScheme, |
| 356 | kNonsecureCookieCryptographicScheme, |
| 357 | kNonsecureCookieNoncryptographicScheme, |
| 358 | kMaxValue = kNonsecureCookieNoncryptographicScheme |
estark | 7feb65c2b | 2015-08-21 23:38:20 | [diff] [blame] | 359 | }; |
| 360 | |
Steven Bingler | 0420a375 | 2020-11-20 21:40:48 | [diff] [blame] | 361 | // Enum for collecting metrics on how frequently a cookie is sent to the same |
| 362 | // port it was set by. |
| 363 | // |
| 364 | // kNoButDefault exists because we expect for cookies being sent between |
| 365 | // schemes to have a port mismatch and want to separate those out from other, |
| 366 | // more interesting, cases. |
| 367 | // |
| 368 | // Do not reorder or renumber. Used for metrics. |
| 369 | enum class CookieSentToSamePort { |
| 370 | kSourcePortUnspecified = 0, // Cookie's source port is unspecified, we |
| 371 | // can't know if this is the same port or not. |
| 372 | kInvalid = 1, // The source port was corrupted to be PORT_INVALID, we |
| 373 | // can't know if this is the same port or not. |
| 374 | kNo = 2, // Source port and destination port are different. |
| 375 | kNoButDefault = |
| 376 | 3, // Source and destination ports are different but they're |
| 377 | // the defaults for their scheme. This can mean that an http |
| 378 | // cookie was sent to a https origin or vice-versa. |
| 379 | kYes = 4, // They're the same. |
| 380 | kMaxValue = kYes |
| 381 | }; |
| 382 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 383 | // Record statistics every kRecordStatisticsIntervalSeconds of uptime. |
| 384 | static const int kRecordStatisticsIntervalSeconds = 10 * 60; |
| 385 | |
rdsmith | a6ce444 | 2017-06-21 17:11:05 | [diff] [blame] | 386 | // Sets a canonical cookie, deletes equivalents and performs garbage |
Lily Chen | 96f29a13 | 2020-04-15 17:59:36 | [diff] [blame] | 387 | // collection. |source_url| indicates what URL the cookie is being set |
Maks Orlovich | fdbc8be | 2019-03-18 18:34:52 | [diff] [blame] | 388 | // from; secure cookies cannot be altered from insecure schemes, and some |
| 389 | // schemes may not be authorized. |
| 390 | // |
| 391 | // |options| indicates if this setting operation is allowed |
| 392 | // to affect http_only or same-site cookies. |
Juba Borgohain | 9fa2414 | 2022-02-04 18:25:42 | [diff] [blame] | 393 | // |
| 394 | // |cookie_access_result| is an optional input status, to allow for status |
| 395 | // chaining from callers. It helps callers provide the status of a |
| 396 | // canonical cookie that may have warnings associated with it. |
| 397 | void SetCanonicalCookie( |
| 398 | std::unique_ptr<CanonicalCookie> cookie, |
| 399 | const GURL& source_url, |
| 400 | const CookieOptions& options, |
| 401 | SetCookiesCallback callback, |
Alex Kalugin | 379f47d8 | 2022-03-08 13:11:59 | [diff] [blame] | 402 | absl::optional<CookieAccessResult> cookie_access_result = absl::nullopt); |
rdsmith | a6ce444 | 2017-06-21 17:11:05 | [diff] [blame] | 403 | |
Lily Chen | f068a76 | 2019-08-21 21:10:50 | [diff] [blame] | 404 | void GetAllCookies(GetAllCookiesCallback callback); |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 405 | |
Lily Chen | e2e9ae01 | 2019-10-09 20:02:54 | [diff] [blame] | 406 | void AttachAccessSemanticsListForCookieList( |
| 407 | GetAllCookiesWithAccessSemanticsCallback callback, |
| 408 | const CookieList& cookie_list); |
| 409 | |
Dylan Cutler | 0b9a4e96 | 2021-09-13 17:34:25 | [diff] [blame] | 410 | void GetCookieListWithOptions( |
| 411 | const GURL& url, |
| 412 | const CookieOptions& options, |
Aykut Bulut | 244341e | 2021-12-09 15:57:25 | [diff] [blame] | 413 | const CookiePartitionKeyCollection& cookie_partition_key_collection, |
Dylan Cutler | 0b9a4e96 | 2021-09-13 17:34:25 | [diff] [blame] | 414 | GetCookieListCallback callback); |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 415 | |
Chris Mumford | d8ed9f8 | 2018-05-01 15:43:13 | [diff] [blame] | 416 | void DeleteAllCreatedInTimeRange( |
| 417 | const CookieDeletionInfo::TimeRange& creation_range, |
| 418 | DeleteCallback callback); |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 419 | |
Christian Dullweber | ff11c45 | 2021-05-12 17:04:45 | [diff] [blame] | 420 | // Returns whether |cookie| matches |delete_info|. |
| 421 | bool MatchCookieDeletionInfo(const CookieDeletionInfo& delete_info, |
| 422 | const net::CanonicalCookie& cookie); |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 423 | |
rdsmith | e5c701d | 2017-07-12 21:50:00 | [diff] [blame] | 424 | void DeleteCanonicalCookie(const CanonicalCookie& cookie, |
| 425 | DeleteCallback callback); |
mmenke | 24379d5 | 2016-02-05 23:50:17 | [diff] [blame] | 426 | |
Christian Dullweber | ff11c45 | 2021-05-12 17:04:45 | [diff] [blame] | 427 | void DeleteMatchingCookies(DeletePredicate predicate, |
| 428 | DeletionCause cause, |
| 429 | DeleteCallback callback); |
[email protected] | 264807b | 2012-04-25 14:49:37 | [diff] [blame] | 430 | |
erikchen | 1dd72a7 | 2015-05-06 20:45:05 | [diff] [blame] | 431 | // The first access to the cookie store initializes it. This method should be |
| 432 | // called before any access to the cookie store. |
| 433 | void MarkCookieStoreAsInitialized(); |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 434 | |
erikchen | 1dd72a7 | 2015-05-06 20:45:05 | [diff] [blame] | 435 | // Fetches all cookies if the backing store exists and they're not already |
| 436 | // being fetched. |
erikchen | 1dd72a7 | 2015-05-06 20:45:05 | [diff] [blame] | 437 | void FetchAllCookiesIfNecessary(); |
| 438 | |
| 439 | // Fetches all cookies from the backing store. |
erikchen | 1dd72a7 | 2015-05-06 20:45:05 | [diff] [blame] | 440 | void FetchAllCookies(); |
| 441 | |
| 442 | // Whether all cookies should be fetched as soon as any is requested. |
| 443 | bool ShouldFetchAllCookiesWhenFetchingAnyCookie(); |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 444 | |
| 445 | // Stores cookies loaded from the backing store and invokes any deferred |
| 446 | // calls. |beginning_time| should be the moment PersistentCookieStore::Load |
| 447 | // was invoked and is used for reporting histogram_time_blocked_on_load_. |
| 448 | // See PersistentCookieStore::Load for details on the contents of cookies. |
| 449 | void OnLoaded(base::TimeTicks beginning_time, |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 450 | std::vector<std::unique_ptr<CanonicalCookie>> cookies); |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 451 | |
| 452 | // Stores cookies loaded from the backing store and invokes the deferred |
| 453 | // task(s) pending loading of cookies associated with the domain key |
Maks Orlovich | 323efaf | 2018-03-06 02:56:39 | [diff] [blame] | 454 | // (GetKey, roughly eTLD+1). Called when all cookies for the domain key have |
| 455 | // been loaded from DB. See PersistentCookieStore::Load for details on the |
| 456 | // contents of cookies. |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 457 | void OnKeyLoaded(const std::string& key, |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 458 | std::vector<std::unique_ptr<CanonicalCookie>> cookies); |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 459 | |
| 460 | // Stores the loaded cookies. |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 461 | void StoreLoadedCookies( |
| 462 | std::vector<std::unique_ptr<CanonicalCookie>> cookies); |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 463 | |
| 464 | // Invokes deferred calls. |
| 465 | void InvokeQueue(); |
| 466 | |
| 467 | // Checks that |cookies_| matches our invariants, and tries to repair any |
| 468 | // inconsistencies. (In other words, it does not have duplicate cookies). |
| 469 | void EnsureCookiesMapIsValid(); |
| 470 | |
| 471 | // Checks for any duplicate cookies for CookieMap key |key| which lie between |
| 472 | // |begin| and |end|. If any are found, all but the most recent are deleted. |
Dylan Cutler | 0b9a4e96 | 2021-09-13 17:34:25 | [diff] [blame] | 473 | // |
| 474 | // If |cookie_partition_it| is not nullopt, then this function trims cookies |
| 475 | // from the CookieMap in |partitioned_cookies_| at |cookie_partition_it| |
| 476 | // instead of trimming cookies from |cookies_|. |
| 477 | void TrimDuplicateCookiesForKey( |
| 478 | const std::string& key, |
| 479 | CookieMap::iterator begin, |
| 480 | CookieMap::iterator end, |
| 481 | absl::optional<PartitionedCookieMap::iterator> cookie_partition_it); |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 482 | |
| 483 | void SetDefaultCookieableSchemes(); |
| 484 | |
cfredric | 09ba72fb | 2020-12-22 21:03:27 | [diff] [blame] | 485 | std::vector<CanonicalCookie*> FindCookiesForRegistryControlledHost( |
Dylan Cutler | 0b9a4e96 | 2021-09-13 17:34:25 | [diff] [blame] | 486 | const GURL& url, |
Dylan Cutler | a7b5f82 | 2022-09-16 16:56:05 | [diff] [blame] | 487 | CookieMap* cookie_map = nullptr, |
| 488 | PartitionedCookieMap::iterator* partition_it = nullptr); |
Dylan Cutler | 0b9a4e96 | 2021-09-13 17:34:25 | [diff] [blame] | 489 | |
| 490 | std::vector<CanonicalCookie*> FindPartitionedCookiesForRegistryControlledHost( |
| 491 | const CookiePartitionKey& cookie_partition_key, |
cfredric | 09ba72fb | 2020-12-22 21:03:27 | [diff] [blame] | 492 | const GURL& url); |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 493 | |
Lily Chen | f068a76 | 2019-08-21 21:10:50 | [diff] [blame] | 494 | void FilterCookiesWithOptions(const GURL url, |
| 495 | const CookieOptions options, |
| 496 | std::vector<CanonicalCookie*>* cookie_ptrs, |
Ayu Ishii | bc6fdb0a | 2020-06-08 22:59:19 | [diff] [blame] | 497 | CookieAccessResultList* included_cookies, |
| 498 | CookieAccessResultList* excluded_cookies); |
Lily Chen | f068a76 | 2019-08-21 21:10:50 | [diff] [blame] | 499 | |
Lily Chen | 4c5f863 | 2019-10-30 18:11:51 | [diff] [blame] | 500 | // Possibly delete an existing cookie equivalent to |cookie_being_set| (same |
| 501 | // path, domain, and name). |
Mike West | c4a777b | 2017-10-06 14:04:20 | [diff] [blame] | 502 | // |
Maks Orlovich | bd04d78 | 2020-11-17 21:23:34 | [diff] [blame] | 503 | // |allowed_to_set_secure_cookie| indicates if the source may override |
| 504 | // existing secure cookies. If the source is not trustworthy, and there is an |
| 505 | // existing "equivalent" cookie that is Secure, that cookie will be preserved, |
| 506 | // under "Leave Secure Cookies Alone" (see |
Lily Chen | 4c5f863 | 2019-10-30 18:11:51 | [diff] [blame] | 507 | // https://tools.ietf.org/html/draft-ietf-httpbis-cookie-alone-01). |
| 508 | // ("equivalent" here is in quotes because the equivalency check for the |
| 509 | // purposes of preserving existing Secure cookies is slightly more inclusive.) |
| 510 | // |
| 511 | // If |skip_httponly| is true, httponly cookies will not be deleted even if |
| 512 | // they are equivalent. |
| 513 | // |key| is the key to find the cookie in cookies_; see the comment before the |
jww | a26e439d | 2017-01-27 18:17:27 | [diff] [blame] | 514 | // CookieMap typedef for details. |
Mike West | c4a777b | 2017-10-06 14:04:20 | [diff] [blame] | 515 | // |
Lily Chen | 4c5f863 | 2019-10-30 18:11:51 | [diff] [blame] | 516 | // If a cookie is deleted, and its value matches |cookie_being_set|'s value, |
| 517 | // then |creation_date_to_inherit| will be set to that cookie's creation date. |
Mike West | c4a777b | 2017-10-06 14:04:20 | [diff] [blame] | 518 | // |
Lily Chen | f53dfbcd | 2019-08-30 01:42:10 | [diff] [blame] | 519 | // The cookie will not be deleted if |*status| is not "include" when calling |
| 520 | // the function. The function will update |*status| with exclusion reasons if |
| 521 | // a secure cookie was skipped or an httponly cookie was skipped. |
| 522 | // |
Dylan Cutler | 0b9a4e96 | 2021-09-13 17:34:25 | [diff] [blame] | 523 | // If |cookie_partition_it| is nullopt, it will search |cookies_| for |
| 524 | // duplicates of |cookie_being_set|. Otherwise, |cookie_partition_it|'s value |
| 525 | // is the iterator of the CookieMap in |partitioned_cookies_| we should search |
| 526 | // for duplicates. |
| 527 | // |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 528 | // NOTE: There should never be more than a single matching equivalent cookie. |
Lily Chen | f53dfbcd | 2019-08-30 01:42:10 | [diff] [blame] | 529 | void MaybeDeleteEquivalentCookieAndUpdateStatus( |
Aaron Tagliaboschi | 29764f5 | 2019-02-21 17:19:59 | [diff] [blame] | 530 | const std::string& key, |
Lily Chen | 4c5f863 | 2019-10-30 18:11:51 | [diff] [blame] | 531 | const CanonicalCookie& cookie_being_set, |
Maks Orlovich | bd04d78 | 2020-11-17 21:23:34 | [diff] [blame] | 532 | bool allowed_to_set_secure_cookie, |
Aaron Tagliaboschi | 29764f5 | 2019-02-21 17:19:59 | [diff] [blame] | 533 | bool skip_httponly, |
| 534 | bool already_expired, |
Lily Chen | f53dfbcd | 2019-08-30 01:42:10 | [diff] [blame] | 535 | base::Time* creation_date_to_inherit, |
Dylan Cutler | 0b9a4e96 | 2021-09-13 17:34:25 | [diff] [blame] | 536 | CookieInclusionStatus* status, |
| 537 | absl::optional<PartitionedCookieMap::iterator> cookie_partition_it); |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 538 | |
Lily Chen | d8d11db | 2021-02-25 19:50:52 | [diff] [blame] | 539 | // Inserts `cc` into cookies_. Returns an iterator that points to the inserted |
| 540 | // cookie in `cookies_`. Guarantee: all iterators to `cookies_` remain valid. |
| 541 | // Dispatches the change to `change_dispatcher_` iff `dispatch_change` is |
| 542 | // true. |
Ayu Ishii | 9f5e72dc | 2020-07-22 19:43:18 | [diff] [blame] | 543 | CookieMap::iterator InternalInsertCookie( |
| 544 | const std::string& key, |
| 545 | std::unique_ptr<CanonicalCookie> cc, |
| 546 | bool sync_to_store, |
Lily Chen | d8d11db | 2021-02-25 19:50:52 | [diff] [blame] | 547 | const CookieAccessResult& access_result, |
| 548 | bool dispatch_change = true); |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 549 | |
Dylan Cutler | 0b9a4e96 | 2021-09-13 17:34:25 | [diff] [blame] | 550 | // Returns true if the cookie should be (or is already) synced to the store. |
| 551 | // Used for cookies during insertion and deletion into the in-memory store. |
| 552 | bool ShouldUpdatePersistentStore(CanonicalCookie* cc); |
| 553 | |
| 554 | void LogCookieTypeToUMA(CanonicalCookie* cc, |
| 555 | const CookieAccessResult& access_result); |
| 556 | |
| 557 | // Inserts `cc` into partitioned_cookies_. Should only be used when |
| 558 | // cc->IsPartitioned() is true. |
| 559 | PartitionedCookieMapIterators InternalInsertPartitionedCookie( |
| 560 | std::string key, |
| 561 | std::unique_ptr<CanonicalCookie> cc, |
| 562 | bool sync_to_store, |
| 563 | const CookieAccessResult& access_result, |
| 564 | bool dispatch_change = true); |
| 565 | |
rdsmith | 2709eee | 2017-06-20 22:43:27 | [diff] [blame] | 566 | // Sets all cookies from |list| after deleting any equivalent cookie. |
| 567 | // For data gathering purposes, this routine is treated as if it is |
| 568 | // restoring saved cookies; some statistics are not gathered in this case. |
rdsmith | e5c701d | 2017-07-12 21:50:00 | [diff] [blame] | 569 | void SetAllCookies(CookieList list, SetCookiesCallback callback); |
droger | d5d1278c | 2015-03-17 19:21:51 | [diff] [blame] | 570 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 571 | void InternalUpdateCookieAccessTime(CanonicalCookie* cc, |
| 572 | const base::Time& current_time); |
| 573 | |
| 574 | // |deletion_cause| argument is used for collecting statistics and choosing |
Victor Costan | 14f47c1 | 2018-03-01 08:02:24 | [diff] [blame] | 575 | // the correct CookieChangeCause for OnCookieChange notifications. Guarantee: |
| 576 | // All iterators to cookies_, except for the deleted entry, remain valid. |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 577 | void InternalDeleteCookie(CookieMap::iterator it, |
| 578 | bool sync_to_store, |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 579 | DeletionCause deletion_cause); |
| 580 | |
Dylan Cutler | 0b9a4e96 | 2021-09-13 17:34:25 | [diff] [blame] | 581 | // Deletes a Partitioned cookie. Returns true if the deletion operation |
| 582 | // resulted in the CookieMap the cookie was stored in was deleted. |
| 583 | // |
| 584 | // If the CookieMap which contains the deleted cookie only has one entry, then |
| 585 | // this function will also delete the CookieMap from PartitionedCookieMap. |
| 586 | // This may invalidate the |cookie_partition_it| argument. |
| 587 | void InternalDeletePartitionedCookie( |
| 588 | PartitionedCookieMap::iterator partition_it, |
| 589 | CookieMap::iterator cookie_it, |
| 590 | bool sync_to_store, |
| 591 | DeletionCause deletion_cause); |
| 592 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 593 | // If the number of cookies for CookieMap key |key|, or globally, are |
| 594 | // over the preset maximums above, garbage collect, first for the host and |
| 595 | // then globally. See comments above garbage collection threshold |
Dylan Cutler | 0b9a4e96 | 2021-09-13 17:34:25 | [diff] [blame] | 596 | // constants for details. Also removes expired cookies. |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 597 | // |
| 598 | // Returns the number of cookies deleted (useful for debugging). |
jww | a26e439d | 2017-01-27 18:17:27 | [diff] [blame] | 599 | size_t GarbageCollect(const base::Time& current, const std::string& key); |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 600 | |
Dylan Cutler | 0b9a4e96 | 2021-09-13 17:34:25 | [diff] [blame] | 601 | // Run garbage collection for PartitionedCookieMap keys |cookie_partition_key| |
| 602 | // and |key|. |
| 603 | // |
| 604 | // Partitioned cookies are subject to different limits than unpartitioned |
| 605 | // cookies in order to prevent leaking entropy about user behavior across |
| 606 | // cookie partitions. |
| 607 | size_t GarbageCollectPartitionedCookies( |
| 608 | const base::Time& current, |
| 609 | const CookiePartitionKey& cookie_partition_key, |
| 610 | const std::string& key); |
| 611 | |
mkwst | e079ac41 | 2016-03-11 09:04:06 | [diff] [blame] | 612 | // Helper for GarbageCollect(). Deletes up to |purge_goal| cookies with a |
| 613 | // priority less than or equal to |priority| from |cookies|, while ensuring |
| 614 | // that at least the |to_protect| most-recent cookies are retained. |
jww | c00ac71 | 2016-05-05 22:21:44 | [diff] [blame] | 615 | // |protected_secure_cookies| specifies whether or not secure cookies should |
| 616 | // be protected from deletion. |
mkwst | e079ac41 | 2016-03-11 09:04:06 | [diff] [blame] | 617 | // |
| 618 | // |cookies| must be sorted from least-recent to most-recent. |
| 619 | // |
mkwst | e079ac41 | 2016-03-11 09:04:06 | [diff] [blame] | 620 | // Returns the number of cookies deleted. |
| 621 | size_t PurgeLeastRecentMatches(CookieItVector* cookies, |
| 622 | CookiePriority priority, |
| 623 | size_t to_protect, |
jww | c00ac71 | 2016-05-05 22:21:44 | [diff] [blame] | 624 | size_t purge_goal, |
| 625 | bool protect_secure_cookies); |
mkwst | e079ac41 | 2016-03-11 09:04:06 | [diff] [blame] | 626 | |
jww | 82d99c1 | 2015-11-25 18:39:53 | [diff] [blame] | 627 | // Helper for GarbageCollect(); can be called directly as well. Deletes all |
| 628 | // expired cookies in |itpair|. If |cookie_its| is non-NULL, all the |
| 629 | // non-expired cookies from |itpair| are appended to |cookie_its|. |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 630 | // |
| 631 | // Returns the number of cookies deleted. |
jww | 82d99c1 | 2015-11-25 18:39:53 | [diff] [blame] | 632 | size_t GarbageCollectExpired(const base::Time& current, |
| 633 | const CookieMapItPair& itpair, |
| 634 | CookieItVector* cookie_its); |
| 635 | |
Dylan Cutler | 0b9a4e96 | 2021-09-13 17:34:25 | [diff] [blame] | 636 | // Deletes all expired cookies in the double-keyed PartitionedCookie map in |
| 637 | // the CookieMap at |cookie_partition_it|. It deletes all cookies in that |
| 638 | // CookieMap in |itpair|. If |cookie_its| is non-NULL, all non-expired cookies |
| 639 | // from |itpair| are appended to |cookie_its|. |
| 640 | // |
| 641 | // Returns the number of cookies deleted. |
| 642 | size_t GarbageCollectExpiredPartitionedCookies( |
| 643 | const base::Time& current, |
| 644 | const PartitionedCookieMap::iterator& cookie_partition_it, |
| 645 | const CookieMapItPair& itpair, |
| 646 | CookieItVector* cookie_its); |
| 647 | |
| 648 | // Helper function to garbage collect all expired cookies in |
| 649 | // PartitionedCookieMap. |
| 650 | void GarbageCollectAllExpiredPartitionedCookies(const base::Time& current); |
| 651 | |
[email protected] | 8ad5d46 | 2013-05-02 08:45:26 | [diff] [blame] | 652 | // Helper for GarbageCollect(). Deletes all cookies in the range specified by |
| 653 | // [|it_begin|, |it_end|). Returns the number of cookies deleted. |
jww | 82d99c1 | 2015-11-25 18:39:53 | [diff] [blame] | 654 | size_t GarbageCollectDeleteRange(const base::Time& current, |
| 655 | DeletionCause cause, |
| 656 | CookieItVector::iterator cookie_its_begin, |
| 657 | CookieItVector::iterator cookie_its_end); |
| 658 | |
| 659 | // Helper for GarbageCollect(). Deletes cookies in |cookie_its| from least to |
| 660 | // most recently used, but only before |safe_date|. Also will stop deleting |
| 661 | // when the number of remaining cookies hits |purge_goal|. |
mmenke | f4721d99 | 2017-06-07 17:13:59 | [diff] [blame] | 662 | // |
| 663 | // Sets |earliest_time| to be the earliest last access time of a cookie that |
| 664 | // was not deleted, or base::Time() if no such cookie exists. |
jww | 82d99c1 | 2015-11-25 18:39:53 | [diff] [blame] | 665 | size_t GarbageCollectLeastRecentlyAccessed(const base::Time& current, |
| 666 | const base::Time& safe_date, |
| 667 | size_t purge_goal, |
mmenke | f4721d99 | 2017-06-07 17:13:59 | [diff] [blame] | 668 | CookieItVector cookie_its, |
| 669 | base::Time* earliest_time); |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 670 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 671 | bool HasCookieableScheme(const GURL& url); |
| 672 | |
Lily Chen | b0eedc2 | 2020-10-26 16:34:42 | [diff] [blame] | 673 | // Get the cookie's access semantics (LEGACY or NONLEGACY), by checking for a |
Lily Chen | b0ca3f7 | 2019-12-05 18:06:29 | [diff] [blame] | 674 | // value from the cookie access delegate, if it is non-null. Otherwise returns |
| 675 | // UNKNOWN. |
Lily Chen | 70f997f | 2019-10-07 22:01:37 | [diff] [blame] | 676 | CookieAccessSemantics GetAccessSemanticsForCookie( |
| 677 | const CanonicalCookie& cookie) const; |
| 678 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 679 | // Statistics support |
| 680 | |
| 681 | // This function should be called repeatedly, and will record |
| 682 | // statistics if a sufficient time period has passed. |
| 683 | void RecordPeriodicStats(const base::Time& current_time); |
| 684 | |
Lily Chen | 229623d7 | 2020-06-01 17:20:14 | [diff] [blame] | 685 | // Records the aforementioned stats if we have already finished loading all |
| 686 | // cookies. Returns whether stats were recorded. |
| 687 | bool DoRecordPeriodicStats(); |
| 688 | |
cfredric | 326a0bc | 2022-01-12 18:51:30 | [diff] [blame] | 689 | // Records periodic stats related to First-Party Sets usage. Note that since |
| 690 | // First-Party Sets presents a potentially asynchronous interface, these stats |
| 691 | // may be collected asynchronously w.r.t. the rest of the stats collected by |
| 692 | // `RecordPeriodicStats`. |
cfredric | 326a0bc | 2022-01-12 18:51:30 | [diff] [blame] | 693 | void RecordPeriodicFirstPartySetsStats( |
Chris Fredrickson | c2efa96f | 2022-08-04 20:40:44 | [diff] [blame] | 694 | base::flat_map<SchemefulSite, FirstPartySetEntry> sets) const; |
cfredric | 326a0bc | 2022-01-12 18:51:30 | [diff] [blame] | 695 | |
Maks Orlovich | 323efaf | 2018-03-06 02:56:39 | [diff] [blame] | 696 | // Defers the callback until the full coookie database has been loaded. If |
| 697 | // it's already been loaded, runs the callback synchronously. |
rdsmith | e5c701d | 2017-07-12 21:50:00 | [diff] [blame] | 698 | void DoCookieCallback(base::OnceClosure callback); |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 699 | |
Maks Orlovich | 323efaf | 2018-03-06 02:56:39 | [diff] [blame] | 700 | // Defers the callback until the cookies relevant to given URL have been |
| 701 | // loaded. If they've already been loaded, runs the callback synchronously. |
rdsmith | e5c701d | 2017-07-12 21:50:00 | [diff] [blame] | 702 | void DoCookieCallbackForURL(base::OnceClosure callback, const GURL& url); |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 703 | |
Maks Orlovich | 323efaf | 2018-03-06 02:56:39 | [diff] [blame] | 704 | // Defers the callback until the cookies relevant to given host or domain |
| 705 | // have been loaded. If they've already been loaded, runs the callback |
| 706 | // synchronously. |
| 707 | void DoCookieCallbackForHostOrDomain(base::OnceClosure callback, |
| 708 | base::StringPiece host_or_domain); |
| 709 | |
Steven Bingler | 0420a375 | 2020-11-20 21:40:48 | [diff] [blame] | 710 | // Checks to see if a cookie is being sent to the same port it was set by. For |
| 711 | // metrics. |
| 712 | // |
| 713 | // This is in CookieMonster because only CookieMonster uses it. It's otherwise |
| 714 | // a standalone utility function. |
| 715 | static CookieSentToSamePort IsCookieSentToSamePortThatSetIt( |
| 716 | const GURL& destination, |
| 717 | int source_port, |
| 718 | CookieSourceScheme source_scheme); |
| 719 | |
Dylan Cutler | 03d2c76c | 2022-02-18 02:23:15 | [diff] [blame] | 720 | // TODO(crbug.com/1296161): Delete this when the partitioned cookies Origin |
| 721 | // Trial ends. |
| 722 | void OnConvertPartitionedCookiesToUnpartitioned(const GURL& url); |
| 723 | void ConvertPartitionedCookie(const net::CanonicalCookie& cookie, |
| 724 | const GURL& url); |
| 725 | |
Lily Chen | 229623d7 | 2020-06-01 17:20:14 | [diff] [blame] | 726 | // Set of keys (eTLD+1's) for which non-expired cookies have |
| 727 | // been evicted for hitting the per-domain max. The size of this set is |
| 728 | // histogrammed periodically. The size is limited to |kMaxDomainPurgedKeys|. |
Chris Fredrickson | 4f22b326 | 2022-09-30 19:57:30 | [diff] [blame^] | 729 | std::set<std::string> domain_purged_keys_ GUARDED_BY_CONTEXT(thread_checker_); |
Lily Chen | 229623d7 | 2020-06-01 17:20:14 | [diff] [blame] | 730 | |
| 731 | // The number of distinct keys (eTLD+1's) currently present in the |cookies_| |
| 732 | // multimap. This is histogrammed periodically. |
Tsuyoshi Horo | 432981d5 | 2022-06-09 09:50:13 | [diff] [blame] | 733 | size_t num_keys_ = 0u; |
Lily Chen | 229623d7 | 2020-06-01 17:20:14 | [diff] [blame] | 734 | |
Chris Fredrickson | 4f22b326 | 2022-09-30 19:57:30 | [diff] [blame^] | 735 | CookieMap cookies_ GUARDED_BY_CONTEXT(thread_checker_); |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 736 | |
Chris Fredrickson | 4f22b326 | 2022-09-30 19:57:30 | [diff] [blame^] | 737 | PartitionedCookieMap partitioned_cookies_ GUARDED_BY_CONTEXT(thread_checker_); |
Dylan Cutler | 0b9a4e96 | 2021-09-13 17:34:25 | [diff] [blame] | 738 | |
| 739 | // Number of distinct partitioned cookies globally. This is used to enforce a |
| 740 | // global maximum on the number of partitioned cookies. |
Tsuyoshi Horo | 432981d5 | 2022-06-09 09:50:13 | [diff] [blame] | 741 | size_t num_partitioned_cookies_ = 0u; |
Dylan Cutler | 0b9a4e96 | 2021-09-13 17:34:25 | [diff] [blame] | 742 | |
Chris Fredrickson | b4cf840 | 2022-09-23 22:59:59 | [diff] [blame] | 743 | bool same_party_attribute_enabled_ = false; |
| 744 | |
Victor Costan | 14f47c1 | 2018-03-01 08:02:24 | [diff] [blame] | 745 | CookieMonsterChangeDispatcher change_dispatcher_; |
| 746 | |
erikchen | 1dd72a7 | 2015-05-06 20:45:05 | [diff] [blame] | 747 | // Indicates whether the cookie store has been initialized. |
Tsuyoshi Horo | 432981d5 | 2022-06-09 09:50:13 | [diff] [blame] | 748 | bool initialized_ = false; |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 749 | |
erikchen | 1dd72a7 | 2015-05-06 20:45:05 | [diff] [blame] | 750 | // Indicates whether the cookie store has started fetching all cookies. |
Tsuyoshi Horo | 432981d5 | 2022-06-09 09:50:13 | [diff] [blame] | 751 | bool started_fetching_all_cookies_ = false; |
erikchen | 1dd72a7 | 2015-05-06 20:45:05 | [diff] [blame] | 752 | // Indicates whether the cookie store has finished fetching all cookies. |
Tsuyoshi Horo | 432981d5 | 2022-06-09 09:50:13 | [diff] [blame] | 753 | bool finished_fetching_all_cookies_ = false; |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 754 | |
| 755 | // List of domain keys that have been loaded from the DB. |
| 756 | std::set<std::string> keys_loaded_; |
| 757 | |
| 758 | // Map of domain keys to their associated task queues. These tasks are blocked |
| 759 | // until all cookies for the associated domain key eTLD+1 are loaded from the |
| 760 | // backend store. |
Brett Wilson | c6a0c82 | 2017-09-12 00:04:29 | [diff] [blame] | 761 | std::map<std::string, base::circular_deque<base::OnceClosure>> |
Chris Fredrickson | 4f22b326 | 2022-09-30 19:57:30 | [diff] [blame^] | 762 | tasks_pending_for_key_ GUARDED_BY_CONTEXT(thread_checker_); |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 763 | |
| 764 | // Queues tasks that are blocked until all cookies are loaded from the backend |
| 765 | // store. |
Chris Fredrickson | 4f22b326 | 2022-09-30 19:57:30 | [diff] [blame^] | 766 | base::circular_deque<base::OnceClosure> tasks_pending_ |
| 767 | GUARDED_BY_CONTEXT(thread_checker_); |
mmenke | f49fca0e | 2016-03-08 12:46:24 | [diff] [blame] | 768 | |
| 769 | // Once a global cookie task has been seen, all per-key tasks must be put in |
| 770 | // |tasks_pending_| instead of |tasks_pending_for_key_| to ensure a reasonable |
rdsmith | e5c701d | 2017-07-12 21:50:00 | [diff] [blame] | 771 | // view of the cookie store. This is more to ensure fancy cookie export/import |
mmenke | f49fca0e | 2016-03-08 12:46:24 | [diff] [blame] | 772 | // code has a consistent view of the CookieStore, rather than out of concern |
| 773 | // for typical use. |
Tsuyoshi Horo | 432981d5 | 2022-06-09 09:50:13 | [diff] [blame] | 774 | bool seen_global_task_ = false; |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 775 | |
Helen Li | cd0fab86 | 2018-08-13 16:07:53 | [diff] [blame] | 776 | NetLogWithSource net_log_; |
| 777 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 778 | scoped_refptr<PersistentCookieStore> store_; |
| 779 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 780 | // Minimum delay after updating a cookie's LastAccessDate before we will |
| 781 | // update it again. |
| 782 | const base::TimeDelta last_access_threshold_; |
| 783 | |
| 784 | // Approximate date of access time of least recently accessed cookie |
| 785 | // in |cookies_|. Note that this is not guaranteed to be accurate, only a) |
| 786 | // to be before or equal to the actual time, and b) to be accurate |
mmenke | f4721d99 | 2017-06-07 17:13:59 | [diff] [blame] | 787 | // immediately after a garbage collection that scans through all the cookies |
| 788 | // (When garbage collection does not scan through all cookies, it may not be |
| 789 | // updated). This value is used to determine whether global garbage collection |
| 790 | // might find cookies to purge. Note: The default Time() constructor will |
| 791 | // create a value that compares earlier than any other time value, which is |
| 792 | // wanted. Thus this value is not initialized. |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 793 | base::Time earliest_access_time_; |
| 794 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 795 | std::vector<std::string> cookieable_schemes_; |
| 796 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 797 | base::Time last_statistic_record_time_; |
| 798 | |
Tsuyoshi Horo | 432981d5 | 2022-06-09 09:50:13 | [diff] [blame] | 799 | bool persist_session_cookies_ = false; |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 800 | |
Kirubel Aklilu | c9b4e41 | 2022-01-12 01:00:01 | [diff] [blame] | 801 | bool first_party_sets_enabled_; |
| 802 | |
Chris Fredrickson | 4f22b326 | 2022-09-30 19:57:30 | [diff] [blame^] | 803 | THREAD_CHECKER(thread_checker_); |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 804 | |
Jeremy Roman | d54000b2 | 2019-07-08 18:40:16 | [diff] [blame] | 805 | base::WeakPtrFactory<CookieMonster> weak_ptr_factory_{this}; |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 806 | }; |
| 807 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 808 | typedef base::RefCountedThreadSafe<CookieMonster::PersistentCookieStore> |
| 809 | RefcountedPersistentCookieStore; |
| 810 | |
[email protected] | c1b6e10 | 2013-04-10 20:54:49 | [diff] [blame] | 811 | class NET_EXPORT CookieMonster::PersistentCookieStore |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 812 | : public RefcountedPersistentCookieStore { |
| 813 | public: |
Maks Orlovich | 108cb4c | 2019-03-26 20:24:57 | [diff] [blame] | 814 | typedef base::OnceCallback<void( |
| 815 | std::vector<std::unique_ptr<CanonicalCookie>>)> |
[email protected] | 5b9bc35 | 2012-07-18 13:13:34 | [diff] [blame] | 816 | LoadedCallback; |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 817 | |
Peter Boström | 407869b | 2021-10-07 04:42:48 | [diff] [blame] | 818 | PersistentCookieStore(const PersistentCookieStore&) = delete; |
| 819 | PersistentCookieStore& operator=(const PersistentCookieStore&) = delete; |
| 820 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 821 | // Initializes the store and retrieves the existing cookies. This will be |
| 822 | // called only once at startup. The callback will return all the cookies |
| 823 | // that are not yet returned to CookieMonster by previous priority loads. |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 824 | // |
| 825 | // |loaded_callback| may not be NULL. |
Helen Li | 92a29f10 | 2018-08-15 23:02:26 | [diff] [blame] | 826 | // |net_log| is a NetLogWithSource that may be copied if the persistent |
| 827 | // store wishes to log NetLog events. |
Maks Orlovich | 108cb4c | 2019-03-26 20:24:57 | [diff] [blame] | 828 | virtual void Load(LoadedCallback loaded_callback, |
Helen Li | 92a29f10 | 2018-08-15 23:02:26 | [diff] [blame] | 829 | const NetLogWithSource& net_log) = 0; |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 830 | |
| 831 | // Does a priority load of all cookies for the domain key (eTLD+1). The |
| 832 | // callback will return all the cookies that are not yet returned by previous |
| 833 | // loads, which includes cookies for the requested domain key if they are not |
| 834 | // already returned, plus all cookies that are chain-loaded and not yet |
| 835 | // returned to CookieMonster. |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 836 | // |
| 837 | // |loaded_callback| may not be NULL. |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 838 | virtual void LoadCookiesForKey(const std::string& key, |
Maks Orlovich | 108cb4c | 2019-03-26 20:24:57 | [diff] [blame] | 839 | LoadedCallback loaded_callback) = 0; |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 840 | |
| 841 | virtual void AddCookie(const CanonicalCookie& cc) = 0; |
| 842 | virtual void UpdateCookieAccessTime(const CanonicalCookie& cc) = 0; |
| 843 | virtual void DeleteCookie(const CanonicalCookie& cc) = 0; |
| 844 | |
[email protected] | bf510ed | 2012-06-05 08:31:43 | [diff] [blame] | 845 | // Instructs the store to not discard session only cookies on shutdown. |
| 846 | virtual void SetForceKeepSessionState() = 0; |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 847 | |
Nick Harper | 14e2333 | 2017-07-28 00:27:23 | [diff] [blame] | 848 | // Sets a callback that will be run before the store flushes. If |callback| |
| 849 | // performs any async operations, the store will not wait for those to finish |
| 850 | // before flushing. |
Lily Chen | 9934f7e | 2019-03-13 19:16:55 | [diff] [blame] | 851 | virtual void SetBeforeCommitCallback(base::RepeatingClosure callback) = 0; |
Nick Harper | 14e2333 | 2017-07-28 00:27:23 | [diff] [blame] | 852 | |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 853 | // Flushes the store and posts |callback| when complete. |callback| may be |
| 854 | // NULL. |
rdsmith | 7ac8171 | 2017-06-22 17:09:54 | [diff] [blame] | 855 | virtual void Flush(base::OnceClosure callback) = 0; |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 856 | |
| 857 | protected: |
Christian Dullweber | ff11c45 | 2021-05-12 17:04:45 | [diff] [blame] | 858 | PersistentCookieStore() = default; |
| 859 | virtual ~PersistentCookieStore() = default; |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 860 | |
| 861 | private: |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 862 | friend class base::RefCountedThreadSafe<PersistentCookieStore>; |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 863 | }; |
| 864 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 865 | } // namespace net |
| 866 | |
| 867 | #endif // NET_COOKIES_COOKIE_MONSTER_H_ |