| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/chromeos/proxy_config_service_impl.h" | 5 #include "chromeos/network/proxy/proxy_config_service_impl.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/threading/worker_pool.h" |
| 13 #include "base/values.h" | 14 #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" | 15 #include "chromeos/network/network_profile.h" |
| 18 #include "chromeos/network/network_profile_handler.h" | 16 #include "chromeos/network/network_profile_handler.h" |
| 19 #include "chromeos/network/network_state.h" | 17 #include "chromeos/network/network_state.h" |
| 20 #include "chromeos/network/network_state_handler.h" | 18 #include "chromeos/network/network_state_handler.h" |
| 21 #include "chromeos/network/onc/onc_utils.h" | 19 #include "chromeos/network/onc/onc_utils.h" |
| 20 #include "chromeos/network/proxy/proxy_config_handler.h" |
| 22 #include "components/onc/onc_pref_names.h" | 21 #include "components/onc/onc_pref_names.h" |
| 23 #include "components/policy/core/common/cloud/cloud_policy_constants.h" | 22 #include "components/policy/core/common/cloud/cloud_policy_constants.h" |
| 24 #include "components/prefs/pref_service.h" | 23 #include "components/prefs/pref_service.h" |
| 25 #include "components/proxy_config/pref_proxy_config_tracker_impl.h" | 24 #include "components/proxy_config/pref_proxy_config_tracker_impl.h" |
| 26 #include "components/proxy_config/proxy_config_dictionary.h" | 25 #include "components/proxy_config/proxy_config_dictionary.h" |
| 27 #include "components/proxy_config/proxy_config_pref_names.h" | 26 #include "components/proxy_config/proxy_config_pref_names.h" |
| 28 #include "components/proxy_config/proxy_prefs.h" | 27 #include "components/proxy_config/proxy_prefs.h" |
| 29 #include "components/user_manager/user_manager.h" | 28 #include "components/user_manager/user_manager.h" |
| 30 #include "content/public/browser/browser_thread.h" | |
| 31 | 29 |
| 32 namespace chromeos { | 30 namespace chromeos { |
| 33 | 31 |
| 34 namespace { | 32 namespace { |
| 35 | 33 |
| 36 // Writes the proxy config of |network| to |proxy_config|. Set |onc_source| to | 34 // Writes the proxy config of |network| to |proxy_config|. Set |onc_source| to |
| 37 // the source of this configuration. Returns false if no | 35 // the source of this configuration. Returns false if no proxy was configured |
| 38 // proxy was configured for this network. | 36 // for this network. |
| 39 bool GetProxyConfig(const PrefService* profile_prefs, | 37 bool GetNetworkProxyConfig(const PrefService* profile_prefs, |
| 40 const PrefService* local_state_prefs, | 38 const PrefService* local_state_prefs, |
| 41 const NetworkState& network, | 39 const NetworkState& network, |
| 42 net::ProxyConfig* proxy_config, | 40 net::ProxyConfig* proxy_config, |
| 43 ::onc::ONCSource* onc_source) { | 41 ::onc::ONCSource* onc_source) { |
| 44 std::unique_ptr<ProxyConfigDictionary> proxy_dict = | 42 std::unique_ptr<ProxyConfigDictionary> proxy_dict = |
| 45 proxy_config::GetProxyConfigForNetwork(profile_prefs, local_state_prefs, | 43 proxy_config::GetProxyConfigForNetwork(profile_prefs, local_state_prefs, |
| 46 network, onc_source); | 44 network, onc_source); |
| 47 if (!proxy_dict) | 45 if (!proxy_dict) |
| 48 return false; | 46 return false; |
| 49 return PrefProxyConfigTrackerImpl::PrefConfigToNetConfig(*proxy_dict, | 47 return PrefProxyConfigTrackerImpl::PrefConfigToNetConfig(*proxy_dict, |
| 50 proxy_config); | 48 proxy_config); |
| 51 } | 49 } |
| 52 | 50 |
| 53 } // namespace | 51 } // namespace |
| 54 | 52 |
| 55 ProxyConfigServiceImpl::ProxyConfigServiceImpl(PrefService* profile_prefs, | 53 ProxyConfigServiceImpl::ProxyConfigServiceImpl( |
| 56 PrefService* local_state_prefs) | 54 PrefService* profile_prefs, |
| 55 PrefService* local_state_prefs, |
| 56 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner) |
| 57 : PrefProxyConfigTrackerImpl( | 57 : PrefProxyConfigTrackerImpl( |
| 58 profile_prefs ? profile_prefs : local_state_prefs, | 58 profile_prefs ? profile_prefs : local_state_prefs, |
| 59 content::BrowserThread::GetTaskRunnerForThread( | 59 io_task_runner), |
| 60 content::BrowserThread::IO)), | |
| 61 active_config_state_(ProxyPrefs::CONFIG_UNSET), | 60 active_config_state_(ProxyPrefs::CONFIG_UNSET), |
| 62 profile_prefs_(profile_prefs), | 61 profile_prefs_(profile_prefs), |
| 63 local_state_prefs_(local_state_prefs), | 62 local_state_prefs_(local_state_prefs), |
| 64 pointer_factory_(this) { | 63 pointer_factory_(this) { |
| 65 const base::Closure proxy_change_callback = base::Bind( | 64 const base::Closure proxy_change_callback = base::Bind( |
| 66 &ProxyConfigServiceImpl::OnProxyPrefChanged, base::Unretained(this)); | 65 &ProxyConfigServiceImpl::OnProxyPrefChanged, base::Unretained(this)); |
| 67 | 66 |
| 68 if (profile_prefs) { | 67 if (profile_prefs) { |
| 69 profile_pref_registrar_.Init(profile_prefs); | 68 profile_pref_registrar_.Init(profile_prefs); |
| 70 profile_pref_registrar_.Add(::onc::prefs::kOpenNetworkConfiguration, | 69 profile_pref_registrar_.Add(::onc::prefs::kOpenNetworkConfiguration, |
| 71 proxy_change_callback); | 70 proxy_change_callback); |
| 72 profile_pref_registrar_.Add(::proxy_config::prefs::kUseSharedProxies, | 71 profile_pref_registrar_.Add(::proxy_config::prefs::kUseSharedProxies, |
| 73 proxy_change_callback); | 72 proxy_change_callback); |
| 74 } | 73 } |
| 75 local_state_pref_registrar_.Init(local_state_prefs); | 74 local_state_pref_registrar_.Init(local_state_prefs); |
| 76 local_state_pref_registrar_.Add(::onc::prefs::kDeviceOpenNetworkConfiguration, | 75 local_state_pref_registrar_.Add(::onc::prefs::kDeviceOpenNetworkConfiguration, |
| 77 proxy_change_callback); | 76 proxy_change_callback); |
| 78 | 77 |
| 79 // Register for changes to the default network. | 78 // Register for changes to the default network. |
| 80 NetworkStateHandler* state_handler = | 79 NetworkStateHandler* state_handler = |
| 81 NetworkHandler::Get()->network_state_handler(); | 80 NetworkHandler::Get()->network_state_handler(); |
| 82 state_handler->AddObserver(this, FROM_HERE); | 81 state_handler->AddObserver(this, FROM_HERE); |
| 83 DefaultNetworkChanged(state_handler->DefaultNetwork()); | 82 DefaultNetworkChanged(state_handler->DefaultNetwork()); |
| 84 } | 83 } |
| 85 | 84 |
| 86 ProxyConfigServiceImpl::~ProxyConfigServiceImpl() { | 85 ProxyConfigServiceImpl::~ProxyConfigServiceImpl() { |
| 87 if (NetworkHandler::IsInitialized()) { | 86 if (NetworkHandler::IsInitialized()) { |
| 88 NetworkHandler::Get()->network_state_handler()->RemoveObserver( | 87 NetworkHandler::Get()->network_state_handler()->RemoveObserver(this, |
| 89 this, FROM_HERE); | 88 FROM_HERE); |
| 90 } | 89 } |
| 91 } | 90 } |
| 92 | 91 |
| 93 void ProxyConfigServiceImpl::OnProxyConfigChanged( | 92 void ProxyConfigServiceImpl::OnProxyConfigChanged( |
| 94 ProxyPrefs::ConfigState config_state, | 93 ProxyPrefs::ConfigState config_state, |
| 95 const net::ProxyConfig& config) { | 94 const net::ProxyConfig& config) { |
| 96 VLOG(1) << "Got prefs change: " | 95 VLOG(1) << "Got prefs change: " |
| 97 << ProxyPrefs::ConfigStateToDebugString(config_state) | 96 << ProxyPrefs::ConfigStateToDebugString(config_state) |
| 98 << ", mode=" << config.proxy_rules().type; | 97 << ", mode=" << config.proxy_rules().type; |
| 99 DetermineEffectiveConfigFromDefaultNetwork(); | 98 DetermineEffectiveConfigFromDefaultNetwork(); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 // If the profile preference are not available, this must be the object | 134 // 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 | 135 // associated to local state used for system requests or login-profile. Make |
| 137 // sure that proxies are enabled. | 136 // sure that proxies are enabled. |
| 138 VLOG(1) << "Use proxy for system requests and sign-in screen."; | 137 VLOG(1) << "Use proxy for system requests and sign-in screen."; |
| 139 return false; | 138 return false; |
| 140 } | 139 } |
| 141 | 140 |
| 142 if (network_profile_path.empty()) | 141 if (network_profile_path.empty()) |
| 143 return true; | 142 return true; |
| 144 | 143 |
| 145 const NetworkProfile* profile = NetworkHandler::Get() | 144 const NetworkProfile* profile = |
| 146 ->network_profile_handler()->GetProfileForPath(network_profile_path); | 145 NetworkHandler::Get()->network_profile_handler()->GetProfileForPath( |
| 146 network_profile_path); |
| 147 if (!profile) { | 147 if (!profile) { |
| 148 VLOG(1) << "Unknown profile_path '" << network_profile_path | 148 VLOG(1) << "Unknown profile_path '" << network_profile_path |
| 149 << "'. Ignoring proxy."; | 149 << "'. Ignoring proxy."; |
| 150 return true; | 150 return true; |
| 151 } | 151 } |
| 152 if (profile->type() == NetworkProfile::TYPE_USER) { | 152 if (profile->type() == NetworkProfile::TYPE_USER) { |
| 153 VLOG(1) << "Respect proxy of not-shared networks."; | 153 VLOG(1) << "Respect proxy of not-shared networks."; |
| 154 return false; | 154 return false; |
| 155 } | 155 } |
| 156 if (onc_source == ::onc::ONC_SOURCE_USER_POLICY) { | 156 if (onc_source == ::onc::ONC_SOURCE_USER_POLICY) { |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 190 if (PrefProxyConfigTrackerImpl::PrefPrecedes(pref_state)) { | 190 if (PrefProxyConfigTrackerImpl::PrefPrecedes(pref_state)) { |
| 191 const PrefService::Preference* const pref = | 191 const PrefService::Preference* const pref = |
| 192 profile_prefs->FindPreference(::proxy_config::prefs::kProxy); | 192 profile_prefs->FindPreference(::proxy_config::prefs::kProxy); |
| 193 const base::DictionaryValue* proxy_config_value; | 193 const base::DictionaryValue* proxy_config_value; |
| 194 bool value_exists = pref->GetValue()->GetAsDictionary(&proxy_config_value); | 194 bool value_exists = pref->GetValue()->GetAsDictionary(&proxy_config_value); |
| 195 DCHECK(value_exists); | 195 DCHECK(value_exists); |
| 196 | 196 |
| 197 return base::MakeUnique<ProxyConfigDictionary>(proxy_config_value); | 197 return base::MakeUnique<ProxyConfigDictionary>(proxy_config_value); |
| 198 } | 198 } |
| 199 | 199 |
| 200 const chromeos::NetworkState* network = chromeos::NetworkHandler::Get() | 200 const NetworkState* network = |
| 201 ->network_state_handler() | 201 NetworkHandler::Get()->network_state_handler()->DefaultNetwork(); |
| 202 ->DefaultNetwork(); | |
| 203 // No connected network. | 202 // No connected network. |
| 204 if (!network) | 203 if (!network) |
| 205 return nullptr; | 204 return nullptr; |
| 206 | 205 |
| 207 // Apply network proxy configuration. | 206 // Apply network proxy configuration. |
| 208 ::onc::ONCSource onc_source; | 207 ::onc::ONCSource onc_source; |
| 209 std::unique_ptr<ProxyConfigDictionary> proxy_config = | 208 std::unique_ptr<ProxyConfigDictionary> proxy_config = |
| 210 chromeos::proxy_config::GetProxyConfigForNetwork( | 209 proxy_config::GetProxyConfigForNetwork(profile_prefs, local_state_prefs, |
| 211 profile_prefs, local_state_prefs, *network, &onc_source); | 210 *network, &onc_source); |
| 212 if (!chromeos::ProxyConfigServiceImpl::IgnoreProxy( | 211 if (!ProxyConfigServiceImpl::IgnoreProxy(profile_prefs, |
| 213 profile_prefs, network->profile_path(), onc_source)) | 212 network->profile_path(), onc_source)) |
| 214 return proxy_config; | 213 return proxy_config; |
| 215 | 214 |
| 216 return base::MakeUnique<ProxyConfigDictionary>( | 215 return base::MakeUnique<ProxyConfigDictionary>( |
| 217 ProxyConfigDictionary::CreateDirect()); | 216 ProxyConfigDictionary::CreateDirect()); |
| 218 } | 217 } |
| 219 | 218 |
| 220 void ProxyConfigServiceImpl::DetermineEffectiveConfigFromDefaultNetwork() { | 219 void ProxyConfigServiceImpl::DetermineEffectiveConfigFromDefaultNetwork() { |
| 221 if (!NetworkHandler::IsInitialized()) | 220 if (!NetworkHandler::IsInitialized()) |
| 222 return; | 221 return; |
| 223 | 222 |
| 224 NetworkStateHandler* handler = NetworkHandler::Get()->network_state_handler(); | 223 NetworkStateHandler* handler = NetworkHandler::Get()->network_state_handler(); |
| 225 const NetworkState* network = handler->DefaultNetwork(); | 224 const NetworkState* network = handler->DefaultNetwork(); |
| 226 | 225 |
| 227 // Get prefs proxy config if available. | 226 // Get prefs proxy config if available. |
| 228 net::ProxyConfig pref_config; | 227 net::ProxyConfig pref_config; |
| 229 ProxyPrefs::ConfigState pref_state = GetProxyConfig(&pref_config); | 228 ProxyPrefs::ConfigState pref_state = GetProxyConfig(&pref_config); |
| 230 | 229 |
| 231 // Get network proxy config if available. | 230 // Get network proxy config if available. |
| 232 net::ProxyConfig network_config; | 231 net::ProxyConfig network_config; |
| 233 net::ProxyConfigService::ConfigAvailability network_availability = | 232 net::ProxyConfigService::ConfigAvailability network_availability = |
| 234 net::ProxyConfigService::CONFIG_UNSET; | 233 net::ProxyConfigService::CONFIG_UNSET; |
| 235 bool ignore_proxy = true; | 234 bool ignore_proxy = true; |
| 236 if (network) { | 235 if (network) { |
| 237 ::onc::ONCSource onc_source = ::onc::ONC_SOURCE_NONE; | 236 ::onc::ONCSource onc_source = ::onc::ONC_SOURCE_NONE; |
| 238 const bool network_proxy_configured = chromeos::GetProxyConfig( | 237 const bool network_proxy_configured = GetNetworkProxyConfig( |
| 239 prefs(), local_state_prefs_, *network, &network_config, &onc_source); | 238 prefs(), local_state_prefs_, *network, &network_config, &onc_source); |
| 240 ignore_proxy = | 239 ignore_proxy = |
| 241 IgnoreProxy(profile_prefs_, network->profile_path(), onc_source); | 240 IgnoreProxy(profile_prefs_, network->profile_path(), onc_source); |
| 242 | 241 |
| 243 // If network is shared but use-shared-proxies is off, use direct mode. | 242 // If network is shared but use-shared-proxies is off, use direct mode. |
| 244 if (ignore_proxy) { | 243 if (ignore_proxy) { |
| 245 network_config = net::ProxyConfig(); | 244 network_config = net::ProxyConfig(); |
| 246 network_availability = net::ProxyConfigService::CONFIG_VALID; | 245 network_availability = net::ProxyConfigService::CONFIG_VALID; |
| 247 } else if (network_proxy_configured) { | 246 } else if (network_proxy_configured) { |
| 248 // Network is private or shared with user using shared proxies. | 247 // Network is private or shared with user using shared proxies. |
| 249 VLOG(1) << this << ": using proxy of network " << network->path(); | 248 VLOG(1) << this << ": using proxy of network " << network->path(); |
| 250 network_availability = net::ProxyConfigService::CONFIG_VALID; | 249 network_availability = net::ProxyConfigService::CONFIG_VALID; |
| 251 } | 250 } |
| 252 } | 251 } |
| 253 | 252 |
| 254 // Determine effective proxy config, either from prefs or network. | 253 // Determine effective proxy config, either from prefs or network. |
| 255 ProxyPrefs::ConfigState effective_config_state; | 254 ProxyPrefs::ConfigState effective_config_state; |
| 256 net::ProxyConfig effective_config; | 255 net::ProxyConfig effective_config; |
| 257 GetEffectiveProxyConfig(pref_state, pref_config, | 256 GetEffectiveProxyConfig(pref_state, pref_config, network_availability, |
| 258 network_availability, network_config, ignore_proxy, | 257 network_config, ignore_proxy, &effective_config_state, |
| 259 &effective_config_state, &effective_config); | 258 &effective_config); |
| 260 | 259 |
| 261 // Activate effective proxy and store into |active_config_|. | 260 // Activate effective proxy and store into |active_config_|. |
| 262 // If last update didn't complete, we definitely update now. | 261 // If last update didn't complete, we definitely update now. |
| 263 bool update_now = update_pending(); | 262 bool update_now = update_pending(); |
| 264 if (!update_now) { // Otherwise, only update now if there're changes. | 263 if (!update_now) { // Otherwise, only update now if there're changes. |
| 265 update_now = active_config_state_ != effective_config_state || | 264 update_now = active_config_state_ != effective_config_state || |
| 266 (active_config_state_ != ProxyPrefs::CONFIG_UNSET && | 265 (active_config_state_ != ProxyPrefs::CONFIG_UNSET && |
| 267 !active_config_.Equals(effective_config)); | 266 !active_config_.Equals(effective_config)); |
| 268 } | 267 } |
| 269 if (update_now) { // Activate and store new effective config. | 268 if (update_now) { // Activate and store new effective config. |
| (...skipping 17 matching lines...) Expand all Loading... |
| 287 std::unique_ptr<base::DictionaryValue> config_dict( | 286 std::unique_ptr<base::DictionaryValue> config_dict( |
| 288 effective_config.ToValue()); | 287 effective_config.ToValue()); |
| 289 VLOG(1) << this << ": Proxy changed: " | 288 VLOG(1) << this << ": Proxy changed: " |
| 290 << ProxyPrefs::ConfigStateToDebugString(active_config_state_) | 289 << ProxyPrefs::ConfigStateToDebugString(active_config_state_) |
| 291 << ", " << *config_dict; | 290 << ", " << *config_dict; |
| 292 } | 291 } |
| 293 } | 292 } |
| 294 } | 293 } |
| 295 | 294 |
| 296 } // namespace chromeos | 295 } // namespace chromeos |
| OLD | NEW |