[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 <deque> |
| 14 | #include <map> |
danakj | a9850e1 | 2016-04-18 22:28:08 | [diff] [blame] | 15 | #include <memory> |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 16 | #include <queue> |
| 17 | #include <set> |
| 18 | #include <string> |
| 19 | #include <utility> |
| 20 | #include <vector> |
| 21 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 22 | #include "base/callback_forward.h" |
| 23 | #include "base/gtest_prod_util.h" |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 24 | #include "base/macros.h" |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 25 | #include "base/memory/ref_counted.h" |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 26 | #include "base/memory/weak_ptr.h" |
| 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" |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 32 | #include "net/cookies/cookie_store.h" |
ellyjones | 399e35a2 | 2014-10-27 11:09:56 | [diff] [blame] | 33 | #include "url/gurl.h" |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 34 | |
| 35 | namespace base { |
[email protected] | de41555 | 2013-01-23 04:12:17 | [diff] [blame] | 36 | class HistogramBase; |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 37 | } // namespace base |
| 38 | |
| 39 | namespace net { |
| 40 | |
[email protected] | 7c4b66b | 2014-01-04 12:28:13 | [diff] [blame] | 41 | class CookieMonsterDelegate; |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 42 | |
| 43 | // The cookie monster is the system for storing and retrieving cookies. It has |
| 44 | // an in-memory list of all cookies, and synchronizes non-session cookies to an |
| 45 | // optional permanent storage that implements the PersistentCookieStore |
| 46 | // interface. |
| 47 | // |
mmenke | 96f3bab | 2016-01-22 17:34:02 | [diff] [blame] | 48 | // Tasks may be deferred if all affected cookies are not yet loaded from the |
| 49 | // backing store. Otherwise, callbacks may be invoked immediately. |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 50 | // |
| 51 | // A cookie task is either pending loading of the entire cookie store, or |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 52 | // loading of cookies for a specific domain key(eTLD+1). In the former case, the |
[email protected] | 0184df3 | 2013-05-14 00:53:55 | [diff] [blame] | 53 | // cookie task will be queued in tasks_pending_ while PersistentCookieStore |
| 54 | // chain loads the cookie store on DB thread. In the latter case, the cookie |
| 55 | // task will be queued in tasks_pending_for_key_ while PermanentCookieStore |
| 56 | // loads cookies for the specified domain key(eTLD+1) on DB thread. |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 57 | // |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 58 | // TODO(deanm) Implement CookieMonster, the cookie database. |
| 59 | // - Verify that our domain enforcement and non-dotted handling is correct |
| 60 | class NET_EXPORT CookieMonster : public CookieStore { |
| 61 | public: |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 62 | class PersistentCookieStore; |
| 63 | |
| 64 | // Terminology: |
| 65 | // * The 'top level domain' (TLD) of an internet domain name is |
| 66 | // the terminal "." free substring (e.g. "com" for google.com |
| 67 | // or world.std.com). |
| 68 | // * The 'effective top level domain' (eTLD) is the longest |
| 69 | // "." initiated terminal substring of an internet domain name |
| 70 | // that is controlled by a general domain registrar. |
| 71 | // (e.g. "co.uk" for news.bbc.co.uk). |
| 72 | // * The 'effective top level domain plus one' (eTLD+1) is the |
| 73 | // shortest "." delimited terminal substring of an internet |
| 74 | // domain name that is not controlled by a general domain |
| 75 | // registrar (e.g. "bbc.co.uk" for news.bbc.co.uk, or |
| 76 | // "google.com" for news.google.com). The general assumption |
| 77 | // is that all hosts and domains under an eTLD+1 share some |
| 78 | // administrative control. |
| 79 | |
| 80 | // CookieMap is the central data structure of the CookieMonster. It |
| 81 | // is a map whose values are pointers to CanonicalCookie data |
| 82 | // structures (the data structures are owned by the CookieMonster |
| 83 | // and must be destroyed when removed from the map). The key is based on the |
| 84 | // effective domain of the cookies. If the domain of the cookie has an |
| 85 | // eTLD+1, that is the key for the map. If the domain of the cookie does not |
| 86 | // have an eTLD+1, the key of the map is the host the cookie applies to (it is |
| 87 | // not legal to have domain cookies without an eTLD+1). This rule |
| 88 | // excludes cookies for, e.g, ".com", ".co.uk", or ".internalnetwork". |
| 89 | // This behavior is the same as the behavior in Firefox v 3.6.10. |
| 90 | |
| 91 | // NOTE(deanm): |
| 92 | // I benchmarked hash_multimap vs multimap. We're going to be query-heavy |
| 93 | // so it would seem like hashing would help. However they were very |
| 94 | // close, with multimap being a tiny bit faster. I think this is because |
| 95 | // our map is at max around 1000 entries, and the additional complexity |
| 96 | // for the hashing might not overcome the O(log(1000)) for querying |
| 97 | // a multimap. Also, multimap is standard, another reason to use it. |
| 98 | // TODO(rdsmith): This benchmark should be re-done now that we're allowing |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 99 | // substantially more entries in the map. |
| 100 | using CookieMap = |
| 101 | std::multimap<std::string, std::unique_ptr<CanonicalCookie>>; |
| 102 | using CookieMapItPair = std::pair<CookieMap::iterator, CookieMap::iterator>; |
| 103 | using CookieItVector = std::vector<CookieMap::iterator>; |
[email protected] | 8ad5d46 | 2013-05-02 08:45:26 | [diff] [blame] | 104 | |
| 105 | // Cookie garbage collection thresholds. Based off of the Mozilla defaults. |
| 106 | // When the number of cookies gets to k{Domain,}MaxCookies |
| 107 | // purge down to k{Domain,}MaxCookies - k{Domain,}PurgeCookies. |
| 108 | // It might seem scary to have a high purge value, but really it's not. |
| 109 | // You just make sure that you increase the max to cover the increase |
| 110 | // in purge, and we would have been purging the same number of cookies. |
| 111 | // We're just going through the garbage collection process less often. |
| 112 | // Note that the DOMAIN values are per eTLD+1; see comment for the |
| 113 | // CookieMap typedef. So, e.g., the maximum number of cookies allowed for |
| 114 | // google.com and all of its subdomains will be 150-180. |
| 115 | // |
| 116 | // Any cookies accessed more recently than kSafeFromGlobalPurgeDays will not |
| 117 | // be evicted by global garbage collection, even if we have more than |
| 118 | // kMaxCookies. This does not affect domain garbage collection. |
| 119 | static const size_t kDomainMaxCookies; |
| 120 | static const size_t kDomainPurgeCookies; |
| 121 | static const size_t kMaxCookies; |
| 122 | static const size_t kPurgeCookies; |
| 123 | |
| 124 | // Quota for cookies with {low, medium, high} priorities within a domain. |
mkwst | 8773435 | 2016-03-03 17:36:23 | [diff] [blame] | 125 | static const size_t kDomainCookiesQuotaLow; |
| 126 | static const size_t kDomainCookiesQuotaMedium; |
| 127 | static const size_t kDomainCookiesQuotaHigh; |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 128 | |
| 129 | // The store passed in should not have had Init() called on it yet. This |
| 130 | // class will take care of initializing it. The backing store is NOT owned by |
| 131 | // this class, but it must remain valid for the duration of the cookie |
| 132 | // monster's existence. If |store| is NULL, then no backing store will be |
| 133 | // updated. If |delegate| is non-NULL, it will be notified on |
| 134 | // creation/deletion of cookies. |
[email protected] | 7c4b66b | 2014-01-04 12:28:13 | [diff] [blame] | 135 | CookieMonster(PersistentCookieStore* store, CookieMonsterDelegate* delegate); |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 136 | |
| 137 | // Only used during unit testing. |
| 138 | CookieMonster(PersistentCookieStore* store, |
[email protected] | 7c4b66b | 2014-01-04 12:28:13 | [diff] [blame] | 139 | CookieMonsterDelegate* delegate, |
shess | f0bc118 | 2016-05-19 04:35:58 | [diff] [blame] | 140 | base::TimeDelta last_access_threshold); |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 141 | |
mmenke | 606c59c | 2016-03-07 18:20:55 | [diff] [blame] | 142 | ~CookieMonster() override; |
| 143 | |
droger | d5d1278c | 2015-03-17 19:21:51 | [diff] [blame] | 144 | // Replaces all the cookies by |list|. This method does not flush the backend. |
mmenke | 009cf62e | 2016-07-18 19:33:31 | [diff] [blame] | 145 | // This method does not support setting secure cookies, which need source |
| 146 | // URLs. |
| 147 | // TODO(mmenke): This method is only used on iOS. Consider removing it. |
droger | d5d1278c | 2015-03-17 19:21:51 | [diff] [blame] | 148 | void SetAllCookiesAsync(const CookieList& list, |
| 149 | const SetCookiesCallback& callback); |
| 150 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 151 | // CookieStore implementation. |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 152 | void SetCookieWithOptionsAsync(const GURL& url, |
| 153 | const std::string& cookie_line, |
| 154 | const CookieOptions& options, |
| 155 | const SetCookiesCallback& callback) override; |
mmenke | ea4cd40 | 2016-02-02 04:03:10 | [diff] [blame] | 156 | void SetCookieWithDetailsAsync(const GURL& url, |
| 157 | const std::string& name, |
| 158 | const std::string& value, |
| 159 | const std::string& domain, |
| 160 | const std::string& path, |
mmenke | fdd4fc7 | 2016-02-05 20:53:24 | [diff] [blame] | 161 | base::Time creation_time, |
| 162 | base::Time expiration_time, |
| 163 | base::Time last_access_time, |
mmenke | ea4cd40 | 2016-02-02 04:03:10 | [diff] [blame] | 164 | bool secure, |
| 165 | bool http_only, |
mkwst | e1a2958 | 2016-03-15 10:07:52 | [diff] [blame] | 166 | CookieSameSite same_site, |
mmenke | ea4cd40 | 2016-02-02 04:03:10 | [diff] [blame] | 167 | CookiePriority priority, |
| 168 | const SetCookiesCallback& callback) override; |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 169 | void GetCookiesWithOptionsAsync(const GURL& url, |
| 170 | const CookieOptions& options, |
| 171 | const GetCookiesCallback& callback) override; |
mkwst | c611e6d | 2016-02-23 15:45:55 | [diff] [blame] | 172 | void GetCookieListWithOptionsAsync( |
| 173 | const GURL& url, |
| 174 | const CookieOptions& options, |
| 175 | const GetCookieListCallback& callback) override; |
mmenke | 9fa44f2d | 2016-01-22 23:36:39 | [diff] [blame] | 176 | void GetAllCookiesAsync(const GetCookieListCallback& callback) override; |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 177 | void DeleteCookieAsync(const GURL& url, |
| 178 | const std::string& cookie_name, |
| 179 | const base::Closure& callback) override; |
mmenke | 24379d5 | 2016-02-05 23:50:17 | [diff] [blame] | 180 | void DeleteCanonicalCookieAsync(const CanonicalCookie& cookie, |
| 181 | const DeleteCallback& callback) override; |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 182 | void DeleteAllCreatedBetweenAsync(const base::Time& delete_begin, |
| 183 | const base::Time& delete_end, |
| 184 | const DeleteCallback& callback) override; |
dmurph | faea244c | 2016-04-09 00:42:30 | [diff] [blame] | 185 | void DeleteAllCreatedBetweenWithPredicateAsync( |
| 186 | const base::Time& delete_begin, |
| 187 | const base::Time& delete_end, |
| 188 | const base::Callback<bool(const CanonicalCookie&)>& predicate, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 189 | const DeleteCallback& callback) override; |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 190 | void DeleteSessionCookiesAsync(const DeleteCallback&) override; |
mmenke | 96f3bab | 2016-01-22 17:34:02 | [diff] [blame] | 191 | void FlushStore(const base::Closure& callback) override; |
mmenke | ded79da | 2016-02-06 08:28:51 | [diff] [blame] | 192 | void SetForceKeepSessionState() override; |
[email protected] | 264807b | 2012-04-25 14:49:37 | [diff] [blame] | 193 | |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 194 | // Resets the list of cookieable schemes to the supplied schemes. Does |
| 195 | // nothing if called after first use of the instance (i.e. after the |
| 196 | // instance initialization process). |
mmenke | 18dd8ba | 2016-02-01 18:42:10 | [diff] [blame] | 197 | void SetCookieableSchemes(const std::vector<std::string>& schemes); |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 198 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 199 | // Enables writing session cookies into the cookie database. If this this |
| 200 | // method is called, it must be called before first use of the instance |
| 201 | // (i.e. as part of the instance initialization process). |
| 202 | void SetPersistSessionCookies(bool persist_session_cookies); |
| 203 | |
[email protected] | 97a3b6e | 2012-06-12 01:53:56 | [diff] [blame] | 204 | // Determines if the scheme of the URL is a scheme that cookies will be |
| 205 | // stored for. |
| 206 | bool IsCookieableScheme(const std::string& scheme); |
| 207 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 208 | // The default list of schemes the cookie monster can handle. |
[email protected] | 5edff3c5 | 2014-06-23 20:27:48 | [diff] [blame] | 209 | static const char* const kDefaultCookieableSchemes[]; |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 210 | static const int kDefaultCookieableSchemesCount; |
| 211 | |
danakj | a9850e1 | 2016-04-18 22:28:08 | [diff] [blame] | 212 | std::unique_ptr<CookieChangedSubscription> AddCallbackForCookie( |
ellyjones | 399e35a2 | 2014-10-27 11:09:56 | [diff] [blame] | 213 | const GURL& url, |
| 214 | const std::string& name, |
| 215 | const CookieChangedCallback& callback) override; |
| 216 | |
nharper | 5babb5e6 | 2016-03-09 18:58:07 | [diff] [blame] | 217 | bool IsEphemeral() override; |
| 218 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 219 | private: |
| 220 | // For queueing the cookie monster calls. |
| 221 | class CookieMonsterTask; |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 222 | template <typename Result> |
| 223 | class DeleteTask; |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 224 | class DeleteAllCreatedBetweenTask; |
dmurph | faea244c | 2016-04-09 00:42:30 | [diff] [blame] | 225 | class DeleteAllCreatedBetweenWithPredicateTask; |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 226 | class DeleteCookieTask; |
| 227 | class DeleteCanonicalCookieTask; |
mkwst | 72b6516 | 2016-02-22 19:58:54 | [diff] [blame] | 228 | class GetCookieListForURLWithOptionsTask; |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 229 | class GetAllCookiesTask; |
| 230 | class GetCookiesWithOptionsTask; |
mkwst | c611e6d | 2016-02-23 15:45:55 | [diff] [blame] | 231 | class GetCookieListWithOptionsTask; |
droger | d5d1278c | 2015-03-17 19:21:51 | [diff] [blame] | 232 | class SetAllCookiesTask; |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 233 | class SetCookieWithDetailsTask; |
| 234 | class SetCookieWithOptionsTask; |
[email protected] | 264807b | 2012-04-25 14:49:37 | [diff] [blame] | 235 | class DeleteSessionCookiesTask; |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 236 | |
| 237 | // Testing support. |
| 238 | // For SetCookieWithCreationTime. |
| 239 | FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, |
| 240 | TestCookieDeleteAllCreatedBetweenTimestamps); |
dmurph | faea244c | 2016-04-09 00:42:30 | [diff] [blame] | 241 | FRIEND_TEST_ALL_PREFIXES( |
| 242 | CookieMonsterTest, |
| 243 | TestCookieDeleteAllCreatedBetweenTimestampsWithPredicate); |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 244 | |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 245 | // For garbage collection constants. |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 246 | FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestHostGarbageCollection); |
| 247 | FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestTotalGarbageCollection); |
| 248 | FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, GarbageCollectionTriggers); |
| 249 | FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestGCTimes); |
| 250 | |
| 251 | // For validation of key values. |
| 252 | FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestDomainTree); |
| 253 | FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestImport); |
| 254 | FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, GetKey); |
| 255 | FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestGetKey); |
| 256 | |
| 257 | // For FindCookiesForKey. |
| 258 | FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, ShortLivedSessionCookies); |
| 259 | |
droger | d5d1278c | 2015-03-17 19:21:51 | [diff] [blame] | 260 | // For ComputeCookieDiff. |
| 261 | FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, ComputeCookieDiff); |
| 262 | |
estark | 7feb65c2b | 2015-08-21 23:38:20 | [diff] [blame] | 263 | // For CookieSource histogram enum. |
| 264 | FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, CookieSourceHistogram); |
| 265 | |
jww | 31e3263 | 2015-12-16 23:38:34 | [diff] [blame] | 266 | // For kSafeFromGlobalPurgeDays in CookieStore. |
jww | a26e439d | 2017-01-27 18:17:27 | [diff] [blame^] | 267 | FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, EvictSecureCookies); |
jww | 82d99c1 | 2015-11-25 18:39:53 | [diff] [blame] | 268 | |
jww | 31e3263 | 2015-12-16 23:38:34 | [diff] [blame] | 269 | // For CookieDeleteEquivalent histogram enum. |
| 270 | FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, |
| 271 | CookieDeleteEquivalentHistogramTest); |
jww | 31e3263 | 2015-12-16 23:38:34 | [diff] [blame] | 272 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 273 | // Internal reasons for deletion, used to populate informative histograms |
| 274 | // and to provide a public cause for onCookieChange notifications. |
| 275 | // |
| 276 | // If you add or remove causes from this list, please be sure to also update |
nharper | 352933e | 2016-09-30 18:24:57 | [diff] [blame] | 277 | // the CookieStore::ChangeCause mapping inside ChangeCauseMapping. |
[email protected] | 7c4b66b | 2014-01-04 12:28:13 | [diff] [blame] | 278 | // Moreover, these are used as array indexes, so avoid reordering to keep the |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 279 | // histogram buckets consistent. New items (if necessary) should be added |
nharper | 6890336 | 2017-01-20 04:07:14 | [diff] [blame] | 280 | // at the end of the list, before DELETE_COOKIE_LAST_ENTRY and the temporary |
| 281 | // values added for debugging. |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 282 | enum DeletionCause { |
nharper | 6890336 | 2017-01-20 04:07:14 | [diff] [blame] | 283 | // DELETE_COOKIE_EXPLICIT is temporarily unused (except for logging to the |
| 284 | // histogram) - see values 13-16 below. |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 285 | DELETE_COOKIE_EXPLICIT = 0, |
mkwst | aa07ee8 | 2016-03-11 15:32:14 | [diff] [blame] | 286 | DELETE_COOKIE_OVERWRITE = 1, |
| 287 | DELETE_COOKIE_EXPIRED = 2, |
| 288 | DELETE_COOKIE_EVICTED = 3, |
| 289 | DELETE_COOKIE_DUPLICATE_IN_BACKING_STORE = 4, |
| 290 | DELETE_COOKIE_DONT_RECORD = 5, // For final cleanup after flush to store. |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 291 | |
mkwst | aa07ee8 | 2016-03-11 15:32:14 | [diff] [blame] | 292 | // Cookies evicted during domain-level garbage collection. |
| 293 | DELETE_COOKIE_EVICTED_DOMAIN = 6, |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 294 | |
mkwst | aa07ee8 | 2016-03-11 15:32:14 | [diff] [blame] | 295 | // Cookies evicted during global garbage collection (which takes place after |
| 296 | // domain-level garbage collection fails to bring the cookie store under |
| 297 | // the overall quota. |
| 298 | DELETE_COOKIE_EVICTED_GLOBAL = 7, |
| 299 | |
| 300 | // #8 was DELETE_COOKIE_EVICTED_DOMAIN_PRE_SAFE |
| 301 | // #9 was DELETE_COOKIE_EVICTED_DOMAIN_POST_SAFE |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 302 | |
| 303 | // A common idiom is to remove a cookie by overwriting it with an |
| 304 | // already-expired expiration date. This captures that case. |
mkwst | aa07ee8 | 2016-03-11 15:32:14 | [diff] [blame] | 305 | DELETE_COOKIE_EXPIRED_OVERWRITE = 10, |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 306 | |
[email protected] | 6210ce5 | 2013-09-20 03:33:14 | [diff] [blame] | 307 | // Cookies are not allowed to contain control characters in the name or |
| 308 | // value. However, we used to allow them, so we are now evicting any such |
| 309 | // cookies as we load them. See http://crbug.com/238041. |
mkwst | aa07ee8 | 2016-03-11 15:32:14 | [diff] [blame] | 310 | DELETE_COOKIE_CONTROL_CHAR = 11, |
[email protected] | 6210ce5 | 2013-09-20 03:33:14 | [diff] [blame] | 311 | |
jww | 82d99c1 | 2015-11-25 18:39:53 | [diff] [blame] | 312 | // When strict secure cookies is enabled, non-secure cookies are evicted |
| 313 | // right after expired cookies. |
mkwst | aa07ee8 | 2016-03-11 15:32:14 | [diff] [blame] | 314 | DELETE_COOKIE_NON_SECURE = 12, |
jww | 82d99c1 | 2015-11-25 18:39:53 | [diff] [blame] | 315 | |
nharper | 6890336 | 2017-01-20 04:07:14 | [diff] [blame] | 316 | // The following values are temporary and being used to track down a bug. |
| 317 | // They should be treated the same as DELETE_COOKIE_EXPLICIT, and are logged |
| 318 | // to the histogram as DELETE_COOKIE_EXPLICIT. |
| 319 | DELETE_COOKIE_CREATED_BETWEEN = 13, |
| 320 | DELETE_COOKIE_CREATED_BETWEEN_WITH_PREDICATE = 14, |
| 321 | DELETE_COOKIE_SINGLE = 15, |
| 322 | DELETE_COOKIE_CANONICAL = 16, |
| 323 | |
| 324 | // Do not add new values between DELETE_COOKIE_CREATED_BETWEEN and |
| 325 | // DELETE_COOKIE_LAST_ENTRY, as the above values are temporary. Instead, new |
| 326 | // values should go before DELETE_COOKIE_CREATED_BETWEEN. |
| 327 | DELETE_COOKIE_LAST_ENTRY = 17 |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 328 | }; |
| 329 | |
mkwst | c1aa4cc | 2015-04-03 19:57:45 | [diff] [blame] | 330 | // This enum is used to generate a histogramed bitmask measureing the types |
| 331 | // of stored cookies. Please do not reorder the list when adding new entries. |
| 332 | // New items MUST be added at the end of the list, just before |
| 333 | // COOKIE_TYPE_LAST_ENTRY; |
| 334 | enum CookieType { |
mkwst | 4654941 | 2016-02-01 10:05:37 | [diff] [blame] | 335 | COOKIE_TYPE_SAME_SITE = 0, |
mkwst | c1aa4cc | 2015-04-03 19:57:45 | [diff] [blame] | 336 | COOKIE_TYPE_HTTPONLY, |
| 337 | COOKIE_TYPE_SECURE, |
| 338 | COOKIE_TYPE_LAST_ENTRY |
| 339 | }; |
| 340 | |
estark | 7feb65c2b | 2015-08-21 23:38:20 | [diff] [blame] | 341 | // Used to populate a histogram containing information about the |
| 342 | // sources of Secure and non-Secure cookies: that is, whether such |
| 343 | // cookies are set by origins with cryptographic or non-cryptographic |
| 344 | // schemes. Please do not reorder the list when adding new |
| 345 | // entries. New items MUST be added at the end of the list, just |
| 346 | // before COOKIE_SOURCE_LAST_ENTRY. |
| 347 | // |
| 348 | // COOKIE_SOURCE_(NON)SECURE_COOKIE_(NON)CRYPTOGRAPHIC_SCHEME means |
| 349 | // that a cookie was set or overwritten from a URL with the given type |
| 350 | // of scheme. This enum should not be used when cookies are *cleared*, |
| 351 | // because its purpose is to understand if Chrome can deprecate the |
| 352 | // ability of HTTP urls to set/overwrite Secure cookies. |
| 353 | enum CookieSource { |
| 354 | COOKIE_SOURCE_SECURE_COOKIE_CRYPTOGRAPHIC_SCHEME = 0, |
| 355 | COOKIE_SOURCE_SECURE_COOKIE_NONCRYPTOGRAPHIC_SCHEME, |
| 356 | COOKIE_SOURCE_NONSECURE_COOKIE_CRYPTOGRAPHIC_SCHEME, |
| 357 | COOKIE_SOURCE_NONSECURE_COOKIE_NONCRYPTOGRAPHIC_SCHEME, |
| 358 | COOKIE_SOURCE_LAST_ENTRY |
| 359 | }; |
| 360 | |
jww | 31e3263 | 2015-12-16 23:38:34 | [diff] [blame] | 361 | // Used to populate a histogram for cookie setting in the "delete equivalent" |
| 362 | // step. Measures total attempts to delete an equivalent cookie as well as if |
| 363 | // a cookie is found to delete, if a cookie is skipped because it is secure, |
| 364 | // and if it is skipped for being secure but would have been deleted |
| 365 | // otherwise. The last two are only possible if strict secure cookies is |
| 366 | // turned on and if an insecure origin attempts to a set a cookie where a |
| 367 | // cookie with the same name and secure attribute already exists. |
| 368 | // |
| 369 | // Enum for UMA. Do no reorder or remove entries. New entries must be place |
| 370 | // directly before COOKIE_DELETE_EQUIVALENT_LAST_ENTRY and histograms.xml must |
| 371 | // be updated accordingly. |
| 372 | enum CookieDeleteEquivalent { |
| 373 | COOKIE_DELETE_EQUIVALENT_ATTEMPT = 0, |
| 374 | COOKIE_DELETE_EQUIVALENT_FOUND, |
| 375 | COOKIE_DELETE_EQUIVALENT_SKIPPING_SECURE, |
| 376 | COOKIE_DELETE_EQUIVALENT_WOULD_HAVE_DELETED, |
| 377 | COOKIE_DELETE_EQUIVALENT_LAST_ENTRY |
| 378 | }; |
| 379 | |
erikchen | 1dd72a7 | 2015-05-06 20:45:05 | [diff] [blame] | 380 | // The strategy for fetching cookies. Controlled by Finch experiment. |
| 381 | enum FetchStrategy { |
| 382 | // Fetches all cookies only when they're needed. |
| 383 | kFetchWhenNecessary = 0, |
| 384 | // Fetches all cookies as soon as any cookie is needed. |
| 385 | // This is the default behavior. |
| 386 | kAlwaysFetch, |
| 387 | // The fetch strategy is not yet determined. |
| 388 | kUnknownFetch, |
| 389 | }; |
| 390 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 391 | // The number of days since last access that cookies will not be subject |
| 392 | // to global garbage collection. |
| 393 | static const int kSafeFromGlobalPurgeDays; |
| 394 | |
| 395 | // Record statistics every kRecordStatisticsIntervalSeconds of uptime. |
| 396 | static const int kRecordStatisticsIntervalSeconds = 10 * 60; |
| 397 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 398 | // The following are synchronous calls to which the asynchronous methods |
| 399 | // delegate either immediately (if the store is loaded) or through a deferred |
| 400 | // task (if the store is not yet loaded). |
| 401 | bool SetCookieWithDetails(const GURL& url, |
| 402 | const std::string& name, |
| 403 | const std::string& value, |
| 404 | const std::string& domain, |
| 405 | const std::string& path, |
mmenke | fdd4fc7 | 2016-02-05 20:53:24 | [diff] [blame] | 406 | base::Time creation_time, |
| 407 | base::Time expiration_time, |
| 408 | base::Time last_access_time, |
[email protected] | ab2d75c8 | 2013-04-19 18:39:04 | [diff] [blame] | 409 | bool secure, |
| 410 | bool http_only, |
mkwst | e1a2958 | 2016-03-15 10:07:52 | [diff] [blame] | 411 | CookieSameSite same_site, |
[email protected] | ab2d75c8 | 2013-04-19 18:39:04 | [diff] [blame] | 412 | CookiePriority priority); |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 413 | |
| 414 | CookieList GetAllCookies(); |
| 415 | |
mkwst | c611e6d | 2016-02-23 15:45:55 | [diff] [blame] | 416 | CookieList GetCookieListWithOptions(const GURL& url, |
| 417 | const CookieOptions& options); |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 418 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 419 | int DeleteAllCreatedBetween(const base::Time& delete_begin, |
| 420 | const base::Time& delete_end); |
| 421 | |
dmurph | faea244c | 2016-04-09 00:42:30 | [diff] [blame] | 422 | // Predicate will be called with the calling thread. |
| 423 | int DeleteAllCreatedBetweenWithPredicate( |
| 424 | const base::Time& delete_begin, |
| 425 | const base::Time& delete_end, |
| 426 | const base::Callback<bool(const CanonicalCookie&)>& predicate); |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 427 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 428 | bool SetCookieWithOptions(const GURL& url, |
| 429 | const std::string& cookie_line, |
| 430 | const CookieOptions& options); |
| 431 | |
| 432 | std::string GetCookiesWithOptions(const GURL& url, |
| 433 | const CookieOptions& options); |
| 434 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 435 | void DeleteCookie(const GURL& url, const std::string& cookie_name); |
| 436 | |
mmenke | 24379d5 | 2016-02-05 23:50:17 | [diff] [blame] | 437 | int DeleteCanonicalCookie(const CanonicalCookie& cookie); |
| 438 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 439 | bool SetCookieWithCreationTime(const GURL& url, |
| 440 | const std::string& cookie_line, |
| 441 | const base::Time& creation_time); |
| 442 | |
[email protected] | 264807b | 2012-04-25 14:49:37 | [diff] [blame] | 443 | int DeleteSessionCookies(); |
| 444 | |
erikchen | 1dd72a7 | 2015-05-06 20:45:05 | [diff] [blame] | 445 | // The first access to the cookie store initializes it. This method should be |
| 446 | // called before any access to the cookie store. |
| 447 | void MarkCookieStoreAsInitialized(); |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 448 | |
erikchen | 1dd72a7 | 2015-05-06 20:45:05 | [diff] [blame] | 449 | // Fetches all cookies if the backing store exists and they're not already |
| 450 | // being fetched. |
erikchen | 1dd72a7 | 2015-05-06 20:45:05 | [diff] [blame] | 451 | void FetchAllCookiesIfNecessary(); |
| 452 | |
| 453 | // Fetches all cookies from the backing store. |
erikchen | 1dd72a7 | 2015-05-06 20:45:05 | [diff] [blame] | 454 | void FetchAllCookies(); |
| 455 | |
| 456 | // Whether all cookies should be fetched as soon as any is requested. |
| 457 | bool ShouldFetchAllCookiesWhenFetchingAnyCookie(); |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 458 | |
| 459 | // Stores cookies loaded from the backing store and invokes any deferred |
| 460 | // calls. |beginning_time| should be the moment PersistentCookieStore::Load |
| 461 | // was invoked and is used for reporting histogram_time_blocked_on_load_. |
| 462 | // See PersistentCookieStore::Load for details on the contents of cookies. |
| 463 | void OnLoaded(base::TimeTicks beginning_time, |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 464 | std::vector<std::unique_ptr<CanonicalCookie>> cookies); |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 465 | |
| 466 | // Stores cookies loaded from the backing store and invokes the deferred |
| 467 | // task(s) pending loading of cookies associated with the domain key |
| 468 | // (eTLD+1). Called when all cookies for the domain key(eTLD+1) have been |
| 469 | // loaded from DB. See PersistentCookieStore::Load for details on the contents |
| 470 | // of cookies. |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 471 | void OnKeyLoaded(const std::string& key, |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 472 | std::vector<std::unique_ptr<CanonicalCookie>> cookies); |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 473 | |
| 474 | // Stores the loaded cookies. |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 475 | void StoreLoadedCookies( |
| 476 | std::vector<std::unique_ptr<CanonicalCookie>> cookies); |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 477 | |
| 478 | // Invokes deferred calls. |
| 479 | void InvokeQueue(); |
| 480 | |
| 481 | // Checks that |cookies_| matches our invariants, and tries to repair any |
| 482 | // inconsistencies. (In other words, it does not have duplicate cookies). |
| 483 | void EnsureCookiesMapIsValid(); |
| 484 | |
| 485 | // Checks for any duplicate cookies for CookieMap key |key| which lie between |
| 486 | // |begin| and |end|. If any are found, all but the most recent are deleted. |
ellyjones | cabf5742 | 2015-08-21 18:44:51 | [diff] [blame] | 487 | void TrimDuplicateCookiesForKey(const std::string& key, |
| 488 | CookieMap::iterator begin, |
| 489 | CookieMap::iterator end); |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 490 | |
| 491 | void SetDefaultCookieableSchemes(); |
| 492 | |
| 493 | void FindCookiesForHostAndDomain(const GURL& url, |
| 494 | const CookieOptions& options, |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 495 | std::vector<CanonicalCookie*>* cookies); |
| 496 | |
| 497 | void FindCookiesForKey(const std::string& key, |
| 498 | const GURL& url, |
| 499 | const CookieOptions& options, |
| 500 | const base::Time& current, |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 501 | std::vector<CanonicalCookie*>* cookies); |
| 502 | |
| 503 | // Delete any cookies that are equivalent to |ecc| (same path, domain, etc). |
mmenke | 009cf62e | 2016-07-18 19:33:31 | [diff] [blame] | 504 | // |source_url| is the URL that is attempting to set the cookie. |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 505 | // If |skip_httponly| is true, httponly cookies will not be deleted. The |
jww | 601411a | 2015-11-20 19:46:57 | [diff] [blame] | 506 | // return value will be true if |skip_httponly| skipped an httponly cookie or |
jww | a26e439d | 2017-01-27 18:17:27 | [diff] [blame^] | 507 | // the cookie to delete was Secure and the scheme of |ecc| is insecure. |key| |
| 508 | // is the key to find the cookie in cookies_; see the comment before the |
| 509 | // CookieMap typedef for details. |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 510 | // NOTE: There should never be more than a single matching equivalent cookie. |
| 511 | bool DeleteAnyEquivalentCookie(const std::string& key, |
| 512 | const CanonicalCookie& ecc, |
mmenke | 009cf62e | 2016-07-18 19:33:31 | [diff] [blame] | 513 | const GURL& source_url, |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 514 | bool skip_httponly, |
jww | a26e439d | 2017-01-27 18:17:27 | [diff] [blame^] | 515 | bool already_expired); |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 516 | |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 517 | // Inserts |cc| into cookies_. Returns an iterator that points to the inserted |
[email protected] | 6210ce5 | 2013-09-20 03:33:14 | [diff] [blame] | 518 | // cookie in cookies_. Guarantee: all iterators to cookies_ remain valid. |
| 519 | CookieMap::iterator InternalInsertCookie(const std::string& key, |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 520 | std::unique_ptr<CanonicalCookie> cc, |
mmenke | 009cf62e | 2016-07-18 19:33:31 | [diff] [blame] | 521 | const GURL& source_url, |
[email protected] | 6210ce5 | 2013-09-20 03:33:14 | [diff] [blame] | 522 | bool sync_to_store); |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 523 | |
| 524 | // Helper function that sets cookies with more control. |
| 525 | // Not exposed as we don't want callers to have the ability |
| 526 | // to specify (potentially duplicate) creation times. |
| 527 | bool SetCookieWithCreationTimeAndOptions(const GURL& url, |
| 528 | const std::string& cookie_line, |
| 529 | const base::Time& creation_time, |
| 530 | const CookieOptions& options); |
| 531 | |
| 532 | // Helper function that sets a canonical cookie, deleting equivalents and |
| 533 | // performing garbage collection. |
mmenke | 009cf62e | 2016-07-18 19:33:31 | [diff] [blame] | 534 | // |source_url| is the URL that's attempting to set the cookie. |
danakj | a9850e1 | 2016-04-18 22:28:08 | [diff] [blame] | 535 | bool SetCanonicalCookie(std::unique_ptr<CanonicalCookie> cc, |
mmenke | 009cf62e | 2016-07-18 19:33:31 | [diff] [blame] | 536 | const GURL& source_url, |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 537 | const CookieOptions& options); |
| 538 | |
droger | d5d1278c | 2015-03-17 19:21:51 | [diff] [blame] | 539 | // Helper function calling SetCanonicalCookie() for all cookies in |list|. |
| 540 | bool SetCanonicalCookies(const CookieList& list); |
| 541 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 542 | void InternalUpdateCookieAccessTime(CanonicalCookie* cc, |
| 543 | const base::Time& current_time); |
| 544 | |
| 545 | // |deletion_cause| argument is used for collecting statistics and choosing |
nharper | 352933e | 2016-09-30 18:24:57 | [diff] [blame] | 546 | // the correct CookieStore::ChangeCause for OnCookieChanged |
[email protected] | 7c4b66b | 2014-01-04 12:28:13 | [diff] [blame] | 547 | // notifications. Guarantee: All iterators to cookies_ except to the |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 548 | // deleted entry remain valid. |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 549 | void InternalDeleteCookie(CookieMap::iterator it, |
| 550 | bool sync_to_store, |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 551 | DeletionCause deletion_cause); |
| 552 | |
| 553 | // If the number of cookies for CookieMap key |key|, or globally, are |
| 554 | // over the preset maximums above, garbage collect, first for the host and |
| 555 | // then globally. See comments above garbage collection threshold |
| 556 | // constants for details. |
| 557 | // |
| 558 | // Returns the number of cookies deleted (useful for debugging). |
jww | a26e439d | 2017-01-27 18:17:27 | [diff] [blame^] | 559 | size_t GarbageCollect(const base::Time& current, const std::string& key); |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 560 | |
mkwst | e079ac41 | 2016-03-11 09:04:06 | [diff] [blame] | 561 | // Helper for GarbageCollect(). Deletes up to |purge_goal| cookies with a |
| 562 | // priority less than or equal to |priority| from |cookies|, while ensuring |
| 563 | // that at least the |to_protect| most-recent cookies are retained. |
jww | c00ac71 | 2016-05-05 22:21:44 | [diff] [blame] | 564 | // |protected_secure_cookies| specifies whether or not secure cookies should |
| 565 | // be protected from deletion. |
mkwst | e079ac41 | 2016-03-11 09:04:06 | [diff] [blame] | 566 | // |
| 567 | // |cookies| must be sorted from least-recent to most-recent. |
| 568 | // |
mkwst | e079ac41 | 2016-03-11 09:04:06 | [diff] [blame] | 569 | // Returns the number of cookies deleted. |
| 570 | size_t PurgeLeastRecentMatches(CookieItVector* cookies, |
| 571 | CookiePriority priority, |
| 572 | size_t to_protect, |
jww | c00ac71 | 2016-05-05 22:21:44 | [diff] [blame] | 573 | size_t purge_goal, |
| 574 | bool protect_secure_cookies); |
mkwst | e079ac41 | 2016-03-11 09:04:06 | [diff] [blame] | 575 | |
jww | 82d99c1 | 2015-11-25 18:39:53 | [diff] [blame] | 576 | // Helper for GarbageCollect(); can be called directly as well. Deletes all |
| 577 | // expired cookies in |itpair|. If |cookie_its| is non-NULL, all the |
| 578 | // non-expired cookies from |itpair| are appended to |cookie_its|. |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 579 | // |
| 580 | // Returns the number of cookies deleted. |
jww | 82d99c1 | 2015-11-25 18:39:53 | [diff] [blame] | 581 | size_t GarbageCollectExpired(const base::Time& current, |
| 582 | const CookieMapItPair& itpair, |
| 583 | CookieItVector* cookie_its); |
| 584 | |
[email protected] | 8ad5d46 | 2013-05-02 08:45:26 | [diff] [blame] | 585 | // Helper for GarbageCollect(). Deletes all cookies in the range specified by |
| 586 | // [|it_begin|, |it_end|). Returns the number of cookies deleted. |
jww | 82d99c1 | 2015-11-25 18:39:53 | [diff] [blame] | 587 | size_t GarbageCollectDeleteRange(const base::Time& current, |
| 588 | DeletionCause cause, |
| 589 | CookieItVector::iterator cookie_its_begin, |
| 590 | CookieItVector::iterator cookie_its_end); |
| 591 | |
| 592 | // Helper for GarbageCollect(). Deletes cookies in |cookie_its| from least to |
| 593 | // most recently used, but only before |safe_date|. Also will stop deleting |
| 594 | // when the number of remaining cookies hits |purge_goal|. |
| 595 | size_t GarbageCollectLeastRecentlyAccessed(const base::Time& current, |
| 596 | const base::Time& safe_date, |
| 597 | size_t purge_goal, |
| 598 | CookieItVector cookie_its); |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 599 | |
davidben | 879199c | 2015-03-06 00:55:04 | [diff] [blame] | 600 | // Find the key (for lookup in cookies_) based on the given domain. |
| 601 | // See comment on keys before the CookieMap typedef. |
| 602 | std::string GetKey(const std::string& domain) const; |
| 603 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 604 | bool HasCookieableScheme(const GURL& url); |
| 605 | |
| 606 | // Statistics support |
| 607 | |
| 608 | // This function should be called repeatedly, and will record |
| 609 | // statistics if a sufficient time period has passed. |
| 610 | void RecordPeriodicStats(const base::Time& current_time); |
| 611 | |
| 612 | // Initialize the above variables; should only be called from |
| 613 | // the constructor. |
| 614 | void InitializeHistograms(); |
| 615 | |
| 616 | // The resolution of our time isn't enough, so we do something |
| 617 | // ugly and increment when we've seen the same time twice. |
| 618 | base::Time CurrentTime(); |
| 619 | |
| 620 | // Runs the task if, or defers the task until, the full cookie database is |
| 621 | // loaded. |
| 622 | void DoCookieTask(const scoped_refptr<CookieMonsterTask>& task_item); |
| 623 | |
| 624 | // Runs the task if, or defers the task until, the cookies for the given URL |
| 625 | // are loaded. |
| 626 | void DoCookieTaskForURL(const scoped_refptr<CookieMonsterTask>& task_item, |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 627 | const GURL& url); |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 628 | |
droger | d5d1278c | 2015-03-17 19:21:51 | [diff] [blame] | 629 | // Computes the difference between |old_cookies| and |new_cookies|, and writes |
| 630 | // the result in |cookies_to_add| and |cookies_to_delete|. |
| 631 | // This function has the side effect of changing the order of |old_cookies| |
| 632 | // and |new_cookies|. |cookies_to_add| and |cookies_to_delete| must be empty, |
| 633 | // and none of the arguments can be null. |
| 634 | void ComputeCookieDiff(CookieList* old_cookies, |
| 635 | CookieList* new_cookies, |
| 636 | CookieList* cookies_to_add, |
| 637 | CookieList* cookies_to_delete); |
| 638 | |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 639 | // Runs the given callback. Used to avoid running callbacks after the store |
| 640 | // has been destroyed. |
| 641 | void RunCallback(const base::Closure& callback); |
| 642 | |
msarda | 0aad8f0 | 2014-10-30 09:22:39 | [diff] [blame] | 643 | // Run all cookie changed callbacks that are monitoring |cookie|. |
| 644 | // |removed| is true if the cookie was deleted. |
nharper | 352933e | 2016-09-30 18:24:57 | [diff] [blame] | 645 | void RunCookieChangedCallbacks(const CanonicalCookie& cookie, |
| 646 | CookieStore::ChangeCause cause); |
msarda | 0aad8f0 | 2014-10-30 09:22:39 | [diff] [blame] | 647 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 648 | // Histogram variables; see CookieMonster::InitializeHistograms() in |
| 649 | // cookie_monster.cc for details. |
[email protected] | de41555 | 2013-01-23 04:12:17 | [diff] [blame] | 650 | base::HistogramBase* histogram_expiration_duration_minutes_; |
[email protected] | de41555 | 2013-01-23 04:12:17 | [diff] [blame] | 651 | base::HistogramBase* histogram_evicted_last_access_minutes_; |
| 652 | base::HistogramBase* histogram_count_; |
[email protected] | de41555 | 2013-01-23 04:12:17 | [diff] [blame] | 653 | base::HistogramBase* histogram_cookie_deletion_cause_; |
mkwst | c1aa4cc | 2015-04-03 19:57:45 | [diff] [blame] | 654 | base::HistogramBase* histogram_cookie_type_; |
estark | 7feb65c2b | 2015-08-21 23:38:20 | [diff] [blame] | 655 | base::HistogramBase* histogram_cookie_source_scheme_; |
jww | 31e3263 | 2015-12-16 23:38:34 | [diff] [blame] | 656 | base::HistogramBase* histogram_cookie_delete_equivalent_; |
[email protected] | de41555 | 2013-01-23 04:12:17 | [diff] [blame] | 657 | base::HistogramBase* histogram_time_blocked_on_load_; |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 658 | |
| 659 | CookieMap cookies_; |
| 660 | |
erikchen | 1dd72a7 | 2015-05-06 20:45:05 | [diff] [blame] | 661 | // Indicates whether the cookie store has been initialized. |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 662 | bool initialized_; |
| 663 | |
erikchen | 1dd72a7 | 2015-05-06 20:45:05 | [diff] [blame] | 664 | // Indicates whether the cookie store has started fetching all cookies. |
| 665 | bool started_fetching_all_cookies_; |
| 666 | // Indicates whether the cookie store has finished fetching all cookies. |
| 667 | bool finished_fetching_all_cookies_; |
| 668 | // The strategy to use for fetching cookies. |
| 669 | FetchStrategy fetch_strategy_; |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 670 | |
| 671 | // List of domain keys that have been loaded from the DB. |
| 672 | std::set<std::string> keys_loaded_; |
| 673 | |
| 674 | // Map of domain keys to their associated task queues. These tasks are blocked |
| 675 | // until all cookies for the associated domain key eTLD+1 are loaded from the |
| 676 | // backend store. |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 677 | std::map<std::string, std::deque<scoped_refptr<CookieMonsterTask>>> |
[email protected] | 0184df3 | 2013-05-14 00:53:55 | [diff] [blame] | 678 | tasks_pending_for_key_; |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 679 | |
| 680 | // Queues tasks that are blocked until all cookies are loaded from the backend |
| 681 | // store. |
mmenke | f49fca0e | 2016-03-08 12:46:24 | [diff] [blame] | 682 | std::deque<scoped_refptr<CookieMonsterTask>> tasks_pending_; |
| 683 | |
| 684 | // Once a global cookie task has been seen, all per-key tasks must be put in |
| 685 | // |tasks_pending_| instead of |tasks_pending_for_key_| to ensure a reasonable |
| 686 | // view of the cookie store. This more to ensure fancy cookie export/import |
| 687 | // code has a consistent view of the CookieStore, rather than out of concern |
| 688 | // for typical use. |
| 689 | bool seen_global_task_; |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 690 | |
| 691 | scoped_refptr<PersistentCookieStore> store_; |
| 692 | |
| 693 | base::Time last_time_seen_; |
| 694 | |
| 695 | // Minimum delay after updating a cookie's LastAccessDate before we will |
| 696 | // update it again. |
| 697 | const base::TimeDelta last_access_threshold_; |
| 698 | |
| 699 | // Approximate date of access time of least recently accessed cookie |
| 700 | // in |cookies_|. Note that this is not guaranteed to be accurate, only a) |
| 701 | // to be before or equal to the actual time, and b) to be accurate |
| 702 | // immediately after a garbage collection that scans through all the cookies. |
| 703 | // This value is used to determine whether global garbage collection might |
| 704 | // find cookies to purge. |
| 705 | // Note: The default Time() constructor will create a value that compares |
| 706 | // earlier than any other time value, which is wanted. Thus this |
| 707 | // value is not initialized. |
| 708 | base::Time earliest_access_time_; |
| 709 | |
| 710 | // During loading, holds the set of all loaded cookie creation times. Used to |
| 711 | // avoid ever letting cookies with duplicate creation times into the store; |
| 712 | // that way we don't have to worry about what sections of code are safe |
| 713 | // to call while it's in that state. |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 714 | std::set<int64_t> creation_times_; |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 715 | |
| 716 | std::vector<std::string> cookieable_schemes_; |
| 717 | |
[email protected] | 7c4b66b | 2014-01-04 12:28:13 | [diff] [blame] | 718 | scoped_refptr<CookieMonsterDelegate> delegate_; |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 719 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 720 | base::Time last_statistic_record_time_; |
| 721 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 722 | bool persist_session_cookies_; |
| 723 | |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 724 | using CookieChangedHookMap = |
| 725 | std::map<std::pair<GURL, std::string>, |
| 726 | std::unique_ptr<CookieChangedCallbackList>>; |
ellyjones | 399e35a2 | 2014-10-27 11:09:56 | [diff] [blame] | 727 | CookieChangedHookMap hook_map_; |
| 728 | |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 729 | base::ThreadChecker thread_checker_; |
| 730 | |
| 731 | base::WeakPtrFactory<CookieMonster> weak_ptr_factory_; |
| 732 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 733 | DISALLOW_COPY_AND_ASSIGN(CookieMonster); |
| 734 | }; |
| 735 | |
[email protected] | 7c4b66b | 2014-01-04 12:28:13 | [diff] [blame] | 736 | class NET_EXPORT CookieMonsterDelegate |
| 737 | : public base::RefCountedThreadSafe<CookieMonsterDelegate> { |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 738 | public: |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 739 | // Will be called when a cookie is added or removed. The function is passed |
| 740 | // the respective |cookie| which was added to or removed from the cookies. |
| 741 | // If |removed| is true, the cookie was deleted, and |cause| will be set |
[email protected] | a2c92a1c | 2012-04-03 12:32:14 | [diff] [blame] | 742 | // to the reason for its removal. If |removed| is false, the cookie was |
nharper | 352933e | 2016-09-30 18:24:57 | [diff] [blame] | 743 | // added, and |cause| will be set to ChangeCause::EXPLICIT. |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 744 | // |
| 745 | // As a special case, note that updating a cookie's properties is implemented |
| 746 | // as a two step process: the cookie to be updated is first removed entirely, |
nharper | 352933e | 2016-09-30 18:24:57 | [diff] [blame] | 747 | // generating a notification with cause ChangeCause::OVERWRITE. Afterwards, |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 748 | // a new cookie is written with the updated values, generating a notification |
nharper | 352933e | 2016-09-30 18:24:57 | [diff] [blame] | 749 | // with cause ChangeCause::EXPLICIT. |
[email protected] | 5b9bc35 | 2012-07-18 13:13:34 | [diff] [blame] | 750 | virtual void OnCookieChanged(const CanonicalCookie& cookie, |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 751 | bool removed, |
nharper | 352933e | 2016-09-30 18:24:57 | [diff] [blame] | 752 | CookieStore::ChangeCause cause) = 0; |
| 753 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 754 | protected: |
[email protected] | 7c4b66b | 2014-01-04 12:28:13 | [diff] [blame] | 755 | friend class base::RefCountedThreadSafe<CookieMonsterDelegate>; |
| 756 | virtual ~CookieMonsterDelegate() {} |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 757 | }; |
| 758 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 759 | typedef base::RefCountedThreadSafe<CookieMonster::PersistentCookieStore> |
| 760 | RefcountedPersistentCookieStore; |
| 761 | |
[email protected] | c1b6e10 | 2013-04-10 20:54:49 | [diff] [blame] | 762 | class NET_EXPORT CookieMonster::PersistentCookieStore |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 763 | : public RefcountedPersistentCookieStore { |
| 764 | public: |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 765 | typedef base::Callback<void(std::vector<std::unique_ptr<CanonicalCookie>>)> |
[email protected] | 5b9bc35 | 2012-07-18 13:13:34 | [diff] [blame] | 766 | LoadedCallback; |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 767 | |
erikchen | 1dd72a7 | 2015-05-06 20:45:05 | [diff] [blame] | 768 | // TODO(erikchen): Depending on the results of the cookie monster Finch |
| 769 | // experiment, update the name and description of this method. The behavior |
| 770 | // of this method doesn't change, but it has different semantics for the two |
| 771 | // different logic paths. See http://crbug.com/473483. |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 772 | // Initializes the store and retrieves the existing cookies. This will be |
| 773 | // called only once at startup. The callback will return all the cookies |
| 774 | // that are not yet returned to CookieMonster by previous priority loads. |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 775 | // |
| 776 | // |loaded_callback| may not be NULL. |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 777 | virtual void Load(const LoadedCallback& loaded_callback) = 0; |
| 778 | |
| 779 | // Does a priority load of all cookies for the domain key (eTLD+1). The |
| 780 | // callback will return all the cookies that are not yet returned by previous |
| 781 | // loads, which includes cookies for the requested domain key if they are not |
| 782 | // already returned, plus all cookies that are chain-loaded and not yet |
| 783 | // returned to CookieMonster. |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 784 | // |
| 785 | // |loaded_callback| may not be NULL. |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 786 | virtual void LoadCookiesForKey(const std::string& key, |
[email protected] | dedec0b | 2013-02-28 04:50:10 | [diff] [blame] | 787 | const LoadedCallback& loaded_callback) = 0; |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 788 | |
| 789 | virtual void AddCookie(const CanonicalCookie& cc) = 0; |
| 790 | virtual void UpdateCookieAccessTime(const CanonicalCookie& cc) = 0; |
| 791 | virtual void DeleteCookie(const CanonicalCookie& cc) = 0; |
| 792 | |
[email protected] | bf510ed | 2012-06-05 08:31:43 | [diff] [blame] | 793 | // Instructs the store to not discard session only cookies on shutdown. |
| 794 | virtual void SetForceKeepSessionState() = 0; |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 795 | |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 796 | // Flushes the store and posts |callback| when complete. |callback| may be |
| 797 | // NULL. |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 798 | virtual void Flush(const base::Closure& callback) = 0; |
| 799 | |
| 800 | protected: |
| 801 | PersistentCookieStore() {} |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 802 | virtual ~PersistentCookieStore() {} |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 803 | |
| 804 | private: |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 805 | friend class base::RefCountedThreadSafe<PersistentCookieStore>; |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 806 | DISALLOW_COPY_AND_ASSIGN(PersistentCookieStore); |
| 807 | }; |
| 808 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 809 | } // namespace net |
| 810 | |
| 811 | #endif // NET_COOKIES_COOKIE_MONSTER_H_ |