Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2017 The Chromium Authors |
asvitkine | 79ab08c | 2017-01-30 23:27:05 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
danakj | 51d26a4 | 2024-04-25 14:23:56 | [diff] [blame] | 5 | #ifdef UNSAFE_BUFFERS_BUILD |
| 6 | // TODO(crbug.com/40284755): Remove this and spanify to fix the errors. |
| 7 | #pragma allow_unsafe_buffers |
| 8 | #endif |
| 9 | |
asvitkine | 79ab08c | 2017-01-30 23:27:05 | [diff] [blame] | 10 | #ifndef BASE_METRICS_FIELD_TRIAL_PARAMS_H_ |
| 11 | #define BASE_METRICS_FIELD_TRIAL_PARAMS_H_ |
| 12 | |
danakj | e75c6c81 | 2024-07-26 20:37:47 | [diff] [blame^] | 13 | #include <array> |
asvitkine | 79ab08c | 2017-01-30 23:27:05 | [diff] [blame] | 14 | #include <map> |
| 15 | #include <string> |
| 16 | |
| 17 | #include "base/base_export.h" |
Anthony Vallee-Dubois | 9dbbbda3 | 2022-08-26 01:25:31 | [diff] [blame] | 18 | #include "base/feature_list.h" |
Fergal Daly | bcac47a | 2020-03-27 01:54:21 | [diff] [blame] | 19 | #include "base/logging.h" |
Keishi Hattori | 8a7e15d | 2023-01-19 07:16:29 | [diff] [blame] | 20 | #include "base/memory/raw_ptr_exclusion.h" |
Hans Wennborg | afeb390 | 2020-06-17 14:42:29 | [diff] [blame] | 21 | #include "base/notreached.h" |
Tal Pressman | 3e91d6fd | 2020-07-02 04:38:01 | [diff] [blame] | 22 | #include "base/time/time.h" |
asvitkine | 79ab08c | 2017-01-30 23:27:05 | [diff] [blame] | 23 | |
| 24 | namespace base { |
| 25 | |
Miyoung Shin | b5ad87f | 2019-05-13 20:12:45 | [diff] [blame] | 26 | // Key-value mapping type for field trial parameters. |
| 27 | typedef std::map<std::string, std::string> FieldTrialParams; |
| 28 | |
Alexei Svitkine | 8724ea50 | 2019-06-14 21:51:46 | [diff] [blame] | 29 | // Param string decoding function for AssociateFieldTrialParamsFromString(). |
| 30 | typedef std::string (*FieldTrialParamsDecodeStringFunc)(const std::string& str); |
| 31 | |
Weilun Shi | 1cd8fb9 | 2020-07-17 23:31:00 | [diff] [blame] | 32 | // Unescapes special characters from the given string. Used in |
| 33 | // AssociateFieldTrialParamsFromString() as one of the feature params decoding |
| 34 | // functions. |
| 35 | BASE_EXPORT std::string UnescapeValue(const std::string& value); |
| 36 | |
asvitkine | 79ab08c | 2017-01-30 23:27:05 | [diff] [blame] | 37 | // Associates the specified set of key-value |params| with the field trial |
| 38 | // specified by |trial_name| and |group_name|. Fails and returns false if the |
| 39 | // specified field trial already has params associated with it or the trial |
| 40 | // is already active (group() has been called on it). Thread safe. |
Miyoung Shin | b5ad87f | 2019-05-13 20:12:45 | [diff] [blame] | 41 | BASE_EXPORT bool AssociateFieldTrialParams(const std::string& trial_name, |
| 42 | const std::string& group_name, |
| 43 | const FieldTrialParams& params); |
asvitkine | 79ab08c | 2017-01-30 23:27:05 | [diff] [blame] | 44 | |
Alexei Svitkine | 8724ea50 | 2019-06-14 21:51:46 | [diff] [blame] | 45 | // Provides a mechanism to associate multiple set of params to multiple groups |
| 46 | // with a formatted string as returned by FieldTrialList::AllParamsToString(). |
| 47 | // |decode_data_func| allows specifying a custom decoding function. |
| 48 | BASE_EXPORT bool AssociateFieldTrialParamsFromString( |
| 49 | const std::string& params_string, |
| 50 | FieldTrialParamsDecodeStringFunc decode_data_func); |
| 51 | |
asvitkine | 79ab08c | 2017-01-30 23:27:05 | [diff] [blame] | 52 | // Retrieves the set of key-value |params| for the specified field trial, based |
| 53 | // on its selected group. If the field trial does not exist or its selected |
| 54 | // group does not have any parameters associated with it, returns false and |
| 55 | // does not modify |params|. Calling this function will result in the field |
| 56 | // trial being marked as active if found (i.e. group() will be called on it), |
| 57 | // if it wasn't already. Thread safe. |
Miyoung Shin | b5ad87f | 2019-05-13 20:12:45 | [diff] [blame] | 58 | BASE_EXPORT bool GetFieldTrialParams(const std::string& trial_name, |
| 59 | FieldTrialParams* params); |
asvitkine | 79ab08c | 2017-01-30 23:27:05 | [diff] [blame] | 60 | |
| 61 | // Retrieves the set of key-value |params| for the field trial associated with |
| 62 | // the specified |feature|. A feature is associated with at most one field |
| 63 | // trial and selected group. See base/feature_list.h for more information on |
| 64 | // features. If the feature is not enabled, or if there's no associated params, |
| 65 | // returns false and does not modify |params|. Calling this function will |
| 66 | // result in the associated field trial being marked as active if found (i.e. |
| 67 | // group() will be called on it), if it wasn't already. Thread safe. |
Miyoung Shin | b5ad87f | 2019-05-13 20:12:45 | [diff] [blame] | 68 | BASE_EXPORT bool GetFieldTrialParamsByFeature(const base::Feature& feature, |
| 69 | FieldTrialParams* params); |
asvitkine | 79ab08c | 2017-01-30 23:27:05 | [diff] [blame] | 70 | |
| 71 | // Retrieves a specific parameter value corresponding to |param_name| for the |
| 72 | // specified field trial, based on its selected group. If the field trial does |
| 73 | // not exist or the specified parameter does not exist, returns an empty |
| 74 | // string. Calling this function will result in the field trial being marked as |
| 75 | // active if found (i.e. group() will be called on it), if it wasn't already. |
| 76 | // Thread safe. |
| 77 | BASE_EXPORT std::string GetFieldTrialParamValue(const std::string& trial_name, |
| 78 | const std::string& param_name); |
| 79 | |
| 80 | // Retrieves a specific parameter value corresponding to |param_name| for the |
| 81 | // field trial associated with the specified |feature|. A feature is associated |
| 82 | // with at most one field trial and selected group. See base/feature_list.h for |
| 83 | // more information on features. If the feature is not enabled, or the |
| 84 | // specified parameter does not exist, returns an empty string. Calling this |
| 85 | // function will result in the associated field trial being marked as active if |
| 86 | // found (i.e. group() will be called on it), if it wasn't already. Thread safe. |
| 87 | BASE_EXPORT std::string GetFieldTrialParamValueByFeature( |
| 88 | const base::Feature& feature, |
| 89 | const std::string& param_name); |
| 90 | |
| 91 | // Same as GetFieldTrialParamValueByFeature(). On top of that, it converts the |
| 92 | // string value into an int using base::StringToInt() and returns it, if |
| 93 | // successful. Otherwise, it returns |default_value|. If the string value is not |
| 94 | // empty and the conversion does not succeed, it produces a warning to LOG. |
| 95 | BASE_EXPORT int GetFieldTrialParamByFeatureAsInt(const base::Feature& feature, |
| 96 | const std::string& param_name, |
| 97 | int default_value); |
| 98 | |
| 99 | // Same as GetFieldTrialParamValueByFeature(). On top of that, it converts the |
| 100 | // string value into a double using base::StringToDouble() and returns it, if |
| 101 | // successful. Otherwise, it returns |default_value|. If the string value is not |
| 102 | // empty and the conversion does not succeed, it produces a warning to LOG. |
| 103 | BASE_EXPORT double GetFieldTrialParamByFeatureAsDouble( |
| 104 | const base::Feature& feature, |
| 105 | const std::string& param_name, |
| 106 | double default_value); |
| 107 | |
| 108 | // Same as GetFieldTrialParamValueByFeature(). On top of that, it converts the |
| 109 | // string value into a boolean and returns it, if successful. Otherwise, it |
| 110 | // returns |default_value|. The only string representations accepted here are |
| 111 | // "true" and "false". If the string value is not empty and the conversion does |
| 112 | // not succeed, it produces a warning to LOG. |
| 113 | BASE_EXPORT bool GetFieldTrialParamByFeatureAsBool( |
| 114 | const base::Feature& feature, |
| 115 | const std::string& param_name, |
| 116 | bool default_value); |
| 117 | |
Minoru Chikamune | fedb827 | 2023-09-27 02:08:24 | [diff] [blame] | 118 | // Same as GetFieldTrialParamValueByFeature(). On top of that, it converts the |
| 119 | // string value into a base::TimeDelta and returns it, if successful. Otherwise, |
| 120 | // it returns `default_value`. If the string value is not empty and the |
| 121 | // conversion does not succeed, it produces a warning to LOG. |
| 122 | BASE_EXPORT base::TimeDelta GetFieldTrialParamByFeatureAsTimeDelta( |
| 123 | const Feature& feature, |
| 124 | const std::string& param_name, |
| 125 | base::TimeDelta default_value); |
| 126 | |
sfiera | 01757be6 | 2017-09-20 16:36:13 | [diff] [blame] | 127 | // Shared declaration for various FeatureParam<T> types. |
| 128 | // |
| 129 | // This template is defined for the following types T: |
| 130 | // bool |
| 131 | // int |
| 132 | // double |
| 133 | // std::string |
| 134 | // enum types |
Tal Pressman | 3e91d6fd | 2020-07-02 04:38:01 | [diff] [blame] | 135 | // base::TimeDelta |
sfiera | 01757be6 | 2017-09-20 16:36:13 | [diff] [blame] | 136 | // |
| 137 | // See the individual definitions below for the appropriate interfaces. |
| 138 | // Attempting to use it with any other type is a compile error. |
Jesse Doherty | 031e5693 | 2020-10-21 23:23:47 | [diff] [blame] | 139 | // |
| 140 | // Getting a param value from a FeatureParam<T> will have the same semantics as |
| 141 | // GetFieldTrialParamValueByFeature(), see that function's comments for details. |
Andrew Rayskiy | 384fa27 | 2023-10-16 17:45:59 | [diff] [blame] | 142 | template <typename T, bool IsEnum = std::is_enum_v<T>> |
sfiera | 01757be6 | 2017-09-20 16:36:13 | [diff] [blame] | 143 | struct FeatureParam { |
| 144 | // Prevent use of FeatureParam<> with unsupported types (e.g. void*). Uses T |
| 145 | // in its definition so that evaluation is deferred until the template is |
| 146 | // instantiated. |
Andrew Rayskiy | 384fa27 | 2023-10-16 17:45:59 | [diff] [blame] | 147 | static_assert(!std::is_same_v<T, T>, "unsupported FeatureParam<> type"); |
sfiera | 01757be6 | 2017-09-20 16:36:13 | [diff] [blame] | 148 | }; |
| 149 | |
| 150 | // Declares a string-valued parameter. Example: |
| 151 | // |
mlcui | f190ddea | 2024-04-25 15:26:59 | [diff] [blame] | 152 | // constexpr FeatureParam<string> kAssistantName = { |
sfiera | 01757be6 | 2017-09-20 16:36:13 | [diff] [blame] | 153 | // &kAssistantFeature, "assistant_name", "HAL"}; |
| 154 | // |
Xiaohan Wang | 0c04ab3 | 2023-04-26 16:01:44 | [diff] [blame] | 155 | // If the feature is not enabled, the parameter is not set, or set to the empty |
| 156 | // string, then Get() will return the default value. |
sfiera | 01757be6 | 2017-09-20 16:36:13 | [diff] [blame] | 157 | template <> |
| 158 | struct FeatureParam<std::string> { |
| 159 | constexpr FeatureParam(const Feature* feature, |
| 160 | const char* name, |
| 161 | const char* default_value) |
| 162 | : feature(feature), name(name), default_value(default_value) {} |
| 163 | |
Jesse Doherty | 031e5693 | 2020-10-21 23:23:47 | [diff] [blame] | 164 | // Calling Get() will activate the field trial associated with |feature|. See |
| 165 | // GetFieldTrialParamValueByFeature() for more details. |
sfiera | 01757be6 | 2017-09-20 16:36:13 | [diff] [blame] | 166 | BASE_EXPORT std::string Get() const; |
| 167 | |
Bartek Nowierski | 26a2cce | 2024-06-23 21:34:06 | [diff] [blame] | 168 | // RAW_PTR_EXCLUSION: #global-scope, |
Keishi Hattori | 8a7e15d | 2023-01-19 07:16:29 | [diff] [blame] | 169 | RAW_PTR_EXCLUSION const Feature* const feature; |
sfiera | 01757be6 | 2017-09-20 16:36:13 | [diff] [blame] | 170 | const char* const name; |
| 171 | const char* const default_value; |
| 172 | }; |
| 173 | |
| 174 | // Declares a double-valued parameter. Example: |
| 175 | // |
mlcui | f190ddea | 2024-04-25 15:26:59 | [diff] [blame] | 176 | // constexpr FeatureParam<double> kAssistantTriggerThreshold = { |
sfiera | 01757be6 | 2017-09-20 16:36:13 | [diff] [blame] | 177 | // &kAssistantFeature, "trigger_threshold", 0.10}; |
| 178 | // |
Xiaohan Wang | 0c04ab3 | 2023-04-26 16:01:44 | [diff] [blame] | 179 | // If the feature is not enabled, the parameter is not set, or set to an invalid |
| 180 | // double value, then Get() will return the default value. |
sfiera | 01757be6 | 2017-09-20 16:36:13 | [diff] [blame] | 181 | template <> |
| 182 | struct FeatureParam<double> { |
| 183 | constexpr FeatureParam(const Feature* feature, |
| 184 | const char* name, |
| 185 | double default_value) |
| 186 | : feature(feature), name(name), default_value(default_value) {} |
| 187 | |
Jesse Doherty | 031e5693 | 2020-10-21 23:23:47 | [diff] [blame] | 188 | // Calling Get() will activate the field trial associated with |feature|. See |
| 189 | // GetFieldTrialParamValueByFeature() for more details. |
sfiera | 01757be6 | 2017-09-20 16:36:13 | [diff] [blame] | 190 | BASE_EXPORT double Get() const; |
| 191 | |
Bartek Nowierski | 26a2cce | 2024-06-23 21:34:06 | [diff] [blame] | 192 | // RAW_PTR_EXCLUSION: #global-scope |
Keishi Hattori | 8a7e15d | 2023-01-19 07:16:29 | [diff] [blame] | 193 | RAW_PTR_EXCLUSION const Feature* const feature; |
sfiera | 01757be6 | 2017-09-20 16:36:13 | [diff] [blame] | 194 | const char* const name; |
| 195 | const double default_value; |
| 196 | }; |
| 197 | |
| 198 | // Declares an int-valued parameter. Example: |
| 199 | // |
mlcui | f190ddea | 2024-04-25 15:26:59 | [diff] [blame] | 200 | // constexpr FeatureParam<int> kAssistantParallelism = { |
sfiera | 01757be6 | 2017-09-20 16:36:13 | [diff] [blame] | 201 | // &kAssistantFeature, "parallelism", 4}; |
| 202 | // |
Xiaohan Wang | 0c04ab3 | 2023-04-26 16:01:44 | [diff] [blame] | 203 | // If the feature is not enabled, the parameter is not set, or set to an invalid |
| 204 | // int value, then Get() will return the default value. |
sfiera | 01757be6 | 2017-09-20 16:36:13 | [diff] [blame] | 205 | template <> |
| 206 | struct FeatureParam<int> { |
| 207 | constexpr FeatureParam(const Feature* feature, |
| 208 | const char* name, |
| 209 | int default_value) |
| 210 | : feature(feature), name(name), default_value(default_value) {} |
| 211 | |
Jesse Doherty | 031e5693 | 2020-10-21 23:23:47 | [diff] [blame] | 212 | // Calling Get() will activate the field trial associated with |feature|. See |
| 213 | // GetFieldTrialParamValueByFeature() for more details. |
sfiera | 01757be6 | 2017-09-20 16:36:13 | [diff] [blame] | 214 | BASE_EXPORT int Get() const; |
| 215 | |
Bartek Nowierski | 26a2cce | 2024-06-23 21:34:06 | [diff] [blame] | 216 | // RAW_PTR_EXCLUSION: #global-scope |
Keishi Hattori | 8a7e15d | 2023-01-19 07:16:29 | [diff] [blame] | 217 | RAW_PTR_EXCLUSION const Feature* const feature; |
sfiera | 01757be6 | 2017-09-20 16:36:13 | [diff] [blame] | 218 | const char* const name; |
| 219 | const int default_value; |
| 220 | }; |
| 221 | |
| 222 | // Declares a bool-valued parameter. Example: |
| 223 | // |
mlcui | f190ddea | 2024-04-25 15:26:59 | [diff] [blame] | 224 | // constexpr FeatureParam<int> kAssistantIsHelpful = { |
sfiera | 01757be6 | 2017-09-20 16:36:13 | [diff] [blame] | 225 | // &kAssistantFeature, "is_helpful", true}; |
| 226 | // |
Xiaohan Wang | 0c04ab3 | 2023-04-26 16:01:44 | [diff] [blame] | 227 | // If the feature is not enabled, the parameter is not set, or set to value |
| 228 | // other than "true" or "false", then Get() will return the default value. |
sfiera | 01757be6 | 2017-09-20 16:36:13 | [diff] [blame] | 229 | template <> |
| 230 | struct FeatureParam<bool> { |
| 231 | constexpr FeatureParam(const Feature* feature, |
| 232 | const char* name, |
| 233 | bool default_value) |
| 234 | : feature(feature), name(name), default_value(default_value) {} |
| 235 | |
Jesse Doherty | 031e5693 | 2020-10-21 23:23:47 | [diff] [blame] | 236 | // Calling Get() will activate the field trial associated with |feature|. See |
| 237 | // GetFieldTrialParamValueByFeature() for more details. |
sfiera | 01757be6 | 2017-09-20 16:36:13 | [diff] [blame] | 238 | BASE_EXPORT bool Get() const; |
| 239 | |
Bartek Nowierski | 26a2cce | 2024-06-23 21:34:06 | [diff] [blame] | 240 | // RAW_PTR_EXCLUSION: #global-scope |
Keishi Hattori | 8a7e15d | 2023-01-19 07:16:29 | [diff] [blame] | 241 | RAW_PTR_EXCLUSION const Feature* const feature; |
sfiera | 01757be6 | 2017-09-20 16:36:13 | [diff] [blame] | 242 | const char* const name; |
| 243 | const bool default_value; |
| 244 | }; |
| 245 | |
Tal Pressman | 3e91d6fd | 2020-07-02 04:38:01 | [diff] [blame] | 246 | // Declares an TimeDelta-valued parameter. Example: |
| 247 | // |
Evan Stade | 87785864f | 2021-10-13 21:49:11 | [diff] [blame] | 248 | // constexpr base::FeatureParam<base::TimeDelta> kPerAgentDelay{ |
| 249 | // &kPerAgentSchedulingExperiments, "delay", base::TimeDelta()}; |
Tal Pressman | 3e91d6fd | 2020-07-02 04:38:01 | [diff] [blame] | 250 | // |
Xiaohan Wang | 0c04ab3 | 2023-04-26 16:01:44 | [diff] [blame] | 251 | // If the feature is not enabled, the parameter is not set, or set to an |
| 252 | // invalid value (as defined by base::TimeDeltaFromString()), then Get() will |
| 253 | // return the default value. |
Tal Pressman | 3e91d6fd | 2020-07-02 04:38:01 | [diff] [blame] | 254 | template <> |
| 255 | struct FeatureParam<base::TimeDelta> { |
| 256 | constexpr FeatureParam(const Feature* feature, |
| 257 | const char* name, |
| 258 | base::TimeDelta default_value) |
| 259 | : feature(feature), name(name), default_value(default_value) {} |
| 260 | |
Jesse Doherty | 031e5693 | 2020-10-21 23:23:47 | [diff] [blame] | 261 | // Calling Get() will activate the field trial associated with |feature|. See |
| 262 | // GetFieldTrialParamValueByFeature() for more details. |
Tal Pressman | 3e91d6fd | 2020-07-02 04:38:01 | [diff] [blame] | 263 | BASE_EXPORT base::TimeDelta Get() const; |
| 264 | |
Bartek Nowierski | 26a2cce | 2024-06-23 21:34:06 | [diff] [blame] | 265 | // RAW_PTR_EXCLUSION: #global-scope |
Keishi Hattori | 8a7e15d | 2023-01-19 07:16:29 | [diff] [blame] | 266 | RAW_PTR_EXCLUSION const Feature* const feature; |
Tal Pressman | 3e91d6fd | 2020-07-02 04:38:01 | [diff] [blame] | 267 | const char* const name; |
| 268 | const base::TimeDelta default_value; |
| 269 | }; |
| 270 | |
sfiera | 01757be6 | 2017-09-20 16:36:13 | [diff] [blame] | 271 | BASE_EXPORT void LogInvalidEnumValue(const Feature& feature, |
| 272 | const std::string& param_name, |
| 273 | const std::string& value_as_string, |
| 274 | int default_value_as_int); |
| 275 | |
| 276 | // Feature param declaration for an enum, with associated options. Example: |
| 277 | // |
Kenichi Ishibashi | 0a84cb5f3 | 2018-12-13 04:02:10 | [diff] [blame] | 278 | // constexpr FeatureParam<ShapeEnum>::Option kShapeParamOptions[] = { |
sfiera | 01757be6 | 2017-09-20 16:36:13 | [diff] [blame] | 279 | // {SHAPE_CIRCLE, "circle"}, |
| 280 | // {SHAPE_CYLINDER, "cylinder"}, |
| 281 | // {SHAPE_PAPERCLIP, "paperclip"}}; |
mlcui | f190ddea | 2024-04-25 15:26:59 | [diff] [blame] | 282 | // constexpr FeatureParam<ShapeEnum> kAssistantShapeParam = { |
sfiera | 01757be6 | 2017-09-20 16:36:13 | [diff] [blame] | 283 | // &kAssistantFeature, "shape", SHAPE_CIRCLE, &kShapeParamOptions}; |
| 284 | // |
| 285 | // With this declaration, the parameter may be set to "circle", "cylinder", or |
| 286 | // "paperclip", and that will be translated to one of the three enum values. By |
| 287 | // default, or if the param is set to an unknown value, the parameter will be |
| 288 | // assumed to be SHAPE_CIRCLE. |
| 289 | template <typename Enum> |
| 290 | struct FeatureParam<Enum, true> { |
| 291 | struct Option { |
| 292 | constexpr Option(Enum value, const char* name) : value(value), name(name) {} |
| 293 | |
| 294 | const Enum value; |
| 295 | const char* const name; |
| 296 | }; |
| 297 | |
| 298 | template <size_t option_count> |
| 299 | constexpr FeatureParam(const Feature* feature, |
| 300 | const char* name, |
| 301 | const Enum default_value, |
danakj | e75c6c81 | 2024-07-26 20:37:47 | [diff] [blame^] | 302 | const std::array<Option, option_count>& options) |
| 303 | : feature(feature), |
| 304 | name(name), |
| 305 | default_value(default_value), |
| 306 | options(options.data()), |
| 307 | option_count(option_count) { |
| 308 | static_assert(option_count >= 1, "FeatureParam<enum> has no options"); |
| 309 | } |
| 310 | |
| 311 | template <size_t option_count> |
| 312 | constexpr FeatureParam(const Feature* feature, |
| 313 | const char* name, |
| 314 | const Enum default_value, |
sfiera | 01757be6 | 2017-09-20 16:36:13 | [diff] [blame] | 315 | const Option (*options)[option_count]) |
| 316 | : feature(feature), |
| 317 | name(name), |
| 318 | default_value(default_value), |
| 319 | options(*options), |
| 320 | option_count(option_count) { |
| 321 | static_assert(option_count >= 1, "FeatureParam<enum> has no options"); |
| 322 | } |
| 323 | |
Jesse Doherty | 031e5693 | 2020-10-21 23:23:47 | [diff] [blame] | 324 | // Calling Get() will activate the field trial associated with |feature|. See |
| 325 | // GetFieldTrialParamValueByFeature() for more details. |
sfiera | 01757be6 | 2017-09-20 16:36:13 | [diff] [blame] | 326 | Enum Get() const { |
| 327 | std::string value = GetFieldTrialParamValueByFeature(*feature, name); |
| 328 | if (value.empty()) |
| 329 | return default_value; |
| 330 | for (size_t i = 0; i < option_count; ++i) { |
| 331 | if (value == options[i].name) |
| 332 | return options[i].value; |
| 333 | } |
| 334 | LogInvalidEnumValue(*feature, name, value, static_cast<int>(default_value)); |
| 335 | return default_value; |
| 336 | } |
| 337 | |
Fergal Daly | bcac47a | 2020-03-27 01:54:21 | [diff] [blame] | 338 | // Returns the param-string for the given enum value. |
| 339 | std::string GetName(Enum value) const { |
| 340 | for (size_t i = 0; i < option_count; ++i) { |
| 341 | if (value == options[i].value) |
| 342 | return options[i].name; |
| 343 | } |
Peter Boström | 8c29f4a | 2024-05-08 01:12:11 | [diff] [blame] | 344 | NOTREACHED_IN_MIGRATION(); |
Fergal Daly | bcac47a | 2020-03-27 01:54:21 | [diff] [blame] | 345 | return ""; |
| 346 | } |
| 347 | |
Bartek Nowierski | 26a2cce | 2024-06-23 21:34:06 | [diff] [blame] | 348 | // RAW_PTR_EXCLUSION: #global-scope |
Keishi Hattori | 8a7e15d | 2023-01-19 07:16:29 | [diff] [blame] | 349 | RAW_PTR_EXCLUSION const base::Feature* const feature; |
sfiera | 01757be6 | 2017-09-20 16:36:13 | [diff] [blame] | 350 | const char* const name; |
| 351 | const Enum default_value; |
Bartek Nowierski | 26a2cce | 2024-06-23 21:34:06 | [diff] [blame] | 352 | // RAW_PTR_EXCLUSION: #global-scope |
Keishi Hattori | 8a7e15d | 2023-01-19 07:16:29 | [diff] [blame] | 353 | RAW_PTR_EXCLUSION const Option* const options; |
sfiera | 01757be6 | 2017-09-20 16:36:13 | [diff] [blame] | 354 | const size_t option_count; |
| 355 | }; |
| 356 | |
asvitkine | 79ab08c | 2017-01-30 23:27:05 | [diff] [blame] | 357 | } // namespace base |
| 358 | |
| 359 | #endif // BASE_METRICS_FIELD_TRIAL_PARAMS_H_ |