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