xref: /aosp_15_r20/external/cronet/net/reporting/reporting_context.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_CONTEXT_H_
6 #define NET_REPORTING_REPORTING_CONTEXT_H_
7 
8 #include <memory>
9 
10 #include "base/memory/raw_ptr.h"
11 #include "base/observer_list.h"
12 #include "net/base/backoff_entry.h"
13 #include "net/base/net_export.h"
14 #include "net/base/rand_callback.h"
15 #include "net/reporting/reporting_cache.h"
16 #include "net/reporting/reporting_policy.h"
17 
18 namespace base {
19 class Clock;
20 class TickClock;
21 }  // namespace base
22 
23 namespace net {
24 
25 class ReportingCacheObserver;
26 class ReportingDelegate;
27 class ReportingDeliveryAgent;
28 class ReportingGarbageCollector;
29 class ReportingNetworkChangeObserver;
30 class ReportingUploader;
31 class URLRequestContext;
32 
33 // Contains the various internal classes that make up the Reporting system.
34 // Wrapped by ReportingService, which provides the external interface.
35 class NET_EXPORT ReportingContext {
36  public:
37   // |request_context| and |store| should outlive the ReportingContext.
38   static std::unique_ptr<ReportingContext> Create(
39       const ReportingPolicy& policy,
40       URLRequestContext* request_context,
41       ReportingCache::PersistentReportingStore* store);
42 
43   ReportingContext(const ReportingContext&) = delete;
44   ReportingContext& operator=(const ReportingContext&) = delete;
45 
46   virtual ~ReportingContext();
47 
policy()48   const ReportingPolicy& policy() const { return policy_; }
49 
clock()50   const base::Clock& clock() const { return *clock_; }
tick_clock()51   const base::TickClock& tick_clock() const { return *tick_clock_; }
uploader()52   ReportingUploader* uploader() { return uploader_.get(); }
delegate()53   ReportingDelegate* delegate() { return delegate_.get(); }
cache()54   ReportingCache* cache() { return cache_.get(); }
store()55   ReportingCache::PersistentReportingStore* store() { return store_; }
delivery_agent()56   ReportingDeliveryAgent* delivery_agent() { return delivery_agent_.get(); }
garbage_collector()57   ReportingGarbageCollector* garbage_collector() {
58     return garbage_collector_.get();
59   }
60 
61   void AddCacheObserver(ReportingCacheObserver* observer);
62   void RemoveCacheObserver(ReportingCacheObserver* observer);
63 
64   void NotifyCachedReportsUpdated();
65   void NotifyReportAdded(const ReportingReport* report);
66   void NotifyReportUpdated(const ReportingReport* report);
67   void NotifyCachedClientsUpdated();
68   void NotifyEndpointsUpdatedForOrigin(
69       const std::vector<ReportingEndpoint>& endpoints);
70 
71   // Returns whether the data in the cache is persisted across restarts in the
72   // PersistentReportingStore.
73   bool IsReportDataPersisted() const;
74   bool IsClientDataPersisted() const;
75 
76   void OnShutdown();
77 
78  protected:
79   ReportingContext(const ReportingPolicy& policy,
80                    base::Clock* clock,
81                    const base::TickClock* tick_clock,
82                    const RandIntCallback& rand_callback,
83                    std::unique_ptr<ReportingUploader> uploader,
84                    std::unique_ptr<ReportingDelegate> delegate,
85                    ReportingCache::PersistentReportingStore* store);
86 
87  private:
88   ReportingPolicy policy_;
89 
90   raw_ptr<base::Clock> clock_;
91   raw_ptr<const base::TickClock> tick_clock_;
92   std::unique_ptr<ReportingUploader> uploader_;
93 
94   base::ObserverList<ReportingCacheObserver, /* check_empty= */ true>::Unchecked
95       cache_observers_;
96 
97   std::unique_ptr<ReportingDelegate> delegate_;
98 
99   std::unique_ptr<ReportingCache> cache_;
100 
101   const raw_ptr<ReportingCache::PersistentReportingStore, DanglingUntriaged>
102       store_;
103 
104   // |delivery_agent_| must come after |tick_clock_|, |delegate_|, |uploader_|,
105   // and |cache_|.
106   std::unique_ptr<ReportingDeliveryAgent> delivery_agent_;
107 
108   // |garbage_collector_| must come after |tick_clock_| and |cache_|.
109   std::unique_ptr<ReportingGarbageCollector> garbage_collector_;
110 
111   // |network_change_observer_| must come after |cache_|.
112   std::unique_ptr<ReportingNetworkChangeObserver> network_change_observer_;
113 };
114 
115 }  // namespace net
116 
117 #endif  // NET_REPORTING_REPORTING_CONTEXT_H_
118