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_REPORT_H_ 6 #define NET_REPORTING_REPORTING_REPORT_H_ 7 8 #include <memory> 9 #include <optional> 10 #include <string> 11 12 #include "base/time/time.h" 13 #include "base/unguessable_token.h" 14 #include "base/values.h" 15 #include "net/base/net_export.h" 16 #include "net/base/network_anonymization_key.h" 17 #include "net/reporting/reporting_endpoint.h" 18 #include "url/gurl.h" 19 20 namespace net { 21 22 // An undelivered report. 23 struct NET_EXPORT ReportingReport { 24 public: 25 enum class Status { 26 // Report has been queued and no attempt has been made to deliver it yet, 27 // or attempted previous upload failed (impermanently). 28 QUEUED, 29 30 // There is an ongoing attempt to upload this report. 31 PENDING, 32 33 // Deletion of this report was requested while it was pending, so it should 34 // be removed after the attempted upload completes. 35 DOOMED, 36 37 // Similar to DOOMED with the difference that the upload was already 38 // successful. 39 SUCCESS, 40 }; 41 42 // TODO(chlily): Remove |attempts| argument as it is (almost?) always 0. 43 ReportingReport(const std::optional<base::UnguessableToken>& reporting_source, 44 const NetworkAnonymizationKey& network_anonymization_key, 45 const GURL& url, 46 const std::string& user_agent, 47 const std::string& group, 48 const std::string& type, 49 base::Value::Dict body, 50 int depth, 51 base::TimeTicks queued, 52 int attempts); 53 54 // Do NOT use this constructor outside of mojo deserialization context. 55 ReportingReport(); 56 ReportingReport(const ReportingReport&) = delete; 57 ReportingReport(ReportingReport&& other); 58 ReportingReport& operator=(const ReportingReport&) = delete; 59 ReportingReport& operator=(ReportingReport&& other); 60 ~ReportingReport(); 61 62 // Bundles together the NAK, origin of the report URL, and group name. 63 // This is not exactly the same as the group key of the endpoint that the 64 // report will be delivered to. The origin may differ if the endpoint is 65 // configured for a superdomain of the report's origin. The NAK and group name 66 // will be the same. 67 ReportingEndpointGroupKey GetGroupKey() const; 68 69 static void RecordReportDiscardedForNoURLRequestContext(); 70 static void RecordReportDiscardedForNoReportingService(); 71 72 // Whether the report is part of an ongoing delivery attempt. 73 bool IsUploadPending() const; 74 75 // The reporting source token for the document or worker which triggered this 76 // report, if it can be associated with one, or nullopt otherwise (Network 77 // reports, such as NEL, for instance, do not support such attribution.) 78 // This is used to identify appropriate endpoints to deliver this report to; 79 // reports with an attached source token may be delivered to a named endpoint 80 // with a matching source, but are also eligible to be delivered to an 81 // endpoint group without a source. Reports without a source token can only be 82 // delivered to endpoint groups without one. 83 // (Not included in the delivered report.) 84 std::optional<base::UnguessableToken> reporting_source; 85 86 // The NAK of the request that triggered this report. (Not included in the 87 // delivered report.) 88 NetworkAnonymizationKey network_anonymization_key; 89 90 // The id of the report, used by DevTools to identify and tell apart 91 // individual reports. 92 base::UnguessableToken id; 93 94 // The URL of the document that triggered the report. (Included in the 95 // delivered report.) 96 GURL url; 97 98 // The User-Agent header that was used for the request. 99 std::string user_agent; 100 101 // The endpoint group that should be used to deliver the report. (Not included 102 // in the delivered report.) 103 std::string group; 104 105 // The type of the report. (Included in the delivered report.) 106 std::string type; 107 108 // The body of the report. (Included in the delivered report.) 109 base::Value::Dict body; 110 111 // How many uploads deep the related request was: 0 if the related request was 112 // not an upload (or there was no related request), or n+1 if it was an upload 113 // reporting on requests of at most depth n. 114 int depth; 115 116 // When the report was queued. (Included in the delivered report as an age 117 // relative to the time of the delivery attempt.) 118 base::TimeTicks queued; 119 120 // The number of delivery attempts made so far, not including an active 121 // attempt. (Not included in the delivered report.) 122 int attempts = 0; 123 124 Status status = Status::QUEUED; 125 }; 126 127 } // namespace net 128 129 #endif // NET_REPORTING_REPORTING_REPORT_H_ 130