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 "base/files/file_path.h"
11 #include "base/logging.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/string_split.h"
14 #include "base/values.h"
15 #include "chrome/common/chrome_switches.h"
16 #include "chrome/common/pref_names.h"
17 #include "components/proxy_config/proxy_config_dictionary.h"
18 #include "components/proxy_config/proxy_config_pref_names.h"
19 #include "ui/base/ui_base_switches.h"
21 #if defined(OS_CHROMEOS)
22 #include "chromeos/chromeos_switches.h"
25 const CommandLinePrefStore::StringSwitchToPreferenceMapEntry
26 CommandLinePrefStore::string_switch_map_
[] = {
27 { switches::kLang
, prefs::kApplicationLocale
},
28 { data_reduction_proxy::switches::kDataReductionProxy
,
29 data_reduction_proxy::prefs::kDataReductionProxy
},
30 { switches::kAuthServerWhitelist
, prefs::kAuthServerWhitelist
},
31 { switches::kSSLVersionMin
, prefs::kSSLVersionMin
},
32 { switches::kSSLVersionMax
, prefs::kSSLVersionMax
},
33 { switches::kSSLVersionFallbackMin
, prefs::kSSLVersionFallbackMin
},
34 #if defined(OS_ANDROID)
35 { switches::kAuthAndroidNegotiateAccountType
,
36 prefs::kAuthAndroidNegotiateAccountType
},
40 const CommandLinePrefStore::PathSwitchToPreferenceMapEntry
41 CommandLinePrefStore::path_switch_map_
[] = {
42 { switches::kDiskCacheDir
, prefs::kDiskCacheDir
},
45 const CommandLinePrefStore::BooleanSwitchToPreferenceMapEntry
46 CommandLinePrefStore::boolean_switch_map_
[] = {
47 { switches::kDisable3DAPIs
, prefs::kDisable3DAPIs
, true },
48 { switches::kEnableCloudPrintProxy
, prefs::kCloudPrintProxyEnabled
,
50 { switches::kAllowOutdatedPlugins
, prefs::kPluginsAllowOutdated
, true },
51 { switches::kAlwaysAuthorizePlugins
, prefs::kPluginsAlwaysAuthorize
,
53 { switches::kNoPings
, prefs::kEnableHyperlinkAuditing
, false },
54 { switches::kNoReferrers
, prefs::kEnableReferrers
, false },
55 { switches::kAllowRunningInsecureContent
,
56 prefs::kWebKitAllowRunningInsecureContent
, true },
57 { switches::kNoDisplayingInsecureContent
,
58 prefs::kWebKitAllowDisplayingInsecureContent
, false },
59 { switches::kAllowCrossOriginAuthPrompt
,
60 prefs::kAllowCrossOriginAuthPrompt
, true },
61 { switches::kDisablePrintPreview
, prefs::kPrintPreviewDisabled
, true },
62 #if defined(OS_CHROMEOS)
63 { chromeos::switches::kEnableTouchpadThreeFingerClick
,
64 prefs::kEnableTouchpadThreeFingerClick
, true },
66 { switches::kDisableAsyncDns
, prefs::kBuiltInDnsClientEnabled
, false },
69 const CommandLinePrefStore::IntegerSwitchToPreferenceMapEntry
70 CommandLinePrefStore::integer_switch_map_
[] = {
71 { switches::kDiskCacheSize
, prefs::kDiskCacheSize
},
72 { switches::kMediaCacheSize
, prefs::kMediaCacheSize
},
75 CommandLinePrefStore::CommandLinePrefStore(
76 const base::CommandLine
* command_line
)
77 : command_line_(command_line
) {
78 ApplySimpleSwitches();
80 ValidateProxySwitches();
82 ApplyBackgroundModeSwitches();
85 CommandLinePrefStore::~CommandLinePrefStore() {}
87 bool CommandLinePrefStore::ValidateProxySwitches() {
88 if (command_line_
->HasSwitch(switches::kNoProxyServer
) &&
89 (command_line_
->HasSwitch(switches::kProxyAutoDetect
) ||
90 command_line_
->HasSwitch(switches::kProxyServer
) ||
91 command_line_
->HasSwitch(switches::kProxyPacUrl
) ||
92 command_line_
->HasSwitch(switches::kProxyBypassList
))) {
93 LOG(WARNING
) << "Additional command-line proxy switches specified when --"
94 << switches::kNoProxyServer
<< " was also specified.";
100 void CommandLinePrefStore::ApplySimpleSwitches() {
101 // Look for each switch we know about and set its preference accordingly.
102 for (size_t i
= 0; i
< arraysize(string_switch_map_
); ++i
) {
103 if (command_line_
->HasSwitch(string_switch_map_
[i
].switch_name
)) {
104 SetValue(string_switch_map_
[i
].preference_path
,
106 new base::StringValue(command_line_
->GetSwitchValueASCII(
107 string_switch_map_
[i
].switch_name
))),
108 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
112 for (size_t i
= 0; i
< arraysize(path_switch_map_
); ++i
) {
113 if (command_line_
->HasSwitch(path_switch_map_
[i
].switch_name
)) {
115 path_switch_map_
[i
].preference_path
,
116 make_scoped_ptr(new base::StringValue(
117 command_line_
->GetSwitchValuePath(path_switch_map_
[i
].switch_name
)
119 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
123 for (size_t i
= 0; i
< arraysize(integer_switch_map_
); ++i
) {
124 if (command_line_
->HasSwitch(integer_switch_map_
[i
].switch_name
)) {
125 std::string str_value
= command_line_
->GetSwitchValueASCII(
126 integer_switch_map_
[i
].switch_name
);
128 if (!base::StringToInt(str_value
, &int_value
)) {
129 LOG(ERROR
) << "The value " << str_value
<< " of "
130 << integer_switch_map_
[i
].switch_name
131 << " can not be converted to integer, ignoring!";
134 SetValue(integer_switch_map_
[i
].preference_path
,
135 make_scoped_ptr(new base::FundamentalValue(int_value
)),
136 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
140 for (size_t i
= 0; i
< arraysize(boolean_switch_map_
); ++i
) {
141 if (command_line_
->HasSwitch(boolean_switch_map_
[i
].switch_name
)) {
142 SetValue(boolean_switch_map_
[i
].preference_path
,
143 make_scoped_ptr(new base::FundamentalValue(
144 boolean_switch_map_
[i
].set_value
)),
145 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
150 void CommandLinePrefStore::ApplyProxyMode() {
151 if (command_line_
->HasSwitch(switches::kNoProxyServer
)) {
152 SetValue(proxy_config::prefs::kProxy
,
153 make_scoped_ptr(ProxyConfigDictionary::CreateDirect()),
154 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
155 } else if (command_line_
->HasSwitch(switches::kProxyPacUrl
)) {
156 std::string pac_script_url
=
157 command_line_
->GetSwitchValueASCII(switches::kProxyPacUrl
);
158 SetValue(proxy_config::prefs::kProxy
,
160 ProxyConfigDictionary::CreatePacScript(pac_script_url
, false)),
161 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
162 } else if (command_line_
->HasSwitch(switches::kProxyAutoDetect
)) {
163 SetValue(proxy_config::prefs::kProxy
,
164 make_scoped_ptr(ProxyConfigDictionary::CreateAutoDetect()),
165 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
166 } else if (command_line_
->HasSwitch(switches::kProxyServer
)) {
167 std::string proxy_server
=
168 command_line_
->GetSwitchValueASCII(switches::kProxyServer
);
169 std::string bypass_list
=
170 command_line_
->GetSwitchValueASCII(switches::kProxyBypassList
);
171 SetValue(proxy_config::prefs::kProxy
,
172 make_scoped_ptr(ProxyConfigDictionary::CreateFixedServers(
173 proxy_server
, bypass_list
)),
174 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
178 void CommandLinePrefStore::ApplySSLSwitches() {
179 if (command_line_
->HasSwitch(switches::kCipherSuiteBlacklist
)) {
180 scoped_ptr
<base::ListValue
> list_value(new base::ListValue());
181 list_value
->AppendStrings(base::SplitString(
182 command_line_
->GetSwitchValueASCII(switches::kCipherSuiteBlacklist
),
183 ",", base::TRIM_WHITESPACE
, base::SPLIT_WANT_ALL
));
184 SetValue(prefs::kCipherSuiteBlacklist
, list_value
.Pass(),
185 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
189 void CommandLinePrefStore::ApplyBackgroundModeSwitches() {
190 if (command_line_
->HasSwitch(switches::kDisableExtensions
)) {
191 SetValue(prefs::kBackgroundModeEnabled
,
192 make_scoped_ptr(new base::FundamentalValue(false)),
193 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);