Skip to content

Commit

Permalink
der
Browse files Browse the repository at this point in the history
  • Loading branch information
briantting committed Apr 27, 2024
1 parent 6b68dc5 commit 017a013
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 326 deletions.
77 changes: 41 additions & 36 deletions cobalt/persistent_storage/persistent_settings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ void PersistentSettings::ValidatePersistentSettingsHelper(bool blocking) {
bool PersistentSettings::GetPersistentSettingAsBool(const std::string& key,
bool default_setting) {
// Wait for all previously posted tasks to finish.
base::task_runner_util::WaitForFence(thread_.task_runner(), FROM_HERE);
PostBlockingTask();
base::AutoLock auto_lock(pref_store_lock_);
absl::optional<bool> result = persistent_settings_.FindBool(key);
return result.value_or(default_setting);
Expand All @@ -113,7 +113,7 @@ bool PersistentSettings::GetPersistentSettingAsBool(const std::string& key,
int PersistentSettings::GetPersistentSettingAsInt(const std::string& key,
int default_setting) {
// Wait for all previously posted tasks to finish.
base::task_runner_util::WaitForFence(thread_.task_runner(), FROM_HERE);
PostBlockingTask();
base::AutoLock auto_lock(pref_store_lock_);
absl::optional<int> result = persistent_settings_.FindInt(key);
return result.value_or(default_setting);
Expand All @@ -122,7 +122,7 @@ int PersistentSettings::GetPersistentSettingAsInt(const std::string& key,
double PersistentSettings::GetPersistentSettingAsDouble(
const std::string& key, double default_setting) {
// Wait for all previously posted tasks to finish.
base::task_runner_util::WaitForFence(thread_.task_runner(), FROM_HERE);
PostBlockingTask();
base::AutoLock auto_lock(pref_store_lock_);
absl::optional<double> result = persistent_settings_.FindDouble(key);
return result.value_or(default_setting);
Expand All @@ -131,7 +131,7 @@ double PersistentSettings::GetPersistentSettingAsDouble(
std::string PersistentSettings::GetPersistentSettingAsString(
const std::string& key, const std::string& default_setting) {
// Wait for all previously posted tasks to finish.
base::task_runner_util::WaitForFence(thread_.task_runner(), FROM_HERE);
PostBlockingTask();
base::AutoLock auto_lock(pref_store_lock_);
const std::string* result = persistent_settings_.FindString(key);
if (result) return *result;
Expand All @@ -141,7 +141,7 @@ std::string PersistentSettings::GetPersistentSettingAsString(
std::vector<base::Value> PersistentSettings::GetPersistentSettingAsList(
const std::string& key) {
// Wait for all previously posted tasks to finish.
base::task_runner_util::WaitForFence(thread_.task_runner(), FROM_HERE);
PostBlockingTask();
base::AutoLock auto_lock(pref_store_lock_);
const base::Value::List* result = persistent_settings_.FindList(key);
std::vector<base::Value> values;
Expand All @@ -156,7 +156,7 @@ std::vector<base::Value> PersistentSettings::GetPersistentSettingAsList(
base::flat_map<std::string, std::unique_ptr<base::Value>>
PersistentSettings::GetPersistentSettingAsDictionary(const std::string& key) {
// Wait for all previously posted tasks to finish.
base::task_runner_util::WaitForFence(thread_.task_runner(), FROM_HERE);
PostBlockingTask();
base::AutoLock auto_lock(pref_store_lock_);
base::Value::Dict* result = persistent_settings_.FindDict(key);
base::flat_map<std::string, std::unique_ptr<base::Value>> dict;
Expand All @@ -171,54 +171,59 @@ PersistentSettings::GetPersistentSettingAsDictionary(const std::string& key) {
return dict;
}

void PersistentSettings::PostBlockingTask() {
base::WaitableEvent task_finished(
base::WaitableEvent::ResetPolicy::MANUAL,
base::WaitableEvent::InitialState::NOT_SIGNALED);
task_runner()->PostTask(
FROM_HERE, base::BindOnce(&PersistentSettings::PostBlockingTaskHelper,
base::Unretained(this), &task_finished));
task_finished.Wait();
}

void PersistentSettings::PostBlockingTaskHelper(
base::WaitableEvent* task_finished) {
DCHECK_EQ(base::SequencedTaskRunner::GetCurrentDefault(), task_runner());
task_finished->Signal();
}

void PersistentSettings::SetPersistentSetting(
const std::string& key, std::unique_ptr<base::Value> value,
base::OnceClosure closure, bool blocking) {
const std::string& key, std::unique_ptr<base::Value> value, bool blocking) {
task_runner()->PostTask(
FROM_HERE, base::BindOnce(&PersistentSettings::SetPersistentSettingHelper,
base::Unretained(this), key, std::move(value),
std::move(closure), blocking));
FROM_HERE,
base::BindOnce(&PersistentSettings::SetPersistentSettingHelper,
base::Unretained(this), key, std::move(value), blocking));
}

void PersistentSettings::SetPersistentSettingHelper(
const std::string& key, std::unique_ptr<base::Value> value,
base::OnceClosure closure, bool blocking) {
const std::string& key, std::unique_ptr<base::Value> value, bool blocking) {
DCHECK_EQ(base::SequencedTaskRunner::GetCurrentDefault(), task_runner());
{
base::AutoLock auto_lock(pref_store_lock_);
pref_store_->SetValue(key,
base::Value::FromUniquePtrValue(std::move(value)),
WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
persistent_settings_ = pref_store_->GetValues();
if (validated_initial_settings_) {
CommitPendingWrite(blocking);
}
base::AutoLock auto_lock(pref_store_lock_);
pref_store_->SetValue(key, base::Value::FromUniquePtrValue(std::move(value)),
WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
persistent_settings_ = pref_store_->GetValues();
if (validated_initial_settings_) {
CommitPendingWrite(blocking);
}
std::move(closure).Run();
}

void PersistentSettings::RemovePersistentSetting(const std::string& key,
base::OnceClosure closure,
bool blocking) {
task_runner()->PostTask(
FROM_HERE,
base::BindOnce(&PersistentSettings::RemovePersistentSettingHelper,
base::Unretained(this), key, std::move(closure),
blocking));
base::Unretained(this), key, blocking));
}

void PersistentSettings::RemovePersistentSettingHelper(
const std::string& key, base::OnceClosure closure, bool blocking) {
void PersistentSettings::RemovePersistentSettingHelper(const std::string& key,
bool blocking) {
DCHECK_EQ(base::SequencedTaskRunner::GetCurrentDefault(), task_runner());
{
base::AutoLock auto_lock(pref_store_lock_);
pref_store_->RemoveValue(key, WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
persistent_settings_ = pref_store_->GetValues();
if (validated_initial_settings_) {
CommitPendingWrite(blocking);
}
base::AutoLock auto_lock(pref_store_lock_);
pref_store_->RemoveValue(key, WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
persistent_settings_ = pref_store_->GetValues();
if (validated_initial_settings_) {
CommitPendingWrite(blocking);
}
std::move(closure).Run();
}

void PersistentSettings::DeletePersistentSettings(base::OnceClosure closure) {
Expand Down
21 changes: 10 additions & 11 deletions cobalt/persistent_storage/persistent_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,11 @@ class PersistentSettings : public base::CurrentThread::DestructionObserver {
base::flat_map<std::string, std::unique_ptr<base::Value>>
GetPersistentSettingAsDictionary(const std::string& key);

void SetPersistentSetting(
const std::string& key, std::unique_ptr<base::Value> value,
base::OnceClosure closure = std::move(base::DoNothing()),
bool blocking = false);
void SetPersistentSetting(const std::string& key,
std::unique_ptr<base::Value> value,
bool blocking = false);

void RemovePersistentSetting(
const std::string& key,
base::OnceClosure closure = std::move(base::DoNothing()),
bool blocking = false);
void RemovePersistentSetting(const std::string& key, bool blocking = false);

void DeletePersistentSettings(
base::OnceClosure closure = std::move(base::DoNothing()));
Expand All @@ -92,12 +88,15 @@ class PersistentSettings : public base::CurrentThread::DestructionObserver {

void ValidatePersistentSettingsHelper(bool blocking);

void PostBlockingTask();

void PostBlockingTaskHelper(base::WaitableEvent* task_finished);

void SetPersistentSettingHelper(const std::string& key,
std::unique_ptr<base::Value> value,
base::OnceClosure closure, bool blocking);
bool blocking);

void RemovePersistentSettingHelper(const std::string& key,
base::OnceClosure closure, bool blocking);
void RemovePersistentSettingHelper(const std::string& key, bool blocking);

void DeletePersistentSettingsHelper(base::OnceClosure closure);

Expand Down
Loading

0 comments on commit 017a013

Please sign in to comment.