Commit 731a95c9 authored by Lily Chen's avatar Lily Chen Committed by Commit Bot

Use a PersistentReportingStore when constructing ReportingService

This CL adds a PersistentReportingStore* to the constructor of
ReportingService. This pointer is plumbed down to the ReportingCache,
which will eventually use the store to persist its data to disk.
The store must outlive the ReportingService. If null, the
ReportingCache will be in-memory only. The ReportingService is
instantiated with a nullptr placeholder as the store for now.

Bug: 895821
Change-Id: I3a3234653707d2dd87f475413c6318ee7b43121a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1575079
Commit-Queue: Lily Chen <chlily@chromium.org>
Reviewed-by: default avatarEric Orth <ericorth@chromium.org>
Cr-Commit-Position: refs/heads/master@{#656973}
parent 86b6dd76
......@@ -11,8 +11,9 @@ namespace net {
// static
std::unique_ptr<ReportingCache> ReportingCache::Create(
ReportingContext* context) {
return std::make_unique<ReportingCacheImpl>(context);
ReportingContext* context,
PersistentReportingStore* store) {
return std::make_unique<ReportingCacheImpl>(context, store);
}
ReportingCache::~ReportingCache() = default;
......
......@@ -15,7 +15,6 @@
#include "base/values.h"
#include "net/base/net_export.h"
#include "net/reporting/reporting_client.h"
#include "net/reporting/reporting_context.h"
#include "net/reporting/reporting_header_parser.h"
#include "net/reporting/reporting_report.h"
#include "url/gurl.h"
......@@ -23,6 +22,8 @@
namespace net {
class ReportingContext;
// The cache holds undelivered reports and clients (per-origin endpoint
// configurations) in memory. (It is not responsible for persisting them.)
//
......@@ -47,7 +48,10 @@ class NET_EXPORT ReportingCache {
public:
class PersistentReportingStore;
static std::unique_ptr<ReportingCache> Create(ReportingContext* context);
// |store| should outlive the ReportingCache.
static std::unique_ptr<ReportingCache> Create(
ReportingContext* context,
PersistentReportingStore* store);
virtual ~ReportingCache();
......
......@@ -40,8 +40,9 @@ std::string GetSuperdomain(const std::string& domain) {
} // namespace
ReportingCacheImpl::ReportingCacheImpl(ReportingContext* context)
: context_(context) {
ReportingCacheImpl::ReportingCacheImpl(ReportingContext* context,
PersistentReportingStore* store)
: context_(context), store_(store) {
DCHECK(context_);
}
......@@ -556,6 +557,14 @@ ReportingCacheImpl::OriginClient::OriginClient(OriginClient&& other) = default;
ReportingCacheImpl::OriginClient::~OriginClient() = default;
bool ReportingCacheImpl::IsReportDataPersisted() const {
return store_ && context_->policy().persist_reports_across_restarts;
}
bool ReportingCacheImpl::IsClientDataPersisted() const {
return store_ && context_->policy().persist_clients_across_restarts;
}
void ReportingCacheImpl::RemoveReportInternal(const ReportingReport* report) {
reports_[report]->RecordOutcome(tick_clock()->NowTicks());
size_t erased = reports_.erase(report);
......
......@@ -31,7 +31,8 @@ namespace net {
class ReportingCacheImpl : public ReportingCache {
public:
explicit ReportingCacheImpl(ReportingContext* context);
ReportingCacheImpl(ReportingContext* context,
PersistentReportingStore* store);
~ReportingCacheImpl() override;
......@@ -127,6 +128,11 @@ class ReportingCacheImpl : public ReportingCache {
std::map<ReportingEndpointGroupKey, CachedReportingEndpointGroup>;
using EndpointMap = std::multimap<ReportingEndpointGroupKey, ReportingClient>;
// Returns whether the cached data is persisted across restarts in the
// PersistentReportingStore.
bool IsReportDataPersisted() const;
bool IsClientDataPersisted() const;
void RemoveReportInternal(const ReportingReport* report);
const ReportingReport* FindReportToEvict() const;
......@@ -285,6 +291,11 @@ class ReportingCacheImpl : public ReportingCache {
ReportingContext* context_;
// Stores cached data persistently, if not null. If |store_| is null, then the
// ReportingCache will store data in memory only.
// TODO(chlily): Implement.
PersistentReportingStore* const store_;
// Owns all reports, keyed by const raw pointer for easier lookup.
std::unordered_map<const ReportingReport*, std::unique_ptr<ReportingReport>>
reports_;
......
......@@ -16,7 +16,6 @@
#include "base/time/time.h"
#include "net/base/backoff_entry.h"
#include "net/base/rand_callback.h"
#include "net/reporting/reporting_cache.h"
#include "net/reporting/reporting_cache_observer.h"
#include "net/reporting/reporting_delegate.h"
#include "net/reporting/reporting_delivery_agent.h"
......@@ -35,13 +34,15 @@ namespace {
class ReportingContextImpl : public ReportingContext {
public:
ReportingContextImpl(const ReportingPolicy& policy,
URLRequestContext* request_context)
URLRequestContext* request_context,
ReportingCache::PersistentReportingStore* store)
: ReportingContext(policy,
base::DefaultClock::GetInstance(),
base::DefaultTickClock::GetInstance(),
base::BindRepeating(&base::RandInt),
ReportingUploader::Create(request_context),
ReportingDelegate::Create(request_context)) {}
ReportingDelegate::Create(request_context),
store) {}
};
} // namespace
......@@ -49,8 +50,9 @@ class ReportingContextImpl : public ReportingContext {
// static
std::unique_ptr<ReportingContext> ReportingContext::Create(
const ReportingPolicy& policy,
URLRequestContext* request_context) {
return std::make_unique<ReportingContextImpl>(policy, request_context);
URLRequestContext* request_context,
ReportingCache::PersistentReportingStore* store) {
return std::make_unique<ReportingContextImpl>(policy, request_context, store);
}
ReportingContext::~ReportingContext() = default;
......@@ -79,18 +81,20 @@ void ReportingContext::OnShutdown() {
uploader_->OnShutdown();
}
ReportingContext::ReportingContext(const ReportingPolicy& policy,
base::Clock* clock,
const base::TickClock* tick_clock,
const RandIntCallback& rand_callback,
std::unique_ptr<ReportingUploader> uploader,
std::unique_ptr<ReportingDelegate> delegate)
ReportingContext::ReportingContext(
const ReportingPolicy& policy,
base::Clock* clock,
const base::TickClock* tick_clock,
const RandIntCallback& rand_callback,
std::unique_ptr<ReportingUploader> uploader,
std::unique_ptr<ReportingDelegate> delegate,
ReportingCache::PersistentReportingStore* store)
: policy_(policy),
clock_(clock),
tick_clock_(tick_clock),
uploader_(std::move(uploader)),
delegate_(std::move(delegate)),
cache_(ReportingCache::Create(this)),
cache_(ReportingCache::Create(this, store)),
endpoint_manager_(ReportingEndpointManager::Create(this, rand_callback)),
delivery_agent_(ReportingDeliveryAgent::Create(this)),
garbage_collector_(ReportingGarbageCollector::Create(this)),
......
......@@ -12,6 +12,7 @@
#include "net/base/backoff_entry.h"
#include "net/base/net_export.h"
#include "net/base/rand_callback.h"
#include "net/reporting/reporting_cache.h"
#include "net/reporting/reporting_policy.h"
namespace base {
......@@ -21,7 +22,6 @@ class TickClock;
namespace net {
class ReportingCache;
class ReportingCacheObserver;
class ReportingDelegate;
class ReportingDeliveryAgent;
......@@ -35,9 +35,11 @@ class URLRequestContext;
// Wrapped by ReportingService, which provides the external interface.
class NET_EXPORT ReportingContext {
public:
// |request_context| and |store| should outlive the ReportingContext.
static std::unique_ptr<ReportingContext> Create(
const ReportingPolicy& policy,
URLRequestContext* request_context);
URLRequestContext* request_context,
ReportingCache::PersistentReportingStore* store);
~ReportingContext();
......@@ -71,7 +73,8 @@ class NET_EXPORT ReportingContext {
const base::TickClock* tick_clock,
const RandIntCallback& rand_callback,
std::unique_ptr<ReportingUploader> uploader,
std::unique_ptr<ReportingDelegate> delegate);
std::unique_ptr<ReportingDelegate> delegate,
ReportingCache::PersistentReportingStore* store);
private:
ReportingPolicy policy_;
......
......@@ -132,9 +132,10 @@ ReportingService::~ReportingService() = default;
// static
std::unique_ptr<ReportingService> ReportingService::Create(
const ReportingPolicy& policy,
URLRequestContext* request_context) {
URLRequestContext* request_context,
ReportingCache::PersistentReportingStore* store) {
return std::make_unique<ReportingServiceImpl>(
ReportingContext::Create(policy, request_context));
ReportingContext::Create(policy, request_context, store));
}
// static
......
......@@ -11,6 +11,7 @@
#include "base/callback.h"
#include "base/macros.h"
#include "net/base/net_export.h"
#include "net/reporting/reporting_cache.h"
class GURL;
......@@ -31,10 +32,12 @@ class NET_EXPORT ReportingService {
virtual ~ReportingService();
// Creates a ReportingService. |policy| will be copied. |request_context| must
// outlive the ReportingService.
// outlive the ReportingService. |store| must outlive the ReportingService.
// If |store| is null, the ReportingCache will be in-memory only.
static std::unique_ptr<ReportingService> Create(
const ReportingPolicy& policy,
URLRequestContext* request_context);
URLRequestContext* request_context,
ReportingCache::PersistentReportingStore* store);
// Creates a ReportingService for testing purposes using an
// already-constructed ReportingContext. The ReportingService will take
......
......@@ -152,9 +152,11 @@ bool TestReportingDelegate::CanUseClient(const url::Origin& origin,
return true;
}
TestReportingContext::TestReportingContext(base::Clock* clock,
const base::TickClock* tick_clock,
const ReportingPolicy& policy)
TestReportingContext::TestReportingContext(
base::Clock* clock,
const base::TickClock* tick_clock,
const ReportingPolicy& policy,
ReportingCache::PersistentReportingStore* store)
: ReportingContext(
policy,
clock,
......@@ -162,7 +164,8 @@ TestReportingContext::TestReportingContext(base::Clock* clock,
base::BindRepeating(&TestReportingContext::RandIntCallback,
base::Unretained(this)),
std::make_unique<TestReportingUploader>(),
std::make_unique<TestReportingDelegate>()),
std::make_unique<TestReportingDelegate>(),
store),
rand_counter_(0),
delivery_timer_(new base::MockOneShotTimer()),
garbage_collection_timer_(new base::MockOneShotTimer()) {
......@@ -181,7 +184,7 @@ int TestReportingContext::RandIntCallback(int min, int max) {
return min + (rand_counter_++ % (max - min + 1));
}
ReportingTestBase::ReportingTestBase() {
ReportingTestBase::ReportingTestBase() : store_(nullptr) {
// For tests, disable jitter.
ReportingPolicy policy;
policy.endpoint_backoff_policy.jitter_factor = 0.0;
......@@ -195,6 +198,12 @@ void ReportingTestBase::UsePolicy(const ReportingPolicy& new_policy) {
CreateContext(new_policy, clock()->Now(), tick_clock()->NowTicks());
}
void ReportingTestBase::UseStore(
ReportingCache::PersistentReportingStore* store) {
store_ = store;
CreateContext(policy(), clock()->Now(), tick_clock()->NowTicks());
}
const ReportingClient ReportingTestBase::FindEndpointInCache(
const url::Origin& origin,
const std::string& group_name,
......@@ -265,8 +274,8 @@ void ReportingTestBase::SimulateRestart(base::TimeDelta delta,
void ReportingTestBase::CreateContext(const ReportingPolicy& policy,
base::Time now,
base::TimeTicks now_ticks) {
context_ =
std::make_unique<TestReportingContext>(&clock_, &tick_clock_, policy);
context_ = std::make_unique<TestReportingContext>(&clock_, &tick_clock_,
policy, store_);
clock()->SetNow(now);
tick_clock()->SetNowTicks(now_ticks);
}
......
......@@ -143,9 +143,11 @@ class TestReportingDelegate : public ReportingDelegate {
// Clock, TickClock, Timer, and ReportingUploader.
class TestReportingContext : public ReportingContext {
public:
TestReportingContext(base::Clock* clock,
const base::TickClock* tick_clock,
const ReportingPolicy& policy);
TestReportingContext(
base::Clock* clock,
const base::TickClock* tick_clock,
const ReportingPolicy& policy,
ReportingCache::PersistentReportingStore* store = nullptr);
~TestReportingContext();
base::MockOneShotTimer* test_delivery_timer() { return delivery_timer_; }
......@@ -181,6 +183,7 @@ class ReportingTestBase : public TestWithScopedTaskEnvironment {
~ReportingTestBase() override;
void UsePolicy(const ReportingPolicy& policy);
void UseStore(ReportingCache::PersistentReportingStore* store);
// Finds a particular endpoint (by origin, group, url) in the cache and
// returns it (or ReportingClient with invalid url, if not found).
......@@ -256,6 +259,7 @@ class ReportingTestBase : public TestWithScopedTaskEnvironment {
ReportingGarbageCollector* garbage_collector() {
return context_->garbage_collector();
}
ReportingCache::PersistentReportingStore* store() { return store_; }
base::TimeTicks yesterday();
base::TimeTicks now();
......@@ -274,6 +278,7 @@ class ReportingTestBase : public TestWithScopedTaskEnvironment {
base::SimpleTestClock clock_;
base::SimpleTestTickClock tick_clock_;
std::unique_ptr<TestReportingContext> context_;
ReportingCache::PersistentReportingStore* store_;
DISALLOW_COPY_AND_ASSIGN(ReportingTestBase);
};
......
......@@ -59,6 +59,7 @@
#if BUILDFLAG(ENABLE_REPORTING)
#include "net/network_error_logging/network_error_logging_service.h"
#include "net/network_error_logging/persistent_reporting_and_nel_store.h"
#include "net/reporting/reporting_policy.h"
#include "net/reporting/reporting_service.h"
#endif // BUILDFLAG(ENABLE_REPORTING)
......@@ -558,15 +559,18 @@ std::unique_ptr<URLRequestContext> URLRequestContextBuilder::Build() {
// Note: ReportingService::Create and NetworkErrorLoggingService::Create can
// both return nullptr if the corresponding base::Feature is disabled.
// TODO(chlily): Use a real one to enable persistent storage. (This is just a
// placeholder for now.)
PersistentReportingAndNELStore* store = nullptr;
if (reporting_policy_) {
storage->set_reporting_service(
ReportingService::Create(*reporting_policy_, context.get()));
ReportingService::Create(*reporting_policy_, context.get(), store));
}
if (network_error_logging_enabled_) {
// TODO(chlily): Create this with an actual PersistentNELStore*.
storage->set_network_error_logging_service(
NetworkErrorLoggingService::Create(nullptr /* store */));
NetworkErrorLoggingService::Create(store));
}
// If both Reporting and Network Error Logging are actually enabled, then
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment