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"
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/browser/prefs/proxy_config_dictionary.h"
18 #include "chrome/browser/prefs/proxy_prefs.h"
19 #include "chrome/common/pref_names.h"
20 #include "chromeos/network/network_profile.h"
21 #include "chromeos/network/network_profile_handler.h"
22 #include "chromeos/network/network_state.h"
23 #include "chromeos/network/network_state_handler.h"
24 #include "chromeos/network/onc/onc_utils.h"
25 #include "components/policy/core/common/cloud/cloud_policy_constants.h"
26 #include "components/user_manager/user_manager.h"
32 // Writes the proxy config of |network| to |proxy_config|. Set |onc_source| to
33 // the source of this configuration. Returns false if no
34 // proxy was configured for this network.
35 bool GetProxyConfig(const PrefService
* profile_prefs
,
36 const PrefService
* local_state_prefs
,
37 const NetworkState
& network
,
38 net::ProxyConfig
* proxy_config
,
39 ::onc::ONCSource
* onc_source
) {
40 scoped_ptr
<ProxyConfigDictionary
> proxy_dict
=
41 proxy_config::GetProxyConfigForNetwork(
42 profile_prefs
, local_state_prefs
, network
, onc_source
);
45 return PrefProxyConfigTrackerImpl::PrefConfigToNetConfig(*proxy_dict
,
51 ProxyConfigServiceImpl::ProxyConfigServiceImpl(PrefService
* profile_prefs
,
52 PrefService
* local_state_prefs
)
53 : PrefProxyConfigTrackerImpl(profile_prefs
? profile_prefs
55 active_config_state_(ProxyPrefs::CONFIG_UNSET
),
56 profile_prefs_(profile_prefs
),
57 local_state_prefs_(local_state_prefs
),
58 pointer_factory_(this) {
59 const base::Closure proxy_change_callback
= base::Bind(
60 &ProxyConfigServiceImpl::OnProxyPrefChanged
, base::Unretained(this));
63 profile_pref_registrar_
.Init(profile_prefs
);
64 profile_pref_registrar_
.Add(prefs::kOpenNetworkConfiguration
,
65 proxy_change_callback
);
66 profile_pref_registrar_
.Add(prefs::kUseSharedProxies
,
67 proxy_change_callback
);
69 local_state_pref_registrar_
.Init(local_state_prefs
);
70 local_state_pref_registrar_
.Add(prefs::kDeviceOpenNetworkConfiguration
,
71 proxy_change_callback
);
73 // Register for changes to the default network.
74 NetworkStateHandler
* state_handler
=
75 NetworkHandler::Get()->network_state_handler();
76 state_handler
->AddObserver(this, FROM_HERE
);
77 DefaultNetworkChanged(state_handler
->DefaultNetwork());
80 ProxyConfigServiceImpl::~ProxyConfigServiceImpl() {
81 if (NetworkHandler::IsInitialized()) {
82 NetworkHandler::Get()->network_state_handler()->RemoveObserver(
87 void ProxyConfigServiceImpl::OnProxyConfigChanged(
88 ProxyPrefs::ConfigState config_state
,
89 const net::ProxyConfig
& config
) {
90 VLOG(1) << "Got prefs change: "
91 << ProxyPrefs::ConfigStateToDebugString(config_state
)
92 << ", mode=" << config
.proxy_rules().type
;
93 DetermineEffectiveConfigFromDefaultNetwork();
96 void ProxyConfigServiceImpl::OnProxyPrefChanged() {
97 DetermineEffectiveConfigFromDefaultNetwork();
100 void ProxyConfigServiceImpl::DefaultNetworkChanged(
101 const NetworkState
* new_network
) {
102 std::string new_network_path
;
104 new_network_path
= new_network
->path();
106 VLOG(1) << "DefaultNetworkChanged to '" << new_network_path
<< "'.";
107 VLOG_IF(1, new_network
) << "New network: name=" << new_network
->name()
108 << ", profile=" << new_network
->profile_path();
110 // Even if the default network is the same, its proxy config (e.g. if private
111 // version of network replaces the shared version after login), or
112 // use-shared-proxies setting (e.g. after login) may have changed, so
113 // re-determine effective proxy config, and activate if different.
114 DetermineEffectiveConfigFromDefaultNetwork();
118 bool ProxyConfigServiceImpl::IgnoreProxy(const PrefService
* profile_prefs
,
119 const std::string network_profile_path
,
120 ::onc::ONCSource onc_source
) {
121 if (!profile_prefs
) {
122 // If the profile preference are not available, this must be the object
123 // associated to local state used for system requests or login-profile. Make
124 // sure that proxies are enabled.
125 VLOG(1) << "Use proxy for system requests and sign-in screen.";
129 if (network_profile_path
.empty())
132 const NetworkProfile
* profile
= NetworkHandler::Get()
133 ->network_profile_handler()->GetProfileForPath(network_profile_path
);
135 VLOG(1) << "Unknown profile_path '" << network_profile_path
136 << "'. Ignoring proxy.";
139 if (profile
->type() == NetworkProfile::TYPE_USER
) {
140 VLOG(1) << "Respect proxy of not-shared networks.";
143 if (onc_source
== ::onc::ONC_SOURCE_DEVICE_POLICY
) {
144 policy::BrowserPolicyConnectorChromeOS
* connector
=
145 g_browser_process
->platform_part()->browser_policy_connector_chromeos();
146 const user_manager::User
* logged_in_user
=
147 user_manager::UserManager::Get()->GetLoggedInUser();
148 if (connector
->GetUserAffiliation(logged_in_user
->email()) ==
149 policy::USER_AFFILIATION_MANAGED
) {
150 VLOG(1) << "Respecting proxy for network, as logged-in user belongs to "
151 << "the domain the device is enrolled to.";
156 // This network is shared and not managed by the user's domain.
157 bool use_shared_proxies
= profile_prefs
->GetBoolean(prefs::kUseSharedProxies
);
158 VLOG(1) << "Use proxy of shared network: " << use_shared_proxies
;
159 return !use_shared_proxies
;
162 void ProxyConfigServiceImpl::DetermineEffectiveConfigFromDefaultNetwork() {
163 NetworkStateHandler
* handler
= NetworkHandler::Get()->network_state_handler();
164 const NetworkState
* network
= handler
->DefaultNetwork();
166 // Get prefs proxy config if available.
167 net::ProxyConfig pref_config
;
168 ProxyPrefs::ConfigState pref_state
= GetProxyConfig(&pref_config
);
170 // Get network proxy config if available.
171 net::ProxyConfig network_config
;
172 net::ProxyConfigService::ConfigAvailability network_availability
=
173 net::ProxyConfigService::CONFIG_UNSET
;
174 bool ignore_proxy
= true;
176 ::onc::ONCSource onc_source
= ::onc::ONC_SOURCE_NONE
;
177 const bool network_proxy_configured
= chromeos::GetProxyConfig(
178 prefs(), local_state_prefs_
, *network
, &network_config
, &onc_source
);
180 IgnoreProxy(profile_prefs_
, network
->profile_path(), onc_source
);
182 // If network is shared but use-shared-proxies is off, use direct mode.
184 network_config
= net::ProxyConfig();
185 network_availability
= net::ProxyConfigService::CONFIG_VALID
;
186 } else if (network_proxy_configured
) {
187 // Network is private or shared with user using shared proxies.
188 VLOG(1) << this << ": using proxy of network " << network
->path();
189 network_availability
= net::ProxyConfigService::CONFIG_VALID
;
193 // Determine effective proxy config, either from prefs or network.
194 ProxyPrefs::ConfigState effective_config_state
;
195 net::ProxyConfig effective_config
;
196 GetEffectiveProxyConfig(pref_state
, pref_config
,
197 network_availability
, network_config
, ignore_proxy
,
198 &effective_config_state
, &effective_config
);
200 // Activate effective proxy and store into |active_config_|.
201 // If last update didn't complete, we definitely update now.
202 bool update_now
= update_pending();
203 if (!update_now
) { // Otherwise, only update now if there're changes.
204 update_now
= active_config_state_
!= effective_config_state
||
205 (active_config_state_
!= ProxyPrefs::CONFIG_UNSET
&&
206 !active_config_
.Equals(effective_config
));
208 if (update_now
) { // Activate and store new effective config.
209 active_config_state_
= effective_config_state
;
210 if (active_config_state_
!= ProxyPrefs::CONFIG_UNSET
)
211 active_config_
= effective_config
;
212 // If effective config is from system (i.e. network), it's considered a
213 // special kind of prefs that ranks below policy/extension but above
214 // others, so bump it up to CONFIG_OTHER_PRECEDE to force its precedence
215 // when PrefProxyConfigTrackerImpl pushes it to ChromeProxyConfigService.
216 if (effective_config_state
== ProxyPrefs::CONFIG_SYSTEM
)
217 effective_config_state
= ProxyPrefs::CONFIG_OTHER_PRECEDE
;
218 // If config is manual, add rule to bypass local host.
219 if (effective_config
.proxy_rules().type
!=
220 net::ProxyConfig::ProxyRules::TYPE_NO_RULES
) {
221 effective_config
.proxy_rules().bypass_rules
.AddRuleToBypassLocal();
223 PrefProxyConfigTrackerImpl::OnProxyConfigChanged(effective_config_state
,
225 if (VLOG_IS_ON(1) && !update_pending()) { // Update was successful.
226 scoped_ptr
<base::DictionaryValue
> config_dict(effective_config
.ToValue());
227 VLOG(1) << this << ": Proxy changed: "
228 << ProxyPrefs::ConfigStateToDebugString(active_config_state_
)
229 << ", " << *config_dict
;
234 } // namespace chromeos