blob: a6801e2ea2ab07cbf207d56096454d864ed11620 [file] [log] [blame]
juliatuttle586843332017-03-27 16:22:371// 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 Chenfc92ff42019-05-06 22:59:105#ifndef NET_REPORTING_REPORTING_ENDPOINT_H_
6#define NET_REPORTING_REPORTING_ENDPOINT_H_
juliatuttle586843332017-03-27 16:22:377
8#include <string>
Lily Chenefb6fcf2019-04-19 04:17:549#include <vector>
juliatuttle586843332017-03-27 16:22:3710
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
17namespace net {
18
Lily Chenefb6fcf2019-04-19 04:17:5419// Identifies an endpoint group.
20struct NET_EXPORT ReportingEndpointGroupKey {
21 ReportingEndpointGroupKey(url::Origin origin, std::string group_name);
22
23 // Origin that configured this endpoint group.
Sam Burnett00099fc12019-07-24 23:26:1524 url::Origin origin;
Lily Chenefb6fcf2019-04-19 04:17:5425
26 // Name of the endpoint group (defaults to "default" during header parsing).
Sam Burnett00099fc12019-07-24 23:26:1527 std::string group_name;
Lily Chenefb6fcf2019-04-19 04:17:5428};
29
30NET_EXPORT bool operator==(const ReportingEndpointGroupKey& lhs,
31 const ReportingEndpointGroupKey& rhs);
32NET_EXPORT bool operator!=(const ReportingEndpointGroupKey& lhs,
33 const ReportingEndpointGroupKey& rhs);
34NET_EXPORT bool operator<(const ReportingEndpointGroupKey& lhs,
35 const ReportingEndpointGroupKey& rhs);
Sam Burnett00099fc12019-07-24 23:26:1536NET_EXPORT bool operator>(const ReportingEndpointGroupKey& lhs,
37 const ReportingEndpointGroupKey& rhs);
Lily Chenefb6fcf2019-04-19 04:17:5438
juliatuttle586843332017-03-27 16:22:3739// The configuration by an origin to use an endpoint for report delivery.
Lily Chenefb6fcf2019-04-19 04:17:5440// 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 Chenfc92ff42019-05-06 22:59:1043struct NET_EXPORT ReportingEndpoint {
Lily Chenefb6fcf2019-04-19 04:17:5444 struct NET_EXPORT EndpointInfo {
45 static const int kDefaultPriority;
46 static const int kDefaultWeight;
Julia Tuttled56350d2017-12-07 19:11:1747
Lily Chenefb6fcf2019-04-19 04:17:5448 // The endpoint to which reports may be delivered. (Origins may configure
49 // many.)
50 GURL url;
juliatuttle586843332017-03-27 16:22:3751
Lily Chenefb6fcf2019-04-19 04:17:5452 // 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 Chenfc92ff42019-05-06 22:59:1076 // Constructs an invalid ReportingEndpoint.
77 ReportingEndpoint();
Lily Chenefb6fcf2019-04-19 04:17:5478
Lily Chenfc92ff42019-05-06 22:59:1079 ReportingEndpoint(url::Origin origin,
80 std::string group_name,
81 EndpointInfo endpoint_info);
Lily Chenefb6fcf2019-04-19 04:17:5482
Lily Chenfc92ff42019-05-06 22:59:1083 ReportingEndpoint(const ReportingEndpoint& other);
84 ReportingEndpoint(ReportingEndpoint&& other);
Lily Chenefb6fcf2019-04-19 04:17:5485
Sam Burnett00099fc12019-07-24 23:26:1586 ReportingEndpoint& operator=(const ReportingEndpoint&);
87 ReportingEndpoint& operator=(ReportingEndpoint&&);
88
Lily Chenfc92ff42019-05-06 22:59:1089 ~ReportingEndpoint();
juliatuttle586843332017-03-27 16:22:3790
Lily Chenefb6fcf2019-04-19 04:17:5491 bool is_valid() const;
92 explicit operator bool() const { return is_valid(); }
juliatuttle586843332017-03-27 16:22:3793
Lily Chenefb6fcf2019-04-19 04:17:5494 // Identifies the endpoint group to which this endpoint belongs.
Sam Burnett00099fc12019-07-24 23:26:1595 ReportingEndpointGroupKey group_key;
juliatuttle586843332017-03-27 16:22:3796
Lily Chenefb6fcf2019-04-19 04:17:5497 // URL, priority, and weight of the endpoint.
98 EndpointInfo info;
juliatuttle586843332017-03-27 16:22:3799
Lily Chenefb6fcf2019-04-19 04:17:54100 // Information about the number of deliveries that we've attempted for this
101 // endpoint. Not persisted across restarts.
102 Statistics stats;
103};
juliatuttle586843332017-03-27 16:22:37104
Lily Chenefb6fcf2019-04-19 04:17:54105// Marks whether a given endpoint group is configured to include its origin's
106// subdomains.
107enum class OriginSubdomains { EXCLUDE, INCLUDE, DEFAULT = EXCLUDE };
juliatuttle586843332017-03-27 16:22:37108
Lily Chenefb6fcf2019-04-19 04:17:54109// Represents an endpoint group set by an origin via Report-To header.
110struct NET_EXPORT ReportingEndpointGroup {
111 ReportingEndpointGroup();
Julia Tuttled56350d2017-12-07 19:11:17112
Lily Chenefb6fcf2019-04-19 04:17:54113 ReportingEndpointGroup(const ReportingEndpointGroup& other);
Julia Tuttled56350d2017-12-07 19:11:17114
Lily Chenefb6fcf2019-04-19 04:17:54115 ~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 Chenfc92ff42019-05-06 22:59:10127 std::vector<ReportingEndpoint::EndpointInfo> endpoints;
Lily Chenefb6fcf2019-04-19 04:17:54128};
129
130// Representation of an endpoint group used for in-memory and persistent
131// storage.
132struct 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 Burnett00099fc12019-07-24 23:26:15146 ReportingEndpointGroupKey group_key;
Lily Chenefb6fcf2019-04-19 04:17:54147
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;
juliatuttle586843332017-03-27 16:22:37160};
161
162} // namespace net
163
Lily Chenfc92ff42019-05-06 22:59:10164#endif // NET_REPORTING_REPORTING_ENDPOINT_H_