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 { switches::kSpdyProxyAuthOrigin
, prefs::kSpdyProxyAuthOrigin
},
32 { switches::kDiskCacheDir
, prefs::kDiskCacheDir
},
33 { switches::kSSLVersionMin
, prefs::kSSLVersionMin
},
34 { switches::kSSLVersionMax
, prefs::kSSLVersionMax
},
37 const CommandLinePrefStore::BooleanSwitchToPreferenceMapEntry
38 CommandLinePrefStore::boolean_switch_map_
[] = {
39 { switches::kDisableAuthNegotiateCnameLookup
,
40 prefs::kDisableAuthNegotiateCnameLookup
, true },
41 { switches::kEnableAuthNegotiatePort
, prefs::kEnableAuthNegotiatePort
,
43 { switches::kDisable3DAPIs
, prefs::kDisable3DAPIs
, true },
44 { switches::kEnableCloudPrintProxy
, prefs::kCloudPrintProxyEnabled
,
46 { switches::kAllowOutdatedPlugins
, prefs::kPluginsAllowOutdated
, true },
47 { switches::kAlwaysAuthorizePlugins
, prefs::kPluginsAlwaysAuthorize
,
49 { switches::kNoPings
, prefs::kEnableHyperlinkAuditing
, false },
50 { switches::kNoReferrers
, prefs::kEnableReferrers
, false },
51 { switches::kAllowRunningInsecureContent
,
52 prefs::kWebKitAllowRunningInsecureContent
, true },
53 { switches::kNoDisplayingInsecureContent
,
54 prefs::kWebKitAllowDisplayingInsecureContent
, false },
55 { switches::kAllowCrossOriginAuthPrompt
,
56 prefs::kAllowCrossOriginAuthPrompt
, true },
57 { switches::kDisableTLSChannelID
, prefs::kEnableOriginBoundCerts
, false },
58 { switches::kDisableSSLFalseStart
, prefs::kDisableSSLRecordSplitting
,
60 { switches::kEnableUnrestrictedSSL3Fallback
,
61 prefs::kEnableUnrestrictedSSL3Fallback
, true },
62 { switches::kEnableMemoryInfo
, prefs::kEnableMemoryInfo
, true },
63 #if defined(GOOGLE_CHROME_BUILD)
64 { switches::kDisablePrintPreview
, prefs::kPrintPreviewDisabled
, true },
66 { switches::kEnablePrintPreview
, prefs::kPrintPreviewDisabled
, false },
68 #if defined(OS_CHROMEOS)
69 { chromeos::switches::kDisableDrive
, prefs::kDisableDrive
, true },
70 { chromeos::switches::kEnableTouchpadThreeFingerClick
,
71 prefs::kEnableTouchpadThreeFingerClick
, true },
73 { switches::kDisableAsyncDns
, prefs::kBuiltInDnsClientEnabled
, false },
74 { switches::kEnableAsyncDns
, prefs::kBuiltInDnsClientEnabled
, true },
77 const CommandLinePrefStore::IntegerSwitchToPreferenceMapEntry
78 CommandLinePrefStore::integer_switch_map_
[] = {
79 { switches::kDiskCacheSize
, prefs::kDiskCacheSize
},
80 { switches::kMediaCacheSize
, prefs::kMediaCacheSize
},
81 #if defined(OS_CHROMEOS)
82 { chromeos::switches::kDeviceRegistered
, prefs::kDeviceRegistered
},
86 CommandLinePrefStore::CommandLinePrefStore(const CommandLine
* command_line
)
87 : command_line_(command_line
) {
88 ApplySimpleSwitches();
90 ValidateProxySwitches();
92 ApplyBackgroundModeSwitches();
95 CommandLinePrefStore::~CommandLinePrefStore() {}
97 bool CommandLinePrefStore::ValidateProxySwitches() {
98 if (command_line_
->HasSwitch(switches::kNoProxyServer
) &&
99 (command_line_
->HasSwitch(switches::kProxyAutoDetect
) ||
100 command_line_
->HasSwitch(switches::kProxyServer
) ||
101 command_line_
->HasSwitch(switches::kProxyPacUrl
) ||
102 command_line_
->HasSwitch(switches::kProxyBypassList
))) {
103 LOG(WARNING
) << "Additional command-line proxy switches specified when --"
104 << switches::kNoProxyServer
<< " was also specified.";
110 void CommandLinePrefStore::ApplySimpleSwitches() {
111 // Look for each switch we know about and set its preference accordingly.
112 for (size_t i
= 0; i
< arraysize(string_switch_map_
); ++i
) {
113 if (command_line_
->HasSwitch(string_switch_map_
[i
].switch_name
)) {
114 base::Value
* value
= base::Value::CreateStringValue(command_line_
->
115 GetSwitchValueASCII(string_switch_map_
[i
].switch_name
));
116 SetValue(string_switch_map_
[i
].preference_path
, value
);
120 for (size_t i
= 0; i
< arraysize(integer_switch_map_
); ++i
) {
121 if (command_line_
->HasSwitch(integer_switch_map_
[i
].switch_name
)) {
122 std::string str_value
= command_line_
->GetSwitchValueASCII(
123 integer_switch_map_
[i
].switch_name
);
125 if (!base::StringToInt(str_value
, &int_value
)) {
126 LOG(ERROR
) << "The value " << str_value
<< " of "
127 << integer_switch_map_
[i
].switch_name
128 << " can not be converted to integer, ignoring!";
131 base::Value
* value
= base::Value::CreateIntegerValue(int_value
);
132 SetValue(integer_switch_map_
[i
].preference_path
, value
);
136 for (size_t i
= 0; i
< arraysize(boolean_switch_map_
); ++i
) {
137 if (command_line_
->HasSwitch(boolean_switch_map_
[i
].switch_name
)) {
138 base::Value
* value
= base::Value::CreateBooleanValue(
139 boolean_switch_map_
[i
].set_value
);
140 SetValue(boolean_switch_map_
[i
].preference_path
, value
);
145 void CommandLinePrefStore::ApplyProxyMode() {
146 if (command_line_
->HasSwitch(switches::kNoProxyServer
)) {
147 SetValue(prefs::kProxy
,
148 ProxyConfigDictionary::CreateDirect());
149 } else if (command_line_
->HasSwitch(switches::kProxyPacUrl
)) {
150 std::string pac_script_url
=
151 command_line_
->GetSwitchValueASCII(switches::kProxyPacUrl
);
152 SetValue(prefs::kProxy
,
153 ProxyConfigDictionary::CreatePacScript(pac_script_url
, false));
154 } else if (command_line_
->HasSwitch(switches::kProxyAutoDetect
)) {
155 SetValue(prefs::kProxy
,
156 ProxyConfigDictionary::CreateAutoDetect());
157 } else if (command_line_
->HasSwitch(switches::kProxyServer
)) {
158 std::string proxy_server
=
159 command_line_
->GetSwitchValueASCII(switches::kProxyServer
);
160 std::string bypass_list
=
161 command_line_
->GetSwitchValueASCII(switches::kProxyBypassList
);
162 SetValue(prefs::kProxy
,
163 ProxyConfigDictionary::CreateFixedServers(proxy_server
,
168 void CommandLinePrefStore::ApplySSLSwitches() {
169 if (command_line_
->HasSwitch(switches::kCipherSuiteBlacklist
)) {
170 std::string cipher_suites
=
171 command_line_
->GetSwitchValueASCII(switches::kCipherSuiteBlacklist
);
172 std::vector
<std::string
> cipher_strings
;
173 base::SplitString(cipher_suites
, ',', &cipher_strings
);
174 base::ListValue
* list_value
= new base::ListValue();
175 for (std::vector
<std::string
>::const_iterator it
= cipher_strings
.begin();
176 it
!= cipher_strings
.end(); ++it
) {
177 list_value
->Append(base::Value::CreateStringValue(*it
));
179 SetValue(prefs::kCipherSuiteBlacklist
, list_value
);
183 void CommandLinePrefStore::ApplyBackgroundModeSwitches() {
184 if (command_line_
->HasSwitch(switches::kDisableBackgroundMode
) ||
185 command_line_
->HasSwitch(switches::kDisableExtensions
)) {
186 base::Value
* value
= base::Value::CreateBooleanValue(false);
187 SetValue(prefs::kBackgroundModeEnabled
, value
);