xref: /aosp_15_r20/external/cronet/net/cookies/cookie_partition_key_collection.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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 #include "net/cookies/cookie_partition_key_collection.h"
6 
7 #include <vector>
8 
9 #include "base/containers/contains.h"
10 #include "base/containers/flat_map.h"
11 #include "base/containers/flat_set.h"
12 #include "base/functional/bind.h"
13 #include "base/functional/callback.h"
14 #include "net/base/schemeful_site.h"
15 #include "net/cookies/cookie_access_delegate.h"
16 #include "net/cookies/cookie_partition_key.h"
17 #include "net/first_party_sets/first_party_set_entry.h"
18 
19 namespace net {
20 
21 CookiePartitionKeyCollection::CookiePartitionKeyCollection() = default;
22 
23 CookiePartitionKeyCollection::CookiePartitionKeyCollection(
24     const CookiePartitionKeyCollection& other) = default;
25 
26 CookiePartitionKeyCollection::CookiePartitionKeyCollection(
27     CookiePartitionKeyCollection&& other) = default;
28 
CookiePartitionKeyCollection(const CookiePartitionKey & key)29 CookiePartitionKeyCollection::CookiePartitionKeyCollection(
30     const CookiePartitionKey& key)
31     : CookiePartitionKeyCollection(base::flat_set<CookiePartitionKey>({key})) {}
32 
CookiePartitionKeyCollection(base::flat_set<CookiePartitionKey> keys)33 CookiePartitionKeyCollection::CookiePartitionKeyCollection(
34     base::flat_set<CookiePartitionKey> keys)
35     : keys_(std::move(keys)) {}
36 
CookiePartitionKeyCollection(bool contains_all_keys)37 CookiePartitionKeyCollection::CookiePartitionKeyCollection(
38     bool contains_all_keys)
39     : contains_all_keys_(contains_all_keys) {}
40 
41 CookiePartitionKeyCollection& CookiePartitionKeyCollection::operator=(
42     const CookiePartitionKeyCollection& other) = default;
43 
44 CookiePartitionKeyCollection& CookiePartitionKeyCollection::operator=(
45     CookiePartitionKeyCollection&& other) = default;
46 
47 CookiePartitionKeyCollection::~CookiePartitionKeyCollection() = default;
48 
Contains(const CookiePartitionKey & key) const49 bool CookiePartitionKeyCollection::Contains(
50     const CookiePartitionKey& key) const {
51   return contains_all_keys_ || base::Contains(keys_, key);
52 }
53 
operator ==(const CookiePartitionKeyCollection & lhs,const CookiePartitionKeyCollection & rhs)54 bool operator==(const CookiePartitionKeyCollection& lhs,
55                 const CookiePartitionKeyCollection& rhs) {
56   if (lhs.ContainsAllKeys()) {
57     return rhs.ContainsAllKeys();
58   }
59 
60   if (rhs.ContainsAllKeys()) {
61     return false;
62   }
63 
64   return lhs.PartitionKeys() == rhs.PartitionKeys();
65 }
66 
operator <<(std::ostream & os,const CookiePartitionKeyCollection & keys)67 std::ostream& operator<<(std::ostream& os,
68                          const CookiePartitionKeyCollection& keys) {
69   if (keys.ContainsAllKeys()) {
70     return os << "(all keys)";
71   }
72 
73   os << "{";
74   bool first = true;
75   for (const net::CookiePartitionKey& key : keys.PartitionKeys()) {
76     if (!first) {
77       os << ", ";
78     }
79 
80     os << key;
81 
82     first = false;
83   }
84   return os << "}";
85 }
86 
87 }  // namespace net
88