Skip to content

Commit

Permalink
Simplify PersistentSettings.
Browse files Browse the repository at this point in the history
Building off the work of @briantting:
youtube#3095

b/305057554
  • Loading branch information
aee-google committed May 17, 2024
1 parent dc9bd39 commit 61ae23e
Show file tree
Hide file tree
Showing 14 changed files with 353 additions and 677 deletions.
12 changes: 8 additions & 4 deletions cobalt/browser/application.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1513,10 +1513,14 @@ void Application::InitMetrics() {
metrics::CobaltMetricsServicesManager::GetInstance();
// Before initializing metrics manager, set any persisted settings like if
// it's enabled or upload interval.
bool is_metrics_enabled = persistent_settings_->GetPersistentSettingAsBool(
metrics::kMetricEnabledSettingName, false);
auto metric_event_interval = persistent_settings_->GetPersistentSettingAsInt(
metrics::kMetricEventIntervalSettingName, 300);
bool is_metrics_enabled =
persistent_settings_->GetValue(metrics::kMetricEnabledSettingName)
.GetIfBool()
.value_or(false);
auto metric_event_interval =
persistent_settings_->GetValue(metrics::kMetricEventIntervalSettingName)
.GetIfInt()
.value_or(300);
metrics_services_manager_->SetEventDispatcher(&event_dispatcher_);
metrics_services_manager_->SetUploadInterval(metric_event_interval);
metrics_services_manager_->ToggleMetricsEnabled(is_metrics_enabled);
Expand Down
2 changes: 1 addition & 1 deletion cobalt/browser/browser_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,7 @@ void BrowserModule::OnLoad() {

web_module_loaded_.Signal();

options_.persistent_settings->ValidatePersistentSettings();
options_.persistent_settings->Validate();
ValidateCacheBackendSettings();
}

Expand Down
10 changes: 4 additions & 6 deletions cobalt/h5vcc/h5vcc_metrics.cc
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,8 @@ script::HandlePromiseVoid H5vccMetrics::Disable(

void H5vccMetrics::ToggleMetricsEnabled(bool is_enabled,
base::OnceClosure done_callback) {
persistent_settings_->SetPersistentSetting(
browser::metrics::kMetricEnabledSettingName,
std::make_unique<base::Value>(is_enabled));
persistent_settings_->Set(browser::metrics::kMetricEnabledSettingName,
base::Value(is_enabled));
browser::metrics::CobaltMetricsServicesManager::GetInstance()
->ToggleMetricsEnabled(is_enabled, std::move(done_callback));
}
Expand All @@ -132,9 +131,8 @@ bool H5vccMetrics::IsEnabled() {
}

void H5vccMetrics::SetMetricEventInterval(uint32_t interval_seconds) {
persistent_settings_->SetPersistentSetting(
browser::metrics::kMetricEventIntervalSettingName,
std::make_unique<base::Value>(static_cast<int>(interval_seconds)));
persistent_settings_->Set(browser::metrics::kMetricEventIntervalSettingName,
base::Value(static_cast<int>(interval_seconds)));
browser::metrics::CobaltMetricsServicesManager::GetInstance()
->SetUploadInterval(interval_seconds);
}
Expand Down
12 changes: 5 additions & 7 deletions cobalt/h5vcc/h5vcc_settings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,8 @@ bool H5vccSettings::Set(const std::string& name, SetValueType value) const {
if (!persistent_settings_) {
return false;
} else {
persistent_settings_->SetPersistentSetting(
network::kQuicEnabledPersistentSettingsKey,
std::make_unique<base::Value>(value.AsType<int32>() != 0));
persistent_settings_->Set(network::kQuicEnabledPersistentSettingsKey,
base::Value(value.AsType<int32>() != 0));
// Tell NetworkModule (if exists) to re-query persistent settings.
if (network_module_) {
network_module_->SetEnableQuicFromPersistentSettings();
Expand All @@ -107,16 +106,15 @@ bool H5vccSettings::Set(const std::string& name, SetValueType value) const {
void H5vccSettings::SetPersistentSettingAsInt(const std::string& key,
int value) const {
if (persistent_settings_) {
persistent_settings_->SetPersistentSetting(
key, std::make_unique<base::Value>(value));
persistent_settings_->Set(key, base::Value(value));
}
}

int H5vccSettings::GetPersistentSettingAsInt(const std::string& key,
int default_setting) const {
if (persistent_settings_) {
return persistent_settings_->GetPersistentSettingAsInt(key,
default_setting);
return persistent_settings_->GetValue(key).GetIfInt().value_or(
default_setting);
}
return default_setting;
}
Expand Down
8 changes: 4 additions & 4 deletions cobalt/h5vcc/h5vcc_storage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -372,9 +372,9 @@ H5vccStorageResourceTypeQuotaBytesDictionary H5vccStorage::GetQuota() {
}

void H5vccStorage::EnableCache() {
persistent_settings_->SetPersistentSetting(
persistent_settings_->Set(
network::disk_cache::kCacheEnabledPersistentSettingsKey,
std::make_unique<base::Value>(true));
base::Value(true));

network::disk_cache::settings::SetCacheEnabled(true);

Expand All @@ -384,9 +384,9 @@ void H5vccStorage::EnableCache() {
}

void H5vccStorage::DisableCache() {
persistent_settings_->SetPersistentSetting(
persistent_settings_->Set(
network::disk_cache::kCacheEnabledPersistentSettingsKey,
std::make_unique<base::Value>(false));
base::Value(false));

network::disk_cache::settings::SetCacheEnabled(false);

Expand Down
6 changes: 4 additions & 2 deletions cobalt/network/network_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,10 @@ void NetworkModule::SetProxy(const std::string& custom_proxy_rules) {
void NetworkModule::SetEnableQuicFromPersistentSettings() {
// Called on initialization and when the persistent setting is changed.
if (options_.persistent_settings != nullptr) {
bool enable_quic = options_.persistent_settings->GetPersistentSettingAsBool(
kQuicEnabledPersistentSettingsKey, false);
bool enable_quic = options_.persistent_settings
->GetValue(kQuicEnabledPersistentSettingsKey)
.GetIfBool()
.value_or(false);
task_runner()->PostTask(
FROM_HERE,
base::Bind(&URLRequestContext::SetEnableQuic,
Expand Down
24 changes: 12 additions & 12 deletions cobalt/network/url_request_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ void LoadDiskCacheQuotaSettings(
disk_cache::ResourceType resource_type = (disk_cache::ResourceType)i;
std::string directory =
disk_cache::defaults::GetSubdirectory(resource_type);
uint32_t bucket_size =
static_cast<uint32_t>(settings->GetPersistentSettingAsDouble(
directory, disk_cache::defaults::GetQuota(resource_type)));
uint32_t bucket_size = static_cast<uint32_t>(
settings->GetValue(directory).GetIfDouble().value_or(
disk_cache::defaults::GetQuota(resource_type)));
quotas[resource_type] = bucket_size;
total_size += bucket_size;
}
Expand All @@ -93,9 +93,7 @@ void LoadDiskCacheQuotaSettings(
disk_cache::settings::SetQuota(resource_type, default_quota);
std::string directory =
disk_cache::defaults::GetSubdirectory(resource_type);
settings->SetPersistentSetting(
directory,
std::make_unique<base::Value>(static_cast<double>(default_quota)));
settings->Set(directory, base::Value(static_cast<double>(default_quota)));
}
}

Expand Down Expand Up @@ -290,8 +288,11 @@ URLRequestContext::URLRequestContext(
http_cache->set_can_disable_by_mime_type(true);
if (persistent_settings != nullptr) {
auto cache_enabled =
persistent_settings->GetPersistentSettingAsBool(
disk_cache::kCacheEnabledPersistentSettingsKey, true);
persistent_settings
->GetValue(
disk_cache::kCacheEnabledPersistentSettingsKey)
.GetIfBool()
.value_or(true);
disk_cache::settings::SetCacheEnabled(cache_enabled);
if (!cache_enabled) {
http_cache->set_mode(net::HttpCache::Mode::DISABLE);
Expand Down Expand Up @@ -344,13 +345,12 @@ void URLRequestContext::OnQuicToggle(const std::string& message) {
void URLRequestContext::UpdateCacheSizeSetting(disk_cache::ResourceType type,
uint32_t bytes) {
CHECK(cache_persistent_settings_);
cache_persistent_settings_->SetPersistentSetting(
disk_cache::defaults::GetSubdirectory(type),
std::make_unique<base::Value>(static_cast<double>(bytes)));
cache_persistent_settings_->Set(disk_cache::defaults::GetSubdirectory(type),
base::Value(static_cast<double>(bytes)));
}

void URLRequestContext::ValidateCachePersistentSettings() {
cache_persistent_settings_->ValidatePersistentSettings();
cache_persistent_settings_->Validate();
}

void URLRequestContext::AssociateKeyWithResourceType(
Expand Down
Loading

0 comments on commit 61ae23e

Please sign in to comment.