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