Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(110)

Side by Side Diff: chromeos/network/proxy/ui_proxy_config_service.cc

Issue 2446893008: NetworkHandler: Add ui_proxy_config_service (Closed)
Patch Set: . Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chromeos/network/proxy/ui_proxy_config_service.h" 5 #include "chromeos/network/proxy/ui_proxy_config_service.h"
6 6
7 #include <memory> 7 #include <memory>
8 8
9 #include "base/bind.h"
10 #include "base/bind_helpers.h"
9 #include "base/logging.h" 11 #include "base/logging.h"
10 #include "base/values.h" 12 #include "base/values.h"
11 #include "chromeos/network/network_state.h" 13 #include "chromeos/network/network_state.h"
12 #include "chromeos/network/network_state_handler.h" 14 #include "chromeos/network/network_state_handler.h"
13 #include "chromeos/network/proxy/proxy_config_handler.h" 15 #include "chromeos/network/proxy/proxy_config_handler.h"
14 #include "chromeos/network/proxy/proxy_config_service_impl.h" 16 #include "chromeos/network/proxy/proxy_config_service_impl.h"
15 #include "components/device_event_log/device_event_log.h" 17 #include "components/device_event_log/device_event_log.h"
16 #include "components/proxy_config/pref_proxy_config_tracker_impl.h" 18 #include "components/proxy_config/pref_proxy_config_tracker_impl.h"
19 #include "components/proxy_config/proxy_config_pref_names.h"
17 #include "net/proxy/proxy_config.h" 20 #include "net/proxy/proxy_config.h"
18 21
19 namespace chromeos { 22 namespace chromeos {
20 23
21 namespace { 24 namespace {
22 25
23 const char* ModeToString(UIProxyConfig::Mode mode) { 26 const char* ModeToString(UIProxyConfig::Mode mode) {
24 switch (mode) { 27 switch (mode) {
25 case UIProxyConfig::MODE_DIRECT: 28 case UIProxyConfig::MODE_DIRECT:
26 return "direct"; 29 return "direct";
27 case UIProxyConfig::MODE_AUTO_DETECT: 30 case UIProxyConfig::MODE_AUTO_DETECT:
28 return "auto-detect"; 31 return "auto-detect";
29 case UIProxyConfig::MODE_PAC_SCRIPT: 32 case UIProxyConfig::MODE_PAC_SCRIPT:
30 return "pacurl"; 33 return "pacurl";
31 case UIProxyConfig::MODE_SINGLE_PROXY: 34 case UIProxyConfig::MODE_SINGLE_PROXY:
32 return "single-proxy"; 35 return "single-proxy";
33 case UIProxyConfig::MODE_PROXY_PER_SCHEME: 36 case UIProxyConfig::MODE_PROXY_PER_SCHEME:
34 return "proxy-per-scheme"; 37 return "proxy-per-scheme";
35 } 38 }
36 NOTREACHED() << "Unrecognized mode type"; 39 NOTREACHED() << "Unrecognized mode type";
37 return ""; 40 return "";
38 } 41 }
39 42
40 // Writes the proxy config of |network| to |proxy_config|. Sets |onc_source| to 43 // Writes the proxy config of |network| to |proxy_config|. Sets |onc_source| to
41 // the source of this configuration. Returns false if no proxy was configured 44 // the source of this configuration. Returns false if no proxy was configured
42 // for this network. 45 // for this network.
43 bool GetProxyConfig(const PrefService* profile_prefs, 46 bool GetProxyConfig(const PrefService* logged_in_profile_prefs,
44 const PrefService* local_state_prefs, 47 const PrefService* local_state_prefs,
45 const NetworkState& network, 48 const NetworkState& network,
46 net::ProxyConfig* proxy_config, 49 net::ProxyConfig* proxy_config,
47 onc::ONCSource* onc_source) { 50 onc::ONCSource* onc_source) {
48 std::unique_ptr<ProxyConfigDictionary> proxy_dict = 51 std::unique_ptr<ProxyConfigDictionary> proxy_dict =
49 proxy_config::GetProxyConfigForNetwork(profile_prefs, local_state_prefs, 52 proxy_config::GetProxyConfigForNetwork(
50 network, onc_source); 53 logged_in_profile_prefs, local_state_prefs, network, onc_source);
51 if (!proxy_dict) 54 if (!proxy_dict)
52 return false; 55 return false;
53 return PrefProxyConfigTrackerImpl::PrefConfigToNetConfig(*proxy_dict, 56 return PrefProxyConfigTrackerImpl::PrefConfigToNetConfig(*proxy_dict,
54 proxy_config); 57 proxy_config);
55 } 58 }
56 59
57 // Returns true if proxy settings from |onc_source| are editable. 60 // Returns true if proxy settings from |onc_source| are editable.
58 bool IsNetworkProxySettingsEditable(const onc::ONCSource onc_source) { 61 bool IsNetworkProxySettingsEditable(const onc::ONCSource onc_source) {
59 return onc_source != onc::ONC_SOURCE_DEVICE_POLICY && 62 return onc_source != onc::ONC_SOURCE_DEVICE_POLICY &&
60 onc_source != onc::ONC_SOURCE_USER_POLICY; 63 onc_source != onc::ONC_SOURCE_USER_POLICY;
61 } 64 }
62 65
63 } // namespace 66 } // namespace
64 67
65 UIProxyConfigService::UIProxyConfigService() 68 UIProxyConfigService::UIProxyConfigService(PrefService* logged_in_profile_prefs,
66 : profile_prefs_(nullptr), local_state_prefs_(nullptr) {} 69 PrefService* local_state_prefs)
70 : logged_in_profile_prefs_(logged_in_profile_prefs),
71 local_state_prefs_(local_state_prefs) {
James Cook 2016/11/01 23:02:04 nit: DCHECK(local_state_prefs) if it cannot be nul
stevenjb 2016/11/01 23:55:11 Done.
72 local_state_registrar_.Init(local_state_prefs);
73 local_state_registrar_.Add(
74 ::proxy_config::prefs::kProxy,
75 base::Bind(&UIProxyConfigService::OnPreferenceChanged,
76 base::Unretained(this)));
77
78 if (logged_in_profile_prefs) {
79 logged_in_profile_registrar_.Init(logged_in_profile_prefs);
80 logged_in_profile_registrar_.Add(
81 ::proxy_config::prefs::kProxy,
82 base::Bind(&UIProxyConfigService::OnPreferenceChanged,
83 base::Unretained(this)));
84 logged_in_profile_registrar_.Add(
85 ::proxy_config::prefs::kUseSharedProxies,
86 base::Bind(&UIProxyConfigService::OnPreferenceChanged,
87 base::Unretained(this)));
88 }
89 }
67 90
68 UIProxyConfigService::~UIProxyConfigService() {} 91 UIProxyConfigService::~UIProxyConfigService() {}
69 92
70 void UIProxyConfigService::SetPrefs(PrefService* profile_prefs, 93 void UIProxyConfigService::UpdateFromPrefs(const std::string& network_guid) {
71 PrefService* local_state_prefs) { 94 current_ui_network_guid_ = network_guid;
72 profile_prefs_ = profile_prefs;
73 local_state_prefs_ = local_state_prefs;
74 }
75
76 void UIProxyConfigService::SetCurrentNetworkGuid(
77 const std::string& current_guid) {
78 current_ui_network_guid_ = current_guid;
79 }
80
81 void UIProxyConfigService::UpdateFromPrefs() {
82 const NetworkState* network = nullptr; 95 const NetworkState* network = nullptr;
83 if (!current_ui_network_guid_.empty()) { 96 if (!network_guid.empty()) {
84 network = 97 network =
85 NetworkHandler::Get()->network_state_handler()->GetNetworkStateFromGuid( 98 NetworkHandler::Get()->network_state_handler()->GetNetworkStateFromGuid(
86 current_ui_network_guid_); 99 network_guid);
100 if (!network) {
101 NET_LOG(ERROR) << "No NetworkState for guid: " << network_guid;
102 } else if (!network->IsInProfile()) {
103 NET_LOG(ERROR) << "Network not in profile: " << network_guid;
104 network = nullptr;
105 }
87 } 106 }
88 if (!network || !network->IsInProfile()) { 107 if (!network) {
89 NET_LOG(ERROR) << "No configured NetworkState for guid: "
90 << current_ui_network_guid_;
91 current_ui_network_guid_.clear(); 108 current_ui_network_guid_.clear();
92 current_ui_config_ = UIProxyConfig(); 109 current_ui_config_ = UIProxyConfig();
93 return; 110 return;
94 } 111 }
95 112
96 DetermineEffectiveConfig(*network); 113 DetermineEffectiveConfig(*network);
97 VLOG(1) << "Current ui network: " << network->name() << ", " 114 VLOG(1) << "Current ui network: " << network->name() << ", "
98 << ModeToString(current_ui_config_.mode) << ", " 115 << ModeToString(current_ui_config_.mode) << ", "
99 << ProxyPrefs::ConfigStateToDebugString(current_ui_config_.state) 116 << ProxyPrefs::ConfigStateToDebugString(current_ui_config_.state)
100 << ", modifiable:" << current_ui_config_.user_modifiable; 117 << ", modifiable:" << current_ui_config_.user_modifiable;
101 } 118 }
102 119
103 void UIProxyConfigService::GetProxyConfig(UIProxyConfig* config) const { 120 void UIProxyConfigService::GetProxyConfig(const std::string& network_guid,
121 UIProxyConfig* config) {
122 if (network_guid != current_ui_network_guid_)
123 UpdateFromPrefs(network_guid);
104 *config = current_ui_config_; 124 *config = current_ui_config_;
105 } 125 }
106 126
107 void UIProxyConfigService::SetProxyConfig(const UIProxyConfig& config) { 127 void UIProxyConfigService::SetProxyConfig(const std::string& network_guid,
128 const UIProxyConfig& config) {
129 current_ui_network_guid_ = network_guid;
108 current_ui_config_ = config; 130 current_ui_config_ = config;
109 if (current_ui_network_guid_.empty()) 131 if (current_ui_network_guid_.empty())
110 return; 132 return;
111 133
112 const NetworkState* network = 134 const NetworkState* network =
113 NetworkHandler::Get()->network_state_handler()->GetNetworkStateFromGuid( 135 NetworkHandler::Get()->network_state_handler()->GetNetworkStateFromGuid(
114 current_ui_network_guid_); 136 current_ui_network_guid_);
115 if (!network || !network->IsInProfile()) { 137 if (!network || !network->IsInProfile()) {
116 NET_LOG(ERROR) << "No configured NetworkState for guid: " 138 NET_LOG(ERROR) << "No configured NetworkState for guid: "
117 << current_ui_network_guid_; 139 << current_ui_network_guid_;
(...skipping 12 matching lines...) Expand all
130 current_ui_config_.state = ProxyPrefs::CONFIG_SYSTEM; 152 current_ui_config_.state = ProxyPrefs::CONFIG_SYSTEM;
131 } 153 }
132 154
133 void UIProxyConfigService::DetermineEffectiveConfig( 155 void UIProxyConfigService::DetermineEffectiveConfig(
134 const NetworkState& network) { 156 const NetworkState& network) {
135 DCHECK(local_state_prefs_); 157 DCHECK(local_state_prefs_);
136 158
137 // The pref service to read proxy settings that apply to all networks. 159 // The pref service to read proxy settings that apply to all networks.
138 // Settings from the profile overrule local state. 160 // Settings from the profile overrule local state.
139 PrefService* top_pref_service = 161 PrefService* top_pref_service =
140 profile_prefs_ ? profile_prefs_ : local_state_prefs_; 162 logged_in_profile_prefs_ ? logged_in_profile_prefs_ : local_state_prefs_;
141 163
142 // Get prefs proxy config if available. 164 // Get prefs proxy config if available.
143 net::ProxyConfig pref_config; 165 net::ProxyConfig pref_config;
144 ProxyPrefs::ConfigState pref_state = 166 ProxyPrefs::ConfigState pref_state =
145 ProxyConfigServiceImpl::ReadPrefConfig(top_pref_service, &pref_config); 167 ProxyConfigServiceImpl::ReadPrefConfig(top_pref_service, &pref_config);
146 168
147 // Get network proxy config if available. 169 // Get network proxy config if available.
148 net::ProxyConfig network_config; 170 net::ProxyConfig network_config;
149 net::ProxyConfigService::ConfigAvailability network_availability = 171 net::ProxyConfigService::ConfigAvailability network_availability =
150 net::ProxyConfigService::CONFIG_UNSET; 172 net::ProxyConfigService::CONFIG_UNSET;
151 onc::ONCSource onc_source = onc::ONC_SOURCE_NONE; 173 onc::ONCSource onc_source = onc::ONC_SOURCE_NONE;
152 if (chromeos::GetProxyConfig(profile_prefs_, local_state_prefs_, network, 174 if (chromeos::GetProxyConfig(logged_in_profile_prefs_, local_state_prefs_,
153 &network_config, &onc_source)) { 175 network, &network_config, &onc_source)) {
154 // Network is private or shared with user using shared proxies. 176 // Network is private or shared with user using shared proxies.
155 VLOG(1) << this << ": using proxy of network: " << network.path(); 177 VLOG(1) << this << ": using proxy of network: " << network.path();
156 network_availability = net::ProxyConfigService::CONFIG_VALID; 178 network_availability = net::ProxyConfigService::CONFIG_VALID;
157 } 179 }
158 180
159 // Determine effective proxy config, either from prefs or network. 181 // Determine effective proxy config, either from prefs or network.
160 ProxyPrefs::ConfigState effective_config_state; 182 ProxyPrefs::ConfigState effective_config_state;
161 net::ProxyConfig effective_config; 183 net::ProxyConfig effective_config;
162 ProxyConfigServiceImpl::GetEffectiveProxyConfig( 184 ProxyConfigServiceImpl::GetEffectiveProxyConfig(
163 pref_state, pref_config, network_availability, network_config, false, 185 pref_state, pref_config, network_availability, network_config, false,
164 &effective_config_state, &effective_config); 186 &effective_config_state, &effective_config);
165 187
166 // Store effective proxy into |current_ui_config_|. 188 // Store effective proxy into |current_ui_config_|.
167 current_ui_config_.FromNetProxyConfig(effective_config); 189 current_ui_config_.FromNetProxyConfig(effective_config);
168 current_ui_config_.state = effective_config_state; 190 current_ui_config_.state = effective_config_state;
169 if (ProxyConfigServiceImpl::PrefPrecedes(effective_config_state)) { 191 if (ProxyConfigServiceImpl::PrefPrecedes(effective_config_state)) {
170 current_ui_config_.user_modifiable = false; 192 current_ui_config_.user_modifiable = false;
171 } else if (!IsNetworkProxySettingsEditable(onc_source)) { 193 } else if (!IsNetworkProxySettingsEditable(onc_source)) {
172 current_ui_config_.state = ProxyPrefs::CONFIG_POLICY; 194 current_ui_config_.state = ProxyPrefs::CONFIG_POLICY;
173 current_ui_config_.user_modifiable = false; 195 current_ui_config_.user_modifiable = false;
174 } else { 196 } else {
175 current_ui_config_.user_modifiable = !ProxyConfigServiceImpl::IgnoreProxy( 197 current_ui_config_.user_modifiable = !ProxyConfigServiceImpl::IgnoreProxy(
176 profile_prefs_, network.profile_path(), onc_source); 198 logged_in_profile_prefs_, network.profile_path(), onc_source);
177 } 199 }
178 } 200 }
179 201
202 void UIProxyConfigService::OnPreferenceChanged(const std::string& pref_name) {
203 if (!current_ui_network_guid_.empty())
204 UpdateFromPrefs(current_ui_network_guid_);
205 }
206
180 } // namespace chromeos 207 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698