- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / common / extensions / feature_switch.cc
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.
4
5 #include "chrome/common/extensions/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 "chrome/common/chrome_switches.h"
12 #include "extensions/common/switches.h"
13
14 namespace extensions {
15
16 namespace {
17
18 class CommonSwitches {
19  public:
20   CommonSwitches()
21       : easy_off_store_install(
22             ::switches::kEasyOffStoreExtensionInstall,
23             FeatureSwitch::DEFAULT_DISABLED),
24         global_commands(
25             ::switches::kGlobalCommands,
26             FeatureSwitch::DEFAULT_DISABLED),
27         script_badges(
28             ::switches::kScriptBadges,
29             FeatureSwitch::DEFAULT_DISABLED),
30         script_bubble(
31             ::switches::kScriptBubble,
32             FeatureSwitch::DEFAULT_DISABLED),
33         prompt_for_external_extensions(
34             ::switches::kPromptForExternalExtensions,
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
44   FeatureSwitch easy_off_store_install;
45   FeatureSwitch global_commands;
46   FeatureSwitch script_badges;
47   FeatureSwitch script_bubble;
48   FeatureSwitch prompt_for_external_extensions;
49   FeatureSwitch error_console;
50 };
51
52 base::LazyInstance<CommonSwitches> g_common_switches =
53     LAZY_INSTANCE_INITIALIZER;
54
55 }  // namespace
56
57 FeatureSwitch* FeatureSwitch::easy_off_store_install() {
58   return &g_common_switches.Get().easy_off_store_install;
59 }
60 FeatureSwitch* FeatureSwitch::global_commands() {
61   return &g_common_switches.Get().global_commands;
62 }
63 FeatureSwitch* FeatureSwitch::script_badges() {
64   return &g_common_switches.Get().script_badges;
65 }
66 FeatureSwitch* FeatureSwitch::script_bubble() {
67   return &g_common_switches.Get().script_bubble;
68 }
69 FeatureSwitch* FeatureSwitch::prompt_for_external_extensions() {
70   return &g_common_switches.Get().prompt_for_external_extensions;
71 }
72 FeatureSwitch* FeatureSwitch::error_console() {
73   return &g_common_switches.Get().error_console;
74 }
75
76 FeatureSwitch::ScopedOverride::ScopedOverride(FeatureSwitch* feature,
77                                               bool override_value)
78     : feature_(feature),
79       previous_value_(feature->GetOverrideValue()) {
80   feature_->SetOverrideValue(
81       override_value ? OVERRIDE_ENABLED : OVERRIDE_DISABLED);
82 }
83
84 FeatureSwitch::ScopedOverride::~ScopedOverride() {
85   feature_->SetOverrideValue(previous_value_);
86 }
87
88 FeatureSwitch::FeatureSwitch(const char* switch_name,
89                              DefaultValue default_value) {
90   Init(CommandLine::ForCurrentProcess(), switch_name, default_value);
91 }
92
93 FeatureSwitch::FeatureSwitch(const CommandLine* command_line,
94                              const char* switch_name,
95                              DefaultValue default_value) {
96   Init(command_line, switch_name, default_value);
97 }
98
99 void FeatureSwitch::Init(const CommandLine* command_line,
100                          const char* switch_name,
101                          DefaultValue default_value) {
102   command_line_ = command_line;
103   switch_name_ = switch_name;
104   default_value_ = default_value == DEFAULT_ENABLED;
105   override_value_ = OVERRIDE_NONE;
106 }
107
108 bool FeatureSwitch::IsEnabled() const {
109   if (override_value_ != OVERRIDE_NONE)
110     return override_value_ == OVERRIDE_ENABLED;
111
112   std::string temp = command_line_->GetSwitchValueASCII(switch_name_);
113   std::string switch_value;
114   TrimWhitespaceASCII(temp, TRIM_ALL, &switch_value);
115
116   if (switch_value == "1")
117     return true;
118
119   if (switch_value == "0")
120     return false;
121
122   if (!default_value_ && command_line_->HasSwitch(GetLegacyEnableFlag()))
123     return true;
124
125   if (default_value_ && command_line_->HasSwitch(GetLegacyDisableFlag()))
126     return false;
127
128   return default_value_;
129 }
130
131 std::string FeatureSwitch::GetLegacyEnableFlag() const {
132   return std::string("enable-") + switch_name_;
133 }
134
135 std::string FeatureSwitch::GetLegacyDisableFlag() const {
136   return std::string("disable-") + switch_name_;
137 }
138
139 void FeatureSwitch::SetOverrideValue(OverrideValue override_value) {
140   override_value_ = override_value;
141 }
142
143 FeatureSwitch::OverrideValue FeatureSwitch::GetOverrideValue() const {
144   return override_value_;
145 }
146
147 }  // namespace extensions