feature_entry.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. // Copyright 2015 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. #ifndef COMPONENTS_FLAGS_UI_FEATURE_ENTRY_H_
  5. #define COMPONENTS_FLAGS_UI_FEATURE_ENTRY_H_
  6. #include <string>
  7. #include "base/containers/span.h"
  8. #include "build/chromeos_buildflags.h"
  9. namespace base {
  10. struct Feature;
  11. }
  12. namespace flags_ui {
  13. extern const char kMultiSeparatorChar;
  14. // Generic experiment choice option names.
  15. extern const char kGenericExperimentChoiceDefault[];
  16. extern const char kGenericExperimentChoiceEnabled[];
  17. extern const char kGenericExperimentChoiceDisabled[];
  18. extern const char kGenericExperimentChoiceAutomatic[];
  19. // FeatureEntry is used to describe an experimental feature.
  20. //
  21. // Note that features should eventually be either turned on by default with no
  22. // about_flags entries or deleted. Most feature entries should only be around
  23. // for a few milestones, until their full launch.
  24. struct FeatureEntry {
  25. enum Type : unsigned short {
  26. // A feature with a single flag value.
  27. //
  28. // For new entries, it is recommended to instead use FEATURE_VALUE macro
  29. // that is backed by a base::Feature struct. See base/feature_list.h.
  30. SINGLE_VALUE,
  31. // A default enabled feature with a single flag value to disable it.
  32. //
  33. // For new entries, it is recommended to instead use FEATURE_VALUE macro
  34. // that is backed by a base::Feature struct. See base/feature_list.h.
  35. SINGLE_DISABLE_VALUE,
  36. // The feature has multiple values only one of which is ever enabled.
  37. // The first of the values should correspond to a deactivated state for this
  38. // feature (i.e. no command line option). For MULTI_VALUE entries, the
  39. // command_line of the FeatureEntry is not used. If the experiment is
  40. // enabled the command line of the selected Choice is enabled.
  41. MULTI_VALUE,
  42. // The feature has three possible values: Default, Enabled and Disabled.
  43. // This allows the Default group to have its own logic to determine if the
  44. // feature is on.
  45. //
  46. // For new entries, it is recommended to instead use FEATURE_VALUE macro
  47. // that is backed by a base::Feature struct. See base/feature_list.h.
  48. ENABLE_DISABLE_VALUE,
  49. // Corresponds to a base::Feature, per base/feature_list.h. The entry will
  50. // have three states: Default, Enabled, Disabled. When not specified or set
  51. // to Default, the normal default value of the feature is used.
  52. //
  53. // This is recommended for all new entries, since it provides a uniform way
  54. // to specify features in the codebase along with their default state, as
  55. // well as the ability enable/disable via run server-side experiments.
  56. FEATURE_VALUE,
  57. // Corresponds to a base::Feature and additional options [O_1, ..., O_n]
  58. // that specify field trial params. Each of the options can specify a set
  59. // of field trial params. The entry will have n+3 states: Default, Enabled,
  60. // Enabled V_1, ..., Enabled: V_n, Disabled. When set to Default, the normal
  61. // default values of the feature and of the parameters are used (possibly
  62. // passed from the server in a trial config). When set to Enabled, the
  63. // feature is overriden to be enabled and empty set of parameters is used
  64. // boiling down to the default behavior in the code.
  65. FEATURE_WITH_PARAMS_VALUE,
  66. // Corresponds to a command line switch where the value is treated as a list
  67. // of url::Origins. (Lists will not be reordered.) Default state is
  68. // disabled like SINGLE_VALUE.
  69. ORIGIN_LIST_VALUE,
  70. #if BUILDFLAG(IS_CHROMEOS_ASH)
  71. // The below two types are for *platform* features -- that is, those defined
  72. // and queried via platform2/featured/feature_library.h. Such features
  73. // should be defined outside of the browser (e.g., in platform2 or
  74. // platform) using a compile-time-constant default value and name.
  75. // See feature_library.h for more documentation.
  76. // Corresponds to a feature *name*, starting with "CrOSLateBoot", for a
  77. // platform feature.
  78. //
  79. // Broadly similar to FEATURE_VALUE, but we cannot define |base::Feature|s
  80. // starting with CrOSLateBoot in the browser directly -- they must instead
  81. // be defined and queried outside of the browser, using
  82. // platform2/featured/feature_library.h.
  83. PLATFORM_FEATURE_NAME_VALUE,
  84. // Corresponds to a feature *name*, starting with "CrOSLateBoot", for a
  85. // platform feature, along with its parameters.
  86. //
  87. // Broadly similar to FEATURE_WITH_PARAMS_VALUE, but we cannot define
  88. // |base::Feature|s starting with CrOSLateBoot in the browser directly --
  89. // they must instead be defined and queried outside of the browser, using
  90. // platform2/featured/feature_library.h.
  91. PLATFORM_FEATURE_NAME_WITH_PARAMS_VALUE,
  92. #endif // BUILDFLAG(IS_CHROMEOS_ASH)
  93. };
  94. // Describes state of a feature.
  95. enum FeatureState {
  96. // The state of the feature is not overridden by the user.
  97. DEFAULT,
  98. // The feature is enabled by the user.
  99. ENABLED,
  100. // The feature is disabled by the user.
  101. DISABLED,
  102. };
  103. // Used for MULTI_VALUE types to describe one of the possible values the user
  104. // can select.
  105. struct Choice {
  106. // The message containing the choice name.
  107. const char* description;
  108. // Command line switch and value to enabled for this choice.
  109. const char* command_line_switch;
  110. // Simple switches that have no value should use "" for command_line_value.
  111. const char* command_line_value;
  112. };
  113. // Configures one parameter for FEATURE_WITH_PARAMS_VALUE.
  114. struct FeatureParam {
  115. const char* param_name;
  116. const char* param_value;
  117. };
  118. // Specified one variation (list of parameter values) for
  119. // FEATURE_WITH_PARAMS_VALUE.
  120. struct FeatureVariation {
  121. // Text that denotes the variation in chrome://flags. For each variation,
  122. // the user is shown an option labeled "Enabled <description_text>" (with
  123. // the exception of the first option labeled "Enabled" to make clear it is
  124. // the default one). No need for description_id, chrome://flags should not
  125. // get translated. The other parts here use ids for historical reasons and
  126. // can realistically also be moved to direct description_texts.
  127. const char* description_text;
  128. const FeatureParam* params;
  129. int num_params;
  130. // A variation id number in the format of
  131. // VariationsIdsProvider::ForceVariationIds() or nullptr if you do
  132. // not need to set any variation_id for this feature variation.
  133. const char* variation_id;
  134. };
  135. // The internal name of the feature entry. This is never shown to the user.
  136. // It _is_ however stored in the prefs file, so you shouldn't change the
  137. // name of existing flags.
  138. const char* internal_name;
  139. // The feature's name.
  140. const char* visible_name;
  141. // The feature's description.
  142. const char* visible_description;
  143. // The platforms the feature is available on.
  144. // Needs to be more than a compile-time #ifdef because of profile sync.
  145. unsigned short supported_platforms; // bitmask
  146. // Type of entry.
  147. Type type;
  148. union {
  149. struct {
  150. // The commandline switch and value that are added when this flag is
  151. // active. This is different from |internal_name| so that the commandline
  152. // flag can be renamed without breaking the prefs file. This is used if
  153. // type is SINGLE_VALUE or ENABLE_DISABLE_VALUE.
  154. const char* command_line_switch;
  155. // Simple switches that have no value should use "" for
  156. // command_line_value.
  157. const char* command_line_value;
  158. // For ENABLE_DISABLE_VALUE, the command line switch and value to
  159. // explicitly disable the feature.
  160. const char* disable_command_line_switch;
  161. const char* disable_command_line_value;
  162. } switches;
  163. struct {
  164. // For FEATURE_VALUE or FEATURE_WITH_PARAMS_VALUE, the base::Feature
  165. // this entry corresponds to. The same feature must not be used in
  166. // multiple FeatureEntries.
  167. const base::Feature* feature;
  168. // This describes the options if type is FEATURE_WITH_PARAMS_VALUE.
  169. // The first variation is the default "Enabled" variation, its
  170. // description_id is disregarded.
  171. base::span<const FeatureVariation> feature_variations;
  172. // The name of the FieldTrial in which the selected variation parameters
  173. // should be registered. This is used if type is
  174. // FEATURE_WITH_PARAMS_VALUE.
  175. const char* feature_trial_name;
  176. } feature;
  177. struct {
  178. // For PLATFORM_FEATURE_NAME_TYPE or
  179. // PLATFORM_FEATURE_WITH_PARAMS_VALUE_TYPE, the name of the feature this
  180. // entry corresponds to. The same feature must not be used in multiple
  181. // FeatureEntries.
  182. const char* name;
  183. // This describes the options if type is
  184. // PLATFORM_FEATURE_NAME_WITH_PARAMS_VALUE.
  185. // The first variation is the default "Enabled" variation, its
  186. // description_id is disregarded.
  187. base::span<const FeatureVariation> feature_variations;
  188. // The name of the FieldTrial in which the selected variation parameters
  189. // should be registered. This is used if type is
  190. // PLATFORM_FEATURE_NAME_WITH_PARAMS_VALUE.
  191. const char* feature_trial_name;
  192. } platform_feature_name;
  193. // This describes the options if type is MULTI_VALUE.
  194. base::span<const Choice> choices;
  195. };
  196. // Check whether internal |name| matches this FeatureEntry. Depending on the
  197. // type of entry, this compared it to either |internal_name| or the values
  198. // produced by NameForOption().
  199. bool InternalNameMatches(const std::string& name) const;
  200. // Number of options to choose from. This is used if type is MULTI_VALUE,
  201. // ENABLE_DISABLE_VALUE, FEATURE_VALUE, or FEATURE_WITH_PARAMS_VALUE.
  202. int NumOptions() const;
  203. // Returns the name used in prefs for the option at the specified |index|.
  204. // Only used for types that use |num_options|.
  205. std::string NameForOption(int index) const;
  206. // Returns the human readable description for the option at |index|.
  207. // Only used for types that use |num_options|.
  208. std::u16string DescriptionForOption(int index) const;
  209. // Returns the choice for the option at |index|. Only applicable for type
  210. // FEATURE_MULTI.
  211. const FeatureEntry::Choice& ChoiceForOption(int index) const;
  212. // Returns the state of the feature at |index|. Only applicable for types
  213. // FEATURE_VALUE and FEATURE_WITH_PARAMS_VALUE.
  214. FeatureEntry::FeatureState StateForOption(int index) const;
  215. // Returns the variation for the option at |index| or nullptr if there is no
  216. // variation associated at |index|. Only applicable for types FEATURE_VALUE
  217. // and FEATURE_WITH_PARAMS_VALUE.
  218. const FeatureEntry::FeatureVariation* VariationForOption(int index) const;
  219. // Returns true if the entry is considered as valid.
  220. // See the implenetation for the details of what is valid.
  221. bool IsValid() const;
  222. // Returns the variation list. Type must be either
  223. // FEATURE_WITH_PARAMS_VALUE or PLATFORM_FEATURE_NAME_WITH_PARAMS_VALUE.
  224. const base::span<const FeatureVariation> GetVariations() const;
  225. };
  226. namespace testing {
  227. // Separator used for multi values. Multi values are represented in prefs as
  228. // name-of-experiment + kMultiSeparator + selected_index.
  229. extern const char kMultiSeparator[];
  230. } // namespace testing
  231. } // namespace flags_ui
  232. #endif // COMPONENTS_FLAGS_UI_FEATURE_ENTRY_H_