xref: /aosp_15_r20/external/cronet/net/cookies/cookie_store.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 number 42.
6*6777b538SAndroid Build Coastguard Worker 
7*6777b538SAndroid Build Coastguard Worker #ifndef NET_COOKIES_COOKIE_STORE_H_
8*6777b538SAndroid Build Coastguard Worker #define NET_COOKIES_COOKIE_STORE_H_
9*6777b538SAndroid Build Coastguard Worker 
10*6777b538SAndroid Build Coastguard Worker #include <stdint.h>
11*6777b538SAndroid Build Coastguard Worker 
12*6777b538SAndroid Build Coastguard Worker #include <memory>
13*6777b538SAndroid Build Coastguard Worker #include <optional>
14*6777b538SAndroid Build Coastguard Worker #include <string>
15*6777b538SAndroid Build Coastguard Worker #include <vector>
16*6777b538SAndroid Build Coastguard Worker 
17*6777b538SAndroid Build Coastguard Worker #include "base/functional/callback_forward.h"
18*6777b538SAndroid Build Coastguard Worker #include "net/base/net_export.h"
19*6777b538SAndroid Build Coastguard Worker #include "net/cookies/canonical_cookie.h"
20*6777b538SAndroid Build Coastguard Worker #include "net/cookies/cookie_access_delegate.h"
21*6777b538SAndroid Build Coastguard Worker #include "net/cookies/cookie_access_result.h"
22*6777b538SAndroid Build Coastguard Worker #include "net/cookies/cookie_deletion_info.h"
23*6777b538SAndroid Build Coastguard Worker #include "net/cookies/cookie_options.h"
24*6777b538SAndroid Build Coastguard Worker #include "net/cookies/cookie_partition_key_collection.h"
25*6777b538SAndroid Build Coastguard Worker 
26*6777b538SAndroid Build Coastguard Worker class GURL;
27*6777b538SAndroid Build Coastguard Worker 
28*6777b538SAndroid Build Coastguard Worker namespace net {
29*6777b538SAndroid Build Coastguard Worker 
30*6777b538SAndroid Build Coastguard Worker class CookieChangeDispatcher;
31*6777b538SAndroid Build Coastguard Worker 
32*6777b538SAndroid Build Coastguard Worker // An interface for storing and retrieving cookies. Implementations are not
33*6777b538SAndroid Build Coastguard Worker // thread safe, as with most other net classes. All methods must be invoked on
34*6777b538SAndroid Build Coastguard Worker // the network thread, and all callbacks will be called there.
35*6777b538SAndroid Build Coastguard Worker //
36*6777b538SAndroid Build Coastguard Worker // All async functions may either invoke the callback asynchronously, or they
37*6777b538SAndroid Build Coastguard Worker // may be invoked immediately (prior to return of the asynchronous function).
38*6777b538SAndroid Build Coastguard Worker // Destroying the CookieStore will cancel pending async callbacks.
39*6777b538SAndroid Build Coastguard Worker class NET_EXPORT CookieStore {
40*6777b538SAndroid Build Coastguard Worker  public:
41*6777b538SAndroid Build Coastguard Worker   // Callback definitions.
42*6777b538SAndroid Build Coastguard Worker   using GetCookieListCallback =
43*6777b538SAndroid Build Coastguard Worker       base::OnceCallback<void(const CookieAccessResultList& included_cookies,
44*6777b538SAndroid Build Coastguard Worker                               const CookieAccessResultList& excluded_list)>;
45*6777b538SAndroid Build Coastguard Worker   using GetAllCookiesCallback =
46*6777b538SAndroid Build Coastguard Worker       base::OnceCallback<void(const CookieList& cookies)>;
47*6777b538SAndroid Build Coastguard Worker   // |access_semantics_list| is guaranteed to the same length as |cookies|.
48*6777b538SAndroid Build Coastguard Worker   using GetAllCookiesWithAccessSemanticsCallback = base::OnceCallback<void(
49*6777b538SAndroid Build Coastguard Worker       const CookieList& cookies,
50*6777b538SAndroid Build Coastguard Worker       const std::vector<CookieAccessSemantics>& access_semantics_list)>;
51*6777b538SAndroid Build Coastguard Worker   using SetCookiesCallback =
52*6777b538SAndroid Build Coastguard Worker       base::OnceCallback<void(CookieAccessResult access_result)>;
53*6777b538SAndroid Build Coastguard Worker   using DeleteCallback = base::OnceCallback<void(uint32_t num_deleted)>;
54*6777b538SAndroid Build Coastguard Worker   using DeletePredicate =
55*6777b538SAndroid Build Coastguard Worker       base::RepeatingCallback<bool(const CanonicalCookie& cookie)>;
56*6777b538SAndroid Build Coastguard Worker   using SetCookieableSchemesCallback = base::OnceCallback<void(bool success)>;
57*6777b538SAndroid Build Coastguard Worker 
58*6777b538SAndroid Build Coastguard Worker   CookieStore();
59*6777b538SAndroid Build Coastguard Worker   virtual ~CookieStore();
60*6777b538SAndroid Build Coastguard Worker 
61*6777b538SAndroid Build Coastguard Worker   // Set the cookie on the cookie store.  |cookie.IsCanonical()| must
62*6777b538SAndroid Build Coastguard Worker   // be true.  |source_url| denotes the url of the resource setting this.
63*6777b538SAndroid Build Coastguard Worker   //
64*6777b538SAndroid Build Coastguard Worker   // |options| is used to determine the context the operation is run in, and
65*6777b538SAndroid Build Coastguard Worker   // which cookies it can alter (e.g. http only, or same site).
66*6777b538SAndroid Build Coastguard Worker   //
67*6777b538SAndroid Build Coastguard Worker   // The current time will be used in place of a null creation time.
68*6777b538SAndroid Build Coastguard Worker   //
69*6777b538SAndroid Build Coastguard Worker   // |cookie_access_result| is an optional input status, to allow for status
70*6777b538SAndroid Build Coastguard Worker   // chaining from callers. It helps callers provide the status of a
71*6777b538SAndroid Build Coastguard Worker   // canonical cookie that may have warnings associated with it.
72*6777b538SAndroid Build Coastguard Worker   virtual void SetCanonicalCookieAsync(
73*6777b538SAndroid Build Coastguard Worker       std::unique_ptr<CanonicalCookie> cookie,
74*6777b538SAndroid Build Coastguard Worker       const GURL& source_url,
75*6777b538SAndroid Build Coastguard Worker       const CookieOptions& options,
76*6777b538SAndroid Build Coastguard Worker       SetCookiesCallback callback,
77*6777b538SAndroid Build Coastguard Worker       std::optional<CookieAccessResult> cookie_access_result =
78*6777b538SAndroid Build Coastguard Worker           std::nullopt) = 0;
79*6777b538SAndroid Build Coastguard Worker 
80*6777b538SAndroid Build Coastguard Worker   // Obtains a CookieList for the given |url| and |options|. The returned
81*6777b538SAndroid Build Coastguard Worker   // cookies are passed into |callback|, ordered by longest path, then earliest
82*6777b538SAndroid Build Coastguard Worker   // creation date.
83*6777b538SAndroid Build Coastguard Worker   // To get all the cookies for a URL, use this method with an all-inclusive
84*6777b538SAndroid Build Coastguard Worker   // |options|.
85*6777b538SAndroid Build Coastguard Worker   // If |cookie_partition_key_collection| is not empty, then this function will
86*6777b538SAndroid Build Coastguard Worker   // return the partitioned cookies for that URL whose partition keys are in the
87*6777b538SAndroid Build Coastguard Worker   // cookie_partition_key_collection *in addition to* the unpartitioned cookies
88*6777b538SAndroid Build Coastguard Worker   // for that URL.
89*6777b538SAndroid Build Coastguard Worker   virtual void GetCookieListWithOptionsAsync(
90*6777b538SAndroid Build Coastguard Worker       const GURL& url,
91*6777b538SAndroid Build Coastguard Worker       const CookieOptions& options,
92*6777b538SAndroid Build Coastguard Worker       const CookiePartitionKeyCollection& cookie_partition_key_collection,
93*6777b538SAndroid Build Coastguard Worker       GetCookieListCallback callback) = 0;
94*6777b538SAndroid Build Coastguard Worker 
95*6777b538SAndroid Build Coastguard Worker   // Returns all the cookies, for use in management UI, etc. This does not mark
96*6777b538SAndroid Build Coastguard Worker   // the cookies as having been accessed. The returned cookies are ordered by
97*6777b538SAndroid Build Coastguard Worker   // longest path, then by earliest creation date.
98*6777b538SAndroid Build Coastguard Worker   virtual void GetAllCookiesAsync(GetAllCookiesCallback callback) = 0;
99*6777b538SAndroid Build Coastguard Worker 
100*6777b538SAndroid Build Coastguard Worker   // Returns all the cookies, for use in management UI, etc. This does not mark
101*6777b538SAndroid Build Coastguard Worker   // the cookies as having been accessed. The returned cookies are ordered by
102*6777b538SAndroid Build Coastguard Worker   // longest path, then by earliest creation date.
103*6777b538SAndroid Build Coastguard Worker   // Additionally returns a vector of CookieAccessSemantics values for the
104*6777b538SAndroid Build Coastguard Worker   // returned cookies, which will be the same length as the vector of returned
105*6777b538SAndroid Build Coastguard Worker   // cookies. This vector will either contain all CookieAccessSemantics::UNKNOWN
106*6777b538SAndroid Build Coastguard Worker   // (if the default implementation is used), or each entry in the
107*6777b538SAndroid Build Coastguard Worker   // vector of CookieAccessSemantics will indicate the access semantics
108*6777b538SAndroid Build Coastguard Worker   // applicable to the cookie at the same index in the returned CookieList.
109*6777b538SAndroid Build Coastguard Worker   virtual void GetAllCookiesWithAccessSemanticsAsync(
110*6777b538SAndroid Build Coastguard Worker       GetAllCookiesWithAccessSemanticsCallback callback);
111*6777b538SAndroid Build Coastguard Worker 
112*6777b538SAndroid Build Coastguard Worker   // Deletes one specific cookie. |cookie| must have been returned by a previous
113*6777b538SAndroid Build Coastguard Worker   // query on this CookieStore. Invokes |callback| with 1 if a cookie was
114*6777b538SAndroid Build Coastguard Worker   // deleted, 0 otherwise.
115*6777b538SAndroid Build Coastguard Worker   virtual void DeleteCanonicalCookieAsync(const CanonicalCookie& cookie,
116*6777b538SAndroid Build Coastguard Worker                                           DeleteCallback callback) = 0;
117*6777b538SAndroid Build Coastguard Worker 
118*6777b538SAndroid Build Coastguard Worker   // Deletes all of the cookies that have a creation_date matching
119*6777b538SAndroid Build Coastguard Worker   // |creation_range|. See CookieDeletionInfo::TimeRange::Matches().
120*6777b538SAndroid Build Coastguard Worker   // Calls |callback| with the number of cookies deleted.
121*6777b538SAndroid Build Coastguard Worker   virtual void DeleteAllCreatedInTimeRangeAsync(
122*6777b538SAndroid Build Coastguard Worker       const CookieDeletionInfo::TimeRange& creation_range,
123*6777b538SAndroid Build Coastguard Worker       DeleteCallback callback) = 0;
124*6777b538SAndroid Build Coastguard Worker 
125*6777b538SAndroid Build Coastguard Worker   // Deletes all of the cookies matching |delete_info|. This includes all
126*6777b538SAndroid Build Coastguard Worker   // http_only and secure cookies. Avoid deleting cookies that could leave
127*6777b538SAndroid Build Coastguard Worker   // websites with a partial set of visible cookies.
128*6777b538SAndroid Build Coastguard Worker   // Calls |callback| with the number of cookies deleted.
129*6777b538SAndroid Build Coastguard Worker   virtual void DeleteAllMatchingInfoAsync(CookieDeletionInfo delete_info,
130*6777b538SAndroid Build Coastguard Worker                                           DeleteCallback callback) = 0;
131*6777b538SAndroid Build Coastguard Worker 
132*6777b538SAndroid Build Coastguard Worker   // Deletes all cookies without expiration data.
133*6777b538SAndroid Build Coastguard Worker   virtual void DeleteSessionCookiesAsync(DeleteCallback callback) = 0;
134*6777b538SAndroid Build Coastguard Worker 
135*6777b538SAndroid Build Coastguard Worker   // Deletes all cookies where |predicate| returns true.
136*6777b538SAndroid Build Coastguard Worker   // Calls |callback| with the number of cookies deleted.
137*6777b538SAndroid Build Coastguard Worker   virtual void DeleteMatchingCookiesAsync(DeletePredicate predicate,
138*6777b538SAndroid Build Coastguard Worker                                           DeleteCallback callback) = 0;
139*6777b538SAndroid Build Coastguard Worker 
140*6777b538SAndroid Build Coastguard Worker   // Deletes all cookies in the store.
141*6777b538SAndroid Build Coastguard Worker   void DeleteAllAsync(DeleteCallback callback);
142*6777b538SAndroid Build Coastguard Worker 
143*6777b538SAndroid Build Coastguard Worker   // Flush the backing store (if any) to disk and post the given callback when
144*6777b538SAndroid Build Coastguard Worker   // done.
145*6777b538SAndroid Build Coastguard Worker   virtual void FlushStore(base::OnceClosure callback) = 0;
146*6777b538SAndroid Build Coastguard Worker 
147*6777b538SAndroid Build Coastguard Worker   // Protects session cookies from deletion on shutdown, if the underlying
148*6777b538SAndroid Build Coastguard Worker   // CookieStore implemention is currently configured to store them to disk.
149*6777b538SAndroid Build Coastguard Worker   // Otherwise, does nothing.
SetForceKeepSessionState()150*6777b538SAndroid Build Coastguard Worker   virtual void SetForceKeepSessionState() {}
151*6777b538SAndroid Build Coastguard Worker 
152*6777b538SAndroid Build Coastguard Worker   // The interface used to observe changes to this CookieStore's contents.
153*6777b538SAndroid Build Coastguard Worker   virtual CookieChangeDispatcher& GetChangeDispatcher() = 0;
154*6777b538SAndroid Build Coastguard Worker 
155*6777b538SAndroid Build Coastguard Worker   // Resets the list of cookieable schemes to the supplied schemes. Does nothing
156*6777b538SAndroid Build Coastguard Worker   // (and returns false) if called after first use of the instance (i.e. after
157*6777b538SAndroid Build Coastguard Worker   // the instance initialization process). Otherwise, this returns true to
158*6777b538SAndroid Build Coastguard Worker   // indicate success. CookieStores which do not support modifying cookieable
159*6777b538SAndroid Build Coastguard Worker   // schemes will always return false.
160*6777b538SAndroid Build Coastguard Worker   virtual void SetCookieableSchemes(const std::vector<std::string>& schemes,
161*6777b538SAndroid Build Coastguard Worker                                     SetCookieableSchemesCallback callback) = 0;
162*6777b538SAndroid Build Coastguard Worker 
163*6777b538SAndroid Build Coastguard Worker   // Transfer ownership of a CookieAccessDelegate.
164*6777b538SAndroid Build Coastguard Worker   void SetCookieAccessDelegate(std::unique_ptr<CookieAccessDelegate> delegate);
165*6777b538SAndroid Build Coastguard Worker 
166*6777b538SAndroid Build Coastguard Worker   // This may be null if no delegate has been set yet, or the delegate has been
167*6777b538SAndroid Build Coastguard Worker   // reset to null.
cookie_access_delegate()168*6777b538SAndroid Build Coastguard Worker   const CookieAccessDelegate* cookie_access_delegate() const {
169*6777b538SAndroid Build Coastguard Worker     return cookie_access_delegate_.get();
170*6777b538SAndroid Build Coastguard Worker   }
171*6777b538SAndroid Build Coastguard Worker 
172*6777b538SAndroid Build Coastguard Worker   // Returns a boolean indicating whether the cookie `site` has any cookie
173*6777b538SAndroid Build Coastguard Worker   // in another partition other than the `cookie_partition_key` supplied.
174*6777b538SAndroid Build Coastguard Worker   // Will return nullopt if cookies have not finished loading.
175*6777b538SAndroid Build Coastguard Worker   // If the partition key is null, the method assumes it is because partitioned
176*6777b538SAndroid Build Coastguard Worker   // cookies are disabled.
177*6777b538SAndroid Build Coastguard Worker   virtual std::optional<bool> SiteHasCookieInOtherPartition(
178*6777b538SAndroid Build Coastguard Worker       const net::SchemefulSite& site,
179*6777b538SAndroid Build Coastguard Worker       const std::optional<CookiePartitionKey>& cookie_partition_key) const;
180*6777b538SAndroid Build Coastguard Worker 
181*6777b538SAndroid Build Coastguard Worker  private:
182*6777b538SAndroid Build Coastguard Worker   // Used to determine whether a particular cookie should be subject to legacy
183*6777b538SAndroid Build Coastguard Worker   // or non-legacy access semantics.
184*6777b538SAndroid Build Coastguard Worker   std::unique_ptr<CookieAccessDelegate> cookie_access_delegate_;
185*6777b538SAndroid Build Coastguard Worker };
186*6777b538SAndroid Build Coastguard Worker 
187*6777b538SAndroid Build Coastguard Worker }  // namespace net
188*6777b538SAndroid Build Coastguard Worker 
189*6777b538SAndroid Build Coastguard Worker #endif  // NET_COOKIES_COOKIE_STORE_H_
190