Commit 86e93517 authored by Junbo Ke's avatar Junbo Ke Committed by Chromium LUCI CQ

[Chromecast] Get rid of base::DictionaryValue and base::ListValue in cast_features.

Bug: internal b/156555613
Test: cast_base_unittests
Change-Id: I5087a0a2df39559cf510ff5f9712ac9ece6a5b7b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2605591Reviewed-by: default avatarLuke Halliwell (slow) <halliwell@chromium.org>
Commit-Queue: Junbo Ke <juke@chromium.org>
Cr-Commit-Position: refs/heads/master@{#839600}
parent 38f4aaf3
...@@ -40,13 +40,13 @@ std::vector<const base::Feature*>& GetTestFeatures() { ...@@ -40,13 +40,13 @@ std::vector<const base::Feature*>& GetTestFeatures() {
return *features_for_test; return *features_for_test;
} }
void SetExperimentIds(const base::ListValue& list) { void SetExperimentIds(const base::Value& list) {
DCHECK(!g_experiment_ids_initialized); DCHECK(!g_experiment_ids_initialized);
DCHECK(list.is_list());
std::unordered_set<int32_t> ids; std::unordered_set<int32_t> ids;
for (size_t i = 0; i < list.GetSize(); ++i) { for (const auto& it : list.GetList()) {
int32_t id; if (it.is_int()) {
if (list.GetInteger(i, &id)) { ids.insert(it.GetInt());
ids.insert(id);
} else { } else {
LOG(ERROR) << "Non-integer value found in experiment id list!"; LOG(ERROR) << "Non-integer value found in experiment id list!";
} }
...@@ -172,9 +172,6 @@ const base::Feature* kFeatures[] = { ...@@ -172,9 +172,6 @@ const base::Feature* kFeatures[] = {
&kEnableChromeAudioManagerAndroid, &kEnableChromeAudioManagerAndroid,
}; };
// An iterator for a base::DictionaryValue. Use an alias for brevity in loops.
using Iterator = base::DictionaryValue::Iterator;
std::vector<const base::Feature*> GetInternalFeatures(); std::vector<const base::Feature*> GetInternalFeatures();
const std::vector<const base::Feature*>& GetFeatures() { const std::vector<const base::Feature*>& GetFeatures() {
...@@ -192,13 +189,15 @@ const std::vector<const base::Feature*>& GetFeatures() { ...@@ -192,13 +189,15 @@ const std::vector<const base::Feature*>& GetFeatures() {
return *features; return *features;
} }
void InitializeFeatureList(const base::DictionaryValue& dcs_features, void InitializeFeatureList(const base::Value& dcs_features,
const base::ListValue& dcs_experiment_ids, const base::Value& dcs_experiment_ids,
const std::string& cmd_line_enable_features, const std::string& cmd_line_enable_features,
const std::string& cmd_line_disable_features, const std::string& cmd_line_disable_features,
const std::string& extra_enable_features, const std::string& extra_enable_features,
const std::string& extra_disable_features) { const std::string& extra_disable_features) {
DCHECK(!base::FeatureList::GetInstance()); DCHECK(!base::FeatureList::GetInstance());
DCHECK(dcs_features.is_dict());
DCHECK(dcs_experiment_ids.is_list());
// Set the experiments. // Set the experiments.
SetExperimentIds(dcs_experiment_ids); SetExperimentIds(dcs_experiment_ids);
...@@ -214,7 +213,7 @@ void InitializeFeatureList(const base::DictionaryValue& dcs_features, ...@@ -214,7 +213,7 @@ void InitializeFeatureList(const base::DictionaryValue& dcs_features,
all_disable_features); all_disable_features);
// Override defaults from the DCS config. // Override defaults from the DCS config.
for (Iterator it(dcs_features); !it.IsAtEnd(); it.Advance()) { for (const auto& kv : dcs_features.DictItems()) {
// Each feature must have its own FieldTrial object. Since experiments are // Each feature must have its own FieldTrial object. Since experiments are
// controlled server-side for Chromecast, and this class is designed with a // controlled server-side for Chromecast, and this class is designed with a
// client-side experimentation framework in mind, these parameters are // client-side experimentation framework in mind, these parameters are
...@@ -232,24 +231,22 @@ void InitializeFeatureList(const base::DictionaryValue& dcs_features, ...@@ -232,24 +231,22 @@ void InitializeFeatureList(const base::DictionaryValue& dcs_features,
// entropy_provider. However, this value doesn't matter. // entropy_provider. However, this value doesn't matter.
// - We don't care about the group_id. // - We don't care about the group_id.
// //
const std::string& feature_name = it.key(); const std::string& feature_name = kv.first;
auto* field_trial = base::FieldTrialList::FactoryGetFieldTrial( auto* field_trial = base::FieldTrialList::FactoryGetFieldTrial(
feature_name, k100PercentProbability, kDefaultDCSFeaturesGroup, feature_name, k100PercentProbability, kDefaultDCSFeaturesGroup,
base::FieldTrial::SESSION_RANDOMIZED, nullptr); base::FieldTrial::SESSION_RANDOMIZED, nullptr);
bool enabled; if (kv.second.is_bool()) {
if (it.value().GetAsBoolean(&enabled)) {
// A boolean entry simply either enables or disables a feature. // A boolean entry simply either enables or disables a feature.
feature_list->RegisterFieldTrialOverride( feature_list->RegisterFieldTrialOverride(
feature_name, feature_name,
enabled ? base::FeatureList::OVERRIDE_ENABLE_FEATURE kv.second.GetBool() ? base::FeatureList::OVERRIDE_ENABLE_FEATURE
: base::FeatureList::OVERRIDE_DISABLE_FEATURE, : base::FeatureList::OVERRIDE_DISABLE_FEATURE,
field_trial); field_trial);
continue; continue;
} }
const base::DictionaryValue* params_dict; if (kv.second.is_dict()) {
if (it.value().GetAsDictionary(&params_dict)) {
// A dictionary entry implies that the feature is enabled. // A dictionary entry implies that the feature is enabled.
feature_list->RegisterFieldTrialOverride( feature_list->RegisterFieldTrialOverride(
feature_name, base::FeatureList::OVERRIDE_ENABLE_FEATURE, feature_name, base::FeatureList::OVERRIDE_ENABLE_FEATURE,
...@@ -262,10 +259,9 @@ void InitializeFeatureList(const base::DictionaryValue& dcs_features, ...@@ -262,10 +259,9 @@ void InitializeFeatureList(const base::DictionaryValue& dcs_features,
// Build a map of the FieldTrial parameters and associate it to the // Build a map of the FieldTrial parameters and associate it to the
// FieldTrial. // FieldTrial.
base::FieldTrialParams params; base::FieldTrialParams params;
for (Iterator p(*params_dict); !p.IsAtEnd(); p.Advance()) { for (const auto& params_kv : kv.second.DictItems()) {
std::string val; if (params_kv.second.is_string()) {
if (p.value().GetAsString(&val)) { params[params_kv.first] = params_kv.second.GetString();
params[p.key()] = val;
} else { } else {
LOG(ERROR) << "Entry in params dict for \"" << feature_name << "\"" LOG(ERROR) << "Entry in params dict for \"" << feature_name << "\""
<< " feature is not a string. Skipping."; << " feature is not a string. Skipping.";
...@@ -282,7 +278,7 @@ void InitializeFeatureList(const base::DictionaryValue& dcs_features, ...@@ -282,7 +278,7 @@ void InitializeFeatureList(const base::DictionaryValue& dcs_features,
// Other base::Value types are not supported. // Other base::Value types are not supported.
LOG(ERROR) << "A DCS feature mapped to an unsupported value. key: " LOG(ERROR) << "A DCS feature mapped to an unsupported value. key: "
<< feature_name << " type: " << it.value().type(); << feature_name << " type: " << kv.second.type();
} }
base::FeatureList::SetInstance(std::move(feature_list)); base::FeatureList::SetInstance(std::move(feature_list));
...@@ -293,43 +289,41 @@ bool IsFeatureEnabled(const base::Feature& feature) { ...@@ -293,43 +289,41 @@ bool IsFeatureEnabled(const base::Feature& feature) {
return base::FeatureList::IsEnabled(feature); return base::FeatureList::IsEnabled(feature);
} }
base::DictionaryValue GetOverriddenFeaturesForStorage( base::Value GetOverriddenFeaturesForStorage(const base::Value& features) {
const base::Value& features) { base::Value persistent_dict(base::Value::Type::DICTIONARY);
base::DictionaryValue persistent_dict;
// |features| maps feature names to either a boolean or a dict of params. // |features| maps feature names to either a boolean or a dict of params.
for (const auto& feature : features.DictItems()) { for (const auto& feature : features.DictItems()) {
if (feature.second.is_bool()) { if (feature.second.is_bool()) {
persistent_dict.SetBoolean(feature.first, feature.second.GetBool()); persistent_dict.SetBoolKey(feature.first, feature.second.GetBool());
continue; continue;
} }
const base::DictionaryValue* params_dict; if (feature.second.is_dict()) {
if (feature.second.GetAsDictionary(&params_dict)) { const base::Value* params_dict = &feature.second;
auto params = std::make_unique<base::DictionaryValue>(); base::Value params(base::Value::Type::DICTIONARY);
bool bval; for (const auto params_kv : params_dict->DictItems()) {
int ival; const auto& param_key = params_kv.first;
double dval; const auto& param_val = params_kv.second;
std::string sval; if (param_val.is_bool()) {
for (Iterator p(*params_dict); !p.IsAtEnd(); p.Advance()) { params.SetStringKey(param_key,
const auto& param_key = p.key(); param_val.GetBool() ? "true" : "false");
const auto& param_val = p.value(); } else if (param_val.is_int()) {
if (param_val.GetAsBoolean(&bval)) { params.SetStringKey(param_key,
params->SetString(param_key, bval ? "true" : "false"); base::NumberToString(param_val.GetInt()));
} else if (param_val.GetAsInteger(&ival)) { } else if (param_val.is_double()) {
params->SetString(param_key, base::NumberToString(ival)); params.SetStringKey(param_key,
} else if (param_val.GetAsDouble(&dval)) { base::NumberToString(param_val.GetDouble()));
params->SetString(param_key, base::NumberToString(dval)); } else if (param_val.is_string()) {
} else if (param_val.GetAsString(&sval)) { params.SetStringKey(param_key, param_val.GetString());
params->SetString(param_key, sval);
} else { } else {
LOG(ERROR) << "Entry in params dict for \"" << feature.first << "\"" LOG(ERROR) << "Entry in params dict for \"" << feature.first << "\""
<< " is not of a supported type (key: " << param_key << " is not of a supported type (key: " << param_key
<< ", type: " << param_val.type(); << ", type: " << param_val.type();
} }
} }
persistent_dict.Set(feature.first, std::move(params)); persistent_dict.SetPath(feature.first, std::move(params));
continue; continue;
} }
......
...@@ -15,8 +15,6 @@ ...@@ -15,8 +15,6 @@
#include "base/macros.h" #include "base/macros.h"
namespace base { namespace base {
class DictionaryValue;
class ListValue;
class Value; class Value;
} // namespace base } // namespace base
...@@ -49,8 +47,10 @@ const std::vector<const base::Feature*>& GetFeatures(); ...@@ -49,8 +47,10 @@ const std::vector<const base::Feature*>& GetFeatures();
// //
// This function should be called before the browser's main loop. After this is // This function should be called before the browser's main loop. After this is
// called, the other functions in this file may be called on any thread. // called, the other functions in this file may be called on any thread.
void InitializeFeatureList(const base::DictionaryValue& dcs_features, // TODO(juke): Keep type info of params by passing in base::flat_map and
const base::ListValue& dcs_experiment_ids, // std::vector instead of base::Value.
void InitializeFeatureList(const base::Value& dcs_features,
const base::Value& dcs_experiment_ids,
const std::string& cmd_line_enable_features, const std::string& cmd_line_enable_features,
const std::string& cmd_line_disable_features, const std::string& cmd_line_disable_features,
const std::string& extra_enable_features, const std::string& extra_enable_features,
...@@ -63,8 +63,7 @@ bool IsFeatureEnabled(const base::Feature& feature); ...@@ -63,8 +63,7 @@ bool IsFeatureEnabled(const base::Feature& feature);
// Given a dictionary of features, create a copy that is ready to be persisted // Given a dictionary of features, create a copy that is ready to be persisted
// to disk. Encodes all values as strings, which is how the FieldTrial // to disk. Encodes all values as strings, which is how the FieldTrial
// classes expect the param data. // classes expect the param data.
base::DictionaryValue GetOverriddenFeaturesForStorage( base::Value GetOverriddenFeaturesForStorage(const base::Value& features);
const base::Value& features);
// Query the set of experiment ids set for this run. Intended only for metrics // Query the set of experiment ids set for this run. Intended only for metrics
// reporting. Must be called after InitializeFeatureList(). May be called on any // reporting. Must be called after InitializeFeatureList(). May be called on any
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment