1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
|
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/390223051): Remove C-library calls to fix the errors.
#pragma allow_unsafe_libc_calls
#endif
#include "components/webui/flags/feature_entry.h"
#include "base/check_op.h"
#include "base/logging.h"
#include "base/notreached.h"
#include "base/numerics/safe_conversions.h"
#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "ui/base/l10n/l10n_util.h"
namespace flags_ui {
// WARNING: '@' is also used in the html file. If you update this constant you
// also need to update the html file.
const char kMultiSeparatorChar = '@';
// These descriptions are translated for display in Chrome Labs. If these
// strings are changed the translated strings in Chrome Labs must also be
// changed (IDS_CHROMELABS_XXX).
const char kGenericExperimentChoiceDefault[] = "Default";
const char kGenericExperimentChoiceEnabled[] = "Enabled";
const char kGenericExperimentChoiceDisabled[] = "Disabled";
const char kGenericExperimentChoiceAutomatic[] = "Automatic";
bool FeatureEntry::InternalNameMatches(const std::string& name) const {
if (!base::StartsWith(name, internal_name, base::CompareCase::SENSITIVE)) {
return false;
}
const size_t internal_name_length = strlen(internal_name);
switch (type) {
case FeatureEntry::SINGLE_VALUE:
case FeatureEntry::SINGLE_DISABLE_VALUE:
case FeatureEntry::ORIGIN_LIST_VALUE:
case FeatureEntry::STRING_VALUE:
return name.size() == internal_name_length;
case FeatureEntry::MULTI_VALUE:
case FeatureEntry::ENABLE_DISABLE_VALUE:
case FeatureEntry::FEATURE_VALUE:
case FeatureEntry::FEATURE_WITH_PARAMS_VALUE:
#if BUILDFLAG(IS_CHROMEOS)
case FeatureEntry::PLATFORM_FEATURE_NAME_VALUE:
case FeatureEntry::PLATFORM_FEATURE_NAME_WITH_PARAMS_VALUE:
#endif // BUILDFLAG(IS_CHROMEOS)
// Check that the pattern matches what's produced by NameForOption().
int index = -1;
return name.size() > internal_name_length + 1 &&
name[internal_name_length] == kMultiSeparatorChar &&
base::StringToInt(name.substr(internal_name_length + 1), &index) &&
index >= 0 && index < NumOptions();
}
}
int FeatureEntry::NumOptions() const {
switch (type) {
case ENABLE_DISABLE_VALUE:
case FEATURE_VALUE:
#if BUILDFLAG(IS_CHROMEOS)
case PLATFORM_FEATURE_NAME_VALUE:
#endif // BUILDFLAG(IS_CHROMEOS)
return 3;
case MULTI_VALUE:
return choices.size();
case FEATURE_WITH_PARAMS_VALUE:
#if BUILDFLAG(IS_CHROMEOS)
case PLATFORM_FEATURE_NAME_WITH_PARAMS_VALUE:
#endif // BUILDFLAG(IS_CHROMEOS)
return 3 + GetVariations().size();
default:
return 0;
}
}
std::string FeatureEntry::NameForOption(int index) const {
DCHECK(type == FeatureEntry::MULTI_VALUE ||
type == FeatureEntry::ENABLE_DISABLE_VALUE ||
type == FeatureEntry::FEATURE_VALUE ||
type == FeatureEntry::FEATURE_WITH_PARAMS_VALUE
#if BUILDFLAG(IS_CHROMEOS)
|| type == FeatureEntry::PLATFORM_FEATURE_NAME_VALUE ||
type == FeatureEntry::PLATFORM_FEATURE_NAME_WITH_PARAMS_VALUE
#endif // BUILDFLAG(IS_CHROMEOS)
);
DCHECK_LT(index, NumOptions());
return std::string(internal_name) + testing::kMultiSeparator +
base::NumberToString(index);
}
// The order in which these descriptions are returned is the same in the
// LabsComboboxModel::GetItemAt(..) (in the chrome_labs_item_view.cc file) for
// the translated version of these strings. If there are changes to this, the
// same changes must be made in LabsComboboxModel
std::u16string FeatureEntry::DescriptionForOption(int index) const {
DCHECK(type == FeatureEntry::MULTI_VALUE ||
type == FeatureEntry::ENABLE_DISABLE_VALUE ||
type == FeatureEntry::FEATURE_VALUE ||
type == FeatureEntry::FEATURE_WITH_PARAMS_VALUE
#if BUILDFLAG(IS_CHROMEOS)
|| type == FeatureEntry::PLATFORM_FEATURE_NAME_VALUE ||
type == FeatureEntry::PLATFORM_FEATURE_NAME_WITH_PARAMS_VALUE
#endif // BUILDFLAG(IS_CHROMEOS)
);
DCHECK_LT(index, NumOptions());
const auto index_s = base::checked_cast<size_t>(index);
const char* description = nullptr;
if (type == FeatureEntry::ENABLE_DISABLE_VALUE ||
type == FeatureEntry::FEATURE_VALUE
#if BUILDFLAG(IS_CHROMEOS)
|| type == FeatureEntry::PLATFORM_FEATURE_NAME_VALUE
#endif
) {
const auto kEnableDisableDescriptions = std::to_array<const char*>(
{kGenericExperimentChoiceDefault, kGenericExperimentChoiceEnabled,
kGenericExperimentChoiceDisabled});
description = kEnableDisableDescriptions[index_s];
} else if (type == FeatureEntry::FEATURE_WITH_PARAMS_VALUE
#if BUILDFLAG(IS_CHROMEOS)
|| type == FeatureEntry::PLATFORM_FEATURE_NAME_WITH_PARAMS_VALUE
#endif
) {
if (index == 0) {
description = kGenericExperimentChoiceDefault;
} else if (index == 1) {
description = kGenericExperimentChoiceEnabled;
} else if (index < NumOptions() - 1) {
// First two options do not have variations params.
return base::ASCIIToUTF16(
base::StrCat({kGenericExperimentChoiceEnabled, " ",
GetVariations()[index_s - 2].description_text}));
} else {
DCHECK_EQ(NumOptions() - 1, index);
description = kGenericExperimentChoiceDisabled;
}
} else {
description = choices[index_s].description;
}
return base::ASCIIToUTF16(description);
}
const FeatureEntry::Choice& FeatureEntry::ChoiceForOption(int index) const {
DCHECK_EQ(FeatureEntry::MULTI_VALUE, type);
DCHECK_LT(index, NumOptions());
return choices[base::checked_cast<size_t>(index)];
}
FeatureEntry::FeatureState FeatureEntry::StateForOption(int index) const {
DCHECK(type == FeatureEntry::FEATURE_VALUE ||
type == FeatureEntry::FEATURE_WITH_PARAMS_VALUE
#if BUILDFLAG(IS_CHROMEOS)
|| type == FeatureEntry::PLATFORM_FEATURE_NAME_VALUE ||
type == FeatureEntry::PLATFORM_FEATURE_NAME_WITH_PARAMS_VALUE
#endif
);
DCHECK_LT(index, NumOptions());
if (index == 0) {
return FeatureEntry::FeatureState::DEFAULT;
}
return (index == NumOptions() - 1) ? FeatureEntry::FeatureState::DISABLED
: FeatureEntry::FeatureState::ENABLED;
}
const FeatureEntry::FeatureVariation* FeatureEntry::VariationForOption(
int index) const {
DCHECK(type == FeatureEntry::FEATURE_VALUE ||
type == FeatureEntry::FEATURE_WITH_PARAMS_VALUE
#if BUILDFLAG(IS_CHROMEOS)
|| type == FeatureEntry::PLATFORM_FEATURE_NAME_VALUE ||
type == FeatureEntry::PLATFORM_FEATURE_NAME_WITH_PARAMS_VALUE
#endif
);
DCHECK_LT(index, NumOptions());
if ((type == FeatureEntry::FEATURE_WITH_PARAMS_VALUE
#if BUILDFLAG(IS_CHROMEOS)
|| type == FeatureEntry::PLATFORM_FEATURE_NAME_WITH_PARAMS_VALUE
#endif
) &&
index > 1 && index < NumOptions() - 1) {
// We have no variations for FEATURE_VALUE type or
// PLATFORM_FEATURE_NAME_VALUE type. Option at |index| corresponds to
// variation at |index| - 2 as the list starts with "Default" and "Enabled"
// (with default parameters).
return &GetVariations()[static_cast<size_t>(index - 2)];
}
return nullptr;
}
bool FeatureEntry::IsValid() const {
switch (type) {
case FeatureEntry::SINGLE_VALUE:
case FeatureEntry::SINGLE_DISABLE_VALUE:
case FeatureEntry::ORIGIN_LIST_VALUE:
case FeatureEntry::STRING_VALUE:
return true;
case FeatureEntry::MULTI_VALUE:
if (choices.size() == 0) {
LOG(ERROR) << "no choice is found";
return false;
}
if (!ChoiceForOption(0).command_line_switch) {
LOG(ERROR) << "command_line_swtich is null";
return false;
}
if (ChoiceForOption(0).command_line_switch[0] != '\0') {
LOG(ERROR) << "The command line value of the first item must be empty";
return false;
}
return true;
case FeatureEntry::ENABLE_DISABLE_VALUE:
if (!switches.command_line_switch) {
LOG(ERROR) << "command_line_switch is null";
return false;
}
if (!switches.command_line_value) {
LOG(ERROR) << "command_line_value is null";
return false;
}
if (!switches.disable_command_line_switch) {
LOG(ERROR) << "disable_command_line_switch is null";
return false;
}
if (!switches.disable_command_line_value) {
LOG(ERROR) << "disable_command_line_value is null";
return false;
}
return true;
case FeatureEntry::FEATURE_VALUE:
if (!feature.feature) {
LOG(ERROR) << "no feature is set";
return false;
}
return true;
case FeatureEntry::FEATURE_WITH_PARAMS_VALUE:
if (!feature.feature) {
LOG(ERROR) << "no feature is set";
return false;
}
if (feature.feature_variations.size() == 0) {
LOG(ERROR) << "feature_variations is empty";
return false;
}
if (!feature.feature_trial_name) {
LOG(ERROR) << "feature_trial_name is null";
return false;
}
return true;
#if BUILDFLAG(IS_CHROMEOS)
case FeatureEntry::PLATFORM_FEATURE_NAME_VALUE:
if (!platform_feature_name.name) {
LOG(ERROR) << "no feature name is set";
return false;
}
#if BUILDFLAG(ENABLE_BANNED_BASE_FEATURE_PREFIX)
if (!base::StartsWith(platform_feature_name.name,
BUILDFLAG(BANNED_BASE_FEATURE_PREFIX))) {
LOG(ERROR) << "missing required feature name prefix, please check "
"BANNED_BASE_FEATURE_PREFIX";
return false;
}
#endif // BUILDFLAG(ENABLED_BANNED_BASE_FEATURE_PREFIX)
return true;
case FeatureEntry::PLATFORM_FEATURE_NAME_WITH_PARAMS_VALUE:
if (!platform_feature_name.name) {
LOG(ERROR) << "no feature name is set";
return false;
}
if (platform_feature_name.feature_variations.size() == 0) {
LOG(ERROR) << "feature_variations is empty";
return false;
}
if (!platform_feature_name.feature_trial_name) {
LOG(ERROR) << "feature_trial_name is null";
return false;
}
#if BUILDFLAG(ENABLE_BANNED_BASE_FEATURE_PREFIX)
if (!base::StartsWith(platform_feature_name.name,
BUILDFLAG(BANNED_BASE_FEATURE_PREFIX))) {
LOG(ERROR) << "missing required feature name prefix, please check "
"BANNED_BASE_FEATURE_PREFIX";
return false;
}
#endif // BUILDFLAG(ENABLED_BANNED_BASE_FEATURE_PREFIX)
return true;
#endif // BUILDFLAG(IS_CHROMEOS)
}
NOTREACHED();
}
const base::span<const FeatureEntry::FeatureVariation>
FeatureEntry::GetVariations() const {
if (type == FeatureEntry::FEATURE_WITH_PARAMS_VALUE) {
return feature.feature_variations;
}
#if BUILDFLAG(IS_CHROMEOS)
if (type == FeatureEntry::PLATFORM_FEATURE_NAME_WITH_PARAMS_VALUE) {
return platform_feature_name.feature_variations;
}
#endif
NOTREACHED();
}
namespace testing {
const char kMultiSeparator[] = {kMultiSeparatorChar, '\0'};
} // namespace testing
} // namespace flags_ui
|