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