Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chromecast/base/cast_features.h" | |
| 6 | |
| 7 #include "base/command_line.h" | |
| 8 #include "base/feature_list.h" | |
| 9 #include "base/lazy_instance.h" | |
| 10 #include "base/metrics/field_trial.h" | |
| 11 #include "base/metrics/field_trial_param_associator.h" | |
| 12 #include "base/metrics/field_trial_params.h" | |
| 13 #include "base/strings/string_number_conversions.h" | |
| 14 #include "base/values.h" | |
| 15 | |
| 16 namespace chromecast { | |
| 17 namespace { | |
| 18 | |
| 19 // A constant used to always activate a FieldTrial. | |
| 20 const base::FieldTrial::Probability k100PercentProbability = 100; | |
| 21 | |
| 22 // The name of the default group to use for Cast DCS features. | |
| 23 const char kDefaultDCSFeaturesGroup[] = "default_dcs_features_group"; | |
| 24 | |
| 25 base::LazyInstance<std::unordered_set<int32_t>>::DestructorAtExit | |
|
halliwell
2017/04/20 02:49:30
don't we usually just do leaky unless there's a st
slan
2017/04/20 16:26:02
Chromium seems to be fairly split. There are 17 us
| |
| 26 g_experiment_ids = LAZY_INSTANCE_INITIALIZER; | |
| 27 bool g_experiment_ids_initialized = false; | |
| 28 | |
| 29 void SetExperimentIds(const base::ListValue& list) { | |
| 30 DCHECK(!g_experiment_ids_initialized); | |
| 31 int32_t id; | |
| 32 auto ids = base::MakeUnique<std::unordered_set<int32_t>>(); | |
| 33 for (size_t i = 0; i < list.GetSize(); ++i) { | |
| 34 if (list.GetInteger(i, &id)) { | |
| 35 ids->insert(id); | |
| 36 } else { | |
| 37 LOG(ERROR) << "Non-integer value found in experiment id list!"; | |
| 38 } | |
| 39 } | |
| 40 g_experiment_ids.Get().swap(*ids); | |
| 41 g_experiment_ids_initialized = true; | |
| 42 } | |
| 43 | |
| 44 } // namespace | |
| 45 | |
| 46 void InitializeFeatureList(const base::DictionaryValue& dcs_features, | |
| 47 const base::ListValue& dcs_experiment_ids, | |
| 48 const std::string& cmd_line_enable_features, | |
| 49 const std::string& cmd_line_disable_features) { | |
| 50 DCHECK(!base::FeatureList::GetInstance()); | |
| 51 | |
| 52 // Set the experiments. | |
| 53 SetExperimentIds(dcs_experiment_ids); | |
| 54 | |
| 55 // Initialize the FeatureList from the command line. | |
| 56 auto feature_list = base::MakeUnique<base::FeatureList>(); | |
| 57 feature_list->InitializeFromCommandLine(cmd_line_enable_features, | |
| 58 cmd_line_disable_features); | |
| 59 | |
| 60 // Override defaults from the DCS config. | |
| 61 base::DictionaryValue::Iterator iterator(dcs_features); | |
|
halliwell
2017/04/20 02:49:30
nit, can init iterator inside for (...)?
slan
2017/04/20 16:26:02
Done, also updated the type and variable name to b
| |
| 62 for (; !iterator.IsAtEnd(); iterator.Advance()) { | |
| 63 // Each feature must have its own FieldTrial object. Since experiments are | |
| 64 // controlled server-side for Chromecast, and this class is designed with a | |
| 65 // client-side experimentation framework in mind, these parameters are | |
| 66 // carefully chosen: | |
| 67 // - The field trial name is unused for our purposes, we simple need it to | |
| 68 // be unique among features. | |
| 69 // - The probability is hard-coded to 100% so that the FeatureList always | |
| 70 // respects the value from DCS. | |
| 71 // - The default group is unused; it will be the same for every feature. | |
| 72 // - Expiration year, month, and day use a special value such that the | |
| 73 // feature will never expire. | |
| 74 // - SESSION_RANDOMIZED is used to prevent the need for an | |
| 75 // entropy_provider. However, this value doesn't matter. | |
| 76 // - We don't care about the group_id. | |
| 77 // | |
| 78 const std::string& field_trial_name = iterator.key(); | |
| 79 auto* field_trial = base::FieldTrialList::FactoryGetFieldTrial( | |
| 80 field_trial_name, k100PercentProbability, kDefaultDCSFeaturesGroup, | |
| 81 base::FieldTrialList::kNoExpirationYear, 1 /* month */, 1 /* day */, | |
| 82 base::FieldTrial::SESSION_RANDOMIZED, nullptr); | |
| 83 | |
| 84 bool enabled; | |
| 85 if (iterator.value().GetAsBoolean(&enabled)) { | |
| 86 // A boolean entry simply either enables or disables a feature. | |
| 87 feature_list->RegisterFieldTrialOverride( | |
| 88 iterator.key(), | |
| 89 enabled ? base::FeatureList::OVERRIDE_ENABLE_FEATURE | |
| 90 : base::FeatureList::OVERRIDE_DISABLE_FEATURE, | |
| 91 field_trial); | |
| 92 continue; | |
| 93 } | |
| 94 | |
| 95 const base::DictionaryValue* params_dict; | |
| 96 if (iterator.value().GetAsDictionary(¶ms_dict)) { | |
| 97 // A dictionary entry implies that the feature is enabled. | |
| 98 feature_list->RegisterFieldTrialOverride( | |
| 99 iterator.key(), base::FeatureList::OVERRIDE_ENABLE_FEATURE, | |
| 100 field_trial); | |
| 101 | |
| 102 // If the feature has not been overriden from the command line, set its | |
| 103 // parameters accordingly. | |
| 104 if (!feature_list->IsFeatureOverriddenFromCommandLine( | |
| 105 iterator.key(), base::FeatureList::OVERRIDE_DISABLE_FEATURE)) { | |
| 106 // Build a map of the FieldTrial parameters and associate it to the | |
| 107 // FieldTrial. | |
| 108 base::FieldTrialParamAssociator::FieldTrialParams params; | |
| 109 base::DictionaryValue::Iterator params_iterator(*params_dict); | |
|
halliwell
2017/04/20 02:49:30
ditto init in for(...)
slan
2017/04/20 16:26:02
Done.
| |
| 110 for (; !params_iterator.IsAtEnd(); params_iterator.Advance()) { | |
| 111 std::string val; | |
| 112 if (params_iterator.value().GetAsString(&val)) { | |
| 113 params[params_iterator.key()] = val; | |
| 114 } else { | |
| 115 LOG(ERROR) << "Entry in params dict for \"" << iterator.key() | |
| 116 << "\"" | |
| 117 << " feature is not a string. Skipping."; | |
| 118 } | |
| 119 } | |
| 120 | |
| 121 // Register the params, so that they can be queried by client code. | |
| 122 bool success = base::AssociateFieldTrialParams( | |
| 123 field_trial_name, kDefaultDCSFeaturesGroup, params); | |
| 124 DCHECK(success); | |
| 125 } | |
| 126 continue; | |
| 127 } | |
| 128 | |
| 129 // Other base::Value types are not supported. | |
| 130 LOG(ERROR) << "A DCS feature mapped to an unsupported value. key: " | |
| 131 << iterator.key() << " type: " << iterator.value().type(); | |
| 132 } | |
| 133 | |
| 134 base::FeatureList::SetInstance(std::move(feature_list)); | |
| 135 } | |
| 136 | |
| 137 std::unique_ptr<base::DictionaryValue> GetOverriddenFeaturesForStorage( | |
| 138 const base::DictionaryValue& features) { | |
| 139 auto persistent_dict = base::MakeUnique<base::DictionaryValue>(); | |
| 140 | |
| 141 // |features| maps feature names to either a boolean or a dict of params. | |
| 142 base::DictionaryValue::Iterator iterator(features); | |
|
halliwell
2017/04/20 02:49:30
ditto
slan
2017/04/20 16:26:02
Done.
| |
| 143 for (; !iterator.IsAtEnd(); iterator.Advance()) { | |
| 144 bool enabled; | |
| 145 if (iterator.value().GetAsBoolean(&enabled)) { | |
| 146 persistent_dict->SetBoolean(iterator.key(), enabled); | |
| 147 continue; | |
| 148 } | |
| 149 | |
| 150 const base::DictionaryValue* params_dict; | |
| 151 if (iterator.value().GetAsDictionary(¶ms_dict)) { | |
| 152 auto params = base::MakeUnique<base::DictionaryValue>(); | |
| 153 base::DictionaryValue::Iterator params_iterator(*params_dict); | |
| 154 bool bval; | |
| 155 int ival; | |
| 156 double dval; | |
| 157 std::string sval; | |
| 158 for (; !params_iterator.IsAtEnd(); params_iterator.Advance()) { | |
| 159 const auto& param_key = params_iterator.key(); | |
|
halliwell
2017/04/20 02:49:31
ditto
slan
2017/04/20 16:26:02
Done.
| |
| 160 const auto& param_val = params_iterator.value(); | |
| 161 if (param_val.GetAsBoolean(&bval)) { | |
| 162 params->SetString(param_key, bval ? "true" : "false"); | |
| 163 } else if (param_val.GetAsInteger(&ival)) { | |
| 164 params->SetString(param_key, base::IntToString(ival)); | |
| 165 } else if (param_val.GetAsDouble(&dval)) { | |
| 166 params->SetString(param_key, base::DoubleToString(dval)); | |
| 167 } else if (param_val.GetAsString(&sval)) { | |
| 168 params->SetString(param_key, sval); | |
| 169 } else { | |
| 170 LOG(ERROR) << "Entry in params dict for \"" << iterator.key() << "\"" | |
| 171 << " is not of a supported type (key: " | |
| 172 << params_iterator.key() << ", type: " << param_val.type(); | |
| 173 } | |
| 174 } | |
| 175 persistent_dict->Set(iterator.key(), std::move(params)); | |
| 176 continue; | |
| 177 } | |
| 178 | |
| 179 // Other base::Value types are not supported. | |
| 180 LOG(ERROR) << "A DCS feature mapped to an unsupported value. key: " | |
| 181 << iterator.key() << " type: " << iterator.value().type(); | |
| 182 } | |
| 183 | |
| 184 return persistent_dict; | |
| 185 } | |
| 186 | |
| 187 std::unordered_set<int32_t> GetDCSExperimentIds() { | |
| 188 DCHECK(g_experiment_ids_initialized); | |
| 189 return g_experiment_ids.Get(); | |
| 190 } | |
| 191 | |
| 192 void ResetCastFeaturesForTest() { | |
| 193 g_experiment_ids_initialized = false; | |
| 194 base::FeatureList::ClearInstanceForTesting(); | |
| 195 } | |
| 196 | |
| 197 } // namespace chromecast | |
| OLD | NEW |