OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/reporting/reporting_context.h" | 5 #include "net/reporting/reporting_context.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 | 8 |
9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
10 #include "base/observer_list.h" | 10 #include "base/observer_list.h" |
11 #include "base/time/clock.h" | 11 #include "base/time/clock.h" |
12 #include "base/time/default_clock.h" | 12 #include "base/time/default_clock.h" |
13 #include "base/time/default_tick_clock.h" | 13 #include "base/time/default_tick_clock.h" |
14 #include "base/time/tick_clock.h" | 14 #include "base/time/tick_clock.h" |
15 #include "base/time/time.h" | 15 #include "base/time/time.h" |
16 #include "net/base/backoff_entry.h" | 16 #include "net/base/backoff_entry.h" |
17 #include "net/reporting/reporting_cache.h" | 17 #include "net/reporting/reporting_cache.h" |
18 #include "net/reporting/reporting_delegate.h" | |
19 #include "net/reporting/reporting_delivery_agent.h" | 18 #include "net/reporting/reporting_delivery_agent.h" |
20 #include "net/reporting/reporting_endpoint_manager.h" | 19 #include "net/reporting/reporting_endpoint_manager.h" |
21 #include "net/reporting/reporting_garbage_collector.h" | 20 #include "net/reporting/reporting_garbage_collector.h" |
22 #include "net/reporting/reporting_network_change_observer.h" | 21 #include "net/reporting/reporting_network_change_observer.h" |
23 #include "net/reporting/reporting_observer.h" | 22 #include "net/reporting/reporting_observer.h" |
24 #include "net/reporting/reporting_persister.h" | 23 #include "net/reporting/reporting_persister.h" |
25 #include "net/reporting/reporting_policy.h" | 24 #include "net/reporting/reporting_policy.h" |
26 #include "net/reporting/reporting_uploader.h" | 25 #include "net/reporting/reporting_uploader.h" |
27 | 26 |
28 namespace net { | 27 namespace net { |
29 | 28 |
30 class URLRequestContext; | 29 class URLRequestContext; |
31 | 30 |
32 namespace { | 31 namespace { |
33 | 32 |
34 class ReportingContextImpl : public ReportingContext { | 33 class ReportingContextImpl : public ReportingContext { |
35 public: | 34 public: |
36 ReportingContextImpl(const ReportingPolicy& policy, | 35 ReportingContextImpl(const ReportingPolicy& policy, |
37 std::unique_ptr<ReportingDelegate> delegate, | |
38 URLRequestContext* request_context) | 36 URLRequestContext* request_context) |
39 : ReportingContext(policy, | 37 : ReportingContext(policy, |
40 std::move(delegate), | |
41 base::MakeUnique<base::DefaultClock>(), | 38 base::MakeUnique<base::DefaultClock>(), |
42 base::MakeUnique<base::DefaultTickClock>(), | 39 base::MakeUnique<base::DefaultTickClock>(), |
43 ReportingUploader::Create(request_context)) {} | 40 ReportingUploader::Create(request_context)) {} |
44 }; | 41 }; |
45 | 42 |
46 } // namespace | 43 } // namespace |
47 | 44 |
48 // static | 45 // static |
49 std::unique_ptr<ReportingContext> ReportingContext::Create( | 46 std::unique_ptr<ReportingContext> ReportingContext::Create( |
50 const ReportingPolicy& policy, | 47 const ReportingPolicy& policy, |
51 std::unique_ptr<ReportingDelegate> delegate, | |
52 URLRequestContext* request_context) { | 48 URLRequestContext* request_context) { |
53 return base::MakeUnique<ReportingContextImpl>(policy, std::move(delegate), | 49 return base::MakeUnique<ReportingContextImpl>(policy, request_context); |
54 request_context); | |
55 } | 50 } |
56 | 51 |
57 ReportingContext::~ReportingContext() {} | 52 ReportingContext::~ReportingContext() {} |
58 | 53 |
59 void ReportingContext::Initialize() { | |
60 DCHECK(!initialized_); | |
61 | |
62 // This order isn't *critical*, but things will work better with it in this | |
63 // order: with the DeliveryAgent after the Persister, it can schedule delivery | |
64 // of persisted reports instead of waiting for a new one to be generated, and | |
65 // with the GarbageCollector in between, it won't bother scheduling delivery | |
66 // of reports that should be discarded instead. | |
67 persister_->Initialize(); | |
68 garbage_collector_->Initialize(); | |
69 delivery_agent_->Initialize(); | |
70 | |
71 initialized_ = true; | |
72 } | |
73 | |
74 void ReportingContext::AddObserver(ReportingObserver* observer) { | 54 void ReportingContext::AddObserver(ReportingObserver* observer) { |
75 DCHECK(!observers_.HasObserver(observer)); | 55 DCHECK(!observers_.HasObserver(observer)); |
76 observers_.AddObserver(observer); | 56 observers_.AddObserver(observer); |
77 } | 57 } |
78 | 58 |
79 void ReportingContext::RemoveObserver(ReportingObserver* observer) { | 59 void ReportingContext::RemoveObserver(ReportingObserver* observer) { |
80 DCHECK(observers_.HasObserver(observer)); | 60 DCHECK(observers_.HasObserver(observer)); |
81 observers_.RemoveObserver(observer); | 61 observers_.RemoveObserver(observer); |
82 } | 62 } |
83 | 63 |
84 void ReportingContext::NotifyCacheUpdated() { | 64 void ReportingContext::NotifyCacheUpdated() { |
85 if (!initialized_) | |
86 return; | |
87 | |
88 for (auto& observer : observers_) | 65 for (auto& observer : observers_) |
89 observer.OnCacheUpdated(); | 66 observer.OnCacheUpdated(); |
90 } | 67 } |
91 | 68 |
92 ReportingContext::ReportingContext(const ReportingPolicy& policy, | 69 ReportingContext::ReportingContext(const ReportingPolicy& policy, |
93 std::unique_ptr<ReportingDelegate> delegate, | |
94 std::unique_ptr<base::Clock> clock, | 70 std::unique_ptr<base::Clock> clock, |
95 std::unique_ptr<base::TickClock> tick_clock, | 71 std::unique_ptr<base::TickClock> tick_clock, |
96 std::unique_ptr<ReportingUploader> uploader) | 72 std::unique_ptr<ReportingUploader> uploader) |
97 : policy_(policy), | 73 : policy_(policy), |
98 delegate_(std::move(delegate)), | |
99 clock_(std::move(clock)), | 74 clock_(std::move(clock)), |
100 tick_clock_(std::move(tick_clock)), | 75 tick_clock_(std::move(tick_clock)), |
101 uploader_(std::move(uploader)), | 76 uploader_(std::move(uploader)), |
102 initialized_(false), | |
103 cache_(base::MakeUnique<ReportingCache>(this)), | 77 cache_(base::MakeUnique<ReportingCache>(this)), |
104 endpoint_manager_(base::MakeUnique<ReportingEndpointManager>(this)), | 78 endpoint_manager_(base::MakeUnique<ReportingEndpointManager>(this)), |
105 delivery_agent_(ReportingDeliveryAgent::Create(this)), | 79 delivery_agent_(ReportingDeliveryAgent::Create(this)), |
106 persister_(ReportingPersister::Create(this)), | 80 persister_(ReportingPersister::Create(this)), |
107 garbage_collector_(ReportingGarbageCollector::Create(this)), | 81 garbage_collector_(ReportingGarbageCollector::Create(this)), |
108 network_change_observer_(ReportingNetworkChangeObserver::Create(this)) {} | 82 network_change_observer_(ReportingNetworkChangeObserver::Create(this)) {} |
109 | 83 |
110 } // namespace net | 84 } // namespace net |
OLD | NEW |