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 FromOptional(const std::optional<CookiePartitionKey> & opt_key)45 static CookiePartitionKeyCollection FromOptional( 46 const std::optional<CookiePartitionKey>& opt_key) { 47 return opt_key ? CookiePartitionKeyCollection(opt_key.value()) 48 : CookiePartitionKeyCollection(); 49 } 50 51 // Temporary method used to record where we need to decide how to build the 52 // CookiePartitionKeyCollection. 53 // 54 // Returns an empty key collection, so no partitioned cookies will be returned 55 // at callsites this is used. 56 // 57 // TODO(crbug.com/1225444): Remove this method and update callsites to use 58 // appropriate constructor. Todo()59 static CookiePartitionKeyCollection Todo() { 60 return CookiePartitionKeyCollection(); 61 } 62 63 // CookieMonster can check if the key collection is empty to avoid searching 64 // the PartitionedCookieMap at all. IsEmpty()65 bool IsEmpty() const { return !contains_all_keys_ && keys_.empty(); } 66 67 // Returns if the key collection contains every partition key. ContainsAllKeys()68 bool ContainsAllKeys() const { return contains_all_keys_; } 69 70 // Iterate over all keys in the key collection, do not call this method if 71 // `contains_all_keys` is true. PartitionKeys()72 const base::flat_set<CookiePartitionKey>& PartitionKeys() const { 73 DCHECK(!contains_all_keys_); 74 return keys_; 75 } 76 77 // Returns true if the collection contains the passed key. 78 bool Contains(const CookiePartitionKey& key) const; 79 80 private: 81 explicit CookiePartitionKeyCollection(bool contains_all_keys); 82 83 bool contains_all_keys_ = false; 84 // If `contains_all_keys_` is true, `keys_` must be empty. 85 // If `keys_` is not empty, then `contains_all_keys_` must be false. 86 base::flat_set<CookiePartitionKey> keys_; 87 }; 88 89 NET_EXPORT bool operator==(const CookiePartitionKeyCollection& lhs, 90 const CookiePartitionKeyCollection& rhs); 91 92 NET_EXPORT std::ostream& operator<<(std::ostream& os, 93 const CookiePartitionKeyCollection& keys); 94 95 } // namespace net 96 97 #endif // NET_COOKIES_COOKIE_PARTITION_KEY_COLLECTION_H_ 98