blob: 083c1941892ac48463b58cbc7dee891a1f22ad0f [file] [log] [blame]
[email protected]63ee33bd2012-03-15 09:29:581// 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]63ee33bd2012-03-15 09:29:589
Avi Drissman13fc8932015-12-20 04:40:4610#include <stddef.h>
11#include <stdint.h>
12
[email protected]63ee33bd2012-03-15 09:29:5813#include <map>
danakja9850e12016-04-18 22:28:0814#include <memory>
[email protected]63ee33bd2012-03-15 09:29:5815#include <set>
16#include <string>
17#include <utility>
18#include <vector>
19
[email protected]63ee33bd2012-03-15 09:29:5820#include "base/callback_forward.h"
Brett Wilsonc6a0c822017-09-12 00:04:2921#include "base/containers/circular_deque.h"
[email protected]63ee33bd2012-03-15 09:29:5822#include "base/gtest_prod_util.h"
Avi Drissman13fc8932015-12-20 04:40:4623#include "base/macros.h"
[email protected]63ee33bd2012-03-15 09:29:5824#include "base/memory/ref_counted.h"
mmenkebe0910d2016-03-01 19:09:0925#include "base/memory/weak_ptr.h"
26#include "base/threading/thread_checker.h"
[email protected]9da992db2013-06-28 05:40:4727#include "base/time/time.h"
[email protected]565c3f42012-08-14 14:22:5828#include "net/base/net_export.h"
[email protected]8da4b1812012-07-25 13:54:3829#include "net/cookies/canonical_cookie.h"
[email protected]ab2d75c82013-04-19 18:39:0430#include "net/cookies/cookie_constants.h"
Victor Costan14f47c12018-03-01 08:02:2431#include "net/cookies/cookie_monster_change_dispatcher.h"
[email protected]63ee33bd2012-03-15 09:29:5832#include "net/cookies/cookie_store.h"
ellyjones399e35a22014-10-27 11:09:5633#include "url/gurl.h"
[email protected]63ee33bd2012-03-15 09:29:5834
35namespace base {
[email protected]de415552013-01-23 04:12:1736class HistogramBase;
[email protected]63ee33bd2012-03-15 09:29:5837} // namespace base
38
39namespace net {
40
nharper2b0ad9a2017-05-22 18:33:4541class ChannelIDService;
Victor Costan14f47c12018-03-01 08:02:2442class CookieChangeDispatcher;
[email protected]63ee33bd2012-03-15 09:29:5843
44// The cookie monster is the system for storing and retrieving cookies. It has
45// an in-memory list of all cookies, and synchronizes non-session cookies to an
46// optional permanent storage that implements the PersistentCookieStore
47// interface.
48//
mmenke96f3bab2016-01-22 17:34:0249// Tasks may be deferred if all affected cookies are not yet loaded from the
50// backing store. Otherwise, callbacks may be invoked immediately.
[email protected]63ee33bd2012-03-15 09:29:5851//
52// A cookie task is either pending loading of the entire cookie store, or
avie7cd11a2016-10-11 02:00:3553// loading of cookies for a specific domain key(eTLD+1). In the former case, the
rdsmithe5c701d2017-07-12 21:50:0054// cookie callback will be queued in tasks_pending_ while PersistentCookieStore
[email protected]0184df32013-05-14 00:53:5555// chain loads the cookie store on DB thread. In the latter case, the cookie
rdsmithe5c701d2017-07-12 21:50:0056// callback will be queued in tasks_pending_for_key_ while PermanentCookieStore
[email protected]0184df32013-05-14 00:53:5557// loads cookies for the specified domain key(eTLD+1) on DB thread.
[email protected]63ee33bd2012-03-15 09:29:5858//
[email protected]63ee33bd2012-03-15 09:29:5859// TODO(deanm) Implement CookieMonster, the cookie database.
60// - Verify that our domain enforcement and non-dotted handling is correct
61class NET_EXPORT CookieMonster : public CookieStore {
62 public:
[email protected]63ee33bd2012-03-15 09:29:5863 class PersistentCookieStore;
64
65 // Terminology:
66 // * The 'top level domain' (TLD) of an internet domain name is
67 // the terminal "." free substring (e.g. "com" for google.com
68 // or world.std.com).
69 // * The 'effective top level domain' (eTLD) is the longest
70 // "." initiated terminal substring of an internet domain name
71 // that is controlled by a general domain registrar.
72 // (e.g. "co.uk" for news.bbc.co.uk).
73 // * The 'effective top level domain plus one' (eTLD+1) is the
74 // shortest "." delimited terminal substring of an internet
75 // domain name that is not controlled by a general domain
76 // registrar (e.g. "bbc.co.uk" for news.bbc.co.uk, or
77 // "google.com" for news.google.com). The general assumption
78 // is that all hosts and domains under an eTLD+1 share some
79 // administrative control.
80
81 // CookieMap is the central data structure of the CookieMonster. It
82 // is a map whose values are pointers to CanonicalCookie data
83 // structures (the data structures are owned by the CookieMonster
84 // and must be destroyed when removed from the map). The key is based on the
85 // effective domain of the cookies. If the domain of the cookie has an
86 // eTLD+1, that is the key for the map. If the domain of the cookie does not
87 // have an eTLD+1, the key of the map is the host the cookie applies to (it is
88 // not legal to have domain cookies without an eTLD+1). This rule
89 // excludes cookies for, e.g, ".com", ".co.uk", or ".internalnetwork".
90 // This behavior is the same as the behavior in Firefox v 3.6.10.
91
92 // NOTE(deanm):
93 // I benchmarked hash_multimap vs multimap. We're going to be query-heavy
94 // so it would seem like hashing would help. However they were very
95 // close, with multimap being a tiny bit faster. I think this is because
96 // our map is at max around 1000 entries, and the additional complexity
97 // for the hashing might not overcome the O(log(1000)) for querying
98 // a multimap. Also, multimap is standard, another reason to use it.
99 // TODO(rdsmith): This benchmark should be re-done now that we're allowing
avie7cd11a2016-10-11 02:00:35100 // substantially more entries in the map.
101 using CookieMap =
102 std::multimap<std::string, std::unique_ptr<CanonicalCookie>>;
103 using CookieMapItPair = std::pair<CookieMap::iterator, CookieMap::iterator>;
104 using CookieItVector = std::vector<CookieMap::iterator>;
[email protected]8ad5d462013-05-02 08:45:26105
106 // Cookie garbage collection thresholds. Based off of the Mozilla defaults.
107 // When the number of cookies gets to k{Domain,}MaxCookies
108 // purge down to k{Domain,}MaxCookies - k{Domain,}PurgeCookies.
109 // It might seem scary to have a high purge value, but really it's not.
110 // You just make sure that you increase the max to cover the increase
111 // in purge, and we would have been purging the same number of cookies.
112 // We're just going through the garbage collection process less often.
113 // Note that the DOMAIN values are per eTLD+1; see comment for the
114 // CookieMap typedef. So, e.g., the maximum number of cookies allowed for
115 // google.com and all of its subdomains will be 150-180.
116 //
117 // Any cookies accessed more recently than kSafeFromGlobalPurgeDays will not
118 // be evicted by global garbage collection, even if we have more than
119 // kMaxCookies. This does not affect domain garbage collection.
120 static const size_t kDomainMaxCookies;
121 static const size_t kDomainPurgeCookies;
122 static const size_t kMaxCookies;
123 static const size_t kPurgeCookies;
124
125 // Quota for cookies with {low, medium, high} priorities within a domain.
mkwst87734352016-03-03 17:36:23126 static const size_t kDomainCookiesQuotaLow;
127 static const size_t kDomainCookiesQuotaMedium;
128 static const size_t kDomainCookiesQuotaHigh;
[email protected]63ee33bd2012-03-15 09:29:58129
130 // The store passed in should not have had Init() called on it yet. This
131 // class will take care of initializing it. The backing store is NOT owned by
132 // this class, but it must remain valid for the duration of the cookie
133 // monster's existence. If |store| is NULL, then no backing store will be
Randy Smith0a522662017-08-30 19:35:34134 // updated.
Thomas Anderson1a03bbe2018-03-02 19:05:47135 explicit CookieMonster(PersistentCookieStore* store);
[email protected]63ee33bd2012-03-15 09:29:58136
nharper2b0ad9a2017-05-22 18:33:45137 // Like above, but includes a non-owning pointer |channel_id_service| for the
138 // corresponding ChannelIDService used with this CookieStore. The
139 // |channel_id_service| must outlive the CookieMonster.
140 CookieMonster(PersistentCookieStore* store,
Thomas Anderson1a03bbe2018-03-02 19:05:47141 ChannelIDService* channel_id_service);
nharper2b0ad9a2017-05-22 18:33:45142
[email protected]63ee33bd2012-03-15 09:29:58143 // Only used during unit testing.
144 CookieMonster(PersistentCookieStore* store,
shessf0bc1182016-05-19 04:35:58145 base::TimeDelta last_access_threshold);
[email protected]63ee33bd2012-03-15 09:29:58146
mmenke606c59c2016-03-07 18:20:55147 ~CookieMonster() override;
148
rdsmith0e84cea2017-07-13 03:09:53149 // Writes all the cookies in |list| into the store, replacing all cookies
150 // currently present in store.
rdsmith2709eee2017-06-20 22:43:27151 // This method does not flush the backend.
152 // TODO(rdsmith, mmenke): Do not use this function; it is deprecated
153 // and should be removed.
154 // See https://codereview.chromium.org/2882063002/#msg64.
rdsmith7ac81712017-06-22 17:09:54155 void SetAllCookiesAsync(const CookieList& list, SetCookiesCallback callback);
drogerd5d1278c2015-03-17 19:21:51156
[email protected]63ee33bd2012-03-15 09:29:58157 // CookieStore implementation.
dchengb03027d2014-10-21 12:00:20158 void SetCookieWithOptionsAsync(const GURL& url,
159 const std::string& cookie_line,
160 const CookieOptions& options,
rdsmith7ac81712017-06-22 17:09:54161 SetCookiesCallback callback) override;
rdsmitha6ce4442017-06-21 17:11:05162 void SetCanonicalCookieAsync(std::unique_ptr<CanonicalCookie> cookie,
163 bool secure_source,
164 bool modify_http_only,
rdsmith7ac81712017-06-22 17:09:54165 SetCookiesCallback callback) override;
rdsmith7ac81712017-06-22 17:09:54166 void GetCookieListWithOptionsAsync(const GURL& url,
167 const CookieOptions& options,
168 GetCookieListCallback callback) override;
169 void GetAllCookiesAsync(GetCookieListCallback callback) override;
dchengb03027d2014-10-21 12:00:20170 void DeleteCookieAsync(const GURL& url,
171 const std::string& cookie_name,
rdsmith7ac81712017-06-22 17:09:54172 base::OnceClosure callback) override;
mmenke24379d52016-02-05 23:50:17173 void DeleteCanonicalCookieAsync(const CanonicalCookie& cookie,
rdsmith7ac81712017-06-22 17:09:54174 DeleteCallback callback) override;
dchengb03027d2014-10-21 12:00:20175 void DeleteAllCreatedBetweenAsync(const base::Time& delete_begin,
176 const base::Time& delete_end,
rdsmith7ac81712017-06-22 17:09:54177 DeleteCallback callback) override;
dmurphfaea244c2016-04-09 00:42:30178 void DeleteAllCreatedBetweenWithPredicateAsync(
179 const base::Time& delete_begin,
180 const base::Time& delete_end,
181 const base::Callback<bool(const CanonicalCookie&)>& predicate,
rdsmith7ac81712017-06-22 17:09:54182 DeleteCallback callback) override;
183 void DeleteSessionCookiesAsync(DeleteCallback) override;
184 void FlushStore(base::OnceClosure callback) override;
mmenkeded79da2016-02-06 08:28:51185 void SetForceKeepSessionState() override;
Victor Costan14f47c12018-03-01 08:02:24186 CookieChangeDispatcher& GetChangeDispatcher() override;
[email protected]264807b2012-04-25 14:49:37187
mmenke74bcbd52016-01-21 17:17:56188 // Resets the list of cookieable schemes to the supplied schemes. Does
189 // nothing if called after first use of the instance (i.e. after the
190 // instance initialization process).
mmenke18dd8ba2016-02-01 18:42:10191 void SetCookieableSchemes(const std::vector<std::string>& schemes);
mmenke74bcbd52016-01-21 17:17:56192
[email protected]63ee33bd2012-03-15 09:29:58193 // Enables writing session cookies into the cookie database. If this this
194 // method is called, it must be called before first use of the instance
195 // (i.e. as part of the instance initialization process).
196 void SetPersistSessionCookies(bool persist_session_cookies);
197
[email protected]97a3b6e2012-06-12 01:53:56198 // Determines if the scheme of the URL is a scheme that cookies will be
199 // stored for.
200 bool IsCookieableScheme(const std::string& scheme);
201
[email protected]63ee33bd2012-03-15 09:29:58202 // The default list of schemes the cookie monster can handle.
[email protected]5edff3c52014-06-23 20:27:48203 static const char* const kDefaultCookieableSchemes[];
[email protected]63ee33bd2012-03-15 09:29:58204 static const int kDefaultCookieableSchemesCount;
205
nharper5babb5e62016-03-09 18:58:07206 bool IsEphemeral() override;
207
[email protected]63ee33bd2012-03-15 09:29:58208 private:
nharper2b0ad9a2017-05-22 18:33:45209 CookieMonster(PersistentCookieStore* store,
nharper2b0ad9a2017-05-22 18:33:45210 ChannelIDService* channel_id_service,
211 base::TimeDelta last_access_threshold);
212
avie7cd11a2016-10-11 02:00:35213 // For garbage collection constants.
[email protected]63ee33bd2012-03-15 09:29:58214 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestHostGarbageCollection);
[email protected]63ee33bd2012-03-15 09:29:58215 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, GarbageCollectionTriggers);
mmenkef4721d992017-06-07 17:13:59216 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest,
217 GarbageCollectWithSecureCookiesOnly);
[email protected]63ee33bd2012-03-15 09:29:58218 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestGCTimes);
219
220 // For validation of key values.
221 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestDomainTree);
222 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestImport);
223 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, GetKey);
224 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestGetKey);
225
226 // For FindCookiesForKey.
227 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, ShortLivedSessionCookies);
228
estark7feb65c2b2015-08-21 23:38:20229 // For CookieSource histogram enum.
230 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, CookieSourceHistogram);
231
jww31e32632015-12-16 23:38:34232 // For kSafeFromGlobalPurgeDays in CookieStore.
jwwa26e439d2017-01-27 18:17:27233 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, EvictSecureCookies);
jww82d99c12015-11-25 18:39:53234
jww31e32632015-12-16 23:38:34235 // For CookieDeleteEquivalent histogram enum.
236 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest,
237 CookieDeleteEquivalentHistogramTest);
jww31e32632015-12-16 23:38:34238
[email protected]63ee33bd2012-03-15 09:29:58239 // Internal reasons for deletion, used to populate informative histograms
240 // and to provide a public cause for onCookieChange notifications.
241 //
242 // If you add or remove causes from this list, please be sure to also update
Victor Costan14f47c12018-03-01 08:02:24243 // the CookieChangeCause mapping inside ChangeCauseMapping. New items (if
244 // necessary) should be added at the end of the list, just before
Nick Harper7a6683a2018-01-30 20:42:52245 // DELETE_COOKIE_LAST_ENTRY.
[email protected]63ee33bd2012-03-15 09:29:58246 enum DeletionCause {
247 DELETE_COOKIE_EXPLICIT = 0,
mkwstaa07ee82016-03-11 15:32:14248 DELETE_COOKIE_OVERWRITE = 1,
249 DELETE_COOKIE_EXPIRED = 2,
250 DELETE_COOKIE_EVICTED = 3,
251 DELETE_COOKIE_DUPLICATE_IN_BACKING_STORE = 4,
252 DELETE_COOKIE_DONT_RECORD = 5, // For final cleanup after flush to store.
[email protected]63ee33bd2012-03-15 09:29:58253
mkwstaa07ee82016-03-11 15:32:14254 // Cookies evicted during domain-level garbage collection.
255 DELETE_COOKIE_EVICTED_DOMAIN = 6,
[email protected]63ee33bd2012-03-15 09:29:58256
mkwstaa07ee82016-03-11 15:32:14257 // Cookies evicted during global garbage collection (which takes place after
258 // domain-level garbage collection fails to bring the cookie store under
259 // the overall quota.
260 DELETE_COOKIE_EVICTED_GLOBAL = 7,
261
262 // #8 was DELETE_COOKIE_EVICTED_DOMAIN_PRE_SAFE
263 // #9 was DELETE_COOKIE_EVICTED_DOMAIN_POST_SAFE
[email protected]63ee33bd2012-03-15 09:29:58264
265 // A common idiom is to remove a cookie by overwriting it with an
266 // already-expired expiration date. This captures that case.
mkwstaa07ee82016-03-11 15:32:14267 DELETE_COOKIE_EXPIRED_OVERWRITE = 10,
[email protected]63ee33bd2012-03-15 09:29:58268
[email protected]6210ce52013-09-20 03:33:14269 // Cookies are not allowed to contain control characters in the name or
270 // value. However, we used to allow them, so we are now evicting any such
271 // cookies as we load them. See http://crbug.com/238041.
mkwstaa07ee82016-03-11 15:32:14272 DELETE_COOKIE_CONTROL_CHAR = 11,
[email protected]6210ce52013-09-20 03:33:14273
jww82d99c12015-11-25 18:39:53274 // When strict secure cookies is enabled, non-secure cookies are evicted
275 // right after expired cookies.
mkwstaa07ee82016-03-11 15:32:14276 DELETE_COOKIE_NON_SECURE = 12,
jww82d99c12015-11-25 18:39:53277
Nick Harper7a6683a2018-01-30 20:42:52278 DELETE_COOKIE_LAST_ENTRY = 13
[email protected]63ee33bd2012-03-15 09:29:58279 };
280
mkwstc1aa4cc2015-04-03 19:57:45281 // This enum is used to generate a histogramed bitmask measureing the types
282 // of stored cookies. Please do not reorder the list when adding new entries.
283 // New items MUST be added at the end of the list, just before
284 // COOKIE_TYPE_LAST_ENTRY;
285 enum CookieType {
mkwst46549412016-02-01 10:05:37286 COOKIE_TYPE_SAME_SITE = 0,
mkwstc1aa4cc2015-04-03 19:57:45287 COOKIE_TYPE_HTTPONLY,
288 COOKIE_TYPE_SECURE,
289 COOKIE_TYPE_LAST_ENTRY
290 };
291
estark7feb65c2b2015-08-21 23:38:20292 // Used to populate a histogram containing information about the
293 // sources of Secure and non-Secure cookies: that is, whether such
294 // cookies are set by origins with cryptographic or non-cryptographic
295 // schemes. Please do not reorder the list when adding new
296 // entries. New items MUST be added at the end of the list, just
297 // before COOKIE_SOURCE_LAST_ENTRY.
298 //
299 // COOKIE_SOURCE_(NON)SECURE_COOKIE_(NON)CRYPTOGRAPHIC_SCHEME means
300 // that a cookie was set or overwritten from a URL with the given type
301 // of scheme. This enum should not be used when cookies are *cleared*,
302 // because its purpose is to understand if Chrome can deprecate the
303 // ability of HTTP urls to set/overwrite Secure cookies.
304 enum CookieSource {
305 COOKIE_SOURCE_SECURE_COOKIE_CRYPTOGRAPHIC_SCHEME = 0,
306 COOKIE_SOURCE_SECURE_COOKIE_NONCRYPTOGRAPHIC_SCHEME,
307 COOKIE_SOURCE_NONSECURE_COOKIE_CRYPTOGRAPHIC_SCHEME,
308 COOKIE_SOURCE_NONSECURE_COOKIE_NONCRYPTOGRAPHIC_SCHEME,
309 COOKIE_SOURCE_LAST_ENTRY
310 };
311
jww31e32632015-12-16 23:38:34312 // Used to populate a histogram for cookie setting in the "delete equivalent"
Mike West86149882017-07-28 10:41:49313 // step. Measures total attempts to delete an equivalent cookie, and
314 // categorizes the outcome.
jww31e32632015-12-16 23:38:34315 //
Mike West86149882017-07-28 10:41:49316 // * COOKIE_DELETE_EQUIVALENT_ATTEMPT is incremented each time a cookie is
317 // set, causing the equivalent deletion algorithm to execute.
318 //
319 // * COOKIE_DELETE_EQUIVALENT_SKIPPING_SECURE is incremented when a non-secure
320 // cookie is ignored because an equivalent, but secure, cookie already
321 // exists.
322 //
323 // * COOKIE_DELETE_EQUIVALENT_WOULD_HAVE_DELETED is incremented when a cookie
324 // is skipped due to `secure` rules (e.g. whenever
325 // COOKIE_DELETE_EQUIVALENT_SKIPPING_SECURE is incremented), but would have
326 // caused a deletion without those rules.
327 //
328 // TODO(mkwst): Now that we've shipped strict secure cookie checks, we don't
329 // need this value anymore.
330 //
331 // * COOKIE_DELETE_EQUIVALENT_FOUND is incremented each time an equivalent
332 // cookie is found (and deleted).
333 //
334 // * COOKIE_DELETE_EQUIVALENT_FOUND_WITH_SAME_VALUE is incremented each time
335 // an equivalent cookie that also shared the same value with the new cookie
336 // is found (and deleted).
337 //
338 // Please do not reorder or remove entries. New entries must be added to the
339 // end of the list, just before COOKIE_DELETE_EQUIVALENT_LAST_ENTRY.
jww31e32632015-12-16 23:38:34340 enum CookieDeleteEquivalent {
341 COOKIE_DELETE_EQUIVALENT_ATTEMPT = 0,
342 COOKIE_DELETE_EQUIVALENT_FOUND,
343 COOKIE_DELETE_EQUIVALENT_SKIPPING_SECURE,
344 COOKIE_DELETE_EQUIVALENT_WOULD_HAVE_DELETED,
Mike West86149882017-07-28 10:41:49345 COOKIE_DELETE_EQUIVALENT_FOUND_WITH_SAME_VALUE,
jww31e32632015-12-16 23:38:34346 COOKIE_DELETE_EQUIVALENT_LAST_ENTRY
347 };
348
[email protected]63ee33bd2012-03-15 09:29:58349 // The number of days since last access that cookies will not be subject
350 // to global garbage collection.
351 static const int kSafeFromGlobalPurgeDays;
352
353 // Record statistics every kRecordStatisticsIntervalSeconds of uptime.
354 static const int kRecordStatisticsIntervalSeconds = 10 * 60;
355
rdsmitha6ce4442017-06-21 17:11:05356 // Sets a canonical cookie, deletes equivalents and performs garbage
357 // collection. |source_secure| indicates if the cookie is being set
358 // from a secure source (e.g. a cryptographic scheme).
359 // |modify_http_only| indicates if this setting operation is allowed
360 // to affect http_only cookies.
rdsmithe5c701d2017-07-12 21:50:00361 void SetCanonicalCookie(std::unique_ptr<CanonicalCookie> cookie,
rdsmitha6ce4442017-06-21 17:11:05362 bool secure_source,
rdsmithe5c701d2017-07-12 21:50:00363 bool can_modify_httponly,
364 SetCookiesCallback callback);
rdsmitha6ce4442017-06-21 17:11:05365
rdsmithe5c701d2017-07-12 21:50:00366 void GetAllCookies(GetCookieListCallback callback);
[email protected]63ee33bd2012-03-15 09:29:58367
rdsmithe5c701d2017-07-12 21:50:00368 void GetCookieListWithOptions(const GURL& url,
369 const CookieOptions& options,
370 GetCookieListCallback callback);
[email protected]63ee33bd2012-03-15 09:29:58371
rdsmithe5c701d2017-07-12 21:50:00372 void DeleteAllCreatedBetween(const base::Time& delete_begin,
373 const base::Time& delete_end,
374 DeleteCallback callback);
[email protected]63ee33bd2012-03-15 09:29:58375
dmurphfaea244c2016-04-09 00:42:30376 // Predicate will be called with the calling thread.
rdsmithe5c701d2017-07-12 21:50:00377 void DeleteAllCreatedBetweenWithPredicate(
dmurphfaea244c2016-04-09 00:42:30378 const base::Time& delete_begin,
379 const base::Time& delete_end,
rdsmithe5c701d2017-07-12 21:50:00380 const base::Callback<bool(const CanonicalCookie&)>& predicate,
381 DeleteCallback callback);
[email protected]63ee33bd2012-03-15 09:29:58382
rdsmithe5c701d2017-07-12 21:50:00383 void SetCookieWithOptions(const GURL& url,
[email protected]63ee33bd2012-03-15 09:29:58384 const std::string& cookie_line,
rdsmithe5c701d2017-07-12 21:50:00385 const CookieOptions& options,
386 SetCookiesCallback callback);
[email protected]63ee33bd2012-03-15 09:29:58387
rdsmithe5c701d2017-07-12 21:50:00388 void DeleteCookie(const GURL& url,
389 const std::string& cookie_name,
390 base::OnceClosure callback);
[email protected]63ee33bd2012-03-15 09:29:58391
rdsmithe5c701d2017-07-12 21:50:00392 void DeleteCanonicalCookie(const CanonicalCookie& cookie,
393 DeleteCallback callback);
mmenke24379d52016-02-05 23:50:17394
rdsmithe5c701d2017-07-12 21:50:00395 void DeleteSessionCookies(DeleteCallback callback);
[email protected]264807b2012-04-25 14:49:37396
erikchen1dd72a72015-05-06 20:45:05397 // The first access to the cookie store initializes it. This method should be
398 // called before any access to the cookie store.
399 void MarkCookieStoreAsInitialized();
[email protected]63ee33bd2012-03-15 09:29:58400
erikchen1dd72a72015-05-06 20:45:05401 // Fetches all cookies if the backing store exists and they're not already
402 // being fetched.
erikchen1dd72a72015-05-06 20:45:05403 void FetchAllCookiesIfNecessary();
404
405 // Fetches all cookies from the backing store.
erikchen1dd72a72015-05-06 20:45:05406 void FetchAllCookies();
407
408 // Whether all cookies should be fetched as soon as any is requested.
409 bool ShouldFetchAllCookiesWhenFetchingAnyCookie();
[email protected]63ee33bd2012-03-15 09:29:58410
411 // Stores cookies loaded from the backing store and invokes any deferred
412 // calls. |beginning_time| should be the moment PersistentCookieStore::Load
413 // was invoked and is used for reporting histogram_time_blocked_on_load_.
414 // See PersistentCookieStore::Load for details on the contents of cookies.
415 void OnLoaded(base::TimeTicks beginning_time,
avie7cd11a2016-10-11 02:00:35416 std::vector<std::unique_ptr<CanonicalCookie>> cookies);
[email protected]63ee33bd2012-03-15 09:29:58417
418 // Stores cookies loaded from the backing store and invokes the deferred
419 // task(s) pending loading of cookies associated with the domain key
420 // (eTLD+1). Called when all cookies for the domain key(eTLD+1) have been
421 // loaded from DB. See PersistentCookieStore::Load for details on the contents
422 // of cookies.
mkwstbe84af312015-02-20 08:52:45423 void OnKeyLoaded(const std::string& key,
avie7cd11a2016-10-11 02:00:35424 std::vector<std::unique_ptr<CanonicalCookie>> cookies);
[email protected]63ee33bd2012-03-15 09:29:58425
426 // Stores the loaded cookies.
avie7cd11a2016-10-11 02:00:35427 void StoreLoadedCookies(
428 std::vector<std::unique_ptr<CanonicalCookie>> cookies);
[email protected]63ee33bd2012-03-15 09:29:58429
430 // Invokes deferred calls.
431 void InvokeQueue();
432
433 // Checks that |cookies_| matches our invariants, and tries to repair any
434 // inconsistencies. (In other words, it does not have duplicate cookies).
435 void EnsureCookiesMapIsValid();
436
437 // Checks for any duplicate cookies for CookieMap key |key| which lie between
438 // |begin| and |end|. If any are found, all but the most recent are deleted.
ellyjonescabf57422015-08-21 18:44:51439 void TrimDuplicateCookiesForKey(const std::string& key,
440 CookieMap::iterator begin,
441 CookieMap::iterator end);
[email protected]63ee33bd2012-03-15 09:29:58442
443 void SetDefaultCookieableSchemes();
444
445 void FindCookiesForHostAndDomain(const GURL& url,
446 const CookieOptions& options,
[email protected]63ee33bd2012-03-15 09:29:58447 std::vector<CanonicalCookie*>* cookies);
448
449 void FindCookiesForKey(const std::string& key,
450 const GURL& url,
451 const CookieOptions& options,
452 const base::Time& current,
[email protected]63ee33bd2012-03-15 09:29:58453 std::vector<CanonicalCookie*>* cookies);
454
455 // Delete any cookies that are equivalent to |ecc| (same path, domain, etc).
rdsmith2709eee2017-06-20 22:43:27456 // |source_secure| indicates if the source may override existing secure
457 // cookies.
Mike Westc4a777b2017-10-06 14:04:20458 //
[email protected]63ee33bd2012-03-15 09:29:58459 // If |skip_httponly| is true, httponly cookies will not be deleted. The
jww601411a2015-11-20 19:46:57460 // return value will be true if |skip_httponly| skipped an httponly cookie or
jwwa26e439d2017-01-27 18:17:27461 // the cookie to delete was Secure and the scheme of |ecc| is insecure. |key|
462 // is the key to find the cookie in cookies_; see the comment before the
463 // CookieMap typedef for details.
Mike Westc4a777b2017-10-06 14:04:20464 //
465 // If a cookie is deleted, and its value matches |ecc|'s value, then
466 // |creation_date_to_inherit| will be set to that cookie's creation date.
467 //
[email protected]63ee33bd2012-03-15 09:29:58468 // NOTE: There should never be more than a single matching equivalent cookie.
469 bool DeleteAnyEquivalentCookie(const std::string& key,
470 const CanonicalCookie& ecc,
rdsmith2709eee2017-06-20 22:43:27471 bool source_secure,
[email protected]63ee33bd2012-03-15 09:29:58472 bool skip_httponly,
Mike Westc4a777b2017-10-06 14:04:20473 bool already_expired,
474 base::Time* creation_date_to_inherit);
[email protected]63ee33bd2012-03-15 09:29:58475
avie7cd11a2016-10-11 02:00:35476 // Inserts |cc| into cookies_. Returns an iterator that points to the inserted
[email protected]6210ce52013-09-20 03:33:14477 // cookie in cookies_. Guarantee: all iterators to cookies_ remain valid.
478 CookieMap::iterator InternalInsertCookie(const std::string& key,
avie7cd11a2016-10-11 02:00:35479 std::unique_ptr<CanonicalCookie> cc,
[email protected]6210ce52013-09-20 03:33:14480 bool sync_to_store);
[email protected]63ee33bd2012-03-15 09:29:58481
rdsmith2709eee2017-06-20 22:43:27482 // Sets all cookies from |list| after deleting any equivalent cookie.
483 // For data gathering purposes, this routine is treated as if it is
484 // restoring saved cookies; some statistics are not gathered in this case.
rdsmithe5c701d2017-07-12 21:50:00485 void SetAllCookies(CookieList list, SetCookiesCallback callback);
drogerd5d1278c2015-03-17 19:21:51486
[email protected]63ee33bd2012-03-15 09:29:58487 void InternalUpdateCookieAccessTime(CanonicalCookie* cc,
488 const base::Time& current_time);
489
490 // |deletion_cause| argument is used for collecting statistics and choosing
Victor Costan14f47c12018-03-01 08:02:24491 // the correct CookieChangeCause for OnCookieChange notifications. Guarantee:
492 // All iterators to cookies_, except for the deleted entry, remain valid.
mkwstbe84af312015-02-20 08:52:45493 void InternalDeleteCookie(CookieMap::iterator it,
494 bool sync_to_store,
[email protected]63ee33bd2012-03-15 09:29:58495 DeletionCause deletion_cause);
496
497 // If the number of cookies for CookieMap key |key|, or globally, are
498 // over the preset maximums above, garbage collect, first for the host and
499 // then globally. See comments above garbage collection threshold
500 // constants for details.
501 //
502 // Returns the number of cookies deleted (useful for debugging).
jwwa26e439d2017-01-27 18:17:27503 size_t GarbageCollect(const base::Time& current, const std::string& key);
[email protected]63ee33bd2012-03-15 09:29:58504
mkwste079ac412016-03-11 09:04:06505 // Helper for GarbageCollect(). Deletes up to |purge_goal| cookies with a
506 // priority less than or equal to |priority| from |cookies|, while ensuring
507 // that at least the |to_protect| most-recent cookies are retained.
jwwc00ac712016-05-05 22:21:44508 // |protected_secure_cookies| specifies whether or not secure cookies should
509 // be protected from deletion.
mkwste079ac412016-03-11 09:04:06510 //
511 // |cookies| must be sorted from least-recent to most-recent.
512 //
mkwste079ac412016-03-11 09:04:06513 // Returns the number of cookies deleted.
514 size_t PurgeLeastRecentMatches(CookieItVector* cookies,
515 CookiePriority priority,
516 size_t to_protect,
jwwc00ac712016-05-05 22:21:44517 size_t purge_goal,
518 bool protect_secure_cookies);
mkwste079ac412016-03-11 09:04:06519
jww82d99c12015-11-25 18:39:53520 // Helper for GarbageCollect(); can be called directly as well. Deletes all
521 // expired cookies in |itpair|. If |cookie_its| is non-NULL, all the
522 // non-expired cookies from |itpair| are appended to |cookie_its|.
[email protected]63ee33bd2012-03-15 09:29:58523 //
524 // Returns the number of cookies deleted.
jww82d99c12015-11-25 18:39:53525 size_t GarbageCollectExpired(const base::Time& current,
526 const CookieMapItPair& itpair,
527 CookieItVector* cookie_its);
528
[email protected]8ad5d462013-05-02 08:45:26529 // Helper for GarbageCollect(). Deletes all cookies in the range specified by
530 // [|it_begin|, |it_end|). Returns the number of cookies deleted.
jww82d99c12015-11-25 18:39:53531 size_t GarbageCollectDeleteRange(const base::Time& current,
532 DeletionCause cause,
533 CookieItVector::iterator cookie_its_begin,
534 CookieItVector::iterator cookie_its_end);
535
536 // Helper for GarbageCollect(). Deletes cookies in |cookie_its| from least to
537 // most recently used, but only before |safe_date|. Also will stop deleting
538 // when the number of remaining cookies hits |purge_goal|.
mmenkef4721d992017-06-07 17:13:59539 //
540 // Sets |earliest_time| to be the earliest last access time of a cookie that
541 // was not deleted, or base::Time() if no such cookie exists.
jww82d99c12015-11-25 18:39:53542 size_t GarbageCollectLeastRecentlyAccessed(const base::Time& current,
543 const base::Time& safe_date,
544 size_t purge_goal,
mmenkef4721d992017-06-07 17:13:59545 CookieItVector cookie_its,
546 base::Time* earliest_time);
[email protected]63ee33bd2012-03-15 09:29:58547
davidben879199c2015-03-06 00:55:04548 // Find the key (for lookup in cookies_) based on the given domain.
549 // See comment on keys before the CookieMap typedef.
550 std::string GetKey(const std::string& domain) const;
551
[email protected]63ee33bd2012-03-15 09:29:58552 bool HasCookieableScheme(const GURL& url);
553
554 // Statistics support
555
556 // This function should be called repeatedly, and will record
557 // statistics if a sufficient time period has passed.
558 void RecordPeriodicStats(const base::Time& current_time);
559
560 // Initialize the above variables; should only be called from
561 // the constructor.
562 void InitializeHistograms();
563
564 // The resolution of our time isn't enough, so we do something
565 // ugly and increment when we've seen the same time twice.
566 base::Time CurrentTime();
567
rdsmithe5c701d2017-07-12 21:50:00568 // Runs the callback if, or defers the callback until, the full cookie
569 // database is loaded.
570 void DoCookieCallback(base::OnceClosure callback);
[email protected]63ee33bd2012-03-15 09:29:58571
rdsmithe5c701d2017-07-12 21:50:00572 // Runs the callback if, or defers the callback until, the cookies for the
573 // given URL are loaded.
574 void DoCookieCallbackForURL(base::OnceClosure callback, const GURL& url);
[email protected]63ee33bd2012-03-15 09:29:58575
576 // Histogram variables; see CookieMonster::InitializeHistograms() in
577 // cookie_monster.cc for details.
[email protected]de415552013-01-23 04:12:17578 base::HistogramBase* histogram_expiration_duration_minutes_;
[email protected]de415552013-01-23 04:12:17579 base::HistogramBase* histogram_count_;
mkwstc1aa4cc2015-04-03 19:57:45580 base::HistogramBase* histogram_cookie_type_;
estark7feb65c2b2015-08-21 23:38:20581 base::HistogramBase* histogram_cookie_source_scheme_;
jww31e32632015-12-16 23:38:34582 base::HistogramBase* histogram_cookie_delete_equivalent_;
[email protected]de415552013-01-23 04:12:17583 base::HistogramBase* histogram_time_blocked_on_load_;
[email protected]63ee33bd2012-03-15 09:29:58584
585 CookieMap cookies_;
586
Victor Costan14f47c12018-03-01 08:02:24587 CookieMonsterChangeDispatcher change_dispatcher_;
588
erikchen1dd72a72015-05-06 20:45:05589 // Indicates whether the cookie store has been initialized.
[email protected]63ee33bd2012-03-15 09:29:58590 bool initialized_;
591
erikchen1dd72a72015-05-06 20:45:05592 // Indicates whether the cookie store has started fetching all cookies.
593 bool started_fetching_all_cookies_;
594 // Indicates whether the cookie store has finished fetching all cookies.
595 bool finished_fetching_all_cookies_;
[email protected]63ee33bd2012-03-15 09:29:58596
597 // List of domain keys that have been loaded from the DB.
598 std::set<std::string> keys_loaded_;
599
600 // Map of domain keys to their associated task queues. These tasks are blocked
601 // until all cookies for the associated domain key eTLD+1 are loaded from the
602 // backend store.
Brett Wilsonc6a0c822017-09-12 00:04:29603 std::map<std::string, base::circular_deque<base::OnceClosure>>
604 tasks_pending_for_key_;
[email protected]63ee33bd2012-03-15 09:29:58605
606 // Queues tasks that are blocked until all cookies are loaded from the backend
607 // store.
Brett Wilsonc6a0c822017-09-12 00:04:29608 base::circular_deque<base::OnceClosure> tasks_pending_;
mmenkef49fca0e2016-03-08 12:46:24609
610 // Once a global cookie task has been seen, all per-key tasks must be put in
611 // |tasks_pending_| instead of |tasks_pending_for_key_| to ensure a reasonable
rdsmithe5c701d2017-07-12 21:50:00612 // view of the cookie store. This is more to ensure fancy cookie export/import
mmenkef49fca0e2016-03-08 12:46:24613 // code has a consistent view of the CookieStore, rather than out of concern
614 // for typical use.
615 bool seen_global_task_;
[email protected]63ee33bd2012-03-15 09:29:58616
617 scoped_refptr<PersistentCookieStore> store_;
618
619 base::Time last_time_seen_;
620
621 // Minimum delay after updating a cookie's LastAccessDate before we will
622 // update it again.
623 const base::TimeDelta last_access_threshold_;
624
625 // Approximate date of access time of least recently accessed cookie
626 // in |cookies_|. Note that this is not guaranteed to be accurate, only a)
627 // to be before or equal to the actual time, and b) to be accurate
mmenkef4721d992017-06-07 17:13:59628 // immediately after a garbage collection that scans through all the cookies
629 // (When garbage collection does not scan through all cookies, it may not be
630 // updated). This value is used to determine whether global garbage collection
631 // might find cookies to purge. Note: The default Time() constructor will
632 // create a value that compares earlier than any other time value, which is
633 // wanted. Thus this value is not initialized.
[email protected]63ee33bd2012-03-15 09:29:58634 base::Time earliest_access_time_;
635
636 // During loading, holds the set of all loaded cookie creation times. Used to
637 // avoid ever letting cookies with duplicate creation times into the store;
638 // that way we don't have to worry about what sections of code are safe
639 // to call while it's in that state.
Avi Drissman13fc8932015-12-20 04:40:46640 std::set<int64_t> creation_times_;
[email protected]63ee33bd2012-03-15 09:29:58641
642 std::vector<std::string> cookieable_schemes_;
643
nharper2b0ad9a2017-05-22 18:33:45644 ChannelIDService* channel_id_service_;
[email protected]63ee33bd2012-03-15 09:29:58645
[email protected]63ee33bd2012-03-15 09:29:58646 base::Time last_statistic_record_time_;
647
[email protected]63ee33bd2012-03-15 09:29:58648 bool persist_session_cookies_;
649
mmenkebe0910d2016-03-01 19:09:09650 base::ThreadChecker thread_checker_;
651
652 base::WeakPtrFactory<CookieMonster> weak_ptr_factory_;
653
[email protected]63ee33bd2012-03-15 09:29:58654 DISALLOW_COPY_AND_ASSIGN(CookieMonster);
655};
656
[email protected]63ee33bd2012-03-15 09:29:58657typedef base::RefCountedThreadSafe<CookieMonster::PersistentCookieStore>
658 RefcountedPersistentCookieStore;
659
[email protected]c1b6e102013-04-10 20:54:49660class NET_EXPORT CookieMonster::PersistentCookieStore
[email protected]63ee33bd2012-03-15 09:29:58661 : public RefcountedPersistentCookieStore {
662 public:
avie7cd11a2016-10-11 02:00:35663 typedef base::Callback<void(std::vector<std::unique_ptr<CanonicalCookie>>)>
[email protected]5b9bc352012-07-18 13:13:34664 LoadedCallback;
[email protected]63ee33bd2012-03-15 09:29:58665
666 // Initializes the store and retrieves the existing cookies. This will be
667 // called only once at startup. The callback will return all the cookies
668 // that are not yet returned to CookieMonster by previous priority loads.
mmenkebe0910d2016-03-01 19:09:09669 //
670 // |loaded_callback| may not be NULL.
Thomas Anderson1a03bbe2018-03-02 19:05:47671 virtual void Load(const LoadedCallback& loaded_callback) = 0;
[email protected]63ee33bd2012-03-15 09:29:58672
673 // Does a priority load of all cookies for the domain key (eTLD+1). The
674 // callback will return all the cookies that are not yet returned by previous
675 // loads, which includes cookies for the requested domain key if they are not
676 // already returned, plus all cookies that are chain-loaded and not yet
677 // returned to CookieMonster.
mmenkebe0910d2016-03-01 19:09:09678 //
679 // |loaded_callback| may not be NULL.
[email protected]63ee33bd2012-03-15 09:29:58680 virtual void LoadCookiesForKey(const std::string& key,
[email protected]dedec0b2013-02-28 04:50:10681 const LoadedCallback& loaded_callback) = 0;
[email protected]63ee33bd2012-03-15 09:29:58682
683 virtual void AddCookie(const CanonicalCookie& cc) = 0;
684 virtual void UpdateCookieAccessTime(const CanonicalCookie& cc) = 0;
685 virtual void DeleteCookie(const CanonicalCookie& cc) = 0;
686
[email protected]bf510ed2012-06-05 08:31:43687 // Instructs the store to not discard session only cookies on shutdown.
688 virtual void SetForceKeepSessionState() = 0;
[email protected]63ee33bd2012-03-15 09:29:58689
Nick Harper14e23332017-07-28 00:27:23690 // Sets a callback that will be run before the store flushes. If |callback|
691 // performs any async operations, the store will not wait for those to finish
692 // before flushing.
693 virtual void SetBeforeFlushCallback(base::RepeatingClosure callback) = 0;
694
mmenkebe0910d2016-03-01 19:09:09695 // Flushes the store and posts |callback| when complete. |callback| may be
696 // NULL.
rdsmith7ac81712017-06-22 17:09:54697 virtual void Flush(base::OnceClosure callback) = 0;
[email protected]63ee33bd2012-03-15 09:29:58698
699 protected:
700 PersistentCookieStore() {}
[email protected]a9813302012-04-28 09:29:28701 virtual ~PersistentCookieStore() {}
[email protected]63ee33bd2012-03-15 09:29:58702
703 private:
[email protected]a9813302012-04-28 09:29:28704 friend class base::RefCountedThreadSafe<PersistentCookieStore>;
[email protected]63ee33bd2012-03-15 09:29:58705 DISALLOW_COPY_AND_ASSIGN(PersistentCookieStore);
706};
707
[email protected]63ee33bd2012-03-15 09:29:58708} // namespace net
709
710#endif // NET_COOKIES_COOKIE_MONSTER_H_