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/logging.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/string_split.h"
13 #include "base/values.h"
14 #include "chrome/browser/prefs/proxy_config_dictionary.h"
15 #include "chrome/common/chrome_switches.h"
16 #include "chrome/common/pref_names.h"
17 #include "ui/base/ui_base_switches.h"
19 #if defined(OS_CHROMEOS)
20 #include "chromeos/chromeos_switches.h"
23 const CommandLinePrefStore::StringSwitchToPreferenceMapEntry
24 CommandLinePrefStore::string_switch_map_
[] = {
25 { switches::kLang
, prefs::kApplicationLocale
},
26 { switches::kAuthSchemes
, prefs::kAuthSchemes
},
27 { switches::kAuthServerWhitelist
, prefs::kAuthServerWhitelist
},
28 { switches::kAuthNegotiateDelegateWhitelist
,
29 prefs::kAuthNegotiateDelegateWhitelist
},
30 { switches::kGSSAPILibraryName
, prefs::kGSSAPILibraryName
},
31 { data_reduction_proxy::switches::kDataReductionProxy
,
32 data_reduction_proxy::prefs::kDataReductionProxy
},
33 { switches::kDiskCacheDir
, prefs::kDiskCacheDir
},
34 { switches::kSSLVersionMin
, prefs::kSSLVersionMin
},
35 { switches::kSSLVersionMax
, prefs::kSSLVersionMax
},
38 const CommandLinePrefStore::BooleanSwitchToPreferenceMapEntry
39 CommandLinePrefStore::boolean_switch_map_
[] = {
40 { switches::kDisableAuthNegotiateCnameLookup
,
41 prefs::kDisableAuthNegotiateCnameLookup
, true },
42 { switches::kEnableAuthNegotiatePort
, prefs::kEnableAuthNegotiatePort
,
44 { switches::kDisable3DAPIs
, prefs::kDisable3DAPIs
, true },
45 { switches::kEnableCloudPrintProxy
, prefs::kCloudPrintProxyEnabled
,
47 { switches::kAllowOutdatedPlugins
, prefs::kPluginsAllowOutdated
, true },
48 { switches::kAlwaysAuthorizePlugins
, prefs::kPluginsAlwaysAuthorize
,
50 { switches::kNoPings
, prefs::kEnableHyperlinkAuditing
, false },
51 { switches::kNoReferrers
, prefs::kEnableReferrers
, false },
52 { switches::kAllowRunningInsecureContent
,
53 prefs::kWebKitAllowRunningInsecureContent
, true },
54 { switches::kNoDisplayingInsecureContent
,
55 prefs::kWebKitAllowDisplayingInsecureContent
, false },
56 { switches::kAllowCrossOriginAuthPrompt
,
57 prefs::kAllowCrossOriginAuthPrompt
, true },
58 { switches::kDisableSSLFalseStart
, prefs::kDisableSSLRecordSplitting
,
60 #if defined(GOOGLE_CHROME_BUILD)
61 { switches::kDisablePrintPreview
, prefs::kPrintPreviewDisabled
, true },
63 { switches::kEnablePrintPreview
, prefs::kPrintPreviewDisabled
, false },
65 #if defined(OS_CHROMEOS)
66 { chromeos::switches::kEnableTouchpadThreeFingerClick
,
67 prefs::kEnableTouchpadThreeFingerClick
, true },
69 { switches::kDisableAsyncDns
, prefs::kBuiltInDnsClientEnabled
, false },
70 { switches::kEnableAsyncDns
, prefs::kBuiltInDnsClientEnabled
, true },
73 const CommandLinePrefStore::IntegerSwitchToPreferenceMapEntry
74 CommandLinePrefStore::integer_switch_map_
[] = {
75 { switches::kDiskCacheSize
, prefs::kDiskCacheSize
},
76 { switches::kMediaCacheSize
, prefs::kMediaCacheSize
},
79 CommandLinePrefStore::CommandLinePrefStore(const 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 base::Value
* value
= base::Value::CreateStringValue(command_line_
->
108 GetSwitchValueASCII(string_switch_map_
[i
].switch_name
));
109 SetValue(string_switch_map_
[i
].preference_path
, 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 base::Value
* value
= base::Value::CreateIntegerValue(int_value
);
125 SetValue(integer_switch_map_
[i
].preference_path
, 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 base::Value
* value
= base::Value::CreateBooleanValue(
132 boolean_switch_map_
[i
].set_value
);
133 SetValue(boolean_switch_map_
[i
].preference_path
, value
);
138 void CommandLinePrefStore::ApplyProxyMode() {
139 if (command_line_
->HasSwitch(switches::kNoProxyServer
)) {
140 SetValue(prefs::kProxy
,
141 ProxyConfigDictionary::CreateDirect());
142 } else if (command_line_
->HasSwitch(switches::kProxyPacUrl
)) {
143 std::string pac_script_url
=
144 command_line_
->GetSwitchValueASCII(switches::kProxyPacUrl
);
145 SetValue(prefs::kProxy
,
146 ProxyConfigDictionary::CreatePacScript(pac_script_url
, false));
147 } else if (command_line_
->HasSwitch(switches::kProxyAutoDetect
)) {
148 SetValue(prefs::kProxy
,
149 ProxyConfigDictionary::CreateAutoDetect());
150 } else if (command_line_
->HasSwitch(switches::kProxyServer
)) {
151 std::string proxy_server
=
152 command_line_
->GetSwitchValueASCII(switches::kProxyServer
);
153 std::string bypass_list
=
154 command_line_
->GetSwitchValueASCII(switches::kProxyBypassList
);
155 SetValue(prefs::kProxy
,
156 ProxyConfigDictionary::CreateFixedServers(proxy_server
,
161 void CommandLinePrefStore::ApplySSLSwitches() {
162 if (command_line_
->HasSwitch(switches::kCipherSuiteBlacklist
)) {
163 std::string cipher_suites
=
164 command_line_
->GetSwitchValueASCII(switches::kCipherSuiteBlacklist
);
165 std::vector
<std::string
> cipher_strings
;
166 base::SplitString(cipher_suites
, ',', &cipher_strings
);
167 base::ListValue
* list_value
= new base::ListValue();
168 for (std::vector
<std::string
>::const_iterator it
= cipher_strings
.begin();
169 it
!= cipher_strings
.end(); ++it
) {
170 list_value
->Append(base::Value::CreateStringValue(*it
));
172 SetValue(prefs::kCipherSuiteBlacklist
, list_value
);
176 void CommandLinePrefStore::ApplyBackgroundModeSwitches() {
177 if (command_line_
->HasSwitch(switches::kDisableBackgroundMode
) ||
178 command_line_
->HasSwitch(switches::kDisableExtensions
)) {
179 base::Value
* value
= base::Value::CreateBooleanValue(false);
180 SetValue(prefs::kBackgroundModeEnabled
, value
);