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