summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/ui_proxy_config_service.cc
blob: 0f6988a65c2a2c769d7d302e78c8ee4267007526 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/chromeos/ui_proxy_config_service.h"

#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/values.h"
#include "chrome/browser/chromeos/net/proxy_config_handler.h"
#include "chrome/browser/chromeos/proxy_config_service_impl.h"
#include "chromeos/network/network_state.h"
#include "chromeos/network/network_state_handler.h"
#include "grit/generated_resources.h"
#include "net/proxy/proxy_config.h"

namespace chromeos {

namespace {

const char* ModeToString(UIProxyConfig::Mode mode) {
  switch (mode) {
    case UIProxyConfig::MODE_DIRECT:
      return "direct";
    case UIProxyConfig::MODE_AUTO_DETECT:
      return "auto-detect";
    case UIProxyConfig::MODE_PAC_SCRIPT:
      return "pacurl";
    case UIProxyConfig::MODE_SINGLE_PROXY:
      return "single-proxy";
    case UIProxyConfig::MODE_PROXY_PER_SCHEME:
      return "proxy-per-scheme";
  }
  NOTREACHED() << "Unrecognized mode type";
  return "";
}

// Writes the proxy config of |network| to |proxy_config|.  Returns false if no
// proxy was configured for this network.
bool GetProxyConfig(const NetworkState& network,
                    net::ProxyConfig* proxy_config) {
  scoped_ptr<ProxyConfigDictionary> proxy_dict =
      proxy_config::GetProxyConfigForNetwork(network);
  if (!proxy_dict)
    return false;
  return PrefProxyConfigTrackerImpl::PrefConfigToNetConfig(*proxy_dict,
                                                           proxy_config);
}

// Returns true if proxy settings of |network| are editable.
bool IsNetworkProxySettingsEditable(const NetworkState& network) {
  onc::ONCSource source = network.onc_source();
  return source != onc::ONC_SOURCE_DEVICE_POLICY &&
      source != onc::ONC_SOURCE_USER_POLICY;
}

}  // namespace

UIProxyConfigService::UIProxyConfigService() {
}

UIProxyConfigService::~UIProxyConfigService() {
}

void UIProxyConfigService::SetPrefs(PrefService* pref_service) {
  pref_service_ = pref_service;
}

void UIProxyConfigService::SetCurrentNetwork(
    const std::string& current_network) {
  const NetworkState* network = NULL;
  if (!current_network.empty()) {
    network = NetworkHandler::Get()->network_state_handler()->GetNetworkState(
        current_network);
    LOG_IF(ERROR, !network)
        << "Can't find requested network " << current_network;
  }
  current_ui_network_ = current_network;
  if (!network) {
    current_ui_network_.clear();
    current_ui_config_ = UIProxyConfig();
    return;
  }

  DetermineEffectiveConfig(*network);
  VLOG(1) << "Current ui network: "
          << network->name()
          << ", " << ModeToString(current_ui_config_.mode) << ", "
          << ProxyPrefs::ConfigStateToDebugString(current_ui_config_.state)
          << ", modifiable:" << current_ui_config_.user_modifiable;
}

void UIProxyConfigService::GetProxyConfig(UIProxyConfig* config) const {
  *config = current_ui_config_;
}

void UIProxyConfigService::SetProxyConfig(const UIProxyConfig& config) {
  current_ui_config_ = config;
  if (current_ui_network_.empty())
    return;

  const NetworkState* network =
      NetworkHandler::Get()->network_state_handler()->
      GetNetworkState(current_ui_network_);
  if (!network) {
    LOG(ERROR) << "Can't find requested network " << current_ui_network_;
    return;
  }

  // Store config for this network.
  scoped_ptr<base::DictionaryValue> proxy_config_value(
      config.ToPrefProxyConfig());
  ProxyConfigDictionary proxy_config_dict(proxy_config_value.get());

  VLOG(1) << "Set proxy for " << current_ui_network_
          << " to " << *proxy_config_value;

  proxy_config::SetProxyConfigForNetwork(proxy_config_dict, *network);
  current_ui_config_.state = ProxyPrefs::CONFIG_SYSTEM;
}

void UIProxyConfigService::DetermineEffectiveConfig(
    const NetworkState& network) {
  DCHECK(pref_service_);

  // Get prefs proxy config if available.
  net::ProxyConfig pref_config;
  ProxyPrefs::ConfigState pref_state = ProxyConfigServiceImpl::ReadPrefConfig(
      pref_service_, &pref_config);

  // Get network proxy config if available.
  net::ProxyConfig network_config;
  net::ProxyConfigService::ConfigAvailability network_availability =
      net::ProxyConfigService::CONFIG_UNSET;
  if (chromeos::GetProxyConfig(network, &network_config)) {
    // Network is private or shared with user using shared proxies.
    VLOG(1) << this << ": using network proxy: " << network.proxy_config();
    network_availability = net::ProxyConfigService::CONFIG_VALID;
  }

  // Determine effective proxy config, either from prefs or network.
  ProxyPrefs::ConfigState effective_config_state;
  net::ProxyConfig effective_config;
  ProxyConfigServiceImpl::GetEffectiveProxyConfig(
      pref_state, pref_config,
      network_availability, network_config, false,
      &effective_config_state, &effective_config);

  // Store effective proxy into |current_ui_config_|.
  current_ui_config_.FromNetProxyConfig(effective_config);
  current_ui_config_.state = effective_config_state;
  if (ProxyConfigServiceImpl::PrefPrecedes(effective_config_state)) {
    current_ui_config_.user_modifiable = false;
  } else if (!IsNetworkProxySettingsEditable(network)) {
    // TODO(xiyuan): Figure out the right way to set config state for managed
    // network.
    current_ui_config_.state = ProxyPrefs::CONFIG_POLICY;
    current_ui_config_.user_modifiable = false;
  } else {
    current_ui_config_.user_modifiable =
        !ProxyConfigServiceImpl::IgnoreProxy(pref_service_,
                                             network.profile_path(),
                                             network.onc_source());
  }
}

}  // namespace chromeos