blob: 80ea2a72e3e218d53794a32f06ee1f114b3e6103 [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"
jkrcal1383d1d2016-06-17 12:40:569#include "base/strings/utf_string_conversions.h"
sdefresne246c5642015-11-16 21:47:2910#include "grit/components_strings.h"
11#include "ui/base/l10n/l10n_util.h"
12
13namespace flags_ui {
14
jkrcal1383d1d2016-06-17 12:40:5615std::string FeatureEntry::NameForOption(int index) const {
sdefresne246c5642015-11-16 21:47:2916 DCHECK(type == FeatureEntry::MULTI_VALUE ||
17 type == FeatureEntry::ENABLE_DISABLE_VALUE ||
jkrcal1383d1d2016-06-17 12:40:5618 type == FeatureEntry::FEATURE_VALUE ||
19 type == FeatureEntry::FEATURE_WITH_VARIATIONS_VALUE);
20 DCHECK_LT(index, num_options);
sdefresne246c5642015-11-16 21:47:2921 return std::string(internal_name) + testing::kMultiSeparator +
22 base::IntToString(index);
23}
24
jkrcal1383d1d2016-06-17 12:40:5625base::string16 FeatureEntry::DescriptionForOption(int index) const {
sdefresne246c5642015-11-16 21:47:2926 DCHECK(type == FeatureEntry::MULTI_VALUE ||
27 type == FeatureEntry::ENABLE_DISABLE_VALUE ||
jkrcal1383d1d2016-06-17 12:40:5628 type == FeatureEntry::FEATURE_VALUE ||
29 type == FeatureEntry::FEATURE_WITH_VARIATIONS_VALUE);
30 DCHECK_LT(index, num_options);
sdefresne246c5642015-11-16 21:47:2931 int description_id;
32 if (type == FeatureEntry::ENABLE_DISABLE_VALUE ||
33 type == FeatureEntry::FEATURE_VALUE) {
34 const int kEnableDisableDescriptionIds[] = {
35 IDS_GENERIC_EXPERIMENT_CHOICE_DEFAULT,
36 IDS_GENERIC_EXPERIMENT_CHOICE_ENABLED,
37 IDS_GENERIC_EXPERIMENT_CHOICE_DISABLED,
38 };
39 description_id = kEnableDisableDescriptionIds[index];
jkrcal1383d1d2016-06-17 12:40:5640 } else if (type == FeatureEntry::FEATURE_WITH_VARIATIONS_VALUE) {
41 if (index == 0) {
42 description_id = IDS_GENERIC_EXPERIMENT_CHOICE_DEFAULT;
43 } else if (index == 1) {
jkrcal1383d1d2016-06-17 12:40:5644 description_id = IDS_GENERIC_EXPERIMENT_CHOICE_ENABLED;
45 } else if (index < num_options - 1) {
jkrcal05f7ce32016-07-13 16:42:0946 // First two options do not have variations params.
47 int variation_index = index - 2;
jkrcal1383d1d2016-06-17 12:40:5648 return l10n_util::GetStringUTF16(IDS_GENERIC_EXPERIMENT_CHOICE_ENABLED) +
49 base::ASCIIToUTF16(" ") +
50 base::ASCIIToUTF16(
51 feature_variations[variation_index].description_text);
52 } else {
53 DCHECK_EQ(num_options - 1, index);
54 description_id = IDS_GENERIC_EXPERIMENT_CHOICE_DISABLED;
55 }
sdefresne246c5642015-11-16 21:47:2956 } else {
57 description_id = choices[index].description_id;
58 }
59 return l10n_util::GetStringUTF16(description_id);
60}
61
jkrcal1383d1d2016-06-17 12:40:5662const FeatureEntry::Choice& FeatureEntry::ChoiceForOption(int index) const {
63 DCHECK_EQ(FeatureEntry::MULTI_VALUE, type);
64 DCHECK_LT(index, num_options);
65
66 return choices[index];
67}
68
69FeatureEntry::FeatureState FeatureEntry::StateForOption(int index) const {
70 DCHECK(type == FeatureEntry::FEATURE_VALUE ||
71 type == FeatureEntry::FEATURE_WITH_VARIATIONS_VALUE);
72 DCHECK_LT(index, num_options);
73
74 if (index == 0)
75 return FeatureEntry::FeatureState::DEFAULT;
76 else if (index == num_options - 1)
77 return FeatureEntry::FeatureState::DISABLED;
78 else
79 return FeatureEntry::FeatureState::ENABLED;
80}
81
82const FeatureEntry::FeatureVariation* FeatureEntry::VariationForOption(
83 int index) const {
84 DCHECK(type == FeatureEntry::FEATURE_VALUE ||
85 type == FeatureEntry::FEATURE_WITH_VARIATIONS_VALUE);
86 DCHECK_LT(index, num_options);
87
jkrcal05f7ce32016-07-13 16:42:0988 if (type == FeatureEntry::FEATURE_WITH_VARIATIONS_VALUE && index > 1 &&
jkrcal1383d1d2016-06-17 12:40:5689 index < num_options - 1) {
90 // We have no variations for FEATURE_VALUE type. Option at |index|
jkrcal05f7ce32016-07-13 16:42:0991 // corresponds to variation at |index| - 2 as the list starts with "Default"
92 // and "Enabled" (with default parameters).
93 return &feature_variations[index - 2];
jkrcal1383d1d2016-06-17 12:40:5694 }
95 return nullptr;
96}
97
sdefresne246c5642015-11-16 21:47:2998namespace testing {
99
100// WARNING: '@' is also used in the html file. If you update this constant you
101// also need to update the html file.
102const char kMultiSeparator[] = "@";
103
104} // namespace testing
105
106} // namespace flags_ui