BookmarkManager: Fix 'new folder text field size changes on clicking it' issue.
[chromium-blink-merge.git] / chrome / browser / chromeos / ui_proxy_config_service.cc
blob7ce57d113fb270c1fe96addaccb530fb6e8ebe06
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 "components/proxy_config/pref_proxy_config_tracker_impl.h"
16 #include "net/proxy/proxy_config.h"
18 namespace chromeos {
20 namespace {
22 const char* ModeToString(UIProxyConfig::Mode mode) {
23 switch (mode) {
24 case UIProxyConfig::MODE_DIRECT:
25 return "direct";
26 case UIProxyConfig::MODE_AUTO_DETECT:
27 return "auto-detect";
28 case UIProxyConfig::MODE_PAC_SCRIPT:
29 return "pacurl";
30 case UIProxyConfig::MODE_SINGLE_PROXY:
31 return "single-proxy";
32 case UIProxyConfig::MODE_PROXY_PER_SCHEME:
33 return "proxy-per-scheme";
35 NOTREACHED() << "Unrecognized mode type";
36 return "";
39 // Writes the proxy config of |network| to |proxy_config|. Sets |onc_source| to
40 // the source of this configuration. Returns false if no proxy was configured
41 // for this network.
42 bool GetProxyConfig(const PrefService* profile_prefs,
43 const PrefService* local_state_prefs,
44 const NetworkState& network,
45 net::ProxyConfig* proxy_config,
46 onc::ONCSource* onc_source) {
47 scoped_ptr<ProxyConfigDictionary> proxy_dict =
48 proxy_config::GetProxyConfigForNetwork(
49 profile_prefs, local_state_prefs, network, onc_source);
50 if (!proxy_dict)
51 return false;
52 return PrefProxyConfigTrackerImpl::PrefConfigToNetConfig(*proxy_dict,
53 proxy_config);
56 // Returns true if proxy settings from |onc_source| are editable.
57 bool IsNetworkProxySettingsEditable(const onc::ONCSource onc_source) {
58 return onc_source != onc::ONC_SOURCE_DEVICE_POLICY &&
59 onc_source != onc::ONC_SOURCE_USER_POLICY;
62 } // namespace
64 UIProxyConfigService::UIProxyConfigService()
65 : profile_prefs_(nullptr), local_state_prefs_(nullptr) {
68 UIProxyConfigService::~UIProxyConfigService() {
71 void UIProxyConfigService::SetPrefs(PrefService* profile_prefs,
72 PrefService* local_state_prefs) {
73 profile_prefs_ = profile_prefs;
74 local_state_prefs_ = local_state_prefs;
77 void UIProxyConfigService::SetCurrentNetworkGuid(
78 const std::string& current_guid) {
79 current_ui_network_guid_ = current_guid;
82 void UIProxyConfigService::UpdateFromPrefs() {
83 const NetworkState* network = nullptr;
84 if (!current_ui_network_guid_.empty()) {
85 network =
86 NetworkHandler::Get()->network_state_handler()->GetNetworkStateFromGuid(
87 current_ui_network_guid_);
89 if (!network || !network->IsInProfile()) {
90 NET_LOG(ERROR) << "No configured NetworkState for guid: "
91 << current_ui_network_guid_;
92 current_ui_network_guid_.clear();
93 current_ui_config_ = UIProxyConfig();
94 return;
97 DetermineEffectiveConfig(*network);
98 VLOG(1) << "Current ui network: " << network->name() << ", "
99 << ModeToString(current_ui_config_.mode) << ", "
100 << ProxyPrefs::ConfigStateToDebugString(current_ui_config_.state)
101 << ", modifiable:" << current_ui_config_.user_modifiable;
104 void UIProxyConfigService::GetProxyConfig(UIProxyConfig* config) const {
105 *config = current_ui_config_;
108 void UIProxyConfigService::SetProxyConfig(const UIProxyConfig& config) {
109 current_ui_config_ = config;
110 if (current_ui_network_guid_.empty())
111 return;
113 const NetworkState* network =
114 NetworkHandler::Get()->network_state_handler()->GetNetworkStateFromGuid(
115 current_ui_network_guid_);
116 if (!network || !network->IsInProfile()) {
117 NET_LOG(ERROR) << "No configured NetworkState for guid: "
118 << current_ui_network_guid_;
119 return;
122 // Store config for this network.
123 scoped_ptr<base::DictionaryValue> proxy_config_value(
124 config.ToPrefProxyConfig());
125 ProxyConfigDictionary proxy_config_dict(proxy_config_value.get());
127 VLOG(1) << "Set proxy for " << current_ui_network_guid_ << " to "
128 << *proxy_config_value;
130 proxy_config::SetProxyConfigForNetwork(proxy_config_dict, *network);
131 current_ui_config_.state = ProxyPrefs::CONFIG_SYSTEM;
134 void UIProxyConfigService::DetermineEffectiveConfig(
135 const NetworkState& network) {
136 DCHECK(local_state_prefs_);
138 // The pref service to read proxy settings that apply to all networks.
139 // Settings from the profile overrule local state.
140 PrefService* top_pref_service =
141 profile_prefs_ ? profile_prefs_ : local_state_prefs_;
143 // Get prefs proxy config if available.
144 net::ProxyConfig pref_config;
145 ProxyPrefs::ConfigState pref_state = ProxyConfigServiceImpl::ReadPrefConfig(
146 top_pref_service, &pref_config);
148 // Get network proxy config if available.
149 net::ProxyConfig network_config;
150 net::ProxyConfigService::ConfigAvailability network_availability =
151 net::ProxyConfigService::CONFIG_UNSET;
152 onc::ONCSource onc_source = onc::ONC_SOURCE_NONE;
153 if (chromeos::GetProxyConfig(profile_prefs_,
154 local_state_prefs_,
155 network,
156 &network_config,
157 &onc_source)) {
158 // Network is private or shared with user using shared proxies.
159 VLOG(1) << this << ": using proxy of network: " << network.path();
160 network_availability = net::ProxyConfigService::CONFIG_VALID;
163 // Determine effective proxy config, either from prefs or network.
164 ProxyPrefs::ConfigState effective_config_state;
165 net::ProxyConfig effective_config;
166 ProxyConfigServiceImpl::GetEffectiveProxyConfig(
167 pref_state, pref_config,
168 network_availability, network_config, false,
169 &effective_config_state, &effective_config);
171 // Store effective proxy into |current_ui_config_|.
172 current_ui_config_.FromNetProxyConfig(effective_config);
173 current_ui_config_.state = effective_config_state;
174 if (ProxyConfigServiceImpl::PrefPrecedes(effective_config_state)) {
175 current_ui_config_.user_modifiable = false;
176 } else if (!IsNetworkProxySettingsEditable(onc_source)) {
177 current_ui_config_.state = ProxyPrefs::CONFIG_POLICY;
178 current_ui_config_.user_modifiable = false;
179 } else {
180 current_ui_config_.user_modifiable = !ProxyConfigServiceImpl::IgnoreProxy(
181 profile_prefs_, network.profile_path(), onc_source);
185 } // namespace chromeos