feature_switch.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // Copyright 2013 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 "extensions/common/feature_switch.h"
  5. #include "base/command_line.h"
  6. #include "base/lazy_instance.h"
  7. #include "base/strings/string_util.h"
  8. #include "build/build_config.h"
  9. #include "extensions/common/switches.h"
  10. namespace extensions {
  11. namespace {
  12. class CommonSwitches {
  13. public:
  14. CommonSwitches()
  15. : force_dev_mode_highlighting(switches::kForceDevModeHighlighting,
  16. FeatureSwitch::DEFAULT_DISABLED),
  17. // Intentionally no flag since turning this off outside of tests
  18. // is a security risk.
  19. prompt_for_external_extensions(nullptr,
  20. #if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC)
  21. FeatureSwitch::DEFAULT_ENABLED),
  22. #else
  23. FeatureSwitch::DEFAULT_DISABLED),
  24. #endif
  25. embedded_extension_options(switches::kEmbeddedExtensionOptions,
  26. FeatureSwitch::DEFAULT_DISABLED),
  27. trace_app_source(switches::kTraceAppSource,
  28. FeatureSwitch::DEFAULT_ENABLED) {
  29. }
  30. FeatureSwitch force_dev_mode_highlighting;
  31. // Should we prompt the user before allowing external extensions to install?
  32. // Default is yes.
  33. FeatureSwitch prompt_for_external_extensions;
  34. FeatureSwitch embedded_extension_options;
  35. FeatureSwitch trace_app_source;
  36. };
  37. base::LazyInstance<CommonSwitches>::DestructorAtExit g_common_switches =
  38. LAZY_INSTANCE_INITIALIZER;
  39. } // namespace
  40. FeatureSwitch* FeatureSwitch::force_dev_mode_highlighting() {
  41. return &g_common_switches.Get().force_dev_mode_highlighting;
  42. }
  43. FeatureSwitch* FeatureSwitch::prompt_for_external_extensions() {
  44. return &g_common_switches.Get().prompt_for_external_extensions;
  45. }
  46. FeatureSwitch* FeatureSwitch::embedded_extension_options() {
  47. return &g_common_switches.Get().embedded_extension_options;
  48. }
  49. FeatureSwitch* FeatureSwitch::trace_app_source() {
  50. return &g_common_switches.Get().trace_app_source;
  51. }
  52. FeatureSwitch::ScopedOverride::ScopedOverride(FeatureSwitch* feature,
  53. bool override_value)
  54. : feature_(feature), previous_value_(feature->GetOverrideValue()) {
  55. feature_->SetOverrideValue(override_value ? OVERRIDE_ENABLED
  56. : OVERRIDE_DISABLED);
  57. }
  58. FeatureSwitch::ScopedOverride::~ScopedOverride() {
  59. feature_->SetOverrideValue(previous_value_);
  60. }
  61. FeatureSwitch::FeatureSwitch(const char* switch_name,
  62. DefaultValue default_value)
  63. : FeatureSwitch(base::CommandLine::ForCurrentProcess(),
  64. switch_name,
  65. default_value) {}
  66. FeatureSwitch::FeatureSwitch(const base::CommandLine* command_line,
  67. const char* switch_name,
  68. DefaultValue default_value)
  69. : command_line_(command_line),
  70. switch_name_(switch_name),
  71. default_value_(default_value == DEFAULT_ENABLED),
  72. override_value_(OVERRIDE_NONE) {}
  73. FeatureSwitch::~FeatureSwitch() = default;
  74. bool FeatureSwitch::IsEnabled() const {
  75. if (override_value_ != OVERRIDE_NONE)
  76. return override_value_ == OVERRIDE_ENABLED;
  77. if (!cached_value_.has_value())
  78. cached_value_ = ComputeValue();
  79. return cached_value_.value();
  80. }
  81. bool FeatureSwitch::ComputeValue() const {
  82. if (!switch_name_)
  83. return default_value_;
  84. std::string temp = command_line_->GetSwitchValueASCII(switch_name_);
  85. std::string switch_value;
  86. base::TrimWhitespaceASCII(temp, base::TRIM_ALL, &switch_value);
  87. if (switch_value == "1")
  88. return true;
  89. if (switch_value == "0")
  90. return false;
  91. if (command_line_->HasSwitch(GetLegacyEnableFlag()))
  92. return true;
  93. if (command_line_->HasSwitch(GetLegacyDisableFlag()))
  94. return false;
  95. return default_value_;
  96. }
  97. bool FeatureSwitch::HasValue() const {
  98. return override_value_ != OVERRIDE_NONE ||
  99. command_line_->HasSwitch(switch_name_) ||
  100. command_line_->HasSwitch(GetLegacyEnableFlag()) ||
  101. command_line_->HasSwitch(GetLegacyDisableFlag());
  102. }
  103. std::string FeatureSwitch::GetLegacyEnableFlag() const {
  104. DCHECK(switch_name_);
  105. return std::string("enable-") + switch_name_;
  106. }
  107. std::string FeatureSwitch::GetLegacyDisableFlag() const {
  108. DCHECK(switch_name_);
  109. return std::string("disable-") + switch_name_;
  110. }
  111. void FeatureSwitch::SetOverrideValue(OverrideValue override_value) {
  112. override_value_ = override_value;
  113. }
  114. FeatureSwitch::OverrideValue FeatureSwitch::GetOverrideValue() const {
  115. return override_value_;
  116. }
  117. } // namespace extensions