Commit 42c57319 authored by juliatuttle's avatar juliatuttle Committed by Commit bot

Reporting: Remove persistence for now.

I'm going to build a general persistence mechanism for //net instead of
tediously plumbing out to the profile's prefs (which gets really messy
with isolated app contexts and such), so I'm removing the old
persistence stuff until that's done.

BUG=704259

Review-Url: https://codereview.chromium.org/2835923005
Cr-Commit-Position: refs/heads/master@{#467862}
parent c045c21a
...@@ -1424,8 +1424,6 @@ component("net") { ...@@ -1424,8 +1424,6 @@ component("net") {
"reporting/reporting_client.h", "reporting/reporting_client.h",
"reporting/reporting_context.cc", "reporting/reporting_context.cc",
"reporting/reporting_context.h", "reporting/reporting_context.h",
"reporting/reporting_delegate.cc",
"reporting/reporting_delegate.h",
"reporting/reporting_delivery_agent.cc", "reporting/reporting_delivery_agent.cc",
"reporting/reporting_delivery_agent.h", "reporting/reporting_delivery_agent.h",
"reporting/reporting_endpoint_manager.cc", "reporting/reporting_endpoint_manager.cc",
......
...@@ -15,7 +15,6 @@ ...@@ -15,7 +15,6 @@
#include "base/time/time.h" #include "base/time/time.h"
#include "net/base/backoff_entry.h" #include "net/base/backoff_entry.h"
#include "net/reporting/reporting_cache.h" #include "net/reporting/reporting_cache.h"
#include "net/reporting/reporting_delegate.h"
#include "net/reporting/reporting_delivery_agent.h" #include "net/reporting/reporting_delivery_agent.h"
#include "net/reporting/reporting_endpoint_manager.h" #include "net/reporting/reporting_endpoint_manager.h"
#include "net/reporting/reporting_garbage_collector.h" #include "net/reporting/reporting_garbage_collector.h"
...@@ -34,10 +33,8 @@ namespace { ...@@ -34,10 +33,8 @@ namespace {
class ReportingContextImpl : public ReportingContext { class ReportingContextImpl : public ReportingContext {
public: public:
ReportingContextImpl(const ReportingPolicy& policy, ReportingContextImpl(const ReportingPolicy& policy,
std::unique_ptr<ReportingDelegate> delegate,
URLRequestContext* request_context) URLRequestContext* request_context)
: ReportingContext(policy, : ReportingContext(policy,
std::move(delegate),
base::MakeUnique<base::DefaultClock>(), base::MakeUnique<base::DefaultClock>(),
base::MakeUnique<base::DefaultTickClock>(), base::MakeUnique<base::DefaultTickClock>(),
ReportingUploader::Create(request_context)) {} ReportingUploader::Create(request_context)) {}
...@@ -48,29 +45,12 @@ class ReportingContextImpl : public ReportingContext { ...@@ -48,29 +45,12 @@ class ReportingContextImpl : public ReportingContext {
// static // static
std::unique_ptr<ReportingContext> ReportingContext::Create( std::unique_ptr<ReportingContext> ReportingContext::Create(
const ReportingPolicy& policy, const ReportingPolicy& policy,
std::unique_ptr<ReportingDelegate> delegate,
URLRequestContext* request_context) { URLRequestContext* request_context) {
return base::MakeUnique<ReportingContextImpl>(policy, std::move(delegate), return base::MakeUnique<ReportingContextImpl>(policy, request_context);
request_context);
} }
ReportingContext::~ReportingContext() {} ReportingContext::~ReportingContext() {}
void ReportingContext::Initialize() {
DCHECK(!initialized_);
// This order isn't *critical*, but things will work better with it in this
// order: with the DeliveryAgent after the Persister, it can schedule delivery
// of persisted reports instead of waiting for a new one to be generated, and
// with the GarbageCollector in between, it won't bother scheduling delivery
// of reports that should be discarded instead.
persister_->Initialize();
garbage_collector_->Initialize();
delivery_agent_->Initialize();
initialized_ = true;
}
void ReportingContext::AddObserver(ReportingObserver* observer) { void ReportingContext::AddObserver(ReportingObserver* observer) {
DCHECK(!observers_.HasObserver(observer)); DCHECK(!observers_.HasObserver(observer));
observers_.AddObserver(observer); observers_.AddObserver(observer);
...@@ -82,24 +62,18 @@ void ReportingContext::RemoveObserver(ReportingObserver* observer) { ...@@ -82,24 +62,18 @@ void ReportingContext::RemoveObserver(ReportingObserver* observer) {
} }
void ReportingContext::NotifyCacheUpdated() { void ReportingContext::NotifyCacheUpdated() {
if (!initialized_)
return;
for (auto& observer : observers_) for (auto& observer : observers_)
observer.OnCacheUpdated(); observer.OnCacheUpdated();
} }
ReportingContext::ReportingContext(const ReportingPolicy& policy, ReportingContext::ReportingContext(const ReportingPolicy& policy,
std::unique_ptr<ReportingDelegate> delegate,
std::unique_ptr<base::Clock> clock, std::unique_ptr<base::Clock> clock,
std::unique_ptr<base::TickClock> tick_clock, std::unique_ptr<base::TickClock> tick_clock,
std::unique_ptr<ReportingUploader> uploader) std::unique_ptr<ReportingUploader> uploader)
: policy_(policy), : policy_(policy),
delegate_(std::move(delegate)),
clock_(std::move(clock)), clock_(std::move(clock)),
tick_clock_(std::move(tick_clock)), tick_clock_(std::move(tick_clock)),
uploader_(std::move(uploader)), uploader_(std::move(uploader)),
initialized_(false),
cache_(base::MakeUnique<ReportingCache>(this)), cache_(base::MakeUnique<ReportingCache>(this)),
endpoint_manager_(base::MakeUnique<ReportingEndpointManager>(this)), endpoint_manager_(base::MakeUnique<ReportingEndpointManager>(this)),
delivery_agent_(ReportingDeliveryAgent::Create(this)), delivery_agent_(ReportingDeliveryAgent::Create(this)),
......
...@@ -21,7 +21,6 @@ class TickClock; ...@@ -21,7 +21,6 @@ class TickClock;
namespace net { namespace net {
class ReportingCache; class ReportingCache;
class ReportingDelegate;
class ReportingDeliveryAgent; class ReportingDeliveryAgent;
class ReportingEndpointManager; class ReportingEndpointManager;
class ReportingGarbageCollector; class ReportingGarbageCollector;
...@@ -37,23 +36,11 @@ class NET_EXPORT ReportingContext { ...@@ -37,23 +36,11 @@ class NET_EXPORT ReportingContext {
public: public:
static std::unique_ptr<ReportingContext> Create( static std::unique_ptr<ReportingContext> Create(
const ReportingPolicy& policy, const ReportingPolicy& policy,
std::unique_ptr<ReportingDelegate> delegate,
URLRequestContext* request_context); URLRequestContext* request_context);
~ReportingContext(); ~ReportingContext();
// Initializes the ReportingContext. This may take a while (e.g. it may
// involve reloading state persisted to disk). Should be called only once.
//
// Components of the ReportingContext won't reference their dependencies (e.g.
// the Clock/TickClock or Timers inside the individual components) until
// during/after the call to Init.
void Initialize();
bool initialized() const { return initialized_; }
const ReportingPolicy& policy() { return policy_; } const ReportingPolicy& policy() { return policy_; }
ReportingDelegate* delegate() { return delegate_.get(); }
base::Clock* clock() { return clock_.get(); } base::Clock* clock() { return clock_.get(); }
base::TickClock* tick_clock() { return tick_clock_.get(); } base::TickClock* tick_clock() { return tick_clock_.get(); }
...@@ -77,21 +64,18 @@ class NET_EXPORT ReportingContext { ...@@ -77,21 +64,18 @@ class NET_EXPORT ReportingContext {
protected: protected:
ReportingContext(const ReportingPolicy& policy, ReportingContext(const ReportingPolicy& policy,
std::unique_ptr<ReportingDelegate> delegate,
std::unique_ptr<base::Clock> clock, std::unique_ptr<base::Clock> clock,
std::unique_ptr<base::TickClock> tick_clock, std::unique_ptr<base::TickClock> tick_clock,
std::unique_ptr<ReportingUploader> uploader); std::unique_ptr<ReportingUploader> uploader);
private: private:
ReportingPolicy policy_; ReportingPolicy policy_;
std::unique_ptr<ReportingDelegate> delegate_;
std::unique_ptr<base::Clock> clock_; std::unique_ptr<base::Clock> clock_;
std::unique_ptr<base::TickClock> tick_clock_; std::unique_ptr<base::TickClock> tick_clock_;
std::unique_ptr<ReportingUploader> uploader_; std::unique_ptr<ReportingUploader> uploader_;
base::ObserverList<ReportingObserver, /* check_empty= */ true> observers_; base::ObserverList<ReportingObserver, /* check_empty= */ true> observers_;
bool initialized_;
std::unique_ptr<ReportingCache> cache_; std::unique_ptr<ReportingCache> cache_;
...@@ -102,8 +86,7 @@ class NET_EXPORT ReportingContext { ...@@ -102,8 +86,7 @@ class NET_EXPORT ReportingContext {
// and |endpoint_manager_|. // and |endpoint_manager_|.
std::unique_ptr<ReportingDeliveryAgent> delivery_agent_; std::unique_ptr<ReportingDeliveryAgent> delivery_agent_;
// |persister_| must come after |delegate_|, |clock_|, |tick_clock_|, and // |persister_| must come after |clock_|, |tick_clock_|, and |cache_|.
// |cache_|.
std::unique_ptr<ReportingPersister> persister_; std::unique_ptr<ReportingPersister> persister_;
// |garbage_collector_| must come after |tick_clock_| and |cache_|. // |garbage_collector_| must come after |tick_clock_| and |cache_|.
......
// 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_delegate.h"
namespace net {
ReportingDelegate::~ReportingDelegate() {}
ReportingDelegate::ReportingDelegate() {}
} // namespace net
// 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.
#ifndef NET_REPORTING_REPORTING_DELEGATE_H_
#define NET_REPORTING_REPORTING_DELEGATE_H_
#include <memory>
#include "base/macros.h"
#include "net/base/net_export.h"
namespace base {
class Value;
} // namespace base
namespace net {
// Delegate for things that the Reporting system can't do by itself, like
// persisting data across embedder restarts.
class NET_EXPORT ReportingDelegate {
public:
virtual ~ReportingDelegate();
// Gets previously persisted data, if any is available. Returns a null pointer
// if no data is available. Can be called any number of times.
virtual std::unique_ptr<const base::Value> GetPersistedData() = 0;
// Sets data to be persisted across embedder restarts. Ideally, this data will
// be returned by any future calls to GetPersistedData() in this or future
// sessions (until newer data is persisted), but no guarantee is made, since
// the underlying persistence mechanism may or may not be reliable.
virtual void PersistData(
std::unique_ptr<const base::Value> persisted_data) = 0;
protected:
ReportingDelegate();
private:
DISALLOW_COPY_AND_ASSIGN(ReportingDelegate);
};
} // namespace net
#endif // NET_REPORTING_REPORTING_DELEGATE_H_
...@@ -62,11 +62,6 @@ class ReportingDeliveryAgentImpl : public ReportingDeliveryAgent, ...@@ -62,11 +62,6 @@ class ReportingDeliveryAgentImpl : public ReportingDeliveryAgent,
~ReportingDeliveryAgentImpl() override { context_->RemoveObserver(this); } ~ReportingDeliveryAgentImpl() override { context_->RemoveObserver(this); }
void Initialize() override {
if (CacheHasReports())
StartTimer();
}
void SetTimerForTesting(std::unique_ptr<base::Timer> timer) override { void SetTimerForTesting(std::unique_ptr<base::Timer> timer) override {
DCHECK(!timer_->IsRunning()); DCHECK(!timer_->IsRunning());
timer_ = std::move(timer); timer_ = std::move(timer);
......
...@@ -55,11 +55,6 @@ class NET_EXPORT ReportingDeliveryAgent { ...@@ -55,11 +55,6 @@ class NET_EXPORT ReportingDeliveryAgent {
virtual ~ReportingDeliveryAgent(); virtual ~ReportingDeliveryAgent();
// Initializes the DeliveryAgent, which schedules delivery (after the Policy's
// delivery_interval) for any previously-persisted reports that can still be
// delivered.
virtual void Initialize() = 0;
// Replaces the internal Timer used for scheduling report delivery attempts // Replaces the internal Timer used for scheduling report delivery attempts
// with a caller-specified one so that unittests can provide a MockTimer. // with a caller-specified one so that unittests can provide a MockTimer.
virtual void SetTimerForTesting(std::unique_ptr<base::Timer> timer) = 0; virtual void SetTimerForTesting(std::unique_ptr<base::Timer> timer) = 0;
......
...@@ -24,28 +24,22 @@ class ReportingGarbageCollectorImpl : public ReportingGarbageCollector, ...@@ -24,28 +24,22 @@ class ReportingGarbageCollectorImpl : public ReportingGarbageCollector,
public ReportingObserver { public ReportingObserver {
public: public:
ReportingGarbageCollectorImpl(ReportingContext* context) ReportingGarbageCollectorImpl(ReportingContext* context)
: context_(context), timer_(base::MakeUnique<base::OneShotTimer>()) {} : context_(context), timer_(base::MakeUnique<base::OneShotTimer>()) {
context_->AddObserver(this);
}
// ReportingGarbageCollector implementation: // ReportingGarbageCollector implementation:
~ReportingGarbageCollectorImpl() override { ~ReportingGarbageCollectorImpl() override {
DCHECK(context_->initialized());
context_->RemoveObserver(this); context_->RemoveObserver(this);
} }
void Initialize() override {
context_->AddObserver(this);
CollectGarbage();
}
void SetTimerForTesting(std::unique_ptr<base::Timer> timer) override { void SetTimerForTesting(std::unique_ptr<base::Timer> timer) override {
DCHECK(!context_->initialized());
timer_ = std::move(timer); timer_ = std::move(timer);
} }
// ReportingObserver implementation: // ReportingObserver implementation:
void OnCacheUpdated() override { void OnCacheUpdated() override {
DCHECK(context_->initialized());
if (!timer_->IsRunning()) if (!timer_->IsRunning())
StartTimer(); StartTimer();
} }
......
...@@ -28,10 +28,6 @@ class NET_EXPORT ReportingGarbageCollector { ...@@ -28,10 +28,6 @@ class NET_EXPORT ReportingGarbageCollector {
virtual ~ReportingGarbageCollector(); virtual ~ReportingGarbageCollector();
// Initializes the GarbageCollector, which performs an initial garbage
// collection pass over any data already in the Cache.
virtual void Initialize() = 0;
// Replaces the internal Timer used for scheduling garbage collection passes // Replaces the internal Timer used for scheduling garbage collection passes
// with a caller-specified one so that unittests can provide a MockTimer. // with a caller-specified one so that unittests can provide a MockTimer.
virtual void SetTimerForTesting(std::unique_ptr<base::Timer> timer) = 0; virtual void SetTimerForTesting(std::unique_ptr<base::Timer> timer) = 0;
......
...@@ -16,7 +16,6 @@ ...@@ -16,7 +16,6 @@
#include "net/reporting/reporting_cache.h" #include "net/reporting/reporting_cache.h"
#include "net/reporting/reporting_client.h" #include "net/reporting/reporting_client.h"
#include "net/reporting/reporting_context.h" #include "net/reporting/reporting_context.h"
#include "net/reporting/reporting_delegate.h"
#include "net/reporting/reporting_observer.h" #include "net/reporting/reporting_observer.h"
#include "net/reporting/reporting_policy.h" #include "net/reporting/reporting_policy.h"
#include "net/reporting/reporting_report.h" #include "net/reporting/reporting_report.h"
...@@ -61,49 +60,15 @@ bool DeserializeOrigin(const base::DictionaryValue& serialized, ...@@ -61,49 +60,15 @@ bool DeserializeOrigin(const base::DictionaryValue& serialized,
return true; return true;
} }
class ReportingPersisterImpl : public ReportingPersister, class ReportingPersisterImpl : public ReportingPersister {
public ReportingObserver {
public: public:
ReportingPersisterImpl(ReportingContext* context) ReportingPersisterImpl(ReportingContext* context) : context_(context) {}
: context_(context), timer_(base::MakeUnique<base::OneShotTimer>()) {}
// ReportingPersister implementation: // ReportingPersister implementation:
~ReportingPersisterImpl() override { ~ReportingPersisterImpl() override {}
DCHECK(context_->initialized());
context_->RemoveObserver(this);
}
void Initialize() override {
std::unique_ptr<const base::Value> persisted_data =
context_->delegate()->GetPersistedData();
if (persisted_data)
Deserialize(*persisted_data);
context_->AddObserver(this);
}
void SetTimerForTesting(std::unique_ptr<base::Timer> timer) override {
DCHECK(!context_->initialized());
timer_ = std::move(timer);
}
// ReportingObserver implementation:
void OnCacheUpdated() override {
DCHECK(context_->initialized());
if (!timer_->IsRunning())
StartTimer();
}
private: private:
void StartTimer() {
timer_->Start(
FROM_HERE, context_->policy().persistence_interval,
base::Bind(&ReportingPersisterImpl::Persist, base::Unretained(this)));
}
void Persist() { delegate()->PersistData(Serialize()); }
std::string SerializeTicks(base::TimeTicks time_ticks) { std::string SerializeTicks(base::TimeTicks time_ticks) {
base::Time time = time_ticks - tick_clock()->NowTicks() + clock()->Now(); base::Time time = time_ticks - tick_clock()->NowTicks() + clock()->Now();
return base::Int64ToString(time.ToInternalValue()); return base::Int64ToString(time.ToInternalValue());
...@@ -336,13 +301,11 @@ class ReportingPersisterImpl : public ReportingPersister, ...@@ -336,13 +301,11 @@ class ReportingPersisterImpl : public ReportingPersister,
} }
const ReportingPolicy& policy() { return context_->policy(); } const ReportingPolicy& policy() { return context_->policy(); }
ReportingDelegate* delegate() { return context_->delegate(); }
base::Clock* clock() { return context_->clock(); } base::Clock* clock() { return context_->clock(); }
base::TickClock* tick_clock() { return context_->tick_clock(); } base::TickClock* tick_clock() { return context_->tick_clock(); }
ReportingCache* cache() { return context_->cache(); } ReportingCache* cache() { return context_->cache(); }
ReportingContext* context_; ReportingContext* context_;
std::unique_ptr<base::Timer> timer_;
}; };
} // namespace } // namespace
......
...@@ -9,30 +9,18 @@ ...@@ -9,30 +9,18 @@
#include "net/base/net_export.h" #include "net/base/net_export.h"
namespace base {
class Timer;
} // namespace base
namespace net { namespace net {
class ReportingContext; class ReportingContext;
// Periodically persists the state of the Reporting system to (reasonably) // Will persist the state of the Reporting system to (reasonably) stable
// stable storage using the methods provided by the ReportingDelegate. // storage using an as-yet-unwritten persistence mechanism within //net.
class NET_EXPORT ReportingPersister { class NET_EXPORT ReportingPersister {
public: public:
// Creates a ReportingPersister. |context| must outlive the persister. // Creates a ReportingPersister. |context| must outlive the persister.
static std::unique_ptr<ReportingPersister> Create(ReportingContext* context); static std::unique_ptr<ReportingPersister> Create(ReportingContext* context);
virtual ~ReportingPersister(); virtual ~ReportingPersister();
// Initializes the Persister, which deserializes any previously-persisted data
// that is available through the Context's Delegate.
virtual void Initialize() = 0;
// Replaces the internal Timer used for scheduling writes to stable storage
// with a caller-specified one so that unittests can provide a MockTimer.
virtual void SetTimerForTesting(std::unique_ptr<base::Timer> timer) = 0;
}; };
} // namespace net } // namespace net
......
...@@ -30,7 +30,8 @@ class ReportingPersisterTest : public ReportingTestBase { ...@@ -30,7 +30,8 @@ class ReportingPersisterTest : public ReportingTestBase {
const std::string kType_ = "default"; const std::string kType_ = "default";
}; };
TEST_F(ReportingPersisterTest, Test) { // Disabled because the Persister has no persistence layer to use yet.
TEST_F(ReportingPersisterTest, DISABLED_Test) {
ReportingPolicy policy; ReportingPolicy policy;
policy.persist_reports_across_restarts = true; policy.persist_reports_across_restarts = true;
policy.persist_clients_across_restarts = true; policy.persist_clients_across_restarts = true;
...@@ -49,8 +50,7 @@ TEST_F(ReportingPersisterTest, Test) { ...@@ -49,8 +50,7 @@ TEST_F(ReportingPersisterTest, Test) {
kGroup_, kGroup_,
tick_clock()->NowTicks() + base::TimeDelta::FromDays(1)); tick_clock()->NowTicks() + base::TimeDelta::FromDays(1));
EXPECT_TRUE(persistence_timer()->IsRunning()); // TODO: Actually trigger persistence, once it's possible.
persistence_timer()->Fire();
SimulateRestart(/* delta= */ base::TimeDelta::FromHours(1), SimulateRestart(/* delta= */ base::TimeDelta::FromHours(1),
/* delta_ticks= */ base::TimeDelta::FromHours(-3)); /* delta_ticks= */ base::TimeDelta::FromHours(-3));
......
...@@ -14,7 +14,6 @@ ...@@ -14,7 +14,6 @@
#include "base/values.h" #include "base/values.h"
#include "net/reporting/reporting_cache.h" #include "net/reporting/reporting_cache.h"
#include "net/reporting/reporting_context.h" #include "net/reporting/reporting_context.h"
#include "net/reporting/reporting_delegate.h"
#include "net/reporting/reporting_header_parser.h" #include "net/reporting/reporting_header_parser.h"
#include "url/gurl.h" #include "url/gurl.h"
...@@ -25,11 +24,7 @@ namespace { ...@@ -25,11 +24,7 @@ namespace {
class ReportingServiceImpl : public ReportingService { class ReportingServiceImpl : public ReportingService {
public: public:
ReportingServiceImpl(std::unique_ptr<ReportingContext> context) ReportingServiceImpl(std::unique_ptr<ReportingContext> context)
: context_(std::move(context)) { : context_(std::move(context)) {}
// TODO(juliatuttle): This can be slow, so it might be better to expose it
// as a separate method and call it separately from constructing everything.
context_->Initialize();
}
~ReportingServiceImpl() override {} ~ReportingServiceImpl() override {}
...@@ -37,14 +32,12 @@ class ReportingServiceImpl : public ReportingService { ...@@ -37,14 +32,12 @@ class ReportingServiceImpl : public ReportingService {
const std::string& group, const std::string& group,
const std::string& type, const std::string& type,
std::unique_ptr<const base::Value> body) override { std::unique_ptr<const base::Value> body) override {
DCHECK(context_->initialized());
context_->cache()->AddReport(url, group, type, std::move(body), context_->cache()->AddReport(url, group, type, std::move(body),
context_->tick_clock()->NowTicks(), 0); context_->tick_clock()->NowTicks(), 0);
} }
void ProcessHeader(const GURL& url, void ProcessHeader(const GURL& url,
const std::string& header_value) override { const std::string& header_value) override {
DCHECK(context_->initialized());
ReportingHeaderParser::ParseHeader(context_.get(), url, header_value); ReportingHeaderParser::ParseHeader(context_.get(), url, header_value);
} }
...@@ -61,10 +54,9 @@ ReportingService::~ReportingService() {} ...@@ -61,10 +54,9 @@ ReportingService::~ReportingService() {}
// static // static
std::unique_ptr<ReportingService> ReportingService::Create( std::unique_ptr<ReportingService> ReportingService::Create(
const ReportingPolicy& policy, const ReportingPolicy& policy,
URLRequestContext* request_context, URLRequestContext* request_context) {
std::unique_ptr<ReportingDelegate> delegate) {
return base::MakeUnique<ReportingServiceImpl>( return base::MakeUnique<ReportingServiceImpl>(
ReportingContext::Create(policy, std::move(delegate), request_context)); ReportingContext::Create(policy, request_context));
} }
// static // static
......
...@@ -21,7 +21,6 @@ class Value; ...@@ -21,7 +21,6 @@ class Value;
namespace net { namespace net {
class ReportingContext; class ReportingContext;
class ReportingDelegate;
struct ReportingPolicy; struct ReportingPolicy;
class URLRequestContext; class URLRequestContext;
...@@ -32,12 +31,10 @@ class NET_EXPORT ReportingService { ...@@ -32,12 +31,10 @@ class NET_EXPORT ReportingService {
virtual ~ReportingService(); virtual ~ReportingService();
// Creates a ReportingService. |policy| will be copied. |request_context| must // Creates a ReportingService. |policy| will be copied. |request_context| must
// outlive the ReportingService. The ReportingService will take ownership of // outlive the ReportingService.
// |delegate| and destroy it when the service is destroyed.
static std::unique_ptr<ReportingService> Create( static std::unique_ptr<ReportingService> Create(
const ReportingPolicy& policy, const ReportingPolicy& policy,
URLRequestContext* request_context, URLRequestContext* request_context);
std::unique_ptr<ReportingDelegate> delegate);
// Creates a ReportingService for testing purposes using an // Creates a ReportingService for testing purposes using an
// already-constructed ReportingContext. The ReportingService will take // already-constructed ReportingContext. The ReportingService will take
......
...@@ -12,7 +12,6 @@ ...@@ -12,7 +12,6 @@
#include "base/values.h" #include "base/values.h"
#include "net/reporting/reporting_cache.h" #include "net/reporting/reporting_cache.h"
#include "net/reporting/reporting_context.h" #include "net/reporting/reporting_context.h"
#include "net/reporting/reporting_delegate.h"
#include "net/reporting/reporting_policy.h" #include "net/reporting/reporting_policy.h"
#include "net/reporting/reporting_report.h" #include "net/reporting/reporting_report.h"
#include "net/reporting/reporting_service.h" #include "net/reporting/reporting_service.h"
......
...@@ -17,7 +17,6 @@ ...@@ -17,7 +17,6 @@
#include "net/reporting/reporting_cache.h" #include "net/reporting/reporting_cache.h"
#include "net/reporting/reporting_client.h" #include "net/reporting/reporting_client.h"
#include "net/reporting/reporting_context.h" #include "net/reporting/reporting_context.h"
#include "net/reporting/reporting_delegate.h"
#include "net/reporting/reporting_delivery_agent.h" #include "net/reporting/reporting_delivery_agent.h"
#include "net/reporting/reporting_garbage_collector.h" #include "net/reporting/reporting_garbage_collector.h"
#include "net/reporting/reporting_persister.h" #include "net/reporting/reporting_persister.h"
...@@ -91,20 +90,6 @@ const ReportingClient* FindClientInCache(const ReportingCache* cache, ...@@ -91,20 +90,6 @@ const ReportingClient* FindClientInCache(const ReportingCache* cache,
return nullptr; return nullptr;
} }
TestReportingDelegate::TestReportingDelegate() {}
TestReportingDelegate::~TestReportingDelegate() {}
void TestReportingDelegate::PersistData(
std::unique_ptr<const base::Value> persisted_data) {
persisted_data_ = std::move(persisted_data);
}
std::unique_ptr<const base::Value> TestReportingDelegate::GetPersistedData() {
if (!persisted_data_)
return std::unique_ptr<const base::Value>();
return persisted_data_->CreateDeepCopy();
}
TestReportingUploader::PendingUpload::~PendingUpload() {} TestReportingUploader::PendingUpload::~PendingUpload() {}
TestReportingUploader::PendingUpload::PendingUpload() {} TestReportingUploader::PendingUpload::PendingUpload() {}
...@@ -120,18 +105,14 @@ void TestReportingUploader::StartUpload(const GURL& url, ...@@ -120,18 +105,14 @@ void TestReportingUploader::StartUpload(const GURL& url,
TestReportingContext::TestReportingContext(const ReportingPolicy& policy) TestReportingContext::TestReportingContext(const ReportingPolicy& policy)
: ReportingContext(policy, : ReportingContext(policy,
base::MakeUnique<TestReportingDelegate>(),
base::MakeUnique<base::SimpleTestClock>(), base::MakeUnique<base::SimpleTestClock>(),
base::MakeUnique<base::SimpleTestTickClock>(), base::MakeUnique<base::SimpleTestTickClock>(),
base::MakeUnique<TestReportingUploader>()), base::MakeUnique<TestReportingUploader>()),
delivery_timer_(new base::MockTimer(/* retain_user_task= */ false, delivery_timer_(new base::MockTimer(/* retain_user_task= */ false,
/* is_repeating= */ false)), /* is_repeating= */ false)),
persistence_timer_(new base::MockTimer(/* retain_user_task= */ false,
/* is_repeating= */ false)),
garbage_collection_timer_( garbage_collection_timer_(
new base::MockTimer(/* retain_user_task= */ false, new base::MockTimer(/* retain_user_task= */ false,
/* is_repeating= */ false)) { /* is_repeating= */ false)) {
persister()->SetTimerForTesting(base::WrapUnique(persistence_timer_));
garbage_collector()->SetTimerForTesting( garbage_collector()->SetTimerForTesting(
base::WrapUnique(garbage_collection_timer_)); base::WrapUnique(garbage_collection_timer_));
delivery_agent()->SetTimerForTesting(base::WrapUnique(delivery_timer_)); delivery_agent()->SetTimerForTesting(base::WrapUnique(delivery_timer_));
...@@ -139,7 +120,6 @@ TestReportingContext::TestReportingContext(const ReportingPolicy& policy) ...@@ -139,7 +120,6 @@ TestReportingContext::TestReportingContext(const ReportingPolicy& policy)
TestReportingContext::~TestReportingContext() { TestReportingContext::~TestReportingContext() {
delivery_timer_ = nullptr; delivery_timer_ = nullptr;
persistence_timer_ = nullptr;
garbage_collection_timer_ = nullptr; garbage_collection_timer_ = nullptr;
} }
...@@ -148,34 +128,27 @@ ReportingTestBase::ReportingTestBase() { ...@@ -148,34 +128,27 @@ ReportingTestBase::ReportingTestBase() {
ReportingPolicy policy; ReportingPolicy policy;
policy.endpoint_backoff_policy.jitter_factor = 0.0; policy.endpoint_backoff_policy.jitter_factor = 0.0;
CreateAndInitializeContext(policy, std::unique_ptr<const base::Value>(), CreateContext(policy, base::Time::Now(), base::TimeTicks::Now());
base::Time::Now(), base::TimeTicks::Now());
} }
ReportingTestBase::~ReportingTestBase() {} ReportingTestBase::~ReportingTestBase() {}
void ReportingTestBase::UsePolicy(const ReportingPolicy& new_policy) { void ReportingTestBase::UsePolicy(const ReportingPolicy& new_policy) {
CreateAndInitializeContext(new_policy, delegate()->GetPersistedData(), CreateContext(new_policy, clock()->Now(), tick_clock()->NowTicks());
clock()->Now(), tick_clock()->NowTicks());
} }
void ReportingTestBase::SimulateRestart(base::TimeDelta delta, void ReportingTestBase::SimulateRestart(base::TimeDelta delta,
base::TimeDelta delta_ticks) { base::TimeDelta delta_ticks) {
CreateAndInitializeContext(policy(), delegate()->GetPersistedData(), CreateContext(policy(), clock()->Now() + delta,
clock()->Now() + delta,
tick_clock()->NowTicks() + delta_ticks); tick_clock()->NowTicks() + delta_ticks);
} }
void ReportingTestBase::CreateAndInitializeContext( void ReportingTestBase::CreateContext(const ReportingPolicy& policy,
const ReportingPolicy& policy,
std::unique_ptr<const base::Value> persisted_data,
base::Time now, base::Time now,
base::TimeTicks now_ticks) { base::TimeTicks now_ticks) {
context_ = base::MakeUnique<TestReportingContext>(policy); context_ = base::MakeUnique<TestReportingContext>(policy);
delegate()->PersistData(std::move(persisted_data));
clock()->SetNow(now); clock()->SetNow(now);
tick_clock()->SetNowTicks(now_ticks); tick_clock()->SetNowTicks(now_ticks);
context_->Initialize();
} }
base::TimeTicks ReportingTestBase::yesterday() { base::TimeTicks ReportingTestBase::yesterday() {
......
...@@ -11,7 +11,6 @@ ...@@ -11,7 +11,6 @@
#include "base/macros.h" #include "base/macros.h"
#include "net/reporting/reporting_context.h" #include "net/reporting/reporting_context.h"
#include "net/reporting/reporting_delegate.h"
#include "net/reporting/reporting_uploader.h" #include "net/reporting/reporting_uploader.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
...@@ -40,24 +39,6 @@ const ReportingClient* FindClientInCache(const ReportingCache* cache, ...@@ -40,24 +39,6 @@ const ReportingClient* FindClientInCache(const ReportingCache* cache,
const url::Origin& origin, const url::Origin& origin,
const GURL& endpoint); const GURL& endpoint);
// A simple implementation of ReportingDelegate that only persists data in RAM.
class TestReportingDelegate : public ReportingDelegate {
public:
TestReportingDelegate();
~TestReportingDelegate() override;
// ReportingDelegate implementation:
std::unique_ptr<const base::Value> GetPersistedData() override;
void PersistData(std::unique_ptr<const base::Value> persisted_data) override;
private:
std::unique_ptr<const base::Value> persisted_data_;
DISALLOW_COPY_AND_ASSIGN(TestReportingDelegate);
};
// A test implementation of ReportingUploader that holds uploads for tests to // A test implementation of ReportingUploader that holds uploads for tests to
// examine and complete with a specified outcome. // examine and complete with a specified outcome.
class TestReportingUploader : public ReportingUploader { class TestReportingUploader : public ReportingUploader {
...@@ -95,15 +76,12 @@ class TestReportingUploader : public ReportingUploader { ...@@ -95,15 +76,12 @@ class TestReportingUploader : public ReportingUploader {
}; };
// A test implementation of ReportingContext that uses test versions of // A test implementation of ReportingContext that uses test versions of
// ReportingDelegate, Clock, TickClock, and ReportingUploader. // Clock, TickClock, Timer, and ReportingUploader.
class TestReportingContext : public ReportingContext { class TestReportingContext : public ReportingContext {
public: public:
TestReportingContext(const ReportingPolicy& policy); TestReportingContext(const ReportingPolicy& policy);
~TestReportingContext(); ~TestReportingContext();
TestReportingDelegate* test_delegate() {
return reinterpret_cast<TestReportingDelegate*>(delegate());
}
base::SimpleTestClock* test_clock() { base::SimpleTestClock* test_clock() {
return reinterpret_cast<base::SimpleTestClock*>(clock()); return reinterpret_cast<base::SimpleTestClock*>(clock());
} }
...@@ -111,7 +89,6 @@ class TestReportingContext : public ReportingContext { ...@@ -111,7 +89,6 @@ class TestReportingContext : public ReportingContext {
return reinterpret_cast<base::SimpleTestTickClock*>(tick_clock()); return reinterpret_cast<base::SimpleTestTickClock*>(tick_clock());
} }
base::MockTimer* test_delivery_timer() { return delivery_timer_; } base::MockTimer* test_delivery_timer() { return delivery_timer_; }
base::MockTimer* test_persistence_timer() { return persistence_timer_; }
base::MockTimer* test_garbage_collection_timer() { base::MockTimer* test_garbage_collection_timer() {
return garbage_collection_timer_; return garbage_collection_timer_;
} }
...@@ -124,7 +101,6 @@ class TestReportingContext : public ReportingContext { ...@@ -124,7 +101,6 @@ class TestReportingContext : public ReportingContext {
// here to preserve type: // here to preserve type:
base::MockTimer* delivery_timer_; base::MockTimer* delivery_timer_;
base::MockTimer* persistence_timer_;
base::MockTimer* garbage_collection_timer_; base::MockTimer* garbage_collection_timer_;
DISALLOW_COPY_AND_ASSIGN(TestReportingContext); DISALLOW_COPY_AND_ASSIGN(TestReportingContext);
...@@ -139,8 +115,7 @@ class ReportingTestBase : public ::testing::Test { ...@@ -139,8 +115,7 @@ class ReportingTestBase : public ::testing::Test {
void UsePolicy(const ReportingPolicy& policy); void UsePolicy(const ReportingPolicy& policy);
// Simulates an embedder restart, preserving the ReportingPolicy and any data // Simulates an embedder restart, preserving the ReportingPolicy.
// persisted via the TestReportingDelegate, but nothing else.
// //
// Advances the Clock by |delta|, and the TickClock by |delta_ticks|. Both can // Advances the Clock by |delta|, and the TickClock by |delta_ticks|. Both can
// be zero or negative. // be zero or negative.
...@@ -150,15 +125,11 @@ class ReportingTestBase : public ::testing::Test { ...@@ -150,15 +125,11 @@ class ReportingTestBase : public ::testing::Test {
const ReportingPolicy& policy() { return context_->policy(); } const ReportingPolicy& policy() { return context_->policy(); }
TestReportingDelegate* delegate() { return context_->test_delegate(); }
base::SimpleTestClock* clock() { return context_->test_clock(); } base::SimpleTestClock* clock() { return context_->test_clock(); }
base::SimpleTestTickClock* tick_clock() { base::SimpleTestTickClock* tick_clock() {
return context_->test_tick_clock(); return context_->test_tick_clock();
} }
base::MockTimer* delivery_timer() { return context_->test_delivery_timer(); } base::MockTimer* delivery_timer() { return context_->test_delivery_timer(); }
base::MockTimer* persistence_timer() {
return context_->test_persistence_timer();
}
base::MockTimer* garbage_collection_timer() { base::MockTimer* garbage_collection_timer() {
return context_->test_garbage_collection_timer(); return context_->test_garbage_collection_timer();
} }
...@@ -187,9 +158,7 @@ class ReportingTestBase : public ::testing::Test { ...@@ -187,9 +158,7 @@ class ReportingTestBase : public ::testing::Test {
} }
private: private:
void CreateAndInitializeContext( void CreateContext(const ReportingPolicy& policy,
const ReportingPolicy& policy,
std::unique_ptr<const base::Value> persisted_data,
base::Time now, base::Time now,
base::TimeTicks now_ticks); base::TimeTicks now_ticks);
......
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