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       { switches::kSpdyProxyAuthOrigin, prefs::kSpdyProxyAuthOrigin },
     32       { switches::kDiskCacheDir, prefs::kDiskCacheDir },
     33       { switches::kSSLVersionMin, prefs::kSSLVersionMin },
     34       { switches::kSSLVersionMax, prefs::kSSLVersionMax },
     35 };
     36 
     37 const CommandLinePrefStore::BooleanSwitchToPreferenceMapEntry
     38     CommandLinePrefStore::boolean_switch_map_[] = {
     39       { switches::kDisableAuthNegotiateCnameLookup,
     40           prefs::kDisableAuthNegotiateCnameLookup, true },
     41       { switches::kEnableAuthNegotiatePort, prefs::kEnableAuthNegotiatePort,
     42           true },
     43       { switches::kDisable3DAPIs, prefs::kDisable3DAPIs, true },
     44       { switches::kEnableCloudPrintProxy, prefs::kCloudPrintProxyEnabled,
     45           true },
     46       { switches::kAllowOutdatedPlugins, prefs::kPluginsAllowOutdated, true },
     47       { switches::kAlwaysAuthorizePlugins, prefs::kPluginsAlwaysAuthorize,
     48           true },
     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,
     59           true },
     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 },
     65 #else
     66       { switches::kEnablePrintPreview, prefs::kPrintPreviewDisabled, false },
     67 #endif
     68 #if defined(OS_CHROMEOS)
     69       { chromeos::switches::kDisableDrive, prefs::kDisableDrive, true },
     70       { chromeos::switches::kEnableTouchpadThreeFingerClick,
     71           prefs::kEnableTouchpadThreeFingerClick, true },
     72 #endif
     73       { switches::kDisableCloudPolicyOnSignin,
     74           prefs::kDisableCloudPolicyOnSignin, true },
     75       { switches::kDisableAsyncDns, prefs::kBuiltInDnsClientEnabled, false },
     76       { switches::kEnableAsyncDns, prefs::kBuiltInDnsClientEnabled, true },
     77 };
     78 
     79 const CommandLinePrefStore::IntegerSwitchToPreferenceMapEntry
     80     CommandLinePrefStore::integer_switch_map_[] = {
     81       { switches::kDiskCacheSize, prefs::kDiskCacheSize },
     82       { switches::kMediaCacheSize, prefs::kMediaCacheSize },
     83     };
     84 
     85 CommandLinePrefStore::CommandLinePrefStore(const CommandLine* command_line)
     86     : command_line_(command_line) {
     87   ApplySimpleSwitches();
     88   ApplyProxyMode();
     89   ValidateProxySwitches();
     90   ApplySSLSwitches();
     91   ApplyBackgroundModeSwitches();
     92 }
     93 
     94 CommandLinePrefStore::~CommandLinePrefStore() {}
     95 
     96 bool CommandLinePrefStore::ValidateProxySwitches() {
     97   if (command_line_->HasSwitch(switches::kNoProxyServer) &&
     98       (command_line_->HasSwitch(switches::kProxyAutoDetect) ||
     99        command_line_->HasSwitch(switches::kProxyServer) ||
    100        command_line_->HasSwitch(switches::kProxyPacUrl) ||
    101        command_line_->HasSwitch(switches::kProxyBypassList))) {
    102     LOG(WARNING) << "Additional command-line proxy switches specified when --"
    103                  << switches::kNoProxyServer << " was also specified.";
    104     return false;
    105   }
    106   return true;
    107 }
    108 
    109 void CommandLinePrefStore::ApplySimpleSwitches() {
    110   // Look for each switch we know about and set its preference accordingly.
    111   for (size_t i = 0; i < arraysize(string_switch_map_); ++i) {
    112     if (command_line_->HasSwitch(string_switch_map_[i].switch_name)) {
    113       Value* value = Value::CreateStringValue(command_line_->
    114           GetSwitchValueASCII(string_switch_map_[i].switch_name));
    115       SetValue(string_switch_map_[i].preference_path, value);
    116     }
    117   }
    118 
    119   for (size_t i = 0; i < arraysize(integer_switch_map_); ++i) {
    120     if (command_line_->HasSwitch(integer_switch_map_[i].switch_name)) {
    121       std::string str_value = command_line_->GetSwitchValueASCII(
    122           integer_switch_map_[i].switch_name);
    123       int int_value = 0;
    124       if (!base::StringToInt(str_value, &int_value)) {
    125         LOG(ERROR) << "The value " << str_value << " of "
    126                    << integer_switch_map_[i].switch_name
    127                    << " can not be converted to integer, ignoring!";
    128         continue;
    129       }
    130       Value* value = Value::CreateIntegerValue(int_value);
    131       SetValue(integer_switch_map_[i].preference_path, value);
    132     }
    133   }
    134 
    135   for (size_t i = 0; i < arraysize(boolean_switch_map_); ++i) {
    136     if (command_line_->HasSwitch(boolean_switch_map_[i].switch_name)) {
    137       Value* value = Value::CreateBooleanValue(
    138           boolean_switch_map_[i].set_value);
    139       SetValue(boolean_switch_map_[i].preference_path, value);
    140     }
    141   }
    142 }
    143 
    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,
    163                                                        bypass_list));
    164   }
    165 }
    166 
    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(base::Value::CreateStringValue(*it));
    177     }
    178     SetValue(prefs::kCipherSuiteBlacklist, list_value);
    179   }
    180 }
    181 
    182 void CommandLinePrefStore::ApplyBackgroundModeSwitches() {
    183   if (command_line_->HasSwitch(switches::kDisableBackgroundMode) ||
    184       command_line_->HasSwitch(switches::kDisableExtensions)) {
    185     Value* value = Value::CreateBooleanValue(false);
    186     SetValue(prefs::kBackgroundModeEnabled, value);
    187   }
    188 }
    189