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