sdefresne | 246c564 | 2015-11-16 21:47:29 | [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 | #include "components/flags_ui/feature_entry.h" |
| 6 | |
| 7 | #include "base/logging.h" |
| 8 | #include "base/strings/string_number_conversions.h" |
Alexei Svitkine | ea9e3961 | 2018-11-14 21:28:03 | [diff] [blame] | 9 | #include "base/strings/string_util.h" |
jkrcal | 1383d1d | 2016-06-17 12:40:56 | [diff] [blame] | 10 | #include "base/strings/utf_string_conversions.h" |
sdefresne | 246c564 | 2015-11-16 21:47:29 | [diff] [blame] | 11 | #include "ui/base/l10n/l10n_util.h" |
| 12 | |
| 13 | namespace flags_ui { |
Alexei Svitkine | ea9e3961 | 2018-11-14 21:28:03 | [diff] [blame] | 14 | namespace { |
| 15 | |
| 16 | // WARNING: '@' is also used in the html file. If you update this constant you |
| 17 | // also need to update the html file. |
| 18 | const char kMultiSeparatorChar = '@'; |
| 19 | |
| 20 | } // namespace |
sdefresne | 246c564 | 2015-11-16 21:47:29 | [diff] [blame] | 21 | |
vabr | 0215a8e | 2017-03-28 12:47:34 | [diff] [blame] | 22 | const char kGenericExperimentChoiceDefault[] = "Default"; |
| 23 | const char kGenericExperimentChoiceEnabled[] = "Enabled"; |
| 24 | const char kGenericExperimentChoiceDisabled[] = "Disabled"; |
| 25 | const char kGenericExperimentChoiceAutomatic[] = "Automatic"; |
| 26 | |
Alexei Svitkine | ea9e3961 | 2018-11-14 21:28:03 | [diff] [blame] | 27 | bool FeatureEntry::InternalNameMatches(const std::string& name) const { |
| 28 | if (!base::StartsWith(name, internal_name, base::CompareCase::SENSITIVE)) |
| 29 | return false; |
| 30 | |
| 31 | const size_t internal_name_length = strlen(internal_name); |
| 32 | switch (type) { |
| 33 | case FeatureEntry::SINGLE_VALUE: |
| 34 | case FeatureEntry::SINGLE_DISABLE_VALUE: |
| 35 | case FeatureEntry::ORIGIN_LIST_VALUE: |
| 36 | return name.size() == internal_name_length; |
| 37 | |
| 38 | case FeatureEntry::MULTI_VALUE: |
| 39 | case FeatureEntry::ENABLE_DISABLE_VALUE: |
| 40 | case FeatureEntry::FEATURE_VALUE: |
| 41 | case FeatureEntry::FEATURE_WITH_PARAMS_VALUE: |
| 42 | // Check that the pattern matches what's produced by NameForOption(). |
| 43 | int index = -1; |
| 44 | return name.size() > internal_name_length + 1 && |
| 45 | name[internal_name_length] == kMultiSeparatorChar && |
| 46 | base::StringToInt(name.substr(internal_name_length + 1), &index) && |
| 47 | index >= 0 && index < num_options; |
| 48 | } |
| 49 | } |
| 50 | |
jkrcal | 1383d1d | 2016-06-17 12:40:56 | [diff] [blame] | 51 | std::string FeatureEntry::NameForOption(int index) const { |
sdefresne | 246c564 | 2015-11-16 21:47:29 | [diff] [blame] | 52 | DCHECK(type == FeatureEntry::MULTI_VALUE || |
| 53 | type == FeatureEntry::ENABLE_DISABLE_VALUE || |
jkrcal | 1383d1d | 2016-06-17 12:40:56 | [diff] [blame] | 54 | type == FeatureEntry::FEATURE_VALUE || |
jkrcal | 531f3675 | 2017-03-22 13:44:25 | [diff] [blame] | 55 | type == FeatureEntry::FEATURE_WITH_PARAMS_VALUE); |
jkrcal | 1383d1d | 2016-06-17 12:40:56 | [diff] [blame] | 56 | DCHECK_LT(index, num_options); |
sdefresne | 246c564 | 2015-11-16 21:47:29 | [diff] [blame] | 57 | return std::string(internal_name) + testing::kMultiSeparator + |
| 58 | base::IntToString(index); |
| 59 | } |
| 60 | |
jkrcal | 1383d1d | 2016-06-17 12:40:56 | [diff] [blame] | 61 | base::string16 FeatureEntry::DescriptionForOption(int index) const { |
sdefresne | 246c564 | 2015-11-16 21:47:29 | [diff] [blame] | 62 | DCHECK(type == FeatureEntry::MULTI_VALUE || |
| 63 | type == FeatureEntry::ENABLE_DISABLE_VALUE || |
jkrcal | 1383d1d | 2016-06-17 12:40:56 | [diff] [blame] | 64 | type == FeatureEntry::FEATURE_VALUE || |
jkrcal | 531f3675 | 2017-03-22 13:44:25 | [diff] [blame] | 65 | type == FeatureEntry::FEATURE_WITH_PARAMS_VALUE); |
jkrcal | 1383d1d | 2016-06-17 12:40:56 | [diff] [blame] | 66 | DCHECK_LT(index, num_options); |
vabr | 0215a8e | 2017-03-28 12:47:34 | [diff] [blame] | 67 | const char* description = nullptr; |
sdefresne | 246c564 | 2015-11-16 21:47:29 | [diff] [blame] | 68 | if (type == FeatureEntry::ENABLE_DISABLE_VALUE || |
| 69 | type == FeatureEntry::FEATURE_VALUE) { |
vabr | 0215a8e | 2017-03-28 12:47:34 | [diff] [blame] | 70 | const char* kEnableDisableDescriptions[] = { |
| 71 | kGenericExperimentChoiceDefault, kGenericExperimentChoiceEnabled, |
| 72 | kGenericExperimentChoiceDisabled, |
sdefresne | 246c564 | 2015-11-16 21:47:29 | [diff] [blame] | 73 | }; |
vabr | 0215a8e | 2017-03-28 12:47:34 | [diff] [blame] | 74 | description = kEnableDisableDescriptions[index]; |
jkrcal | 531f3675 | 2017-03-22 13:44:25 | [diff] [blame] | 75 | } else if (type == FeatureEntry::FEATURE_WITH_PARAMS_VALUE) { |
jkrcal | 1383d1d | 2016-06-17 12:40:56 | [diff] [blame] | 76 | if (index == 0) { |
vabr | 0215a8e | 2017-03-28 12:47:34 | [diff] [blame] | 77 | description = kGenericExperimentChoiceDefault; |
jkrcal | 1383d1d | 2016-06-17 12:40:56 | [diff] [blame] | 78 | } else if (index == 1) { |
vabr | 0215a8e | 2017-03-28 12:47:34 | [diff] [blame] | 79 | description = kGenericExperimentChoiceEnabled; |
jkrcal | 1383d1d | 2016-06-17 12:40:56 | [diff] [blame] | 80 | } else if (index < num_options - 1) { |
jkrcal | 05f7ce3 | 2016-07-13 16:42:09 | [diff] [blame] | 81 | // First two options do not have variations params. |
| 82 | int variation_index = index - 2; |
vabr | 0215a8e | 2017-03-28 12:47:34 | [diff] [blame] | 83 | return base::ASCIIToUTF16( |
| 84 | base::StringPiece(kGenericExperimentChoiceEnabled)) + |
jkrcal | 1383d1d | 2016-06-17 12:40:56 | [diff] [blame] | 85 | base::ASCIIToUTF16(" ") + |
| 86 | base::ASCIIToUTF16( |
| 87 | feature_variations[variation_index].description_text); |
| 88 | } else { |
| 89 | DCHECK_EQ(num_options - 1, index); |
vabr | 0215a8e | 2017-03-28 12:47:34 | [diff] [blame] | 90 | description = kGenericExperimentChoiceDisabled; |
jkrcal | 1383d1d | 2016-06-17 12:40:56 | [diff] [blame] | 91 | } |
sdefresne | 246c564 | 2015-11-16 21:47:29 | [diff] [blame] | 92 | } else { |
vabr | 0215a8e | 2017-03-28 12:47:34 | [diff] [blame] | 93 | description = choices[index].description; |
sdefresne | 246c564 | 2015-11-16 21:47:29 | [diff] [blame] | 94 | } |
vabr | 0215a8e | 2017-03-28 12:47:34 | [diff] [blame] | 95 | return base::ASCIIToUTF16(base::StringPiece(description)); |
sdefresne | 246c564 | 2015-11-16 21:47:29 | [diff] [blame] | 96 | } |
| 97 | |
jkrcal | 1383d1d | 2016-06-17 12:40:56 | [diff] [blame] | 98 | const FeatureEntry::Choice& FeatureEntry::ChoiceForOption(int index) const { |
| 99 | DCHECK_EQ(FeatureEntry::MULTI_VALUE, type); |
| 100 | DCHECK_LT(index, num_options); |
| 101 | |
| 102 | return choices[index]; |
| 103 | } |
| 104 | |
| 105 | FeatureEntry::FeatureState FeatureEntry::StateForOption(int index) const { |
| 106 | DCHECK(type == FeatureEntry::FEATURE_VALUE || |
jkrcal | 531f3675 | 2017-03-22 13:44:25 | [diff] [blame] | 107 | type == FeatureEntry::FEATURE_WITH_PARAMS_VALUE); |
jkrcal | 1383d1d | 2016-06-17 12:40:56 | [diff] [blame] | 108 | DCHECK_LT(index, num_options); |
| 109 | |
| 110 | if (index == 0) |
| 111 | return FeatureEntry::FeatureState::DEFAULT; |
Lei Zhang | 6779c82 | 2017-11-28 01:00:18 | [diff] [blame] | 112 | if (index == num_options - 1) |
jkrcal | 1383d1d | 2016-06-17 12:40:56 | [diff] [blame] | 113 | return FeatureEntry::FeatureState::DISABLED; |
Lei Zhang | 6779c82 | 2017-11-28 01:00:18 | [diff] [blame] | 114 | return FeatureEntry::FeatureState::ENABLED; |
jkrcal | 1383d1d | 2016-06-17 12:40:56 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | const FeatureEntry::FeatureVariation* FeatureEntry::VariationForOption( |
| 118 | int index) const { |
| 119 | DCHECK(type == FeatureEntry::FEATURE_VALUE || |
jkrcal | 531f3675 | 2017-03-22 13:44:25 | [diff] [blame] | 120 | type == FeatureEntry::FEATURE_WITH_PARAMS_VALUE); |
jkrcal | 1383d1d | 2016-06-17 12:40:56 | [diff] [blame] | 121 | DCHECK_LT(index, num_options); |
| 122 | |
jkrcal | 531f3675 | 2017-03-22 13:44:25 | [diff] [blame] | 123 | if (type == FeatureEntry::FEATURE_WITH_PARAMS_VALUE && index > 1 && |
jkrcal | 1383d1d | 2016-06-17 12:40:56 | [diff] [blame] | 124 | index < num_options - 1) { |
| 125 | // We have no variations for FEATURE_VALUE type. Option at |index| |
jkrcal | 05f7ce3 | 2016-07-13 16:42:09 | [diff] [blame] | 126 | // corresponds to variation at |index| - 2 as the list starts with "Default" |
| 127 | // and "Enabled" (with default parameters). |
| 128 | return &feature_variations[index - 2]; |
jkrcal | 1383d1d | 2016-06-17 12:40:56 | [diff] [blame] | 129 | } |
| 130 | return nullptr; |
| 131 | } |
| 132 | |
sdefresne | 246c564 | 2015-11-16 21:47:29 | [diff] [blame] | 133 | namespace testing { |
| 134 | |
Alexei Svitkine | ea9e3961 | 2018-11-14 21:28:03 | [diff] [blame] | 135 | const char kMultiSeparator[] = {kMultiSeparatorChar, '\0'}; |
sdefresne | 246c564 | 2015-11-16 21:47:29 | [diff] [blame] | 136 | |
| 137 | } // namespace testing |
| 138 | |
| 139 | } // namespace flags_ui |