blob: de21ee52de6674fc18f6fac89f5db24e70128976 [file] [log] [blame]
sdefresne0e566342015-11-24 08:55:461// 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
avibc5337b2015-12-25 23:16:338#include <stddef.h>
9
sdefresne0e566342015-11-24 08:55:4610#include <map>
11#include <set>
12#include <string>
jkrcalbf073372016-07-29 07:21:3113#include <vector>
sdefresne0e566342015-11-24 08:55:4614
Elly Fong-Jonesc3e9aea2019-10-24 19:44:1915#include "base/callback.h"
sdefresne0e566342015-11-24 08:55:4616#include "base/command_line.h"
Elly Fong-Jones3cd75282019-11-12 20:26:5017#include "base/containers/span.h"
Elly Fong-Jonesd488661c2019-07-31 21:53:2618#include "base/feature_list.h"
sdefresne0e566342015-11-24 08:55:4619#include "base/macros.h"
20
21namespace base {
22class ListValue;
23}
24
25namespace flags_ui {
26
jkrcal1383d1d2016-06-17 12:40:5627// Internal functionality exposed for tests.
28namespace internal {
29// The trial group selected when feature variation parameters are registered via
30// FlagsState::RegisterFeatureVariationParameters().
31extern const char kTrialGroupAboutFlags[];
32} // namespace internal
33
sdefresne0e566342015-11-24 08:55:4634struct FeatureEntry;
35class FlagsStorage;
36struct SwitchEntry;
37
Yann Dagobaa08aa2019-07-29 20:52:3738// Enumeration of flag filters.
sdefresne0e566342015-11-24 08:55:4639enum {
40 kOsMac = 1 << 0,
41 kOsWin = 1 << 1,
42 kOsLinux = 1 << 2,
43 kOsCrOS = 1 << 3,
44 kOsAndroid = 1 << 4,
sdefresneabf860062015-11-26 09:45:2245 kOsCrOSOwnerOnly = 1 << 5,
46 kOsIos = 1 << 6,
Yann Dago78028712019-09-04 15:23:5447 kDeprecated = 1 << 7,
Wez04eb259f2019-09-16 19:41:5948 kOsFuchsia = 1 << 8,
Elly Fong-Jones4054f142020-04-17 17:12:3349
50 // Flags marked with this are internal to the flags system. Never set this on
51 // a manually-added flag.
52 kFlagInfrastructure = 1 << 9,
sdefresne0e566342015-11-24 08:55:4653};
54
55// A flag controlling the behavior of the |ConvertFlagsToSwitches| function -
56// whether it should add the sentinel switches around flags.
57enum SentinelsMode { kNoSentinels, kAddSentinels };
58
59// Differentiate between generic flags available on a per session base and flags
60// that influence the whole machine and can be said by the admin only. This flag
61// is relevant for ChromeOS for now only and dictates whether entries marked
62// with the |kOsCrOSOwnerOnly| label should be enabled in the UI or not.
noyauc8da0d22017-07-19 14:39:5263enum FlagAccess { kGeneralAccessFlagsOnly, kOwnerAccessToFlags };
sdefresne0e566342015-11-24 08:55:4664
65// Stores and encapsulates the little state that about:flags has.
66class FlagsState {
67 public:
Elly Fong-Jones3cd75282019-11-12 20:26:5068 // This delegate is used for embedders to configure the behavior of
69 // FlagsState. The delegate is optional.
70 class Delegate {
71 public:
72 // Returns whether |entry| should be excluded from the sets of
73 // switches/features generated by ConvertFlagsToSwitches().
Elly Fong-Jonesf8a4aa42020-08-04 15:53:3074 virtual bool ShouldExcludeFlag(const FlagsStorage* state,
75 const FeatureEntry& entry);
Elly Fong-Jones3cd75282019-11-12 20:26:5076
77 protected:
78 Delegate();
79 virtual ~Delegate();
80
81 Delegate(const Delegate&) = delete;
82 Delegate& operator=(const Delegate&) = delete;
83 };
84
85 // The delegate may be nullptr.
86 FlagsState(base::span<const FeatureEntry> feature_entries,
87 Delegate* delegate);
sdefresne0e566342015-11-24 08:55:4688 ~FlagsState();
89
jkrcal1383d1d2016-06-17 12:40:5690 // Reads the state from |flags_storage| and adds the command line flags
91 // belonging to the active feature entries to |command_line|. Features are
92 // appended via |enable_features_flag_name| and |disable_features_flag_name|.
sdefresne0e566342015-11-24 08:55:4693 void ConvertFlagsToSwitches(FlagsStorage* flags_storage,
94 base::CommandLine* command_line,
95 SentinelsMode sentinels,
96 const char* enable_features_flag_name,
97 const char* disable_features_flag_name);
asvitkinec1a0c4f2016-08-31 20:37:3998
Elaine Chien29fce992020-12-01 17:44:3499 // Returns the FeatureEntry named |internal_name|. Returns null if no entry is
100 // matched.
101 const FeatureEntry* FindFeatureEntryByName(
102 const std::string& internal_name) const;
103
104 // Gets sanitized entries from |flags_storage|, filtering out any entries that
105 // don't exist in |feature_entries_|, and updates |flags_storage|.
106 void GetSanitizedEnabledFlags(FlagsStorage* flags_storage,
107 std::set<std::string>* result) const;
108
Alexei Svitkine103faf82018-11-14 16:43:15109 // Reads the state from |flags_storage| and fills |switches| with the set of
110 // switches corresponding to enabled entries and |features| with the set of
111 // strings corresponding to enabled/disabled base::Feature states. Feature
112 // names are suffixed with ":enabled" or ":disabled" depending on their state.
113 void GetSwitchesAndFeaturesFromFlags(FlagsStorage* flags_storage,
114 std::set<std::string>* switches,
115 std::set<std::string>* features) const;
lawrencewu95059d1e2016-09-19 18:07:02116
sdefresne0e566342015-11-24 08:55:46117 bool IsRestartNeededToCommitChanges();
118 void SetFeatureEntryEnabled(FlagsStorage* flags_storage,
119 const std::string& internal_name,
120 bool enable);
Mustafa Emre Acerb3aa36a82018-05-22 21:44:05121
122 // Sets |value| as the command line switch for feature given by
123 // |internal_name|. |value| contains a list of origins (serialized form of
124 // url::Origin()) separated by whitespace and/or comma. Invalid values in this
125 // list are ignored.
126 void SetOriginListFlag(const std::string& internal_name,
127 const std::string& value,
128 FlagsStorage* flags_storage);
129
Jeremy Roman863386d2017-10-31 19:25:38130 void RemoveFlagsSwitches(base::CommandLine::SwitchMap* switch_list);
sdefresne0e566342015-11-24 08:55:46131 void ResetAllFlags(FlagsStorage* flags_storage);
132 void Reset();
133
jkrcald1d20082016-07-14 15:04:24134 // Registers variations parameter values selected for features in about:flags.
135 // The selected flags are retrieved from |flags_storage|, the registered
136 // variation parameters are connected to their corresponding features in
jkrcalbf073372016-07-29 07:21:31137 // |feature_list|. Returns the (possibly empty) comma separated list of
138 // additional variation ids to register in the MetricsService that come from
139 // variations selected using chrome://flags.
140 std::vector<std::string> RegisterAllFeatureVariationParameters(
141 FlagsStorage* flags_storage,
142 base::FeatureList* feature_list);
jkrcal1383d1d2016-06-17 12:40:56143
sdefresne0e566342015-11-24 08:55:46144 // Gets the list of feature entries. Entries that are available for the
145 // current platform are appended to |supported_entries|; all other entries are
146 // appended to |unsupported_entries|.
Ken Rockot6fc4e282019-12-20 21:07:16147 //
148 // |skip_feature_entry| is called once for each feature in |feature_entries_|,
149 // and entry data for a feature is only included in the output data if the
150 // callback returns |false| for the entry.
sdefresne0e566342015-11-24 08:55:46151 void GetFlagFeatureEntries(
152 FlagsStorage* flags_storage,
153 FlagAccess access,
154 base::ListValue* supported_entries,
155 base::ListValue* unsupported_entries,
Ken Rockot6fc4e282019-12-20 21:07:16156 base::RepeatingCallback<bool(const FeatureEntry&)> skip_feature_entry);
sdefresne0e566342015-11-24 08:55:46157
158 // Returns the value for the current platform. This is one of the values
159 // defined by the OS enum above.
160 // This is exposed only for testing.
Peter Kasting2bd21a152021-06-15 17:20:03161 static unsigned short GetCurrentPlatform();
sdefresne0e566342015-11-24 08:55:46162
sdefresne0e566342015-11-24 08:55:46163 private:
asvitkinec1a0c4f2016-08-31 20:37:39164 // Keeps track of affected switches for each FeatureEntry, based on which
165 // choice is selected for it.
166 struct SwitchEntry;
167
sdefresne0e566342015-11-24 08:55:46168 // Adds mapping to |name_to_switch_map| to set the given switch name/value.
Alexei Svitkine103faf82018-11-14 16:43:15169 void AddSwitchMapping(
170 const std::string& key,
171 const std::string& switch_name,
172 const std::string& switch_value,
173 std::map<std::string, SwitchEntry>* name_to_switch_map) const;
sdefresne0e566342015-11-24 08:55:46174
175 // Adds mapping to |name_to_switch_map| to toggle base::Feature |feature_name|
176 // to state |feature_state|.
177 void AddFeatureMapping(
178 const std::string& key,
179 const std::string& feature_name,
180 bool feature_state,
Alexei Svitkine103faf82018-11-14 16:43:15181 std::map<std::string, SwitchEntry>* name_to_switch_map) const;
sdefresne0e566342015-11-24 08:55:46182
183 // Updates the switches in |command_line| by applying the modifications
184 // specified in |name_to_switch_map| for each entry in |enabled_entries|.
185 // |enable_features_flag_name| and |disable_features_flag_name| are switches
186 // used by the embedder to enable/disable features respectively if supported.
187 void AddSwitchesToCommandLine(
188 const std::set<std::string>& enabled_entries,
189 const std::map<std::string, SwitchEntry>& name_to_switch_map,
190 SentinelsMode sentinels,
191 base::CommandLine* command_line,
192 const char* enable_features_flag_name,
193 const char* disable_features_flag_name);
194
195 // Updates |command_line| by merging the value of the --enable-features= or
196 // --disable-features= list (per the |switch_name| param) with corresponding
197 // entries in |feature_switches| that have value |feature_state|. Keeps track
198 // of the changes by updating |appended_switches|.
199 void MergeFeatureCommandLineSwitch(
200 const std::map<std::string, bool>& feature_switches,
201 const char* switch_name,
202 bool feature_state,
203 base::CommandLine* command_line);
204
Alexei Svitkineea9e39612018-11-14 21:28:03205 // Sanitizes |enabled_entries| to only contain entries that are defined in the
206 // |feature_entries_| and whose |supported_platforms| matches |platform_mask|.
207 // Pass -1 to |platform_mask| to not do platform filtering.
208 std::set<std::string> SanitizeList(
Elly Fong-Jonesf8a4aa42020-08-04 15:53:30209 const FlagsStorage* storage,
Alexei Svitkineea9e39612018-11-14 21:28:03210 const std::set<std::string>& enabled_entries,
211 int platform_mask) const;
sdefresne0e566342015-11-24 08:55:46212
sdefresne0e566342015-11-24 08:55:46213
214 // Variant of GetSanitizedEnabledFlags that also removes any flags that aren't
215 // enabled on the current platform.
216 void GetSanitizedEnabledFlagsForCurrentPlatform(
217 FlagsStorage* flags_storage,
Alexei Svitkine103faf82018-11-14 16:43:15218 std::set<std::string>* result) const;
sdefresne0e566342015-11-24 08:55:46219
asvitkinec1a0c4f2016-08-31 20:37:39220 // Generates a flags to switches mapping based on the set of enabled flags
221 // from |flags_storage|. On output, |enabled_entries| will contain the
222 // internal names of enabled flags and |name_to_switch_map| will contain
223 // information on how they map to command-line flags or features.
224 void GenerateFlagsToSwitchesMapping(
225 FlagsStorage* flags_storage,
226 std::set<std::string>* enabled_entries,
Alexei Svitkine103faf82018-11-14 16:43:15227 std::map<std::string, SwitchEntry>* name_to_switch_map) const;
asvitkinec1a0c4f2016-08-31 20:37:39228
Mustafa Emre Acerb3aa36a82018-05-22 21:44:05229
Elly Fong-Jonesc3e9aea2019-10-24 19:44:19230 // Returns whether there is a FeatureEntry named by |name| in
231 // |feature_entries_| that:
232 // a) Is supported on this |platform_mask|, and
233 // b) Is not excluded by |exclude_predicate_|, if it is set (i.e. for which
234 // |exclude_predicate_| returns false).
Elly Fong-Jonesf8a4aa42020-08-04 15:53:30235 bool IsSupportedFeature(const FlagsStorage* storage,
236 const std::string& name,
237 int platform_mask) const;
Elly Fong-Jonesc3e9aea2019-10-24 19:44:19238
Elly Fong-Jones3cd75282019-11-12 20:26:50239 const base::span<const FeatureEntry> feature_entries_;
sdefresne0e566342015-11-24 08:55:46240
241 bool needs_restart_;
242 std::map<std::string, std::string> flags_switches_;
243
244 // Map from switch name to a set of string, that keeps track which strings
245 // were appended to existing (list value) switches.
246 std::map<std::string, std::set<std::string>> appended_switches_;
247
Elly Fong-Jones3cd75282019-11-12 20:26:50248 // Delegate used for embedders to control display and application of flags.
249 // May be null.
250 Delegate* delegate_;
Elly Fong-Jonesc3e9aea2019-10-24 19:44:19251
sdefresne0e566342015-11-24 08:55:46252 DISALLOW_COPY_AND_ASSIGN(FlagsState);
253};
254
255} // namespace flags_ui
256
257#endif // COMPONENTS_FLAGS_UI_FLAGS_STATE_H_