#ifndef CONTENT_BROWSER_AGGREGATION_SERVICE_AGGREGATABLE_REPORT_ASSEMBLER_H_
#define CONTENT_BROWSER_AGGREGATION_SERVICE_AGGREGATABLE_REPORT_ASSEMBLER_H_
#include <stddef.h>
#include <stdint.h>
#include <memory>
#include <optional>
#include "base/containers/flat_map.h"
#include "base/functional/callback.h"
#include "content/browser/aggregation_service/aggregatable_report.h"
#include "content/browser/aggregation_service/aggregation_service_key_fetcher.h"
#include "content/browser/aggregation_service/public_key.h"
#include "content/common/content_export.h"
template <class T>
class scoped_refptr;
namespace network {
class SharedURLLoaderFactory;
}
namespace content {
class AggregationServiceStorageContext;
class StoragePartition;
class CONTENT_EXPORT AggregatableReportAssembler {
public:
enum class AssemblyStatus {
kOk = 0,
kPublicKeyFetchFailed = 1,
kAssemblyFailed = 2,
kTooManySimultaneousRequests = 3,
kMaxValue = kTooManySimultaneousRequests,
};
using AssemblyCallback =
base::OnceCallback<void(AggregatableReportRequest,
std::optional<AggregatableReport>,
AssemblyStatus)>;
static constexpr size_t kMaxSimultaneousRequests = 1000;
AggregatableReportAssembler(AggregationServiceStorageContext* storage_context,
StoragePartition* storage_partition);
AggregatableReportAssembler(const AggregatableReportAssembler& other) =
delete;
AggregatableReportAssembler& operator=(
const AggregatableReportAssembler& other) = delete;
virtual ~AggregatableReportAssembler();
static std::unique_ptr<AggregatableReportAssembler> CreateForTesting(
std::unique_ptr<AggregationServiceKeyFetcher> fetcher,
std::unique_ptr<AggregatableReport::Provider> report_provider);
static std::unique_ptr<AggregatableReportAssembler> CreateForTesting(
AggregationServiceStorageContext* storage_context,
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
bool enable_debug_logging);
virtual void AssembleReport(AggregatableReportRequest report_request,
AssemblyCallback callback);
protected:
AggregatableReportAssembler(
AggregationServiceStorageContext* storage_context,
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
bool enable_debug_logging = false);
private:
struct PendingRequest {
PendingRequest(AggregatableReportRequest report_request,
AssemblyCallback callback);
PendingRequest(PendingRequest&& other);
PendingRequest& operator=(PendingRequest&& other);
~PendingRequest();
AggregatableReportRequest report_request;
AssemblyCallback callback;
};
AggregatableReportAssembler(
std::unique_ptr<AggregationServiceKeyFetcher> fetcher,
std::unique_ptr<AggregatableReport::Provider> report_provider);
void OnPublicKeyFetched(
int64_t report_id,
std::optional<PublicKey> key,
AggregationServiceKeyFetcher::PublicKeyFetchStatus status);
base::flat_map<int64_t, PendingRequest> pending_requests_;
int64_t unique_id_counter_ = 0;
std::unique_ptr<AggregationServiceKeyFetcher> fetcher_;
std::unique_ptr<AggregatableReport::Provider> report_provider_;
};
}
#endif