blob: e1d703fc00bd51e498ad3a4e6818ef3d299ec4c6 [file] [log] [blame]
sdefresne246c5642015-11-16 21:47:291// 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 Svitkineea9e39612018-11-14 21:28:039#include "base/strings/string_util.h"
jkrcal1383d1d2016-06-17 12:40:5610#include "base/strings/utf_string_conversions.h"
sdefresne246c5642015-11-16 21:47:2911#include "ui/base/l10n/l10n_util.h"
12
13namespace flags_ui {
Alexei Svitkineea9e39612018-11-14 21:28:0314namespace {
15
16// WARNING: '@' is also used in the html file. If you update this constant you
17// also need to update the html file.
18const char kMultiSeparatorChar = '@';
19
20} // namespace
sdefresne246c5642015-11-16 21:47:2921
vabr0215a8e2017-03-28 12:47:3422const char kGenericExperimentChoiceDefault[] = "Default";
23const char kGenericExperimentChoiceEnabled[] = "Enabled";
24const char kGenericExperimentChoiceDisabled[] = "Disabled";
25const char kGenericExperimentChoiceAutomatic[] = "Automatic";
26
Alexei Svitkineea9e39612018-11-14 21:28:0327bool 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
jkrcal1383d1d2016-06-17 12:40:5651std::string FeatureEntry::NameForOption(int index) const {
sdefresne246c5642015-11-16 21:47:2952 DCHECK(type == FeatureEntry::MULTI_VALUE ||
53 type == FeatureEntry::ENABLE_DISABLE_VALUE ||
jkrcal1383d1d2016-06-17 12:40:5654 type == FeatureEntry::FEATURE_VALUE ||
jkrcal531f36752017-03-22 13:44:2555 type == FeatureEntry::FEATURE_WITH_PARAMS_VALUE);
jkrcal1383d1d2016-06-17 12:40:5656 DCHECK_LT(index, num_options);
sdefresne246c5642015-11-16 21:47:2957 return std::string(internal_name) + testing::kMultiSeparator +
58 base::IntToString(index);
59}
60
jkrcal1383d1d2016-06-17 12:40:5661base::string16 FeatureEntry::DescriptionForOption(int index) const {
sdefresne246c5642015-11-16 21:47:2962 DCHECK(type == FeatureEntry::MULTI_VALUE ||
63 type == FeatureEntry::ENABLE_DISABLE_VALUE ||
jkrcal1383d1d2016-06-17 12:40:5664 type == FeatureEntry::FEATURE_VALUE ||
jkrcal531f36752017-03-22 13:44:2565 type == FeatureEntry::FEATURE_WITH_PARAMS_VALUE);
jkrcal1383d1d2016-06-17 12:40:5666 DCHECK_LT(index, num_options);
vabr0215a8e2017-03-28 12:47:3467 const char* description = nullptr;
sdefresne246c5642015-11-16 21:47:2968 if (type == FeatureEntry::ENABLE_DISABLE_VALUE ||
69 type == FeatureEntry::FEATURE_VALUE) {
vabr0215a8e2017-03-28 12:47:3470 const char* kEnableDisableDescriptions[] = {
71 kGenericExperimentChoiceDefault, kGenericExperimentChoiceEnabled,
72 kGenericExperimentChoiceDisabled,
sdefresne246c5642015-11-16 21:47:2973 };
vabr0215a8e2017-03-28 12:47:3474 description = kEnableDisableDescriptions[index];
jkrcal531f36752017-03-22 13:44:2575 } else if (type == FeatureEntry::FEATURE_WITH_PARAMS_VALUE) {
jkrcal1383d1d2016-06-17 12:40:5676 if (index == 0) {
vabr0215a8e2017-03-28 12:47:3477 description = kGenericExperimentChoiceDefault;
jkrcal1383d1d2016-06-17 12:40:5678 } else if (index == 1) {
vabr0215a8e2017-03-28 12:47:3479 description = kGenericExperimentChoiceEnabled;
jkrcal1383d1d2016-06-17 12:40:5680 } else if (index < num_options - 1) {
jkrcal05f7ce32016-07-13 16:42:0981 // First two options do not have variations params.
82 int variation_index = index - 2;
vabr0215a8e2017-03-28 12:47:3483 return base::ASCIIToUTF16(
84 base::StringPiece(kGenericExperimentChoiceEnabled)) +
jkrcal1383d1d2016-06-17 12:40:5685 base::ASCIIToUTF16(" ") +
86 base::ASCIIToUTF16(
87 feature_variations[variation_index].description_text);
88 } else {
89 DCHECK_EQ(num_options - 1, index);
vabr0215a8e2017-03-28 12:47:3490 description = kGenericExperimentChoiceDisabled;
jkrcal1383d1d2016-06-17 12:40:5691 }
sdefresne246c5642015-11-16 21:47:2992 } else {
vabr0215a8e2017-03-28 12:47:3493 description = choices[index].description;
sdefresne246c5642015-11-16 21:47:2994 }
vabr0215a8e2017-03-28 12:47:3495 return base::ASCIIToUTF16(base::StringPiece(description));
sdefresne246c5642015-11-16 21:47:2996}
97
jkrcal1383d1d2016-06-17 12:40:5698const 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
105FeatureEntry::FeatureState FeatureEntry::StateForOption(int index) const {
106 DCHECK(type == FeatureEntry::FEATURE_VALUE ||
jkrcal531f36752017-03-22 13:44:25107 type == FeatureEntry::FEATURE_WITH_PARAMS_VALUE);
jkrcal1383d1d2016-06-17 12:40:56108 DCHECK_LT(index, num_options);
109
110 if (index == 0)
111 return FeatureEntry::FeatureState::DEFAULT;
Lei Zhang6779c822017-11-28 01:00:18112 if (index == num_options - 1)
jkrcal1383d1d2016-06-17 12:40:56113 return FeatureEntry::FeatureState::DISABLED;
Lei Zhang6779c822017-11-28 01:00:18114 return FeatureEntry::FeatureState::ENABLED;
jkrcal1383d1d2016-06-17 12:40:56115}
116
117const FeatureEntry::FeatureVariation* FeatureEntry::VariationForOption(
118 int index) const {
119 DCHECK(type == FeatureEntry::FEATURE_VALUE ||
jkrcal531f36752017-03-22 13:44:25120 type == FeatureEntry::FEATURE_WITH_PARAMS_VALUE);
jkrcal1383d1d2016-06-17 12:40:56121 DCHECK_LT(index, num_options);
122
jkrcal531f36752017-03-22 13:44:25123 if (type == FeatureEntry::FEATURE_WITH_PARAMS_VALUE && index > 1 &&
jkrcal1383d1d2016-06-17 12:40:56124 index < num_options - 1) {
125 // We have no variations for FEATURE_VALUE type. Option at |index|
jkrcal05f7ce32016-07-13 16:42:09126 // corresponds to variation at |index| - 2 as the list starts with "Default"
127 // and "Enabled" (with default parameters).
128 return &feature_variations[index - 2];
jkrcal1383d1d2016-06-17 12:40:56129 }
130 return nullptr;
131}
132
sdefresne246c5642015-11-16 21:47:29133namespace testing {
134
Alexei Svitkineea9e39612018-11-14 21:28:03135const char kMultiSeparator[] = {kMultiSeparatorChar, '\0'};
sdefresne246c5642015-11-16 21:47:29136
137} // namespace testing
138
139} // namespace flags_ui