Switch global error menu icon to vectorized MD asset
[chromium-blink-merge.git] / chrome / browser / chromeos / proxy_config_service_impl.cc
blobd5b69a6fdd452b011c5ba70e456a74f6ec2a736e
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.
5 #include "chrome/browser/chromeos/proxy_config_service_impl.h"
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/prefs/pref_service.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/browser/chromeos/policy/browser_policy_connector_chromeos.h"
17 #include "chrome/common/pref_names.h"
18 #include "chromeos/network/network_profile.h"
19 #include "chromeos/network/network_profile_handler.h"
20 #include "chromeos/network/network_state.h"
21 #include "chromeos/network/network_state_handler.h"
22 #include "chromeos/network/onc/onc_utils.h"
23 #include "components/policy/core/common/cloud/cloud_policy_constants.h"
24 #include "components/proxy_config/pref_proxy_config_tracker_impl.h"
25 #include "components/proxy_config/proxy_config_dictionary.h"
26 #include "components/proxy_config/proxy_prefs.h"
27 #include "components/user_manager/user_manager.h"
28 #include "content/public/browser/browser_thread.h"
30 namespace chromeos {
32 namespace {
34 // Writes the proxy config of |network| to |proxy_config|. Set |onc_source| to
35 // the source of this configuration. Returns false if no
36 // proxy was configured for this network.
37 bool GetProxyConfig(const PrefService* profile_prefs,
38 const PrefService* local_state_prefs,
39 const NetworkState& network,
40 net::ProxyConfig* proxy_config,
41 ::onc::ONCSource* onc_source) {
42 scoped_ptr<ProxyConfigDictionary> proxy_dict =
43 proxy_config::GetProxyConfigForNetwork(
44 profile_prefs, local_state_prefs, network, onc_source);
45 if (!proxy_dict)
46 return false;
47 return PrefProxyConfigTrackerImpl::PrefConfigToNetConfig(*proxy_dict,
48 proxy_config);
51 } // namespace
53 ProxyConfigServiceImpl::ProxyConfigServiceImpl(PrefService* profile_prefs,
54 PrefService* local_state_prefs)
55 : PrefProxyConfigTrackerImpl(
56 profile_prefs ? profile_prefs : local_state_prefs,
57 content::BrowserThread::GetMessageLoopProxyForThread(
58 content::BrowserThread::IO)),
59 active_config_state_(ProxyPrefs::CONFIG_UNSET),
60 profile_prefs_(profile_prefs),
61 local_state_prefs_(local_state_prefs),
62 pointer_factory_(this) {
63 const base::Closure proxy_change_callback = base::Bind(
64 &ProxyConfigServiceImpl::OnProxyPrefChanged, base::Unretained(this));
66 if (profile_prefs) {
67 profile_pref_registrar_.Init(profile_prefs);
68 profile_pref_registrar_.Add(prefs::kOpenNetworkConfiguration,
69 proxy_change_callback);
70 profile_pref_registrar_.Add(prefs::kUseSharedProxies,
71 proxy_change_callback);
73 local_state_pref_registrar_.Init(local_state_prefs);
74 local_state_pref_registrar_.Add(prefs::kDeviceOpenNetworkConfiguration,
75 proxy_change_callback);
77 // Register for changes to the default network.
78 NetworkStateHandler* state_handler =
79 NetworkHandler::Get()->network_state_handler();
80 state_handler->AddObserver(this, FROM_HERE);
81 DefaultNetworkChanged(state_handler->DefaultNetwork());
84 ProxyConfigServiceImpl::~ProxyConfigServiceImpl() {
85 if (NetworkHandler::IsInitialized()) {
86 NetworkHandler::Get()->network_state_handler()->RemoveObserver(
87 this, FROM_HERE);
91 void ProxyConfigServiceImpl::OnProxyConfigChanged(
92 ProxyPrefs::ConfigState config_state,
93 const net::ProxyConfig& config) {
94 VLOG(1) << "Got prefs change: "
95 << ProxyPrefs::ConfigStateToDebugString(config_state)
96 << ", mode=" << config.proxy_rules().type;
97 DetermineEffectiveConfigFromDefaultNetwork();
100 void ProxyConfigServiceImpl::OnProxyPrefChanged() {
101 DetermineEffectiveConfigFromDefaultNetwork();
104 void ProxyConfigServiceImpl::DefaultNetworkChanged(
105 const NetworkState* new_network) {
106 std::string new_network_path;
107 if (new_network)
108 new_network_path = new_network->path();
110 VLOG(1) << "DefaultNetworkChanged to '" << new_network_path << "'.";
111 VLOG_IF(1, new_network) << "New network: name=" << new_network->name()
112 << ", profile=" << new_network->profile_path();
114 // Even if the default network is the same, its proxy config (e.g. if private
115 // version of network replaces the shared version after login), or
116 // use-shared-proxies setting (e.g. after login) may have changed, so
117 // re-determine effective proxy config, and activate if different.
118 DetermineEffectiveConfigFromDefaultNetwork();
121 // static
122 bool ProxyConfigServiceImpl::IgnoreProxy(const PrefService* profile_prefs,
123 const std::string network_profile_path,
124 ::onc::ONCSource onc_source) {
125 if (!profile_prefs) {
126 // If the profile preference are not available, this must be the object
127 // associated to local state used for system requests or login-profile. Make
128 // sure that proxies are enabled.
129 VLOG(1) << "Use proxy for system requests and sign-in screen.";
130 return false;
133 if (network_profile_path.empty())
134 return true;
136 const NetworkProfile* profile = NetworkHandler::Get()
137 ->network_profile_handler()->GetProfileForPath(network_profile_path);
138 if (!profile) {
139 VLOG(1) << "Unknown profile_path '" << network_profile_path
140 << "'. Ignoring proxy.";
141 return true;
143 if (profile->type() == NetworkProfile::TYPE_USER) {
144 VLOG(1) << "Respect proxy of not-shared networks.";
145 return false;
147 if (onc_source == ::onc::ONC_SOURCE_USER_POLICY) {
148 // Note that this case can occur if the network is shared (e.g. ethernet)
149 // but the proxy is determined by user policy.
150 // See https://crbug.com/454966 .
151 VLOG(1) << "Respect proxy from user policy although network is shared.";
152 return false;
154 if (onc_source == ::onc::ONC_SOURCE_DEVICE_POLICY) {
155 policy::BrowserPolicyConnectorChromeOS* connector =
156 g_browser_process->platform_part()->browser_policy_connector_chromeos();
157 const user_manager::User* logged_in_user =
158 user_manager::UserManager::Get()->GetLoggedInUser();
159 if (connector->GetUserAffiliation(logged_in_user->email()) ==
160 policy::USER_AFFILIATION_MANAGED) {
161 VLOG(1) << "Respecting proxy for network, as logged-in user belongs to "
162 << "the domain the device is enrolled to.";
163 return false;
167 // This network is shared and not managed by the user's domain.
168 bool use_shared_proxies = profile_prefs->GetBoolean(prefs::kUseSharedProxies);
169 VLOG(1) << "Use proxy of shared network: " << use_shared_proxies;
170 return !use_shared_proxies;
173 void ProxyConfigServiceImpl::DetermineEffectiveConfigFromDefaultNetwork() {
174 NetworkStateHandler* handler = NetworkHandler::Get()->network_state_handler();
175 const NetworkState* network = handler->DefaultNetwork();
177 // Get prefs proxy config if available.
178 net::ProxyConfig pref_config;
179 ProxyPrefs::ConfigState pref_state = GetProxyConfig(&pref_config);
181 // Get network proxy config if available.
182 net::ProxyConfig network_config;
183 net::ProxyConfigService::ConfigAvailability network_availability =
184 net::ProxyConfigService::CONFIG_UNSET;
185 bool ignore_proxy = true;
186 if (network) {
187 ::onc::ONCSource onc_source = ::onc::ONC_SOURCE_NONE;
188 const bool network_proxy_configured = chromeos::GetProxyConfig(
189 prefs(), local_state_prefs_, *network, &network_config, &onc_source);
190 ignore_proxy =
191 IgnoreProxy(profile_prefs_, network->profile_path(), onc_source);
193 // If network is shared but use-shared-proxies is off, use direct mode.
194 if (ignore_proxy) {
195 network_config = net::ProxyConfig();
196 network_availability = net::ProxyConfigService::CONFIG_VALID;
197 } else if (network_proxy_configured) {
198 // Network is private or shared with user using shared proxies.
199 VLOG(1) << this << ": using proxy of network " << network->path();
200 network_availability = net::ProxyConfigService::CONFIG_VALID;
204 // Determine effective proxy config, either from prefs or network.
205 ProxyPrefs::ConfigState effective_config_state;
206 net::ProxyConfig effective_config;
207 GetEffectiveProxyConfig(pref_state, pref_config,
208 network_availability, network_config, ignore_proxy,
209 &effective_config_state, &effective_config);
211 // Activate effective proxy and store into |active_config_|.
212 // If last update didn't complete, we definitely update now.
213 bool update_now = update_pending();
214 if (!update_now) { // Otherwise, only update now if there're changes.
215 update_now = active_config_state_ != effective_config_state ||
216 (active_config_state_ != ProxyPrefs::CONFIG_UNSET &&
217 !active_config_.Equals(effective_config));
219 if (update_now) { // Activate and store new effective config.
220 active_config_state_ = effective_config_state;
221 if (active_config_state_ != ProxyPrefs::CONFIG_UNSET)
222 active_config_ = effective_config;
223 // If effective config is from system (i.e. network), it's considered a
224 // special kind of prefs that ranks below policy/extension but above
225 // others, so bump it up to CONFIG_OTHER_PRECEDE to force its precedence
226 // when PrefProxyConfigTrackerImpl pushes it to ChromeProxyConfigService.
227 if (effective_config_state == ProxyPrefs::CONFIG_SYSTEM)
228 effective_config_state = ProxyPrefs::CONFIG_OTHER_PRECEDE;
229 // If config is manual, add rule to bypass local host.
230 if (effective_config.proxy_rules().type !=
231 net::ProxyConfig::ProxyRules::TYPE_NO_RULES) {
232 effective_config.proxy_rules().bypass_rules.AddRuleToBypassLocal();
234 PrefProxyConfigTrackerImpl::OnProxyConfigChanged(effective_config_state,
235 effective_config);
236 if (VLOG_IS_ON(1) && !update_pending()) { // Update was successful.
237 scoped_ptr<base::DictionaryValue> config_dict(effective_config.ToValue());
238 VLOG(1) << this << ": Proxy changed: "
239 << ProxyPrefs::ConfigStateToDebugString(active_config_state_)
240 << ", " << *config_dict;
245 } // namespace chromeos