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/prefs/pref_service.h"
13 #include "base/values.h"
14 #include "chrome/browser/browser_process.h"
15 #include "chrome/browser/chromeos/login/user_manager.h"
16 #include "chrome/browser/chromeos/net/proxy_config_handler.h"
17 #include "chrome/browser/policy/browser_policy_connector.h"
18 #include "chrome/browser/prefs/proxy_config_dictionary.h"
19 #include "chrome/browser/prefs/proxy_prefs.h"
20 #include "chrome/common/pref_names.h"
21 #include "chromeos/network/favorite_state.h"
22 #include "chromeos/network/network_profile.h"
23 #include "chromeos/network/network_profile_handler.h"
24 #include "chromeos/network/network_state.h"
25 #include "chromeos/network/network_state_handler.h"
26 #include "chromeos/network/onc/onc_utils.h"
27 #include "components/policy/core/common/cloud/cloud_policy_constants.h"
28
29 namespace chromeos {
30
31 namespace {
32
33 // Writes the proxy config of |network| to |proxy_config|. Set |onc_source| to
34 // the source of this configuration. Returns false if no
35 // proxy was configured for this network.
GetProxyConfig(const PrefService * profile_prefs,const PrefService * local_state_prefs,const FavoriteState & network,net::ProxyConfig * proxy_config,::onc::ONCSource * onc_source)36 bool GetProxyConfig(const PrefService* profile_prefs,
37 const PrefService* local_state_prefs,
38 const FavoriteState& network,
39 net::ProxyConfig* proxy_config,
40 ::onc::ONCSource* onc_source) {
41 scoped_ptr<ProxyConfigDictionary> proxy_dict =
42 proxy_config::GetProxyConfigForFavoriteNetwork(
43 profile_prefs, local_state_prefs, network, onc_source);
44 if (!proxy_dict)
45 return false;
46 return PrefProxyConfigTrackerImpl::PrefConfigToNetConfig(*proxy_dict,
47 proxy_config);
48 }
49
50 } // namespace
51
ProxyConfigServiceImpl(PrefService * profile_prefs,PrefService * local_state_prefs)52 ProxyConfigServiceImpl::ProxyConfigServiceImpl(PrefService* profile_prefs,
53 PrefService* local_state_prefs)
54 : PrefProxyConfigTrackerImpl(profile_prefs ? profile_prefs
55 : local_state_prefs),
56 active_config_state_(ProxyPrefs::CONFIG_UNSET),
57 profile_prefs_(profile_prefs),
58 local_state_prefs_(local_state_prefs),
59 pointer_factory_(this) {
60 const base::Closure proxy_change_callback = base::Bind(
61 &ProxyConfigServiceImpl::OnProxyPrefChanged, base::Unretained(this));
62
63 if (profile_prefs) {
64 profile_pref_registrar_.Init(profile_prefs);
65 profile_pref_registrar_.Add(prefs::kOpenNetworkConfiguration,
66 proxy_change_callback);
67 profile_pref_registrar_.Add(prefs::kUseSharedProxies,
68 proxy_change_callback);
69 }
70 local_state_pref_registrar_.Init(local_state_prefs);
71 local_state_pref_registrar_.Add(prefs::kDeviceOpenNetworkConfiguration,
72 proxy_change_callback);
73
74 // Register for changes to the default network.
75 NetworkStateHandler* state_handler =
76 NetworkHandler::Get()->network_state_handler();
77 state_handler->AddObserver(this, FROM_HERE);
78 DefaultNetworkChanged(state_handler->DefaultNetwork());
79 }
80
~ProxyConfigServiceImpl()81 ProxyConfigServiceImpl::~ProxyConfigServiceImpl() {
82 if (NetworkHandler::IsInitialized()) {
83 NetworkHandler::Get()->network_state_handler()->RemoveObserver(
84 this, FROM_HERE);
85 }
86 }
87
OnProxyConfigChanged(ProxyPrefs::ConfigState config_state,const net::ProxyConfig & config)88 void ProxyConfigServiceImpl::OnProxyConfigChanged(
89 ProxyPrefs::ConfigState config_state,
90 const net::ProxyConfig& config) {
91 VLOG(1) << "Got prefs change: "
92 << ProxyPrefs::ConfigStateToDebugString(config_state)
93 << ", mode=" << config.proxy_rules().type;
94 DetermineEffectiveConfigFromDefaultNetwork();
95 }
96
OnProxyPrefChanged()97 void ProxyConfigServiceImpl::OnProxyPrefChanged() {
98 DetermineEffectiveConfigFromDefaultNetwork();
99 }
100
DefaultNetworkChanged(const NetworkState * new_network)101 void ProxyConfigServiceImpl::DefaultNetworkChanged(
102 const NetworkState* new_network) {
103 std::string new_network_path;
104 if (new_network)
105 new_network_path = new_network->path();
106
107 VLOG(1) << "DefaultNetworkChanged to '" << new_network_path << "'.";
108 VLOG_IF(1, new_network) << "New network: name=" << new_network->name()
109 << ", profile=" << new_network->profile_path();
110
111 // Even if the default network is the same, its proxy config (e.g. if private
112 // version of network replaces the shared version after login), or
113 // use-shared-proxies setting (e.g. after login) may have changed, so
114 // re-determine effective proxy config, and activate if different.
115 DetermineEffectiveConfigFromDefaultNetwork();
116 }
117
118 // static
IgnoreProxy(const PrefService * profile_prefs,const std::string network_profile_path,::onc::ONCSource onc_source)119 bool ProxyConfigServiceImpl::IgnoreProxy(const PrefService* profile_prefs,
120 const std::string network_profile_path,
121 ::onc::ONCSource onc_source) {
122 if (!profile_prefs) {
123 // If the profile preference are not available, this must be the object
124 // associated to local state used for system requests or login-profile. Make
125 // sure that proxies are enabled.
126 VLOG(1) << "Use proxy for system requests and sign-in screen.";
127 return false;
128 }
129
130 if (network_profile_path.empty())
131 return true;
132
133 const NetworkProfile* profile = NetworkHandler::Get()
134 ->network_profile_handler()->GetProfileForPath(network_profile_path);
135 if (!profile) {
136 LOG(WARNING) << "Unknown profile_path '" << network_profile_path
137 << "'. Ignoring proxy.";
138 return true;
139 }
140 if (profile->type() == NetworkProfile::TYPE_USER) {
141 VLOG(1) << "Respect proxy of not-shared networks.";
142 return false;
143 }
144 if (onc_source == ::onc::ONC_SOURCE_DEVICE_POLICY) {
145 policy::BrowserPolicyConnector* connector =
146 g_browser_process->browser_policy_connector();
147 const User* logged_in_user = 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.";
152 return false;
153 }
154 }
155
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;
160 }
161
DetermineEffectiveConfigFromDefaultNetwork()162 void ProxyConfigServiceImpl::DetermineEffectiveConfigFromDefaultNetwork() {
163 NetworkStateHandler* handler = NetworkHandler::Get()->network_state_handler();
164 const FavoriteState* network = handler->DefaultFavoriteNetwork();
165
166 // Get prefs proxy config if available.
167 net::ProxyConfig pref_config;
168 ProxyPrefs::ConfigState pref_state = GetProxyConfig(&pref_config);
169
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;
175 if (network) {
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);
179 ignore_proxy =
180 IgnoreProxy(profile_prefs_, network->profile_path(), onc_source);
181
182 // If network is shared but use-shared-proxies is off, use direct mode.
183 if (ignore_proxy) {
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;
190 }
191 }
192
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);
199
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));
207 }
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();
222 }
223 PrefProxyConfigTrackerImpl::OnProxyConfigChanged(effective_config_state,
224 effective_config);
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;
230 }
231 }
232 }
233
234 } // namespace chromeos
235