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

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

Issue 2446893008: NetworkHandler: Add ui_proxy_config_service (Closed)
Patch Set: Feedback + elim bogus shill errors 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* local_state_prefs,
66 : profile_prefs_(nullptr), local_state_prefs_(nullptr) {} 69 PrefService* logged_in_profile_prefs)
michaelpg 2016/11/02 05:34:51 nit: Can you order the pref service args to match
stevenjb 2016/11/02 18:03:41 Argh. I tried to change some code to local, user s
70 : logged_in_profile_prefs_(logged_in_profile_prefs),
michaelpg 2016/11/02 05:34:51 order according to args
stevenjb 2016/11/02 18:03:41 Done.
71 local_state_prefs_(local_state_prefs) {
72 DCHECK(local_state_prefs);
73 local_state_registrar_.Init(local_state_prefs);
74 local_state_registrar_.Add(
75 ::proxy_config::prefs::kProxy,
76 base::Bind(&UIProxyConfigService::OnPreferenceChanged,
77 base::Unretained(this)));
78
79 if (logged_in_profile_prefs) {
80 logged_in_profile_registrar_.Init(logged_in_profile_prefs);
81 logged_in_profile_registrar_.Add(
82 ::proxy_config::prefs::kProxy,
83 base::Bind(&UIProxyConfigService::OnPreferenceChanged,
84 base::Unretained(this)));
85 logged_in_profile_registrar_.Add(
86 ::proxy_config::prefs::kUseSharedProxies,
87 base::Bind(&UIProxyConfigService::OnPreferenceChanged,
88 base::Unretained(this)));
89 }
90 }
67 91
68 UIProxyConfigService::~UIProxyConfigService() {} 92 UIProxyConfigService::~UIProxyConfigService() {}
69 93
70 void UIProxyConfigService::SetPrefs(PrefService* profile_prefs, 94 void UIProxyConfigService::UpdateFromPrefs(const std::string& network_guid) {
71 PrefService* local_state_prefs) { 95 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; 96 const NetworkState* network = nullptr;
83 if (!current_ui_network_guid_.empty()) { 97 if (!network_guid.empty()) {
84 network = 98 network =
85 NetworkHandler::Get()->network_state_handler()->GetNetworkStateFromGuid( 99 NetworkHandler::Get()->network_state_handler()->GetNetworkStateFromGuid(
86 current_ui_network_guid_); 100 network_guid);
101 if (!network) {
102 NET_LOG(ERROR) << "No NetworkState for guid: " << network_guid;
103 } else if (!network->IsInProfile()) {
104 NET_LOG(ERROR) << "Network not in profile: " << network_guid;
105 network = nullptr;
106 }
87 } 107 }
88 if (!network || !network->IsInProfile()) { 108 if (!network) {
89 NET_LOG(ERROR) << "No configured NetworkState for guid: "
90 << current_ui_network_guid_;
91 current_ui_network_guid_.clear(); 109 current_ui_network_guid_.clear();
92 current_ui_config_ = UIProxyConfig(); 110 current_ui_config_ = UIProxyConfig();
93 return; 111 return;
94 } 112 }
95 113
96 DetermineEffectiveConfig(*network); 114 DetermineEffectiveConfig(*network);
97 VLOG(1) << "Current ui network: " << network->name() << ", " 115 VLOG(1) << "Current ui network: " << network->name() << ", "
98 << ModeToString(current_ui_config_.mode) << ", " 116 << ModeToString(current_ui_config_.mode) << ", "
99 << ProxyPrefs::ConfigStateToDebugString(current_ui_config_.state) 117 << ProxyPrefs::ConfigStateToDebugString(current_ui_config_.state)
100 << ", modifiable:" << current_ui_config_.user_modifiable; 118 << ", modifiable:" << current_ui_config_.user_modifiable;
101 } 119 }
102 120
103 void UIProxyConfigService::GetProxyConfig(UIProxyConfig* config) const { 121 void UIProxyConfigService::GetProxyConfig(const std::string& network_guid,
122 UIProxyConfig* config) {
123 if (network_guid != current_ui_network_guid_)
124 UpdateFromPrefs(network_guid);
104 *config = current_ui_config_; 125 *config = current_ui_config_;
michaelpg 2016/11/02 05:34:51 Is this a copy? So current_ui_config_ is just used
stevenjb 2016/11/02 18:03:41 Correct. The old options code will call into this
105 } 126 }
106 127
107 void UIProxyConfigService::SetProxyConfig(const UIProxyConfig& config) { 128 void UIProxyConfigService::SetProxyConfig(const std::string& network_guid,
129 const UIProxyConfig& config) {
130 current_ui_network_guid_ = network_guid;
108 current_ui_config_ = config; 131 current_ui_config_ = config;
109 if (current_ui_network_guid_.empty()) 132 if (current_ui_network_guid_.empty())
110 return; 133 return;
111 134
112 const NetworkState* network = 135 const NetworkState* network =
113 NetworkHandler::Get()->network_state_handler()->GetNetworkStateFromGuid( 136 NetworkHandler::Get()->network_state_handler()->GetNetworkStateFromGuid(
114 current_ui_network_guid_); 137 current_ui_network_guid_);
115 if (!network || !network->IsInProfile()) { 138 if (!network || !network->IsInProfile()) {
116 NET_LOG(ERROR) << "No configured NetworkState for guid: " 139 NET_LOG(ERROR) << "No configured NetworkState for guid: "
117 << current_ui_network_guid_; 140 << current_ui_network_guid_;
(...skipping 12 matching lines...) Expand all
130 current_ui_config_.state = ProxyPrefs::CONFIG_SYSTEM; 153 current_ui_config_.state = ProxyPrefs::CONFIG_SYSTEM;
131 } 154 }
132 155
133 void UIProxyConfigService::DetermineEffectiveConfig( 156 void UIProxyConfigService::DetermineEffectiveConfig(
134 const NetworkState& network) { 157 const NetworkState& network) {
135 DCHECK(local_state_prefs_); 158 DCHECK(local_state_prefs_);
136 159
137 // The pref service to read proxy settings that apply to all networks. 160 // The pref service to read proxy settings that apply to all networks.
138 // Settings from the profile overrule local state. 161 // Settings from the profile overrule local state.
139 PrefService* top_pref_service = 162 PrefService* top_pref_service =
140 profile_prefs_ ? profile_prefs_ : local_state_prefs_; 163 logged_in_profile_prefs_ ? logged_in_profile_prefs_ : local_state_prefs_;
141 164
142 // Get prefs proxy config if available. 165 // Get prefs proxy config if available.
143 net::ProxyConfig pref_config; 166 net::ProxyConfig pref_config;
144 ProxyPrefs::ConfigState pref_state = 167 ProxyPrefs::ConfigState pref_state =
145 ProxyConfigServiceImpl::ReadPrefConfig(top_pref_service, &pref_config); 168 ProxyConfigServiceImpl::ReadPrefConfig(top_pref_service, &pref_config);
146 169
147 // Get network proxy config if available. 170 // Get network proxy config if available.
148 net::ProxyConfig network_config; 171 net::ProxyConfig network_config;
149 net::ProxyConfigService::ConfigAvailability network_availability = 172 net::ProxyConfigService::ConfigAvailability network_availability =
150 net::ProxyConfigService::CONFIG_UNSET; 173 net::ProxyConfigService::CONFIG_UNSET;
151 onc::ONCSource onc_source = onc::ONC_SOURCE_NONE; 174 onc::ONCSource onc_source = onc::ONC_SOURCE_NONE;
152 if (chromeos::GetProxyConfig(profile_prefs_, local_state_prefs_, network, 175 if (chromeos::GetProxyConfig(logged_in_profile_prefs_, local_state_prefs_,
153 &network_config, &onc_source)) { 176 network, &network_config, &onc_source)) {
154 // Network is private or shared with user using shared proxies. 177 // Network is private or shared with user using shared proxies.
155 VLOG(1) << this << ": using proxy of network: " << network.path(); 178 VLOG(1) << this << ": using proxy of network: " << network.path();
156 network_availability = net::ProxyConfigService::CONFIG_VALID; 179 network_availability = net::ProxyConfigService::CONFIG_VALID;
157 } 180 }
158 181
159 // Determine effective proxy config, either from prefs or network. 182 // Determine effective proxy config, either from prefs or network.
160 ProxyPrefs::ConfigState effective_config_state; 183 ProxyPrefs::ConfigState effective_config_state;
161 net::ProxyConfig effective_config; 184 net::ProxyConfig effective_config;
162 ProxyConfigServiceImpl::GetEffectiveProxyConfig( 185 ProxyConfigServiceImpl::GetEffectiveProxyConfig(
163 pref_state, pref_config, network_availability, network_config, false, 186 pref_state, pref_config, network_availability, network_config, false,
164 &effective_config_state, &effective_config); 187 &effective_config_state, &effective_config);
165 188
166 // Store effective proxy into |current_ui_config_|. 189 // Store effective proxy into |current_ui_config_|.
167 current_ui_config_.FromNetProxyConfig(effective_config); 190 current_ui_config_.FromNetProxyConfig(effective_config);
168 current_ui_config_.state = effective_config_state; 191 current_ui_config_.state = effective_config_state;
169 if (ProxyConfigServiceImpl::PrefPrecedes(effective_config_state)) { 192 if (ProxyConfigServiceImpl::PrefPrecedes(effective_config_state)) {
170 current_ui_config_.user_modifiable = false; 193 current_ui_config_.user_modifiable = false;
171 } else if (!IsNetworkProxySettingsEditable(onc_source)) { 194 } else if (!IsNetworkProxySettingsEditable(onc_source)) {
172 current_ui_config_.state = ProxyPrefs::CONFIG_POLICY; 195 current_ui_config_.state = ProxyPrefs::CONFIG_POLICY;
173 current_ui_config_.user_modifiable = false; 196 current_ui_config_.user_modifiable = false;
174 } else { 197 } else {
175 current_ui_config_.user_modifiable = !ProxyConfigServiceImpl::IgnoreProxy( 198 current_ui_config_.user_modifiable = !ProxyConfigServiceImpl::IgnoreProxy(
176 profile_prefs_, network.profile_path(), onc_source); 199 logged_in_profile_prefs_, network.profile_path(), onc_source);
177 } 200 }
178 } 201 }
179 202
203 void UIProxyConfigService::OnPreferenceChanged(const std::string& pref_name) {
204 if (!current_ui_network_guid_.empty())
205 UpdateFromPrefs(current_ui_network_guid_);
206 }
207
180 } // namespace chromeos 208 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698