xref: /aosp_15_r20/external/cronet/net/reporting/reporting_delivery_agent.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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_DELIVERY_AGENT_H_
6 #define NET_REPORTING_REPORTING_DELIVERY_AGENT_H_
7 
8 #include <memory>
9 
10 #include "base/unguessable_token.h"
11 #include "net/base/net_export.h"
12 #include "net/base/rand_callback.h"
13 
14 namespace base {
15 class OneShotTimer;
16 }  // namespace base
17 
18 namespace net {
19 
20 class ReportingContext;
21 
22 // Batches reports fetched from the ReportingCache and uploads them using the
23 // ReportingUploader.
24 //
25 // Reports are only considered for delivery if all of the following are true:
26 //  - The report is not already part of a pending upload request.
27 //  - Uploads are allowed for the report's origin (i.e. the origin of the URL
28 //    associated with the reported event).
29 //  - There is not already a pending upload for any reports sharing the same
30 //    (NAK, origin, group) key.
31 //
32 // Reports are batched for upload to an endpoint URL such that:
33 //  - The available reports with the same (NAK, origin, group) are always
34 //    uploaded together.
35 //  - All reports uploaded together must share a NAK and origin.
36 //  - Reports for the same (NAK, origin) can be uploaded separately if they are
37 //    for different groups.
38 //  - Reports for different groups can be batched together, if they are assigned
39 //    to ReportingEndpoints sharing a URL (that is, the upload URL).
40 //
41 // There is no limit to the number of reports that can be uploaded together.
42 // (Aside from the global cap on total reports.)
43 //
44 // TODO(juliatuttle): Consider capping the maximum number of reports per
45 // delivery attempt.
46 class NET_EXPORT ReportingDeliveryAgent {
47  public:
48   // These values are persisted to logs. Entries should not be renumbered and
49   // numeric values should never be reused.
50   // They should also be kept in sync with the NetReportingUploadHeaderType
51   // enum in tools/metrics/histograms/enums.xml
52   enum class ReportingUploadHeaderType {
53     kReportTo = 0,
54     kReportingEndpoints = 1,
55     kMaxValue = kReportingEndpoints
56   };
57   // Creates a ReportingDeliveryAgent. |context| must outlive the agent.
58   static std::unique_ptr<ReportingDeliveryAgent> Create(
59       ReportingContext* context,
60       const RandIntCallback& rand_callback);
61 
62   virtual ~ReportingDeliveryAgent();
63 
64   // Replaces the internal OneShotTimer used for scheduling report delivery
65   // attempts with a caller-specified one so that unittests can provide a
66   // MockOneShotTimer.
67   virtual void SetTimerForTesting(
68       std::unique_ptr<base::OneShotTimer> timer) = 0;
69 
70   // Bypasses the schedule to attempt delivery of all outstanding reports
71   // for a single `reporting_source`. Called when the source document or worker
72   // is being destroyed.
73   virtual void SendReportsForSource(
74       base::UnguessableToken reporting_source) = 0;
75 };
76 
77 }  // namespace net
78 
79 #endif  // NET_REPORTING_REPORTING_DELIVERY_AGENT_H_
80