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 switches::kEasyOffStoreExtensionInstall, 22 FeatureSwitch::DEFAULT_DISABLED), 23 force_dev_mode_highlighting( 24 switches::kForceDevModeHighlighting, 25 FeatureSwitch::DEFAULT_DISABLED), 26 global_commands( 27 switches::kGlobalCommands, 28 FeatureSwitch::DEFAULT_DISABLED), 29 script_badges( 30 switches::kScriptBadges, 31 FeatureSwitch::DEFAULT_DISABLED), 32 script_bubble( 33 switches::kScriptBubble, 34 FeatureSwitch::DEFAULT_DISABLED), 35 prompt_for_external_extensions( 36 switches::kPromptForExternalExtensions, 37 #if defined(OS_WIN) 38 FeatureSwitch::DEFAULT_ENABLED), 39 #else 40 FeatureSwitch::DEFAULT_DISABLED), 41 #endif 42 error_console( 43 switches::kErrorConsole, 44 FeatureSwitch::DEFAULT_DISABLED), 45 enable_override_bookmarks_ui( 46 switches::kEnableOverrideBookmarksUI, 47 FeatureSwitch::DEFAULT_DISABLED) {} 48 49 FeatureSwitch easy_off_store_install; 50 FeatureSwitch force_dev_mode_highlighting; 51 FeatureSwitch global_commands; 52 FeatureSwitch script_badges; 53 FeatureSwitch script_bubble; 54 FeatureSwitch prompt_for_external_extensions; 55 FeatureSwitch error_console; 56 FeatureSwitch enable_override_bookmarks_ui; 57 }; 58 59 base::LazyInstance<CommonSwitches> g_common_switches = 60 LAZY_INSTANCE_INITIALIZER; 61 62 } // namespace 63 64 FeatureSwitch* FeatureSwitch::force_dev_mode_highlighting() { 65 return &g_common_switches.Get().force_dev_mode_highlighting; 66 } 67 FeatureSwitch* FeatureSwitch::easy_off_store_install() { 68 return &g_common_switches.Get().easy_off_store_install; 69 } 70 FeatureSwitch* FeatureSwitch::global_commands() { 71 return &g_common_switches.Get().global_commands; 72 } 73 FeatureSwitch* FeatureSwitch::script_badges() { 74 return &g_common_switches.Get().script_badges; 75 } 76 FeatureSwitch* FeatureSwitch::script_bubble() { 77 return &g_common_switches.Get().script_bubble; 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::ScopedOverride::ScopedOverride(FeatureSwitch* feature, 90 bool override_value) 91 : feature_(feature), 92 previous_value_(feature->GetOverrideValue()) { 93 feature_->SetOverrideValue( 94 override_value ? OVERRIDE_ENABLED : OVERRIDE_DISABLED); 95 } 96 97 FeatureSwitch::ScopedOverride::~ScopedOverride() { 98 feature_->SetOverrideValue(previous_value_); 99 } 100 101 FeatureSwitch::FeatureSwitch(const char* switch_name, 102 DefaultValue default_value) { 103 Init(CommandLine::ForCurrentProcess(), switch_name, default_value); 104 } 105 106 FeatureSwitch::FeatureSwitch(const CommandLine* command_line, 107 const char* switch_name, 108 DefaultValue default_value) { 109 Init(command_line, switch_name, default_value); 110 } 111 112 void FeatureSwitch::Init(const CommandLine* command_line, 113 const char* switch_name, 114 DefaultValue default_value) { 115 command_line_ = command_line; 116 switch_name_ = switch_name; 117 default_value_ = default_value == DEFAULT_ENABLED; 118 override_value_ = OVERRIDE_NONE; 119 } 120 121 bool FeatureSwitch::IsEnabled() const { 122 if (override_value_ != OVERRIDE_NONE) 123 return override_value_ == OVERRIDE_ENABLED; 124 125 std::string temp = command_line_->GetSwitchValueASCII(switch_name_); 126 std::string switch_value; 127 TrimWhitespaceASCII(temp, TRIM_ALL, &switch_value); 128 129 if (switch_value == "1") 130 return true; 131 132 if (switch_value == "0") 133 return false; 134 135 if (!default_value_ && command_line_->HasSwitch(GetLegacyEnableFlag())) 136 return true; 137 138 if (default_value_ && command_line_->HasSwitch(GetLegacyDisableFlag())) 139 return false; 140 141 return default_value_; 142 } 143 144 std::string FeatureSwitch::GetLegacyEnableFlag() const { 145 return std::string("enable-") + switch_name_; 146 } 147 148 std::string FeatureSwitch::GetLegacyDisableFlag() const { 149 return std::string("disable-") + switch_name_; 150 } 151 152 void FeatureSwitch::SetOverrideValue(OverrideValue override_value) { 153 override_value_ = override_value; 154 } 155 156 FeatureSwitch::OverrideValue FeatureSwitch::GetOverrideValue() const { 157 return override_value_; 158 } 159 160 } // namespace extensions 161