OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "net/reporting/reporting_garbage_collector.h" |
| 6 |
| 7 #include <vector> |
| 8 |
| 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/time/tick_clock.h" |
| 11 #include "base/time/time.h" |
| 12 #include "base/timer/timer.h" |
| 13 #include "net/reporting/reporting_cache.h" |
| 14 #include "net/reporting/reporting_context.h" |
| 15 #include "net/reporting/reporting_observer.h" |
| 16 #include "net/reporting/reporting_policy.h" |
| 17 #include "net/reporting/reporting_report.h" |
| 18 |
| 19 namespace net { |
| 20 |
| 21 namespace { |
| 22 |
| 23 class ReportingGarbageCollectorImpl : public ReportingGarbageCollector, |
| 24 public ReportingObserver { |
| 25 public: |
| 26 ReportingGarbageCollectorImpl(ReportingContext* context) : context_(context) { |
| 27 context_->AddObserver(this); |
| 28 } |
| 29 |
| 30 ~ReportingGarbageCollectorImpl() override { context_->RemoveObserver(this); } |
| 31 |
| 32 // ReportingObserver implementation: |
| 33 void OnCacheUpdated() override { |
| 34 if (!context_->garbage_collection_timer()->IsRunning()) |
| 35 StartTimer(); |
| 36 } |
| 37 |
| 38 private: |
| 39 void StartTimer() { |
| 40 context_->garbage_collection_timer()->Start( |
| 41 FROM_HERE, context_->policy().garbage_collection_interval, |
| 42 base::Bind(&ReportingGarbageCollectorImpl::CollectGarbage, |
| 43 base::Unretained(this))); |
| 44 } |
| 45 |
| 46 void CollectGarbage() { |
| 47 base::TimeTicks now = context_->tick_clock()->NowTicks(); |
| 48 const ReportingPolicy& policy = context_->policy(); |
| 49 |
| 50 std::vector<const ReportingReport*> all_reports; |
| 51 context_->cache()->GetReports(&all_reports); |
| 52 |
| 53 std::vector<const ReportingReport*> reports_to_remove; |
| 54 for (const ReportingReport* report : all_reports) { |
| 55 if (now - report->queued >= policy.max_report_age || |
| 56 report->attempts >= policy.max_report_attempts) { |
| 57 reports_to_remove.push_back(report); |
| 58 } |
| 59 } |
| 60 |
| 61 // Don't restart the timer on the garbage collector's own updates. |
| 62 context_->RemoveObserver(this); |
| 63 context_->cache()->RemoveReports(reports_to_remove); |
| 64 context_->AddObserver(this); |
| 65 } |
| 66 |
| 67 ReportingContext* context_; |
| 68 }; |
| 69 |
| 70 } // namespace |
| 71 |
| 72 // static |
| 73 std::unique_ptr<ReportingGarbageCollector> ReportingGarbageCollector::Create( |
| 74 ReportingContext* context) { |
| 75 return base::MakeUnique<ReportingGarbageCollectorImpl>(context); |
| 76 } |
| 77 |
| 78 ReportingGarbageCollector::~ReportingGarbageCollector() {} |
| 79 |
| 80 } // namespace net |
OLD | NEW |