Home | History | Annotate | Download | only in chromeos
      1 // Copyright 2013 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/chromeos/ui_proxy_config_service.h"
      6 
      7 #include "base/logging.h"
      8 #include "base/memory/scoped_ptr.h"
      9 #include "base/values.h"
     10 #include "chrome/browser/chromeos/net/proxy_config_handler.h"
     11 #include "chrome/browser/chromeos/proxy_config_service_impl.h"
     12 #include "chromeos/network/network_state.h"
     13 #include "chromeos/network/network_state_handler.h"
     14 #include "grit/generated_resources.h"
     15 #include "net/proxy/proxy_config.h"
     16 
     17 namespace chromeos {
     18 
     19 namespace {
     20 
     21 const char* ModeToString(UIProxyConfig::Mode mode) {
     22   switch (mode) {
     23     case UIProxyConfig::MODE_DIRECT:
     24       return "direct";
     25     case UIProxyConfig::MODE_AUTO_DETECT:
     26       return "auto-detect";
     27     case UIProxyConfig::MODE_PAC_SCRIPT:
     28       return "pacurl";
     29     case UIProxyConfig::MODE_SINGLE_PROXY:
     30       return "single-proxy";
     31     case UIProxyConfig::MODE_PROXY_PER_SCHEME:
     32       return "proxy-per-scheme";
     33   }
     34   NOTREACHED() << "Unrecognized mode type";
     35   return "";
     36 }
     37 
     38 // Writes the proxy config of |network| to |proxy_config|.  Sets |onc_source| to
     39 // the source of this configuration. Returns false if no proxy was configured
     40 // for this network.
     41 bool GetProxyConfig(const PrefService* profile_prefs,
     42                     const PrefService* local_state_prefs,
     43                     const NetworkState& network,
     44                     net::ProxyConfig* proxy_config,
     45                     onc::ONCSource* onc_source) {
     46   scoped_ptr<ProxyConfigDictionary> proxy_dict =
     47       proxy_config::GetProxyConfigForNetwork(
     48           profile_prefs, local_state_prefs, network, onc_source);
     49   if (!proxy_dict)
     50     return false;
     51   return PrefProxyConfigTrackerImpl::PrefConfigToNetConfig(*proxy_dict,
     52                                                            proxy_config);
     53 }
     54 
     55 // Returns true if proxy settings from |onc_source| are editable.
     56 bool IsNetworkProxySettingsEditable(const onc::ONCSource onc_source) {
     57   return onc_source != onc::ONC_SOURCE_DEVICE_POLICY &&
     58          onc_source != onc::ONC_SOURCE_USER_POLICY;
     59 }
     60 
     61 }  // namespace
     62 
     63 UIProxyConfigService::UIProxyConfigService()
     64     : profile_prefs_(NULL), local_state_prefs_(NULL) {
     65 }
     66 
     67 UIProxyConfigService::~UIProxyConfigService() {
     68 }
     69 
     70 void UIProxyConfigService::SetPrefs(PrefService* profile_prefs,
     71                                     PrefService* local_state_prefs) {
     72   profile_prefs_ = profile_prefs;
     73   local_state_prefs_ = local_state_prefs;
     74 }
     75 
     76 void UIProxyConfigService::SetCurrentNetwork(
     77     const std::string& current_network) {
     78   current_ui_network_ = current_network;
     79 }
     80 
     81 void UIProxyConfigService::UpdateFromPrefs() {
     82   const NetworkState* network = NULL;
     83   if (!current_ui_network_.empty()) {
     84     network = NetworkHandler::Get()->network_state_handler()
     85         ->GetNetworkState(current_ui_network_);
     86     LOG_IF(ERROR, !network) << "Can't find requested network "
     87                             << current_ui_network_;
     88   }
     89   if (!network) {
     90     current_ui_network_.clear();
     91     current_ui_config_ = UIProxyConfig();
     92     return;
     93   }
     94 
     95   DetermineEffectiveConfig(*network);
     96   VLOG(1) << "Current ui network: " << network->name() << ", "
     97           << ModeToString(current_ui_config_.mode) << ", "
     98           << ProxyPrefs::ConfigStateToDebugString(current_ui_config_.state)
     99           << ", modifiable:" << current_ui_config_.user_modifiable;
    100 }
    101 
    102 void UIProxyConfigService::GetProxyConfig(UIProxyConfig* config) const {
    103   *config = current_ui_config_;
    104 }
    105 
    106 void UIProxyConfigService::SetProxyConfig(const UIProxyConfig& config) {
    107   current_ui_config_ = config;
    108   if (current_ui_network_.empty())
    109     return;
    110 
    111   const NetworkState* network =
    112       NetworkHandler::Get()->network_state_handler()->
    113       GetNetworkState(current_ui_network_);
    114   if (!network) {
    115     LOG(ERROR) << "Can't find requested network " << current_ui_network_;
    116     return;
    117   }
    118 
    119   // Store config for this network.
    120   scoped_ptr<base::DictionaryValue> proxy_config_value(
    121       config.ToPrefProxyConfig());
    122   ProxyConfigDictionary proxy_config_dict(proxy_config_value.get());
    123 
    124   VLOG(1) << "Set proxy for " << current_ui_network_
    125           << " to " << *proxy_config_value;
    126 
    127   proxy_config::SetProxyConfigForNetwork(proxy_config_dict, *network);
    128   current_ui_config_.state = ProxyPrefs::CONFIG_SYSTEM;
    129 }
    130 
    131 void UIProxyConfigService::DetermineEffectiveConfig(
    132     const NetworkState& network) {
    133   DCHECK(local_state_prefs_);
    134 
    135   // The pref service to read proxy settings that apply to all networks.
    136   // Settings from the profile overrule local state.
    137   PrefService* top_pref_service =
    138       profile_prefs_ ? profile_prefs_ : local_state_prefs_;
    139 
    140   // Get prefs proxy config if available.
    141   net::ProxyConfig pref_config;
    142   ProxyPrefs::ConfigState pref_state = ProxyConfigServiceImpl::ReadPrefConfig(
    143       top_pref_service, &pref_config);
    144 
    145   // Get network proxy config if available.
    146   net::ProxyConfig network_config;
    147   net::ProxyConfigService::ConfigAvailability network_availability =
    148       net::ProxyConfigService::CONFIG_UNSET;
    149   onc::ONCSource onc_source = onc::ONC_SOURCE_NONE;
    150   if (chromeos::GetProxyConfig(profile_prefs_,
    151                                local_state_prefs_,
    152                                network,
    153                                &network_config,
    154                                &onc_source)) {
    155     // Network is private or shared with user using shared proxies.
    156     VLOG(1) << this << ": using proxy of network: " << network.path();
    157     network_availability = net::ProxyConfigService::CONFIG_VALID;
    158   }
    159 
    160   // Determine effective proxy config, either from prefs or network.
    161   ProxyPrefs::ConfigState effective_config_state;
    162   net::ProxyConfig effective_config;
    163   ProxyConfigServiceImpl::GetEffectiveProxyConfig(
    164       pref_state, pref_config,
    165       network_availability, network_config, false,
    166       &effective_config_state, &effective_config);
    167 
    168   // Store effective proxy into |current_ui_config_|.
    169   current_ui_config_.FromNetProxyConfig(effective_config);
    170   current_ui_config_.state = effective_config_state;
    171   if (ProxyConfigServiceImpl::PrefPrecedes(effective_config_state)) {
    172     current_ui_config_.user_modifiable = false;
    173   } else if (!IsNetworkProxySettingsEditable(onc_source)) {
    174     current_ui_config_.state = ProxyPrefs::CONFIG_POLICY;
    175     current_ui_config_.user_modifiable = false;
    176   } else {
    177     current_ui_config_.user_modifiable = !ProxyConfigServiceImpl::IgnoreProxy(
    178         profile_prefs_, network.profile_path(), onc_source);
    179   }
    180 }
    181 
    182 }  // namespace chromeos
    183