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