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/browser/prefs/command_line_pref_store.h"
10 #include "ash/ash_switches.h"
11 #include "base/files/file_path.h"
12 #include "base/logging.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/string_split.h"
15 #include "base/values.h"
16 #include "chrome/common/chrome_switches.h"
17 #include "chrome/common/pref_names.h"
18 #include "components/proxy_config/proxy_config_dictionary.h"
19 #include "components/proxy_config/proxy_config_pref_names.h"
20 #include "ui/base/ui_base_switches.h"
22 #if defined(OS_CHROMEOS)
23 #include "chromeos/chromeos_switches.h"
26 const CommandLinePrefStore::StringSwitchToPreferenceMapEntry
27 CommandLinePrefStore::string_switch_map_
[] = {
28 { switches::kLang
, prefs::kApplicationLocale
},
29 { data_reduction_proxy::switches::kDataReductionProxy
,
30 data_reduction_proxy::prefs::kDataReductionProxy
},
31 { switches::kAuthServerWhitelist
, prefs::kAuthServerWhitelist
},
32 { switches::kSSLVersionMin
, prefs::kSSLVersionMin
},
33 { switches::kSSLVersionMax
, prefs::kSSLVersionMax
},
34 { switches::kSSLVersionFallbackMin
, prefs::kSSLVersionFallbackMin
},
35 #if defined(OS_ANDROID)
36 { switches::kAuthAndroidNegotiateAccountType
,
37 prefs::kAuthAndroidNegotiateAccountType
},
41 const CommandLinePrefStore::PathSwitchToPreferenceMapEntry
42 CommandLinePrefStore::path_switch_map_
[] = {
43 { switches::kDiskCacheDir
, prefs::kDiskCacheDir
},
46 const CommandLinePrefStore::BooleanSwitchToPreferenceMapEntry
47 CommandLinePrefStore::boolean_switch_map_
[] = {
48 { switches::kDisable3DAPIs
, prefs::kDisable3DAPIs
, true },
49 { switches::kEnableCloudPrintProxy
, prefs::kCloudPrintProxyEnabled
,
51 { switches::kAllowOutdatedPlugins
, prefs::kPluginsAllowOutdated
, true },
52 { switches::kAlwaysAuthorizePlugins
, prefs::kPluginsAlwaysAuthorize
,
54 { switches::kNoPings
, prefs::kEnableHyperlinkAuditing
, false },
55 { switches::kNoReferrers
, prefs::kEnableReferrers
, false },
56 { switches::kAllowRunningInsecureContent
,
57 prefs::kWebKitAllowRunningInsecureContent
, true },
58 { switches::kNoDisplayingInsecureContent
,
59 prefs::kWebKitAllowDisplayingInsecureContent
, false },
60 { switches::kAllowCrossOriginAuthPrompt
,
61 prefs::kAllowCrossOriginAuthPrompt
, true },
62 { switches::kDisablePrintPreview
, prefs::kPrintPreviewDisabled
, true },
63 #if defined(OS_CHROMEOS)
64 { chromeos::switches::kEnableTouchpadThreeFingerClick
,
65 prefs::kEnableTouchpadThreeFingerClick
, true },
66 { ash::switches::kAshEnableUnifiedDesktop
,
67 prefs::kUnifiedDesktopEnabledByDefault
, true },
69 { switches::kDisableAsyncDns
, prefs::kBuiltInDnsClientEnabled
, false },
72 const CommandLinePrefStore::IntegerSwitchToPreferenceMapEntry
73 CommandLinePrefStore::integer_switch_map_
[] = {
74 { switches::kDiskCacheSize
, prefs::kDiskCacheSize
},
75 { switches::kMediaCacheSize
, prefs::kMediaCacheSize
},
78 CommandLinePrefStore::CommandLinePrefStore(
79 const base::CommandLine
* command_line
)
80 : command_line_(command_line
) {
81 ApplySimpleSwitches();
83 ValidateProxySwitches();
85 ApplyBackgroundModeSwitches();
88 CommandLinePrefStore::~CommandLinePrefStore() {}
90 bool CommandLinePrefStore::ValidateProxySwitches() {
91 if (command_line_
->HasSwitch(switches::kNoProxyServer
) &&
92 (command_line_
->HasSwitch(switches::kProxyAutoDetect
) ||
93 command_line_
->HasSwitch(switches::kProxyServer
) ||
94 command_line_
->HasSwitch(switches::kProxyPacUrl
) ||
95 command_line_
->HasSwitch(switches::kProxyBypassList
))) {
96 LOG(WARNING
) << "Additional command-line proxy switches specified when --"
97 << switches::kNoProxyServer
<< " was also specified.";
103 void CommandLinePrefStore::ApplySimpleSwitches() {
104 // Look for each switch we know about and set its preference accordingly.
105 for (size_t i
= 0; i
< arraysize(string_switch_map_
); ++i
) {
106 if (command_line_
->HasSwitch(string_switch_map_
[i
].switch_name
)) {
107 SetValue(string_switch_map_
[i
].preference_path
,
109 new base::StringValue(command_line_
->GetSwitchValueASCII(
110 string_switch_map_
[i
].switch_name
))),
111 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
115 for (size_t i
= 0; i
< arraysize(path_switch_map_
); ++i
) {
116 if (command_line_
->HasSwitch(path_switch_map_
[i
].switch_name
)) {
118 path_switch_map_
[i
].preference_path
,
119 make_scoped_ptr(new base::StringValue(
120 command_line_
->GetSwitchValuePath(path_switch_map_
[i
].switch_name
)
122 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
126 for (size_t i
= 0; i
< arraysize(integer_switch_map_
); ++i
) {
127 if (command_line_
->HasSwitch(integer_switch_map_
[i
].switch_name
)) {
128 std::string str_value
= command_line_
->GetSwitchValueASCII(
129 integer_switch_map_
[i
].switch_name
);
131 if (!base::StringToInt(str_value
, &int_value
)) {
132 LOG(ERROR
) << "The value " << str_value
<< " of "
133 << integer_switch_map_
[i
].switch_name
134 << " can not be converted to integer, ignoring!";
137 SetValue(integer_switch_map_
[i
].preference_path
,
138 make_scoped_ptr(new base::FundamentalValue(int_value
)),
139 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
143 for (size_t i
= 0; i
< arraysize(boolean_switch_map_
); ++i
) {
144 if (command_line_
->HasSwitch(boolean_switch_map_
[i
].switch_name
)) {
145 SetValue(boolean_switch_map_
[i
].preference_path
,
146 make_scoped_ptr(new base::FundamentalValue(
147 boolean_switch_map_
[i
].set_value
)),
148 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
153 void CommandLinePrefStore::ApplyProxyMode() {
154 if (command_line_
->HasSwitch(switches::kNoProxyServer
)) {
155 SetValue(proxy_config::prefs::kProxy
,
156 make_scoped_ptr(ProxyConfigDictionary::CreateDirect()),
157 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
158 } else if (command_line_
->HasSwitch(switches::kProxyPacUrl
)) {
159 std::string pac_script_url
=
160 command_line_
->GetSwitchValueASCII(switches::kProxyPacUrl
);
161 SetValue(proxy_config::prefs::kProxy
,
163 ProxyConfigDictionary::CreatePacScript(pac_script_url
, false)),
164 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
165 } else if (command_line_
->HasSwitch(switches::kProxyAutoDetect
)) {
166 SetValue(proxy_config::prefs::kProxy
,
167 make_scoped_ptr(ProxyConfigDictionary::CreateAutoDetect()),
168 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
169 } else if (command_line_
->HasSwitch(switches::kProxyServer
)) {
170 std::string proxy_server
=
171 command_line_
->GetSwitchValueASCII(switches::kProxyServer
);
172 std::string bypass_list
=
173 command_line_
->GetSwitchValueASCII(switches::kProxyBypassList
);
174 SetValue(proxy_config::prefs::kProxy
,
175 make_scoped_ptr(ProxyConfigDictionary::CreateFixedServers(
176 proxy_server
, bypass_list
)),
177 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
181 void CommandLinePrefStore::ApplySSLSwitches() {
182 if (command_line_
->HasSwitch(switches::kCipherSuiteBlacklist
)) {
183 scoped_ptr
<base::ListValue
> list_value(new base::ListValue());
184 list_value
->AppendStrings(base::SplitString(
185 command_line_
->GetSwitchValueASCII(switches::kCipherSuiteBlacklist
),
186 ",", base::TRIM_WHITESPACE
, base::SPLIT_WANT_ALL
));
187 SetValue(prefs::kCipherSuiteBlacklist
, list_value
.Pass(),
188 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
192 void CommandLinePrefStore::ApplyBackgroundModeSwitches() {
193 if (command_line_
->HasSwitch(switches::kDisableExtensions
)) {
194 SetValue(prefs::kBackgroundModeEnabled
,
195 make_scoped_ptr(new base::FundamentalValue(false)),
196 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);