| 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); |
| 29 } |
| 28 | 30 |
| 29 // ReportingGarbageCollector implementation: | 31 // ReportingGarbageCollector implementation: |
| 30 | 32 |
| 31 ~ReportingGarbageCollectorImpl() override { | 33 ~ReportingGarbageCollectorImpl() override { |
| 32 DCHECK(context_->initialized()); | |
| 33 context_->RemoveObserver(this); | 34 context_->RemoveObserver(this); |
| 34 } | 35 } |
| 35 | 36 |
| 36 void Initialize() override { | |
| 37 context_->AddObserver(this); | |
| 38 CollectGarbage(); | |
| 39 } | |
| 40 | |
| 41 void SetTimerForTesting(std::unique_ptr<base::Timer> timer) override { | 37 void SetTimerForTesting(std::unique_ptr<base::Timer> timer) override { |
| 42 DCHECK(!context_->initialized()); | |
| 43 timer_ = std::move(timer); | 38 timer_ = std::move(timer); |
| 44 } | 39 } |
| 45 | 40 |
| 46 // ReportingObserver implementation: | 41 // ReportingObserver implementation: |
| 47 void OnCacheUpdated() override { | 42 void OnCacheUpdated() override { |
| 48 DCHECK(context_->initialized()); | |
| 49 if (!timer_->IsRunning()) | 43 if (!timer_->IsRunning()) |
| 50 StartTimer(); | 44 StartTimer(); |
| 51 } | 45 } |
| 52 | 46 |
| 53 private: | 47 private: |
| 54 void StartTimer() { | 48 void StartTimer() { |
| 55 timer_->Start(FROM_HERE, context_->policy().garbage_collection_interval, | 49 timer_->Start(FROM_HERE, context_->policy().garbage_collection_interval, |
| 56 base::Bind(&ReportingGarbageCollectorImpl::CollectGarbage, | 50 base::Bind(&ReportingGarbageCollectorImpl::CollectGarbage, |
| 57 base::Unretained(this))); | 51 base::Unretained(this))); |
| 58 } | 52 } |
| (...skipping 27 matching lines...) Expand all Loading... |
| 86 | 80 |
| 87 // static | 81 // static |
| 88 std::unique_ptr<ReportingGarbageCollector> ReportingGarbageCollector::Create( | 82 std::unique_ptr<ReportingGarbageCollector> ReportingGarbageCollector::Create( |
| 89 ReportingContext* context) { | 83 ReportingContext* context) { |
| 90 return base::MakeUnique<ReportingGarbageCollectorImpl>(context); | 84 return base::MakeUnique<ReportingGarbageCollectorImpl>(context); |
| 91 } | 85 } |
| 92 | 86 |
| 93 ReportingGarbageCollector::~ReportingGarbageCollector() {} | 87 ReportingGarbageCollector::~ReportingGarbageCollector() {} |
| 94 | 88 |
| 95 } // namespace net | 89 } // namespace net |
| OLD | NEW |