juliatuttle | 58684333 | 2017-03-27 16:22:37 | [diff] [blame] | 1 | // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Lily Chen | fc92ff4 | 2019-05-06 22:59:10 | [diff] [blame] | 5 | #ifndef NET_REPORTING_REPORTING_ENDPOINT_H_ |
| 6 | #define NET_REPORTING_REPORTING_ENDPOINT_H_ |
juliatuttle | 58684333 | 2017-03-27 16:22:37 | [diff] [blame] | 7 | |
| 8 | #include <string> |
Lily Chen | efb6fcf | 2019-04-19 04:17:54 | [diff] [blame] | 9 | #include <vector> |
juliatuttle | 58684333 | 2017-03-27 16:22:37 | [diff] [blame] | 10 | |
| 11 | #include "base/macros.h" |
| 12 | #include "base/time/time.h" |
| 13 | #include "net/base/net_export.h" |
| 14 | #include "url/gurl.h" |
| 15 | #include "url/origin.h" |
| 16 | |
| 17 | namespace net { |
| 18 | |
Lily Chen | efb6fcf | 2019-04-19 04:17:54 | [diff] [blame] | 19 | // Identifies an endpoint group. |
| 20 | struct NET_EXPORT ReportingEndpointGroupKey { |
| 21 | ReportingEndpointGroupKey(url::Origin origin, std::string group_name); |
| 22 | |
| 23 | // Origin that configured this endpoint group. |
Sam Burnett | 00099fc1 | 2019-07-24 23:26:15 | [diff] [blame] | 24 | url::Origin origin; |
Lily Chen | efb6fcf | 2019-04-19 04:17:54 | [diff] [blame] | 25 | |
| 26 | // Name of the endpoint group (defaults to "default" during header parsing). |
Sam Burnett | 00099fc1 | 2019-07-24 23:26:15 | [diff] [blame] | 27 | std::string group_name; |
Lily Chen | efb6fcf | 2019-04-19 04:17:54 | [diff] [blame] | 28 | }; |
| 29 | |
| 30 | NET_EXPORT bool operator==(const ReportingEndpointGroupKey& lhs, |
| 31 | const ReportingEndpointGroupKey& rhs); |
| 32 | NET_EXPORT bool operator!=(const ReportingEndpointGroupKey& lhs, |
| 33 | const ReportingEndpointGroupKey& rhs); |
| 34 | NET_EXPORT bool operator<(const ReportingEndpointGroupKey& lhs, |
| 35 | const ReportingEndpointGroupKey& rhs); |
Sam Burnett | 00099fc1 | 2019-07-24 23:26:15 | [diff] [blame] | 36 | NET_EXPORT bool operator>(const ReportingEndpointGroupKey& lhs, |
| 37 | const ReportingEndpointGroupKey& rhs); |
Lily Chen | efb6fcf | 2019-04-19 04:17:54 | [diff] [blame] | 38 | |
juliatuttle | 58684333 | 2017-03-27 16:22:37 | [diff] [blame] | 39 | // The configuration by an origin to use an endpoint for report delivery. |
Lily Chen | efb6fcf | 2019-04-19 04:17:54 | [diff] [blame] | 40 | // TODO(crbug.com/921049): Rename to ReportingEndpoint because that's what it |
| 41 | // actually represents. |
| 42 | // TODO(crbug.com/912622): Track endpoint failures for garbage collection. |
Lily Chen | fc92ff4 | 2019-05-06 22:59:10 | [diff] [blame] | 43 | struct NET_EXPORT ReportingEndpoint { |
Lily Chen | efb6fcf | 2019-04-19 04:17:54 | [diff] [blame] | 44 | struct NET_EXPORT EndpointInfo { |
| 45 | static const int kDefaultPriority; |
| 46 | static const int kDefaultWeight; |
Julia Tuttle | d56350d | 2017-12-07 19:11:17 | [diff] [blame] | 47 | |
Lily Chen | efb6fcf | 2019-04-19 04:17:54 | [diff] [blame] | 48 | // The endpoint to which reports may be delivered. (Origins may configure |
| 49 | // many.) |
| 50 | GURL url; |
juliatuttle | 58684333 | 2017-03-27 16:22:37 | [diff] [blame] | 51 | |
Lily Chen | efb6fcf | 2019-04-19 04:17:54 | [diff] [blame] | 52 | // Priority when multiple endpoints are configured for an origin; endpoints |
| 53 | // with numerically lower priorities are used first. |
| 54 | int priority = kDefaultPriority; |
| 55 | |
| 56 | // Weight when multiple endpoints are configured for an origin with the same |
| 57 | // priority; among those with the same priority, each endpoint has a chance |
| 58 | // of being chosen that is proportional to its weight. |
| 59 | int weight = kDefaultWeight; |
| 60 | }; |
| 61 | |
| 62 | struct Statistics { |
| 63 | // The number of attempted uploads that we've made for this endpoint. |
| 64 | int attempted_uploads = 0; |
| 65 | // The number of uploads that have succeeded for this endpoint. |
| 66 | int successful_uploads = 0; |
| 67 | // The number of individual reports that we've attempted to upload for this |
| 68 | // endpoint. (Failed uploads will cause a report to be counted multiple |
| 69 | // times, once for each attempt.) |
| 70 | int attempted_reports = 0; |
| 71 | // The number of individual reports that we've successfully uploaded for |
| 72 | // this endpoint. |
| 73 | int successful_reports = 0; |
| 74 | }; |
| 75 | |
Lily Chen | fc92ff4 | 2019-05-06 22:59:10 | [diff] [blame] | 76 | // Constructs an invalid ReportingEndpoint. |
| 77 | ReportingEndpoint(); |
Lily Chen | efb6fcf | 2019-04-19 04:17:54 | [diff] [blame] | 78 | |
Lily Chen | fc92ff4 | 2019-05-06 22:59:10 | [diff] [blame] | 79 | ReportingEndpoint(url::Origin origin, |
| 80 | std::string group_name, |
| 81 | EndpointInfo endpoint_info); |
Lily Chen | efb6fcf | 2019-04-19 04:17:54 | [diff] [blame] | 82 | |
Lily Chen | fc92ff4 | 2019-05-06 22:59:10 | [diff] [blame] | 83 | ReportingEndpoint(const ReportingEndpoint& other); |
| 84 | ReportingEndpoint(ReportingEndpoint&& other); |
Lily Chen | efb6fcf | 2019-04-19 04:17:54 | [diff] [blame] | 85 | |
Sam Burnett | 00099fc1 | 2019-07-24 23:26:15 | [diff] [blame] | 86 | ReportingEndpoint& operator=(const ReportingEndpoint&); |
| 87 | ReportingEndpoint& operator=(ReportingEndpoint&&); |
| 88 | |
Lily Chen | fc92ff4 | 2019-05-06 22:59:10 | [diff] [blame] | 89 | ~ReportingEndpoint(); |
juliatuttle | 58684333 | 2017-03-27 16:22:37 | [diff] [blame] | 90 | |
Lily Chen | efb6fcf | 2019-04-19 04:17:54 | [diff] [blame] | 91 | bool is_valid() const; |
| 92 | explicit operator bool() const { return is_valid(); } |
juliatuttle | 58684333 | 2017-03-27 16:22:37 | [diff] [blame] | 93 | |
Lily Chen | efb6fcf | 2019-04-19 04:17:54 | [diff] [blame] | 94 | // Identifies the endpoint group to which this endpoint belongs. |
Sam Burnett | 00099fc1 | 2019-07-24 23:26:15 | [diff] [blame] | 95 | ReportingEndpointGroupKey group_key; |
juliatuttle | 58684333 | 2017-03-27 16:22:37 | [diff] [blame] | 96 | |
Lily Chen | efb6fcf | 2019-04-19 04:17:54 | [diff] [blame] | 97 | // URL, priority, and weight of the endpoint. |
| 98 | EndpointInfo info; |
juliatuttle | 58684333 | 2017-03-27 16:22:37 | [diff] [blame] | 99 | |
Lily Chen | efb6fcf | 2019-04-19 04:17:54 | [diff] [blame] | 100 | // Information about the number of deliveries that we've attempted for this |
| 101 | // endpoint. Not persisted across restarts. |
| 102 | Statistics stats; |
| 103 | }; |
juliatuttle | 58684333 | 2017-03-27 16:22:37 | [diff] [blame] | 104 | |
Lily Chen | efb6fcf | 2019-04-19 04:17:54 | [diff] [blame] | 105 | // Marks whether a given endpoint group is configured to include its origin's |
| 106 | // subdomains. |
| 107 | enum class OriginSubdomains { EXCLUDE, INCLUDE, DEFAULT = EXCLUDE }; |
juliatuttle | 58684333 | 2017-03-27 16:22:37 | [diff] [blame] | 108 | |
Lily Chen | efb6fcf | 2019-04-19 04:17:54 | [diff] [blame] | 109 | // Represents an endpoint group set by an origin via Report-To header. |
| 110 | struct NET_EXPORT ReportingEndpointGroup { |
| 111 | ReportingEndpointGroup(); |
Julia Tuttle | d56350d | 2017-12-07 19:11:17 | [diff] [blame] | 112 | |
Lily Chen | efb6fcf | 2019-04-19 04:17:54 | [diff] [blame] | 113 | ReportingEndpointGroup(const ReportingEndpointGroup& other); |
Julia Tuttle | d56350d | 2017-12-07 19:11:17 | [diff] [blame] | 114 | |
Lily Chen | efb6fcf | 2019-04-19 04:17:54 | [diff] [blame] | 115 | ~ReportingEndpointGroup(); |
| 116 | |
| 117 | // Group name. |
| 118 | std::string name; |
| 119 | |
| 120 | // Whether this group applies to subdomains of its origin. |
| 121 | OriginSubdomains include_subdomains = OriginSubdomains::DEFAULT; |
| 122 | |
| 123 | // Time for which the endpoint group remains valid after it is set. |
| 124 | base::TimeDelta ttl; |
| 125 | |
| 126 | // Endpoints in this group. |
Lily Chen | fc92ff4 | 2019-05-06 22:59:10 | [diff] [blame] | 127 | std::vector<ReportingEndpoint::EndpointInfo> endpoints; |
Lily Chen | efb6fcf | 2019-04-19 04:17:54 | [diff] [blame] | 128 | }; |
| 129 | |
| 130 | // Representation of an endpoint group used for in-memory and persistent |
| 131 | // storage. |
| 132 | struct NET_EXPORT CachedReportingEndpointGroup { |
| 133 | CachedReportingEndpointGroup(url::Origin origin, |
| 134 | std::string name, |
| 135 | OriginSubdomains include_subdomains, |
| 136 | base::Time expires, |
| 137 | base::Time last_used); |
| 138 | |
| 139 | // |origin| is the origin that set |endpoint_group|, |now| is the time at |
| 140 | // which the header was processed. |
| 141 | CachedReportingEndpointGroup(url::Origin origin, |
| 142 | const ReportingEndpointGroup& endpoint_group, |
| 143 | base::Time now); |
| 144 | |
| 145 | // Origin and group name. |
Sam Burnett | 00099fc1 | 2019-07-24 23:26:15 | [diff] [blame] | 146 | ReportingEndpointGroupKey group_key; |
Lily Chen | efb6fcf | 2019-04-19 04:17:54 | [diff] [blame] | 147 | |
| 148 | // Whether this group applies to subdomains of |group_key.origin|. |
| 149 | OriginSubdomains include_subdomains = OriginSubdomains::DEFAULT; |
| 150 | |
| 151 | // When this group's max_age expires. |
| 152 | // (base::Time is used here instead of base::TimeTicks for ease of |
| 153 | // serialization for persistent storage, and because it is more appropriate |
| 154 | // for expiration times, as per //base/time/time.h.) |
| 155 | base::Time expires; |
| 156 | |
| 157 | // Last time that this group was accessed for a delivery or updated via a |
| 158 | // new header. |
| 159 | base::Time last_used; |
juliatuttle | 58684333 | 2017-03-27 16:22:37 | [diff] [blame] | 160 | }; |
| 161 | |
| 162 | } // namespace net |
| 163 | |
Lily Chen | fc92ff4 | 2019-05-06 22:59:10 | [diff] [blame] | 164 | #endif // NET_REPORTING_REPORTING_ENDPOINT_H_ |