1 // Copyright 2021 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_PARTITION_KEY_COLLECTION_H_ 6 #define NET_COOKIES_COOKIE_PARTITION_KEY_COLLECTION_H_ 7 8 #include <iosfwd> 9 10 #include "base/containers/flat_set.h" 11 #include "base/functional/callback_forward.h" 12 #include "net/base/net_export.h" 13 #include "net/cookies/cookie_partition_key.h" 14 15 namespace net { 16 17 // A data structure used to represent a collection of cookie partition keys. 18 // 19 // It can represent all possible cookie partition keys when 20 // `contains_all_keys_` is true. 21 // 22 // It can also represent a finite number of cookie partition keys, including 23 // zero. 24 class NET_EXPORT CookiePartitionKeyCollection { 25 public: 26 // Creates an empty key collection. 27 CookiePartitionKeyCollection(); 28 CookiePartitionKeyCollection(const CookiePartitionKeyCollection& other); 29 CookiePartitionKeyCollection(CookiePartitionKeyCollection&& other); 30 // Creates a key collection with a single element. 31 explicit CookiePartitionKeyCollection(const CookiePartitionKey& key); 32 // Creates a set that contains each partition key in the set. 33 explicit CookiePartitionKeyCollection( 34 base::flat_set<CookiePartitionKey> keys); 35 36 CookiePartitionKeyCollection& operator=( 37 const CookiePartitionKeyCollection& other); 38 CookiePartitionKeyCollection& operator=(CookiePartitionKeyCollection&& other); 39 ~CookiePartitionKeyCollection(); 40 ContainsAll()41 static CookiePartitionKeyCollection ContainsAll() { 42 return CookiePartitionKeyCollection(true); 43 } 44 45 // Builds a Collection that contains the same-site and cross-site 46 // partitionKeys associated with the `top_level_site`. 47 // `top_level_site` must be non-empty and valid. 48 static CookiePartitionKeyCollection MatchesSite( 49 const net::SchemefulSite& top_level_site); 50 FromOptional(const std::optional<CookiePartitionKey> & opt_key)51 static CookiePartitionKeyCollection FromOptional( 52 const std::optional<CookiePartitionKey>& opt_key) { 53 return opt_key ? CookiePartitionKeyCollection(opt_key.value()) 54 : CookiePartitionKeyCollection(); 55 } 56 57 // Temporary method used to record where we need to decide how to build the 58 // CookiePartitionKeyCollection. 59 // 60 // Returns an empty key collection, so no partitioned cookies will be returned 61 // at callsites this is used. 62 // 63 // TODO(crbug.com/40188414): Remove this method and update callsites to use 64 // appropriate constructor. Todo()65 static CookiePartitionKeyCollection Todo() { 66 return CookiePartitionKeyCollection(); 67 } 68 69 // CookieMonster can check if the key collection is empty to avoid searching 70 // the PartitionedCookieMap at all. IsEmpty()71 bool IsEmpty() const { return !contains_all_keys_ && keys_.empty(); } 72 73 // Returns if the key collection contains every partition key. ContainsAllKeys()74 bool ContainsAllKeys() const { return contains_all_keys_; } 75 76 // Iterate over all keys in the key collection, do not call this method if 77 // `contains_all_keys` is true. PartitionKeys()78 const base::flat_set<CookiePartitionKey>& PartitionKeys() const { 79 DCHECK(!contains_all_keys_); 80 return keys_; 81 } 82 83 // Returns true if the collection contains the passed key. 84 bool Contains(const CookiePartitionKey& key) const; 85 86 private: 87 explicit CookiePartitionKeyCollection(bool contains_all_keys); 88 89 bool contains_all_keys_ = false; 90 // If `contains_all_keys_` is true, `keys_` must be empty. 91 // If `keys_` is not empty, then `contains_all_keys_` must be false. 92 base::flat_set<CookiePartitionKey> keys_; 93 }; 94 95 NET_EXPORT bool operator==(const CookiePartitionKeyCollection& lhs, 96 const CookiePartitionKeyCollection& rhs); 97 98 NET_EXPORT std::ostream& operator<<(std::ostream& os, 99 const CookiePartitionKeyCollection& keys); 100 101 } // namespace net 102 103 #endif // NET_COOKIES_COOKIE_PARTITION_KEY_COLLECTION_H_ 104