• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef NET_REPORTING_REPORTING_ENDPOINT_H_
6 #define NET_REPORTING_REPORTING_ENDPOINT_H_
7 
8 #include <string>
9 #include <vector>
10 
11 #include "base/time/time.h"
12 #include "base/unguessable_token.h"
13 #include "net/base/net_export.h"
14 #include "net/base/network_anonymization_key.h"
15 #include "third_party/abseil-cpp/absl/types/optional.h"
16 #include "url/gurl.h"
17 #include "url/origin.h"
18 
19 namespace net {
20 
21 // Identifies an endpoint group.
22 struct NET_EXPORT ReportingEndpointGroupKey {
23   ReportingEndpointGroupKey();
24 
25   ReportingEndpointGroupKey(
26       const NetworkAnonymizationKey& network_anonymization_key,
27       const url::Origin& origin,
28       const std::string& group_name);
29 
30   ReportingEndpointGroupKey(
31       const NetworkAnonymizationKey& network_anonymization_key,
32       absl::optional<base::UnguessableToken> reporting_source,
33       const url::Origin& origin,
34       const std::string& group_name);
35 
36   ReportingEndpointGroupKey(
37       const ReportingEndpointGroupKey& other,
38       const absl::optional<base::UnguessableToken>& reporting_source);
39 
40   ReportingEndpointGroupKey(const ReportingEndpointGroupKey& other);
41   ReportingEndpointGroupKey(ReportingEndpointGroupKey&& other);
42 
43   ReportingEndpointGroupKey& operator=(const ReportingEndpointGroupKey&);
44   ReportingEndpointGroupKey& operator=(ReportingEndpointGroupKey&&);
45 
46   ~ReportingEndpointGroupKey();
47 
48   std::string ToString() const;
49 
50   // True if this endpoint "group" is actually being used to represent a single
51   // V1 document endpoint.
IsDocumentEndpointReportingEndpointGroupKey52   bool IsDocumentEndpoint() const { return reporting_source.has_value(); }
53 
54   // The NetworkAnonymizationKey the group is scoped to. Needed to prevent
55   // leaking third party contexts across sites.
56   NetworkAnonymizationKey network_anonymization_key;
57 
58   // Source token for the document or worker which configured this endpoint, if
59   // this was configured with the Reporting-Endpoints header. For endpoint
60   // groups configured with the Report-To header, this will be nullopt.
61   absl::optional<base::UnguessableToken> reporting_source;
62 
63   // Origin that configured this endpoint group.
64   url::Origin origin;
65 
66   // Name of the endpoint group (defaults to "default" during header parsing).
67   std::string group_name;
68 };
69 
70 NET_EXPORT bool operator==(const ReportingEndpointGroupKey& lhs,
71                            const ReportingEndpointGroupKey& rhs);
72 NET_EXPORT bool operator!=(const ReportingEndpointGroupKey& lhs,
73                            const ReportingEndpointGroupKey& rhs);
74 NET_EXPORT bool operator<(const ReportingEndpointGroupKey& lhs,
75                           const ReportingEndpointGroupKey& rhs);
76 NET_EXPORT bool operator>(const ReportingEndpointGroupKey& lhs,
77                           const ReportingEndpointGroupKey& rhs);
78 
79 // The configuration by an origin to use an endpoint for report delivery.
80 // TODO(crbug.com/912622): Track endpoint failures for garbage collection.
81 struct NET_EXPORT ReportingEndpoint {
82   struct NET_EXPORT EndpointInfo {
83     static const int kDefaultPriority;
84     static const int kDefaultWeight;
85 
86     // The endpoint to which reports may be delivered. (Origins may configure
87     // many.)
88     GURL url;
89 
90     // Priority when multiple endpoints are configured for an origin; endpoints
91     // with numerically lower priorities are used first.
92     int priority = kDefaultPriority;
93 
94     // Weight when multiple endpoints are configured for an origin with the same
95     // priority; among those with the same priority, each endpoint has a chance
96     // of being chosen that is proportional to its weight.
97     int weight = kDefaultWeight;
98   };
99 
100   struct Statistics {
101     // The number of attempted uploads that we've made for this endpoint.
102     int attempted_uploads = 0;
103     // The number of uploads that have succeeded for this endpoint.
104     int successful_uploads = 0;
105     // The number of individual reports that we've attempted to upload for this
106     // endpoint.  (Failed uploads will cause a report to be counted multiple
107     // times, once for each attempt.)
108     int attempted_reports = 0;
109     // The number of individual reports that we've successfully uploaded for
110     // this endpoint.
111     int successful_reports = 0;
112   };
113 
114   // Constructs an invalid ReportingEndpoint.
115   ReportingEndpoint();
116 
117   ReportingEndpoint(const ReportingEndpointGroupKey& group,
118                     const EndpointInfo& info);
119 
120   ReportingEndpoint(const ReportingEndpoint& other);
121   ReportingEndpoint(ReportingEndpoint&& other);
122 
123   ReportingEndpoint& operator=(const ReportingEndpoint&);
124   ReportingEndpoint& operator=(ReportingEndpoint&&);
125 
126   ~ReportingEndpoint();
127 
128   bool is_valid() const;
129   explicit operator bool() const { return is_valid(); }
130 
131   // Identifies the endpoint group to which this endpoint belongs.
132   ReportingEndpointGroupKey group_key;
133 
134   // URL, priority, and weight of the endpoint.
135   EndpointInfo info;
136 
137   // Information about the number of deliveries that we've attempted for this
138   // endpoint. Not persisted across restarts.
139   Statistics stats;
140 };
141 
142 // Marks whether a given endpoint group is configured to include its origin's
143 // subdomains.
144 enum class OriginSubdomains { EXCLUDE, INCLUDE, DEFAULT = EXCLUDE };
145 
146 // Represents an endpoint group set by an origin via Report-To header.
147 struct NET_EXPORT ReportingEndpointGroup {
148   ReportingEndpointGroup();
149 
150   ReportingEndpointGroup(const ReportingEndpointGroup& other);
151 
152   ~ReportingEndpointGroup();
153 
154   ReportingEndpointGroupKey group_key;
155 
156   // Whether this group applies to subdomains of its origin.
157   OriginSubdomains include_subdomains = OriginSubdomains::DEFAULT;
158 
159   // Time for which the endpoint group remains valid after it is set.
160   base::TimeDelta ttl;
161 
162   // Endpoints in this group.
163   std::vector<ReportingEndpoint::EndpointInfo> endpoints;
164 };
165 
166 // Representation of an endpoint group used for in-memory and persistent
167 // storage.
168 struct NET_EXPORT CachedReportingEndpointGroup {
169   CachedReportingEndpointGroup(const ReportingEndpointGroupKey& group_key,
170                                OriginSubdomains include_subdomains,
171                                base::Time expires,
172                                base::Time last_used);
173 
174   // |now| is the time at which the header was processed.
175   CachedReportingEndpointGroup(const ReportingEndpointGroup& endpoint_group,
176                                base::Time now);
177 
178   // Origin and group name.
179   ReportingEndpointGroupKey group_key;
180 
181   // Whether this group applies to subdomains of |group_key.origin|.
182   OriginSubdomains include_subdomains = OriginSubdomains::DEFAULT;
183 
184   // When this group's max_age expires.
185   // (base::Time is used here instead of base::TimeTicks for ease of
186   // serialization for persistent storage, and because it is more appropriate
187   // for expiration times, as per //base/time/time.h.)
188   base::Time expires;
189 
190   // Last time that this group was accessed for a delivery or updated via a
191   // new header.
192   base::Time last_used;
193 };
194 
195 }  // namespace net
196 
197 #endif  // NET_REPORTING_REPORTING_ENDPOINT_H_
198