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_garbage_collector.h" | 5 #include "net/reporting/reporting_garbage_collector.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
10 #include "base/time/tick_clock.h" | 10 #include "base/time/tick_clock.h" |
11 #include "base/time/time.h" | 11 #include "base/time/time.h" |
12 #include "base/timer/timer.h" | 12 #include "base/timer/timer.h" |
13 #include "net/reporting/reporting_cache.h" | 13 #include "net/reporting/reporting_cache.h" |
14 #include "net/reporting/reporting_context.h" | 14 #include "net/reporting/reporting_context.h" |
15 #include "net/reporting/reporting_observer.h" | 15 #include "net/reporting/reporting_observer.h" |
16 #include "net/reporting/reporting_policy.h" | 16 #include "net/reporting/reporting_policy.h" |
17 #include "net/reporting/reporting_report.h" | 17 #include "net/reporting/reporting_report.h" |
18 | 18 |
19 namespace net { | 19 namespace net { |
20 | 20 |
21 namespace { | 21 namespace { |
22 | 22 |
23 class ReportingGarbageCollectorImpl : public ReportingGarbageCollector, | 23 class ReportingGarbageCollectorImpl : public ReportingGarbageCollector, |
24 public ReportingObserver { | 24 public ReportingObserver { |
25 public: | 25 public: |
26 ReportingGarbageCollectorImpl(ReportingContext* context) | 26 ReportingGarbageCollectorImpl(ReportingContext* context) |
27 : context_(context), timer_(base::MakeUnique<base::OneShotTimer>()) { | 27 : context_(context), timer_(base::MakeUnique<base::OneShotTimer>()) {} |
28 context_->AddObserver(this); | 28 |
| 29 // ReportingGarbageCollector implementation: |
| 30 |
| 31 ~ReportingGarbageCollectorImpl() override { |
| 32 DCHECK(context_->initialized()); |
| 33 context_->RemoveObserver(this); |
29 } | 34 } |
30 | 35 |
31 // ReportingGarbageCollector implementation: | 36 void Initialize() override { |
32 // | 37 context_->AddObserver(this); |
33 ~ReportingGarbageCollectorImpl() override { context_->RemoveObserver(this); } | 38 CollectGarbage(); |
| 39 } |
34 | 40 |
35 void SetTimerForTesting(std::unique_ptr<base::Timer> timer) override { | 41 void SetTimerForTesting(std::unique_ptr<base::Timer> timer) override { |
36 DCHECK(!timer_->IsRunning()); | 42 DCHECK(!context_->initialized()); |
37 timer_ = std::move(timer); | 43 timer_ = std::move(timer); |
38 } | 44 } |
39 | 45 |
40 // ReportingObserver implementation: | 46 // ReportingObserver implementation: |
41 void OnCacheUpdated() override { | 47 void OnCacheUpdated() override { |
| 48 DCHECK(context_->initialized()); |
42 if (!timer_->IsRunning()) | 49 if (!timer_->IsRunning()) |
43 StartTimer(); | 50 StartTimer(); |
44 } | 51 } |
45 | 52 |
46 private: | 53 private: |
47 void StartTimer() { | 54 void StartTimer() { |
48 timer_->Start(FROM_HERE, context_->policy().garbage_collection_interval, | 55 timer_->Start(FROM_HERE, context_->policy().garbage_collection_interval, |
49 base::Bind(&ReportingGarbageCollectorImpl::CollectGarbage, | 56 base::Bind(&ReportingGarbageCollectorImpl::CollectGarbage, |
50 base::Unretained(this))); | 57 base::Unretained(this))); |
51 } | 58 } |
(...skipping 27 matching lines...) Expand all Loading... |
79 | 86 |
80 // static | 87 // static |
81 std::unique_ptr<ReportingGarbageCollector> ReportingGarbageCollector::Create( | 88 std::unique_ptr<ReportingGarbageCollector> ReportingGarbageCollector::Create( |
82 ReportingContext* context) { | 89 ReportingContext* context) { |
83 return base::MakeUnique<ReportingGarbageCollectorImpl>(context); | 90 return base::MakeUnique<ReportingGarbageCollectorImpl>(context); |
84 } | 91 } |
85 | 92 |
86 ReportingGarbageCollector::~ReportingGarbageCollector() {} | 93 ReportingGarbageCollector::~ReportingGarbageCollector() {} |
87 | 94 |
88 } // namespace net | 95 } // namespace net |
OLD | NEW |