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