Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(128)

Side by Side Diff: net/reporting/reporting_garbage_collector.cc

Issue 2740833004: Reporting: Implement garbage collector. (Closed)
Patch Set: rebase Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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)
27 : context_(context), timer_(base::MakeUnique<base::OneShotTimer>()) {
28 context_->AddObserver(this);
29 }
30
31 // ReportingGarbageCollector implementation:
32 //
33 ~ReportingGarbageCollectorImpl() override { context_->RemoveObserver(this); }
34
35 void SetTimerForTesting(std::unique_ptr<base::Timer> timer) override {
36 DCHECK(!timer_->IsRunning());
37 timer_ = std::move(timer);
38 }
39
40 // ReportingObserver implementation:
41 void OnCacheUpdated() override {
42 if (!timer_->IsRunning())
43 StartTimer();
44 }
45
46 private:
47 void StartTimer() {
48 timer_->Start(FROM_HERE, context_->policy().garbage_collection_interval,
49 base::Bind(&ReportingGarbageCollectorImpl::CollectGarbage,
50 base::Unretained(this)));
51 }
52
53 void CollectGarbage() {
54 base::TimeTicks now = context_->tick_clock()->NowTicks();
55 const ReportingPolicy& policy = context_->policy();
56
57 std::vector<const ReportingReport*> all_reports;
58 context_->cache()->GetReports(&all_reports);
59
60 std::vector<const ReportingReport*> reports_to_remove;
61 for (const ReportingReport* report : all_reports) {
62 if (now - report->queued >= policy.max_report_age ||
63 report->attempts >= policy.max_report_attempts) {
64 reports_to_remove.push_back(report);
65 }
66 }
67
68 // Don't restart the timer on the garbage collector's own updates.
69 context_->RemoveObserver(this);
70 context_->cache()->RemoveReports(reports_to_remove);
71 context_->AddObserver(this);
72 }
73
74 ReportingContext* context_;
75 std::unique_ptr<base::Timer> timer_;
76 };
77
78 } // namespace
79
80 // static
81 std::unique_ptr<ReportingGarbageCollector> ReportingGarbageCollector::Create(
82 ReportingContext* context) {
83 return base::MakeUnique<ReportingGarbageCollectorImpl>(context);
84 }
85
86 ReportingGarbageCollector::~ReportingGarbageCollector() {}
87
88 } // namespace net
OLDNEW
« no previous file with comments | « net/reporting/reporting_garbage_collector.h ('k') | net/reporting/reporting_garbage_collector_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698