1 // Copyright (c) 2012 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 "chrome/common/extensions/feature_switch.h" 6 7 #include "base/command_line.h" 8 #include "base/lazy_instance.h" 9 #include "base/metrics/field_trial.h" 10 #include "base/strings/string_util.h" 11 #include "chrome/common/chrome_switches.h" 12 13 namespace extensions { 14 15 namespace { 16 17 class CommonSwitches { 18 public: 19 CommonSwitches() 20 : easy_off_store_install( 21 switches::kEasyOffStoreExtensionInstall, 22 FeatureSwitch::DEFAULT_DISABLED), 23 script_badges( 24 switches::kScriptBadges, 25 FeatureSwitch::DEFAULT_DISABLED), 26 script_bubble( 27 switches::kScriptBubble, 28 FeatureSwitch::DEFAULT_DISABLED), 29 prompt_for_external_extensions( 30 switches::kPromptForExternalExtensions, 31 #if defined(OS_WIN) 32 FeatureSwitch::DEFAULT_ENABLED) {} 33 #else 34 FeatureSwitch::DEFAULT_DISABLED) {} 35 #endif 36 37 FeatureSwitch easy_off_store_install; 38 FeatureSwitch script_badges; 39 FeatureSwitch script_bubble; 40 FeatureSwitch prompt_for_external_extensions; 41 }; 42 43 base::LazyInstance<CommonSwitches> g_common_switches = 44 LAZY_INSTANCE_INITIALIZER; 45 46 } // namespace 47 48 FeatureSwitch* FeatureSwitch::easy_off_store_install() { 49 return &g_common_switches.Get().easy_off_store_install; 50 } 51 FeatureSwitch* FeatureSwitch::script_badges() { 52 return &g_common_switches.Get().script_badges; 53 } 54 FeatureSwitch* FeatureSwitch::script_bubble() { 55 return &g_common_switches.Get().script_bubble; 56 } 57 FeatureSwitch* FeatureSwitch::prompt_for_external_extensions() { 58 return &g_common_switches.Get().prompt_for_external_extensions; 59 } 60 61 FeatureSwitch::ScopedOverride::ScopedOverride(FeatureSwitch* feature, 62 bool override_value) 63 : feature_(feature), 64 previous_value_(feature->GetOverrideValue()) { 65 feature_->SetOverrideValue( 66 override_value ? OVERRIDE_ENABLED : OVERRIDE_DISABLED); 67 } 68 69 FeatureSwitch::ScopedOverride::~ScopedOverride() { 70 feature_->SetOverrideValue(previous_value_); 71 } 72 73 FeatureSwitch::FeatureSwitch(const char* switch_name, 74 DefaultValue default_value) { 75 Init(CommandLine::ForCurrentProcess(), switch_name, default_value); 76 } 77 78 FeatureSwitch::FeatureSwitch(const CommandLine* command_line, 79 const char* switch_name, 80 DefaultValue default_value) { 81 Init(command_line, switch_name, default_value); 82 } 83 84 void FeatureSwitch::Init(const CommandLine* command_line, 85 const char* switch_name, 86 DefaultValue default_value) { 87 command_line_ = command_line; 88 switch_name_ = switch_name; 89 default_value_ = default_value == DEFAULT_ENABLED; 90 override_value_ = OVERRIDE_NONE; 91 } 92 93 bool FeatureSwitch::IsEnabled() const { 94 if (override_value_ != OVERRIDE_NONE) 95 return override_value_ == OVERRIDE_ENABLED; 96 97 std::string temp = command_line_->GetSwitchValueASCII(switch_name_); 98 std::string switch_value; 99 TrimWhitespaceASCII(temp, TRIM_ALL, &switch_value); 100 101 if (switch_value == "1") 102 return true; 103 104 if (switch_value == "0") 105 return false; 106 107 if (!default_value_ && command_line_->HasSwitch(GetLegacyEnableFlag())) 108 return true; 109 110 if (default_value_ && command_line_->HasSwitch(GetLegacyDisableFlag())) 111 return false; 112 113 return default_value_; 114 } 115 116 std::string FeatureSwitch::GetLegacyEnableFlag() const { 117 return std::string("enable-") + switch_name_; 118 } 119 120 std::string FeatureSwitch::GetLegacyDisableFlag() const { 121 return std::string("disable-") + switch_name_; 122 } 123 124 void FeatureSwitch::SetOverrideValue(OverrideValue override_value) { 125 override_value_ = override_value; 126 } 127 128 FeatureSwitch::OverrideValue FeatureSwitch::GetOverrideValue() const { 129 return override_value_; 130 } 131 132 } // namespace extensions 133