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 5 #include "extensions/common/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 "extensions/common/switches.h" 12 13 namespace extensions { 14 15 namespace { 16 17 class CommonSwitches { 18 public: 19 CommonSwitches() 20 : easy_off_store_install( 21 NULL, 22 FeatureSwitch::DEFAULT_DISABLED), 23 force_dev_mode_highlighting( 24 switches::kForceDevModeHighlighting, 25 FeatureSwitch::DEFAULT_DISABLED), 26 global_commands( 27 switches::kGlobalCommands, 28 #if defined(OS_CHROMEOS) 29 FeatureSwitch::DEFAULT_DISABLED), 30 #else 31 FeatureSwitch::DEFAULT_ENABLED), 32 #endif 33 prompt_for_external_extensions( 34 NULL, 35 #if defined(OS_WIN) 36 FeatureSwitch::DEFAULT_ENABLED), 37 #else 38 FeatureSwitch::DEFAULT_DISABLED), 39 #endif 40 error_console( 41 switches::kErrorConsole, 42 FeatureSwitch::DEFAULT_DISABLED), 43 enable_override_bookmarks_ui( 44 switches::kEnableOverrideBookmarksUI, 45 FeatureSwitch::DEFAULT_DISABLED), 46 scripts_require_action(switches::kScriptsRequireAction, 47 FeatureSwitch::DEFAULT_DISABLED) {} 48 49 // Enables extensions to be easily installed from sites other than the web 50 // store. 51 FeatureSwitch easy_off_store_install; 52 53 FeatureSwitch force_dev_mode_highlighting; 54 FeatureSwitch global_commands; 55 56 // Should we prompt the user before allowing external extensions to install? 57 // Default is yes. 58 FeatureSwitch prompt_for_external_extensions; 59 60 FeatureSwitch error_console; 61 FeatureSwitch enable_override_bookmarks_ui; 62 FeatureSwitch scripts_require_action; 63 }; 64 65 base::LazyInstance<CommonSwitches> g_common_switches = 66 LAZY_INSTANCE_INITIALIZER; 67 68 } // namespace 69 70 FeatureSwitch* FeatureSwitch::force_dev_mode_highlighting() { 71 return &g_common_switches.Get().force_dev_mode_highlighting; 72 } 73 FeatureSwitch* FeatureSwitch::easy_off_store_install() { 74 return &g_common_switches.Get().easy_off_store_install; 75 } 76 FeatureSwitch* FeatureSwitch::global_commands() { 77 return &g_common_switches.Get().global_commands; 78 } 79 FeatureSwitch* FeatureSwitch::prompt_for_external_extensions() { 80 return &g_common_switches.Get().prompt_for_external_extensions; 81 } 82 FeatureSwitch* FeatureSwitch::error_console() { 83 return &g_common_switches.Get().error_console; 84 } 85 FeatureSwitch* FeatureSwitch::enable_override_bookmarks_ui() { 86 return &g_common_switches.Get().enable_override_bookmarks_ui; 87 } 88 89 FeatureSwitch* FeatureSwitch::scripts_require_action() { 90 return &g_common_switches.Get().scripts_require_action; 91 } 92 93 FeatureSwitch::ScopedOverride::ScopedOverride(FeatureSwitch* feature, 94 bool override_value) 95 : feature_(feature), 96 previous_value_(feature->GetOverrideValue()) { 97 feature_->SetOverrideValue( 98 override_value ? OVERRIDE_ENABLED : OVERRIDE_DISABLED); 99 } 100 101 FeatureSwitch::ScopedOverride::~ScopedOverride() { 102 feature_->SetOverrideValue(previous_value_); 103 } 104 105 FeatureSwitch::FeatureSwitch(const char* switch_name, 106 DefaultValue default_value) { 107 Init(CommandLine::ForCurrentProcess(), switch_name, default_value); 108 } 109 110 FeatureSwitch::FeatureSwitch(const CommandLine* command_line, 111 const char* switch_name, 112 DefaultValue default_value) { 113 Init(command_line, switch_name, default_value); 114 } 115 116 void FeatureSwitch::Init(const CommandLine* command_line, 117 const char* switch_name, 118 DefaultValue default_value) { 119 command_line_ = command_line; 120 switch_name_ = switch_name; 121 default_value_ = default_value == DEFAULT_ENABLED; 122 override_value_ = OVERRIDE_NONE; 123 } 124 125 bool FeatureSwitch::IsEnabled() const { 126 if (override_value_ != OVERRIDE_NONE) 127 return override_value_ == OVERRIDE_ENABLED; 128 129 if (!switch_name_) 130 return default_value_; 131 132 std::string temp = command_line_->GetSwitchValueASCII(switch_name_); 133 std::string switch_value; 134 base::TrimWhitespaceASCII(temp, base::TRIM_ALL, &switch_value); 135 136 if (switch_value == "1") 137 return true; 138 139 if (switch_value == "0") 140 return false; 141 142 if (!default_value_ && command_line_->HasSwitch(GetLegacyEnableFlag())) 143 return true; 144 145 if (default_value_ && command_line_->HasSwitch(GetLegacyDisableFlag())) 146 return false; 147 148 return default_value_; 149 } 150 151 std::string FeatureSwitch::GetLegacyEnableFlag() const { 152 DCHECK(switch_name_); 153 return std::string("enable-") + switch_name_; 154 } 155 156 std::string FeatureSwitch::GetLegacyDisableFlag() const { 157 DCHECK(switch_name_); 158 return std::string("disable-") + switch_name_; 159 } 160 161 void FeatureSwitch::SetOverrideValue(OverrideValue override_value) { 162 override_value_ = override_value; 163 } 164 165 FeatureSwitch::OverrideValue FeatureSwitch::GetOverrideValue() const { 166 return override_value_; 167 } 168 169 } // namespace extensions 170