| Index: net/reporting/reporting_garbage_collector.cc
|
| diff --git a/net/reporting/reporting_garbage_collector.cc b/net/reporting/reporting_garbage_collector.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..53043f7694cc30b1edd924773e7a6a83363fb74d
|
| --- /dev/null
|
| +++ b/net/reporting/reporting_garbage_collector.cc
|
| @@ -0,0 +1,80 @@
|
| +// Copyright 2017 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "net/reporting/reporting_garbage_collector.h"
|
| +
|
| +#include <vector>
|
| +
|
| +#include "base/memory/ptr_util.h"
|
| +#include "base/time/tick_clock.h"
|
| +#include "base/time/time.h"
|
| +#include "base/timer/timer.h"
|
| +#include "net/reporting/reporting_cache.h"
|
| +#include "net/reporting/reporting_context.h"
|
| +#include "net/reporting/reporting_observer.h"
|
| +#include "net/reporting/reporting_policy.h"
|
| +#include "net/reporting/reporting_report.h"
|
| +
|
| +namespace net {
|
| +
|
| +namespace {
|
| +
|
| +class ReportingGarbageCollectorImpl : public ReportingGarbageCollector,
|
| + public ReportingObserver {
|
| + public:
|
| + ReportingGarbageCollectorImpl(ReportingContext* context) : context_(context) {
|
| + context_->AddObserver(this);
|
| + }
|
| +
|
| + ~ReportingGarbageCollectorImpl() override { context_->RemoveObserver(this); }
|
| +
|
| + // ReportingObserver implementation:
|
| + void OnCacheUpdated() override {
|
| + if (!context_->garbage_collection_timer()->IsRunning())
|
| + StartTimer();
|
| + }
|
| +
|
| + private:
|
| + void StartTimer() {
|
| + context_->garbage_collection_timer()->Start(
|
| + FROM_HERE, context_->policy().garbage_collection_interval,
|
| + base::Bind(&ReportingGarbageCollectorImpl::CollectGarbage,
|
| + base::Unretained(this)));
|
| + }
|
| +
|
| + void CollectGarbage() {
|
| + base::TimeTicks now = context_->tick_clock()->NowTicks();
|
| + const ReportingPolicy& policy = context_->policy();
|
| +
|
| + std::vector<const ReportingReport*> all_reports;
|
| + context_->cache()->GetReports(&all_reports);
|
| +
|
| + std::vector<const ReportingReport*> reports_to_remove;
|
| + for (const ReportingReport* report : all_reports) {
|
| + if (now - report->queued >= policy.max_report_age ||
|
| + report->attempts >= policy.max_report_attempts) {
|
| + reports_to_remove.push_back(report);
|
| + }
|
| + }
|
| +
|
| + // Don't restart the timer on the garbage collector's own updates.
|
| + context_->RemoveObserver(this);
|
| + context_->cache()->RemoveReports(reports_to_remove);
|
| + context_->AddObserver(this);
|
| + }
|
| +
|
| + ReportingContext* context_;
|
| +};
|
| +
|
| +} // namespace
|
| +
|
| +// static
|
| +std::unique_ptr<ReportingGarbageCollector> ReportingGarbageCollector::Create(
|
| + ReportingContext* context) {
|
| + return base::MakeUnique<ReportingGarbageCollectorImpl>(context);
|
| +}
|
| +
|
| +ReportingGarbageCollector::~ReportingGarbageCollector() {}
|
| +
|
| +} // namespace net
|
|
|