xref: /aosp_15_r20/external/cronet/net/cookies/cookie_deletion_info.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2018 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef NET_COOKIES_COOKIE_DELETION_INFO_H_
6 #define NET_COOKIES_COOKIE_DELETION_INFO_H_
7 
8 #include <optional>
9 #include <set>
10 #include <string>
11 
12 #include "base/time/time.h"
13 #include "net/cookies/canonical_cookie.h"
14 #include "net/cookies/cookie_constants.h"
15 #include "net/cookies/cookie_partition_key_collection.h"
16 
17 namespace net {
18 
19 // Used to specify which cookies to delete. All members are ANDed together.
20 struct NET_EXPORT CookieDeletionInfo {
21   // TODO(cmumford): Combine with
22   // network::mojom::CookieDeletionSessionControl.
23   enum SessionControl {
24     IGNORE_CONTROL,
25     SESSION_COOKIES,
26     PERSISTENT_COOKIES,
27   };
28 
29   // Define a range of time from [start, end) where start is inclusive and end
30   // is exclusive. There is a special case where |start| == |end| (matching a
31   // single time) where |end| is inclusive. This special case is for iOS that
32   // will be removed in the future.
33   //
34   // TODO(crbug.com/830689): Delete the start=end special case.
35   class NET_EXPORT TimeRange {
36    public:
37     // Default constructor matches any non-null time.
38     TimeRange();
39     TimeRange(const TimeRange& other);
40     TimeRange(base::Time start, base::Time end);
41     TimeRange& operator=(const TimeRange& rhs);
42 
43     // Is |time| within this time range?
44     //
45     // Will return true if:
46     //
47     //   |start_| <= |time| < |end_|
48     //
49     // If |start_| is null then the range is unbounded on the lower range.
50     // If |end_| is null then the range is unbounded on the upper range.
51     //
52     // Note 1: |time| cannot be null.
53     // Note 2: If |start_| == |end_| then end_ is inclusive.
54     //
55     bool Contains(const base::Time& time) const;
56 
57     // Set the range start time. Set to null (i.e. Time()) to indicated an
58     // unbounded lower range.
59     void SetStart(base::Time value);
60 
61     // Set the range end time. Set to null (i.e. Time()) to indicated an
62     // unbounded upper range.
63     void SetEnd(base::Time value);
64 
65     // Return the start time.
startCookieDeletionInfo66     base::Time start() const { return start_; }
67 
68     // Return the end time.
endCookieDeletionInfo69     base::Time end() const { return end_; }
70 
71    private:
72     // The inclusive start time of this range.
73     base::Time start_;
74     // The exclusive end time of this range.
75     base::Time end_;
76   };
77 
78   CookieDeletionInfo();
79   CookieDeletionInfo(CookieDeletionInfo&& other);
80   CookieDeletionInfo(const CookieDeletionInfo& other);
81   CookieDeletionInfo(base::Time start_time, base::Time end_time);
82   ~CookieDeletionInfo();
83 
84   CookieDeletionInfo& operator=(CookieDeletionInfo&& rhs);
85   CookieDeletionInfo& operator=(const CookieDeletionInfo& rhs);
86 
87   // Return true if |cookie| matches all members of this instance. All members
88   // are ANDed together. For example: if the |cookie| creation date is within
89   // |creation_range| AND the |cookie| name is equal to |name|, etc. then true
90   // will be returned. If not false.
91   //
92   // |params.access_semantics| is the access semantics mode of the cookie at the
93   // time of the attempted match. This is used to determine whether the cookie
94   // matches a particular URL based on effective SameSite mode. (But the value
95   // should not matter because the CookieOptions used for this check includes
96   // all cookies for a URL regardless of SameSite).
97   //
98   // |params.delegate_treats_url_as_trustworthy| should be set to true if |url|
99   // was granted access to secure cookies by the CookieAccessDelegate.
100   //
101   // All members are used. See comments above other members for specifics
102   // about how checking is done for that value.
103   bool Matches(const CanonicalCookie& cookie,
104                const CookieAccessParams& params) const;
105 
106   // See comment above for TimeRange::Contains() for more info.
107   TimeRange creation_range;
108 
109   // By default ignore session type and delete both session and persistent
110   // cookies.
111   SessionControl session_control = SessionControl::IGNORE_CONTROL;
112 
113   // If has a value then cookie.Host() must equal |host|.
114   std::optional<std::string> host;
115 
116   // If has a value then cookie.Name() must equal |name|.
117   std::optional<std::string> name;
118 
119   // If has a value then will match if the cookie being evaluated would be
120   // included for a request of |url|.
121   std::optional<GURL> url;
122 
123   // If has a value then any cookie with a domain/ip contained in this set
124   // will be deleted (assuming other fields match).
125   // Domains must not have a leading period. e.g "example.com" and not
126   // ".example.com".
127   //
128   // Note: |domains_and_ips_to_ignore| takes precedence. For example if this
129   // has a value of ["A", "B"] and |domains_and_ips_to_ignore| is ["B", "C"]
130   // then only "A" will be deleted.
131   std::optional<std::set<std::string>> domains_and_ips_to_delete;
132 
133   // If has a value then any cookie with a domain/ip contained in this set
134   // will be ignored (and not deleted).
135   // Domains must not have a leading period. e.g "example.com" and not
136   // ".example.com".
137   //
138   // See precedence note above.
139   std::optional<std::set<std::string>> domains_and_ips_to_ignore;
140 
141   // Used only for testing purposes.
142   std::optional<std::string> value_for_testing;
143 
144   // Cookie partition collection. Partitioned cookies are not deleted if their
145   // partition key is not in the collection. By default, it clears cookies in
146   // all partitions.
147   CookiePartitionKeyCollection cookie_partition_key_collection =
148       CookiePartitionKeyCollection::ContainsAll();
149 
150   // If true, third-party cookie blocking applies to the context that triggered
151   // the deletion. In this case, we should only delete partitioned cookies.
152   bool partitioned_state_only = false;
153 };
154 
155 }  // namespace net
156 
157 #endif  // NET_COOKIES_COOKIE_DELETION_INFO_H_
158