| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "extensions/common/feature_switch.h" | 5 #include "extensions/common/feature_switch.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
| 9 #include "base/metrics/field_trial.h" | 9 #include "base/metrics/field_trial.h" |
| 10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| 11 #include "build/build_config.h" | 11 #include "build/build_config.h" |
| 12 #include "extensions/common/switches.h" | 12 #include "extensions/common/switches.h" |
| 13 | 13 |
| 14 namespace extensions { | 14 namespace extensions { |
| 15 | 15 |
| 16 namespace { | 16 namespace { |
| 17 | 17 |
| 18 // The switch media-router is defined in chrome/common/chrome_switches.cc, but | 18 // The switch media-router is defined in chrome/common/chrome_switches.cc, but |
| 19 // we can't depend on chrome here. | 19 // we can't depend on chrome here. |
| 20 const char kMediaRouterFlag[] = "media-router"; | 20 const char kMediaRouterFlag[] = "media-router"; |
| 21 | 21 |
| 22 const char kEnableMediaRouterExperiment[] = "EnableMediaRouter"; | 22 const char kEnableMediaRouterExperiment[] = "EnableMediaRouter"; |
| 23 const char kEnableMediaRouterWithCastExtensionExperiment[] = | |
| 24 "EnableMediaRouterWithCastExtension"; | |
| 25 const char kExtensionActionRedesignExperiment[] = "ExtensionActionRedesign"; | 23 const char kExtensionActionRedesignExperiment[] = "ExtensionActionRedesign"; |
| 26 | 24 |
| 27 const char* kMediaRouterRequiredExperiments[] = { | |
| 28 kEnableMediaRouterExperiment, kExtensionActionRedesignExperiment}; | |
| 29 const char* kMediaRouterWithCastExtensionRequiredExperiments[] = { | |
| 30 kEnableMediaRouterWithCastExtensionExperiment, | |
| 31 kExtensionActionRedesignExperiment}; | |
| 32 | |
| 33 class CommonSwitches { | 25 class CommonSwitches { |
| 34 public: | 26 public: |
| 35 CommonSwitches() | 27 CommonSwitches() |
| 36 : easy_off_store_install(nullptr, FeatureSwitch::DEFAULT_DISABLED), | 28 : easy_off_store_install(nullptr, FeatureSwitch::DEFAULT_DISABLED), |
| 37 force_dev_mode_highlighting(switches::kForceDevModeHighlighting, | 29 force_dev_mode_highlighting(switches::kForceDevModeHighlighting, |
| 38 FeatureSwitch::DEFAULT_DISABLED), | 30 FeatureSwitch::DEFAULT_DISABLED), |
| 39 prompt_for_external_extensions( | 31 prompt_for_external_extensions( |
| 40 #if defined(CHROMIUM_BUILD) | 32 #if defined(CHROMIUM_BUILD) |
| 41 switches::kPromptForExternalExtensions, | 33 switches::kPromptForExternalExtensions, |
| 42 #else | 34 #else |
| (...skipping 12 matching lines...) Expand all Loading... |
| 55 FeatureSwitch::DEFAULT_ENABLED), | 47 FeatureSwitch::DEFAULT_ENABLED), |
| 56 extension_action_redesign_override(switches::kExtensionActionRedesign, | 48 extension_action_redesign_override(switches::kExtensionActionRedesign, |
| 57 FeatureSwitch::DEFAULT_ENABLED), | 49 FeatureSwitch::DEFAULT_ENABLED), |
| 58 scripts_require_action(switches::kScriptsRequireAction, | 50 scripts_require_action(switches::kScriptsRequireAction, |
| 59 FeatureSwitch::DEFAULT_DISABLED), | 51 FeatureSwitch::DEFAULT_DISABLED), |
| 60 embedded_extension_options(switches::kEmbeddedExtensionOptions, | 52 embedded_extension_options(switches::kEmbeddedExtensionOptions, |
| 61 FeatureSwitch::DEFAULT_DISABLED), | 53 FeatureSwitch::DEFAULT_DISABLED), |
| 62 trace_app_source(switches::kTraceAppSource, | 54 trace_app_source(switches::kTraceAppSource, |
| 63 FeatureSwitch::DEFAULT_ENABLED), | 55 FeatureSwitch::DEFAULT_ENABLED), |
| 64 media_router(kMediaRouterFlag, | 56 media_router(kMediaRouterFlag, |
| 65 std::vector<std::string>( | 57 kEnableMediaRouterExperiment, |
| 66 kMediaRouterRequiredExperiments, | 58 FeatureSwitch::DEFAULT_DISABLED) { |
| 67 kMediaRouterRequiredExperiments + | |
| 68 arraysize(kMediaRouterRequiredExperiments)), | |
| 69 FeatureSwitch::DEFAULT_DISABLED), | |
| 70 media_router_with_cast_extension( | |
| 71 kMediaRouterFlag, | |
| 72 std::vector<std::string>( | |
| 73 kMediaRouterWithCastExtensionRequiredExperiments, | |
| 74 kMediaRouterWithCastExtensionRequiredExperiments + | |
| 75 arraysize( | |
| 76 kMediaRouterWithCastExtensionRequiredExperiments)), | |
| 77 FeatureSwitch::DEFAULT_DISABLED) { | |
| 78 } | 59 } |
| 79 | 60 |
| 80 // Enables extensions to be easily installed from sites other than the web | 61 // Enables extensions to be easily installed from sites other than the web |
| 81 // store. | 62 // store. |
| 82 FeatureSwitch easy_off_store_install; | 63 FeatureSwitch easy_off_store_install; |
| 83 | 64 |
| 84 FeatureSwitch force_dev_mode_highlighting; | 65 FeatureSwitch force_dev_mode_highlighting; |
| 85 | 66 |
| 86 // Should we prompt the user before allowing external extensions to install? | 67 // Should we prompt the user before allowing external extensions to install? |
| 87 // Default is yes. | 68 // Default is yes. |
| 88 FeatureSwitch prompt_for_external_extensions; | 69 FeatureSwitch prompt_for_external_extensions; |
| 89 | 70 |
| 90 FeatureSwitch error_console; | 71 FeatureSwitch error_console; |
| 91 FeatureSwitch enable_override_bookmarks_ui; | 72 FeatureSwitch enable_override_bookmarks_ui; |
| 92 FeatureSwitch extension_action_redesign; | 73 FeatureSwitch extension_action_redesign; |
| 93 FeatureSwitch extension_action_redesign_override; | 74 FeatureSwitch extension_action_redesign_override; |
| 94 FeatureSwitch scripts_require_action; | 75 FeatureSwitch scripts_require_action; |
| 95 FeatureSwitch embedded_extension_options; | 76 FeatureSwitch embedded_extension_options; |
| 96 FeatureSwitch trace_app_source; | 77 FeatureSwitch trace_app_source; |
| 97 FeatureSwitch media_router; | 78 FeatureSwitch media_router; |
| 98 FeatureSwitch media_router_with_cast_extension; | |
| 99 }; | 79 }; |
| 100 | 80 |
| 101 base::LazyInstance<CommonSwitches> g_common_switches = | 81 base::LazyInstance<CommonSwitches> g_common_switches = |
| 102 LAZY_INSTANCE_INITIALIZER; | 82 LAZY_INSTANCE_INITIALIZER; |
| 103 | 83 |
| 104 } // namespace | 84 } // namespace |
| 105 | 85 |
| 106 FeatureSwitch* FeatureSwitch::force_dev_mode_highlighting() { | 86 FeatureSwitch* FeatureSwitch::force_dev_mode_highlighting() { |
| 107 return &g_common_switches.Get().force_dev_mode_highlighting; | 87 return &g_common_switches.Get().force_dev_mode_highlighting; |
| 108 } | 88 } |
| (...skipping 27 matching lines...) Expand all Loading... |
| 136 } | 116 } |
| 137 FeatureSwitch* FeatureSwitch::embedded_extension_options() { | 117 FeatureSwitch* FeatureSwitch::embedded_extension_options() { |
| 138 return &g_common_switches.Get().embedded_extension_options; | 118 return &g_common_switches.Get().embedded_extension_options; |
| 139 } | 119 } |
| 140 FeatureSwitch* FeatureSwitch::trace_app_source() { | 120 FeatureSwitch* FeatureSwitch::trace_app_source() { |
| 141 return &g_common_switches.Get().trace_app_source; | 121 return &g_common_switches.Get().trace_app_source; |
| 142 } | 122 } |
| 143 FeatureSwitch* FeatureSwitch::media_router() { | 123 FeatureSwitch* FeatureSwitch::media_router() { |
| 144 return &g_common_switches.Get().media_router; | 124 return &g_common_switches.Get().media_router; |
| 145 } | 125 } |
| 146 FeatureSwitch* FeatureSwitch::media_router_with_cast_extension() { | |
| 147 return &g_common_switches.Get().media_router_with_cast_extension; | |
| 148 } | |
| 149 | 126 |
| 150 FeatureSwitch::ScopedOverride::ScopedOverride(FeatureSwitch* feature, | 127 FeatureSwitch::ScopedOverride::ScopedOverride(FeatureSwitch* feature, |
| 151 bool override_value) | 128 bool override_value) |
| 152 : feature_(feature), | 129 : feature_(feature), |
| 153 previous_value_(feature->GetOverrideValue()) { | 130 previous_value_(feature->GetOverrideValue()) { |
| 154 feature_->SetOverrideValue( | 131 feature_->SetOverrideValue( |
| 155 override_value ? OVERRIDE_ENABLED : OVERRIDE_DISABLED); | 132 override_value ? OVERRIDE_ENABLED : OVERRIDE_DISABLED); |
| 156 } | 133 } |
| 157 | 134 |
| 158 FeatureSwitch::ScopedOverride::~ScopedOverride() { | 135 FeatureSwitch::ScopedOverride::~ScopedOverride() { |
| 159 feature_->SetOverrideValue(previous_value_); | 136 feature_->SetOverrideValue(previous_value_); |
| 160 } | 137 } |
| 161 | 138 |
| 162 FeatureSwitch::FeatureSwitch(const char* switch_name, | 139 FeatureSwitch::FeatureSwitch(const char* switch_name, |
| 163 DefaultValue default_value) | 140 DefaultValue default_value) |
| 164 : FeatureSwitch(base::CommandLine::ForCurrentProcess(), | 141 : FeatureSwitch(base::CommandLine::ForCurrentProcess(), |
| 165 switch_name, | 142 switch_name, |
| 166 default_value) {} | 143 default_value) {} |
| 167 | 144 |
| 168 FeatureSwitch::FeatureSwitch(const char* switch_name, | 145 FeatureSwitch::FeatureSwitch(const char* switch_name, |
| 169 const char* field_trial_name, | 146 const char* field_trial_name, |
| 170 DefaultValue default_value) | 147 DefaultValue default_value) |
| 171 : FeatureSwitch(base::CommandLine::ForCurrentProcess(), | 148 : FeatureSwitch(base::CommandLine::ForCurrentProcess(), |
| 172 switch_name, | 149 switch_name, |
| 173 std::vector<std::string>(1, field_trial_name), | 150 field_trial_name, |
| 174 default_value) {} | |
| 175 | |
| 176 FeatureSwitch::FeatureSwitch( | |
| 177 const char* switch_name, | |
| 178 const std::vector<std::string>& required_field_trials, | |
| 179 DefaultValue default_value) | |
| 180 : FeatureSwitch(base::CommandLine::ForCurrentProcess(), | |
| 181 switch_name, | |
| 182 required_field_trials, | |
| 183 default_value) {} | 151 default_value) {} |
| 184 | 152 |
| 185 FeatureSwitch::FeatureSwitch(const base::CommandLine* command_line, | 153 FeatureSwitch::FeatureSwitch(const base::CommandLine* command_line, |
| 186 const char* switch_name, | 154 const char* switch_name, |
| 187 DefaultValue default_value) | 155 DefaultValue default_value) |
| 188 : FeatureSwitch(command_line, | 156 : FeatureSwitch(command_line, switch_name, nullptr, default_value) {} |
| 189 switch_name, | |
| 190 std::vector<std::string>(), | |
| 191 default_value) {} | |
| 192 | 157 |
| 193 FeatureSwitch::FeatureSwitch( | 158 FeatureSwitch::FeatureSwitch(const base::CommandLine* command_line, |
| 194 const base::CommandLine* command_line, | 159 const char* switch_name, |
| 195 const char* switch_name, | 160 const char* field_trial_name, |
| 196 const std::vector<std::string>& required_field_trials, | 161 DefaultValue default_value) |
| 197 DefaultValue default_value) | |
| 198 : command_line_(command_line), | 162 : command_line_(command_line), |
| 199 switch_name_(switch_name), | 163 switch_name_(switch_name), |
| 200 required_field_trials_(required_field_trials), | 164 field_trial_name_(field_trial_name), |
| 201 default_value_(default_value == DEFAULT_ENABLED), | 165 default_value_(default_value == DEFAULT_ENABLED), |
| 202 override_value_(OVERRIDE_NONE) {} | 166 override_value_(OVERRIDE_NONE) {} |
| 203 | 167 |
| 204 FeatureSwitch::~FeatureSwitch(){}; | |
| 205 | |
| 206 bool FeatureSwitch::IsEnabled() const { | 168 bool FeatureSwitch::IsEnabled() const { |
| 207 if (override_value_ != OVERRIDE_NONE) | 169 if (override_value_ != OVERRIDE_NONE) |
| 208 return override_value_ == OVERRIDE_ENABLED; | 170 return override_value_ == OVERRIDE_ENABLED; |
| 209 | 171 |
| 210 if (!switch_name_) | 172 if (!switch_name_) |
| 211 return default_value_; | 173 return default_value_; |
| 212 | 174 |
| 213 std::string temp = command_line_->GetSwitchValueASCII(switch_name_); | 175 std::string temp = command_line_->GetSwitchValueASCII(switch_name_); |
| 214 std::string switch_value; | 176 std::string switch_value; |
| 215 base::TrimWhitespaceASCII(temp, base::TRIM_ALL, &switch_value); | 177 base::TrimWhitespaceASCII(temp, base::TRIM_ALL, &switch_value); |
| 216 | 178 |
| 217 if (switch_value == "1") | 179 if (switch_value == "1") |
| 218 return true; | 180 return true; |
| 219 | 181 |
| 220 if (switch_value == "0") | 182 if (switch_value == "0") |
| 221 return false; | 183 return false; |
| 222 | 184 |
| 223 // TODO(imcheng): Don't check |default_value_|. Otherwise, we could improperly | 185 // TODO(imcheng): Don't check |default_value_|. Otherwise, we could improperly |
| 224 // ignore an enable/disable switch if there is a field trial active. | 186 // ignore an enable/disable switch if there is a field trial active. |
| 225 // crbug.com/585569 | 187 // crbug.com/585569 |
| 226 if (!default_value_ && command_line_->HasSwitch(GetLegacyEnableFlag())) | 188 if (!default_value_ && command_line_->HasSwitch(GetLegacyEnableFlag())) |
| 227 return true; | 189 return true; |
| 228 | 190 |
| 229 if (default_value_ && command_line_->HasSwitch(GetLegacyDisableFlag())) | 191 if (default_value_ && command_line_->HasSwitch(GetLegacyDisableFlag())) |
| 230 return false; | 192 return false; |
| 231 | 193 |
| 232 if (!required_field_trials_.empty()) { | 194 if (field_trial_name_) { |
| 233 bool enabled_by_field_trial = true; | 195 std::string group_name = |
| 234 bool disabled_by_field_trial = false; | 196 base::FieldTrialList::FindFullName(field_trial_name_); |
| 235 for (const std::string& field_trial_name : required_field_trials_) { | 197 if (base::StartsWith(group_name, "Enabled", base::CompareCase::SENSITIVE)) |
| 236 std::string group_name = | 198 return true; |
| 237 base::FieldTrialList::FindFullName(field_trial_name); | 199 if (base::StartsWith(group_name, "Disabled", base::CompareCase::SENSITIVE)) |
| 238 if (!base::StartsWith(group_name, "Enabled", | |
| 239 base::CompareCase::SENSITIVE)) { | |
| 240 enabled_by_field_trial = false; | |
| 241 if (base::StartsWith(group_name, "Disabled", | |
| 242 base::CompareCase::SENSITIVE)) { | |
| 243 disabled_by_field_trial = true; | |
| 244 break; | |
| 245 } | |
| 246 } | |
| 247 } | |
| 248 if (disabled_by_field_trial) | |
| 249 return false; | 200 return false; |
| 250 if (enabled_by_field_trial) | |
| 251 return true; | |
| 252 } | 201 } |
| 253 | 202 |
| 254 return default_value_; | 203 return default_value_; |
| 255 } | 204 } |
| 256 | 205 |
| 257 std::string FeatureSwitch::GetLegacyEnableFlag() const { | 206 std::string FeatureSwitch::GetLegacyEnableFlag() const { |
| 258 DCHECK(switch_name_); | 207 DCHECK(switch_name_); |
| 259 return std::string("enable-") + switch_name_; | 208 return std::string("enable-") + switch_name_; |
| 260 } | 209 } |
| 261 | 210 |
| 262 std::string FeatureSwitch::GetLegacyDisableFlag() const { | 211 std::string FeatureSwitch::GetLegacyDisableFlag() const { |
| 263 DCHECK(switch_name_); | 212 DCHECK(switch_name_); |
| 264 return std::string("disable-") + switch_name_; | 213 return std::string("disable-") + switch_name_; |
| 265 } | 214 } |
| 266 | 215 |
| 267 void FeatureSwitch::SetOverrideValue(OverrideValue override_value) { | 216 void FeatureSwitch::SetOverrideValue(OverrideValue override_value) { |
| 268 override_value_ = override_value; | 217 override_value_ = override_value; |
| 269 } | 218 } |
| 270 | 219 |
| 271 FeatureSwitch::OverrideValue FeatureSwitch::GetOverrideValue() const { | 220 FeatureSwitch::OverrideValue FeatureSwitch::GetOverrideValue() const { |
| 272 return override_value_; | 221 return override_value_; |
| 273 } | 222 } |
| 274 | 223 |
| 275 } // namespace extensions | 224 } // namespace extensions |
| OLD | NEW |