#ifdef UNSAFE_BUFFERS_BUILD
#pragma allow_unsafe_buffers
#endif
#include "net/extras/sqlite/sqlite_persistent_shared_dictionary_store.h"
#include "base/containers/span.h"
#include "base/debug/dump_without_crashing.h"
#include "base/files/file_path.h"
#include "base/metrics/histogram_functions.h"
#include "base/pickle.h"
#include "base/strings/strcat.h"
#include "base/task/sequenced_task_runner.h"
#include "base/types/expected_macros.h"
#include "net/base/network_isolation_key.h"
#include "net/extras/sqlite/sqlite_persistent_store_backend_base.h"
#include "net/shared_dictionary/shared_dictionary_isolation_key.h"
#include "sql/database.h"
#include "sql/statement.h"
#include "sql/transaction.h"
namespace net {
namespace {
constexpr char kHistogramTag[] = …;
constexpr char kHistogramPrefix[] = …;
constexpr char kTableName[] = …;
constexpr char kTotalDictSizeKey[] = …;
const int kCurrentVersionNumber = …;
const int kCompatibleVersionNumber = …;
bool CreateV3Schema(sql::Database* db, sql::MetaTable* meta_table) { … }
std::optional<SHA256HashValue> ToSHA256HashValue(
base::span<const uint8_t> sha256_bytes) { … }
std::optional<base::UnguessableToken> ToUnguessableToken(int64_t token_high,
int64_t token_low) { … }
template <typename ResultType>
base::OnceCallback<void(ResultType)> WrapCallbackWithWeakPtrCheck(
base::WeakPtr<SQLitePersistentSharedDictionaryStore> weak_ptr,
base::OnceCallback<void(ResultType)> callback) { … }
void RecordErrorHistogram(const char* method_name,
SQLitePersistentSharedDictionaryStore::Error error) { … }
template <typename ResultType>
void RecordErrorHistogram(
const char* method_name,
base::expected<ResultType, SQLitePersistentSharedDictionaryStore::Error>
result) { … }
}
SQLitePersistentSharedDictionaryStore::RegisterDictionaryResult::
RegisterDictionaryResult(
int64_t primary_key_in_database,
std::optional<base::UnguessableToken> replaced_disk_cache_key_token,
std::set<base::UnguessableToken> evicted_disk_cache_key_tokens,
uint64_t total_dictionary_size,
uint64_t total_dictionary_count)
: … { … }
SQLitePersistentSharedDictionaryStore::RegisterDictionaryResult::
~RegisterDictionaryResult() = default;
SQLitePersistentSharedDictionaryStore::RegisterDictionaryResult::
RegisterDictionaryResult(const RegisterDictionaryResult& other) = default;
SQLitePersistentSharedDictionaryStore::RegisterDictionaryResult::
RegisterDictionaryResult(RegisterDictionaryResult&& other) = default;
SQLitePersistentSharedDictionaryStore::RegisterDictionaryResult&
SQLitePersistentSharedDictionaryStore::RegisterDictionaryResult::operator=(
const RegisterDictionaryResult& other) = default;
SQLitePersistentSharedDictionaryStore::RegisterDictionaryResult&
SQLitePersistentSharedDictionaryStore::RegisterDictionaryResult::operator=(
RegisterDictionaryResult&& other) = default;
class SQLitePersistentSharedDictionaryStore::Backend
: public SQLitePersistentStoreBackendBase { … };
bool SQLitePersistentSharedDictionaryStore::Backend::CreateDatabaseSchema() { … }
std::optional<int>
SQLitePersistentSharedDictionaryStore::Backend::DoMigrateDatabaseSchema() { … }
void SQLitePersistentSharedDictionaryStore::Backend::DoCommit() { … }
SQLitePersistentSharedDictionaryStore::Error
SQLitePersistentSharedDictionaryStore::Backend::
CommitDictionaryLastUsedTimeUpdate(int64_t primary_key_in_database,
base::Time last_used_time) { … }
base::expected<uint64_t, SQLitePersistentSharedDictionaryStore::Error>
SQLitePersistentSharedDictionaryStore::Backend::GetTotalDictionarySizeImpl() { … }
SQLitePersistentSharedDictionaryStore::RegisterDictionaryResultOrError
SQLitePersistentSharedDictionaryStore::Backend::RegisterDictionaryImpl(
const SharedDictionaryIsolationKey& isolation_key,
const SharedDictionaryInfo& dictionary_info,
uint64_t max_size_per_site,
uint64_t max_count_per_site) { … }
SQLitePersistentSharedDictionaryStore::Error
SQLitePersistentSharedDictionaryStore::Backend::
MaybeEvictDictionariesForPerSiteLimit(
const SchemefulSite& top_frame_site,
uint64_t max_size_per_site,
uint64_t max_count_per_site,
std::vector<base::UnguessableToken>* evicted_disk_cache_key_tokens,
uint64_t* total_dictionary_size_out) { … }
SQLitePersistentSharedDictionaryStore::Error
SQLitePersistentSharedDictionaryStore::Backend::
SelectCandidatesForPerSiteEviction(
const SchemefulSite& top_frame_site,
uint64_t max_size_per_site,
uint64_t max_count_per_site,
std::vector<int64_t>* primary_keys_out,
std::vector<base::UnguessableToken>* tokens_out,
int64_t* total_size_of_candidates_out) { … }
base::expected<uint64_t, SQLitePersistentSharedDictionaryStore::Error>
SQLitePersistentSharedDictionaryStore::Backend::GetDictionaryCountPerSite(
const SchemefulSite& top_frame_site) { … }
base::expected<uint64_t, SQLitePersistentSharedDictionaryStore::Error>
SQLitePersistentSharedDictionaryStore::Backend::GetDictionarySizePerSite(
const SchemefulSite& top_frame_site) { … }
SQLitePersistentSharedDictionaryStore::DictionaryListOrError
SQLitePersistentSharedDictionaryStore::Backend::GetDictionariesImpl(
const SharedDictionaryIsolationKey& isolation_key) { … }
SQLitePersistentSharedDictionaryStore::DictionaryMapOrError
SQLitePersistentSharedDictionaryStore::Backend::GetAllDictionariesImpl() { … }
SQLitePersistentSharedDictionaryStore::UsageInfoOrError
SQLitePersistentSharedDictionaryStore::Backend::GetUsageInfoImpl() { … }
SQLitePersistentSharedDictionaryStore::OriginListOrError
SQLitePersistentSharedDictionaryStore::Backend::GetOriginsBetweenImpl(
const base::Time start_time,
const base::Time end_time) { … }
SQLitePersistentSharedDictionaryStore::UnguessableTokenSetOrError
SQLitePersistentSharedDictionaryStore::Backend::ClearAllDictionariesImpl() { … }
SQLitePersistentSharedDictionaryStore::UnguessableTokenSetOrError
SQLitePersistentSharedDictionaryStore::Backend::ClearDictionariesImpl(
base::Time start_time,
base::Time end_time,
base::RepeatingCallback<bool(const GURL&)> url_matcher) { … }
SQLitePersistentSharedDictionaryStore::Error
SQLitePersistentSharedDictionaryStore::Backend::SelectMatchingDictionaries(
base::Time start_time,
base::Time end_time,
std::vector<int64_t>* primary_keys_out,
std::vector<base::UnguessableToken>* tokens_out,
int64_t* total_size_out) { … }
SQLitePersistentSharedDictionaryStore::Error
SQLitePersistentSharedDictionaryStore::Backend::
SelectMatchingDictionariesWithUrlMatcher(
base::Time start_time,
base::Time end_time,
base::RepeatingCallback<bool(const GURL&)> url_matcher,
std::vector<int64_t>* primary_keys_out,
std::vector<base::UnguessableToken>* tokens_out,
int64_t* total_size_out) { … }
SQLitePersistentSharedDictionaryStore::UnguessableTokenSetOrError
SQLitePersistentSharedDictionaryStore::Backend::
ClearDictionariesForIsolationKeyImpl(
const SharedDictionaryIsolationKey& isolation_key) { … }
SQLitePersistentSharedDictionaryStore::UnguessableTokenSetOrError
SQLitePersistentSharedDictionaryStore::Backend::DeleteExpiredDictionariesImpl(
base::Time now) { … }
SQLitePersistentSharedDictionaryStore::UnguessableTokenSetOrError
SQLitePersistentSharedDictionaryStore::Backend::ProcessEvictionImpl(
uint64_t cache_max_size,
uint64_t size_low_watermark,
uint64_t cache_max_count,
uint64_t count_low_watermark) { … }
SQLitePersistentSharedDictionaryStore::Error
SQLitePersistentSharedDictionaryStore::Backend::SelectEvictionCandidates(
uint64_t cache_max_size,
uint64_t size_low_watermark,
uint64_t cache_max_count,
uint64_t count_low_watermark,
std::vector<int64_t>* primary_keys_out,
std::vector<base::UnguessableToken>* tokens_out,
int64_t* total_size_after_eviction_out) { … }
SQLitePersistentSharedDictionaryStore::Error
SQLitePersistentSharedDictionaryStore::Backend::DeleteDictionaryByPrimaryKey(
int64_t primary_key) { … }
SQLitePersistentSharedDictionaryStore::Error
SQLitePersistentSharedDictionaryStore::Backend::
DeleteDictionariesByDiskCacheKeyTokensImpl(
const std::set<base::UnguessableToken>& disk_cache_key_tokens) { … }
SQLitePersistentSharedDictionaryStore::Error
SQLitePersistentSharedDictionaryStore::Backend::
UpdateDictionaryLastFetchTimeImpl(int64_t primary_key_in_database,
base::Time last_fetch_time) { … }
base::expected<uint64_t, SQLitePersistentSharedDictionaryStore::Error>
SQLitePersistentSharedDictionaryStore::Backend::
DeleteDictionaryByDiskCacheToken(
const base::UnguessableToken& disk_cache_key_token) { … }
SQLitePersistentSharedDictionaryStore::UnguessableTokenSetOrError
SQLitePersistentSharedDictionaryStore::Backend::GetAllDiskCacheKeyTokensImpl() { … }
void SQLitePersistentSharedDictionaryStore::Backend::
UpdateDictionaryLastUsedTime(int64_t primary_key_in_database,
base::Time last_used_time) { … }
base::expected<uint64_t, SQLitePersistentSharedDictionaryStore::Error>
SQLitePersistentSharedDictionaryStore::Backend::GetTotalDictionaryCount() { … }
bool SQLitePersistentSharedDictionaryStore::Backend::
GetExistingDictionarySizeAndDiskCacheKeyToken(
const SharedDictionaryIsolationKey& isolation_key,
const url::SchemeHostPort& host,
const std::string& match,
const std::string& match_dest,
int64_t* size_out,
std::optional<base::UnguessableToken>* disk_cache_key_out) { … }
SQLitePersistentSharedDictionaryStore::Error
SQLitePersistentSharedDictionaryStore::Backend::
UpdateTotalDictionarySizeInMetaTable(int64_t size_delta,
uint64_t* total_dictionary_size_out) { … }
SQLitePersistentSharedDictionaryStore::SQLitePersistentSharedDictionaryStore(
const base::FilePath& path,
const scoped_refptr<base::SequencedTaskRunner>& client_task_runner,
const scoped_refptr<base::SequencedTaskRunner>& background_task_runner)
: … { … }
SQLitePersistentSharedDictionaryStore::
~SQLitePersistentSharedDictionaryStore() { … }
void SQLitePersistentSharedDictionaryStore::GetTotalDictionarySize(
base::OnceCallback<void(SizeOrError)> callback) { … }
void SQLitePersistentSharedDictionaryStore::RegisterDictionary(
const SharedDictionaryIsolationKey& isolation_key,
SharedDictionaryInfo dictionary_info,
const uint64_t max_size_per_site,
const uint64_t max_count_per_site,
base::OnceCallback<void(RegisterDictionaryResultOrError)> callback) { … }
void SQLitePersistentSharedDictionaryStore::GetDictionaries(
const SharedDictionaryIsolationKey& isolation_key,
base::OnceCallback<void(DictionaryListOrError)> callback) { … }
void SQLitePersistentSharedDictionaryStore::GetAllDictionaries(
base::OnceCallback<void(DictionaryMapOrError)> callback) { … }
void SQLitePersistentSharedDictionaryStore::GetUsageInfo(
base::OnceCallback<void(UsageInfoOrError)> callback) { … }
void SQLitePersistentSharedDictionaryStore::GetOriginsBetween(
const base::Time start_time,
const base::Time end_time,
base::OnceCallback<void(OriginListOrError)> callback) { … }
void SQLitePersistentSharedDictionaryStore::ClearAllDictionaries(
base::OnceCallback<void(UnguessableTokenSetOrError)> callback) { … }
void SQLitePersistentSharedDictionaryStore::ClearDictionaries(
const base::Time start_time,
const base::Time end_time,
base::RepeatingCallback<bool(const GURL&)> url_matcher,
base::OnceCallback<void(UnguessableTokenSetOrError)> callback) { … }
void SQLitePersistentSharedDictionaryStore::ClearDictionariesForIsolationKey(
const SharedDictionaryIsolationKey& isolation_key,
base::OnceCallback<void(UnguessableTokenSetOrError)> callback) { … }
void SQLitePersistentSharedDictionaryStore::DeleteExpiredDictionaries(
const base::Time now,
base::OnceCallback<void(UnguessableTokenSetOrError)> callback) { … }
void SQLitePersistentSharedDictionaryStore::ProcessEviction(
const uint64_t cache_max_size,
const uint64_t size_low_watermark,
const uint64_t cache_max_count,
const uint64_t count_low_watermark,
base::OnceCallback<void(UnguessableTokenSetOrError)> callback) { … }
void SQLitePersistentSharedDictionaryStore::GetAllDiskCacheKeyTokens(
base::OnceCallback<void(UnguessableTokenSetOrError)> callback) { … }
void SQLitePersistentSharedDictionaryStore::
DeleteDictionariesByDiskCacheKeyTokens(
std::set<base::UnguessableToken> disk_cache_key_tokens,
base::OnceCallback<void(Error)> callback) { … }
void SQLitePersistentSharedDictionaryStore::UpdateDictionaryLastFetchTime(
const int64_t primary_key_in_database,
const base::Time last_fetch_time,
base::OnceCallback<void(Error)> callback) { … }
void SQLitePersistentSharedDictionaryStore::UpdateDictionaryLastUsedTime(
int64_t primary_key_in_database,
base::Time last_used_time) { … }
base::WeakPtr<SQLitePersistentSharedDictionaryStore>
SQLitePersistentSharedDictionaryStore::GetWeakPtr() { … }
}