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 { switches::kAuthSchemes, prefs::kAuthSchemes },
28 { switches::kAuthServerWhitelist, prefs::kAuthServerWhitelist },
29 { switches::kAuthNegotiateDelegateWhitelist,
30 prefs::kAuthNegotiateDelegateWhitelist },
31 { switches::kGSSAPILibraryName, prefs::kGSSAPILibraryName },
32 { data_reduction_proxy::switches::kDataReductionProxy,
33 data_reduction_proxy::prefs::kDataReductionProxy },
34 { switches::kSSLVersionMin, prefs::kSSLVersionMin },
35 { switches::kSSLVersionMax, prefs::kSSLVersionMax },
38 const CommandLinePrefStore::PathSwitchToPreferenceMapEntry
39 CommandLinePrefStore::path_switch_map_[] = {
40 { switches::kDiskCacheDir, prefs::kDiskCacheDir },
43 const CommandLinePrefStore::BooleanSwitchToPreferenceMapEntry
44 CommandLinePrefStore::boolean_switch_map_[] = {
45 { switches::kDisableAuthNegotiateCnameLookup,
46 prefs::kDisableAuthNegotiateCnameLookup, true },
47 { switches::kEnableAuthNegotiatePort, prefs::kEnableAuthNegotiatePort,
49 { switches::kDisable3DAPIs, prefs::kDisable3DAPIs, true },
50 { switches::kEnableCloudPrintProxy, prefs::kCloudPrintProxyEnabled,
52 { switches::kAllowOutdatedPlugins, prefs::kPluginsAllowOutdated, true },
53 { switches::kAlwaysAuthorizePlugins, prefs::kPluginsAlwaysAuthorize,
55 { switches::kNoPings, prefs::kEnableHyperlinkAuditing, false },
56 { switches::kNoReferrers, prefs::kEnableReferrers, false },
57 { switches::kAllowRunningInsecureContent,
58 prefs::kWebKitAllowRunningInsecureContent, true },
59 { switches::kNoDisplayingInsecureContent,
60 prefs::kWebKitAllowDisplayingInsecureContent, false },
61 { switches::kAllowCrossOriginAuthPrompt,
62 prefs::kAllowCrossOriginAuthPrompt, true },
63 { switches::kDisablePrintPreview, prefs::kPrintPreviewDisabled, true },
64 #if defined(OS_CHROMEOS)
65 { chromeos::switches::kEnableTouchpadThreeFingerClick,
66 prefs::kEnableTouchpadThreeFingerClick, true },
68 { switches::kDisableAsyncDns, prefs::kBuiltInDnsClientEnabled, false },
69 { switches::kEnableAsyncDns, prefs::kBuiltInDnsClientEnabled, true },
72 const CommandLinePrefStore::IntegerSwitchToPreferenceMapEntry
73 CommandLinePrefStore::integer_switch_map_[] = {
74 { switches::kDiskCacheSize, prefs::kDiskCacheSize },
75 { switches::kMediaCacheSize, prefs::kMediaCacheSize },
78 CommandLinePrefStore::CommandLinePrefStore(const CommandLine* command_line)
79 : command_line_(command_line) {
80 ApplySimpleSwitches();
82 ValidateProxySwitches();
84 ApplyBackgroundModeSwitches();
87 CommandLinePrefStore::~CommandLinePrefStore() {}
89 bool CommandLinePrefStore::ValidateProxySwitches() {
90 if (command_line_->HasSwitch(switches::kNoProxyServer) &&
91 (command_line_->HasSwitch(switches::kProxyAutoDetect) ||
92 command_line_->HasSwitch(switches::kProxyServer) ||
93 command_line_->HasSwitch(switches::kProxyPacUrl) ||
94 command_line_->HasSwitch(switches::kProxyBypassList))) {
95 LOG(WARNING) << "Additional command-line proxy switches specified when --"
96 << switches::kNoProxyServer << " was also specified.";
102 void CommandLinePrefStore::ApplySimpleSwitches() {
103 // Look for each switch we know about and set its preference accordingly.
104 for (size_t i = 0; i < arraysize(string_switch_map_); ++i) {
105 if (command_line_->HasSwitch(string_switch_map_[i].switch_name)) {
106 SetValue(string_switch_map_[i].preference_path,
107 new base::StringValue(command_line_->GetSwitchValueASCII(
108 string_switch_map_[i].switch_name)));
112 for (size_t i = 0; i < arraysize(path_switch_map_); ++i) {
113 if (command_line_->HasSwitch(path_switch_map_[i].switch_name)) {
114 SetValue(path_switch_map_[i].preference_path,
115 new base::StringValue(command_line_->GetSwitchValuePath(
116 path_switch_map_[i].switch_name).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 SetValue(integer_switch_map_[i].preference_path,
132 new base::FundamentalValue(int_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 SetValue(boolean_switch_map_[i].preference_path,
139 new base::FundamentalValue(boolean_switch_map_[i].set_value));
144 void CommandLinePrefStore::ApplyProxyMode() {
145 if (command_line_->HasSwitch(switches::kNoProxyServer)) {
146 SetValue(prefs::kProxy,
147 ProxyConfigDictionary::CreateDirect());
148 } else if (command_line_->HasSwitch(switches::kProxyPacUrl)) {
149 std::string pac_script_url =
150 command_line_->GetSwitchValueASCII(switches::kProxyPacUrl);
151 SetValue(prefs::kProxy,
152 ProxyConfigDictionary::CreatePacScript(pac_script_url, false));
153 } else if (command_line_->HasSwitch(switches::kProxyAutoDetect)) {
154 SetValue(prefs::kProxy,
155 ProxyConfigDictionary::CreateAutoDetect());
156 } else if (command_line_->HasSwitch(switches::kProxyServer)) {
157 std::string proxy_server =
158 command_line_->GetSwitchValueASCII(switches::kProxyServer);
159 std::string bypass_list =
160 command_line_->GetSwitchValueASCII(switches::kProxyBypassList);
161 SetValue(prefs::kProxy,
162 ProxyConfigDictionary::CreateFixedServers(proxy_server,
167 void CommandLinePrefStore::ApplySSLSwitches() {
168 if (command_line_->HasSwitch(switches::kCipherSuiteBlacklist)) {
169 std::string cipher_suites =
170 command_line_->GetSwitchValueASCII(switches::kCipherSuiteBlacklist);
171 std::vector<std::string> cipher_strings;
172 base::SplitString(cipher_suites, ',', &cipher_strings);
173 base::ListValue* list_value = new base::ListValue();
174 for (std::vector<std::string>::const_iterator it = cipher_strings.begin();
175 it != cipher_strings.end(); ++it) {
176 list_value->Append(new base::StringValue(*it));
178 SetValue(prefs::kCipherSuiteBlacklist, list_value);
182 void CommandLinePrefStore::ApplyBackgroundModeSwitches() {
183 if (command_line_->HasSwitch(switches::kDisableExtensions))
184 SetValue(prefs::kBackgroundModeEnabled, new base::FundamentalValue(false));