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