Home | History | Annotate | Download | only in prefs
      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.
      4 
      5 #include "chrome/browser/prefs/command_line_pref_store.h"
      6 
      7 #include <string>
      8 #include <vector>
      9 
     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"
     18 
     19 #if defined(OS_CHROMEOS)
     20 #include "chromeos/chromeos_switches.h"
     21 #endif
     22 
     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 },
     36 };
     37 
     38 const CommandLinePrefStore::BooleanSwitchToPreferenceMapEntry
     39     CommandLinePrefStore::boolean_switch_map_[] = {
     40       { switches::kDisableAuthNegotiateCnameLookup,
     41           prefs::kDisableAuthNegotiateCnameLookup, true },
     42       { switches::kEnableAuthNegotiatePort, prefs::kEnableAuthNegotiatePort,
     43           true },
     44       { switches::kDisable3DAPIs, prefs::kDisable3DAPIs, true },
     45       { switches::kEnableCloudPrintProxy, prefs::kCloudPrintProxyEnabled,
     46           true },
     47       { switches::kAllowOutdatedPlugins, prefs::kPluginsAllowOutdated, true },
     48       { switches::kAlwaysAuthorizePlugins, prefs::kPluginsAlwaysAuthorize,
     49           true },
     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,
     59           true },
     60       { switches::kDisablePrintPreview, prefs::kPrintPreviewDisabled, true },
     61 #if defined(OS_CHROMEOS)
     62       { chromeos::switches::kEnableTouchpadThreeFingerClick,
     63           prefs::kEnableTouchpadThreeFingerClick, true },
     64 #endif
     65       { switches::kDisableAsyncDns, prefs::kBuiltInDnsClientEnabled, false },
     66       { switches::kEnableAsyncDns, prefs::kBuiltInDnsClientEnabled, true },
     67 };
     68 
     69 const CommandLinePrefStore::IntegerSwitchToPreferenceMapEntry
     70     CommandLinePrefStore::integer_switch_map_[] = {
     71       { switches::kDiskCacheSize, prefs::kDiskCacheSize },
     72       { switches::kMediaCacheSize, prefs::kMediaCacheSize },
     73     };
     74 
     75 CommandLinePrefStore::CommandLinePrefStore(const CommandLine* command_line)
     76     : command_line_(command_line) {
     77   ApplySimpleSwitches();
     78   ApplyProxyMode();
     79   ValidateProxySwitches();
     80   ApplySSLSwitches();
     81   ApplyBackgroundModeSwitches();
     82 }
     83 
     84 CommandLinePrefStore::~CommandLinePrefStore() {}
     85 
     86 bool CommandLinePrefStore::ValidateProxySwitches() {
     87   if (command_line_->HasSwitch(switches::kNoProxyServer) &&
     88       (command_line_->HasSwitch(switches::kProxyAutoDetect) ||
     89        command_line_->HasSwitch(switches::kProxyServer) ||
     90        command_line_->HasSwitch(switches::kProxyPacUrl) ||
     91        command_line_->HasSwitch(switches::kProxyBypassList))) {
     92     LOG(WARNING) << "Additional command-line proxy switches specified when --"
     93                  << switches::kNoProxyServer << " was also specified.";
     94     return false;
     95   }
     96   return true;
     97 }
     98 
     99 void CommandLinePrefStore::ApplySimpleSwitches() {
    100   // Look for each switch we know about and set its preference accordingly.
    101   for (size_t i = 0; i < arraysize(string_switch_map_); ++i) {
    102     if (command_line_->HasSwitch(string_switch_map_[i].switch_name)) {
    103       base::Value* value = base::Value::CreateStringValue(command_line_->
    104           GetSwitchValueASCII(string_switch_map_[i].switch_name));
    105       SetValue(string_switch_map_[i].preference_path, value);
    106     }
    107   }
    108 
    109   for (size_t i = 0; i < arraysize(integer_switch_map_); ++i) {
    110     if (command_line_->HasSwitch(integer_switch_map_[i].switch_name)) {
    111       std::string str_value = command_line_->GetSwitchValueASCII(
    112           integer_switch_map_[i].switch_name);
    113       int int_value = 0;
    114       if (!base::StringToInt(str_value, &int_value)) {
    115         LOG(ERROR) << "The value " << str_value << " of "
    116                    << integer_switch_map_[i].switch_name
    117                    << " can not be converted to integer, ignoring!";
    118         continue;
    119       }
    120       base::Value* value = base::Value::CreateIntegerValue(int_value);
    121       SetValue(integer_switch_map_[i].preference_path, value);
    122     }
    123   }
    124 
    125   for (size_t i = 0; i < arraysize(boolean_switch_map_); ++i) {
    126     if (command_line_->HasSwitch(boolean_switch_map_[i].switch_name)) {
    127       base::Value* value = base::Value::CreateBooleanValue(
    128           boolean_switch_map_[i].set_value);
    129       SetValue(boolean_switch_map_[i].preference_path, value);
    130     }
    131   }
    132 }
    133 
    134 void CommandLinePrefStore::ApplyProxyMode() {
    135   if (command_line_->HasSwitch(switches::kNoProxyServer)) {
    136     SetValue(prefs::kProxy,
    137              ProxyConfigDictionary::CreateDirect());
    138   } else if (command_line_->HasSwitch(switches::kProxyPacUrl)) {
    139     std::string pac_script_url =
    140         command_line_->GetSwitchValueASCII(switches::kProxyPacUrl);
    141     SetValue(prefs::kProxy,
    142              ProxyConfigDictionary::CreatePacScript(pac_script_url, false));
    143   } else if (command_line_->HasSwitch(switches::kProxyAutoDetect)) {
    144     SetValue(prefs::kProxy,
    145              ProxyConfigDictionary::CreateAutoDetect());
    146   } else if (command_line_->HasSwitch(switches::kProxyServer)) {
    147     std::string proxy_server =
    148         command_line_->GetSwitchValueASCII(switches::kProxyServer);
    149     std::string bypass_list =
    150         command_line_->GetSwitchValueASCII(switches::kProxyBypassList);
    151     SetValue(prefs::kProxy,
    152              ProxyConfigDictionary::CreateFixedServers(proxy_server,
    153                                                        bypass_list));
    154   }
    155 }
    156 
    157 void CommandLinePrefStore::ApplySSLSwitches() {
    158   if (command_line_->HasSwitch(switches::kCipherSuiteBlacklist)) {
    159     std::string cipher_suites =
    160         command_line_->GetSwitchValueASCII(switches::kCipherSuiteBlacklist);
    161     std::vector<std::string> cipher_strings;
    162     base::SplitString(cipher_suites, ',', &cipher_strings);
    163     base::ListValue* list_value = new base::ListValue();
    164     for (std::vector<std::string>::const_iterator it = cipher_strings.begin();
    165          it != cipher_strings.end(); ++it) {
    166       list_value->Append(base::Value::CreateStringValue(*it));
    167     }
    168     SetValue(prefs::kCipherSuiteBlacklist, list_value);
    169   }
    170 }
    171 
    172 void CommandLinePrefStore::ApplyBackgroundModeSwitches() {
    173   if (command_line_->HasSwitch(switches::kDisableExtensions)) {
    174     base::Value* value = base::Value::CreateBooleanValue(false);
    175     SetValue(prefs::kBackgroundModeEnabled, value);
    176   }
    177 }
    178