xref: /aosp_15_r20/external/cronet/net/cookies/cookie_monster.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1*6777b538SAndroid Build Coastguard Worker // Copyright 2012 The Chromium Authors
2*6777b538SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*6777b538SAndroid Build Coastguard Worker // found in the LICENSE file.
4*6777b538SAndroid Build Coastguard Worker 
5*6777b538SAndroid Build Coastguard Worker // Brought to you by the letter D and the number 2.
6*6777b538SAndroid Build Coastguard Worker 
7*6777b538SAndroid Build Coastguard Worker #ifndef NET_COOKIES_COOKIE_MONSTER_H_
8*6777b538SAndroid Build Coastguard Worker #define NET_COOKIES_COOKIE_MONSTER_H_
9*6777b538SAndroid Build Coastguard Worker 
10*6777b538SAndroid Build Coastguard Worker #include <stddef.h>
11*6777b538SAndroid Build Coastguard Worker #include <stdint.h>
12*6777b538SAndroid Build Coastguard Worker 
13*6777b538SAndroid Build Coastguard Worker #include <map>
14*6777b538SAndroid Build Coastguard Worker #include <memory>
15*6777b538SAndroid Build Coastguard Worker #include <optional>
16*6777b538SAndroid Build Coastguard Worker #include <set>
17*6777b538SAndroid Build Coastguard Worker #include <string>
18*6777b538SAndroid Build Coastguard Worker #include <string_view>
19*6777b538SAndroid Build Coastguard Worker #include <vector>
20*6777b538SAndroid Build Coastguard Worker 
21*6777b538SAndroid Build Coastguard Worker #include "base/containers/circular_deque.h"
22*6777b538SAndroid Build Coastguard Worker #include "base/containers/flat_map.h"
23*6777b538SAndroid Build Coastguard Worker #include "base/functional/callback_forward.h"
24*6777b538SAndroid Build Coastguard Worker #include "base/gtest_prod_util.h"
25*6777b538SAndroid Build Coastguard Worker #include "base/memory/ref_counted.h"
26*6777b538SAndroid Build Coastguard Worker #include "base/memory/weak_ptr.h"
27*6777b538SAndroid Build Coastguard Worker #include "base/thread_annotations.h"
28*6777b538SAndroid Build Coastguard Worker #include "base/threading/thread_checker.h"
29*6777b538SAndroid Build Coastguard Worker #include "base/time/time.h"
30*6777b538SAndroid Build Coastguard Worker #include "net/base/net_export.h"
31*6777b538SAndroid Build Coastguard Worker #include "net/base/schemeful_site.h"
32*6777b538SAndroid Build Coastguard Worker #include "net/cookies/canonical_cookie.h"
33*6777b538SAndroid Build Coastguard Worker #include "net/cookies/cookie_access_delegate.h"
34*6777b538SAndroid Build Coastguard Worker #include "net/cookies/cookie_constants.h"
35*6777b538SAndroid Build Coastguard Worker #include "net/cookies/cookie_inclusion_status.h"
36*6777b538SAndroid Build Coastguard Worker #include "net/cookies/cookie_monster_change_dispatcher.h"
37*6777b538SAndroid Build Coastguard Worker #include "net/cookies/cookie_store.h"
38*6777b538SAndroid Build Coastguard Worker #include "net/log/net_log_with_source.h"
39*6777b538SAndroid Build Coastguard Worker #include "url/gurl.h"
40*6777b538SAndroid Build Coastguard Worker 
41*6777b538SAndroid Build Coastguard Worker namespace net {
42*6777b538SAndroid Build Coastguard Worker 
43*6777b538SAndroid Build Coastguard Worker class CookieChangeDispatcher;
44*6777b538SAndroid Build Coastguard Worker 
45*6777b538SAndroid Build Coastguard Worker // The cookie monster is the system for storing and retrieving cookies. It has
46*6777b538SAndroid Build Coastguard Worker // an in-memory list of all cookies, and synchronizes non-session cookies to an
47*6777b538SAndroid Build Coastguard Worker // optional permanent storage that implements the PersistentCookieStore
48*6777b538SAndroid Build Coastguard Worker // interface.
49*6777b538SAndroid Build Coastguard Worker //
50*6777b538SAndroid Build Coastguard Worker // Tasks may be deferred if all affected cookies are not yet loaded from the
51*6777b538SAndroid Build Coastguard Worker // backing store. Otherwise, callbacks may be invoked immediately.
52*6777b538SAndroid Build Coastguard Worker //
53*6777b538SAndroid Build Coastguard Worker // A cookie task is either pending loading of the entire cookie store, or
54*6777b538SAndroid Build Coastguard Worker // loading of cookies for a specific domain key (GetKey(), roughly eTLD+1). In
55*6777b538SAndroid Build Coastguard Worker // the former case, the cookie callback will be queued in tasks_pending_ while
56*6777b538SAndroid Build Coastguard Worker // PersistentCookieStore chain loads the cookie store on DB thread. In the
57*6777b538SAndroid Build Coastguard Worker // latter case, the cookie callback will be queued in tasks_pending_for_key_
58*6777b538SAndroid Build Coastguard Worker // while PermanentCookieStore loads cookies for the specified domain key on DB
59*6777b538SAndroid Build Coastguard Worker // thread.
60*6777b538SAndroid Build Coastguard Worker class NET_EXPORT CookieMonster : public CookieStore {
61*6777b538SAndroid Build Coastguard Worker  public:
62*6777b538SAndroid Build Coastguard Worker   class PersistentCookieStore;
63*6777b538SAndroid Build Coastguard Worker 
64*6777b538SAndroid Build Coastguard Worker   // Terminology:
65*6777b538SAndroid Build Coastguard Worker   //    * The 'top level domain' (TLD) of an internet domain name is
66*6777b538SAndroid Build Coastguard Worker   //      the terminal "." free substring (e.g. "com" for google.com
67*6777b538SAndroid Build Coastguard Worker   //      or world.std.com).
68*6777b538SAndroid Build Coastguard Worker   //    * The 'effective top level domain' (eTLD) is the longest
69*6777b538SAndroid Build Coastguard Worker   //      "." initiated terminal substring of an internet domain name
70*6777b538SAndroid Build Coastguard Worker   //      that is controlled by a general domain registrar.
71*6777b538SAndroid Build Coastguard Worker   //      (e.g. "co.uk" for news.bbc.co.uk).
72*6777b538SAndroid Build Coastguard Worker   //    * The 'effective top level domain plus one' (eTLD+1) is the
73*6777b538SAndroid Build Coastguard Worker   //      shortest "." delimited terminal substring of an internet
74*6777b538SAndroid Build Coastguard Worker   //      domain name that is not controlled by a general domain
75*6777b538SAndroid Build Coastguard Worker   //      registrar (e.g. "bbc.co.uk" for news.bbc.co.uk, or
76*6777b538SAndroid Build Coastguard Worker   //      "google.com" for news.google.com).  The general assumption
77*6777b538SAndroid Build Coastguard Worker   //      is that all hosts and domains under an eTLD+1 share some
78*6777b538SAndroid Build Coastguard Worker   //      administrative control.
79*6777b538SAndroid Build Coastguard Worker 
80*6777b538SAndroid Build Coastguard Worker   // CookieMap is the central data structure of the CookieMonster.  It
81*6777b538SAndroid Build Coastguard Worker   // is a map whose values are pointers to CanonicalCookie data
82*6777b538SAndroid Build Coastguard Worker   // structures (the data structures are owned by the CookieMonster
83*6777b538SAndroid Build Coastguard Worker   // and must be destroyed when removed from the map).  The key is based on the
84*6777b538SAndroid Build Coastguard Worker   // effective domain of the cookies.  If the domain of the cookie has an
85*6777b538SAndroid Build Coastguard Worker   // eTLD+1, that is the key for the map.  If the domain of the cookie does not
86*6777b538SAndroid Build Coastguard Worker   // have an eTLD+1, the key of the map is the host the cookie applies to (it is
87*6777b538SAndroid Build Coastguard Worker   // not legal to have domain cookies without an eTLD+1).  This rule
88*6777b538SAndroid Build Coastguard Worker   // excludes cookies for, e.g, ".com", ".co.uk", or ".internalnetwork".
89*6777b538SAndroid Build Coastguard Worker   // This behavior is the same as the behavior in Firefox v 3.6.10.
90*6777b538SAndroid Build Coastguard Worker   // CookieMap does not store cookies that were set with the Partitioned
91*6777b538SAndroid Build Coastguard Worker   // attribute, those are stored in PartitionedCookieMap.
92*6777b538SAndroid Build Coastguard Worker 
93*6777b538SAndroid Build Coastguard Worker   // NOTE(deanm):
94*6777b538SAndroid Build Coastguard Worker   // I benchmarked hash_multimap vs multimap.  We're going to be query-heavy
95*6777b538SAndroid Build Coastguard Worker   // so it would seem like hashing would help.  However they were very
96*6777b538SAndroid Build Coastguard Worker   // close, with multimap being a tiny bit faster.  I think this is because
97*6777b538SAndroid Build Coastguard Worker   // our map is at max around 1000 entries, and the additional complexity
98*6777b538SAndroid Build Coastguard Worker   // for the hashing might not overcome the O(log(1000)) for querying
99*6777b538SAndroid Build Coastguard Worker   // a multimap.  Also, multimap is standard, another reason to use it.
100*6777b538SAndroid Build Coastguard Worker   // TODO(rdsmith): This benchmark should be re-done now that we're allowing
101*6777b538SAndroid Build Coastguard Worker   // substantially more entries in the map.
102*6777b538SAndroid Build Coastguard Worker   using CookieMap =
103*6777b538SAndroid Build Coastguard Worker       std::multimap<std::string, std::unique_ptr<CanonicalCookie>>;
104*6777b538SAndroid Build Coastguard Worker   using CookieMapItPair = std::pair<CookieMap::iterator, CookieMap::iterator>;
105*6777b538SAndroid Build Coastguard Worker   using CookieItVector = std::vector<CookieMap::iterator>;
106*6777b538SAndroid Build Coastguard Worker   using CookieItList = std::list<CookieMap::iterator>;
107*6777b538SAndroid Build Coastguard Worker 
108*6777b538SAndroid Build Coastguard Worker   // PartitionedCookieMap only stores cookies that were set with the Partitioned
109*6777b538SAndroid Build Coastguard Worker   // attribute. The map is double-keyed on cookie's partition key and
110*6777b538SAndroid Build Coastguard Worker   // the cookie's effective domain of the cookie (the key of CookieMap).
111*6777b538SAndroid Build Coastguard Worker   // We store partitioned cookies in a separate map so that the queries for a
112*6777b538SAndroid Build Coastguard Worker   // request's unpartitioned and partitioned cookies will both be more
113*6777b538SAndroid Build Coastguard Worker   // efficient (since querying two smaller maps is more efficient that querying
114*6777b538SAndroid Build Coastguard Worker   // one larger map twice).
115*6777b538SAndroid Build Coastguard Worker   using PartitionedCookieMap =
116*6777b538SAndroid Build Coastguard Worker       std::map<CookiePartitionKey, std::unique_ptr<CookieMap>>;
117*6777b538SAndroid Build Coastguard Worker   using PartitionedCookieMapIterators =
118*6777b538SAndroid Build Coastguard Worker       std::pair<PartitionedCookieMap::iterator, CookieMap::iterator>;
119*6777b538SAndroid Build Coastguard Worker 
120*6777b538SAndroid Build Coastguard Worker   // Cookie garbage collection thresholds.  Based off of the Mozilla defaults.
121*6777b538SAndroid Build Coastguard Worker   // When the number of cookies gets to k{Domain,}MaxCookies
122*6777b538SAndroid Build Coastguard Worker   // purge down to k{Domain,}MaxCookies - k{Domain,}PurgeCookies.
123*6777b538SAndroid Build Coastguard Worker   // It might seem scary to have a high purge value, but really it's not.
124*6777b538SAndroid Build Coastguard Worker   // You just make sure that you increase the max to cover the increase
125*6777b538SAndroid Build Coastguard Worker   // in purge, and we would have been purging the same number of cookies.
126*6777b538SAndroid Build Coastguard Worker   // We're just going through the garbage collection process less often.
127*6777b538SAndroid Build Coastguard Worker   // Note that the DOMAIN values are per eTLD+1; see comment for the
128*6777b538SAndroid Build Coastguard Worker   // CookieMap typedef.  So, e.g., the maximum number of cookies allowed for
129*6777b538SAndroid Build Coastguard Worker   // google.com and all of its subdomains will be 150-180.
130*6777b538SAndroid Build Coastguard Worker   //
131*6777b538SAndroid Build Coastguard Worker   // Any cookies accessed more recently than kSafeFromGlobalPurgeDays will not
132*6777b538SAndroid Build Coastguard Worker   // be evicted by global garbage collection, even if we have more than
133*6777b538SAndroid Build Coastguard Worker   // kMaxCookies.  This does not affect domain garbage collection.
134*6777b538SAndroid Build Coastguard Worker   static const size_t kDomainMaxCookies;
135*6777b538SAndroid Build Coastguard Worker   static const size_t kDomainPurgeCookies;
136*6777b538SAndroid Build Coastguard Worker   static const size_t kMaxCookies;
137*6777b538SAndroid Build Coastguard Worker   static const size_t kPurgeCookies;
138*6777b538SAndroid Build Coastguard Worker 
139*6777b538SAndroid Build Coastguard Worker   // Max number of keys to store for domains that have been purged.
140*6777b538SAndroid Build Coastguard Worker   static const size_t kMaxDomainPurgedKeys;
141*6777b538SAndroid Build Coastguard Worker 
142*6777b538SAndroid Build Coastguard Worker   // Partitioned cookie garbage collection thresholds.
143*6777b538SAndroid Build Coastguard Worker   static const size_t kPerPartitionDomainMaxCookieBytes;
144*6777b538SAndroid Build Coastguard Worker   static const size_t kPerPartitionDomainMaxCookies;
145*6777b538SAndroid Build Coastguard Worker   // TODO(crbug.com/1225444): Add global limit to number of partitioned cookies.
146*6777b538SAndroid Build Coastguard Worker 
147*6777b538SAndroid Build Coastguard Worker   // Quota for cookies with {low, medium, high} priorities within a domain.
148*6777b538SAndroid Build Coastguard Worker   static const size_t kDomainCookiesQuotaLow;
149*6777b538SAndroid Build Coastguard Worker   static const size_t kDomainCookiesQuotaMedium;
150*6777b538SAndroid Build Coastguard Worker   static const size_t kDomainCookiesQuotaHigh;
151*6777b538SAndroid Build Coastguard Worker 
152*6777b538SAndroid Build Coastguard Worker   // The number of days since last access that cookies will not be subject
153*6777b538SAndroid Build Coastguard Worker   // to global garbage collection.
154*6777b538SAndroid Build Coastguard Worker   static const int kSafeFromGlobalPurgeDays;
155*6777b538SAndroid Build Coastguard Worker 
156*6777b538SAndroid Build Coastguard Worker   // The store passed in should not have had Init() called on it yet. This
157*6777b538SAndroid Build Coastguard Worker   // class will take care of initializing it. The backing store is NOT owned by
158*6777b538SAndroid Build Coastguard Worker   // this class, but it must remain valid for the duration of the cookie
159*6777b538SAndroid Build Coastguard Worker   // monster's existence. If |store| is NULL, then no backing store will be
160*6777b538SAndroid Build Coastguard Worker   // updated. |net_log| must outlive the CookieMonster and can be null.
161*6777b538SAndroid Build Coastguard Worker   CookieMonster(scoped_refptr<PersistentCookieStore> store, NetLog* net_log);
162*6777b538SAndroid Build Coastguard Worker 
163*6777b538SAndroid Build Coastguard Worker   // Only used during unit testing.
164*6777b538SAndroid Build Coastguard Worker   // |net_log| must outlive the CookieMonster.
165*6777b538SAndroid Build Coastguard Worker   CookieMonster(scoped_refptr<PersistentCookieStore> store,
166*6777b538SAndroid Build Coastguard Worker                 base::TimeDelta last_access_threshold,
167*6777b538SAndroid Build Coastguard Worker                 NetLog* net_log);
168*6777b538SAndroid Build Coastguard Worker 
169*6777b538SAndroid Build Coastguard Worker   CookieMonster(const CookieMonster&) = delete;
170*6777b538SAndroid Build Coastguard Worker   CookieMonster& operator=(const CookieMonster&) = delete;
171*6777b538SAndroid Build Coastguard Worker 
172*6777b538SAndroid Build Coastguard Worker   ~CookieMonster() override;
173*6777b538SAndroid Build Coastguard Worker 
174*6777b538SAndroid Build Coastguard Worker   // Writes all the cookies in |list| into the store, replacing all cookies
175*6777b538SAndroid Build Coastguard Worker   // currently present in store.
176*6777b538SAndroid Build Coastguard Worker   // This method does not flush the backend.
177*6777b538SAndroid Build Coastguard Worker   // TODO(rdsmith, mmenke): Do not use this function; it is deprecated
178*6777b538SAndroid Build Coastguard Worker   // and should be removed.
179*6777b538SAndroid Build Coastguard Worker   // See https://codereview.chromium.org/2882063002/#msg64.
180*6777b538SAndroid Build Coastguard Worker   void SetAllCookiesAsync(const CookieList& list, SetCookiesCallback callback);
181*6777b538SAndroid Build Coastguard Worker 
182*6777b538SAndroid Build Coastguard Worker   // CookieStore implementation.
183*6777b538SAndroid Build Coastguard Worker   void SetCanonicalCookieAsync(
184*6777b538SAndroid Build Coastguard Worker       std::unique_ptr<CanonicalCookie> cookie,
185*6777b538SAndroid Build Coastguard Worker       const GURL& source_url,
186*6777b538SAndroid Build Coastguard Worker       const CookieOptions& options,
187*6777b538SAndroid Build Coastguard Worker       SetCookiesCallback callback,
188*6777b538SAndroid Build Coastguard Worker       std::optional<CookieAccessResult> cookie_access_result =
189*6777b538SAndroid Build Coastguard Worker           std::nullopt) override;
190*6777b538SAndroid Build Coastguard Worker   void GetCookieListWithOptionsAsync(const GURL& url,
191*6777b538SAndroid Build Coastguard Worker                                      const CookieOptions& options,
192*6777b538SAndroid Build Coastguard Worker                                      const CookiePartitionKeyCollection& s,
193*6777b538SAndroid Build Coastguard Worker                                      GetCookieListCallback callback) override;
194*6777b538SAndroid Build Coastguard Worker   void GetAllCookiesAsync(GetAllCookiesCallback callback) override;
195*6777b538SAndroid Build Coastguard Worker   void GetAllCookiesWithAccessSemanticsAsync(
196*6777b538SAndroid Build Coastguard Worker       GetAllCookiesWithAccessSemanticsCallback callback) override;
197*6777b538SAndroid Build Coastguard Worker   void DeleteCanonicalCookieAsync(const CanonicalCookie& cookie,
198*6777b538SAndroid Build Coastguard Worker                                   DeleteCallback callback) override;
199*6777b538SAndroid Build Coastguard Worker   void DeleteAllCreatedInTimeRangeAsync(
200*6777b538SAndroid Build Coastguard Worker       const CookieDeletionInfo::TimeRange& creation_range,
201*6777b538SAndroid Build Coastguard Worker       DeleteCallback callback) override;
202*6777b538SAndroid Build Coastguard Worker   void DeleteAllMatchingInfoAsync(CookieDeletionInfo delete_info,
203*6777b538SAndroid Build Coastguard Worker                                   DeleteCallback callback) override;
204*6777b538SAndroid Build Coastguard Worker   void DeleteSessionCookiesAsync(DeleteCallback callback) override;
205*6777b538SAndroid Build Coastguard Worker   void DeleteMatchingCookiesAsync(DeletePredicate predicate,
206*6777b538SAndroid Build Coastguard Worker                                   DeleteCallback callback) override;
207*6777b538SAndroid Build Coastguard Worker   void FlushStore(base::OnceClosure callback) override;
208*6777b538SAndroid Build Coastguard Worker   void SetForceKeepSessionState() override;
209*6777b538SAndroid Build Coastguard Worker   CookieChangeDispatcher& GetChangeDispatcher() override;
210*6777b538SAndroid Build Coastguard Worker   void SetCookieableSchemes(const std::vector<std::string>& schemes,
211*6777b538SAndroid Build Coastguard Worker                             SetCookieableSchemesCallback callback) override;
212*6777b538SAndroid Build Coastguard Worker   std::optional<bool> SiteHasCookieInOtherPartition(
213*6777b538SAndroid Build Coastguard Worker       const net::SchemefulSite& site,
214*6777b538SAndroid Build Coastguard Worker       const std::optional<CookiePartitionKey>& partition_key) const override;
215*6777b538SAndroid Build Coastguard Worker 
216*6777b538SAndroid Build Coastguard Worker   // Enables writing session cookies into the cookie database. If this this
217*6777b538SAndroid Build Coastguard Worker   // method is called, it must be called before first use of the instance
218*6777b538SAndroid Build Coastguard Worker   // (i.e. as part of the instance initialization process).
219*6777b538SAndroid Build Coastguard Worker   void SetPersistSessionCookies(bool persist_session_cookies);
220*6777b538SAndroid Build Coastguard Worker 
221*6777b538SAndroid Build Coastguard Worker   // The default list of schemes the cookie monster can handle.
222*6777b538SAndroid Build Coastguard Worker   static const char* const kDefaultCookieableSchemes[];
223*6777b538SAndroid Build Coastguard Worker   static const int kDefaultCookieableSchemesCount;
224*6777b538SAndroid Build Coastguard Worker 
225*6777b538SAndroid Build Coastguard Worker   // Find a key based on the given domain, which will be used to find all
226*6777b538SAndroid Build Coastguard Worker   // cookies potentially relevant to it. This is used for lookup in cookies_ as
227*6777b538SAndroid Build Coastguard Worker   // well as for PersistentCookieStore::LoadCookiesForKey. See comment on keys
228*6777b538SAndroid Build Coastguard Worker   // before the CookieMap typedef.
229*6777b538SAndroid Build Coastguard Worker   static std::string GetKey(std::string_view domain);
230*6777b538SAndroid Build Coastguard Worker 
231*6777b538SAndroid Build Coastguard Worker   // Exposes the comparison function used when sorting cookies.
232*6777b538SAndroid Build Coastguard Worker   static bool CookieSorter(const CanonicalCookie* cc1,
233*6777b538SAndroid Build Coastguard Worker                            const CanonicalCookie* cc2);
234*6777b538SAndroid Build Coastguard Worker 
235*6777b538SAndroid Build Coastguard Worker   // Triggers immediate recording of stats that are typically reported
236*6777b538SAndroid Build Coastguard Worker   // periodically.
DoRecordPeriodicStatsForTesting()237*6777b538SAndroid Build Coastguard Worker   bool DoRecordPeriodicStatsForTesting() { return DoRecordPeriodicStats(); }
238*6777b538SAndroid Build Coastguard Worker 
239*6777b538SAndroid Build Coastguard Worker  private:
240*6777b538SAndroid Build Coastguard Worker   // For garbage collection constants.
241*6777b538SAndroid Build Coastguard Worker   FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestHostGarbageCollection);
242*6777b538SAndroid Build Coastguard Worker   FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest,
243*6777b538SAndroid Build Coastguard Worker                            GarbageCollectWithSecureCookiesOnly);
244*6777b538SAndroid Build Coastguard Worker   FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestGCTimes);
245*6777b538SAndroid Build Coastguard Worker 
246*6777b538SAndroid Build Coastguard Worker   // For validation of key values.
247*6777b538SAndroid Build Coastguard Worker   FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestDomainTree);
248*6777b538SAndroid Build Coastguard Worker   FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestImport);
249*6777b538SAndroid Build Coastguard Worker   FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, GetKey);
250*6777b538SAndroid Build Coastguard Worker   FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestGetKey);
251*6777b538SAndroid Build Coastguard Worker 
252*6777b538SAndroid Build Coastguard Worker   // For FindCookiesForKey.
253*6777b538SAndroid Build Coastguard Worker   FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, ShortLivedSessionCookies);
254*6777b538SAndroid Build Coastguard Worker 
255*6777b538SAndroid Build Coastguard Worker   // For CookieSource histogram enum.
256*6777b538SAndroid Build Coastguard Worker   FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, CookieSourceHistogram);
257*6777b538SAndroid Build Coastguard Worker 
258*6777b538SAndroid Build Coastguard Worker   // For kSafeFromGlobalPurgeDays in CookieStore.
259*6777b538SAndroid Build Coastguard Worker   FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, EvictSecureCookies);
260*6777b538SAndroid Build Coastguard Worker 
261*6777b538SAndroid Build Coastguard Worker   // For CookieDeleteEquivalent histogram enum.
262*6777b538SAndroid Build Coastguard Worker   FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest,
263*6777b538SAndroid Build Coastguard Worker                            CookieDeleteEquivalentHistogramTest);
264*6777b538SAndroid Build Coastguard Worker 
265*6777b538SAndroid Build Coastguard Worker   // For CookieSentToSamePort enum.
266*6777b538SAndroid Build Coastguard Worker   FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest,
267*6777b538SAndroid Build Coastguard Worker                            CookiePortReadDiffersFromSetHistogram);
268*6777b538SAndroid Build Coastguard Worker   FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, IsCookieSentToSamePortThatSetIt);
269*6777b538SAndroid Build Coastguard Worker 
270*6777b538SAndroid Build Coastguard Worker   // For FilterCookiesWithOptions domain shadowing.
271*6777b538SAndroid Build Coastguard Worker   FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest,
272*6777b538SAndroid Build Coastguard Worker                            FilterCookiesWithOptionsExcludeShadowingDomains);
273*6777b538SAndroid Build Coastguard Worker   FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest,
274*6777b538SAndroid Build Coastguard Worker                            FilterCookiesWithOptionsWarnShadowingDomains);
275*6777b538SAndroid Build Coastguard Worker 
276*6777b538SAndroid Build Coastguard Worker   // For StoreLoadedCookies behavior with origin-bound cookies.
277*6777b538SAndroid Build Coastguard Worker   FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest_StoreLoadedCookies,
278*6777b538SAndroid Build Coastguard Worker                            NoSchemeNoPort);
279*6777b538SAndroid Build Coastguard Worker   FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest_StoreLoadedCookies,
280*6777b538SAndroid Build Coastguard Worker                            YesSchemeNoPort);
281*6777b538SAndroid Build Coastguard Worker   FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest_StoreLoadedCookies,
282*6777b538SAndroid Build Coastguard Worker                            NoSchemeYesPort);
283*6777b538SAndroid Build Coastguard Worker   FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest_StoreLoadedCookies,
284*6777b538SAndroid Build Coastguard Worker                            YesSchemeYesPort);
285*6777b538SAndroid Build Coastguard Worker 
286*6777b538SAndroid Build Coastguard Worker   // Internal reasons for deletion, used to populate informative histograms
287*6777b538SAndroid Build Coastguard Worker   // and to provide a public cause for onCookieChange notifications.
288*6777b538SAndroid Build Coastguard Worker   //
289*6777b538SAndroid Build Coastguard Worker   // If you add or remove causes from this list, please be sure to also update
290*6777b538SAndroid Build Coastguard Worker   // the CookieChangeCause mapping inside ChangeCauseMapping. New items (if
291*6777b538SAndroid Build Coastguard Worker   // necessary) should be added at the end of the list, just before
292*6777b538SAndroid Build Coastguard Worker   // DELETE_COOKIE_LAST_ENTRY.
293*6777b538SAndroid Build Coastguard Worker   enum DeletionCause {
294*6777b538SAndroid Build Coastguard Worker     DELETE_COOKIE_EXPLICIT = 0,
295*6777b538SAndroid Build Coastguard Worker     DELETE_COOKIE_OVERWRITE = 1,
296*6777b538SAndroid Build Coastguard Worker     DELETE_COOKIE_EXPIRED = 2,
297*6777b538SAndroid Build Coastguard Worker     DELETE_COOKIE_EVICTED = 3,
298*6777b538SAndroid Build Coastguard Worker     DELETE_COOKIE_DUPLICATE_IN_BACKING_STORE = 4,
299*6777b538SAndroid Build Coastguard Worker     DELETE_COOKIE_DONT_RECORD = 5,  // For final cleanup after flush to store.
300*6777b538SAndroid Build Coastguard Worker 
301*6777b538SAndroid Build Coastguard Worker     // Cookies evicted during domain-level garbage collection.
302*6777b538SAndroid Build Coastguard Worker     DELETE_COOKIE_EVICTED_DOMAIN = 6,
303*6777b538SAndroid Build Coastguard Worker 
304*6777b538SAndroid Build Coastguard Worker     // Cookies evicted during global garbage collection, which takes place after
305*6777b538SAndroid Build Coastguard Worker     // domain-level garbage collection fails to bring the cookie store under
306*6777b538SAndroid Build Coastguard Worker     // the overall quota.
307*6777b538SAndroid Build Coastguard Worker     DELETE_COOKIE_EVICTED_GLOBAL = 7,
308*6777b538SAndroid Build Coastguard Worker 
309*6777b538SAndroid Build Coastguard Worker     // #8 was DELETE_COOKIE_EVICTED_DOMAIN_PRE_SAFE
310*6777b538SAndroid Build Coastguard Worker     // #9 was DELETE_COOKIE_EVICTED_DOMAIN_POST_SAFE
311*6777b538SAndroid Build Coastguard Worker 
312*6777b538SAndroid Build Coastguard Worker     // A common idiom is to remove a cookie by overwriting it with an
313*6777b538SAndroid Build Coastguard Worker     // already-expired expiration date. This captures that case.
314*6777b538SAndroid Build Coastguard Worker     DELETE_COOKIE_EXPIRED_OVERWRITE = 10,
315*6777b538SAndroid Build Coastguard Worker 
316*6777b538SAndroid Build Coastguard Worker     // Cookies are not allowed to contain control characters in the name or
317*6777b538SAndroid Build Coastguard Worker     // value. However, we used to allow them, so we are now evicting any such
318*6777b538SAndroid Build Coastguard Worker     // cookies as we load them. See http://crbug.com/238041.
319*6777b538SAndroid Build Coastguard Worker     DELETE_COOKIE_CONTROL_CHAR = 11,
320*6777b538SAndroid Build Coastguard Worker 
321*6777b538SAndroid Build Coastguard Worker     // When strict secure cookies is enabled, non-secure cookies are evicted
322*6777b538SAndroid Build Coastguard Worker     // right after expired cookies.
323*6777b538SAndroid Build Coastguard Worker     DELETE_COOKIE_NON_SECURE = 12,
324*6777b538SAndroid Build Coastguard Worker 
325*6777b538SAndroid Build Coastguard Worker     // Partitioned cookies evicted during per-partition domain-level garbage
326*6777b538SAndroid Build Coastguard Worker     // collection.
327*6777b538SAndroid Build Coastguard Worker     DELETE_COOKIE_EVICTED_PER_PARTITION_DOMAIN = 13,
328*6777b538SAndroid Build Coastguard Worker 
329*6777b538SAndroid Build Coastguard Worker     DELETE_COOKIE_LAST_ENTRY = 14,
330*6777b538SAndroid Build Coastguard Worker   };
331*6777b538SAndroid Build Coastguard Worker 
332*6777b538SAndroid Build Coastguard Worker   // Used to populate a histogram containing information about the
333*6777b538SAndroid Build Coastguard Worker   // sources of Secure and non-Secure cookies: that is, whether such
334*6777b538SAndroid Build Coastguard Worker   // cookies are set by origins with cryptographic or non-cryptographic
335*6777b538SAndroid Build Coastguard Worker   // schemes. Please do not reorder the list when adding new
336*6777b538SAndroid Build Coastguard Worker   // entries. New items MUST be added at the end of the list, and kMaxValue
337*6777b538SAndroid Build Coastguard Worker   // should be updated to the last value.
338*6777b538SAndroid Build Coastguard Worker   //
339*6777b538SAndroid Build Coastguard Worker   // CookieSource::k(Non)SecureCookie(Non)CryptographicScheme means
340*6777b538SAndroid Build Coastguard Worker   // that a cookie was set or overwritten from a URL with the given type
341*6777b538SAndroid Build Coastguard Worker   // of scheme. This enum should not be used when cookies are *cleared*,
342*6777b538SAndroid Build Coastguard Worker   // because its purpose is to understand if Chrome can deprecate the
343*6777b538SAndroid Build Coastguard Worker   // ability of HTTP urls to set/overwrite Secure cookies.
344*6777b538SAndroid Build Coastguard Worker   enum class CookieSource : uint8_t {
345*6777b538SAndroid Build Coastguard Worker     kSecureCookieCryptographicScheme = 0,
346*6777b538SAndroid Build Coastguard Worker     kSecureCookieNoncryptographicScheme,
347*6777b538SAndroid Build Coastguard Worker     kNonsecureCookieCryptographicScheme,
348*6777b538SAndroid Build Coastguard Worker     kNonsecureCookieNoncryptographicScheme,
349*6777b538SAndroid Build Coastguard Worker     kMaxValue = kNonsecureCookieNoncryptographicScheme
350*6777b538SAndroid Build Coastguard Worker   };
351*6777b538SAndroid Build Coastguard Worker 
352*6777b538SAndroid Build Coastguard Worker   // Enum for collecting metrics on how frequently a cookie is sent to the same
353*6777b538SAndroid Build Coastguard Worker   // port it was set by.
354*6777b538SAndroid Build Coastguard Worker   //
355*6777b538SAndroid Build Coastguard Worker   // kNoButDefault exists because we expect for cookies being sent between
356*6777b538SAndroid Build Coastguard Worker   // schemes to have a port mismatch and want to separate those out from other,
357*6777b538SAndroid Build Coastguard Worker   // more interesting, cases.
358*6777b538SAndroid Build Coastguard Worker   //
359*6777b538SAndroid Build Coastguard Worker   // Do not reorder or renumber. Used for metrics.
360*6777b538SAndroid Build Coastguard Worker   enum class CookieSentToSamePort {
361*6777b538SAndroid Build Coastguard Worker     kSourcePortUnspecified = 0,  // Cookie's source port is unspecified, we
362*6777b538SAndroid Build Coastguard Worker                                  // can't know if this is the same port or not.
363*6777b538SAndroid Build Coastguard Worker     kInvalid = 1,  // The source port was corrupted to be PORT_INVALID, we
364*6777b538SAndroid Build Coastguard Worker                    // can't know if this is the same port or not.
365*6777b538SAndroid Build Coastguard Worker     kNo = 2,       // Source port and destination port are different.
366*6777b538SAndroid Build Coastguard Worker     kNoButDefault =
367*6777b538SAndroid Build Coastguard Worker         3,     // Source and destination ports are different but they're
368*6777b538SAndroid Build Coastguard Worker                // the defaults for their scheme. This can mean that an http
369*6777b538SAndroid Build Coastguard Worker                // cookie was sent to a https origin or vice-versa.
370*6777b538SAndroid Build Coastguard Worker     kYes = 4,  // They're the same.
371*6777b538SAndroid Build Coastguard Worker     kMaxValue = kYes
372*6777b538SAndroid Build Coastguard Worker   };
373*6777b538SAndroid Build Coastguard Worker 
374*6777b538SAndroid Build Coastguard Worker   // Record statistics every kRecordStatisticsIntervalSeconds of uptime.
375*6777b538SAndroid Build Coastguard Worker   static const int kRecordStatisticsIntervalSeconds = 10 * 60;
376*6777b538SAndroid Build Coastguard Worker 
377*6777b538SAndroid Build Coastguard Worker   // Sets a canonical cookie, deletes equivalents and performs garbage
378*6777b538SAndroid Build Coastguard Worker   // collection.  |source_url| indicates what URL the cookie is being set
379*6777b538SAndroid Build Coastguard Worker   // from; secure cookies cannot be altered from insecure schemes, and some
380*6777b538SAndroid Build Coastguard Worker   // schemes may not be authorized.
381*6777b538SAndroid Build Coastguard Worker   //
382*6777b538SAndroid Build Coastguard Worker   // |options| indicates if this setting operation is allowed
383*6777b538SAndroid Build Coastguard Worker   // to affect http_only or same-site cookies.
384*6777b538SAndroid Build Coastguard Worker   //
385*6777b538SAndroid Build Coastguard Worker   // |cookie_access_result| is an optional input status, to allow for status
386*6777b538SAndroid Build Coastguard Worker   // chaining from callers. It helps callers provide the status of a
387*6777b538SAndroid Build Coastguard Worker   // canonical cookie that may have warnings associated with it.
388*6777b538SAndroid Build Coastguard Worker   void SetCanonicalCookie(
389*6777b538SAndroid Build Coastguard Worker       std::unique_ptr<CanonicalCookie> cookie,
390*6777b538SAndroid Build Coastguard Worker       const GURL& source_url,
391*6777b538SAndroid Build Coastguard Worker       const CookieOptions& options,
392*6777b538SAndroid Build Coastguard Worker       SetCookiesCallback callback,
393*6777b538SAndroid Build Coastguard Worker       std::optional<CookieAccessResult> cookie_access_result = std::nullopt);
394*6777b538SAndroid Build Coastguard Worker 
395*6777b538SAndroid Build Coastguard Worker   void GetAllCookies(GetAllCookiesCallback callback);
396*6777b538SAndroid Build Coastguard Worker 
397*6777b538SAndroid Build Coastguard Worker   void AttachAccessSemanticsListForCookieList(
398*6777b538SAndroid Build Coastguard Worker       GetAllCookiesWithAccessSemanticsCallback callback,
399*6777b538SAndroid Build Coastguard Worker       const CookieList& cookie_list);
400*6777b538SAndroid Build Coastguard Worker 
401*6777b538SAndroid Build Coastguard Worker   void GetCookieListWithOptions(
402*6777b538SAndroid Build Coastguard Worker       const GURL& url,
403*6777b538SAndroid Build Coastguard Worker       const CookieOptions& options,
404*6777b538SAndroid Build Coastguard Worker       const CookiePartitionKeyCollection& cookie_partition_key_collection,
405*6777b538SAndroid Build Coastguard Worker       GetCookieListCallback callback);
406*6777b538SAndroid Build Coastguard Worker 
407*6777b538SAndroid Build Coastguard Worker   void DeleteAllCreatedInTimeRange(
408*6777b538SAndroid Build Coastguard Worker       const CookieDeletionInfo::TimeRange& creation_range,
409*6777b538SAndroid Build Coastguard Worker       DeleteCallback callback);
410*6777b538SAndroid Build Coastguard Worker 
411*6777b538SAndroid Build Coastguard Worker   // Returns whether |cookie| matches |delete_info|.
412*6777b538SAndroid Build Coastguard Worker   bool MatchCookieDeletionInfo(const CookieDeletionInfo& delete_info,
413*6777b538SAndroid Build Coastguard Worker                                const net::CanonicalCookie& cookie);
414*6777b538SAndroid Build Coastguard Worker 
415*6777b538SAndroid Build Coastguard Worker   void DeleteCanonicalCookie(const CanonicalCookie& cookie,
416*6777b538SAndroid Build Coastguard Worker                              DeleteCallback callback);
417*6777b538SAndroid Build Coastguard Worker 
418*6777b538SAndroid Build Coastguard Worker   void DeleteMatchingCookies(DeletePredicate predicate,
419*6777b538SAndroid Build Coastguard Worker                              DeletionCause cause,
420*6777b538SAndroid Build Coastguard Worker                              DeleteCallback callback);
421*6777b538SAndroid Build Coastguard Worker 
422*6777b538SAndroid Build Coastguard Worker   // The first access to the cookie store initializes it. This method should be
423*6777b538SAndroid Build Coastguard Worker   // called before any access to the cookie store.
424*6777b538SAndroid Build Coastguard Worker   void MarkCookieStoreAsInitialized();
425*6777b538SAndroid Build Coastguard Worker 
426*6777b538SAndroid Build Coastguard Worker   // Fetches all cookies if the backing store exists and they're not already
427*6777b538SAndroid Build Coastguard Worker   // being fetched.
428*6777b538SAndroid Build Coastguard Worker   void FetchAllCookiesIfNecessary();
429*6777b538SAndroid Build Coastguard Worker 
430*6777b538SAndroid Build Coastguard Worker   // Fetches all cookies from the backing store.
431*6777b538SAndroid Build Coastguard Worker   void FetchAllCookies();
432*6777b538SAndroid Build Coastguard Worker 
433*6777b538SAndroid Build Coastguard Worker   // Whether all cookies should be fetched as soon as any is requested.
434*6777b538SAndroid Build Coastguard Worker   bool ShouldFetchAllCookiesWhenFetchingAnyCookie();
435*6777b538SAndroid Build Coastguard Worker 
436*6777b538SAndroid Build Coastguard Worker   // Stores cookies loaded from the backing store and invokes any deferred
437*6777b538SAndroid Build Coastguard Worker   // calls. |beginning_time| should be the moment PersistentCookieStore::Load
438*6777b538SAndroid Build Coastguard Worker   // was invoked and is used for reporting histogram_time_blocked_on_load_.
439*6777b538SAndroid Build Coastguard Worker   // See PersistentCookieStore::Load for details on the contents of cookies.
440*6777b538SAndroid Build Coastguard Worker   void OnLoaded(base::TimeTicks beginning_time,
441*6777b538SAndroid Build Coastguard Worker                 std::vector<std::unique_ptr<CanonicalCookie>> cookies);
442*6777b538SAndroid Build Coastguard Worker 
443*6777b538SAndroid Build Coastguard Worker   // Stores cookies loaded from the backing store and invokes the deferred
444*6777b538SAndroid Build Coastguard Worker   // task(s) pending loading of cookies associated with the domain key
445*6777b538SAndroid Build Coastguard Worker   // (GetKey, roughly eTLD+1). Called when all cookies for the domain key have
446*6777b538SAndroid Build Coastguard Worker   // been loaded from DB. See PersistentCookieStore::Load for details on the
447*6777b538SAndroid Build Coastguard Worker   // contents of cookies.
448*6777b538SAndroid Build Coastguard Worker   void OnKeyLoaded(const std::string& key,
449*6777b538SAndroid Build Coastguard Worker                    std::vector<std::unique_ptr<CanonicalCookie>> cookies);
450*6777b538SAndroid Build Coastguard Worker 
451*6777b538SAndroid Build Coastguard Worker   // Stores the loaded cookies.
452*6777b538SAndroid Build Coastguard Worker   void StoreLoadedCookies(
453*6777b538SAndroid Build Coastguard Worker       std::vector<std::unique_ptr<CanonicalCookie>> cookies);
454*6777b538SAndroid Build Coastguard Worker 
455*6777b538SAndroid Build Coastguard Worker   // Invokes deferred calls.
456*6777b538SAndroid Build Coastguard Worker   void InvokeQueue();
457*6777b538SAndroid Build Coastguard Worker 
458*6777b538SAndroid Build Coastguard Worker   // Checks that |cookies_| matches our invariants, and tries to repair any
459*6777b538SAndroid Build Coastguard Worker   // inconsistencies. (In other words, it does not have duplicate cookies).
460*6777b538SAndroid Build Coastguard Worker   void EnsureCookiesMapIsValid();
461*6777b538SAndroid Build Coastguard Worker 
462*6777b538SAndroid Build Coastguard Worker   // Checks for any duplicate cookies for CookieMap key |key| which lie between
463*6777b538SAndroid Build Coastguard Worker   // |begin| and |end|. If any are found, all but the most recent are deleted.
464*6777b538SAndroid Build Coastguard Worker   //
465*6777b538SAndroid Build Coastguard Worker   // If |cookie_partition_it| is not nullopt, then this function trims cookies
466*6777b538SAndroid Build Coastguard Worker   // from the CookieMap in |partitioned_cookies_| at |cookie_partition_it|
467*6777b538SAndroid Build Coastguard Worker   // instead of trimming cookies from |cookies_|.
468*6777b538SAndroid Build Coastguard Worker   void TrimDuplicateCookiesForKey(
469*6777b538SAndroid Build Coastguard Worker       const std::string& key,
470*6777b538SAndroid Build Coastguard Worker       CookieMap::iterator begin,
471*6777b538SAndroid Build Coastguard Worker       CookieMap::iterator end,
472*6777b538SAndroid Build Coastguard Worker       std::optional<PartitionedCookieMap::iterator> cookie_partition_it);
473*6777b538SAndroid Build Coastguard Worker 
474*6777b538SAndroid Build Coastguard Worker   void SetDefaultCookieableSchemes();
475*6777b538SAndroid Build Coastguard Worker 
476*6777b538SAndroid Build Coastguard Worker   std::vector<CanonicalCookie*> FindCookiesForRegistryControlledHost(
477*6777b538SAndroid Build Coastguard Worker       const GURL& url,
478*6777b538SAndroid Build Coastguard Worker       CookieMap* cookie_map = nullptr,
479*6777b538SAndroid Build Coastguard Worker       PartitionedCookieMap::iterator* partition_it = nullptr);
480*6777b538SAndroid Build Coastguard Worker 
481*6777b538SAndroid Build Coastguard Worker   std::vector<CanonicalCookie*> FindPartitionedCookiesForRegistryControlledHost(
482*6777b538SAndroid Build Coastguard Worker       const CookiePartitionKey& cookie_partition_key,
483*6777b538SAndroid Build Coastguard Worker       const GURL& url);
484*6777b538SAndroid Build Coastguard Worker 
485*6777b538SAndroid Build Coastguard Worker   void FilterCookiesWithOptions(const GURL url,
486*6777b538SAndroid Build Coastguard Worker                                 const CookieOptions options,
487*6777b538SAndroid Build Coastguard Worker                                 std::vector<CanonicalCookie*>* cookie_ptrs,
488*6777b538SAndroid Build Coastguard Worker                                 CookieAccessResultList* included_cookies,
489*6777b538SAndroid Build Coastguard Worker                                 CookieAccessResultList* excluded_cookies);
490*6777b538SAndroid Build Coastguard Worker 
491*6777b538SAndroid Build Coastguard Worker   // Possibly delete an existing cookie equivalent to |cookie_being_set| (same
492*6777b538SAndroid Build Coastguard Worker   // path, domain, and name).
493*6777b538SAndroid Build Coastguard Worker   //
494*6777b538SAndroid Build Coastguard Worker   // |allowed_to_set_secure_cookie| indicates if the source may override
495*6777b538SAndroid Build Coastguard Worker   // existing secure cookies. If the source is not trustworthy, and there is an
496*6777b538SAndroid Build Coastguard Worker   // existing "equivalent" cookie that is Secure, that cookie will be preserved,
497*6777b538SAndroid Build Coastguard Worker   // under "Leave Secure Cookies Alone" (see
498*6777b538SAndroid Build Coastguard Worker   // https://tools.ietf.org/html/draft-ietf-httpbis-cookie-alone-01).
499*6777b538SAndroid Build Coastguard Worker   // ("equivalent" here is in quotes because the equivalency check for the
500*6777b538SAndroid Build Coastguard Worker   // purposes of preserving existing Secure cookies is slightly more inclusive.)
501*6777b538SAndroid Build Coastguard Worker   //
502*6777b538SAndroid Build Coastguard Worker   // If |skip_httponly| is true, httponly cookies will not be deleted even if
503*6777b538SAndroid Build Coastguard Worker   // they are equivalent.
504*6777b538SAndroid Build Coastguard Worker   // |key| is the key to find the cookie in cookies_; see the comment before the
505*6777b538SAndroid Build Coastguard Worker   // CookieMap typedef for details.
506*6777b538SAndroid Build Coastguard Worker   //
507*6777b538SAndroid Build Coastguard Worker   // If a cookie is deleted, and its value matches |cookie_being_set|'s value,
508*6777b538SAndroid Build Coastguard Worker   // then |creation_date_to_inherit| will be set to that cookie's creation date.
509*6777b538SAndroid Build Coastguard Worker   //
510*6777b538SAndroid Build Coastguard Worker   // The cookie will not be deleted if |*status| is not "include" when calling
511*6777b538SAndroid Build Coastguard Worker   // the function. The function will update |*status| with exclusion reasons if
512*6777b538SAndroid Build Coastguard Worker   // a secure cookie was skipped or an httponly cookie was skipped.
513*6777b538SAndroid Build Coastguard Worker   //
514*6777b538SAndroid Build Coastguard Worker   // If |cookie_partition_it| is nullopt, it will search |cookies_| for
515*6777b538SAndroid Build Coastguard Worker   // duplicates of |cookie_being_set|. Otherwise, |cookie_partition_it|'s value
516*6777b538SAndroid Build Coastguard Worker   // is the iterator of the CookieMap in |partitioned_cookies_| we should search
517*6777b538SAndroid Build Coastguard Worker   // for duplicates.
518*6777b538SAndroid Build Coastguard Worker   //
519*6777b538SAndroid Build Coastguard Worker   // NOTE: There should never be more than a single matching equivalent cookie.
520*6777b538SAndroid Build Coastguard Worker   void MaybeDeleteEquivalentCookieAndUpdateStatus(
521*6777b538SAndroid Build Coastguard Worker       const std::string& key,
522*6777b538SAndroid Build Coastguard Worker       const CanonicalCookie& cookie_being_set,
523*6777b538SAndroid Build Coastguard Worker       bool allowed_to_set_secure_cookie,
524*6777b538SAndroid Build Coastguard Worker       bool skip_httponly,
525*6777b538SAndroid Build Coastguard Worker       bool already_expired,
526*6777b538SAndroid Build Coastguard Worker       base::Time* creation_date_to_inherit,
527*6777b538SAndroid Build Coastguard Worker       CookieInclusionStatus* status,
528*6777b538SAndroid Build Coastguard Worker       std::optional<PartitionedCookieMap::iterator> cookie_partition_it);
529*6777b538SAndroid Build Coastguard Worker 
530*6777b538SAndroid Build Coastguard Worker   // Inserts `cc` into cookies_. Returns an iterator that points to the inserted
531*6777b538SAndroid Build Coastguard Worker   // cookie in `cookies_`. Guarantee: all iterators to `cookies_` remain valid.
532*6777b538SAndroid Build Coastguard Worker   // Dispatches the change to `change_dispatcher_` iff `dispatch_change` is
533*6777b538SAndroid Build Coastguard Worker   // true.
534*6777b538SAndroid Build Coastguard Worker   CookieMap::iterator InternalInsertCookie(
535*6777b538SAndroid Build Coastguard Worker       const std::string& key,
536*6777b538SAndroid Build Coastguard Worker       std::unique_ptr<CanonicalCookie> cc,
537*6777b538SAndroid Build Coastguard Worker       bool sync_to_store,
538*6777b538SAndroid Build Coastguard Worker       const CookieAccessResult& access_result,
539*6777b538SAndroid Build Coastguard Worker       bool dispatch_change = true);
540*6777b538SAndroid Build Coastguard Worker 
541*6777b538SAndroid Build Coastguard Worker   // Returns true if the cookie should be (or is already) synced to the store.
542*6777b538SAndroid Build Coastguard Worker   // Used for cookies during insertion and deletion into the in-memory store.
543*6777b538SAndroid Build Coastguard Worker   bool ShouldUpdatePersistentStore(CanonicalCookie* cc);
544*6777b538SAndroid Build Coastguard Worker 
545*6777b538SAndroid Build Coastguard Worker   // Inserts `cc` into partitioned_cookies_. Should only be used when
546*6777b538SAndroid Build Coastguard Worker   // cc->IsPartitioned() is true.
547*6777b538SAndroid Build Coastguard Worker   PartitionedCookieMapIterators InternalInsertPartitionedCookie(
548*6777b538SAndroid Build Coastguard Worker       std::string key,
549*6777b538SAndroid Build Coastguard Worker       std::unique_ptr<CanonicalCookie> cc,
550*6777b538SAndroid Build Coastguard Worker       bool sync_to_store,
551*6777b538SAndroid Build Coastguard Worker       const CookieAccessResult& access_result,
552*6777b538SAndroid Build Coastguard Worker       bool dispatch_change = true);
553*6777b538SAndroid Build Coastguard Worker 
554*6777b538SAndroid Build Coastguard Worker   // Sets all cookies from |list| after deleting any equivalent cookie.
555*6777b538SAndroid Build Coastguard Worker   // For data gathering purposes, this routine is treated as if it is
556*6777b538SAndroid Build Coastguard Worker   // restoring saved cookies; some statistics are not gathered in this case.
557*6777b538SAndroid Build Coastguard Worker   void SetAllCookies(CookieList list, SetCookiesCallback callback);
558*6777b538SAndroid Build Coastguard Worker 
559*6777b538SAndroid Build Coastguard Worker   void InternalUpdateCookieAccessTime(CanonicalCookie* cc,
560*6777b538SAndroid Build Coastguard Worker                                       const base::Time& current_time);
561*6777b538SAndroid Build Coastguard Worker 
562*6777b538SAndroid Build Coastguard Worker   // |deletion_cause| argument is used for collecting statistics and choosing
563*6777b538SAndroid Build Coastguard Worker   // the correct CookieChangeCause for OnCookieChange notifications. Guarantee:
564*6777b538SAndroid Build Coastguard Worker   // All iterators to cookies_, except for the deleted entry, remain valid.
565*6777b538SAndroid Build Coastguard Worker   void InternalDeleteCookie(CookieMap::iterator it,
566*6777b538SAndroid Build Coastguard Worker                             bool sync_to_store,
567*6777b538SAndroid Build Coastguard Worker                             DeletionCause deletion_cause);
568*6777b538SAndroid Build Coastguard Worker 
569*6777b538SAndroid Build Coastguard Worker   // Deletes a Partitioned cookie. Returns true if the deletion operation
570*6777b538SAndroid Build Coastguard Worker   // resulted in the CookieMap the cookie was stored in was deleted.
571*6777b538SAndroid Build Coastguard Worker   //
572*6777b538SAndroid Build Coastguard Worker   // If the CookieMap which contains the deleted cookie only has one entry, then
573*6777b538SAndroid Build Coastguard Worker   // this function will also delete the CookieMap from PartitionedCookieMap.
574*6777b538SAndroid Build Coastguard Worker   // This may invalidate the |cookie_partition_it| argument.
575*6777b538SAndroid Build Coastguard Worker   void InternalDeletePartitionedCookie(
576*6777b538SAndroid Build Coastguard Worker       PartitionedCookieMap::iterator partition_it,
577*6777b538SAndroid Build Coastguard Worker       CookieMap::iterator cookie_it,
578*6777b538SAndroid Build Coastguard Worker       bool sync_to_store,
579*6777b538SAndroid Build Coastguard Worker       DeletionCause deletion_cause);
580*6777b538SAndroid Build Coastguard Worker 
581*6777b538SAndroid Build Coastguard Worker   // If the number of cookies for CookieMap key |key|, or globally, are
582*6777b538SAndroid Build Coastguard Worker   // over the preset maximums above, garbage collect, first for the host and
583*6777b538SAndroid Build Coastguard Worker   // then globally.  See comments above garbage collection threshold
584*6777b538SAndroid Build Coastguard Worker   // constants for details. Also removes expired cookies.
585*6777b538SAndroid Build Coastguard Worker   //
586*6777b538SAndroid Build Coastguard Worker   // Returns the number of cookies deleted (useful for debugging).
587*6777b538SAndroid Build Coastguard Worker   size_t GarbageCollect(const base::Time& current, const std::string& key);
588*6777b538SAndroid Build Coastguard Worker 
589*6777b538SAndroid Build Coastguard Worker   // Run garbage collection for PartitionedCookieMap keys |cookie_partition_key|
590*6777b538SAndroid Build Coastguard Worker   // and |key|.
591*6777b538SAndroid Build Coastguard Worker   //
592*6777b538SAndroid Build Coastguard Worker   // Partitioned cookies are subject to different limits than unpartitioned
593*6777b538SAndroid Build Coastguard Worker   // cookies in order to prevent leaking entropy about user behavior across
594*6777b538SAndroid Build Coastguard Worker   // cookie partitions.
595*6777b538SAndroid Build Coastguard Worker   size_t GarbageCollectPartitionedCookies(
596*6777b538SAndroid Build Coastguard Worker       const base::Time& current,
597*6777b538SAndroid Build Coastguard Worker       const CookiePartitionKey& cookie_partition_key,
598*6777b538SAndroid Build Coastguard Worker       const std::string& key);
599*6777b538SAndroid Build Coastguard Worker 
600*6777b538SAndroid Build Coastguard Worker   // Helper for GarbageCollect(). Deletes up to |purge_goal| cookies with a
601*6777b538SAndroid Build Coastguard Worker   // priority less than or equal to |priority| from |cookies|, while ensuring
602*6777b538SAndroid Build Coastguard Worker   // that at least the |to_protect| most-recent cookies are retained.
603*6777b538SAndroid Build Coastguard Worker   // |protected_secure_cookies| specifies whether or not secure cookies should
604*6777b538SAndroid Build Coastguard Worker   // be protected from deletion.
605*6777b538SAndroid Build Coastguard Worker   //
606*6777b538SAndroid Build Coastguard Worker   // |cookies| must be sorted from least-recent to most-recent.
607*6777b538SAndroid Build Coastguard Worker   //
608*6777b538SAndroid Build Coastguard Worker   // Returns the number of cookies deleted.
609*6777b538SAndroid Build Coastguard Worker   size_t PurgeLeastRecentMatches(CookieItVector* cookies,
610*6777b538SAndroid Build Coastguard Worker                                  CookiePriority priority,
611*6777b538SAndroid Build Coastguard Worker                                  size_t to_protect,
612*6777b538SAndroid Build Coastguard Worker                                  size_t purge_goal,
613*6777b538SAndroid Build Coastguard Worker                                  bool protect_secure_cookies);
614*6777b538SAndroid Build Coastguard Worker   // Same as above except that for a given {priority, secureness} tuple domain
615*6777b538SAndroid Build Coastguard Worker   // cookies will be deleted before host cookies.
616*6777b538SAndroid Build Coastguard Worker   size_t PurgeLeastRecentMatchesForOBC(CookieItList* cookies,
617*6777b538SAndroid Build Coastguard Worker                                        CookiePriority priority,
618*6777b538SAndroid Build Coastguard Worker                                        size_t to_protect,
619*6777b538SAndroid Build Coastguard Worker                                        size_t purge_goal,
620*6777b538SAndroid Build Coastguard Worker                                        bool protect_secure_cookies);
621*6777b538SAndroid Build Coastguard Worker 
622*6777b538SAndroid Build Coastguard Worker   // Helper for GarbageCollect(); can be called directly as well.  Deletes all
623*6777b538SAndroid Build Coastguard Worker   // expired cookies in |itpair|.  If |cookie_its| is non-NULL, all the
624*6777b538SAndroid Build Coastguard Worker   // non-expired cookies from |itpair| are appended to |cookie_its|.
625*6777b538SAndroid Build Coastguard Worker   //
626*6777b538SAndroid Build Coastguard Worker   // Returns the number of cookies deleted.
627*6777b538SAndroid Build Coastguard Worker   size_t GarbageCollectExpired(const base::Time& current,
628*6777b538SAndroid Build Coastguard Worker                                const CookieMapItPair& itpair,
629*6777b538SAndroid Build Coastguard Worker                                CookieItVector* cookie_its);
630*6777b538SAndroid Build Coastguard Worker 
631*6777b538SAndroid Build Coastguard Worker   // Deletes all expired cookies in the double-keyed PartitionedCookie map in
632*6777b538SAndroid Build Coastguard Worker   // the CookieMap at |cookie_partition_it|. It deletes all cookies in that
633*6777b538SAndroid Build Coastguard Worker   // CookieMap in |itpair|. If |cookie_its| is non-NULL, all non-expired cookies
634*6777b538SAndroid Build Coastguard Worker   // from |itpair| are appended to |cookie_its|.
635*6777b538SAndroid Build Coastguard Worker   //
636*6777b538SAndroid Build Coastguard Worker   // Returns the number of cookies deleted.
637*6777b538SAndroid Build Coastguard Worker   size_t GarbageCollectExpiredPartitionedCookies(
638*6777b538SAndroid Build Coastguard Worker       const base::Time& current,
639*6777b538SAndroid Build Coastguard Worker       const PartitionedCookieMap::iterator& cookie_partition_it,
640*6777b538SAndroid Build Coastguard Worker       const CookieMapItPair& itpair,
641*6777b538SAndroid Build Coastguard Worker       CookieItVector* cookie_its);
642*6777b538SAndroid Build Coastguard Worker 
643*6777b538SAndroid Build Coastguard Worker   // Helper function to garbage collect all expired cookies in
644*6777b538SAndroid Build Coastguard Worker   // PartitionedCookieMap.
645*6777b538SAndroid Build Coastguard Worker   void GarbageCollectAllExpiredPartitionedCookies(const base::Time& current);
646*6777b538SAndroid Build Coastguard Worker 
647*6777b538SAndroid Build Coastguard Worker   // Helper for GarbageCollect(). Deletes all cookies in the range specified by
648*6777b538SAndroid Build Coastguard Worker   // [|it_begin|, |it_end|). Returns the number of cookies deleted.
649*6777b538SAndroid Build Coastguard Worker   size_t GarbageCollectDeleteRange(const base::Time& current,
650*6777b538SAndroid Build Coastguard Worker                                    DeletionCause cause,
651*6777b538SAndroid Build Coastguard Worker                                    CookieItVector::iterator cookie_its_begin,
652*6777b538SAndroid Build Coastguard Worker                                    CookieItVector::iterator cookie_its_end);
653*6777b538SAndroid Build Coastguard Worker 
654*6777b538SAndroid Build Coastguard Worker   // Helper for GarbageCollect(). Deletes cookies in |cookie_its| from least to
655*6777b538SAndroid Build Coastguard Worker   // most recently used, but only before |safe_date|. Also will stop deleting
656*6777b538SAndroid Build Coastguard Worker   // when the number of remaining cookies hits |purge_goal|.
657*6777b538SAndroid Build Coastguard Worker   //
658*6777b538SAndroid Build Coastguard Worker   // Sets |earliest_time| to be the earliest last access time of a cookie that
659*6777b538SAndroid Build Coastguard Worker   // was not deleted, or base::Time() if no such cookie exists.
660*6777b538SAndroid Build Coastguard Worker   size_t GarbageCollectLeastRecentlyAccessed(const base::Time& current,
661*6777b538SAndroid Build Coastguard Worker                                              const base::Time& safe_date,
662*6777b538SAndroid Build Coastguard Worker                                              size_t purge_goal,
663*6777b538SAndroid Build Coastguard Worker                                              CookieItVector cookie_its,
664*6777b538SAndroid Build Coastguard Worker                                              base::Time* earliest_time);
665*6777b538SAndroid Build Coastguard Worker 
666*6777b538SAndroid Build Coastguard Worker   bool HasCookieableScheme(const GURL& url);
667*6777b538SAndroid Build Coastguard Worker 
668*6777b538SAndroid Build Coastguard Worker   // Get the cookie's access semantics (LEGACY or NONLEGACY), by checking for a
669*6777b538SAndroid Build Coastguard Worker   // value from the cookie access delegate, if it is non-null. Otherwise returns
670*6777b538SAndroid Build Coastguard Worker   // UNKNOWN.
671*6777b538SAndroid Build Coastguard Worker   CookieAccessSemantics GetAccessSemanticsForCookie(
672*6777b538SAndroid Build Coastguard Worker       const CanonicalCookie& cookie) const;
673*6777b538SAndroid Build Coastguard Worker 
674*6777b538SAndroid Build Coastguard Worker   // Statistics support
675*6777b538SAndroid Build Coastguard Worker 
676*6777b538SAndroid Build Coastguard Worker   // This function should be called repeatedly, and will record
677*6777b538SAndroid Build Coastguard Worker   // statistics if a sufficient time period has passed.
678*6777b538SAndroid Build Coastguard Worker   void RecordPeriodicStats(const base::Time& current_time);
679*6777b538SAndroid Build Coastguard Worker 
680*6777b538SAndroid Build Coastguard Worker   // Records the aforementioned stats if we have already finished loading all
681*6777b538SAndroid Build Coastguard Worker   // cookies. Returns whether stats were recorded.
682*6777b538SAndroid Build Coastguard Worker   bool DoRecordPeriodicStats();
683*6777b538SAndroid Build Coastguard Worker 
684*6777b538SAndroid Build Coastguard Worker   // Records periodic stats related to First-Party Sets usage. Note that since
685*6777b538SAndroid Build Coastguard Worker   // First-Party Sets presents a potentially asynchronous interface, these stats
686*6777b538SAndroid Build Coastguard Worker   // may be collected asynchronously w.r.t. the rest of the stats collected by
687*6777b538SAndroid Build Coastguard Worker   // `RecordPeriodicStats`.
688*6777b538SAndroid Build Coastguard Worker   void RecordPeriodicFirstPartySetsStats(
689*6777b538SAndroid Build Coastguard Worker       base::flat_map<SchemefulSite, FirstPartySetEntry> sets) const;
690*6777b538SAndroid Build Coastguard Worker 
691*6777b538SAndroid Build Coastguard Worker   // Defers the callback until the full coookie database has been loaded. If
692*6777b538SAndroid Build Coastguard Worker   // it's already been loaded, runs the callback synchronously.
693*6777b538SAndroid Build Coastguard Worker   void DoCookieCallback(base::OnceClosure callback);
694*6777b538SAndroid Build Coastguard Worker 
695*6777b538SAndroid Build Coastguard Worker   // Defers the callback until the cookies relevant to given URL have been
696*6777b538SAndroid Build Coastguard Worker   // loaded. If they've already been loaded, runs the callback synchronously.
697*6777b538SAndroid Build Coastguard Worker   void DoCookieCallbackForURL(base::OnceClosure callback, const GURL& url);
698*6777b538SAndroid Build Coastguard Worker 
699*6777b538SAndroid Build Coastguard Worker   // Defers the callback until the cookies relevant to given host or domain
700*6777b538SAndroid Build Coastguard Worker   // have been loaded. If they've already been loaded, runs the callback
701*6777b538SAndroid Build Coastguard Worker   // synchronously.
702*6777b538SAndroid Build Coastguard Worker   void DoCookieCallbackForHostOrDomain(base::OnceClosure callback,
703*6777b538SAndroid Build Coastguard Worker                                        std::string_view host_or_domain);
704*6777b538SAndroid Build Coastguard Worker 
705*6777b538SAndroid Build Coastguard Worker   // Checks to see if a cookie is being sent to the same port it was set by. For
706*6777b538SAndroid Build Coastguard Worker   // metrics.
707*6777b538SAndroid Build Coastguard Worker   //
708*6777b538SAndroid Build Coastguard Worker   // This is in CookieMonster because only CookieMonster uses it. It's otherwise
709*6777b538SAndroid Build Coastguard Worker   // a standalone utility function.
710*6777b538SAndroid Build Coastguard Worker   static CookieSentToSamePort IsCookieSentToSamePortThatSetIt(
711*6777b538SAndroid Build Coastguard Worker       const GURL& destination,
712*6777b538SAndroid Build Coastguard Worker       int source_port,
713*6777b538SAndroid Build Coastguard Worker       CookieSourceScheme source_scheme);
714*6777b538SAndroid Build Coastguard Worker 
715*6777b538SAndroid Build Coastguard Worker   // Set of keys (eTLD+1's) for which non-expired cookies have
716*6777b538SAndroid Build Coastguard Worker   // been evicted for hitting the per-domain max. The size of this set is
717*6777b538SAndroid Build Coastguard Worker   // histogrammed periodically. The size is limited to |kMaxDomainPurgedKeys|.
718*6777b538SAndroid Build Coastguard Worker   std::set<std::string> domain_purged_keys_ GUARDED_BY_CONTEXT(thread_checker_);
719*6777b538SAndroid Build Coastguard Worker 
720*6777b538SAndroid Build Coastguard Worker   // The number of distinct keys (eTLD+1's) currently present in the |cookies_|
721*6777b538SAndroid Build Coastguard Worker   // multimap. This is histogrammed periodically.
722*6777b538SAndroid Build Coastguard Worker   size_t num_keys_ = 0u;
723*6777b538SAndroid Build Coastguard Worker 
724*6777b538SAndroid Build Coastguard Worker   CookieMap cookies_ GUARDED_BY_CONTEXT(thread_checker_);
725*6777b538SAndroid Build Coastguard Worker 
726*6777b538SAndroid Build Coastguard Worker   PartitionedCookieMap partitioned_cookies_ GUARDED_BY_CONTEXT(thread_checker_);
727*6777b538SAndroid Build Coastguard Worker 
728*6777b538SAndroid Build Coastguard Worker   // Number of distinct partitioned cookies globally. This is used to enforce a
729*6777b538SAndroid Build Coastguard Worker   // global maximum on the number of partitioned cookies.
730*6777b538SAndroid Build Coastguard Worker   size_t num_partitioned_cookies_ = 0u;
731*6777b538SAndroid Build Coastguard Worker   // Number of partitioned cookies whose partition key has a nonce.
732*6777b538SAndroid Build Coastguard Worker   size_t num_nonced_partitioned_cookies_ = 0u;
733*6777b538SAndroid Build Coastguard Worker 
734*6777b538SAndroid Build Coastguard Worker   // Number of bytes used by the partitioned cookie jar.
735*6777b538SAndroid Build Coastguard Worker   size_t num_partitioned_cookies_bytes_ = 0u;
736*6777b538SAndroid Build Coastguard Worker   // Number of bytes used by partitioned cookies whose partition key has a
737*6777b538SAndroid Build Coastguard Worker   // nonce.
738*6777b538SAndroid Build Coastguard Worker   size_t num_nonced_partitioned_cookie_bytes_ = 0u;
739*6777b538SAndroid Build Coastguard Worker   // Cookie jar sizes per partition.
740*6777b538SAndroid Build Coastguard Worker   std::map<CookiePartitionKey, size_t> bytes_per_cookie_partition_;
741*6777b538SAndroid Build Coastguard Worker 
742*6777b538SAndroid Build Coastguard Worker   CookieMonsterChangeDispatcher change_dispatcher_;
743*6777b538SAndroid Build Coastguard Worker 
744*6777b538SAndroid Build Coastguard Worker   // Indicates whether the cookie store has been initialized.
745*6777b538SAndroid Build Coastguard Worker   bool initialized_ = false;
746*6777b538SAndroid Build Coastguard Worker 
747*6777b538SAndroid Build Coastguard Worker   // Indicates whether the cookie store has started fetching all cookies.
748*6777b538SAndroid Build Coastguard Worker   bool started_fetching_all_cookies_ = false;
749*6777b538SAndroid Build Coastguard Worker   // Indicates whether the cookie store has finished fetching all cookies.
750*6777b538SAndroid Build Coastguard Worker   bool finished_fetching_all_cookies_ = false;
751*6777b538SAndroid Build Coastguard Worker 
752*6777b538SAndroid Build Coastguard Worker   // List of domain keys that have been loaded from the DB.
753*6777b538SAndroid Build Coastguard Worker   std::set<std::string> keys_loaded_;
754*6777b538SAndroid Build Coastguard Worker 
755*6777b538SAndroid Build Coastguard Worker   // Map of domain keys to their associated task queues. These tasks are blocked
756*6777b538SAndroid Build Coastguard Worker   // until all cookies for the associated domain key eTLD+1 are loaded from the
757*6777b538SAndroid Build Coastguard Worker   // backend store.
758*6777b538SAndroid Build Coastguard Worker   std::map<std::string, base::circular_deque<base::OnceClosure>>
759*6777b538SAndroid Build Coastguard Worker       tasks_pending_for_key_ GUARDED_BY_CONTEXT(thread_checker_);
760*6777b538SAndroid Build Coastguard Worker 
761*6777b538SAndroid Build Coastguard Worker   // Queues tasks that are blocked until all cookies are loaded from the backend
762*6777b538SAndroid Build Coastguard Worker   // store.
763*6777b538SAndroid Build Coastguard Worker   base::circular_deque<base::OnceClosure> tasks_pending_
764*6777b538SAndroid Build Coastguard Worker       GUARDED_BY_CONTEXT(thread_checker_);
765*6777b538SAndroid Build Coastguard Worker 
766*6777b538SAndroid Build Coastguard Worker   // Once a global cookie task has been seen, all per-key tasks must be put in
767*6777b538SAndroid Build Coastguard Worker   // |tasks_pending_| instead of |tasks_pending_for_key_| to ensure a reasonable
768*6777b538SAndroid Build Coastguard Worker   // view of the cookie store. This is more to ensure fancy cookie export/import
769*6777b538SAndroid Build Coastguard Worker   // code has a consistent view of the CookieStore, rather than out of concern
770*6777b538SAndroid Build Coastguard Worker   // for typical use.
771*6777b538SAndroid Build Coastguard Worker   bool seen_global_task_ = false;
772*6777b538SAndroid Build Coastguard Worker 
773*6777b538SAndroid Build Coastguard Worker   NetLogWithSource net_log_;
774*6777b538SAndroid Build Coastguard Worker 
775*6777b538SAndroid Build Coastguard Worker   scoped_refptr<PersistentCookieStore> store_;
776*6777b538SAndroid Build Coastguard Worker 
777*6777b538SAndroid Build Coastguard Worker   // Minimum delay after updating a cookie's LastAccessDate before we will
778*6777b538SAndroid Build Coastguard Worker   // update it again.
779*6777b538SAndroid Build Coastguard Worker   const base::TimeDelta last_access_threshold_;
780*6777b538SAndroid Build Coastguard Worker 
781*6777b538SAndroid Build Coastguard Worker   // Approximate date of access time of least recently accessed cookie
782*6777b538SAndroid Build Coastguard Worker   // in |cookies_|.  Note that this is not guaranteed to be accurate, only a)
783*6777b538SAndroid Build Coastguard Worker   // to be before or equal to the actual time, and b) to be accurate
784*6777b538SAndroid Build Coastguard Worker   // immediately after a garbage collection that scans through all the cookies
785*6777b538SAndroid Build Coastguard Worker   // (When garbage collection does not scan through all cookies, it may not be
786*6777b538SAndroid Build Coastguard Worker   // updated). This value is used to determine whether global garbage collection
787*6777b538SAndroid Build Coastguard Worker   // might find cookies to purge. Note: The default Time() constructor will
788*6777b538SAndroid Build Coastguard Worker   // create a value that compares earlier than any other time value, which is
789*6777b538SAndroid Build Coastguard Worker   // wanted.  Thus this value is not initialized.
790*6777b538SAndroid Build Coastguard Worker   base::Time earliest_access_time_;
791*6777b538SAndroid Build Coastguard Worker 
792*6777b538SAndroid Build Coastguard Worker   std::vector<std::string> cookieable_schemes_;
793*6777b538SAndroid Build Coastguard Worker 
794*6777b538SAndroid Build Coastguard Worker   base::Time last_statistic_record_time_;
795*6777b538SAndroid Build Coastguard Worker 
796*6777b538SAndroid Build Coastguard Worker   bool persist_session_cookies_ = false;
797*6777b538SAndroid Build Coastguard Worker 
798*6777b538SAndroid Build Coastguard Worker   THREAD_CHECKER(thread_checker_);
799*6777b538SAndroid Build Coastguard Worker 
800*6777b538SAndroid Build Coastguard Worker   base::WeakPtrFactory<CookieMonster> weak_ptr_factory_{this};
801*6777b538SAndroid Build Coastguard Worker };
802*6777b538SAndroid Build Coastguard Worker 
803*6777b538SAndroid Build Coastguard Worker typedef base::RefCountedThreadSafe<CookieMonster::PersistentCookieStore>
804*6777b538SAndroid Build Coastguard Worker     RefcountedPersistentCookieStore;
805*6777b538SAndroid Build Coastguard Worker 
806*6777b538SAndroid Build Coastguard Worker class NET_EXPORT CookieMonster::PersistentCookieStore
807*6777b538SAndroid Build Coastguard Worker     : public RefcountedPersistentCookieStore {
808*6777b538SAndroid Build Coastguard Worker  public:
809*6777b538SAndroid Build Coastguard Worker   typedef base::OnceCallback<void(
810*6777b538SAndroid Build Coastguard Worker       std::vector<std::unique_ptr<CanonicalCookie>>)>
811*6777b538SAndroid Build Coastguard Worker       LoadedCallback;
812*6777b538SAndroid Build Coastguard Worker 
813*6777b538SAndroid Build Coastguard Worker   PersistentCookieStore(const PersistentCookieStore&) = delete;
814*6777b538SAndroid Build Coastguard Worker   PersistentCookieStore& operator=(const PersistentCookieStore&) = delete;
815*6777b538SAndroid Build Coastguard Worker 
816*6777b538SAndroid Build Coastguard Worker   // Initializes the store and retrieves the existing cookies. This will be
817*6777b538SAndroid Build Coastguard Worker   // called only once at startup. The callback will return all the cookies
818*6777b538SAndroid Build Coastguard Worker   // that are not yet returned to CookieMonster by previous priority loads.
819*6777b538SAndroid Build Coastguard Worker   //
820*6777b538SAndroid Build Coastguard Worker   // |loaded_callback| may not be NULL.
821*6777b538SAndroid Build Coastguard Worker   // |net_log| is a NetLogWithSource that may be copied if the persistent
822*6777b538SAndroid Build Coastguard Worker   // store wishes to log NetLog events.
823*6777b538SAndroid Build Coastguard Worker   virtual void Load(LoadedCallback loaded_callback,
824*6777b538SAndroid Build Coastguard Worker                     const NetLogWithSource& net_log) = 0;
825*6777b538SAndroid Build Coastguard Worker 
826*6777b538SAndroid Build Coastguard Worker   // Does a priority load of all cookies for the domain key (eTLD+1). The
827*6777b538SAndroid Build Coastguard Worker   // callback will return all the cookies that are not yet returned by previous
828*6777b538SAndroid Build Coastguard Worker   // loads, which includes cookies for the requested domain key if they are not
829*6777b538SAndroid Build Coastguard Worker   // already returned, plus all cookies that are chain-loaded and not yet
830*6777b538SAndroid Build Coastguard Worker   // returned to CookieMonster.
831*6777b538SAndroid Build Coastguard Worker   //
832*6777b538SAndroid Build Coastguard Worker   // |loaded_callback| may not be NULL.
833*6777b538SAndroid Build Coastguard Worker   virtual void LoadCookiesForKey(const std::string& key,
834*6777b538SAndroid Build Coastguard Worker                                  LoadedCallback loaded_callback) = 0;
835*6777b538SAndroid Build Coastguard Worker 
836*6777b538SAndroid Build Coastguard Worker   virtual void AddCookie(const CanonicalCookie& cc) = 0;
837*6777b538SAndroid Build Coastguard Worker   virtual void UpdateCookieAccessTime(const CanonicalCookie& cc) = 0;
838*6777b538SAndroid Build Coastguard Worker   virtual void DeleteCookie(const CanonicalCookie& cc) = 0;
839*6777b538SAndroid Build Coastguard Worker 
840*6777b538SAndroid Build Coastguard Worker   // Instructs the store to not discard session only cookies on shutdown.
841*6777b538SAndroid Build Coastguard Worker   virtual void SetForceKeepSessionState() = 0;
842*6777b538SAndroid Build Coastguard Worker 
843*6777b538SAndroid Build Coastguard Worker   // Sets a callback that will be run before the store flushes.  If |callback|
844*6777b538SAndroid Build Coastguard Worker   // performs any async operations, the store will not wait for those to finish
845*6777b538SAndroid Build Coastguard Worker   // before flushing.
846*6777b538SAndroid Build Coastguard Worker   virtual void SetBeforeCommitCallback(base::RepeatingClosure callback) = 0;
847*6777b538SAndroid Build Coastguard Worker 
848*6777b538SAndroid Build Coastguard Worker   // Flushes the store and posts |callback| when complete. |callback| may be
849*6777b538SAndroid Build Coastguard Worker   // NULL.
850*6777b538SAndroid Build Coastguard Worker   virtual void Flush(base::OnceClosure callback) = 0;
851*6777b538SAndroid Build Coastguard Worker 
852*6777b538SAndroid Build Coastguard Worker  protected:
853*6777b538SAndroid Build Coastguard Worker   PersistentCookieStore() = default;
854*6777b538SAndroid Build Coastguard Worker   virtual ~PersistentCookieStore() = default;
855*6777b538SAndroid Build Coastguard Worker 
856*6777b538SAndroid Build Coastguard Worker  private:
857*6777b538SAndroid Build Coastguard Worker   friend class base::RefCountedThreadSafe<PersistentCookieStore>;
858*6777b538SAndroid Build Coastguard Worker };
859*6777b538SAndroid Build Coastguard Worker 
860*6777b538SAndroid Build Coastguard Worker }  // namespace net
861*6777b538SAndroid Build Coastguard Worker 
862*6777b538SAndroid Build Coastguard Worker #endif  // NET_COOKIES_COOKIE_MONSTER_H_
863