Changes for start_with_ext Telemetry test to work on Windows.
[chromium-blink-merge.git] / chrome / browser / chromeos / ui_proxy_config_service.cc
blob83a0e5a84c4afefbdbbf604260f24e63fffa890e
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.
5 #include "chrome/browser/chromeos/ui_proxy_config_service.h"
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 "components/device_event_log/device_event_log.h"
15 #include "net/proxy/proxy_config.h"
17 namespace chromeos {
19 namespace {
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";
34 NOTREACHED() << "Unrecognized mode type";
35 return "";
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);
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;
61 } // namespace
63 UIProxyConfigService::UIProxyConfigService()
64 : profile_prefs_(nullptr), local_state_prefs_(nullptr) {
67 UIProxyConfigService::~UIProxyConfigService() {
70 void UIProxyConfigService::SetPrefs(PrefService* profile_prefs,
71 PrefService* local_state_prefs) {
72 profile_prefs_ = profile_prefs;
73 local_state_prefs_ = local_state_prefs;
76 void UIProxyConfigService::SetCurrentNetworkGuid(
77 const std::string& current_guid) {
78 current_ui_network_guid_ = current_guid;
81 void UIProxyConfigService::UpdateFromPrefs() {
82 const NetworkState* network = nullptr;
83 if (!current_ui_network_guid_.empty()) {
84 network =
85 NetworkHandler::Get()->network_state_handler()->GetNetworkStateFromGuid(
86 current_ui_network_guid_);
88 if (!network || !network->IsInProfile()) {
89 NET_LOG(ERROR) << "No configured NetworkState for guid: "
90 << current_ui_network_guid_;
91 current_ui_network_guid_.clear();
92 current_ui_config_ = UIProxyConfig();
93 return;
96 DetermineEffectiveConfig(*network);
97 VLOG(1) << "Current ui network: " << network->name() << ", "
98 << ModeToString(current_ui_config_.mode) << ", "
99 << ProxyPrefs::ConfigStateToDebugString(current_ui_config_.state)
100 << ", modifiable:" << current_ui_config_.user_modifiable;
103 void UIProxyConfigService::GetProxyConfig(UIProxyConfig* config) const {
104 *config = current_ui_config_;
107 void UIProxyConfigService::SetProxyConfig(const UIProxyConfig& config) {
108 current_ui_config_ = config;
109 if (current_ui_network_guid_.empty())
110 return;
112 const NetworkState* network =
113 NetworkHandler::Get()->network_state_handler()->GetNetworkStateFromGuid(
114 current_ui_network_guid_);
115 if (!network || !network->IsInProfile()) {
116 NET_LOG(ERROR) << "No configured NetworkState for guid: "
117 << current_ui_network_guid_;
118 return;
121 // Store config for this network.
122 scoped_ptr<base::DictionaryValue> proxy_config_value(
123 config.ToPrefProxyConfig());
124 ProxyConfigDictionary proxy_config_dict(proxy_config_value.get());
126 VLOG(1) << "Set proxy for " << current_ui_network_guid_ << " to "
127 << *proxy_config_value;
129 proxy_config::SetProxyConfigForNetwork(proxy_config_dict, *network);
130 current_ui_config_.state = ProxyPrefs::CONFIG_SYSTEM;
133 void UIProxyConfigService::DetermineEffectiveConfig(
134 const NetworkState& network) {
135 DCHECK(local_state_prefs_);
137 // The pref service to read proxy settings that apply to all networks.
138 // Settings from the profile overrule local state.
139 PrefService* top_pref_service =
140 profile_prefs_ ? profile_prefs_ : local_state_prefs_;
142 // Get prefs proxy config if available.
143 net::ProxyConfig pref_config;
144 ProxyPrefs::ConfigState pref_state = ProxyConfigServiceImpl::ReadPrefConfig(
145 top_pref_service, &pref_config);
147 // Get network proxy config if available.
148 net::ProxyConfig network_config;
149 net::ProxyConfigService::ConfigAvailability network_availability =
150 net::ProxyConfigService::CONFIG_UNSET;
151 onc::ONCSource onc_source = onc::ONC_SOURCE_NONE;
152 if (chromeos::GetProxyConfig(profile_prefs_,
153 local_state_prefs_,
154 network,
155 &network_config,
156 &onc_source)) {
157 // Network is private or shared with user using shared proxies.
158 VLOG(1) << this << ": using proxy of network: " << network.path();
159 network_availability = net::ProxyConfigService::CONFIG_VALID;
162 // Determine effective proxy config, either from prefs or network.
163 ProxyPrefs::ConfigState effective_config_state;
164 net::ProxyConfig effective_config;
165 ProxyConfigServiceImpl::GetEffectiveProxyConfig(
166 pref_state, pref_config,
167 network_availability, network_config, false,
168 &effective_config_state, &effective_config);
170 // Store effective proxy into |current_ui_config_|.
171 current_ui_config_.FromNetProxyConfig(effective_config);
172 current_ui_config_.state = effective_config_state;
173 if (ProxyConfigServiceImpl::PrefPrecedes(effective_config_state)) {
174 current_ui_config_.user_modifiable = false;
175 } else if (!IsNetworkProxySettingsEditable(onc_source)) {
176 current_ui_config_.state = ProxyPrefs::CONFIG_POLICY;
177 current_ui_config_.user_modifiable = false;
178 } else {
179 current_ui_config_.user_modifiable = !ProxyConfigServiceImpl::IgnoreProxy(
180 profile_prefs_, network.profile_path(), onc_source);
184 } // namespace chromeos