Introduce AndroidMetricsProvider class.
[chromium-blink-merge.git] / extensions / common / feature_switch.cc
blob121dceb25ce89b7efb04f8e5cdde4e4dd52a1ada
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.
5 #include "extensions/common/feature_switch.h"
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"
13 namespace extensions {
15 namespace {
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) {}
49 // Enables extensions to be easily installed from sites other than the web
50 // store.
51 FeatureSwitch easy_off_store_install;
53 FeatureSwitch force_dev_mode_highlighting;
54 FeatureSwitch global_commands;
56 // Should we prompt the user before allowing external extensions to install?
57 // Default is yes.
58 FeatureSwitch prompt_for_external_extensions;
60 FeatureSwitch error_console;
61 FeatureSwitch enable_override_bookmarks_ui;
62 FeatureSwitch scripts_require_action;
65 base::LazyInstance<CommonSwitches> g_common_switches =
66 LAZY_INSTANCE_INITIALIZER;
68 } // namespace
70 FeatureSwitch* FeatureSwitch::force_dev_mode_highlighting() {
71 return &g_common_switches.Get().force_dev_mode_highlighting;
73 FeatureSwitch* FeatureSwitch::easy_off_store_install() {
74 return &g_common_switches.Get().easy_off_store_install;
76 FeatureSwitch* FeatureSwitch::global_commands() {
77 return &g_common_switches.Get().global_commands;
79 FeatureSwitch* FeatureSwitch::prompt_for_external_extensions() {
80 return &g_common_switches.Get().prompt_for_external_extensions;
82 FeatureSwitch* FeatureSwitch::error_console() {
83 return &g_common_switches.Get().error_console;
85 FeatureSwitch* FeatureSwitch::enable_override_bookmarks_ui() {
86 return &g_common_switches.Get().enable_override_bookmarks_ui;
89 FeatureSwitch* FeatureSwitch::scripts_require_action() {
90 return &g_common_switches.Get().scripts_require_action;
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);
101 FeatureSwitch::ScopedOverride::~ScopedOverride() {
102 feature_->SetOverrideValue(previous_value_);
105 FeatureSwitch::FeatureSwitch(const char* switch_name,
106 DefaultValue default_value) {
107 Init(CommandLine::ForCurrentProcess(), switch_name, default_value);
110 FeatureSwitch::FeatureSwitch(const CommandLine* command_line,
111 const char* switch_name,
112 DefaultValue default_value) {
113 Init(command_line, switch_name, default_value);
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;
125 bool FeatureSwitch::IsEnabled() const {
126 if (override_value_ != OVERRIDE_NONE)
127 return override_value_ == OVERRIDE_ENABLED;
129 if (!switch_name_)
130 return default_value_;
132 std::string temp = command_line_->GetSwitchValueASCII(switch_name_);
133 std::string switch_value;
134 base::TrimWhitespaceASCII(temp, base::TRIM_ALL, &switch_value);
136 if (switch_value == "1")
137 return true;
139 if (switch_value == "0")
140 return false;
142 if (!default_value_ && command_line_->HasSwitch(GetLegacyEnableFlag()))
143 return true;
145 if (default_value_ && command_line_->HasSwitch(GetLegacyDisableFlag()))
146 return false;
148 return default_value_;
151 std::string FeatureSwitch::GetLegacyEnableFlag() const {
152 DCHECK(switch_name_);
153 return std::string("enable-") + switch_name_;
156 std::string FeatureSwitch::GetLegacyDisableFlag() const {
157 DCHECK(switch_name_);
158 return std::string("disable-") + switch_name_;
161 void FeatureSwitch::SetOverrideValue(OverrideValue override_value) {
162 override_value_ = override_value;
165 FeatureSwitch::OverrideValue FeatureSwitch::GetOverrideValue() const {
166 return override_value_;
169 } // namespace extensions