#include "content/browser/aggregation_service/aggregation_service_storage_sql.h"
#include <stddef.h>
#include <stdint.h>
#include <optional>
#include <set>
#include <string>
#include <string_view>
#include <tuple>
#include <utility>
#include <vector>
#include "base/check.h"
#include "base/check_op.h"
#include "base/containers/span.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/logging.h"
#include "base/metrics/histogram_functions.h"
#include "base/sequence_checker.h"
#include "base/strings/cstring_view.h"
#include "base/time/clock.h"
#include "base/time/time.h"
#include "base/timer/elapsed_timer.h"
#include "content/browser/aggregation_service/aggregatable_report.h"
#include "content/browser/aggregation_service/aggregation_service_storage.h"
#include "content/browser/aggregation_service/proto/aggregatable_report.pb.h"
#include "content/browser/aggregation_service/public_key.h"
#include "content/public/browser/storage_partition.h"
#include "services/network/public/cpp/is_potentially_trustworthy.h"
#include "sql/database.h"
#include "sql/error_delegate_util.h"
#include "sql/meta_table.h"
#include "sql/sqlite_result_code.h"
#include "sql/statement.h"
#include "sql/statement_id.h"
#include "sql/transaction.h"
#include "third_party/blink/public/common/storage_key/storage_key.h"
#include "url/gurl.h"
#include "url/origin.h"
namespace content {
constexpr int AggregationServiceStorageSql::kCurrentVersionNumber = …;
constexpr int AggregationServiceStorageSql::kCompatibleVersionNumber = …;
constexpr int AggregationServiceStorageSql::kDeprecatedVersionNumber = …;
namespace {
constexpr base::FilePath::CharType kDatabasePath[] = …);
static constexpr base::cstring_view kReportRequestsCreateTableSql = …
"CREATE TABLE IF NOT EXISTS report_requests("
"request_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"
"report_time INTEGER NOT NULL,"
"creation_time INTEGER NOT NULL,"
"reporting_origin TEXT NOT NULL,"
"request_proto BLOB NOT NULL)";
static constexpr base::cstring_view kReportTimeIndexSql = …;
static constexpr base::cstring_view kCreationTimeIndexSql = …;
static constexpr base::cstring_view kReportingOriginIndexSql = …;
bool UpgradeAggregationServiceStorageSqlSchema(sql::Database& db,
sql::MetaTable& meta_table) { … }
void RecordInitializationStatus(
const AggregationServiceStorageSql::InitStatus status) { … }
}
AggregationServiceStorageSql::AggregationServiceStorageSql(
bool run_in_memory,
const base::FilePath& path_to_database,
const base::Clock* clock,
int max_stored_requests_per_reporting_origin)
: … { … }
AggregationServiceStorageSql::~AggregationServiceStorageSql() { … }
std::vector<PublicKey> AggregationServiceStorageSql::GetPublicKeys(
const GURL& url) { … }
void AggregationServiceStorageSql::SetPublicKeys(const GURL& url,
const PublicKeyset& keyset) { … }
void AggregationServiceStorageSql::ClearPublicKeys(const GURL& url) { … }
void AggregationServiceStorageSql::ClearPublicKeysFetchedBetween(
base::Time delete_begin,
base::Time delete_end) { … }
void AggregationServiceStorageSql::ClearPublicKeysExpiredBy(
base::Time delete_end) { … }
bool AggregationServiceStorageSql::InsertPublicKeysImpl(
const GURL& url,
const PublicKeyset& keyset) { … }
bool AggregationServiceStorageSql::ClearPublicKeysImpl(const GURL& url) { … }
bool AggregationServiceStorageSql::ClearPublicKeysByUrlId(int64_t url_id) { … }
void AggregationServiceStorageSql::ClearAllPublicKeys() { … }
bool AggregationServiceStorageSql::ReportingOriginHasCapacity(
std::string_view serialized_reporting_origin) { … }
void AggregationServiceStorageSql::UpdateReportForSendFailure(
AggregationServiceStorage::RequestId request_id,
base::Time new_report_time) { … }
void AggregationServiceStorageSql::StoreRequest(
AggregatableReportRequest request) { … }
void AggregationServiceStorageSql::DeleteRequest(
AggregationServiceStorage::RequestId request_id) { … }
bool AggregationServiceStorageSql::DeleteRequestImpl(RequestId request_id) { … }
std::optional<base::Time> AggregationServiceStorageSql::NextReportTimeAfter(
base::Time strictly_after_time) { … }
std::optional<base::Time> AggregationServiceStorageSql::NextReportTimeAfterImpl(
base::Time strictly_after_time) { … }
std::vector<AggregationServiceStorage::RequestAndId>
AggregationServiceStorageSql::GetRequestsReportingOnOrBefore(
base::Time not_after_time,
std::optional<int> limit) { … }
std::vector<AggregationServiceStorage::RequestAndId>
AggregationServiceStorageSql::GetRequests(
const std::vector<AggregationServiceStorage::RequestId>& ids) { … }
std::optional<base::Time>
AggregationServiceStorageSql::AdjustOfflineReportTimes(
base::Time now,
base::TimeDelta min_delay,
base::TimeDelta max_delay) { … }
std::set<url::Origin>
AggregationServiceStorageSql::GetReportRequestReportingOrigins() { … }
void AggregationServiceStorageSql::ClearDataBetween(
base::Time delete_begin,
base::Time delete_end,
StoragePartition::StorageKeyMatcherFunction filter) { … }
void AggregationServiceStorageSql::ClearRequestsStoredBetween(
base::Time delete_begin,
base::Time delete_end,
StoragePartition::StorageKeyMatcherFunction filter) { … }
void AggregationServiceStorageSql::ClearAllRequests() { … }
void AggregationServiceStorageSql::HandleInitializationFailure(
const InitStatus status) { … }
bool AggregationServiceStorageSql::EnsureDatabaseOpen(
DbCreationPolicy creation_policy) { … }
bool AggregationServiceStorageSql::InitializeSchema(bool db_empty) { … }
bool AggregationServiceStorageSql::CreateSchema() { … }
void AggregationServiceStorageSql::DatabaseErrorCallback(int extended_error,
sql::Statement* stmt) { … }
}