#include "components/optimization_guide/core/optimization_guide_store.h"
#include <memory>
#include <optional>
#include <string>
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/logging.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros.h"
#include "base/sequence_checker.h"
#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/thread_pool.h"
#include "components/leveldb_proto/public/proto_database.h"
#include "components/leveldb_proto/public/proto_database_provider.h"
#include "components/leveldb_proto/public/shared_proto_database_client_list.h"
#include "components/optimization_guide/core/memory_hint.h"
#include "components/optimization_guide/core/model_util.h"
#include "components/optimization_guide/core/optimization_guide_features.h"
#include "components/optimization_guide/core/optimization_guide_prefs.h"
#include "components/optimization_guide/core/optimization_guide_util.h"
#include "components/optimization_guide/proto/hint_cache.pb.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/scoped_user_pref_update.h"
namespace optimization_guide {
namespace {
static_assert …;
constexpr size_t kDatabaseWriteBufferSizeBytes = …;
constexpr char kKeySectionDelimiter = …;
enum class OptimizationGuideHintCacheLevelDBStoreLoadMetadataResult { … };
class ScopedLoadMetadataResultRecorder { … };
void RecordStatusChange(OptimizationGuideStore::Status status) { … }
bool DatabasePrefixFilter(const std::string& key_prefix,
const std::string& key) { … }
bool KeySetFilter(const base::flat_set<std::string>& key_set,
const std::string& key) { … }
}
OptimizationGuideStore::OptimizationGuideStore(
leveldb_proto::ProtoDatabaseProvider* database_provider,
const base::FilePath& database_dir,
scoped_refptr<base::SequencedTaskRunner> store_task_runner,
PrefService* pref_service)
: … { … }
OptimizationGuideStore::OptimizationGuideStore(
std::unique_ptr<leveldb_proto::ProtoDatabase<proto::StoreEntry>> database,
scoped_refptr<base::SequencedTaskRunner> store_task_runner,
PrefService* pref_service)
: … { … }
OptimizationGuideStore::~OptimizationGuideStore() { … }
void OptimizationGuideStore::Initialize(bool purge_existing_data,
base::OnceClosure callback) { … }
std::unique_ptr<StoreUpdateData>
OptimizationGuideStore::MaybeCreateUpdateDataForComponentHints(
const base::Version& version) const { … }
std::unique_ptr<StoreUpdateData>
OptimizationGuideStore::CreateUpdateDataForFetchedHints(
base::Time update_time) const { … }
void OptimizationGuideStore::UpdateComponentHints(
std::unique_ptr<StoreUpdateData> component_data,
base::OnceClosure callback) { … }
void OptimizationGuideStore::UpdateFetchedHints(
std::unique_ptr<StoreUpdateData> fetched_hints_data,
base::OnceClosure callback) { … }
void OptimizationGuideStore::PurgeExpiredFetchedHints() { … }
void OptimizationGuideStore::OnLoadEntriesToPurgeExpired(
bool success,
std::unique_ptr<EntryMap> entries) { … }
void OptimizationGuideStore::RemoveFetchedHintsByKey(
base::OnceClosure on_success,
const base::flat_set<std::string>& hint_keys) { … }
void OptimizationGuideStore::OnFetchedEntriesRemoved(
base::OnceClosure on_success,
const EntryKeySet& keys,
bool success) { … }
bool OptimizationGuideStore::FindHintEntryKey(
const std::string& host,
EntryKey* out_hint_entry_key) const { … }
bool OptimizationGuideStore::FindEntryKeyForHostWithPrefix(
const std::string& host,
EntryKey* out_entry_key,
const EntryKeyPrefix& entry_key_prefix) const { … }
void OptimizationGuideStore::LoadHint(const EntryKey& hint_entry_key,
HintLoadedCallback callback) { … }
base::Time OptimizationGuideStore::GetFetchedHintsUpdateTime() const { … }
const char OptimizationGuideStore::kStoreSchemaVersion[] = …;
OptimizationGuideStore::EntryKeyPrefix
OptimizationGuideStore::GetMetadataEntryKeyPrefix() { … }
OptimizationGuideStore::EntryKey
OptimizationGuideStore::GetMetadataTypeEntryKey(MetadataType metadata_type) { … }
OptimizationGuideStore::EntryKeyPrefix
OptimizationGuideStore::GetComponentHintEntryKeyPrefixWithoutVersion() { … }
OptimizationGuideStore::EntryKeyPrefix
OptimizationGuideStore::GetComponentHintEntryKeyPrefix(
const base::Version& component_version) { … }
OptimizationGuideStore::EntryKeyPrefix
OptimizationGuideStore::GetFetchedHintEntryKeyPrefix() { … }
void OptimizationGuideStore::UpdateStatus(Status new_status) { … }
bool OptimizationGuideStore::IsAvailable() const { … }
void OptimizationGuideStore::PurgeDatabase(base::OnceClosure callback) { … }
void OptimizationGuideStore::SetComponentVersion(
const base::Version& component_version) { … }
void OptimizationGuideStore::ClearComponentVersion() { … }
void OptimizationGuideStore::ClearFetchedHintsFromDatabase() { … }
void OptimizationGuideStore::MaybeLoadEntryKeys(base::OnceClosure callback) { … }
size_t OptimizationGuideStore::GetEntryKeyCount() const { … }
void OptimizationGuideStore::OnDatabaseInitialized(
bool purge_existing_data,
base::OnceClosure callback,
leveldb_proto::Enums::InitStatus status) { … }
void OptimizationGuideStore::OnDatabaseDestroyed(bool ) { … }
void OptimizationGuideStore::OnLoadMetadata(
base::OnceClosure callback,
bool success,
std::unique_ptr<EntryMap> metadata_entries) { … }
void OptimizationGuideStore::OnPurgeDatabase(base::OnceClosure callback,
bool success) { … }
void OptimizationGuideStore::OnUpdateStore(base::OnceClosure callback,
bool success) { … }
void OptimizationGuideStore::OnLoadEntryKeys(
std::unique_ptr<EntryKeySet> hint_entry_keys,
base::OnceClosure callback,
bool success,
std::unique_ptr<EntryMap> ) { … }
void OptimizationGuideStore::OnLoadHint(
const std::string& entry_key,
HintLoadedCallback callback,
bool success,
std::unique_ptr<proto::StoreEntry> entry) { … }
void OptimizationGuideStore::CleanUpFilePaths() { … }
void OptimizationGuideStore::OnFilePathDeleted(
const std::string& file_path_to_clean_up,
bool success) { … }
}