sdefresne | 0e56634 | 2015-11-24 08:55:46 | [diff] [blame] | 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 | |
| 5 | #ifndef COMPONENTS_FLAGS_UI_FLAGS_STATE_H_ |
| 6 | #define COMPONENTS_FLAGS_UI_FLAGS_STATE_H_ |
| 7 | |
avi | bc5337b | 2015-12-25 23:16:33 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | |
sdefresne | 0e56634 | 2015-11-24 08:55:46 | [diff] [blame] | 10 | #include <map> |
| 11 | #include <set> |
| 12 | #include <string> |
jkrcal | bf07337 | 2016-07-29 07:21:31 | [diff] [blame] | 13 | #include <vector> |
sdefresne | 0e56634 | 2015-11-24 08:55:46 | [diff] [blame] | 14 | |
Elly Fong-Jones | c3e9aea | 2019-10-24 19:44:19 | [diff] [blame] | 15 | #include "base/callback.h" |
sdefresne | 0e56634 | 2015-11-24 08:55:46 | [diff] [blame] | 16 | #include "base/command_line.h" |
Elly Fong-Jones | 3cd7528 | 2019-11-12 20:26:50 | [diff] [blame] | 17 | #include "base/containers/span.h" |
Elly Fong-Jones | d488661c | 2019-07-31 21:53:26 | [diff] [blame] | 18 | #include "base/feature_list.h" |
sdefresne | 0e56634 | 2015-11-24 08:55:46 | [diff] [blame] | 19 | #include "base/macros.h" |
Morten Stenshorne | 7afa580 | 2021-07-15 10:04:43 | [diff] [blame^] | 20 | #include "base/values.h" |
sdefresne | 0e56634 | 2015-11-24 08:55:46 | [diff] [blame] | 21 | |
| 22 | namespace flags_ui { |
| 23 | |
jkrcal | 1383d1d | 2016-06-17 12:40:56 | [diff] [blame] | 24 | // Internal functionality exposed for tests. |
| 25 | namespace internal { |
| 26 | // The trial group selected when feature variation parameters are registered via |
| 27 | // FlagsState::RegisterFeatureVariationParameters(). |
| 28 | extern const char kTrialGroupAboutFlags[]; |
| 29 | } // namespace internal |
| 30 | |
sdefresne | 0e56634 | 2015-11-24 08:55:46 | [diff] [blame] | 31 | struct FeatureEntry; |
| 32 | class FlagsStorage; |
| 33 | struct SwitchEntry; |
| 34 | |
Yann Dago | baa08aa | 2019-07-29 20:52:37 | [diff] [blame] | 35 | // Enumeration of flag filters. |
sdefresne | 0e56634 | 2015-11-24 08:55:46 | [diff] [blame] | 36 | enum { |
| 37 | kOsMac = 1 << 0, |
| 38 | kOsWin = 1 << 1, |
| 39 | kOsLinux = 1 << 2, |
| 40 | kOsCrOS = 1 << 3, |
| 41 | kOsAndroid = 1 << 4, |
sdefresne | abf86006 | 2015-11-26 09:45:22 | [diff] [blame] | 42 | kOsCrOSOwnerOnly = 1 << 5, |
| 43 | kOsIos = 1 << 6, |
Yann Dago | 7802871 | 2019-09-04 15:23:54 | [diff] [blame] | 44 | kDeprecated = 1 << 7, |
Wez | 04eb259f | 2019-09-16 19:41:59 | [diff] [blame] | 45 | kOsFuchsia = 1 << 8, |
Elly Fong-Jones | 4054f14 | 2020-04-17 17:12:33 | [diff] [blame] | 46 | |
| 47 | // Flags marked with this are internal to the flags system. Never set this on |
| 48 | // a manually-added flag. |
| 49 | kFlagInfrastructure = 1 << 9, |
sdefresne | 0e56634 | 2015-11-24 08:55:46 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | // A flag controlling the behavior of the |ConvertFlagsToSwitches| function - |
| 53 | // whether it should add the sentinel switches around flags. |
| 54 | enum SentinelsMode { kNoSentinels, kAddSentinels }; |
| 55 | |
| 56 | // Differentiate between generic flags available on a per session base and flags |
| 57 | // that influence the whole machine and can be said by the admin only. This flag |
| 58 | // is relevant for ChromeOS for now only and dictates whether entries marked |
| 59 | // with the |kOsCrOSOwnerOnly| label should be enabled in the UI or not. |
noyau | c8da0d2 | 2017-07-19 14:39:52 | [diff] [blame] | 60 | enum FlagAccess { kGeneralAccessFlagsOnly, kOwnerAccessToFlags }; |
sdefresne | 0e56634 | 2015-11-24 08:55:46 | [diff] [blame] | 61 | |
| 62 | // Stores and encapsulates the little state that about:flags has. |
| 63 | class FlagsState { |
| 64 | public: |
Elly Fong-Jones | 3cd7528 | 2019-11-12 20:26:50 | [diff] [blame] | 65 | // This delegate is used for embedders to configure the behavior of |
| 66 | // FlagsState. The delegate is optional. |
| 67 | class Delegate { |
| 68 | public: |
| 69 | // Returns whether |entry| should be excluded from the sets of |
| 70 | // switches/features generated by ConvertFlagsToSwitches(). |
Elly Fong-Jones | f8a4aa4 | 2020-08-04 15:53:30 | [diff] [blame] | 71 | virtual bool ShouldExcludeFlag(const FlagsStorage* state, |
| 72 | const FeatureEntry& entry); |
Elly Fong-Jones | 3cd7528 | 2019-11-12 20:26:50 | [diff] [blame] | 73 | |
| 74 | protected: |
| 75 | Delegate(); |
| 76 | virtual ~Delegate(); |
| 77 | |
| 78 | Delegate(const Delegate&) = delete; |
| 79 | Delegate& operator=(const Delegate&) = delete; |
| 80 | }; |
| 81 | |
| 82 | // The delegate may be nullptr. |
| 83 | FlagsState(base::span<const FeatureEntry> feature_entries, |
| 84 | Delegate* delegate); |
sdefresne | 0e56634 | 2015-11-24 08:55:46 | [diff] [blame] | 85 | ~FlagsState(); |
| 86 | |
jkrcal | 1383d1d | 2016-06-17 12:40:56 | [diff] [blame] | 87 | // Reads the state from |flags_storage| and adds the command line flags |
| 88 | // belonging to the active feature entries to |command_line|. Features are |
| 89 | // appended via |enable_features_flag_name| and |disable_features_flag_name|. |
sdefresne | 0e56634 | 2015-11-24 08:55:46 | [diff] [blame] | 90 | void ConvertFlagsToSwitches(FlagsStorage* flags_storage, |
| 91 | base::CommandLine* command_line, |
| 92 | SentinelsMode sentinels, |
| 93 | const char* enable_features_flag_name, |
| 94 | const char* disable_features_flag_name); |
asvitkine | c1a0c4f | 2016-08-31 20:37:39 | [diff] [blame] | 95 | |
Elaine Chien | 29fce99 | 2020-12-01 17:44:34 | [diff] [blame] | 96 | // Returns the FeatureEntry named |internal_name|. Returns null if no entry is |
| 97 | // matched. |
| 98 | const FeatureEntry* FindFeatureEntryByName( |
| 99 | const std::string& internal_name) const; |
| 100 | |
| 101 | // Gets sanitized entries from |flags_storage|, filtering out any entries that |
| 102 | // don't exist in |feature_entries_|, and updates |flags_storage|. |
| 103 | void GetSanitizedEnabledFlags(FlagsStorage* flags_storage, |
| 104 | std::set<std::string>* result) const; |
| 105 | |
Alexei Svitkine | 103faf8 | 2018-11-14 16:43:15 | [diff] [blame] | 106 | // Reads the state from |flags_storage| and fills |switches| with the set of |
| 107 | // switches corresponding to enabled entries and |features| with the set of |
| 108 | // strings corresponding to enabled/disabled base::Feature states. Feature |
| 109 | // names are suffixed with ":enabled" or ":disabled" depending on their state. |
| 110 | void GetSwitchesAndFeaturesFromFlags(FlagsStorage* flags_storage, |
| 111 | std::set<std::string>* switches, |
| 112 | std::set<std::string>* features) const; |
lawrencewu | 95059d1e | 2016-09-19 18:07:02 | [diff] [blame] | 113 | |
sdefresne | 0e56634 | 2015-11-24 08:55:46 | [diff] [blame] | 114 | bool IsRestartNeededToCommitChanges(); |
| 115 | void SetFeatureEntryEnabled(FlagsStorage* flags_storage, |
| 116 | const std::string& internal_name, |
| 117 | bool enable); |
Mustafa Emre Acer | b3aa36a8 | 2018-05-22 21:44:05 | [diff] [blame] | 118 | |
| 119 | // Sets |value| as the command line switch for feature given by |
| 120 | // |internal_name|. |value| contains a list of origins (serialized form of |
| 121 | // url::Origin()) separated by whitespace and/or comma. Invalid values in this |
| 122 | // list are ignored. |
| 123 | void SetOriginListFlag(const std::string& internal_name, |
| 124 | const std::string& value, |
| 125 | FlagsStorage* flags_storage); |
| 126 | |
Jeremy Roman | 863386d | 2017-10-31 19:25:38 | [diff] [blame] | 127 | void RemoveFlagsSwitches(base::CommandLine::SwitchMap* switch_list); |
sdefresne | 0e56634 | 2015-11-24 08:55:46 | [diff] [blame] | 128 | void ResetAllFlags(FlagsStorage* flags_storage); |
| 129 | void Reset(); |
| 130 | |
jkrcal | d1d2008 | 2016-07-14 15:04:24 | [diff] [blame] | 131 | // Registers variations parameter values selected for features in about:flags. |
| 132 | // The selected flags are retrieved from |flags_storage|, the registered |
| 133 | // variation parameters are connected to their corresponding features in |
jkrcal | bf07337 | 2016-07-29 07:21:31 | [diff] [blame] | 134 | // |feature_list|. Returns the (possibly empty) comma separated list of |
| 135 | // additional variation ids to register in the MetricsService that come from |
| 136 | // variations selected using chrome://flags. |
| 137 | std::vector<std::string> RegisterAllFeatureVariationParameters( |
| 138 | FlagsStorage* flags_storage, |
| 139 | base::FeatureList* feature_list); |
jkrcal | 1383d1d | 2016-06-17 12:40:56 | [diff] [blame] | 140 | |
sdefresne | 0e56634 | 2015-11-24 08:55:46 | [diff] [blame] | 141 | // Gets the list of feature entries. Entries that are available for the |
| 142 | // current platform are appended to |supported_entries|; all other entries are |
| 143 | // appended to |unsupported_entries|. |
Ken Rockot | 6fc4e28 | 2019-12-20 21:07:16 | [diff] [blame] | 144 | // |
| 145 | // |skip_feature_entry| is called once for each feature in |feature_entries_|, |
| 146 | // and entry data for a feature is only included in the output data if the |
| 147 | // callback returns |false| for the entry. |
sdefresne | 0e56634 | 2015-11-24 08:55:46 | [diff] [blame] | 148 | void GetFlagFeatureEntries( |
| 149 | FlagsStorage* flags_storage, |
| 150 | FlagAccess access, |
Morten Stenshorne | 7afa580 | 2021-07-15 10:04:43 | [diff] [blame^] | 151 | base::Value::ListStorage& supported_entries, |
| 152 | base::Value::ListStorage& unsupported_entries, |
Ken Rockot | 6fc4e28 | 2019-12-20 21:07:16 | [diff] [blame] | 153 | base::RepeatingCallback<bool(const FeatureEntry&)> skip_feature_entry); |
sdefresne | 0e56634 | 2015-11-24 08:55:46 | [diff] [blame] | 154 | |
| 155 | // Returns the value for the current platform. This is one of the values |
| 156 | // defined by the OS enum above. |
| 157 | // This is exposed only for testing. |
Peter Kasting | 2bd21a15 | 2021-06-15 17:20:03 | [diff] [blame] | 158 | static unsigned short GetCurrentPlatform(); |
sdefresne | 0e56634 | 2015-11-24 08:55:46 | [diff] [blame] | 159 | |
sdefresne | 0e56634 | 2015-11-24 08:55:46 | [diff] [blame] | 160 | private: |
asvitkine | c1a0c4f | 2016-08-31 20:37:39 | [diff] [blame] | 161 | // Keeps track of affected switches for each FeatureEntry, based on which |
| 162 | // choice is selected for it. |
| 163 | struct SwitchEntry; |
| 164 | |
sdefresne | 0e56634 | 2015-11-24 08:55:46 | [diff] [blame] | 165 | // Adds mapping to |name_to_switch_map| to set the given switch name/value. |
Alexei Svitkine | 103faf8 | 2018-11-14 16:43:15 | [diff] [blame] | 166 | void AddSwitchMapping( |
| 167 | const std::string& key, |
| 168 | const std::string& switch_name, |
| 169 | const std::string& switch_value, |
| 170 | std::map<std::string, SwitchEntry>* name_to_switch_map) const; |
sdefresne | 0e56634 | 2015-11-24 08:55:46 | [diff] [blame] | 171 | |
| 172 | // Adds mapping to |name_to_switch_map| to toggle base::Feature |feature_name| |
| 173 | // to state |feature_state|. |
| 174 | void AddFeatureMapping( |
| 175 | const std::string& key, |
| 176 | const std::string& feature_name, |
| 177 | bool feature_state, |
Alexei Svitkine | 103faf8 | 2018-11-14 16:43:15 | [diff] [blame] | 178 | std::map<std::string, SwitchEntry>* name_to_switch_map) const; |
sdefresne | 0e56634 | 2015-11-24 08:55:46 | [diff] [blame] | 179 | |
| 180 | // Updates the switches in |command_line| by applying the modifications |
| 181 | // specified in |name_to_switch_map| for each entry in |enabled_entries|. |
| 182 | // |enable_features_flag_name| and |disable_features_flag_name| are switches |
| 183 | // used by the embedder to enable/disable features respectively if supported. |
| 184 | void AddSwitchesToCommandLine( |
| 185 | const std::set<std::string>& enabled_entries, |
| 186 | const std::map<std::string, SwitchEntry>& name_to_switch_map, |
| 187 | SentinelsMode sentinels, |
| 188 | base::CommandLine* command_line, |
| 189 | const char* enable_features_flag_name, |
| 190 | const char* disable_features_flag_name); |
| 191 | |
| 192 | // Updates |command_line| by merging the value of the --enable-features= or |
| 193 | // --disable-features= list (per the |switch_name| param) with corresponding |
| 194 | // entries in |feature_switches| that have value |feature_state|. Keeps track |
| 195 | // of the changes by updating |appended_switches|. |
| 196 | void MergeFeatureCommandLineSwitch( |
| 197 | const std::map<std::string, bool>& feature_switches, |
| 198 | const char* switch_name, |
| 199 | bool feature_state, |
| 200 | base::CommandLine* command_line); |
| 201 | |
Alexei Svitkine | ea9e3961 | 2018-11-14 21:28:03 | [diff] [blame] | 202 | // Sanitizes |enabled_entries| to only contain entries that are defined in the |
| 203 | // |feature_entries_| and whose |supported_platforms| matches |platform_mask|. |
| 204 | // Pass -1 to |platform_mask| to not do platform filtering. |
| 205 | std::set<std::string> SanitizeList( |
Elly Fong-Jones | f8a4aa4 | 2020-08-04 15:53:30 | [diff] [blame] | 206 | const FlagsStorage* storage, |
Alexei Svitkine | ea9e3961 | 2018-11-14 21:28:03 | [diff] [blame] | 207 | const std::set<std::string>& enabled_entries, |
| 208 | int platform_mask) const; |
sdefresne | 0e56634 | 2015-11-24 08:55:46 | [diff] [blame] | 209 | |
sdefresne | 0e56634 | 2015-11-24 08:55:46 | [diff] [blame] | 210 | |
| 211 | // Variant of GetSanitizedEnabledFlags that also removes any flags that aren't |
| 212 | // enabled on the current platform. |
| 213 | void GetSanitizedEnabledFlagsForCurrentPlatform( |
| 214 | FlagsStorage* flags_storage, |
Alexei Svitkine | 103faf8 | 2018-11-14 16:43:15 | [diff] [blame] | 215 | std::set<std::string>* result) const; |
sdefresne | 0e56634 | 2015-11-24 08:55:46 | [diff] [blame] | 216 | |
asvitkine | c1a0c4f | 2016-08-31 20:37:39 | [diff] [blame] | 217 | // Generates a flags to switches mapping based on the set of enabled flags |
| 218 | // from |flags_storage|. On output, |enabled_entries| will contain the |
| 219 | // internal names of enabled flags and |name_to_switch_map| will contain |
| 220 | // information on how they map to command-line flags or features. |
| 221 | void GenerateFlagsToSwitchesMapping( |
| 222 | FlagsStorage* flags_storage, |
| 223 | std::set<std::string>* enabled_entries, |
Alexei Svitkine | 103faf8 | 2018-11-14 16:43:15 | [diff] [blame] | 224 | std::map<std::string, SwitchEntry>* name_to_switch_map) const; |
asvitkine | c1a0c4f | 2016-08-31 20:37:39 | [diff] [blame] | 225 | |
Mustafa Emre Acer | b3aa36a8 | 2018-05-22 21:44:05 | [diff] [blame] | 226 | |
Elly Fong-Jones | c3e9aea | 2019-10-24 19:44:19 | [diff] [blame] | 227 | // Returns whether there is a FeatureEntry named by |name| in |
| 228 | // |feature_entries_| that: |
| 229 | // a) Is supported on this |platform_mask|, and |
| 230 | // b) Is not excluded by |exclude_predicate_|, if it is set (i.e. for which |
| 231 | // |exclude_predicate_| returns false). |
Elly Fong-Jones | f8a4aa4 | 2020-08-04 15:53:30 | [diff] [blame] | 232 | bool IsSupportedFeature(const FlagsStorage* storage, |
| 233 | const std::string& name, |
| 234 | int platform_mask) const; |
Elly Fong-Jones | c3e9aea | 2019-10-24 19:44:19 | [diff] [blame] | 235 | |
Elly Fong-Jones | 3cd7528 | 2019-11-12 20:26:50 | [diff] [blame] | 236 | const base::span<const FeatureEntry> feature_entries_; |
sdefresne | 0e56634 | 2015-11-24 08:55:46 | [diff] [blame] | 237 | |
| 238 | bool needs_restart_; |
| 239 | std::map<std::string, std::string> flags_switches_; |
| 240 | |
| 241 | // Map from switch name to a set of string, that keeps track which strings |
| 242 | // were appended to existing (list value) switches. |
| 243 | std::map<std::string, std::set<std::string>> appended_switches_; |
| 244 | |
Elly Fong-Jones | 3cd7528 | 2019-11-12 20:26:50 | [diff] [blame] | 245 | // Delegate used for embedders to control display and application of flags. |
| 246 | // May be null. |
| 247 | Delegate* delegate_; |
Elly Fong-Jones | c3e9aea | 2019-10-24 19:44:19 | [diff] [blame] | 248 | |
sdefresne | 0e56634 | 2015-11-24 08:55:46 | [diff] [blame] | 249 | DISALLOW_COPY_AND_ASSIGN(FlagsState); |
| 250 | }; |
| 251 | |
| 252 | } // namespace flags_ui |
| 253 | |
| 254 | #endif // COMPONENTS_FLAGS_UI_FLAGS_STATE_H_ |