Home | History | Annotate | Download | only in prefs
      1 // Copyright (c) 2011 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/proxy_config_dictionary.h"
      6 
      7 #include "base/basictypes.h"
      8 #include "base/logging.h"
      9 #include "base/values.h"
     10 
     11 namespace {
     12 
     13 // Integer to specify the type of proxy settings.
     14 // See ProxyPrefs for possible values and interactions with the other proxy
     15 // preferences.
     16 const char kProxyMode[] = "mode";
     17 // String specifying the proxy server. For a specification of the expected
     18 // syntax see net::ProxyConfig::ProxyRules::ParseFromString().
     19 const char kProxyServer[] = "server";
     20 // URL to the proxy .pac file.
     21 const char kProxyPacUrl[] = "pac_url";
     22 // String containing proxy bypass rules. For a specification of the
     23 // expected syntax see net::ProxyBypassRules::ParseFromString().
     24 const char kProxyBypassList[] = "bypass_list";
     25 
     26 }  // namespace
     27 
     28 ProxyConfigDictionary::ProxyConfigDictionary(const DictionaryValue* dict)
     29     : dict_(dict->DeepCopy()) {
     30 }
     31 
     32 ProxyConfigDictionary::~ProxyConfigDictionary() {}
     33 
     34 bool ProxyConfigDictionary::GetMode(ProxyPrefs::ProxyMode* out) const {
     35   std::string mode_str;
     36   return dict_->GetString(kProxyMode, &mode_str)
     37       && StringToProxyMode(mode_str, out);
     38 }
     39 
     40 bool ProxyConfigDictionary::GetPacUrl(std::string* out) const {
     41   return dict_->GetString(kProxyPacUrl, out);
     42 }
     43 
     44 bool ProxyConfigDictionary::GetProxyServer(std::string* out) const {
     45   return dict_->GetString(kProxyServer, out);
     46 }
     47 
     48 bool ProxyConfigDictionary::GetBypassList(std::string* out) const {
     49   return dict_->GetString(kProxyBypassList, out);
     50 }
     51 
     52 bool ProxyConfigDictionary::HasBypassList() const {
     53   return dict_->HasKey(kProxyBypassList);
     54 }
     55 
     56 // static
     57 DictionaryValue* ProxyConfigDictionary::CreateDirect() {
     58   return CreateDictionary(ProxyPrefs::MODE_DIRECT, "", "", "");
     59 }
     60 
     61 // static
     62 DictionaryValue* ProxyConfigDictionary::CreateAutoDetect() {
     63   return CreateDictionary(ProxyPrefs::MODE_AUTO_DETECT, "", "", "");
     64 }
     65 
     66 // static
     67 DictionaryValue* ProxyConfigDictionary::CreatePacScript(
     68     const std::string& pac_url) {
     69   return CreateDictionary(ProxyPrefs::MODE_PAC_SCRIPT, pac_url, "", "");
     70 }
     71 
     72 // static
     73 DictionaryValue* ProxyConfigDictionary::CreateFixedServers(
     74     const std::string& proxy_server,
     75     const std::string& bypass_list) {
     76   if (!proxy_server.empty()) {
     77     return CreateDictionary(
     78         ProxyPrefs::MODE_FIXED_SERVERS, "", proxy_server, bypass_list);
     79   } else {
     80     return CreateDirect();
     81   }
     82 }
     83 
     84 // static
     85 DictionaryValue* ProxyConfigDictionary::CreateSystem() {
     86   return CreateDictionary(ProxyPrefs::MODE_SYSTEM, "", "", "");
     87 }
     88 
     89 // static
     90 DictionaryValue* ProxyConfigDictionary::CreateDictionary(
     91     ProxyPrefs::ProxyMode mode,
     92     const std::string& pac_url,
     93     const std::string& proxy_server,
     94     const std::string& bypass_list) {
     95   DictionaryValue* dict = new DictionaryValue();
     96   dict->SetString(kProxyMode, ProxyModeToString(mode));
     97   if (!pac_url.empty())
     98     dict->SetString(kProxyPacUrl, pac_url);
     99   if (!proxy_server.empty())
    100     dict->SetString(kProxyServer, proxy_server);
    101   if (!bypass_list.empty())
    102     dict->SetString(kProxyBypassList, bypass_list);
    103   return dict;
    104 }
    105