xref: /aosp_15_r20/external/cronet/net/reporting/reporting_endpoint_manager.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_ENDPOINT_MANAGER_H_
6 #define NET_REPORTING_REPORTING_ENDPOINT_MANAGER_H_
7 
8 #include <memory>
9 
10 #include "net/base/net_export.h"
11 #include "net/base/rand_callback.h"
12 #include "net/reporting/reporting_endpoint.h"
13 
14 class GURL;
15 
16 namespace base {
17 class TickClock;
18 }
19 
20 namespace net {
21 
22 class NetworkAnonymizationKey;
23 class ReportingCache;
24 class ReportingDelegate;
25 struct ReportingEndpoint;
26 struct ReportingPolicy;
27 
28 // Keeps track of which endpoints are pending (have active delivery attempts to
29 // them) or in exponential backoff after one or more failures, and chooses an
30 // endpoint from an endpoint group to receive reports for an origin.
31 class NET_EXPORT ReportingEndpointManager {
32  public:
33   // Maximum size of the backoff cache. This is deliberately much larger than is
34   // likely necessary - the only goal is to prevent it from growing without
35   // bound, while preventing repeatedly trying to upload data to down servrs.
36   // Public for tests.
37   static constexpr int kMaxEndpointBackoffCacheSize = 200;
38 
39   // The ReportingEndpointManager must not be used after any of the objects
40   // passed to its constructor are destroyed.
41   static std::unique_ptr<ReportingEndpointManager> Create(
42       const ReportingPolicy* policy,
43       const base::TickClock* tick_clock,
44       const ReportingDelegate* delegate,
45       ReportingCache* cache,
46       const RandIntCallback& rand_callback);
47 
48   virtual ~ReportingEndpointManager();
49 
50   // Finds an endpoint that applies to deliveries to the group identified by
51   // |group_key| that are not expired or in exponential backoff from failed
52   // requests. The returned endpoint may have been configured by a superdomain
53   // of the group's origin. Deliberately chooses an endpoint randomly to ensure
54   // sites aren't relying on any sort of fallback ordering. If no suitable
55   // endpoint was found, returns an endpoint with is_valid() false.
56   virtual const ReportingEndpoint FindEndpointForDelivery(
57       const ReportingEndpointGroupKey& group_key) = 0;
58 
59   // Informs the EndpointManager of a successful or unsuccessful request made to
60   // |endpoint| so it can manage exponential backoff of failing endpoints.
61   virtual void InformOfEndpointRequest(
62       const NetworkAnonymizationKey& network_anonymization_key,
63       const GURL& endpoint,
64       bool succeeded) = 0;
65 };
66 
67 }  // namespace net
68 
69 #endif  // NET_REPORTING_REPORTING_ENDPOINT_MANAGER_H_
70