#include "net/extras/sqlite/sqlite_persistent_reporting_and_nel_store.h"
#include <list>
#include <map>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "base/feature_list.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/json/json_reader.h"
#include "base/json/json_string_value_serializer.h"
#include "base/metrics/histogram_macros.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/task_traits.h"
#include "base/thread_annotations.h"
#include "net/base/features.h"
#include "net/base/network_anonymization_key.h"
#include "net/extras/sqlite/sqlite_persistent_store_backend_base.h"
#include "net/reporting/reporting_endpoint.h"
#include "net/reporting/reporting_target_type.h"
#include "sql/database.h"
#include "sql/meta_table.h"
#include "sql/statement.h"
#include "sql/transaction.h"
#include "url/origin.h"
namespace net {
namespace {
const int kCurrentVersionNumber = …;
const int kCompatibleVersionNumber = …;
const char kNumberOfLoadedNelPoliciesHistogramName[] = …;
const char kNumberOfLoadedNelPolicies2HistogramName[] = …;
const char kNumberOfLoadedReportingEndpoints2HistogramName[] = …;
const char kNumberOfLoadedReportingEndpointGroups2HistogramName[] = …;
}
base::TaskPriority GetReportingAndNelStoreBackgroundSequencePriority() { … }
[[nodiscard]] bool NetworkAnonymizationKeyToString(
const NetworkAnonymizationKey& network_anonymization_key,
std::string* out_string) { … }
[[nodiscard]] bool NetworkAnonymizationKeyFromString(
const std::string& string,
NetworkAnonymizationKey* out_network_anonymization_key) { … }
class SQLitePersistentReportingAndNelStore::Backend
: public SQLitePersistentStoreBackendBase { … };
namespace {
bool CreateV2NelPoliciesSchema(sql::Database* db) { … }
bool CreateV2ReportingEndpointsSchema(sql::Database* db) { … }
bool CreateV2ReportingEndpointGroupsSchema(sql::Database* db) { … }
}
template <typename DataType>
class SQLitePersistentReportingAndNelStore::Backend::PendingOperation { … };
struct SQLitePersistentReportingAndNelStore::Backend::NelPolicyInfo { … };
struct SQLitePersistentReportingAndNelStore::Backend::ReportingEndpointInfo { … };
struct SQLitePersistentReportingAndNelStore::Backend::
ReportingEndpointGroupInfo { … };
void SQLitePersistentReportingAndNelStore::Backend::LoadNelPolicies(
NelPoliciesLoadedCallback loaded_callback) { … }
void SQLitePersistentReportingAndNelStore::Backend::AddNelPolicy(
const NetworkErrorLoggingService::NelPolicy& policy) { … }
void SQLitePersistentReportingAndNelStore::Backend::UpdateNelPolicyAccessTime(
const NetworkErrorLoggingService::NelPolicy& policy) { … }
void SQLitePersistentReportingAndNelStore::Backend::DeleteNelPolicy(
const NetworkErrorLoggingService::NelPolicy& policy) { … }
void SQLitePersistentReportingAndNelStore::Backend::LoadReportingClients(
ReportingClientsLoadedCallback loaded_callback) { … }
void SQLitePersistentReportingAndNelStore::Backend::AddReportingEndpoint(
const ReportingEndpoint& endpoint) { … }
void SQLitePersistentReportingAndNelStore::Backend::AddReportingEndpointGroup(
const CachedReportingEndpointGroup& group) { … }
void SQLitePersistentReportingAndNelStore::Backend::
UpdateReportingEndpointGroupAccessTime(
const CachedReportingEndpointGroup& group) { … }
void SQLitePersistentReportingAndNelStore::Backend::
UpdateReportingEndpointDetails(const ReportingEndpoint& endpoint) { … }
void SQLitePersistentReportingAndNelStore::Backend::
UpdateReportingEndpointGroupDetails(
const CachedReportingEndpointGroup& group) { … }
void SQLitePersistentReportingAndNelStore::Backend::DeleteReportingEndpoint(
const ReportingEndpoint& endpoint) { … }
void SQLitePersistentReportingAndNelStore::Backend::
DeleteReportingEndpointGroup(const CachedReportingEndpointGroup& group) { … }
size_t SQLitePersistentReportingAndNelStore::Backend::GetQueueLengthForTesting()
const { … }
bool SQLitePersistentReportingAndNelStore::Backend::CreateDatabaseSchema() { … }
std::optional<int>
SQLitePersistentReportingAndNelStore::Backend::DoMigrateDatabaseSchema() { … }
void SQLitePersistentReportingAndNelStore::Backend::DoCommit() { … }
bool SQLitePersistentReportingAndNelStore::Backend::CommitNelPolicyOperation(
PendingOperation<NelPolicyInfo>* op) { … }
bool SQLitePersistentReportingAndNelStore::Backend::
CommitReportingEndpointOperation(
PendingOperation<ReportingEndpointInfo>* op) { … }
bool SQLitePersistentReportingAndNelStore::Backend::
CommitReportingEndpointGroupOperation(
PendingOperation<ReportingEndpointGroupInfo>* op) { … }
template <typename KeyType, typename DataType>
void SQLitePersistentReportingAndNelStore::Backend::BatchOperation(
KeyType key,
std::unique_ptr<PendingOperation<DataType>> po,
QueueType<KeyType, DataType>* queue) { … }
template <typename DataType>
void SQLitePersistentReportingAndNelStore::Backend::MaybeCoalesceOperations(
PendingOperationsVector<DataType>* ops_for_key,
PendingOperation<DataType>* new_op) { … }
void SQLitePersistentReportingAndNelStore::Backend::OnOperationBatched(
size_t num_pending) { … }
void SQLitePersistentReportingAndNelStore::Backend::
LoadNelPoliciesAndNotifyInBackground(
NelPoliciesLoadedCallback loaded_callback) { … }
void SQLitePersistentReportingAndNelStore::Backend::
CompleteLoadNelPoliciesAndNotifyInForeground(
NelPoliciesLoadedCallback loaded_callback,
std::vector<NetworkErrorLoggingService::NelPolicy> loaded_policies,
bool load_success) { … }
void SQLitePersistentReportingAndNelStore::Backend::
LoadReportingClientsAndNotifyInBackground(
ReportingClientsLoadedCallback loaded_callback) { … }
void SQLitePersistentReportingAndNelStore::Backend::
CompleteLoadReportingClientsAndNotifyInForeground(
ReportingClientsLoadedCallback loaded_callback,
std::vector<ReportingEndpoint> loaded_endpoints,
std::vector<CachedReportingEndpointGroup> loaded_endpoint_groups,
bool load_success) { … }
void SQLitePersistentReportingAndNelStore::Backend::
RecordNumberOfLoadedNelPolicies(size_t count) { … }
void SQLitePersistentReportingAndNelStore::Backend::
RecordNumberOfLoadedReportingEndpoints(size_t count) { … }
void SQLitePersistentReportingAndNelStore::Backend::
RecordNumberOfLoadedReportingEndpointGroups(size_t count) { … }
SQLitePersistentReportingAndNelStore::SQLitePersistentReportingAndNelStore(
const base::FilePath& path,
const scoped_refptr<base::SequencedTaskRunner>& client_task_runner,
const scoped_refptr<base::SequencedTaskRunner>& background_task_runner)
: … { … }
SQLitePersistentReportingAndNelStore::~SQLitePersistentReportingAndNelStore() { … }
void SQLitePersistentReportingAndNelStore::LoadNelPolicies(
NelPoliciesLoadedCallback loaded_callback) { … }
void SQLitePersistentReportingAndNelStore::AddNelPolicy(
const NetworkErrorLoggingService::NelPolicy& policy) { … }
void SQLitePersistentReportingAndNelStore::UpdateNelPolicyAccessTime(
const NetworkErrorLoggingService::NelPolicy& policy) { … }
void SQLitePersistentReportingAndNelStore::DeleteNelPolicy(
const NetworkErrorLoggingService::NelPolicy& policy) { … }
void SQLitePersistentReportingAndNelStore::LoadReportingClients(
ReportingClientsLoadedCallback loaded_callback) { … }
void SQLitePersistentReportingAndNelStore::AddReportingEndpoint(
const ReportingEndpoint& endpoint) { … }
void SQLitePersistentReportingAndNelStore::AddReportingEndpointGroup(
const CachedReportingEndpointGroup& group) { … }
void SQLitePersistentReportingAndNelStore::
UpdateReportingEndpointGroupAccessTime(
const CachedReportingEndpointGroup& group) { … }
void SQLitePersistentReportingAndNelStore::UpdateReportingEndpointDetails(
const ReportingEndpoint& endpoint) { … }
void SQLitePersistentReportingAndNelStore::UpdateReportingEndpointGroupDetails(
const CachedReportingEndpointGroup& group) { … }
void SQLitePersistentReportingAndNelStore::DeleteReportingEndpoint(
const ReportingEndpoint& endpoint) { … }
void SQLitePersistentReportingAndNelStore::DeleteReportingEndpointGroup(
const CachedReportingEndpointGroup& group) { … }
void SQLitePersistentReportingAndNelStore::Flush() { … }
size_t SQLitePersistentReportingAndNelStore::GetQueueLengthForTesting() const { … }
void SQLitePersistentReportingAndNelStore::CompleteLoadNelPolicies(
NelPoliciesLoadedCallback callback,
std::vector<NetworkErrorLoggingService::NelPolicy> policies) { … }
void SQLitePersistentReportingAndNelStore::CompleteLoadReportingClients(
ReportingClientsLoadedCallback callback,
std::vector<ReportingEndpoint> endpoints,
std::vector<CachedReportingEndpointGroup> endpoint_groups) { … }
}