blob: a251ed7403cc486556b07cfc126389993619e5c6 [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"
Maks Orlovich323efaf2018-03-06 02:56:3926#include "base/strings/string_piece.h"
mmenkebe0910d2016-03-01 19:09:0927#include "base/threading/thread_checker.h"
[email protected]9da992db2013-06-28 05:40:4728#include "base/time/time.h"
[email protected]565c3f42012-08-14 14:22:5829#include "net/base/net_export.h"
[email protected]8da4b1812012-07-25 13:54:3830#include "net/cookies/canonical_cookie.h"
[email protected]ab2d75c82013-04-19 18:39:0431#include "net/cookies/cookie_constants.h"
Victor Costan14f47c12018-03-01 08:02:2432#include "net/cookies/cookie_monster_change_dispatcher.h"
[email protected]63ee33bd2012-03-15 09:29:5833#include "net/cookies/cookie_store.h"
Helen Licd0fab862018-08-13 16:07:5334#include "net/log/net_log_with_source.h"
ellyjones399e35a22014-10-27 11:09:5635#include "url/gurl.h"
[email protected]63ee33bd2012-03-15 09:29:5836
37namespace base {
[email protected]de415552013-01-23 04:12:1738class HistogramBase;
[email protected]63ee33bd2012-03-15 09:29:5839} // namespace base
40
41namespace net {
42
nharper2b0ad9a2017-05-22 18:33:4543class ChannelIDService;
Victor Costan14f47c12018-03-01 08:02:2444class CookieChangeDispatcher;
[email protected]63ee33bd2012-03-15 09:29:5845
46// The cookie monster is the system for storing and retrieving cookies. It has
47// an in-memory list of all cookies, and synchronizes non-session cookies to an
48// optional permanent storage that implements the PersistentCookieStore
49// interface.
50//
mmenke96f3bab2016-01-22 17:34:0251// Tasks may be deferred if all affected cookies are not yet loaded from the
52// backing store. Otherwise, callbacks may be invoked immediately.
[email protected]63ee33bd2012-03-15 09:29:5853//
54// A cookie task is either pending loading of the entire cookie store, or
Maks Orlovich323efaf2018-03-06 02:56:3955// loading of cookies for a specific domain key (GetKey(), roughly eTLD+1). In
56// the former case, the cookie callback will be queued in tasks_pending_ while
57// PersistentCookieStore chain loads the cookie store on DB thread. In the
58// latter case, the cookie callback will be queued in tasks_pending_for_key_
59// while PermanentCookieStore loads cookies for the specified domain key on DB
60// thread.
[email protected]63ee33bd2012-03-15 09:29:5861class 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.
Pritam8354cf702018-03-10 08:55:41135 explicit CookieMonster(scoped_refptr<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.
Pritam8354cf702018-03-10 08:55:41140 CookieMonster(scoped_refptr<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.
Pritam8354cf702018-03-10 08:55:41144 CookieMonster(scoped_refptr<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;
Chris Mumfordd8ed9f82018-05-01 15:43:13175 void DeleteAllCreatedInTimeRangeAsync(
176 const CookieDeletionInfo::TimeRange& creation_range,
177 DeleteCallback callback) override;
Chris Mumford800caa62018-04-20 19:34:44178 void DeleteAllMatchingInfoAsync(CookieDeletionInfo delete_info,
179 DeleteCallback callback) override;
rdsmith7ac81712017-06-22 17:09:54180 void DeleteSessionCookiesAsync(DeleteCallback) override;
181 void FlushStore(base::OnceClosure callback) override;
mmenkeded79da2016-02-06 08:28:51182 void SetForceKeepSessionState() override;
Victor Costan14f47c12018-03-01 08:02:24183 CookieChangeDispatcher& GetChangeDispatcher() override;
[email protected]264807b2012-04-25 14:49:37184
mmenke74bcbd52016-01-21 17:17:56185 // Resets the list of cookieable schemes to the supplied schemes. Does
186 // nothing if called after first use of the instance (i.e. after the
187 // instance initialization process).
mmenke18dd8ba2016-02-01 18:42:10188 void SetCookieableSchemes(const std::vector<std::string>& schemes);
mmenke74bcbd52016-01-21 17:17:56189
[email protected]63ee33bd2012-03-15 09:29:58190 // Enables writing session cookies into the cookie database. If this this
191 // method is called, it must be called before first use of the instance
192 // (i.e. as part of the instance initialization process).
193 void SetPersistSessionCookies(bool persist_session_cookies);
194
[email protected]97a3b6e2012-06-12 01:53:56195 // Determines if the scheme of the URL is a scheme that cookies will be
196 // stored for.
197 bool IsCookieableScheme(const std::string& scheme);
198
[email protected]63ee33bd2012-03-15 09:29:58199 // The default list of schemes the cookie monster can handle.
[email protected]5edff3c52014-06-23 20:27:48200 static const char* const kDefaultCookieableSchemes[];
[email protected]63ee33bd2012-03-15 09:29:58201 static const int kDefaultCookieableSchemesCount;
202
nharper5babb5e62016-03-09 18:58:07203 bool IsEphemeral() override;
204
Maks Orlovich5cf437b02018-03-27 04:40:42205 void DumpMemoryStats(base::trace_event::ProcessMemoryDump* pmd,
206 const std::string& parent_absolute_name) const override;
207
Maks Orlovich323efaf2018-03-06 02:56:39208 // Find a key based on the given domain, which will be used to find all
209 // cookies potentially relevant to it. This is used for lookup in cookies_ as
210 // well as for PersistentCookieStore::LoadCookiesForKey. See comment on keys
211 // before the CookieMap typedef.
212 static std::string GetKey(base::StringPiece domain);
213
[email protected]63ee33bd2012-03-15 09:29:58214 private:
Pritam8354cf702018-03-10 08:55:41215 CookieMonster(scoped_refptr<PersistentCookieStore> store,
nharper2b0ad9a2017-05-22 18:33:45216 ChannelIDService* channel_id_service,
217 base::TimeDelta last_access_threshold);
218
avie7cd11a2016-10-11 02:00:35219 // For garbage collection constants.
[email protected]63ee33bd2012-03-15 09:29:58220 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestHostGarbageCollection);
[email protected]63ee33bd2012-03-15 09:29:58221 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, GarbageCollectionTriggers);
mmenkef4721d992017-06-07 17:13:59222 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest,
223 GarbageCollectWithSecureCookiesOnly);
[email protected]63ee33bd2012-03-15 09:29:58224 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestGCTimes);
225
226 // For validation of key values.
227 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestDomainTree);
228 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestImport);
229 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, GetKey);
230 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestGetKey);
231
232 // For FindCookiesForKey.
233 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, ShortLivedSessionCookies);
234
estark7feb65c2b2015-08-21 23:38:20235 // For CookieSource histogram enum.
236 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, CookieSourceHistogram);
237
jww31e32632015-12-16 23:38:34238 // For kSafeFromGlobalPurgeDays in CookieStore.
jwwa26e439d2017-01-27 18:17:27239 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, EvictSecureCookies);
jww82d99c12015-11-25 18:39:53240
jww31e32632015-12-16 23:38:34241 // For CookieDeleteEquivalent histogram enum.
242 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest,
243 CookieDeleteEquivalentHistogramTest);
jww31e32632015-12-16 23:38:34244
[email protected]63ee33bd2012-03-15 09:29:58245 // Internal reasons for deletion, used to populate informative histograms
246 // and to provide a public cause for onCookieChange notifications.
247 //
248 // If you add or remove causes from this list, please be sure to also update
Victor Costan14f47c12018-03-01 08:02:24249 // the CookieChangeCause mapping inside ChangeCauseMapping. New items (if
250 // necessary) should be added at the end of the list, just before
Nick Harper7a6683a2018-01-30 20:42:52251 // DELETE_COOKIE_LAST_ENTRY.
[email protected]63ee33bd2012-03-15 09:29:58252 enum DeletionCause {
253 DELETE_COOKIE_EXPLICIT = 0,
mkwstaa07ee82016-03-11 15:32:14254 DELETE_COOKIE_OVERWRITE = 1,
255 DELETE_COOKIE_EXPIRED = 2,
256 DELETE_COOKIE_EVICTED = 3,
257 DELETE_COOKIE_DUPLICATE_IN_BACKING_STORE = 4,
258 DELETE_COOKIE_DONT_RECORD = 5, // For final cleanup after flush to store.
[email protected]63ee33bd2012-03-15 09:29:58259
mkwstaa07ee82016-03-11 15:32:14260 // Cookies evicted during domain-level garbage collection.
261 DELETE_COOKIE_EVICTED_DOMAIN = 6,
[email protected]63ee33bd2012-03-15 09:29:58262
mkwstaa07ee82016-03-11 15:32:14263 // Cookies evicted during global garbage collection (which takes place after
264 // domain-level garbage collection fails to bring the cookie store under
265 // the overall quota.
266 DELETE_COOKIE_EVICTED_GLOBAL = 7,
267
268 // #8 was DELETE_COOKIE_EVICTED_DOMAIN_PRE_SAFE
269 // #9 was DELETE_COOKIE_EVICTED_DOMAIN_POST_SAFE
[email protected]63ee33bd2012-03-15 09:29:58270
271 // A common idiom is to remove a cookie by overwriting it with an
272 // already-expired expiration date. This captures that case.
mkwstaa07ee82016-03-11 15:32:14273 DELETE_COOKIE_EXPIRED_OVERWRITE = 10,
[email protected]63ee33bd2012-03-15 09:29:58274
[email protected]6210ce52013-09-20 03:33:14275 // Cookies are not allowed to contain control characters in the name or
276 // value. However, we used to allow them, so we are now evicting any such
277 // cookies as we load them. See http://crbug.com/238041.
mkwstaa07ee82016-03-11 15:32:14278 DELETE_COOKIE_CONTROL_CHAR = 11,
[email protected]6210ce52013-09-20 03:33:14279
jww82d99c12015-11-25 18:39:53280 // When strict secure cookies is enabled, non-secure cookies are evicted
281 // right after expired cookies.
mkwstaa07ee82016-03-11 15:32:14282 DELETE_COOKIE_NON_SECURE = 12,
jww82d99c12015-11-25 18:39:53283
Nick Harper7a6683a2018-01-30 20:42:52284 DELETE_COOKIE_LAST_ENTRY = 13
[email protected]63ee33bd2012-03-15 09:29:58285 };
286
mkwstc1aa4cc2015-04-03 19:57:45287 // This enum is used to generate a histogramed bitmask measureing the types
288 // of stored cookies. Please do not reorder the list when adding new entries.
289 // New items MUST be added at the end of the list, just before
290 // COOKIE_TYPE_LAST_ENTRY;
291 enum CookieType {
mkwst46549412016-02-01 10:05:37292 COOKIE_TYPE_SAME_SITE = 0,
mkwstc1aa4cc2015-04-03 19:57:45293 COOKIE_TYPE_HTTPONLY,
294 COOKIE_TYPE_SECURE,
295 COOKIE_TYPE_LAST_ENTRY
296 };
297
estark7feb65c2b2015-08-21 23:38:20298 // Used to populate a histogram containing information about the
299 // sources of Secure and non-Secure cookies: that is, whether such
300 // cookies are set by origins with cryptographic or non-cryptographic
301 // schemes. Please do not reorder the list when adding new
302 // entries. New items MUST be added at the end of the list, just
303 // before COOKIE_SOURCE_LAST_ENTRY.
304 //
305 // COOKIE_SOURCE_(NON)SECURE_COOKIE_(NON)CRYPTOGRAPHIC_SCHEME means
306 // that a cookie was set or overwritten from a URL with the given type
307 // of scheme. This enum should not be used when cookies are *cleared*,
308 // because its purpose is to understand if Chrome can deprecate the
309 // ability of HTTP urls to set/overwrite Secure cookies.
310 enum CookieSource {
311 COOKIE_SOURCE_SECURE_COOKIE_CRYPTOGRAPHIC_SCHEME = 0,
312 COOKIE_SOURCE_SECURE_COOKIE_NONCRYPTOGRAPHIC_SCHEME,
313 COOKIE_SOURCE_NONSECURE_COOKIE_CRYPTOGRAPHIC_SCHEME,
314 COOKIE_SOURCE_NONSECURE_COOKIE_NONCRYPTOGRAPHIC_SCHEME,
315 COOKIE_SOURCE_LAST_ENTRY
316 };
317
jww31e32632015-12-16 23:38:34318 // Used to populate a histogram for cookie setting in the "delete equivalent"
Mike West86149882017-07-28 10:41:49319 // step. Measures total attempts to delete an equivalent cookie, and
320 // categorizes the outcome.
jww31e32632015-12-16 23:38:34321 //
Mike West86149882017-07-28 10:41:49322 // * COOKIE_DELETE_EQUIVALENT_ATTEMPT is incremented each time a cookie is
323 // set, causing the equivalent deletion algorithm to execute.
324 //
325 // * COOKIE_DELETE_EQUIVALENT_SKIPPING_SECURE is incremented when a non-secure
326 // cookie is ignored because an equivalent, but secure, cookie already
327 // exists.
328 //
329 // * COOKIE_DELETE_EQUIVALENT_WOULD_HAVE_DELETED is incremented when a cookie
330 // is skipped due to `secure` rules (e.g. whenever
331 // COOKIE_DELETE_EQUIVALENT_SKIPPING_SECURE is incremented), but would have
332 // caused a deletion without those rules.
333 //
334 // TODO(mkwst): Now that we've shipped strict secure cookie checks, we don't
335 // need this value anymore.
336 //
337 // * COOKIE_DELETE_EQUIVALENT_FOUND is incremented each time an equivalent
338 // cookie is found (and deleted).
339 //
340 // * COOKIE_DELETE_EQUIVALENT_FOUND_WITH_SAME_VALUE is incremented each time
341 // an equivalent cookie that also shared the same value with the new cookie
342 // is found (and deleted).
343 //
344 // Please do not reorder or remove entries. New entries must be added to the
345 // end of the list, just before COOKIE_DELETE_EQUIVALENT_LAST_ENTRY.
jww31e32632015-12-16 23:38:34346 enum CookieDeleteEquivalent {
347 COOKIE_DELETE_EQUIVALENT_ATTEMPT = 0,
348 COOKIE_DELETE_EQUIVALENT_FOUND,
349 COOKIE_DELETE_EQUIVALENT_SKIPPING_SECURE,
350 COOKIE_DELETE_EQUIVALENT_WOULD_HAVE_DELETED,
Mike West86149882017-07-28 10:41:49351 COOKIE_DELETE_EQUIVALENT_FOUND_WITH_SAME_VALUE,
jww31e32632015-12-16 23:38:34352 COOKIE_DELETE_EQUIVALENT_LAST_ENTRY
353 };
354
[email protected]63ee33bd2012-03-15 09:29:58355 // The number of days since last access that cookies will not be subject
356 // to global garbage collection.
357 static const int kSafeFromGlobalPurgeDays;
358
359 // Record statistics every kRecordStatisticsIntervalSeconds of uptime.
360 static const int kRecordStatisticsIntervalSeconds = 10 * 60;
361
rdsmitha6ce4442017-06-21 17:11:05362 // Sets a canonical cookie, deletes equivalents and performs garbage
363 // collection. |source_secure| indicates if the cookie is being set
364 // from a secure source (e.g. a cryptographic scheme).
365 // |modify_http_only| indicates if this setting operation is allowed
366 // to affect http_only cookies.
rdsmithe5c701d2017-07-12 21:50:00367 void SetCanonicalCookie(std::unique_ptr<CanonicalCookie> cookie,
rdsmitha6ce4442017-06-21 17:11:05368 bool secure_source,
rdsmithe5c701d2017-07-12 21:50:00369 bool can_modify_httponly,
370 SetCookiesCallback callback);
rdsmitha6ce4442017-06-21 17:11:05371
rdsmithe5c701d2017-07-12 21:50:00372 void GetAllCookies(GetCookieListCallback callback);
[email protected]63ee33bd2012-03-15 09:29:58373
rdsmithe5c701d2017-07-12 21:50:00374 void GetCookieListWithOptions(const GURL& url,
375 const CookieOptions& options,
376 GetCookieListCallback callback);
[email protected]63ee33bd2012-03-15 09:29:58377
Chris Mumfordd8ed9f82018-05-01 15:43:13378 void DeleteAllCreatedInTimeRange(
379 const CookieDeletionInfo::TimeRange& creation_range,
380 DeleteCallback callback);
[email protected]63ee33bd2012-03-15 09:29:58381
Chris Mumfordd8ed9f82018-05-01 15:43:13382 void DeleteAllMatchingInfo(net::CookieDeletionInfo delete_info,
Chris Mumford800caa62018-04-20 19:34:44383 DeleteCallback callback);
[email protected]63ee33bd2012-03-15 09:29:58384
rdsmithe5c701d2017-07-12 21:50:00385 void SetCookieWithOptions(const GURL& url,
[email protected]63ee33bd2012-03-15 09:29:58386 const std::string& cookie_line,
rdsmithe5c701d2017-07-12 21:50:00387 const CookieOptions& options,
388 SetCookiesCallback callback);
[email protected]63ee33bd2012-03-15 09:29:58389
rdsmithe5c701d2017-07-12 21:50:00390 void DeleteCookie(const GURL& url,
391 const std::string& cookie_name,
392 base::OnceClosure callback);
[email protected]63ee33bd2012-03-15 09:29:58393
rdsmithe5c701d2017-07-12 21:50:00394 void DeleteCanonicalCookie(const CanonicalCookie& cookie,
395 DeleteCallback callback);
mmenke24379d52016-02-05 23:50:17396
rdsmithe5c701d2017-07-12 21:50:00397 void DeleteSessionCookies(DeleteCallback callback);
[email protected]264807b2012-04-25 14:49:37398
erikchen1dd72a72015-05-06 20:45:05399 // The first access to the cookie store initializes it. This method should be
400 // called before any access to the cookie store.
401 void MarkCookieStoreAsInitialized();
[email protected]63ee33bd2012-03-15 09:29:58402
erikchen1dd72a72015-05-06 20:45:05403 // Fetches all cookies if the backing store exists and they're not already
404 // being fetched.
erikchen1dd72a72015-05-06 20:45:05405 void FetchAllCookiesIfNecessary();
406
407 // Fetches all cookies from the backing store.
erikchen1dd72a72015-05-06 20:45:05408 void FetchAllCookies();
409
410 // Whether all cookies should be fetched as soon as any is requested.
411 bool ShouldFetchAllCookiesWhenFetchingAnyCookie();
[email protected]63ee33bd2012-03-15 09:29:58412
413 // Stores cookies loaded from the backing store and invokes any deferred
414 // calls. |beginning_time| should be the moment PersistentCookieStore::Load
415 // was invoked and is used for reporting histogram_time_blocked_on_load_.
416 // See PersistentCookieStore::Load for details on the contents of cookies.
417 void OnLoaded(base::TimeTicks beginning_time,
avie7cd11a2016-10-11 02:00:35418 std::vector<std::unique_ptr<CanonicalCookie>> cookies);
[email protected]63ee33bd2012-03-15 09:29:58419
420 // Stores cookies loaded from the backing store and invokes the deferred
421 // task(s) pending loading of cookies associated with the domain key
Maks Orlovich323efaf2018-03-06 02:56:39422 // (GetKey, roughly eTLD+1). Called when all cookies for the domain key have
423 // been loaded from DB. See PersistentCookieStore::Load for details on the
424 // contents of cookies.
mkwstbe84af312015-02-20 08:52:45425 void OnKeyLoaded(const std::string& key,
avie7cd11a2016-10-11 02:00:35426 std::vector<std::unique_ptr<CanonicalCookie>> cookies);
[email protected]63ee33bd2012-03-15 09:29:58427
428 // Stores the loaded cookies.
avie7cd11a2016-10-11 02:00:35429 void StoreLoadedCookies(
430 std::vector<std::unique_ptr<CanonicalCookie>> cookies);
[email protected]63ee33bd2012-03-15 09:29:58431
432 // Invokes deferred calls.
433 void InvokeQueue();
434
435 // Checks that |cookies_| matches our invariants, and tries to repair any
436 // inconsistencies. (In other words, it does not have duplicate cookies).
437 void EnsureCookiesMapIsValid();
438
439 // Checks for any duplicate cookies for CookieMap key |key| which lie between
440 // |begin| and |end|. If any are found, all but the most recent are deleted.
ellyjonescabf57422015-08-21 18:44:51441 void TrimDuplicateCookiesForKey(const std::string& key,
442 CookieMap::iterator begin,
443 CookieMap::iterator end);
[email protected]63ee33bd2012-03-15 09:29:58444
445 void SetDefaultCookieableSchemes();
446
447 void FindCookiesForHostAndDomain(const GURL& url,
448 const CookieOptions& options,
[email protected]63ee33bd2012-03-15 09:29:58449 std::vector<CanonicalCookie*>* cookies);
450
451 void FindCookiesForKey(const std::string& key,
452 const GURL& url,
453 const CookieOptions& options,
454 const base::Time& current,
[email protected]63ee33bd2012-03-15 09:29:58455 std::vector<CanonicalCookie*>* cookies);
456
457 // Delete any cookies that are equivalent to |ecc| (same path, domain, etc).
rdsmith2709eee2017-06-20 22:43:27458 // |source_secure| indicates if the source may override existing secure
459 // cookies.
Mike Westc4a777b2017-10-06 14:04:20460 //
[email protected]63ee33bd2012-03-15 09:29:58461 // If |skip_httponly| is true, httponly cookies will not be deleted. The
jww601411a2015-11-20 19:46:57462 // return value will be true if |skip_httponly| skipped an httponly cookie or
jwwa26e439d2017-01-27 18:17:27463 // the cookie to delete was Secure and the scheme of |ecc| is insecure. |key|
464 // is the key to find the cookie in cookies_; see the comment before the
465 // CookieMap typedef for details.
Mike Westc4a777b2017-10-06 14:04:20466 //
467 // If a cookie is deleted, and its value matches |ecc|'s value, then
468 // |creation_date_to_inherit| will be set to that cookie's creation date.
469 //
[email protected]63ee33bd2012-03-15 09:29:58470 // NOTE: There should never be more than a single matching equivalent cookie.
471 bool DeleteAnyEquivalentCookie(const std::string& key,
472 const CanonicalCookie& ecc,
rdsmith2709eee2017-06-20 22:43:27473 bool source_secure,
[email protected]63ee33bd2012-03-15 09:29:58474 bool skip_httponly,
Mike Westc4a777b2017-10-06 14:04:20475 bool already_expired,
476 base::Time* creation_date_to_inherit);
[email protected]63ee33bd2012-03-15 09:29:58477
avie7cd11a2016-10-11 02:00:35478 // Inserts |cc| into cookies_. Returns an iterator that points to the inserted
[email protected]6210ce52013-09-20 03:33:14479 // cookie in cookies_. Guarantee: all iterators to cookies_ remain valid.
480 CookieMap::iterator InternalInsertCookie(const std::string& key,
avie7cd11a2016-10-11 02:00:35481 std::unique_ptr<CanonicalCookie> cc,
[email protected]6210ce52013-09-20 03:33:14482 bool sync_to_store);
[email protected]63ee33bd2012-03-15 09:29:58483
rdsmith2709eee2017-06-20 22:43:27484 // Sets all cookies from |list| after deleting any equivalent cookie.
485 // For data gathering purposes, this routine is treated as if it is
486 // restoring saved cookies; some statistics are not gathered in this case.
rdsmithe5c701d2017-07-12 21:50:00487 void SetAllCookies(CookieList list, SetCookiesCallback callback);
drogerd5d1278c2015-03-17 19:21:51488
[email protected]63ee33bd2012-03-15 09:29:58489 void InternalUpdateCookieAccessTime(CanonicalCookie* cc,
490 const base::Time& current_time);
491
492 // |deletion_cause| argument is used for collecting statistics and choosing
Victor Costan14f47c12018-03-01 08:02:24493 // the correct CookieChangeCause for OnCookieChange notifications. Guarantee:
494 // All iterators to cookies_, except for the deleted entry, remain valid.
mkwstbe84af312015-02-20 08:52:45495 void InternalDeleteCookie(CookieMap::iterator it,
496 bool sync_to_store,
[email protected]63ee33bd2012-03-15 09:29:58497 DeletionCause deletion_cause);
498
499 // If the number of cookies for CookieMap key |key|, or globally, are
500 // over the preset maximums above, garbage collect, first for the host and
501 // then globally. See comments above garbage collection threshold
502 // constants for details.
503 //
504 // Returns the number of cookies deleted (useful for debugging).
jwwa26e439d2017-01-27 18:17:27505 size_t GarbageCollect(const base::Time& current, const std::string& key);
[email protected]63ee33bd2012-03-15 09:29:58506
mkwste079ac412016-03-11 09:04:06507 // Helper for GarbageCollect(). Deletes up to |purge_goal| cookies with a
508 // priority less than or equal to |priority| from |cookies|, while ensuring
509 // that at least the |to_protect| most-recent cookies are retained.
jwwc00ac712016-05-05 22:21:44510 // |protected_secure_cookies| specifies whether or not secure cookies should
511 // be protected from deletion.
mkwste079ac412016-03-11 09:04:06512 //
513 // |cookies| must be sorted from least-recent to most-recent.
514 //
mkwste079ac412016-03-11 09:04:06515 // Returns the number of cookies deleted.
516 size_t PurgeLeastRecentMatches(CookieItVector* cookies,
517 CookiePriority priority,
518 size_t to_protect,
jwwc00ac712016-05-05 22:21:44519 size_t purge_goal,
520 bool protect_secure_cookies);
mkwste079ac412016-03-11 09:04:06521
jww82d99c12015-11-25 18:39:53522 // Helper for GarbageCollect(); can be called directly as well. Deletes all
523 // expired cookies in |itpair|. If |cookie_its| is non-NULL, all the
524 // non-expired cookies from |itpair| are appended to |cookie_its|.
[email protected]63ee33bd2012-03-15 09:29:58525 //
526 // Returns the number of cookies deleted.
jww82d99c12015-11-25 18:39:53527 size_t GarbageCollectExpired(const base::Time& current,
528 const CookieMapItPair& itpair,
529 CookieItVector* cookie_its);
530
[email protected]8ad5d462013-05-02 08:45:26531 // Helper for GarbageCollect(). Deletes all cookies in the range specified by
532 // [|it_begin|, |it_end|). Returns the number of cookies deleted.
jww82d99c12015-11-25 18:39:53533 size_t GarbageCollectDeleteRange(const base::Time& current,
534 DeletionCause cause,
535 CookieItVector::iterator cookie_its_begin,
536 CookieItVector::iterator cookie_its_end);
537
538 // Helper for GarbageCollect(). Deletes cookies in |cookie_its| from least to
539 // most recently used, but only before |safe_date|. Also will stop deleting
540 // when the number of remaining cookies hits |purge_goal|.
mmenkef4721d992017-06-07 17:13:59541 //
542 // Sets |earliest_time| to be the earliest last access time of a cookie that
543 // was not deleted, or base::Time() if no such cookie exists.
jww82d99c12015-11-25 18:39:53544 size_t GarbageCollectLeastRecentlyAccessed(const base::Time& current,
545 const base::Time& safe_date,
546 size_t purge_goal,
mmenkef4721d992017-06-07 17:13:59547 CookieItVector cookie_its,
548 base::Time* earliest_time);
[email protected]63ee33bd2012-03-15 09:29:58549
[email protected]63ee33bd2012-03-15 09:29:58550 bool HasCookieableScheme(const GURL& url);
551
552 // Statistics support
553
554 // This function should be called repeatedly, and will record
555 // statistics if a sufficient time period has passed.
556 void RecordPeriodicStats(const base::Time& current_time);
557
558 // Initialize the above variables; should only be called from
559 // the constructor.
560 void InitializeHistograms();
561
562 // The resolution of our time isn't enough, so we do something
563 // ugly and increment when we've seen the same time twice.
564 base::Time CurrentTime();
565
Maks Orlovich323efaf2018-03-06 02:56:39566 // Defers the callback until the full coookie database has been loaded. If
567 // it's already been loaded, runs the callback synchronously.
rdsmithe5c701d2017-07-12 21:50:00568 void DoCookieCallback(base::OnceClosure callback);
[email protected]63ee33bd2012-03-15 09:29:58569
Maks Orlovich323efaf2018-03-06 02:56:39570 // Defers the callback until the cookies relevant to given URL have been
571 // loaded. If they've already been loaded, runs the callback synchronously.
rdsmithe5c701d2017-07-12 21:50:00572 void DoCookieCallbackForURL(base::OnceClosure callback, const GURL& url);
[email protected]63ee33bd2012-03-15 09:29:58573
Maks Orlovich323efaf2018-03-06 02:56:39574 // Defers the callback until the cookies relevant to given host or domain
575 // have been loaded. If they've already been loaded, runs the callback
576 // synchronously.
577 void DoCookieCallbackForHostOrDomain(base::OnceClosure callback,
578 base::StringPiece host_or_domain);
579
[email protected]63ee33bd2012-03-15 09:29:58580 // Histogram variables; see CookieMonster::InitializeHistograms() in
581 // cookie_monster.cc for details.
[email protected]de415552013-01-23 04:12:17582 base::HistogramBase* histogram_expiration_duration_minutes_;
[email protected]de415552013-01-23 04:12:17583 base::HistogramBase* histogram_count_;
mkwstc1aa4cc2015-04-03 19:57:45584 base::HistogramBase* histogram_cookie_type_;
estark7feb65c2b2015-08-21 23:38:20585 base::HistogramBase* histogram_cookie_source_scheme_;
jww31e32632015-12-16 23:38:34586 base::HistogramBase* histogram_cookie_delete_equivalent_;
[email protected]de415552013-01-23 04:12:17587 base::HistogramBase* histogram_time_blocked_on_load_;
[email protected]63ee33bd2012-03-15 09:29:58588
589 CookieMap cookies_;
590
Victor Costan14f47c12018-03-01 08:02:24591 CookieMonsterChangeDispatcher change_dispatcher_;
592
erikchen1dd72a72015-05-06 20:45:05593 // Indicates whether the cookie store has been initialized.
[email protected]63ee33bd2012-03-15 09:29:58594 bool initialized_;
595
erikchen1dd72a72015-05-06 20:45:05596 // Indicates whether the cookie store has started fetching all cookies.
597 bool started_fetching_all_cookies_;
598 // Indicates whether the cookie store has finished fetching all cookies.
599 bool finished_fetching_all_cookies_;
[email protected]63ee33bd2012-03-15 09:29:58600
601 // List of domain keys that have been loaded from the DB.
602 std::set<std::string> keys_loaded_;
603
604 // Map of domain keys to their associated task queues. These tasks are blocked
605 // until all cookies for the associated domain key eTLD+1 are loaded from the
606 // backend store.
Brett Wilsonc6a0c822017-09-12 00:04:29607 std::map<std::string, base::circular_deque<base::OnceClosure>>
608 tasks_pending_for_key_;
[email protected]63ee33bd2012-03-15 09:29:58609
610 // Queues tasks that are blocked until all cookies are loaded from the backend
611 // store.
Brett Wilsonc6a0c822017-09-12 00:04:29612 base::circular_deque<base::OnceClosure> tasks_pending_;
mmenkef49fca0e2016-03-08 12:46:24613
614 // Once a global cookie task has been seen, all per-key tasks must be put in
615 // |tasks_pending_| instead of |tasks_pending_for_key_| to ensure a reasonable
rdsmithe5c701d2017-07-12 21:50:00616 // view of the cookie store. This is more to ensure fancy cookie export/import
mmenkef49fca0e2016-03-08 12:46:24617 // code has a consistent view of the CookieStore, rather than out of concern
618 // for typical use.
619 bool seen_global_task_;
[email protected]63ee33bd2012-03-15 09:29:58620
Helen Licd0fab862018-08-13 16:07:53621 NetLogWithSource net_log_;
622
[email protected]63ee33bd2012-03-15 09:29:58623 scoped_refptr<PersistentCookieStore> store_;
624
625 base::Time last_time_seen_;
626
627 // Minimum delay after updating a cookie's LastAccessDate before we will
628 // update it again.
629 const base::TimeDelta last_access_threshold_;
630
631 // Approximate date of access time of least recently accessed cookie
632 // in |cookies_|. Note that this is not guaranteed to be accurate, only a)
633 // to be before or equal to the actual time, and b) to be accurate
mmenkef4721d992017-06-07 17:13:59634 // immediately after a garbage collection that scans through all the cookies
635 // (When garbage collection does not scan through all cookies, it may not be
636 // updated). This value is used to determine whether global garbage collection
637 // might find cookies to purge. Note: The default Time() constructor will
638 // create a value that compares earlier than any other time value, which is
639 // wanted. Thus this value is not initialized.
[email protected]63ee33bd2012-03-15 09:29:58640 base::Time earliest_access_time_;
641
[email protected]63ee33bd2012-03-15 09:29:58642 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_