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 const base::flat_map<std::string, GURL>& enterprise_reporting_endpoints); 43 44 ReportingContext(const ReportingContext&) = delete; 45 ReportingContext& operator=(const ReportingContext&) = delete; 46 47 virtual ~ReportingContext(); 48 policy()49 const ReportingPolicy& policy() const { return policy_; } 50 clock()51 const base::Clock& clock() const { return *clock_; } tick_clock()52 const base::TickClock& tick_clock() const { return *tick_clock_; } uploader()53 ReportingUploader* uploader() { return uploader_.get(); } delegate()54 ReportingDelegate* delegate() { return delegate_.get(); } cache()55 ReportingCache* cache() { return cache_.get(); } store()56 ReportingCache::PersistentReportingStore* store() { return store_; } delivery_agent()57 ReportingDeliveryAgent* delivery_agent() { return delivery_agent_.get(); } garbage_collector()58 ReportingGarbageCollector* garbage_collector() { 59 return garbage_collector_.get(); 60 } 61 62 void AddCacheObserver(ReportingCacheObserver* observer); 63 void RemoveCacheObserver(ReportingCacheObserver* observer); 64 65 void NotifyCachedReportsUpdated(); 66 void NotifyReportAdded(const ReportingReport* report); 67 void NotifyReportUpdated(const ReportingReport* report); 68 void NotifyCachedClientsUpdated(); 69 void NotifyEndpointsUpdatedForOrigin( 70 const std::vector<ReportingEndpoint>& endpoints); 71 72 // Returns whether the data in the cache is persisted across restarts in the 73 // PersistentReportingStore. 74 bool IsReportDataPersisted() const; 75 bool IsClientDataPersisted() const; 76 77 void OnShutdown(); 78 79 protected: 80 ReportingContext( 81 const ReportingPolicy& policy, 82 base::Clock* clock, 83 const base::TickClock* tick_clock, 84 const RandIntCallback& rand_callback, 85 std::unique_ptr<ReportingUploader> uploader, 86 std::unique_ptr<ReportingDelegate> delegate, 87 ReportingCache::PersistentReportingStore* store, 88 const base::flat_map<std::string, GURL>& enterprise_reporting_endpoints); 89 90 private: 91 ReportingPolicy policy_; 92 93 raw_ptr<base::Clock> clock_; 94 raw_ptr<const base::TickClock> tick_clock_; 95 std::unique_ptr<ReportingUploader> uploader_; 96 97 base::ObserverList<ReportingCacheObserver, /* check_empty= */ true>::Unchecked 98 cache_observers_; 99 100 std::unique_ptr<ReportingDelegate> delegate_; 101 102 std::unique_ptr<ReportingCache> cache_; 103 104 const raw_ptr<ReportingCache::PersistentReportingStore> store_; 105 106 // |delivery_agent_| must come after |tick_clock_|, |delegate_|, |uploader_|, 107 // and |cache_|. 108 std::unique_ptr<ReportingDeliveryAgent> delivery_agent_; 109 110 // |garbage_collector_| must come after |tick_clock_| and |cache_|. 111 std::unique_ptr<ReportingGarbageCollector> garbage_collector_; 112 113 // |network_change_observer_| must come after |cache_|. 114 std::unique_ptr<ReportingNetworkChangeObserver> network_change_observer_; 115 }; 116 117 } // namespace net 118 119 #endif // NET_REPORTING_REPORTING_CONTEXT_H_ 120