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

Side by Side Diff: chrome/browser/chromeos/proxy_config_service_impl.cc

Issue 2442313003: Move some proxy config code out of src/chrome (Closed)
Patch Set: Fix DEPS 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
(Empty)
1 // Copyright (c) 2012 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.
4
5 #include "chrome/browser/chromeos/proxy_config_service_impl.h"
6
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/callback.h"
10 #include "base/location.h"
11 #include "base/logging.h"
12 #include "base/memory/ptr_util.h"
13 #include "base/values.h"
14 #include "chrome/browser/browser_process.h"
15 #include "chrome/browser/chromeos/net/proxy_config_handler.h"
16 #include "chrome/common/pref_names.h"
17 #include "chromeos/network/network_profile.h"
18 #include "chromeos/network/network_profile_handler.h"
19 #include "chromeos/network/network_state.h"
20 #include "chromeos/network/network_state_handler.h"
21 #include "chromeos/network/onc/onc_utils.h"
22 #include "components/onc/onc_pref_names.h"
23 #include "components/policy/core/common/cloud/cloud_policy_constants.h"
24 #include "components/prefs/pref_service.h"
25 #include "components/proxy_config/pref_proxy_config_tracker_impl.h"
26 #include "components/proxy_config/proxy_config_dictionary.h"
27 #include "components/proxy_config/proxy_config_pref_names.h"
28 #include "components/proxy_config/proxy_prefs.h"
29 #include "components/user_manager/user_manager.h"
30 #include "content/public/browser/browser_thread.h"
31
32 namespace chromeos {
33
34 namespace {
35
36 // Writes the proxy config of |network| to |proxy_config|. Set |onc_source| to
37 // the source of this configuration. Returns false if no
38 // proxy was configured for this network.
39 bool GetProxyConfig(const PrefService* profile_prefs,
40 const PrefService* local_state_prefs,
41 const NetworkState& network,
42 net::ProxyConfig* proxy_config,
43 ::onc::ONCSource* onc_source) {
44 std::unique_ptr<ProxyConfigDictionary> proxy_dict =
45 proxy_config::GetProxyConfigForNetwork(profile_prefs, local_state_prefs,
46 network, onc_source);
47 if (!proxy_dict)
48 return false;
49 return PrefProxyConfigTrackerImpl::PrefConfigToNetConfig(*proxy_dict,
50 proxy_config);
51 }
52
53 } // namespace
54
55 ProxyConfigServiceImpl::ProxyConfigServiceImpl(PrefService* profile_prefs,
56 PrefService* local_state_prefs)
57 : PrefProxyConfigTrackerImpl(
58 profile_prefs ? profile_prefs : local_state_prefs,
59 content::BrowserThread::GetTaskRunnerForThread(
60 content::BrowserThread::IO)),
61 active_config_state_(ProxyPrefs::CONFIG_UNSET),
62 profile_prefs_(profile_prefs),
63 local_state_prefs_(local_state_prefs),
64 pointer_factory_(this) {
65 const base::Closure proxy_change_callback = base::Bind(
66 &ProxyConfigServiceImpl::OnProxyPrefChanged, base::Unretained(this));
67
68 if (profile_prefs) {
69 profile_pref_registrar_.Init(profile_prefs);
70 profile_pref_registrar_.Add(::onc::prefs::kOpenNetworkConfiguration,
71 proxy_change_callback);
72 profile_pref_registrar_.Add(::proxy_config::prefs::kUseSharedProxies,
73 proxy_change_callback);
74 }
75 local_state_pref_registrar_.Init(local_state_prefs);
76 local_state_pref_registrar_.Add(::onc::prefs::kDeviceOpenNetworkConfiguration,
77 proxy_change_callback);
78
79 // Register for changes to the default network.
80 NetworkStateHandler* state_handler =
81 NetworkHandler::Get()->network_state_handler();
82 state_handler->AddObserver(this, FROM_HERE);
83 DefaultNetworkChanged(state_handler->DefaultNetwork());
84 }
85
86 ProxyConfigServiceImpl::~ProxyConfigServiceImpl() {
87 if (NetworkHandler::IsInitialized()) {
88 NetworkHandler::Get()->network_state_handler()->RemoveObserver(
89 this, FROM_HERE);
90 }
91 }
92
93 void ProxyConfigServiceImpl::OnProxyConfigChanged(
94 ProxyPrefs::ConfigState config_state,
95 const net::ProxyConfig& config) {
96 VLOG(1) << "Got prefs change: "
97 << ProxyPrefs::ConfigStateToDebugString(config_state)
98 << ", mode=" << config.proxy_rules().type;
99 DetermineEffectiveConfigFromDefaultNetwork();
100 }
101
102 void ProxyConfigServiceImpl::OnProxyPrefChanged() {
103 DetermineEffectiveConfigFromDefaultNetwork();
104 }
105
106 void ProxyConfigServiceImpl::DefaultNetworkChanged(
107 const NetworkState* new_network) {
108 std::string new_network_path;
109 if (new_network)
110 new_network_path = new_network->path();
111
112 VLOG(1) << "DefaultNetworkChanged to '" << new_network_path << "'.";
113 VLOG_IF(1, new_network) << "New network: name=" << new_network->name()
114 << ", profile=" << new_network->profile_path();
115
116 // Even if the default network is the same, its proxy config (e.g. if private
117 // version of network replaces the shared version after login), or
118 // use-shared-proxies setting (e.g. after login) may have changed, so
119 // re-determine effective proxy config, and activate if different.
120 DetermineEffectiveConfigFromDefaultNetwork();
121 }
122
123 void ProxyConfigServiceImpl::OnShuttingDown() {
124 // Ownership of this class is complicated. Stop observing NetworkStateHandler
125 // when the class shuts down.
126 NetworkHandler::Get()->network_state_handler()->RemoveObserver(this,
127 FROM_HERE);
128 }
129
130 // static
131 bool ProxyConfigServiceImpl::IgnoreProxy(const PrefService* profile_prefs,
132 const std::string network_profile_path,
133 ::onc::ONCSource onc_source) {
134 if (!profile_prefs) {
135 // If the profile preference are not available, this must be the object
136 // associated to local state used for system requests or login-profile. Make
137 // sure that proxies are enabled.
138 VLOG(1) << "Use proxy for system requests and sign-in screen.";
139 return false;
140 }
141
142 if (network_profile_path.empty())
143 return true;
144
145 const NetworkProfile* profile = NetworkHandler::Get()
146 ->network_profile_handler()->GetProfileForPath(network_profile_path);
147 if (!profile) {
148 VLOG(1) << "Unknown profile_path '" << network_profile_path
149 << "'. Ignoring proxy.";
150 return true;
151 }
152 if (profile->type() == NetworkProfile::TYPE_USER) {
153 VLOG(1) << "Respect proxy of not-shared networks.";
154 return false;
155 }
156 if (onc_source == ::onc::ONC_SOURCE_USER_POLICY) {
157 // Note that this case can occur if the network is shared (e.g. ethernet)
158 // but the proxy is determined by user policy.
159 // See https://crbug.com/454966 .
160 VLOG(1) << "Respect proxy from user policy although network is shared.";
161 return false;
162 }
163 if (onc_source == ::onc::ONC_SOURCE_DEVICE_POLICY) {
164 const user_manager::User* logged_in_user =
165 user_manager::UserManager::Get()->GetLoggedInUser();
166 if (logged_in_user->IsAffiliated()) {
167 VLOG(1) << "Respecting proxy for network, as logged-in user belongs to "
168 << "the domain the device is enrolled to.";
169 return false;
170 }
171 }
172
173 // This network is shared and not managed by the user's domain.
174 bool use_shared_proxies =
175 profile_prefs->GetBoolean(::proxy_config::prefs::kUseSharedProxies);
176 VLOG(1) << "Use proxy of shared network: " << use_shared_proxies;
177 return !use_shared_proxies;
178 }
179
180 // static
181 std::unique_ptr<ProxyConfigDictionary>
182 ProxyConfigServiceImpl::GetActiveProxyConfigDictionary(
183 const PrefService* profile_prefs,
184 const PrefService* local_state_prefs) {
185 // Apply Pref Proxy configuration if available.
186 net::ProxyConfig pref_proxy_config;
187 ProxyPrefs::ConfigState pref_state =
188 PrefProxyConfigTrackerImpl::ReadPrefConfig(profile_prefs,
189 &pref_proxy_config);
190 if (PrefProxyConfigTrackerImpl::PrefPrecedes(pref_state)) {
191 const PrefService::Preference* const pref =
192 profile_prefs->FindPreference(::proxy_config::prefs::kProxy);
193 const base::DictionaryValue* proxy_config_value;
194 bool value_exists = pref->GetValue()->GetAsDictionary(&proxy_config_value);
195 DCHECK(value_exists);
196
197 return base::MakeUnique<ProxyConfigDictionary>(proxy_config_value);
198 }
199
200 const chromeos::NetworkState* network = chromeos::NetworkHandler::Get()
201 ->network_state_handler()
202 ->DefaultNetwork();
203 // No connected network.
204 if (!network)
205 return nullptr;
206
207 // Apply network proxy configuration.
208 ::onc::ONCSource onc_source;
209 std::unique_ptr<ProxyConfigDictionary> proxy_config =
210 chromeos::proxy_config::GetProxyConfigForNetwork(
211 profile_prefs, local_state_prefs, *network, &onc_source);
212 if (!chromeos::ProxyConfigServiceImpl::IgnoreProxy(
213 profile_prefs, network->profile_path(), onc_source))
214 return proxy_config;
215
216 return base::MakeUnique<ProxyConfigDictionary>(
217 ProxyConfigDictionary::CreateDirect());
218 }
219
220 void ProxyConfigServiceImpl::DetermineEffectiveConfigFromDefaultNetwork() {
221 if (!NetworkHandler::IsInitialized())
222 return;
223
224 NetworkStateHandler* handler = NetworkHandler::Get()->network_state_handler();
225 const NetworkState* network = handler->DefaultNetwork();
226
227 // Get prefs proxy config if available.
228 net::ProxyConfig pref_config;
229 ProxyPrefs::ConfigState pref_state = GetProxyConfig(&pref_config);
230
231 // Get network proxy config if available.
232 net::ProxyConfig network_config;
233 net::ProxyConfigService::ConfigAvailability network_availability =
234 net::ProxyConfigService::CONFIG_UNSET;
235 bool ignore_proxy = true;
236 if (network) {
237 ::onc::ONCSource onc_source = ::onc::ONC_SOURCE_NONE;
238 const bool network_proxy_configured = chromeos::GetProxyConfig(
239 prefs(), local_state_prefs_, *network, &network_config, &onc_source);
240 ignore_proxy =
241 IgnoreProxy(profile_prefs_, network->profile_path(), onc_source);
242
243 // If network is shared but use-shared-proxies is off, use direct mode.
244 if (ignore_proxy) {
245 network_config = net::ProxyConfig();
246 network_availability = net::ProxyConfigService::CONFIG_VALID;
247 } else if (network_proxy_configured) {
248 // Network is private or shared with user using shared proxies.
249 VLOG(1) << this << ": using proxy of network " << network->path();
250 network_availability = net::ProxyConfigService::CONFIG_VALID;
251 }
252 }
253
254 // Determine effective proxy config, either from prefs or network.
255 ProxyPrefs::ConfigState effective_config_state;
256 net::ProxyConfig effective_config;
257 GetEffectiveProxyConfig(pref_state, pref_config,
258 network_availability, network_config, ignore_proxy,
259 &effective_config_state, &effective_config);
260
261 // Activate effective proxy and store into |active_config_|.
262 // If last update didn't complete, we definitely update now.
263 bool update_now = update_pending();
264 if (!update_now) { // Otherwise, only update now if there're changes.
265 update_now = active_config_state_ != effective_config_state ||
266 (active_config_state_ != ProxyPrefs::CONFIG_UNSET &&
267 !active_config_.Equals(effective_config));
268 }
269 if (update_now) { // Activate and store new effective config.
270 active_config_state_ = effective_config_state;
271 if (active_config_state_ != ProxyPrefs::CONFIG_UNSET)
272 active_config_ = effective_config;
273 // If effective config is from system (i.e. network), it's considered a
274 // special kind of prefs that ranks below policy/extension but above
275 // others, so bump it up to CONFIG_OTHER_PRECEDE to force its precedence
276 // when PrefProxyConfigTrackerImpl pushes it to ChromeProxyConfigService.
277 if (effective_config_state == ProxyPrefs::CONFIG_SYSTEM)
278 effective_config_state = ProxyPrefs::CONFIG_OTHER_PRECEDE;
279 // If config is manual, add rule to bypass local host.
280 if (effective_config.proxy_rules().type !=
281 net::ProxyConfig::ProxyRules::TYPE_NO_RULES) {
282 effective_config.proxy_rules().bypass_rules.AddRuleToBypassLocal();
283 }
284 PrefProxyConfigTrackerImpl::OnProxyConfigChanged(effective_config_state,
285 effective_config);
286 if (VLOG_IS_ON(1) && !update_pending()) { // Update was successful.
287 std::unique_ptr<base::DictionaryValue> config_dict(
288 effective_config.ToValue());
289 VLOG(1) << this << ": Proxy changed: "
290 << ProxyPrefs::ConfigStateToDebugString(active_config_state_)
291 << ", " << *config_dict;
292 }
293 }
294 }
295
296 } // namespace chromeos
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/proxy_config_service_impl.h ('k') | chrome/browser/chromeos/proxy_config_service_impl_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698