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 #include "net/reporting/reporting_context.h"
6
7 #include <utility>
8
9 #include "base/functional/bind.h"
10 #include "base/observer_list.h"
11 #include "base/rand_util.h"
12 #include "base/time/clock.h"
13 #include "base/time/default_clock.h"
14 #include "base/time/default_tick_clock.h"
15 #include "base/time/tick_clock.h"
16 #include "base/time/time.h"
17 #include "net/base/backoff_entry.h"
18 #include "net/base/rand_callback.h"
19 #include "net/reporting/reporting_cache_observer.h"
20 #include "net/reporting/reporting_delegate.h"
21 #include "net/reporting/reporting_delivery_agent.h"
22 #include "net/reporting/reporting_garbage_collector.h"
23 #include "net/reporting/reporting_network_change_observer.h"
24 #include "net/reporting/reporting_policy.h"
25 #include "net/reporting/reporting_uploader.h"
26
27 namespace net {
28
29 class URLRequestContext;
30
31 namespace {
32
33 class ReportingContextImpl : public ReportingContext {
34 public:
ReportingContextImpl(const ReportingPolicy & policy,URLRequestContext * request_context,ReportingCache::PersistentReportingStore * store,const base::flat_map<std::string,GURL> & enterprise_reporting_endpoints)35 ReportingContextImpl(
36 const ReportingPolicy& policy,
37 URLRequestContext* request_context,
38 ReportingCache::PersistentReportingStore* store,
39 const base::flat_map<std::string, GURL>& enterprise_reporting_endpoints)
40 : ReportingContext(policy,
41 base::DefaultClock::GetInstance(),
42 base::DefaultTickClock::GetInstance(),
43 base::BindRepeating(&base::RandInt),
44 ReportingUploader::Create(request_context),
45 ReportingDelegate::Create(request_context),
46 store,
47 enterprise_reporting_endpoints) {}
48 };
49
50 } // namespace
51
52 // static
Create(const ReportingPolicy & policy,URLRequestContext * request_context,ReportingCache::PersistentReportingStore * store,const base::flat_map<std::string,GURL> & enterprise_reporting_endpoints)53 std::unique_ptr<ReportingContext> ReportingContext::Create(
54 const ReportingPolicy& policy,
55 URLRequestContext* request_context,
56 ReportingCache::PersistentReportingStore* store,
57 const base::flat_map<std::string, GURL>& enterprise_reporting_endpoints) {
58 return std::make_unique<ReportingContextImpl>(policy, request_context, store,
59 enterprise_reporting_endpoints);
60 }
61
62 ReportingContext::~ReportingContext() = default;
63
AddCacheObserver(ReportingCacheObserver * observer)64 void ReportingContext::AddCacheObserver(ReportingCacheObserver* observer) {
65 DCHECK(!cache_observers_.HasObserver(observer));
66 cache_observers_.AddObserver(observer);
67 }
68
RemoveCacheObserver(ReportingCacheObserver * observer)69 void ReportingContext::RemoveCacheObserver(ReportingCacheObserver* observer) {
70 DCHECK(cache_observers_.HasObserver(observer));
71 cache_observers_.RemoveObserver(observer);
72 }
73
NotifyCachedReportsUpdated()74 void ReportingContext::NotifyCachedReportsUpdated() {
75 for (auto& observer : cache_observers_)
76 observer.OnReportsUpdated();
77 }
78
NotifyReportAdded(const ReportingReport * report)79 void ReportingContext::NotifyReportAdded(const ReportingReport* report) {
80 for (auto& observer : cache_observers_)
81 observer.OnReportAdded(report);
82 }
83
NotifyReportUpdated(const ReportingReport * report)84 void ReportingContext::NotifyReportUpdated(const ReportingReport* report) {
85 for (auto& observer : cache_observers_)
86 observer.OnReportUpdated(report);
87 }
88
NotifyCachedClientsUpdated()89 void ReportingContext::NotifyCachedClientsUpdated() {
90 for (auto& observer : cache_observers_)
91 observer.OnClientsUpdated();
92 }
93
NotifyEndpointsUpdatedForOrigin(const std::vector<ReportingEndpoint> & endpoints)94 void ReportingContext::NotifyEndpointsUpdatedForOrigin(
95 const std::vector<ReportingEndpoint>& endpoints) {
96 for (auto& observer : cache_observers_)
97 observer.OnEndpointsUpdatedForOrigin(endpoints);
98 }
99
IsReportDataPersisted() const100 bool ReportingContext::IsReportDataPersisted() const {
101 return store_ && policy_.persist_reports_across_restarts;
102 }
103
IsClientDataPersisted() const104 bool ReportingContext::IsClientDataPersisted() const {
105 return store_ && policy_.persist_clients_across_restarts;
106 }
107
OnShutdown()108 void ReportingContext::OnShutdown() {
109 uploader_->OnShutdown();
110 }
111
ReportingContext(const ReportingPolicy & policy,base::Clock * clock,const base::TickClock * tick_clock,const RandIntCallback & rand_callback,std::unique_ptr<ReportingUploader> uploader,std::unique_ptr<ReportingDelegate> delegate,ReportingCache::PersistentReportingStore * store,const base::flat_map<std::string,GURL> & enterprise_reporting_endpoints)112 ReportingContext::ReportingContext(
113 const ReportingPolicy& policy,
114 base::Clock* clock,
115 const base::TickClock* tick_clock,
116 const RandIntCallback& rand_callback,
117 std::unique_ptr<ReportingUploader> uploader,
118 std::unique_ptr<ReportingDelegate> delegate,
119 ReportingCache::PersistentReportingStore* store,
120 const base::flat_map<std::string, GURL>& enterprise_reporting_endpoints)
121 : policy_(policy),
122 clock_(clock),
123 tick_clock_(tick_clock),
124 uploader_(std::move(uploader)),
125 delegate_(std::move(delegate)),
126 cache_(ReportingCache::Create(this, enterprise_reporting_endpoints)),
127 store_(store),
128 delivery_agent_(ReportingDeliveryAgent::Create(this, rand_callback)),
129 garbage_collector_(ReportingGarbageCollector::Create(this)),
130 network_change_observer_(ReportingNetworkChangeObserver::Create(this)) {}
131
132 } // namespace net
133