Extract SIGPIPE ignoring code to a common place.
[chromium-blink-merge.git] / chrome / common / extensions / feature_switch.cc
blob0e624370e7a3f0796cf84821145760cc80cb6b93
1 // Copyright (c) 2012 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 "chrome/common/extensions/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/string_util.h"
11 #include "chrome/common/chrome_switches.h"
13 namespace extensions {
15 namespace {
17 class CommonSwitches {
18 public:
19 CommonSwitches()
20 : action_box(
21 switches::kActionBox,
22 FeatureSwitch::DEFAULT_DISABLED),
23 easy_off_store_install(
24 switches::kEasyOffStoreExtensionInstall,
25 FeatureSwitch::DEFAULT_DISABLED),
26 extensions_in_action_box(
27 switches::kExtensionsInActionBox,
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 // TODO(finnur): When enabling this, only enable for OS_WIN.
36 sideload_wipeout(
37 switches::kSideloadWipeout,
38 base::FieldTrialList::FindFullName("SideloadWipeout") == "Enabled" ?
39 FeatureSwitch::DEFAULT_ENABLED :
40 FeatureSwitch::DEFAULT_DISABLED),
41 prompt_for_external_extensions(
42 switches::kPromptForExternalExtensions,
43 base::FieldTrialList::FindFullName("SideloadWipeout") == "Enabled" ?
44 FeatureSwitch::DEFAULT_ENABLED :
45 FeatureSwitch::DEFAULT_DISABLED),
46 tab_capture(
47 switches::kTabCapture,
48 FeatureSwitch::DEFAULT_DISABLED)
50 if (!action_box.IsEnabled()){
51 extensions_in_action_box.SetOverrideValue(
52 FeatureSwitch::OVERRIDE_DISABLED);
56 FeatureSwitch action_box;
57 FeatureSwitch easy_off_store_install;
58 FeatureSwitch extensions_in_action_box;
59 FeatureSwitch script_badges;
60 FeatureSwitch script_bubble;
61 FeatureSwitch sideload_wipeout;
62 FeatureSwitch prompt_for_external_extensions;
63 FeatureSwitch tab_capture;
66 base::LazyInstance<CommonSwitches> g_common_switches =
67 LAZY_INSTANCE_INITIALIZER;
69 } // namespace
72 FeatureSwitch* FeatureSwitch::action_box() {
73 return &g_common_switches.Get().action_box;
75 FeatureSwitch* FeatureSwitch::easy_off_store_install() {
76 return &g_common_switches.Get().easy_off_store_install;
78 FeatureSwitch* FeatureSwitch::extensions_in_action_box() {
79 return &g_common_switches.Get().extensions_in_action_box;
81 FeatureSwitch* FeatureSwitch::script_badges() {
82 return &g_common_switches.Get().script_badges;
84 FeatureSwitch* FeatureSwitch::script_bubble() {
85 return &g_common_switches.Get().script_bubble;
87 FeatureSwitch* FeatureSwitch::sideload_wipeout() {
88 return &g_common_switches.Get().sideload_wipeout;
90 FeatureSwitch* FeatureSwitch::prompt_for_external_extensions() {
91 return &g_common_switches.Get().prompt_for_external_extensions;
93 FeatureSwitch* FeatureSwitch::tab_capture() {
94 return &g_common_switches.Get().tab_capture;
98 FeatureSwitch::ScopedOverride::ScopedOverride(FeatureSwitch* feature,
99 bool override_value)
100 : feature_(feature),
101 previous_value_(feature->GetOverrideValue()) {
102 feature_->SetOverrideValue(
103 override_value ? OVERRIDE_ENABLED : OVERRIDE_DISABLED);
106 FeatureSwitch::ScopedOverride::~ScopedOverride() {
107 feature_->SetOverrideValue(previous_value_);
110 FeatureSwitch::FeatureSwitch(const char* switch_name,
111 DefaultValue default_value) {
112 Init(CommandLine::ForCurrentProcess(), switch_name, default_value);
115 FeatureSwitch::FeatureSwitch(const CommandLine* command_line,
116 const char* switch_name,
117 DefaultValue default_value) {
118 Init(command_line, switch_name, default_value);
121 void FeatureSwitch::Init(const CommandLine* command_line,
122 const char* switch_name,
123 DefaultValue default_value) {
124 command_line_ = command_line;
125 switch_name_ = switch_name;
126 default_value_ = default_value == DEFAULT_ENABLED;
127 override_value_ = OVERRIDE_NONE;
130 bool FeatureSwitch::IsEnabled() const {
131 if (override_value_ != OVERRIDE_NONE)
132 return override_value_ == OVERRIDE_ENABLED;
134 std::string temp = command_line_->GetSwitchValueASCII(switch_name_);
135 std::string switch_value;
136 TrimWhitespaceASCII(temp, TRIM_ALL, &switch_value);
138 if (switch_value == "1")
139 return true;
141 if (switch_value == "0")
142 return false;
144 if (!default_value_ && command_line_->HasSwitch(GetLegacyEnableFlag()))
145 return true;
147 if (default_value_ && command_line_->HasSwitch(GetLegacyDisableFlag()))
148 return false;
150 return default_value_;
153 std::string FeatureSwitch::GetLegacyEnableFlag() const {
154 return std::string("enable-") + switch_name_;
157 std::string FeatureSwitch::GetLegacyDisableFlag() const {
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