scoped_add_feature_flags.cc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright 2019 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. #include "base/scoped_add_feature_flags.h"
  5. #include "base/base_switches.h"
  6. #include "base/command_line.h"
  7. #include "base/containers/contains.h"
  8. #include "base/strings/strcat.h"
  9. #include "base/strings/string_util.h"
  10. namespace base {
  11. ScopedAddFeatureFlags::ScopedAddFeatureFlags(CommandLine* command_line)
  12. : command_line_(command_line) {
  13. std::string enabled_features =
  14. command_line->GetSwitchValueASCII(switches::kEnableFeatures);
  15. std::string disabled_features =
  16. command_line->GetSwitchValueASCII(switches::kDisableFeatures);
  17. for (const StringPiece& feature :
  18. FeatureList::SplitFeatureListString(enabled_features)) {
  19. enabled_features_.emplace_back(feature);
  20. }
  21. for (const StringPiece& feature :
  22. FeatureList::SplitFeatureListString(disabled_features)) {
  23. disabled_features_.emplace_back(feature);
  24. }
  25. }
  26. ScopedAddFeatureFlags::~ScopedAddFeatureFlags() {
  27. command_line_->RemoveSwitch(switches::kEnableFeatures);
  28. command_line_->AppendSwitchASCII(switches::kEnableFeatures,
  29. JoinString(enabled_features_, ","));
  30. command_line_->RemoveSwitch(switches::kDisableFeatures);
  31. command_line_->AppendSwitchASCII(switches::kDisableFeatures,
  32. JoinString(disabled_features_, ","));
  33. }
  34. void ScopedAddFeatureFlags::EnableIfNotSet(const Feature& feature) {
  35. AddFeatureIfNotSet(feature, /*suffix=*/"", /*enable=*/true);
  36. }
  37. void ScopedAddFeatureFlags::EnableIfNotSetWithParameter(const Feature& feature,
  38. StringPiece name,
  39. StringPiece value) {
  40. std::string suffix = StrCat({":", name, "/", value});
  41. AddFeatureIfNotSet(feature, suffix, true /* enable */);
  42. }
  43. void ScopedAddFeatureFlags::DisableIfNotSet(const Feature& feature) {
  44. AddFeatureIfNotSet(feature, /*suffix=*/"", /*enable=*/false);
  45. }
  46. bool ScopedAddFeatureFlags::IsEnabled(const Feature& feature) {
  47. return IsEnabledWithParameter(feature, /*parameter_name=*/"",
  48. /*parameter_value=*/"");
  49. }
  50. bool ScopedAddFeatureFlags::IsEnabledWithParameter(
  51. const Feature& feature,
  52. StringPiece parameter_name,
  53. StringPiece parameter_value) {
  54. std::string feature_name = feature.name;
  55. if (!parameter_name.empty()) {
  56. StrAppend(&feature_name, {":", parameter_name, "/", parameter_value});
  57. }
  58. if (Contains(disabled_features_, feature_name))
  59. return false;
  60. if (Contains(enabled_features_, feature_name))
  61. return true;
  62. return feature.default_state == FEATURE_ENABLED_BY_DEFAULT;
  63. }
  64. void ScopedAddFeatureFlags::AddFeatureIfNotSet(const Feature& feature,
  65. StringPiece suffix,
  66. bool enable) {
  67. std::string feature_name = StrCat({feature.name, suffix});
  68. if (Contains(enabled_features_, feature_name) ||
  69. Contains(disabled_features_, feature_name)) {
  70. return;
  71. }
  72. if (enable) {
  73. enabled_features_.emplace_back(feature_name);
  74. } else {
  75. disabled_features_.emplace_back(feature_name);
  76. }
  77. }
  78. } // namespace base