#ifndef SERVICES_NETWORK_SCT_AUDITING_SCT_AUDITING_REPORTER_H_
#define SERVICES_NETWORK_SCT_AUDITING_SCT_AUDITING_REPORTER_H_
#include <memory>
#include <optional>
#include <string>
#include "base/component_export.h"
#include "base/functional/callback_forward.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
#include "base/time/time.h"
#include "base/values.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "net/base/backoff_entry.h"
#include "net/base/hash_value.h"
#include "net/cert/signed_certificate_timestamp_and_status.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "services/network/public/mojom/network_service.mojom-forward.h"
#include "services/network/public/mojom/url_loader_factory.mojom.h"
#include "services/network/public/proto/sct_audit_report.pb.h"
namespace net {
class HttpResponseHeaders;
}
namespace network {
class NetworkContext;
class SimpleURLLoader;
class COMPONENT_EXPORT(NETWORK_SERVICE) SCTAuditingReporter {
public:
using ReporterDoneCallback = base::OnceCallback<void(net::HashValue)>;
using ReporterUpdatedCallback = base::RepeatingCallback<void()>;
struct COMPONENT_EXPORT(NETWORK_SERVICE) SCTHashdanceMetadata {
static std::optional<SCTHashdanceMetadata> FromValue(
const base::Value& value);
SCTHashdanceMetadata();
~SCTHashdanceMetadata();
SCTHashdanceMetadata(const SCTHashdanceMetadata&) = delete;
SCTHashdanceMetadata operator=(const SCTHashdanceMetadata&) = delete;
SCTHashdanceMetadata(SCTHashdanceMetadata&&);
SCTHashdanceMetadata& operator=(SCTHashdanceMetadata&&);
base::Value ToValue() const;
std::string leaf_hash;
base::Time issued;
std::string log_id;
base::TimeDelta log_mmd;
base::Time certificate_expiry;
};
enum class LookupQueryResult {
kHTTPError = 0,
kInvalidJson = 1,
kStatusNotOk = 2,
kCertificateExpired = 3,
kLogNotFound = 4,
kLogNotYetIngested = 5,
kSCTSuffixFound = 6,
kSCTSuffixNotFound = 7,
kMaxValue = kSCTSuffixNotFound,
};
enum class CompletionStatus {
kSuccessFirstTry = 0,
kSuccessAfterRetries = 1,
kRetriesExhausted = 2,
kMaxValue = kRetriesExhausted,
};
SCTAuditingReporter(
NetworkContext* owner_network_context_,
net::HashValue reporter_key,
std::unique_ptr<sct_auditing::SCTClientReport> report,
bool is_hashdance,
std::optional<SCTHashdanceMetadata> hashdance_metadata,
mojom::SCTAuditingConfigurationPtr configuration,
mojom::URLLoaderFactory* url_loader_factory,
ReporterUpdatedCallback update_callback,
ReporterDoneCallback done_callback,
std::unique_ptr<net::BackoffEntry> backoff_entry = nullptr,
bool counted_towards_report_limit = false);
~SCTAuditingReporter();
SCTAuditingReporter(const SCTAuditingReporter&) = delete;
SCTAuditingReporter& operator=(const SCTAuditingReporter&) = delete;
static const net::BackoffEntry::Policy kDefaultBackoffPolicy;
void Start();
net::HashValue key() { return reporter_key_; }
sct_auditing::SCTClientReport* report() { return report_.get(); }
net::BackoffEntry* backoff_entry() { return backoff_entry_.get(); }
const std::optional<SCTHashdanceMetadata>& sct_hashdance_metadata() {
return sct_hashdance_metadata_;
}
bool counted_towards_report_limit() { return counted_towards_report_limit_; }
static void SetRetryDelayForTesting(std::optional<base::TimeDelta> delay);
private:
void OnCheckReportAllowedStatusComplete(bool allowed);
void ScheduleRequestWithBackoff(base::OnceClosure request,
base::TimeDelta minimum_delay);
void SendLookupQuery();
void OnSendLookupQueryComplete(std::optional<std::string> response_body);
void SendReport();
void OnSendReportComplete(scoped_refptr<net::HttpResponseHeaders> headers);
void MaybeRetryRequest();
raw_ptr<NetworkContext> owner_network_context_;
net::HashValue reporter_key_;
std::unique_ptr<sct_auditing::SCTClientReport> report_;
bool is_hashdance_;
std::optional<SCTHashdanceMetadata> sct_hashdance_metadata_;
mojo::Remote<mojom::URLLoaderFactory> url_loader_factory_remote_;
std::unique_ptr<SimpleURLLoader> url_loader_;
mojom::SCTAuditingConfigurationPtr configuration_;
ReporterUpdatedCallback update_callback_;
ReporterDoneCallback done_callback_;
net::BackoffEntry::Policy backoff_policy_;
std::unique_ptr<net::BackoffEntry> backoff_entry_;
int max_retries_;
bool counted_towards_report_limit_;
base::WeakPtrFactory<SCTAuditingReporter> weak_factory_{this};
};
}
#endif