ui_proxy_config_service.cc revision 868fa2fe829687343ffae624259930155e16dbd8
1868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
2868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// found in the LICENSE file.
4868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
5868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "chrome/browser/chromeos/ui_proxy_config_service.h"
6868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
7868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/logging.h"
8868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
9868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/values.h"
10868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "chrome/browser/chromeos/net/proxy_config_handler.h"
11868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "chrome/browser/chromeos/proxy_config_service_impl.h"
12868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "chromeos/network/network_state.h"
13868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "chromeos/network/network_state_handler.h"
14868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "grit/generated_resources.h"
15868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "net/proxy/proxy_config.h"
16868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
17868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)namespace chromeos {
18868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
19868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)namespace {
20868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
21868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)const char* ModeToString(UIProxyConfig::Mode mode) {
22868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  switch (mode) {
23868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    case UIProxyConfig::MODE_DIRECT:
24868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      return "direct";
25868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    case UIProxyConfig::MODE_AUTO_DETECT:
26868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      return "auto-detect";
27868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    case UIProxyConfig::MODE_PAC_SCRIPT:
28868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      return "pacurl";
29868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    case UIProxyConfig::MODE_SINGLE_PROXY:
30868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      return "single-proxy";
31868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    case UIProxyConfig::MODE_PROXY_PER_SCHEME:
32868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      return "proxy-per-scheme";
33868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
34868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  NOTREACHED() << "Unrecognized mode type";
35868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return "";
36868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
37868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
38868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Writes the proxy config of |network| to |proxy_config|.  Returns false if no
39868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// proxy was configured for this network.
40868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)bool GetProxyConfig(const NetworkState& network,
41868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                    net::ProxyConfig* proxy_config) {
42868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  scoped_ptr<ProxyConfigDictionary> proxy_dict =
43868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      proxy_config::GetProxyConfigForNetwork(network);
44868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (!proxy_dict)
45868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return false;
46868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return PrefProxyConfigTrackerImpl::PrefConfigToNetConfig(*proxy_dict,
47868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                                           proxy_config);
48868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
49868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
50868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Returns true if proxy settings of |network| are editable.
51868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)bool IsNetworkProxySettingsEditable(const NetworkState& network) {
52868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  onc::ONCSource source = network.onc_source();
53868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return source != onc::ONC_SOURCE_DEVICE_POLICY &&
54868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      source != onc::ONC_SOURCE_USER_POLICY;
55868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
56868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
57868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}  // namespace
58868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
59868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)UIProxyConfigService::UIProxyConfigService() {
60868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
61868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
62868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)UIProxyConfigService::~UIProxyConfigService() {
63868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
64868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
65868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void UIProxyConfigService::SetPrefs(PrefService* pref_service) {
66868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  pref_service_ = pref_service;
67868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
68868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
69868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void UIProxyConfigService::SetCurrentNetwork(
70868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    const std::string& current_network) {
71868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const NetworkState* network = NULL;
72868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (!current_network.empty()) {
73868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    network = NetworkHandler::Get()->network_state_handler()->GetNetworkState(
74868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        current_network);
75868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    LOG_IF(ERROR, !network)
76868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        << "Can't find requested network " << current_network;
77868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
78868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  current_ui_network_ = current_network;
79868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (!network) {
80868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    current_ui_network_.clear();
81868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    current_ui_config_ = UIProxyConfig();
82868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return;
83868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
84868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
85868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  DetermineEffectiveConfig(*network);
86868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  VLOG(1) << "Current ui network: "
87868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)          << network->name()
88868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)          << ", " << ModeToString(current_ui_config_.mode) << ", "
89868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)          << ProxyPrefs::ConfigStateToDebugString(current_ui_config_.state)
90868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)          << ", modifiable:" << current_ui_config_.user_modifiable;
91868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
92868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
93868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void UIProxyConfigService::GetProxyConfig(UIProxyConfig* config) const {
94868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  *config = current_ui_config_;
95868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
96868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
97868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void UIProxyConfigService::SetProxyConfig(const UIProxyConfig& config) {
98868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  current_ui_config_ = config;
99868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (current_ui_network_.empty())
100868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return;
101868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
102868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const NetworkState* network =
103868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      NetworkHandler::Get()->network_state_handler()->
104868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      GetNetworkState(current_ui_network_);
105868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (!network) {
106868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    LOG(ERROR) << "Can't find requested network " << current_ui_network_;
107868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return;
108868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
109868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
110868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Store config for this network.
111868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  scoped_ptr<base::DictionaryValue> proxy_config_value(
112868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      config.ToPrefProxyConfig());
113868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ProxyConfigDictionary proxy_config_dict(proxy_config_value.get());
114868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
115868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  VLOG(1) << "Set proxy for " << current_ui_network_
116868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)          << " to " << *proxy_config_value;
117868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
118868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  proxy_config::SetProxyConfigForNetwork(proxy_config_dict, *network);
119868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  current_ui_config_.state = ProxyPrefs::CONFIG_SYSTEM;
120868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
121868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
122868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void UIProxyConfigService::DetermineEffectiveConfig(
123868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    const NetworkState& network) {
124868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  DCHECK(pref_service_);
125868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
126868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Get prefs proxy config if available.
127868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  net::ProxyConfig pref_config;
128868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ProxyPrefs::ConfigState pref_state = ProxyConfigServiceImpl::ReadPrefConfig(
129868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      pref_service_, &pref_config);
130868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
131868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Get network proxy config if available.
132868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  net::ProxyConfig network_config;
133868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  net::ProxyConfigService::ConfigAvailability network_availability =
134868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      net::ProxyConfigService::CONFIG_UNSET;
135868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (chromeos::GetProxyConfig(network, &network_config)) {
136868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    // Network is private or shared with user using shared proxies.
137868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    VLOG(1) << this << ": using network proxy: " << network.proxy_config();
138868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    network_availability = net::ProxyConfigService::CONFIG_VALID;
139868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
140868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
141868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Determine effective proxy config, either from prefs or network.
142868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ProxyPrefs::ConfigState effective_config_state;
143868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  net::ProxyConfig effective_config;
144868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ProxyConfigServiceImpl::GetEffectiveProxyConfig(
145868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      pref_state, pref_config,
146868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      network_availability, network_config, false,
147868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      &effective_config_state, &effective_config);
148868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
149868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Store effective proxy into |current_ui_config_|.
150868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  current_ui_config_.FromNetProxyConfig(effective_config);
151868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  current_ui_config_.state = effective_config_state;
152868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (ProxyConfigServiceImpl::PrefPrecedes(effective_config_state)) {
153868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    current_ui_config_.user_modifiable = false;
154868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  } else if (!IsNetworkProxySettingsEditable(network)) {
155868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    // TODO(xiyuan): Figure out the right way to set config state for managed
156868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    // network.
157868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    current_ui_config_.state = ProxyPrefs::CONFIG_POLICY;
158868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    current_ui_config_.user_modifiable = false;
159868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  } else {
160868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    current_ui_config_.user_modifiable =
161868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        !ProxyConfigServiceImpl::IgnoreProxy(pref_service_,
162868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                             network.profile_path(),
163868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                             network.onc_source());
164868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
165868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
166868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
167868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}  // namespace chromeos
168