reporting_endpoint.h 7.0 KB

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