[email protected] | 9c7453d | 2012-01-21 00:45:40 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 1a47d7e | 2010-10-15 00:37:24 | [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 | |
| 5 | #ifndef CHROME_BROWSER_ABOUT_FLAGS_H_ |
| 6 | #define CHROME_BROWSER_ABOUT_FLAGS_H_ |
[email protected] | 1a47d7e | 2010-10-15 00:37:24 | [diff] [blame] | 7 | |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 8 | #include <map> |
[email protected] | 1a47d7e | 2010-10-15 00:37:24 | [diff] [blame] | 9 | #include <string> |
| 10 | |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 11 | #include "base/command_line.h" |
[email protected] | 83e9fa70 | 2013-02-25 19:30:44 | [diff] [blame] | 12 | #include "base/string16.h" |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 13 | |
[email protected] | 1a47d7e | 2010-10-15 00:37:24 | [diff] [blame] | 14 | class PrefService; |
| 15 | |
[email protected] | f3a1c64 | 2011-07-12 19:15:03 | [diff] [blame] | 16 | namespace base { |
| 17 | class ListValue; |
| 18 | } |
| 19 | |
[email protected] | 1a47d7e | 2010-10-15 00:37:24 | [diff] [blame] | 20 | namespace about_flags { |
| 21 | |
[email protected] | a314ee5a | 2010-10-26 21:23:28 | [diff] [blame] | 22 | // Enumeration of OSs. |
| 23 | // This is exposed only for testing. |
[email protected] | 9c7453d | 2012-01-21 00:45:40 | [diff] [blame] | 24 | enum { kOsMac = 1 << 0, kOsWin = 1 << 1, kOsLinux = 1 << 2 , kOsCrOS = 1 << 3, |
[email protected] | 37736bd | 2013-04-18 11:53:58 | [diff] [blame] | 25 | kOsAndroid = 1 << 4, kOsCrOSOwnerOnly = 1 << 5 }; |
[email protected] | a314ee5a | 2010-10-26 21:23:28 | [diff] [blame] | 26 | |
| 27 | // Experiment is used internally by about_flags to describe an experiment (and |
| 28 | // for testing). |
| 29 | // This is exposed only for testing. |
| 30 | struct Experiment { |
[email protected] | 8a6ff28d | 2010-12-02 16:35:19 | [diff] [blame] | 31 | enum Type { |
| 32 | // An experiment with a single value. This is typically what you want. |
| 33 | SINGLE_VALUE, |
| 34 | |
| 35 | // The experiment has multiple values only one of which is ever enabled. |
[email protected] | 28e35af | 2011-02-09 12:56:22 | [diff] [blame] | 36 | // The first of the values should correspond to a deactivated state for this |
| 37 | // lab (i.e. no command line option). For MULTI_VALUE experiments the |
| 38 | // command_line of the Experiment is not used. If the experiment is enabled |
| 39 | // the command line of the selected Choice is enabled. |
[email protected] | 8a6ff28d | 2010-12-02 16:35:19 | [diff] [blame] | 40 | MULTI_VALUE, |
[email protected] | 83e9fa70 | 2013-02-25 19:30:44 | [diff] [blame] | 41 | |
| 42 | // The experiment has three possible values: Default, Enabled and Disabled. |
| 43 | // This should be used for experiments that may have their own logic to |
| 44 | // decide if the feature should be on when not explicitly specified via |
| 45 | // about flags - for example via FieldTrials. |
| 46 | ENABLE_DISABLE_VALUE, |
[email protected] | 8a6ff28d | 2010-12-02 16:35:19 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | // Used for MULTI_VALUE types to describe one of the possible values the user |
| 50 | // can select. |
| 51 | struct Choice { |
| 52 | // ID of the message containing the choice name. |
| 53 | int description_id; |
| 54 | |
[email protected] | a8274453 | 2011-02-11 16:15:53 | [diff] [blame] | 55 | // Command line switch and value to enabled for this choice. |
| 56 | const char* command_line_switch; |
| 57 | // Simple switches that have no value should use "" for command_line_value. |
| 58 | const char* command_line_value; |
[email protected] | 8a6ff28d | 2010-12-02 16:35:19 | [diff] [blame] | 59 | }; |
| 60 | |
[email protected] | a314ee5a | 2010-10-26 21:23:28 | [diff] [blame] | 61 | // The internal name of the experiment. This is never shown to the user. |
| 62 | // It _is_ however stored in the prefs file, so you shouldn't change the |
| 63 | // name of existing flags. |
| 64 | const char* internal_name; |
| 65 | |
| 66 | // String id of the message containing the experiment's name. |
| 67 | int visible_name_id; |
| 68 | |
| 69 | // String id of the message containing the experiment's description. |
| 70 | int visible_description_id; |
| 71 | |
| 72 | // The platforms the experiment is available on |
| 73 | // Needs to be more than a compile-time #ifdef because of profile sync. |
| 74 | unsigned supported_platforms; // bitmask |
| 75 | |
[email protected] | 8a6ff28d | 2010-12-02 16:35:19 | [diff] [blame] | 76 | // Type of experiment. |
| 77 | Type type; |
| 78 | |
[email protected] | 83e9fa70 | 2013-02-25 19:30:44 | [diff] [blame] | 79 | // The commandline switch and value that are added when this flag is active. |
[email protected] | a8274453 | 2011-02-11 16:15:53 | [diff] [blame] | 80 | // This is different from |internal_name| so that the commandline flag can be |
[email protected] | a314ee5a | 2010-10-26 21:23:28 | [diff] [blame] | 81 | // renamed without breaking the prefs file. |
[email protected] | 83e9fa70 | 2013-02-25 19:30:44 | [diff] [blame] | 82 | // This is used if type is SINGLE_VALUE or ENABLE_DISABLE_VALUE. |
[email protected] | a8274453 | 2011-02-11 16:15:53 | [diff] [blame] | 83 | const char* command_line_switch; |
| 84 | // Simple switches that have no value should use "" for command_line_value. |
| 85 | const char* command_line_value; |
[email protected] | 8a6ff28d | 2010-12-02 16:35:19 | [diff] [blame] | 86 | |
[email protected] | 83e9fa70 | 2013-02-25 19:30:44 | [diff] [blame] | 87 | // For ENABLE_DISABLE_VALUE, the command line switch and value to explictly |
| 88 | // disable the feature. |
| 89 | const char* disable_command_line_switch; |
| 90 | const char* disable_command_line_value; |
| 91 | |
[email protected] | 8a6ff28d | 2010-12-02 16:35:19 | [diff] [blame] | 92 | // This is used if type is MULTI_VALUE. |
| 93 | const Choice* choices; |
| 94 | |
| 95 | // Number of |choices|. |
| 96 | // This is used if type is MULTI_VALUE. |
| 97 | int num_choices; |
[email protected] | 83e9fa70 | 2013-02-25 19:30:44 | [diff] [blame] | 98 | |
| 99 | // Returns the name used in prefs for the choice at the specified |index|. |
| 100 | std::string NameForChoice(int index) const; |
| 101 | |
| 102 | // Returns the human readable description for the choice at |index|. |
| 103 | string16 DescriptionForChoice(int index) const; |
[email protected] | a314ee5a | 2010-10-26 21:23:28 | [diff] [blame] | 104 | }; |
| 105 | |
[email protected] | 1a47d7e | 2010-10-15 00:37:24 | [diff] [blame] | 106 | // Reads the Labs |prefs| (called "Labs" for historical reasons) and adds the |
| 107 | // commandline flags belonging to the active experiments to |command_line|. |
| 108 | void ConvertFlagsToSwitches(PrefService* prefs, CommandLine* command_line); |
| 109 | |
[email protected] | 37736bd | 2013-04-18 11:53:58 | [diff] [blame] | 110 | // Differentiate between generic flags available on a per session base and flags |
| 111 | // that influence the whole machine and can be said by the admin only. This flag |
| 112 | // is relevant for ChromeOS for now only and dictates whether entries marked |
| 113 | // with the |kOsCrOSOwnerOnly| label should be enabled in the UI or not. |
| 114 | enum FlagAccess { kGeneralAccessFlagsOnly, kOwnerAccessToFlags }; |
| 115 | |
[email protected] | 1a47d7e | 2010-10-15 00:37:24 | [diff] [blame] | 116 | // Get a list of all available experiments. The caller owns the result. |
[email protected] | 37736bd | 2013-04-18 11:53:58 | [diff] [blame] | 117 | base::ListValue* GetFlagsExperimentsData(PrefService* prefs, FlagAccess access); |
[email protected] | 1a47d7e | 2010-10-15 00:37:24 | [diff] [blame] | 118 | |
| 119 | // Returns true if one of the experiment flags has been flipped since startup. |
| 120 | bool IsRestartNeededToCommitChanges(); |
| 121 | |
| 122 | // Enables or disables the experiment with id |internal_name|. |
| 123 | void SetExperimentEnabled( |
| 124 | PrefService* prefs, const std::string& internal_name, bool enable); |
| 125 | |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 126 | // Removes all switches that were added to a command line by a previous call to |
| 127 | // |ConvertFlagsToSwitches()|. |
| 128 | void RemoveFlagsSwitches( |
| 129 | std::map<std::string, CommandLine::StringType>* switch_list); |
| 130 | |
[email protected] | cb93bf5 | 2013-02-20 01:20:00 | [diff] [blame] | 131 | // Reset all flags to the default state by clearing all flags. |
| 132 | void ResetAllFlags(PrefService* prefs); |
| 133 | |
[email protected] | a314ee5a | 2010-10-26 21:23:28 | [diff] [blame] | 134 | // Returns the value for the current platform. This is one of the values defined |
| 135 | // by the OS enum above. |
| 136 | // This is exposed only for testing. |
| 137 | int GetCurrentPlatform(); |
| 138 | |
[email protected] | 4bc5050c | 2010-11-18 17:55:54 | [diff] [blame] | 139 | // Sends UMA stats about experimental flag usage. This should be called once per |
| 140 | // startup. |
| 141 | void RecordUMAStatistics(const PrefService* prefs); |
| 142 | |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 143 | namespace testing { |
[email protected] | 83e9fa70 | 2013-02-25 19:30:44 | [diff] [blame] | 144 | |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 145 | // Clears internal global state, for unit tests. |
| 146 | void ClearState(); |
[email protected] | a314ee5a | 2010-10-26 21:23:28 | [diff] [blame] | 147 | |
| 148 | // Sets the list of experiments. Pass in NULL to use the default set. This does |
| 149 | // NOT take ownership of the supplied Experiments. |
| 150 | void SetExperiments(const Experiment* e, size_t count); |
[email protected] | 8a6ff28d | 2010-12-02 16:35:19 | [diff] [blame] | 151 | |
| 152 | // Returns the current set of experiments. |
| 153 | const Experiment* GetExperiments(size_t* count); |
| 154 | |
| 155 | // Separator used for multi values. Multi values are represented in prefs as |
| 156 | // name-of-experiment + kMultiSeparator + selected_index. |
| 157 | extern const char kMultiSeparator[]; |
| 158 | |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 159 | } // namespace testing |
| 160 | |
[email protected] | 1a47d7e | 2010-10-15 00:37:24 | [diff] [blame] | 161 | } // namespace about_flags |
| 162 | |
| 163 | #endif // CHROME_BROWSER_ABOUT_FLAGS_H_ |