1 // Copyright 2017 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/reporting/reporting_endpoint.h"
6
7 #include <string>
8 #include <tuple>
9
10 #include "base/time/time.h"
11 #include "net/base/network_anonymization_key.h"
12 #include "url/gurl.h"
13 #include "url/origin.h"
14
15 namespace net {
16
17 ReportingEndpointGroupKey::ReportingEndpointGroupKey() = default;
18
ReportingEndpointGroupKey(const NetworkAnonymizationKey & network_anonymization_key,const url::Origin & origin,const std::string & group_name)19 ReportingEndpointGroupKey::ReportingEndpointGroupKey(
20 const NetworkAnonymizationKey& network_anonymization_key,
21 const url::Origin& origin,
22 const std::string& group_name)
23 : ReportingEndpointGroupKey(network_anonymization_key,
24 std::nullopt,
25 origin,
26 group_name) {}
27
ReportingEndpointGroupKey(const NetworkAnonymizationKey & network_anonymization_key,std::optional<base::UnguessableToken> reporting_source,const url::Origin & origin,const std::string & group_name)28 ReportingEndpointGroupKey::ReportingEndpointGroupKey(
29 const NetworkAnonymizationKey& network_anonymization_key,
30 std::optional<base::UnguessableToken> reporting_source,
31 const url::Origin& origin,
32 const std::string& group_name)
33 : network_anonymization_key(network_anonymization_key),
34 reporting_source(std::move(reporting_source)),
35 origin(origin),
36 group_name(group_name) {
37 // If |reporting_source| is present, it must not be empty.
38 DCHECK(!(this->reporting_source.has_value() &&
39 this->reporting_source->is_empty()));
40 }
41
ReportingEndpointGroupKey(const ReportingEndpointGroupKey & other,const std::optional<base::UnguessableToken> & reporting_source)42 ReportingEndpointGroupKey::ReportingEndpointGroupKey(
43 const ReportingEndpointGroupKey& other,
44 const std::optional<base::UnguessableToken>& reporting_source)
45 : ReportingEndpointGroupKey(other.network_anonymization_key,
46 reporting_source,
47 other.origin,
48 other.group_name) {}
49
50 ReportingEndpointGroupKey::ReportingEndpointGroupKey(
51 const ReportingEndpointGroupKey& other) = default;
52 ReportingEndpointGroupKey::ReportingEndpointGroupKey(
53 ReportingEndpointGroupKey&& other) = default;
54
55 ReportingEndpointGroupKey& ReportingEndpointGroupKey::operator=(
56 const ReportingEndpointGroupKey&) = default;
57 ReportingEndpointGroupKey& ReportingEndpointGroupKey::operator=(
58 ReportingEndpointGroupKey&&) = default;
59
60 ReportingEndpointGroupKey::~ReportingEndpointGroupKey() = default;
61
operator ==(const ReportingEndpointGroupKey & lhs,const ReportingEndpointGroupKey & rhs)62 bool operator==(const ReportingEndpointGroupKey& lhs,
63 const ReportingEndpointGroupKey& rhs) {
64 return std::tie(lhs.reporting_source, lhs.network_anonymization_key,
65 lhs.origin, lhs.group_name) ==
66 std::tie(rhs.reporting_source, rhs.network_anonymization_key,
67 rhs.origin, rhs.group_name);
68 }
69
operator !=(const ReportingEndpointGroupKey & lhs,const ReportingEndpointGroupKey & rhs)70 bool operator!=(const ReportingEndpointGroupKey& lhs,
71 const ReportingEndpointGroupKey& rhs) {
72 return !(lhs == rhs);
73 }
74
operator <(const ReportingEndpointGroupKey & lhs,const ReportingEndpointGroupKey & rhs)75 bool operator<(const ReportingEndpointGroupKey& lhs,
76 const ReportingEndpointGroupKey& rhs) {
77 return std::tie(lhs.reporting_source, lhs.network_anonymization_key,
78 lhs.origin, lhs.group_name) <
79 std::tie(rhs.reporting_source, rhs.network_anonymization_key,
80 rhs.origin, rhs.group_name);
81 }
82
operator >(const ReportingEndpointGroupKey & lhs,const ReportingEndpointGroupKey & rhs)83 bool operator>(const ReportingEndpointGroupKey& lhs,
84 const ReportingEndpointGroupKey& rhs) {
85 return std::tie(lhs.reporting_source, lhs.network_anonymization_key,
86 lhs.origin, lhs.group_name) >
87 std::tie(rhs.reporting_source, rhs.network_anonymization_key,
88 rhs.origin, rhs.group_name);
89 }
90
ToString() const91 std::string ReportingEndpointGroupKey::ToString() const {
92 return "Source: " +
93 (reporting_source ? reporting_source->ToString() : "null") +
94 "; NAK: " + network_anonymization_key.ToDebugString() +
95 "; Origin: " + origin.Serialize() + "; Group name: " + group_name;
96 }
97
98 const int ReportingEndpoint::EndpointInfo::kDefaultPriority = 1;
99 const int ReportingEndpoint::EndpointInfo::kDefaultWeight = 1;
100
101 ReportingEndpoint::ReportingEndpoint() = default;
102
ReportingEndpoint(const ReportingEndpointGroupKey & group,const EndpointInfo & info)103 ReportingEndpoint::ReportingEndpoint(const ReportingEndpointGroupKey& group,
104 const EndpointInfo& info)
105 : group_key(group), info(info) {
106 DCHECK_LE(0, info.weight);
107 DCHECK_LE(0, info.priority);
108 }
109
110 ReportingEndpoint::ReportingEndpoint(const ReportingEndpoint& other) = default;
111 ReportingEndpoint::ReportingEndpoint(ReportingEndpoint&& other) = default;
112
113 ReportingEndpoint& ReportingEndpoint::operator=(const ReportingEndpoint&) =
114 default;
115 ReportingEndpoint& ReportingEndpoint::operator=(ReportingEndpoint&&) = default;
116
117 ReportingEndpoint::~ReportingEndpoint() = default;
118
is_valid() const119 bool ReportingEndpoint::is_valid() const {
120 return info.url.is_valid();
121 }
122
123 ReportingEndpointGroup::ReportingEndpointGroup() = default;
124
125 ReportingEndpointGroup::ReportingEndpointGroup(
126 const ReportingEndpointGroup& other) = default;
127
128 ReportingEndpointGroup::~ReportingEndpointGroup() = default;
129
CachedReportingEndpointGroup(const ReportingEndpointGroupKey & group_key,OriginSubdomains include_subdomains,base::Time expires,base::Time last_used)130 CachedReportingEndpointGroup::CachedReportingEndpointGroup(
131 const ReportingEndpointGroupKey& group_key,
132 OriginSubdomains include_subdomains,
133 base::Time expires,
134 base::Time last_used)
135 : group_key(group_key),
136 include_subdomains(include_subdomains),
137 expires(expires),
138 last_used(last_used) {}
139
CachedReportingEndpointGroup(const ReportingEndpointGroup & endpoint_group,base::Time now)140 CachedReportingEndpointGroup::CachedReportingEndpointGroup(
141 const ReportingEndpointGroup& endpoint_group,
142 base::Time now)
143 : CachedReportingEndpointGroup(endpoint_group.group_key,
144 endpoint_group.include_subdomains,
145 now + endpoint_group.ttl /* expires */,
146 now /* last_used */) {
147 // Don't cache V1 document endpoints; this should only be used for V0
148 // endpoint groups.
149 DCHECK(!endpoint_group.group_key.IsDocumentEndpoint());
150 }
151
152 } // namespace net
153