#ifndef COMPONENTS_DOMAIN_RELIABILITY_CONTEXT_H_
#define COMPONENTS_DOMAIN_RELIABILITY_CONTEXT_H_
#include <deque>
#include <map>
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/time/time.h"
#include "components/domain_reliability/beacon.h"
#include "components/domain_reliability/config.h"
#include "components/domain_reliability/domain_reliability_export.h"
#include "components/domain_reliability/scheduler.h"
#include "components/domain_reliability/uploader.h"
#include "components/domain_reliability/util.h"
class GURL;
namespace domain_reliability {
class DomainReliabilityDispatcher;
class MockableTime;
class DOMAIN_RELIABILITY_EXPORT DomainReliabilityContext {
public:
DomainReliabilityContext(
MockableTime* time,
const DomainReliabilityScheduler::Params& scheduler_params,
DomainReliabilityDispatcher* dispatcher,
DomainReliabilityUploader* uploader,
scoped_ptr<const DomainReliabilityConfig> config);
virtual ~DomainReliabilityContext();
void AddBeacon(const DomainReliabilityBeacon& beacon, const GURL& url);
void GetQueuedDataForTesting(
int resource_index,
std::vector<DomainReliabilityBeacon>* beacons_out,
int* successful_requests_out,
int* failed_requests_out) const;
const DomainReliabilityConfig& config() { return *config_.get(); }
static const int kMaxQueuedBeacons;
private:
class ResourceState {
public:
ResourceState(DomainReliabilityContext* context,
const DomainReliabilityConfig::Resource* config);
~ResourceState();
scoped_ptr<base::Value> ToValue(base::TimeTicks upload_time) const;
void MarkUpload();
void CommitUpload();
bool GetOldestBeaconStart(base::TimeTicks* oldest_start_out) const;
void RemoveOldestBeacon();
DomainReliabilityContext* context;
const DomainReliabilityConfig::Resource* config;
std::deque<DomainReliabilityBeacon> beacons;
int successful_requests;
int failed_requests;
size_t uploading_beacons_size;
int uploading_successful_requests;
int uploading_failed_requests;
private:
DISALLOW_COPY_AND_ASSIGN(ResourceState);
};
typedef ScopedVector<ResourceState> ResourceStateVector;
typedef ResourceStateVector::const_iterator ResourceStateIterator;
void InitializeResourceStates();
void ScheduleUpload(base::TimeDelta min_delay, base::TimeDelta max_delay);
void StartUpload();
void OnUploadComplete(bool success);
scoped_ptr<const base::Value> CreateReport(base::TimeTicks upload_time) const;
void MarkUpload();
void CommitUpload();
void RemoveOldestBeacon();
scoped_ptr<const DomainReliabilityConfig> config_;
MockableTime* time_;
DomainReliabilityScheduler scheduler_;
DomainReliabilityDispatcher* dispatcher_;
DomainReliabilityUploader* uploader_;
ResourceStateVector states_;
int beacon_count_;
int uploading_beacon_count_;
base::WeakPtrFactory<DomainReliabilityContext> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(DomainReliabilityContext);
};
}
#endif