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/browser/prefs/proxy_config_dictionary.h"
16 #include "chrome/common/chrome_switches.h"
17 #include "chrome/common/pref_names.h"
18 #include "ui/base/ui_base_switches.h"
20 #if defined(OS_CHROMEOS)
21 #include "chromeos/chromeos_switches.h"
24 const CommandLinePrefStore::StringSwitchToPreferenceMapEntry
25 CommandLinePrefStore::string_switch_map_
[] = {
26 { switches::kLang
, prefs::kApplicationLocale
},
27 { data_reduction_proxy::switches::kDataReductionProxy
,
28 data_reduction_proxy::prefs::kDataReductionProxy
},
29 { switches::kSSLVersionMin
, prefs::kSSLVersionMin
},
30 { switches::kSSLVersionMax
, prefs::kSSLVersionMax
},
31 { switches::kSSLVersionFallbackMin
, prefs::kSSLVersionFallbackMin
},
34 const CommandLinePrefStore::PathSwitchToPreferenceMapEntry
35 CommandLinePrefStore::path_switch_map_
[] = {
36 { switches::kDiskCacheDir
, prefs::kDiskCacheDir
},
39 const CommandLinePrefStore::BooleanSwitchToPreferenceMapEntry
40 CommandLinePrefStore::boolean_switch_map_
[] = {
41 { switches::kDisable3DAPIs
, prefs::kDisable3DAPIs
, true },
42 { switches::kEnableCloudPrintProxy
, prefs::kCloudPrintProxyEnabled
,
44 { switches::kAllowOutdatedPlugins
, prefs::kPluginsAllowOutdated
, true },
45 { switches::kAlwaysAuthorizePlugins
, prefs::kPluginsAlwaysAuthorize
,
47 { switches::kNoPings
, prefs::kEnableHyperlinkAuditing
, false },
48 { switches::kNoReferrers
, prefs::kEnableReferrers
, false },
49 { switches::kAllowRunningInsecureContent
,
50 prefs::kWebKitAllowRunningInsecureContent
, true },
51 { switches::kNoDisplayingInsecureContent
,
52 prefs::kWebKitAllowDisplayingInsecureContent
, false },
53 { switches::kAllowCrossOriginAuthPrompt
,
54 prefs::kAllowCrossOriginAuthPrompt
, true },
55 { switches::kDisablePrintPreview
, prefs::kPrintPreviewDisabled
, true },
56 #if defined(OS_CHROMEOS)
57 { chromeos::switches::kEnableTouchpadThreeFingerClick
,
58 prefs::kEnableTouchpadThreeFingerClick
, true },
60 { switches::kDisableAsyncDns
, prefs::kBuiltInDnsClientEnabled
, false },
61 { switches::kEnableAsyncDns
, prefs::kBuiltInDnsClientEnabled
, true },
64 const CommandLinePrefStore::IntegerSwitchToPreferenceMapEntry
65 CommandLinePrefStore::integer_switch_map_
[] = {
66 { switches::kDiskCacheSize
, prefs::kDiskCacheSize
},
67 { switches::kMediaCacheSize
, prefs::kMediaCacheSize
},
70 CommandLinePrefStore::CommandLinePrefStore(
71 const base::CommandLine
* command_line
)
72 : command_line_(command_line
) {
73 ApplySimpleSwitches();
75 ValidateProxySwitches();
77 ApplyBackgroundModeSwitches();
80 CommandLinePrefStore::~CommandLinePrefStore() {}
82 bool CommandLinePrefStore::ValidateProxySwitches() {
83 if (command_line_
->HasSwitch(switches::kNoProxyServer
) &&
84 (command_line_
->HasSwitch(switches::kProxyAutoDetect
) ||
85 command_line_
->HasSwitch(switches::kProxyServer
) ||
86 command_line_
->HasSwitch(switches::kProxyPacUrl
) ||
87 command_line_
->HasSwitch(switches::kProxyBypassList
))) {
88 LOG(WARNING
) << "Additional command-line proxy switches specified when --"
89 << switches::kNoProxyServer
<< " was also specified.";
95 void CommandLinePrefStore::ApplySimpleSwitches() {
96 // Look for each switch we know about and set its preference accordingly.
97 for (size_t i
= 0; i
< arraysize(string_switch_map_
); ++i
) {
98 if (command_line_
->HasSwitch(string_switch_map_
[i
].switch_name
)) {
99 SetValue(string_switch_map_
[i
].preference_path
,
100 new base::StringValue(command_line_
->GetSwitchValueASCII(
101 string_switch_map_
[i
].switch_name
)));
105 for (size_t i
= 0; i
< arraysize(path_switch_map_
); ++i
) {
106 if (command_line_
->HasSwitch(path_switch_map_
[i
].switch_name
)) {
107 SetValue(path_switch_map_
[i
].preference_path
,
108 new base::StringValue(command_line_
->GetSwitchValuePath(
109 path_switch_map_
[i
].switch_name
).value()));
113 for (size_t i
= 0; i
< arraysize(integer_switch_map_
); ++i
) {
114 if (command_line_
->HasSwitch(integer_switch_map_
[i
].switch_name
)) {
115 std::string str_value
= command_line_
->GetSwitchValueASCII(
116 integer_switch_map_
[i
].switch_name
);
118 if (!base::StringToInt(str_value
, &int_value
)) {
119 LOG(ERROR
) << "The value " << str_value
<< " of "
120 << integer_switch_map_
[i
].switch_name
121 << " can not be converted to integer, ignoring!";
124 SetValue(integer_switch_map_
[i
].preference_path
,
125 new base::FundamentalValue(int_value
));
129 for (size_t i
= 0; i
< arraysize(boolean_switch_map_
); ++i
) {
130 if (command_line_
->HasSwitch(boolean_switch_map_
[i
].switch_name
)) {
131 SetValue(boolean_switch_map_
[i
].preference_path
,
132 new base::FundamentalValue(boolean_switch_map_
[i
].set_value
));
137 void CommandLinePrefStore::ApplyProxyMode() {
138 if (command_line_
->HasSwitch(switches::kNoProxyServer
)) {
139 SetValue(prefs::kProxy
,
140 ProxyConfigDictionary::CreateDirect());
141 } else if (command_line_
->HasSwitch(switches::kProxyPacUrl
)) {
142 std::string pac_script_url
=
143 command_line_
->GetSwitchValueASCII(switches::kProxyPacUrl
);
144 SetValue(prefs::kProxy
,
145 ProxyConfigDictionary::CreatePacScript(pac_script_url
, false));
146 } else if (command_line_
->HasSwitch(switches::kProxyAutoDetect
)) {
147 SetValue(prefs::kProxy
,
148 ProxyConfigDictionary::CreateAutoDetect());
149 } else if (command_line_
->HasSwitch(switches::kProxyServer
)) {
150 std::string proxy_server
=
151 command_line_
->GetSwitchValueASCII(switches::kProxyServer
);
152 std::string bypass_list
=
153 command_line_
->GetSwitchValueASCII(switches::kProxyBypassList
);
154 SetValue(prefs::kProxy
,
155 ProxyConfigDictionary::CreateFixedServers(proxy_server
,
160 void CommandLinePrefStore::ApplySSLSwitches() {
161 if (command_line_
->HasSwitch(switches::kCipherSuiteBlacklist
)) {
162 std::string cipher_suites
=
163 command_line_
->GetSwitchValueASCII(switches::kCipherSuiteBlacklist
);
164 std::vector
<std::string
> cipher_strings
;
165 base::SplitString(cipher_suites
, ',', &cipher_strings
);
166 base::ListValue
* list_value
= new base::ListValue();
167 for (std::vector
<std::string
>::const_iterator it
= cipher_strings
.begin();
168 it
!= cipher_strings
.end(); ++it
) {
169 list_value
->Append(new base::StringValue(*it
));
171 SetValue(prefs::kCipherSuiteBlacklist
, list_value
);
175 void CommandLinePrefStore::ApplyBackgroundModeSwitches() {
176 if (command_line_
->HasSwitch(switches::kDisableExtensions
))
177 SetValue(prefs::kBackgroundModeEnabled
, new base::FundamentalValue(false));