[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 1 | // 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] | 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> |
| 17 | #include <utility> |
| 18 | #include <vector> |
| 19 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 20 | #include "base/callback_forward.h" |
Brett Wilson | c6a0c82 | 2017-09-12 00:04:29 | [diff] [blame] | 21 | #include "base/containers/circular_deque.h" |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 22 | #include "base/gtest_prod_util.h" |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 23 | #include "base/macros.h" |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 24 | #include "base/memory/ref_counted.h" |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 25 | #include "base/memory/weak_ptr.h" |
Lily Chen | b851acc | 2019-08-07 15:54:44 | [diff] [blame] | 26 | #include "base/optional.h" |
Maks Orlovich | 323efaf | 2018-03-06 02:56:39 | [diff] [blame] | 27 | #include "base/strings/string_piece.h" |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 28 | #include "base/threading/thread_checker.h" |
[email protected] | 9da992db | 2013-06-28 05:40:47 | [diff] [blame] | 29 | #include "base/time/time.h" |
[email protected] | 565c3f4 | 2012-08-14 14:22:58 | [diff] [blame] | 30 | #include "net/base/net_export.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" |
Victor Costan | 14f47c1 | 2018-03-01 08:02:24 | [diff] [blame] | 34 | #include "net/cookies/cookie_monster_change_dispatcher.h" |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 35 | #include "net/cookies/cookie_store.h" |
Helen Li | cd0fab86 | 2018-08-13 16:07:53 | [diff] [blame] | 36 | #include "net/log/net_log_with_source.h" |
ellyjones | 399e35a2 | 2014-10-27 11:09:56 | [diff] [blame] | 37 | #include "url/gurl.h" |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 38 | |
| 39 | namespace base { |
[email protected] | de41555 | 2013-01-23 04:12:17 | [diff] [blame] | 40 | class HistogramBase; |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 41 | } // namespace base |
| 42 | |
| 43 | namespace net { |
| 44 | |
Victor Costan | 14f47c1 | 2018-03-01 08:02:24 | [diff] [blame] | 45 | class CookieChangeDispatcher; |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 46 | |
| 47 | // The cookie monster is the system for storing and retrieving cookies. It has |
| 48 | // an in-memory list of all cookies, and synchronizes non-session cookies to an |
| 49 | // optional permanent storage that implements the PersistentCookieStore |
| 50 | // interface. |
| 51 | // |
mmenke | 96f3bab | 2016-01-22 17:34:02 | [diff] [blame] | 52 | // Tasks may be deferred if all affected cookies are not yet loaded from the |
| 53 | // backing store. Otherwise, callbacks may be invoked immediately. |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 54 | // |
| 55 | // A cookie task is either pending loading of the entire cookie store, or |
Maks Orlovich | 323efaf | 2018-03-06 02:56:39 | [diff] [blame] | 56 | // loading of cookies for a specific domain key (GetKey(), roughly eTLD+1). In |
| 57 | // the former case, the cookie callback will be queued in tasks_pending_ while |
| 58 | // PersistentCookieStore chain loads the cookie store on DB thread. In the |
| 59 | // latter case, the cookie callback will be queued in tasks_pending_for_key_ |
| 60 | // while PermanentCookieStore loads cookies for the specified domain key on DB |
| 61 | // thread. |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 62 | class NET_EXPORT CookieMonster : public CookieStore { |
| 63 | public: |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 64 | class PersistentCookieStore; |
| 65 | |
| 66 | // Terminology: |
| 67 | // * The 'top level domain' (TLD) of an internet domain name is |
| 68 | // the terminal "." free substring (e.g. "com" for google.com |
| 69 | // or world.std.com). |
| 70 | // * The 'effective top level domain' (eTLD) is the longest |
| 71 | // "." initiated terminal substring of an internet domain name |
| 72 | // that is controlled by a general domain registrar. |
| 73 | // (e.g. "co.uk" for news.bbc.co.uk). |
| 74 | // * The 'effective top level domain plus one' (eTLD+1) is the |
| 75 | // shortest "." delimited terminal substring of an internet |
| 76 | // domain name that is not controlled by a general domain |
| 77 | // registrar (e.g. "bbc.co.uk" for news.bbc.co.uk, or |
| 78 | // "google.com" for news.google.com). The general assumption |
| 79 | // is that all hosts and domains under an eTLD+1 share some |
| 80 | // administrative control. |
| 81 | |
| 82 | // CookieMap is the central data structure of the CookieMonster. It |
| 83 | // is a map whose values are pointers to CanonicalCookie data |
| 84 | // structures (the data structures are owned by the CookieMonster |
| 85 | // and must be destroyed when removed from the map). The key is based on the |
| 86 | // effective domain of the cookies. If the domain of the cookie has an |
| 87 | // eTLD+1, that is the key for the map. If the domain of the cookie does not |
| 88 | // have an eTLD+1, the key of the map is the host the cookie applies to (it is |
| 89 | // not legal to have domain cookies without an eTLD+1). This rule |
| 90 | // excludes cookies for, e.g, ".com", ".co.uk", or ".internalnetwork". |
| 91 | // This behavior is the same as the behavior in Firefox v 3.6.10. |
| 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 | |
| 107 | // Cookie garbage collection thresholds. Based off of the Mozilla defaults. |
| 108 | // When the number of cookies gets to k{Domain,}MaxCookies |
| 109 | // purge down to k{Domain,}MaxCookies - k{Domain,}PurgeCookies. |
| 110 | // It might seem scary to have a high purge value, but really it's not. |
| 111 | // You just make sure that you increase the max to cover the increase |
| 112 | // in purge, and we would have been purging the same number of cookies. |
| 113 | // We're just going through the garbage collection process less often. |
| 114 | // Note that the DOMAIN values are per eTLD+1; see comment for the |
| 115 | // CookieMap typedef. So, e.g., the maximum number of cookies allowed for |
| 116 | // google.com and all of its subdomains will be 150-180. |
| 117 | // |
| 118 | // Any cookies accessed more recently than kSafeFromGlobalPurgeDays will not |
| 119 | // be evicted by global garbage collection, even if we have more than |
| 120 | // kMaxCookies. This does not affect domain garbage collection. |
| 121 | static const size_t kDomainMaxCookies; |
| 122 | static const size_t kDomainPurgeCookies; |
| 123 | static const size_t kMaxCookies; |
| 124 | static const size_t kPurgeCookies; |
| 125 | |
| 126 | // Quota for cookies with {low, medium, high} priorities within a domain. |
mkwst | 8773435 | 2016-03-03 17:36:23 | [diff] [blame] | 127 | static const size_t kDomainCookiesQuotaLow; |
| 128 | static const size_t kDomainCookiesQuotaMedium; |
| 129 | static const size_t kDomainCookiesQuotaHigh; |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 130 | |
Matt Menke | 477ab63 | 2019-06-27 23:12:17 | [diff] [blame] | 131 | // The number of days since last access that cookies will not be subject |
| 132 | // to global garbage collection. |
| 133 | static const int kSafeFromGlobalPurgeDays; |
| 134 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 135 | // The store passed in should not have had Init() called on it yet. This |
| 136 | // class will take care of initializing it. The backing store is NOT owned by |
| 137 | // this class, but it must remain valid for the duration of the cookie |
| 138 | // monster's existence. If |store| is NULL, then no backing store will be |
Nick Harper | 57142b1c | 2019-03-14 21:03:59 | [diff] [blame] | 139 | // updated. |net_log| must outlive the CookieMonster and can be null. |
Pritam | 8354cf70 | 2018-03-10 08:55:41 | [diff] [blame] | 140 | CookieMonster(scoped_refptr<PersistentCookieStore> store, |
Helen Li | fb313a9 | 2018-08-14 15:46:44 | [diff] [blame] | 141 | NetLog* net_log); |
nharper | 2b0ad9a | 2017-05-22 18:33:45 | [diff] [blame] | 142 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 143 | // Only used during unit testing. |
Helen Li | fb313a9 | 2018-08-14 15:46:44 | [diff] [blame] | 144 | // |net_log| must outlive the CookieMonster. |
Pritam | 8354cf70 | 2018-03-10 08:55:41 | [diff] [blame] | 145 | CookieMonster(scoped_refptr<PersistentCookieStore> store, |
Helen Li | fb313a9 | 2018-08-14 15:46:44 | [diff] [blame] | 146 | base::TimeDelta last_access_threshold, |
| 147 | NetLog* net_log); |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 148 | |
mmenke | 606c59c | 2016-03-07 18:20:55 | [diff] [blame] | 149 | ~CookieMonster() override; |
| 150 | |
rdsmith | 0e84cea | 2017-07-13 03:09:53 | [diff] [blame] | 151 | // Writes all the cookies in |list| into the store, replacing all cookies |
| 152 | // currently present in store. |
rdsmith | 2709eee | 2017-06-20 22:43:27 | [diff] [blame] | 153 | // This method does not flush the backend. |
| 154 | // TODO(rdsmith, mmenke): Do not use this function; it is deprecated |
| 155 | // and should be removed. |
| 156 | // See https://codereview.chromium.org/2882063002/#msg64. |
rdsmith | 7ac8171 | 2017-06-22 17:09:54 | [diff] [blame] | 157 | void SetAllCookiesAsync(const CookieList& list, SetCookiesCallback callback); |
droger | d5d1278c | 2015-03-17 19:21:51 | [diff] [blame] | 158 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 159 | // CookieStore implementation. |
rdsmith | a6ce444 | 2017-06-21 17:11:05 | [diff] [blame] | 160 | void SetCanonicalCookieAsync(std::unique_ptr<CanonicalCookie> cookie, |
Maks Orlovich | 44525ce | 2019-02-25 14:17:58 | [diff] [blame] | 161 | std::string source_scheme, |
Maks Orlovich | fdbc8be | 2019-03-18 18:34:52 | [diff] [blame] | 162 | const CookieOptions& options, |
rdsmith | 7ac8171 | 2017-06-22 17:09:54 | [diff] [blame] | 163 | SetCookiesCallback callback) override; |
rdsmith | 7ac8171 | 2017-06-22 17:09:54 | [diff] [blame] | 164 | void GetCookieListWithOptionsAsync(const GURL& url, |
| 165 | const CookieOptions& options, |
| 166 | GetCookieListCallback callback) override; |
Lily Chen | f068a76 | 2019-08-21 21:10:50 | [diff] [blame] | 167 | void GetAllCookiesAsync(GetAllCookiesCallback callback) override; |
Lily Chen | e2e9ae01 | 2019-10-09 20:02:54 | [diff] [blame^] | 168 | void GetAllCookiesWithAccessSemanticsAsync( |
| 169 | GetAllCookiesWithAccessSemanticsCallback callback) override; |
mmenke | 24379d5 | 2016-02-05 23:50:17 | [diff] [blame] | 170 | void DeleteCanonicalCookieAsync(const CanonicalCookie& cookie, |
rdsmith | 7ac8171 | 2017-06-22 17:09:54 | [diff] [blame] | 171 | DeleteCallback callback) override; |
Chris Mumford | d8ed9f8 | 2018-05-01 15:43:13 | [diff] [blame] | 172 | void DeleteAllCreatedInTimeRangeAsync( |
| 173 | const CookieDeletionInfo::TimeRange& creation_range, |
| 174 | DeleteCallback callback) override; |
Chris Mumford | 800caa6 | 2018-04-20 19:34:44 | [diff] [blame] | 175 | void DeleteAllMatchingInfoAsync(CookieDeletionInfo delete_info, |
| 176 | DeleteCallback callback) override; |
rdsmith | 7ac8171 | 2017-06-22 17:09:54 | [diff] [blame] | 177 | void DeleteSessionCookiesAsync(DeleteCallback) override; |
| 178 | void FlushStore(base::OnceClosure callback) override; |
mmenke | ded79da | 2016-02-06 08:28:51 | [diff] [blame] | 179 | void SetForceKeepSessionState() override; |
Victor Costan | 14f47c1 | 2018-03-01 08:02:24 | [diff] [blame] | 180 | CookieChangeDispatcher& GetChangeDispatcher() override; |
Nate Fischer | c6fb6cf | 2019-03-27 00:39:49 | [diff] [blame] | 181 | void SetCookieableSchemes(const std::vector<std::string>& schemes, |
| 182 | SetCookieableSchemesCallback callback) override; |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 183 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 184 | // Enables writing session cookies into the cookie database. If this this |
| 185 | // method is called, it must be called before first use of the instance |
| 186 | // (i.e. as part of the instance initialization process). |
| 187 | void SetPersistSessionCookies(bool persist_session_cookies); |
| 188 | |
[email protected] | 97a3b6e | 2012-06-12 01:53:56 | [diff] [blame] | 189 | // Determines if the scheme of the URL is a scheme that cookies will be |
| 190 | // stored for. |
| 191 | bool IsCookieableScheme(const std::string& scheme); |
| 192 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 193 | // The default list of schemes the cookie monster can handle. |
[email protected] | 5edff3c5 | 2014-06-23 20:27:48 | [diff] [blame] | 194 | static const char* const kDefaultCookieableSchemes[]; |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 195 | static const int kDefaultCookieableSchemesCount; |
| 196 | |
Maks Orlovich | 5cf437b0 | 2018-03-27 04:40:42 | [diff] [blame] | 197 | void DumpMemoryStats(base::trace_event::ProcessMemoryDump* pmd, |
| 198 | const std::string& parent_absolute_name) const override; |
| 199 | |
Maks Orlovich | 323efaf | 2018-03-06 02:56:39 | [diff] [blame] | 200 | // Find a key based on the given domain, which will be used to find all |
| 201 | // cookies potentially relevant to it. This is used for lookup in cookies_ as |
| 202 | // well as for PersistentCookieStore::LoadCookiesForKey. See comment on keys |
| 203 | // before the CookieMap typedef. |
| 204 | static std::string GetKey(base::StringPiece domain); |
| 205 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 206 | private: |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 207 | // For garbage collection constants. |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 208 | FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestHostGarbageCollection); |
mmenke | f4721d99 | 2017-06-07 17:13:59 | [diff] [blame] | 209 | FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, |
| 210 | GarbageCollectWithSecureCookiesOnly); |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 211 | FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestGCTimes); |
| 212 | |
| 213 | // For validation of key values. |
| 214 | FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestDomainTree); |
| 215 | FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestImport); |
| 216 | FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, GetKey); |
| 217 | FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestGetKey); |
| 218 | |
| 219 | // For FindCookiesForKey. |
| 220 | FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, ShortLivedSessionCookies); |
| 221 | |
estark | 7feb65c2b | 2015-08-21 23:38:20 | [diff] [blame] | 222 | // For CookieSource histogram enum. |
| 223 | FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, CookieSourceHistogram); |
| 224 | |
jww | 31e3263 | 2015-12-16 23:38:34 | [diff] [blame] | 225 | // For kSafeFromGlobalPurgeDays in CookieStore. |
jww | a26e439d | 2017-01-27 18:17:27 | [diff] [blame] | 226 | FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, EvictSecureCookies); |
jww | 82d99c1 | 2015-11-25 18:39:53 | [diff] [blame] | 227 | |
jww | 31e3263 | 2015-12-16 23:38:34 | [diff] [blame] | 228 | // For CookieDeleteEquivalent histogram enum. |
| 229 | FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, |
| 230 | CookieDeleteEquivalentHistogramTest); |
jww | 31e3263 | 2015-12-16 23:38:34 | [diff] [blame] | 231 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 232 | // Internal reasons for deletion, used to populate informative histograms |
| 233 | // and to provide a public cause for onCookieChange notifications. |
| 234 | // |
| 235 | // 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] | 236 | // the CookieChangeCause mapping inside ChangeCauseMapping. New items (if |
| 237 | // necessary) should be added at the end of the list, just before |
Nick Harper | 7a6683a | 2018-01-30 20:42:52 | [diff] [blame] | 238 | // DELETE_COOKIE_LAST_ENTRY. |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 239 | enum DeletionCause { |
| 240 | DELETE_COOKIE_EXPLICIT = 0, |
mkwst | aa07ee8 | 2016-03-11 15:32:14 | [diff] [blame] | 241 | DELETE_COOKIE_OVERWRITE = 1, |
| 242 | DELETE_COOKIE_EXPIRED = 2, |
| 243 | DELETE_COOKIE_EVICTED = 3, |
| 244 | DELETE_COOKIE_DUPLICATE_IN_BACKING_STORE = 4, |
| 245 | DELETE_COOKIE_DONT_RECORD = 5, // For final cleanup after flush to store. |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 246 | |
mkwst | aa07ee8 | 2016-03-11 15:32:14 | [diff] [blame] | 247 | // Cookies evicted during domain-level garbage collection. |
| 248 | DELETE_COOKIE_EVICTED_DOMAIN = 6, |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 249 | |
mkwst | aa07ee8 | 2016-03-11 15:32:14 | [diff] [blame] | 250 | // Cookies evicted during global garbage collection (which takes place after |
| 251 | // domain-level garbage collection fails to bring the cookie store under |
| 252 | // the overall quota. |
| 253 | DELETE_COOKIE_EVICTED_GLOBAL = 7, |
| 254 | |
| 255 | // #8 was DELETE_COOKIE_EVICTED_DOMAIN_PRE_SAFE |
| 256 | // #9 was DELETE_COOKIE_EVICTED_DOMAIN_POST_SAFE |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 257 | |
| 258 | // A common idiom is to remove a cookie by overwriting it with an |
| 259 | // already-expired expiration date. This captures that case. |
mkwst | aa07ee8 | 2016-03-11 15:32:14 | [diff] [blame] | 260 | DELETE_COOKIE_EXPIRED_OVERWRITE = 10, |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 261 | |
[email protected] | 6210ce5 | 2013-09-20 03:33:14 | [diff] [blame] | 262 | // Cookies are not allowed to contain control characters in the name or |
| 263 | // value. However, we used to allow them, so we are now evicting any such |
| 264 | // cookies as we load them. See http://crbug.com/238041. |
mkwst | aa07ee8 | 2016-03-11 15:32:14 | [diff] [blame] | 265 | DELETE_COOKIE_CONTROL_CHAR = 11, |
[email protected] | 6210ce5 | 2013-09-20 03:33:14 | [diff] [blame] | 266 | |
jww | 82d99c1 | 2015-11-25 18:39:53 | [diff] [blame] | 267 | // When strict secure cookies is enabled, non-secure cookies are evicted |
| 268 | // right after expired cookies. |
mkwst | aa07ee8 | 2016-03-11 15:32:14 | [diff] [blame] | 269 | DELETE_COOKIE_NON_SECURE = 12, |
jww | 82d99c1 | 2015-11-25 18:39:53 | [diff] [blame] | 270 | |
Nick Harper | 7a6683a | 2018-01-30 20:42:52 | [diff] [blame] | 271 | DELETE_COOKIE_LAST_ENTRY = 13 |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 272 | }; |
| 273 | |
mkwst | c1aa4cc | 2015-04-03 19:57:45 | [diff] [blame] | 274 | // This enum is used to generate a histogramed bitmask measureing the types |
| 275 | // of stored cookies. Please do not reorder the list when adding new entries. |
| 276 | // New items MUST be added at the end of the list, just before |
| 277 | // COOKIE_TYPE_LAST_ENTRY; |
| 278 | enum CookieType { |
mkwst | 4654941 | 2016-02-01 10:05:37 | [diff] [blame] | 279 | COOKIE_TYPE_SAME_SITE = 0, |
mkwst | c1aa4cc | 2015-04-03 19:57:45 | [diff] [blame] | 280 | COOKIE_TYPE_HTTPONLY, |
| 281 | COOKIE_TYPE_SECURE, |
| 282 | COOKIE_TYPE_LAST_ENTRY |
| 283 | }; |
| 284 | |
estark | 7feb65c2b | 2015-08-21 23:38:20 | [diff] [blame] | 285 | // Used to populate a histogram containing information about the |
| 286 | // sources of Secure and non-Secure cookies: that is, whether such |
| 287 | // cookies are set by origins with cryptographic or non-cryptographic |
| 288 | // schemes. Please do not reorder the list when adding new |
| 289 | // entries. New items MUST be added at the end of the list, just |
| 290 | // before COOKIE_SOURCE_LAST_ENTRY. |
| 291 | // |
| 292 | // COOKIE_SOURCE_(NON)SECURE_COOKIE_(NON)CRYPTOGRAPHIC_SCHEME means |
| 293 | // that a cookie was set or overwritten from a URL with the given type |
| 294 | // of scheme. This enum should not be used when cookies are *cleared*, |
| 295 | // because its purpose is to understand if Chrome can deprecate the |
| 296 | // ability of HTTP urls to set/overwrite Secure cookies. |
| 297 | enum CookieSource { |
| 298 | COOKIE_SOURCE_SECURE_COOKIE_CRYPTOGRAPHIC_SCHEME = 0, |
| 299 | COOKIE_SOURCE_SECURE_COOKIE_NONCRYPTOGRAPHIC_SCHEME, |
| 300 | COOKIE_SOURCE_NONSECURE_COOKIE_CRYPTOGRAPHIC_SCHEME, |
| 301 | COOKIE_SOURCE_NONSECURE_COOKIE_NONCRYPTOGRAPHIC_SCHEME, |
| 302 | COOKIE_SOURCE_LAST_ENTRY |
| 303 | }; |
| 304 | |
jww | 31e3263 | 2015-12-16 23:38:34 | [diff] [blame] | 305 | // Used to populate a histogram for cookie setting in the "delete equivalent" |
Mike West | 8614988 | 2017-07-28 10:41:49 | [diff] [blame] | 306 | // step. Measures total attempts to delete an equivalent cookie, and |
| 307 | // categorizes the outcome. |
jww | 31e3263 | 2015-12-16 23:38:34 | [diff] [blame] | 308 | // |
Mike West | 8614988 | 2017-07-28 10:41:49 | [diff] [blame] | 309 | // * COOKIE_DELETE_EQUIVALENT_ATTEMPT is incremented each time a cookie is |
| 310 | // set, causing the equivalent deletion algorithm to execute. |
| 311 | // |
| 312 | // * COOKIE_DELETE_EQUIVALENT_SKIPPING_SECURE is incremented when a non-secure |
| 313 | // cookie is ignored because an equivalent, but secure, cookie already |
| 314 | // exists. |
| 315 | // |
| 316 | // * COOKIE_DELETE_EQUIVALENT_WOULD_HAVE_DELETED is incremented when a cookie |
| 317 | // is skipped due to `secure` rules (e.g. whenever |
| 318 | // COOKIE_DELETE_EQUIVALENT_SKIPPING_SECURE is incremented), but would have |
| 319 | // caused a deletion without those rules. |
| 320 | // |
| 321 | // TODO(mkwst): Now that we've shipped strict secure cookie checks, we don't |
| 322 | // need this value anymore. |
| 323 | // |
| 324 | // * COOKIE_DELETE_EQUIVALENT_FOUND is incremented each time an equivalent |
| 325 | // cookie is found (and deleted). |
| 326 | // |
| 327 | // * COOKIE_DELETE_EQUIVALENT_FOUND_WITH_SAME_VALUE is incremented each time |
| 328 | // an equivalent cookie that also shared the same value with the new cookie |
| 329 | // is found (and deleted). |
| 330 | // |
| 331 | // Please do not reorder or remove entries. New entries must be added to the |
| 332 | // end of the list, just before COOKIE_DELETE_EQUIVALENT_LAST_ENTRY. |
jww | 31e3263 | 2015-12-16 23:38:34 | [diff] [blame] | 333 | enum CookieDeleteEquivalent { |
| 334 | COOKIE_DELETE_EQUIVALENT_ATTEMPT = 0, |
| 335 | COOKIE_DELETE_EQUIVALENT_FOUND, |
| 336 | COOKIE_DELETE_EQUIVALENT_SKIPPING_SECURE, |
| 337 | COOKIE_DELETE_EQUIVALENT_WOULD_HAVE_DELETED, |
Mike West | 8614988 | 2017-07-28 10:41:49 | [diff] [blame] | 338 | COOKIE_DELETE_EQUIVALENT_FOUND_WITH_SAME_VALUE, |
jww | 31e3263 | 2015-12-16 23:38:34 | [diff] [blame] | 339 | COOKIE_DELETE_EQUIVALENT_LAST_ENTRY |
| 340 | }; |
| 341 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 342 | // Record statistics every kRecordStatisticsIntervalSeconds of uptime. |
| 343 | static const int kRecordStatisticsIntervalSeconds = 10 * 60; |
| 344 | |
rdsmith | a6ce444 | 2017-06-21 17:11:05 | [diff] [blame] | 345 | // Sets a canonical cookie, deletes equivalents and performs garbage |
Maks Orlovich | fdbc8be | 2019-03-18 18:34:52 | [diff] [blame] | 346 | // collection. |source_scheme| indicates what scheme the cookie is being set |
| 347 | // from; secure cookies cannot be altered from insecure schemes, and some |
| 348 | // schemes may not be authorized. |
| 349 | // |
| 350 | // |options| indicates if this setting operation is allowed |
| 351 | // to affect http_only or same-site cookies. |
rdsmith | e5c701d | 2017-07-12 21:50:00 | [diff] [blame] | 352 | void SetCanonicalCookie(std::unique_ptr<CanonicalCookie> cookie, |
Maks Orlovich | 44525ce | 2019-02-25 14:17:58 | [diff] [blame] | 353 | std::string source_scheme, |
Maks Orlovich | fdbc8be | 2019-03-18 18:34:52 | [diff] [blame] | 354 | const CookieOptions& options, |
rdsmith | e5c701d | 2017-07-12 21:50:00 | [diff] [blame] | 355 | SetCookiesCallback callback); |
rdsmith | a6ce444 | 2017-06-21 17:11:05 | [diff] [blame] | 356 | |
Lily Chen | f068a76 | 2019-08-21 21:10:50 | [diff] [blame] | 357 | void GetAllCookies(GetAllCookiesCallback callback); |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 358 | |
Lily Chen | e2e9ae01 | 2019-10-09 20:02:54 | [diff] [blame^] | 359 | void AttachAccessSemanticsListForCookieList( |
| 360 | GetAllCookiesWithAccessSemanticsCallback callback, |
| 361 | const CookieList& cookie_list); |
| 362 | |
rdsmith | e5c701d | 2017-07-12 21:50:00 | [diff] [blame] | 363 | void GetCookieListWithOptions(const GURL& url, |
| 364 | const CookieOptions& options, |
| 365 | GetCookieListCallback callback); |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 366 | |
Chris Mumford | d8ed9f8 | 2018-05-01 15:43:13 | [diff] [blame] | 367 | void DeleteAllCreatedInTimeRange( |
| 368 | const CookieDeletionInfo::TimeRange& creation_range, |
| 369 | DeleteCallback callback); |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 370 | |
Chris Mumford | d8ed9f8 | 2018-05-01 15:43:13 | [diff] [blame] | 371 | void DeleteAllMatchingInfo(net::CookieDeletionInfo delete_info, |
Chris Mumford | 800caa6 | 2018-04-20 19:34:44 | [diff] [blame] | 372 | DeleteCallback callback); |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 373 | |
rdsmith | e5c701d | 2017-07-12 21:50:00 | [diff] [blame] | 374 | void DeleteCanonicalCookie(const CanonicalCookie& cookie, |
| 375 | DeleteCallback callback); |
mmenke | 24379d5 | 2016-02-05 23:50:17 | [diff] [blame] | 376 | |
rdsmith | e5c701d | 2017-07-12 21:50:00 | [diff] [blame] | 377 | void DeleteSessionCookies(DeleteCallback callback); |
[email protected] | 264807b | 2012-04-25 14:49:37 | [diff] [blame] | 378 | |
erikchen | 1dd72a7 | 2015-05-06 20:45:05 | [diff] [blame] | 379 | // The first access to the cookie store initializes it. This method should be |
| 380 | // called before any access to the cookie store. |
| 381 | void MarkCookieStoreAsInitialized(); |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 382 | |
erikchen | 1dd72a7 | 2015-05-06 20:45:05 | [diff] [blame] | 383 | // Fetches all cookies if the backing store exists and they're not already |
| 384 | // being fetched. |
erikchen | 1dd72a7 | 2015-05-06 20:45:05 | [diff] [blame] | 385 | void FetchAllCookiesIfNecessary(); |
| 386 | |
| 387 | // Fetches all cookies from the backing store. |
erikchen | 1dd72a7 | 2015-05-06 20:45:05 | [diff] [blame] | 388 | void FetchAllCookies(); |
| 389 | |
| 390 | // Whether all cookies should be fetched as soon as any is requested. |
| 391 | bool ShouldFetchAllCookiesWhenFetchingAnyCookie(); |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 392 | |
| 393 | // Stores cookies loaded from the backing store and invokes any deferred |
| 394 | // calls. |beginning_time| should be the moment PersistentCookieStore::Load |
| 395 | // was invoked and is used for reporting histogram_time_blocked_on_load_. |
| 396 | // See PersistentCookieStore::Load for details on the contents of cookies. |
| 397 | void OnLoaded(base::TimeTicks beginning_time, |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 398 | std::vector<std::unique_ptr<CanonicalCookie>> cookies); |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 399 | |
| 400 | // Stores cookies loaded from the backing store and invokes the deferred |
| 401 | // task(s) pending loading of cookies associated with the domain key |
Maks Orlovich | 323efaf | 2018-03-06 02:56:39 | [diff] [blame] | 402 | // (GetKey, roughly eTLD+1). Called when all cookies for the domain key have |
| 403 | // been loaded from DB. See PersistentCookieStore::Load for details on the |
| 404 | // contents of cookies. |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 405 | void OnKeyLoaded(const std::string& key, |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 406 | std::vector<std::unique_ptr<CanonicalCookie>> cookies); |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 407 | |
| 408 | // Stores the loaded cookies. |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 409 | void StoreLoadedCookies( |
| 410 | std::vector<std::unique_ptr<CanonicalCookie>> cookies); |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 411 | |
| 412 | // Invokes deferred calls. |
| 413 | void InvokeQueue(); |
| 414 | |
| 415 | // Checks that |cookies_| matches our invariants, and tries to repair any |
| 416 | // inconsistencies. (In other words, it does not have duplicate cookies). |
| 417 | void EnsureCookiesMapIsValid(); |
| 418 | |
| 419 | // Checks for any duplicate cookies for CookieMap key |key| which lie between |
| 420 | // |begin| and |end|. If any are found, all but the most recent are deleted. |
ellyjones | cabf5742 | 2015-08-21 18:44:51 | [diff] [blame] | 421 | void TrimDuplicateCookiesForKey(const std::string& key, |
| 422 | CookieMap::iterator begin, |
| 423 | CookieMap::iterator end); |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 424 | |
| 425 | void SetDefaultCookieableSchemes(); |
| 426 | |
Aaron Tagliaboschi | bd34cc0c | 2019-01-31 01:19:52 | [diff] [blame] | 427 | void FindCookiesForRegistryControlledHost( |
| 428 | const GURL& url, |
| 429 | std::vector<CanonicalCookie*>* cookies); |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 430 | |
Lily Chen | f068a76 | 2019-08-21 21:10:50 | [diff] [blame] | 431 | void FilterCookiesWithOptions(const GURL url, |
| 432 | const CookieOptions options, |
| 433 | std::vector<CanonicalCookie*>* cookie_ptrs, |
| 434 | CookieStatusList* included_cookies, |
| 435 | CookieStatusList* excluded_cookies); |
| 436 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 437 | // Delete any cookies that are equivalent to |ecc| (same path, domain, etc). |
rdsmith | 2709eee | 2017-06-20 22:43:27 | [diff] [blame] | 438 | // |source_secure| indicates if the source may override existing secure |
| 439 | // cookies. |
Mike West | c4a777b | 2017-10-06 14:04:20 | [diff] [blame] | 440 | // |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 441 | // If |skip_httponly| is true, httponly cookies will not be deleted. The |
jww | 601411a | 2015-11-20 19:46:57 | [diff] [blame] | 442 | // return value will be true if |skip_httponly| skipped an httponly cookie or |
jww | a26e439d | 2017-01-27 18:17:27 | [diff] [blame] | 443 | // the cookie to delete was Secure and the scheme of |ecc| is insecure. |key| |
| 444 | // is the key to find the cookie in cookies_; see the comment before the |
| 445 | // CookieMap typedef for details. |
Mike West | c4a777b | 2017-10-06 14:04:20 | [diff] [blame] | 446 | // |
| 447 | // If a cookie is deleted, and its value matches |ecc|'s value, then |
| 448 | // |creation_date_to_inherit| will be set to that cookie's creation date. |
| 449 | // |
Lily Chen | f53dfbcd | 2019-08-30 01:42:10 | [diff] [blame] | 450 | // The cookie will not be deleted if |*status| is not "include" when calling |
| 451 | // the function. The function will update |*status| with exclusion reasons if |
| 452 | // a secure cookie was skipped or an httponly cookie was skipped. |
| 453 | // |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 454 | // NOTE: There should never be more than a single matching equivalent cookie. |
Lily Chen | f53dfbcd | 2019-08-30 01:42:10 | [diff] [blame] | 455 | void MaybeDeleteEquivalentCookieAndUpdateStatus( |
Aaron Tagliaboschi | 29764f5 | 2019-02-21 17:19:59 | [diff] [blame] | 456 | const std::string& key, |
| 457 | const CanonicalCookie& ecc, |
| 458 | bool source_secure, |
| 459 | bool skip_httponly, |
| 460 | bool already_expired, |
Lily Chen | f53dfbcd | 2019-08-30 01:42:10 | [diff] [blame] | 461 | base::Time* creation_date_to_inherit, |
| 462 | CanonicalCookie::CookieInclusionStatus* status); |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 463 | |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 464 | // Inserts |cc| into cookies_. Returns an iterator that points to the inserted |
[email protected] | 6210ce5 | 2013-09-20 03:33:14 | [diff] [blame] | 465 | // cookie in cookies_. Guarantee: all iterators to cookies_ remain valid. |
| 466 | CookieMap::iterator InternalInsertCookie(const std::string& key, |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 467 | std::unique_ptr<CanonicalCookie> cc, |
[email protected] | 6210ce5 | 2013-09-20 03:33:14 | [diff] [blame] | 468 | bool sync_to_store); |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 469 | |
rdsmith | 2709eee | 2017-06-20 22:43:27 | [diff] [blame] | 470 | // Sets all cookies from |list| after deleting any equivalent cookie. |
| 471 | // For data gathering purposes, this routine is treated as if it is |
| 472 | // restoring saved cookies; some statistics are not gathered in this case. |
rdsmith | e5c701d | 2017-07-12 21:50:00 | [diff] [blame] | 473 | void SetAllCookies(CookieList list, SetCookiesCallback callback); |
droger | d5d1278c | 2015-03-17 19:21:51 | [diff] [blame] | 474 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 475 | void InternalUpdateCookieAccessTime(CanonicalCookie* cc, |
| 476 | const base::Time& current_time); |
| 477 | |
| 478 | // |deletion_cause| argument is used for collecting statistics and choosing |
Victor Costan | 14f47c1 | 2018-03-01 08:02:24 | [diff] [blame] | 479 | // the correct CookieChangeCause for OnCookieChange notifications. Guarantee: |
| 480 | // All iterators to cookies_, except for the deleted entry, remain valid. |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 481 | void InternalDeleteCookie(CookieMap::iterator it, |
| 482 | bool sync_to_store, |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 483 | DeletionCause deletion_cause); |
| 484 | |
| 485 | // If the number of cookies for CookieMap key |key|, or globally, are |
| 486 | // over the preset maximums above, garbage collect, first for the host and |
| 487 | // then globally. See comments above garbage collection threshold |
| 488 | // constants for details. |
| 489 | // |
| 490 | // Returns the number of cookies deleted (useful for debugging). |
jww | a26e439d | 2017-01-27 18:17:27 | [diff] [blame] | 491 | size_t GarbageCollect(const base::Time& current, const std::string& key); |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 492 | |
mkwst | e079ac41 | 2016-03-11 09:04:06 | [diff] [blame] | 493 | // Helper for GarbageCollect(). Deletes up to |purge_goal| cookies with a |
| 494 | // priority less than or equal to |priority| from |cookies|, while ensuring |
| 495 | // that at least the |to_protect| most-recent cookies are retained. |
jww | c00ac71 | 2016-05-05 22:21:44 | [diff] [blame] | 496 | // |protected_secure_cookies| specifies whether or not secure cookies should |
| 497 | // be protected from deletion. |
mkwst | e079ac41 | 2016-03-11 09:04:06 | [diff] [blame] | 498 | // |
| 499 | // |cookies| must be sorted from least-recent to most-recent. |
| 500 | // |
mkwst | e079ac41 | 2016-03-11 09:04:06 | [diff] [blame] | 501 | // Returns the number of cookies deleted. |
| 502 | size_t PurgeLeastRecentMatches(CookieItVector* cookies, |
| 503 | CookiePriority priority, |
| 504 | size_t to_protect, |
jww | c00ac71 | 2016-05-05 22:21:44 | [diff] [blame] | 505 | size_t purge_goal, |
| 506 | bool protect_secure_cookies); |
mkwst | e079ac41 | 2016-03-11 09:04:06 | [diff] [blame] | 507 | |
jww | 82d99c1 | 2015-11-25 18:39:53 | [diff] [blame] | 508 | // Helper for GarbageCollect(); can be called directly as well. Deletes all |
| 509 | // expired cookies in |itpair|. If |cookie_its| is non-NULL, all the |
| 510 | // non-expired cookies from |itpair| are appended to |cookie_its|. |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 511 | // |
| 512 | // Returns the number of cookies deleted. |
jww | 82d99c1 | 2015-11-25 18:39:53 | [diff] [blame] | 513 | size_t GarbageCollectExpired(const base::Time& current, |
| 514 | const CookieMapItPair& itpair, |
| 515 | CookieItVector* cookie_its); |
| 516 | |
[email protected] | 8ad5d46 | 2013-05-02 08:45:26 | [diff] [blame] | 517 | // Helper for GarbageCollect(). Deletes all cookies in the range specified by |
| 518 | // [|it_begin|, |it_end|). Returns the number of cookies deleted. |
jww | 82d99c1 | 2015-11-25 18:39:53 | [diff] [blame] | 519 | size_t GarbageCollectDeleteRange(const base::Time& current, |
| 520 | DeletionCause cause, |
| 521 | CookieItVector::iterator cookie_its_begin, |
| 522 | CookieItVector::iterator cookie_its_end); |
| 523 | |
| 524 | // Helper for GarbageCollect(). Deletes cookies in |cookie_its| from least to |
| 525 | // most recently used, but only before |safe_date|. Also will stop deleting |
| 526 | // when the number of remaining cookies hits |purge_goal|. |
mmenke | f4721d99 | 2017-06-07 17:13:59 | [diff] [blame] | 527 | // |
| 528 | // Sets |earliest_time| to be the earliest last access time of a cookie that |
| 529 | // was not deleted, or base::Time() if no such cookie exists. |
jww | 82d99c1 | 2015-11-25 18:39:53 | [diff] [blame] | 530 | size_t GarbageCollectLeastRecentlyAccessed(const base::Time& current, |
| 531 | const base::Time& safe_date, |
| 532 | size_t purge_goal, |
mmenke | f4721d99 | 2017-06-07 17:13:59 | [diff] [blame] | 533 | CookieItVector cookie_its, |
| 534 | base::Time* earliest_time); |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 535 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 536 | bool HasCookieableScheme(const GURL& url); |
| 537 | |
Lily Chen | 70f997f | 2019-10-07 22:01:37 | [diff] [blame] | 538 | // Get the cookie's access semantics (LEGACY or NONLEGACY) from the cookie |
| 539 | // access delegate, if it is non-null. Otherwise return UNKNOWN. |
| 540 | CookieAccessSemantics GetAccessSemanticsForCookie( |
| 541 | const CanonicalCookie& cookie) const; |
| 542 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 543 | // Statistics support |
| 544 | |
| 545 | // This function should be called repeatedly, and will record |
| 546 | // statistics if a sufficient time period has passed. |
| 547 | void RecordPeriodicStats(const base::Time& current_time); |
| 548 | |
| 549 | // Initialize the above variables; should only be called from |
| 550 | // the constructor. |
| 551 | void InitializeHistograms(); |
| 552 | |
Maks Orlovich | 323efaf | 2018-03-06 02:56:39 | [diff] [blame] | 553 | // Defers the callback until the full coookie database has been loaded. If |
| 554 | // it's already been loaded, runs the callback synchronously. |
rdsmith | e5c701d | 2017-07-12 21:50:00 | [diff] [blame] | 555 | void DoCookieCallback(base::OnceClosure callback); |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 556 | |
Maks Orlovich | 323efaf | 2018-03-06 02:56:39 | [diff] [blame] | 557 | // Defers the callback until the cookies relevant to given URL have been |
| 558 | // loaded. If they've already been loaded, runs the callback synchronously. |
rdsmith | e5c701d | 2017-07-12 21:50:00 | [diff] [blame] | 559 | void DoCookieCallbackForURL(base::OnceClosure callback, const GURL& url); |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 560 | |
Maks Orlovich | 323efaf | 2018-03-06 02:56:39 | [diff] [blame] | 561 | // Defers the callback until the cookies relevant to given host or domain |
| 562 | // have been loaded. If they've already been loaded, runs the callback |
| 563 | // synchronously. |
| 564 | void DoCookieCallbackForHostOrDomain(base::OnceClosure callback, |
| 565 | base::StringPiece host_or_domain); |
| 566 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 567 | // Histogram variables; see CookieMonster::InitializeHistograms() in |
| 568 | // cookie_monster.cc for details. |
[email protected] | de41555 | 2013-01-23 04:12:17 | [diff] [blame] | 569 | base::HistogramBase* histogram_expiration_duration_minutes_; |
[email protected] | de41555 | 2013-01-23 04:12:17 | [diff] [blame] | 570 | base::HistogramBase* histogram_count_; |
mkwst | c1aa4cc | 2015-04-03 19:57:45 | [diff] [blame] | 571 | base::HistogramBase* histogram_cookie_type_; |
estark | 7feb65c2b | 2015-08-21 23:38:20 | [diff] [blame] | 572 | base::HistogramBase* histogram_cookie_source_scheme_; |
jww | 31e3263 | 2015-12-16 23:38:34 | [diff] [blame] | 573 | base::HistogramBase* histogram_cookie_delete_equivalent_; |
[email protected] | de41555 | 2013-01-23 04:12:17 | [diff] [blame] | 574 | base::HistogramBase* histogram_time_blocked_on_load_; |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 575 | |
| 576 | CookieMap cookies_; |
| 577 | |
Victor Costan | 14f47c1 | 2018-03-01 08:02:24 | [diff] [blame] | 578 | CookieMonsterChangeDispatcher change_dispatcher_; |
| 579 | |
erikchen | 1dd72a7 | 2015-05-06 20:45:05 | [diff] [blame] | 580 | // Indicates whether the cookie store has been initialized. |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 581 | bool initialized_; |
| 582 | |
erikchen | 1dd72a7 | 2015-05-06 20:45:05 | [diff] [blame] | 583 | // Indicates whether the cookie store has started fetching all cookies. |
| 584 | bool started_fetching_all_cookies_; |
| 585 | // Indicates whether the cookie store has finished fetching all cookies. |
| 586 | bool finished_fetching_all_cookies_; |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 587 | |
| 588 | // List of domain keys that have been loaded from the DB. |
| 589 | std::set<std::string> keys_loaded_; |
| 590 | |
| 591 | // Map of domain keys to their associated task queues. These tasks are blocked |
| 592 | // until all cookies for the associated domain key eTLD+1 are loaded from the |
| 593 | // backend store. |
Brett Wilson | c6a0c82 | 2017-09-12 00:04:29 | [diff] [blame] | 594 | std::map<std::string, base::circular_deque<base::OnceClosure>> |
| 595 | tasks_pending_for_key_; |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 596 | |
| 597 | // Queues tasks that are blocked until all cookies are loaded from the backend |
| 598 | // store. |
Brett Wilson | c6a0c82 | 2017-09-12 00:04:29 | [diff] [blame] | 599 | base::circular_deque<base::OnceClosure> tasks_pending_; |
mmenke | f49fca0e | 2016-03-08 12:46:24 | [diff] [blame] | 600 | |
| 601 | // Once a global cookie task has been seen, all per-key tasks must be put in |
| 602 | // |tasks_pending_| instead of |tasks_pending_for_key_| to ensure a reasonable |
rdsmith | e5c701d | 2017-07-12 21:50:00 | [diff] [blame] | 603 | // view of the cookie store. This is more to ensure fancy cookie export/import |
mmenke | f49fca0e | 2016-03-08 12:46:24 | [diff] [blame] | 604 | // code has a consistent view of the CookieStore, rather than out of concern |
| 605 | // for typical use. |
| 606 | bool seen_global_task_; |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 607 | |
Helen Li | cd0fab86 | 2018-08-13 16:07:53 | [diff] [blame] | 608 | NetLogWithSource net_log_; |
| 609 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 610 | scoped_refptr<PersistentCookieStore> store_; |
| 611 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 612 | // Minimum delay after updating a cookie's LastAccessDate before we will |
| 613 | // update it again. |
| 614 | const base::TimeDelta last_access_threshold_; |
| 615 | |
| 616 | // Approximate date of access time of least recently accessed cookie |
| 617 | // in |cookies_|. Note that this is not guaranteed to be accurate, only a) |
| 618 | // to be before or equal to the actual time, and b) to be accurate |
mmenke | f4721d99 | 2017-06-07 17:13:59 | [diff] [blame] | 619 | // immediately after a garbage collection that scans through all the cookies |
| 620 | // (When garbage collection does not scan through all cookies, it may not be |
| 621 | // updated). This value is used to determine whether global garbage collection |
| 622 | // might find cookies to purge. Note: The default Time() constructor will |
| 623 | // create a value that compares earlier than any other time value, which is |
| 624 | // wanted. Thus this value is not initialized. |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 625 | base::Time earliest_access_time_; |
| 626 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 627 | std::vector<std::string> cookieable_schemes_; |
| 628 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 629 | base::Time last_statistic_record_time_; |
| 630 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 631 | bool persist_session_cookies_; |
| 632 | |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 633 | base::ThreadChecker thread_checker_; |
| 634 | |
Jeremy Roman | d54000b2 | 2019-07-08 18:40:16 | [diff] [blame] | 635 | base::WeakPtrFactory<CookieMonster> weak_ptr_factory_{this}; |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 636 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 637 | DISALLOW_COPY_AND_ASSIGN(CookieMonster); |
| 638 | }; |
| 639 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 640 | typedef base::RefCountedThreadSafe<CookieMonster::PersistentCookieStore> |
| 641 | RefcountedPersistentCookieStore; |
| 642 | |
[email protected] | c1b6e10 | 2013-04-10 20:54:49 | [diff] [blame] | 643 | class NET_EXPORT CookieMonster::PersistentCookieStore |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 644 | : public RefcountedPersistentCookieStore { |
| 645 | public: |
Maks Orlovich | 108cb4c | 2019-03-26 20:24:57 | [diff] [blame] | 646 | typedef base::OnceCallback<void( |
| 647 | std::vector<std::unique_ptr<CanonicalCookie>>)> |
[email protected] | 5b9bc35 | 2012-07-18 13:13:34 | [diff] [blame] | 648 | LoadedCallback; |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 649 | |
| 650 | // Initializes the store and retrieves the existing cookies. This will be |
| 651 | // called only once at startup. The callback will return all the cookies |
| 652 | // that are not yet returned to CookieMonster by previous priority loads. |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 653 | // |
| 654 | // |loaded_callback| may not be NULL. |
Helen Li | 92a29f10 | 2018-08-15 23:02:26 | [diff] [blame] | 655 | // |net_log| is a NetLogWithSource that may be copied if the persistent |
| 656 | // store wishes to log NetLog events. |
Maks Orlovich | 108cb4c | 2019-03-26 20:24:57 | [diff] [blame] | 657 | virtual void Load(LoadedCallback loaded_callback, |
Helen Li | 92a29f10 | 2018-08-15 23:02:26 | [diff] [blame] | 658 | const NetLogWithSource& net_log) = 0; |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 659 | |
| 660 | // Does a priority load of all cookies for the domain key (eTLD+1). The |
| 661 | // callback will return all the cookies that are not yet returned by previous |
| 662 | // loads, which includes cookies for the requested domain key if they are not |
| 663 | // already returned, plus all cookies that are chain-loaded and not yet |
| 664 | // returned to CookieMonster. |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 665 | // |
| 666 | // |loaded_callback| may not be NULL. |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 667 | virtual void LoadCookiesForKey(const std::string& key, |
Maks Orlovich | 108cb4c | 2019-03-26 20:24:57 | [diff] [blame] | 668 | LoadedCallback loaded_callback) = 0; |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 669 | |
| 670 | virtual void AddCookie(const CanonicalCookie& cc) = 0; |
| 671 | virtual void UpdateCookieAccessTime(const CanonicalCookie& cc) = 0; |
| 672 | virtual void DeleteCookie(const CanonicalCookie& cc) = 0; |
| 673 | |
[email protected] | bf510ed | 2012-06-05 08:31:43 | [diff] [blame] | 674 | // Instructs the store to not discard session only cookies on shutdown. |
| 675 | virtual void SetForceKeepSessionState() = 0; |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 676 | |
Nick Harper | 14e2333 | 2017-07-28 00:27:23 | [diff] [blame] | 677 | // Sets a callback that will be run before the store flushes. If |callback| |
| 678 | // performs any async operations, the store will not wait for those to finish |
| 679 | // before flushing. |
Lily Chen | 9934f7e | 2019-03-13 19:16:55 | [diff] [blame] | 680 | virtual void SetBeforeCommitCallback(base::RepeatingClosure callback) = 0; |
Nick Harper | 14e2333 | 2017-07-28 00:27:23 | [diff] [blame] | 681 | |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 682 | // Flushes the store and posts |callback| when complete. |callback| may be |
| 683 | // NULL. |
rdsmith | 7ac8171 | 2017-06-22 17:09:54 | [diff] [blame] | 684 | virtual void Flush(base::OnceClosure callback) = 0; |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 685 | |
| 686 | protected: |
| 687 | PersistentCookieStore() {} |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 688 | virtual ~PersistentCookieStore() {} |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 689 | |
| 690 | private: |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 691 | friend class base::RefCountedThreadSafe<PersistentCookieStore>; |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 692 | DISALLOW_COPY_AND_ASSIGN(PersistentCookieStore); |
| 693 | }; |
| 694 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 695 | } // namespace net |
| 696 | |
| 697 | #endif // NET_COOKIES_COOKIE_MONSTER_H_ |