| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 "chromeos/network/network_connection_handler.h" | 5 #include "chromeos/network/network_connection_handler.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/json/json_reader.h" | 8 #include "base/json/json_reader.h" |
| 9 #include "base/location.h" | 9 #include "base/location.h" |
| 10 #include "base/message_loop/message_loop_proxy.h" | 10 #include "base/message_loop/message_loop_proxy.h" |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 ConnectState connect_state; | 145 ConnectState connect_state; |
| 146 base::Closure success_callback; | 146 base::Closure success_callback; |
| 147 network_handler::ErrorCallback error_callback; | 147 network_handler::ErrorCallback error_callback; |
| 148 }; | 148 }; |
| 149 | 149 |
| 150 NetworkConnectionHandler::NetworkConnectionHandler() | 150 NetworkConnectionHandler::NetworkConnectionHandler() |
| 151 : cert_loader_(NULL), | 151 : cert_loader_(NULL), |
| 152 network_state_handler_(NULL), | 152 network_state_handler_(NULL), |
| 153 configuration_handler_(NULL), | 153 configuration_handler_(NULL), |
| 154 logged_in_(false), | 154 logged_in_(false), |
| 155 certificates_loaded_(false), | 155 certificates_loaded_(false) { |
| 156 applied_autoconnect_policy_(false), | |
| 157 requested_connect_to_best_network_(false) { | |
| 158 } | 156 } |
| 159 | 157 |
| 160 NetworkConnectionHandler::~NetworkConnectionHandler() { | 158 NetworkConnectionHandler::~NetworkConnectionHandler() { |
| 161 if (network_state_handler_) | 159 if (network_state_handler_) |
| 162 network_state_handler_->RemoveObserver(this, FROM_HERE); | 160 network_state_handler_->RemoveObserver(this, FROM_HERE); |
| 163 if (cert_loader_) | 161 if (cert_loader_) |
| 164 cert_loader_->RemoveObserver(this); | 162 cert_loader_->RemoveObserver(this); |
| 165 if (LoginState::IsInitialized()) | 163 if (LoginState::IsInitialized()) |
| 166 LoginState::Get()->RemoveObserver(this); | 164 LoginState::Get()->RemoveObserver(this); |
| 167 } | 165 } |
| (...skipping 16 matching lines...) Expand all Loading... |
| 184 // TODO(tbarzic): Require a mock or stub cert_loader in tests. | 182 // TODO(tbarzic): Require a mock or stub cert_loader in tests. |
| 185 NET_LOG_EVENT("Certificate Loader not initialized", ""); | 183 NET_LOG_EVENT("Certificate Loader not initialized", ""); |
| 186 certificates_loaded_ = true; | 184 certificates_loaded_ = true; |
| 187 } | 185 } |
| 188 | 186 |
| 189 if (network_state_handler) { | 187 if (network_state_handler) { |
| 190 network_state_handler_ = network_state_handler; | 188 network_state_handler_ = network_state_handler; |
| 191 network_state_handler_->AddObserver(this, FROM_HERE); | 189 network_state_handler_->AddObserver(this, FROM_HERE); |
| 192 } | 190 } |
| 193 configuration_handler_ = network_configuration_handler; | 191 configuration_handler_ = network_configuration_handler; |
| 194 | 192 managed_configuration_handler_ = managed_network_configuration_handler; |
| 195 if (managed_network_configuration_handler) { | |
| 196 managed_configuration_handler_ = managed_network_configuration_handler; | |
| 197 managed_configuration_handler_->AddObserver(this); | |
| 198 } | |
| 199 | 193 |
| 200 // After this point, the NetworkConnectionHandler is fully initialized (all | 194 // After this point, the NetworkConnectionHandler is fully initialized (all |
| 201 // handler references set, observers registered, ...). | 195 // handler references set, observers registered, ...). |
| 202 | 196 |
| 203 if (LoginState::IsInitialized()) | 197 if (LoginState::IsInitialized()) |
| 204 LoggedInStateChanged(); | 198 LoggedInStateChanged(); |
| 205 } | 199 } |
| 206 | 200 |
| 201 void NetworkConnectionHandler::AddObserver(Observer* observer) { |
| 202 observers_.AddObserver(observer); |
| 203 } |
| 204 |
| 205 void NetworkConnectionHandler::RemoveObserver(Observer* observer) { |
| 206 observers_.RemoveObserver(observer); |
| 207 } |
| 208 |
| 207 void NetworkConnectionHandler::LoggedInStateChanged() { | 209 void NetworkConnectionHandler::LoggedInStateChanged() { |
| 208 LoginState* login_state = LoginState::Get(); | 210 LoginState* login_state = LoginState::Get(); |
| 209 if (logged_in_ || !login_state->IsUserLoggedIn()) | 211 if (logged_in_ || !login_state->IsUserLoggedIn()) |
| 210 return; | 212 return; |
| 211 | 213 |
| 212 NET_LOG_EVENT("Logged In", ""); | 214 NET_LOG_EVENT("Logged In", ""); |
| 213 logged_in_ = true; | 215 logged_in_ = true; |
| 214 logged_in_time_ = base::TimeTicks::Now(); | 216 logged_in_time_ = base::TimeTicks::Now(); |
| 215 | |
| 216 DisconnectIfPolicyRequires(); | |
| 217 } | 217 } |
| 218 | 218 |
| 219 void NetworkConnectionHandler::OnCertificatesLoaded( | 219 void NetworkConnectionHandler::OnCertificatesLoaded( |
| 220 const net::CertificateList& cert_list, | 220 const net::CertificateList& cert_list, |
| 221 bool initial_load) { | 221 bool initial_load) { |
| 222 certificates_loaded_ = true; | 222 certificates_loaded_ = true; |
| 223 NET_LOG_EVENT("Certificates Loaded", ""); | 223 NET_LOG_EVENT("Certificates Loaded", ""); |
| 224 if (queued_connect_) { | 224 if (queued_connect_) |
| 225 ConnectToQueuedNetwork(); | 225 ConnectToQueuedNetwork(); |
| 226 } else if (initial_load) { | |
| 227 // Connecting to the "best" available network requires certificates to be | |
| 228 // loaded. Try to connect now. | |
| 229 ConnectToBestNetworkAfterLogin(); | |
| 230 } | |
| 231 } | |
| 232 | |
| 233 void NetworkConnectionHandler::PolicyChanged(const std::string& userhash) { | |
| 234 // Ignore user policies. | |
| 235 if (!userhash.empty()) | |
| 236 return; | |
| 237 DisconnectIfPolicyRequires(); | |
| 238 } | 226 } |
| 239 | 227 |
| 240 void NetworkConnectionHandler::ConnectToNetwork( | 228 void NetworkConnectionHandler::ConnectToNetwork( |
| 241 const std::string& service_path, | 229 const std::string& service_path, |
| 242 const base::Closure& success_callback, | 230 const base::Closure& success_callback, |
| 243 const network_handler::ErrorCallback& error_callback, | 231 const network_handler::ErrorCallback& error_callback, |
| 244 bool check_error_state) { | 232 bool check_error_state) { |
| 245 NET_LOG_USER("ConnectToNetwork", service_path); | 233 NET_LOG_USER("ConnectToNetwork", service_path); |
| 234 FOR_EACH_OBSERVER(Observer, observers_, |
| 235 ConnectToNetworkRequested(service_path)); |
| 236 |
| 246 // Clear any existing queued connect request. | 237 // Clear any existing queued connect request. |
| 247 queued_connect_.reset(); | 238 queued_connect_.reset(); |
| 248 if (HasConnectingNetwork(service_path)) { | 239 if (HasConnectingNetwork(service_path)) { |
| 249 NET_LOG_USER("Connect Request While Pending", service_path); | 240 NET_LOG_USER("Connect Request While Pending", service_path); |
| 250 InvokeErrorCallback(service_path, error_callback, kErrorConnecting); | 241 InvokeErrorCallback(service_path, error_callback, kErrorConnecting); |
| 251 return; | 242 return; |
| 252 } | 243 } |
| 253 | 244 |
| 254 // Check cached network state for connected, connecting, or unactivated | 245 // Check cached network state for connected, connecting, or unactivated |
| 255 // networks. These states will not be affected by a recent configuration. | 246 // networks. These states will not be affected by a recent configuration. |
| (...skipping 498 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 754 } | 745 } |
| 755 | 746 |
| 756 void NetworkConnectionHandler::HandleShillDisconnectSuccess( | 747 void NetworkConnectionHandler::HandleShillDisconnectSuccess( |
| 757 const std::string& service_path, | 748 const std::string& service_path, |
| 758 const base::Closure& success_callback) { | 749 const base::Closure& success_callback) { |
| 759 NET_LOG_EVENT("Disconnect Request Sent", service_path); | 750 NET_LOG_EVENT("Disconnect Request Sent", service_path); |
| 760 if (!success_callback.is_null()) | 751 if (!success_callback.is_null()) |
| 761 success_callback.Run(); | 752 success_callback.Run(); |
| 762 } | 753 } |
| 763 | 754 |
| 764 void NetworkConnectionHandler::ConnectToBestNetworkAfterLogin() { | |
| 765 if (requested_connect_to_best_network_ || !applied_autoconnect_policy_ || | |
| 766 !certificates_loaded_) { | |
| 767 return; | |
| 768 } | |
| 769 | |
| 770 requested_connect_to_best_network_ = true; | |
| 771 network_state_handler_->ConnectToBestWifiNetwork(); | |
| 772 } | |
| 773 | |
| 774 void NetworkConnectionHandler::DisconnectIfPolicyRequires() { | |
| 775 if (applied_autoconnect_policy_ || !LoginState::Get()->IsUserLoggedIn()) | |
| 776 return; | |
| 777 | |
| 778 const base::DictionaryValue* global_network_config = | |
| 779 managed_configuration_handler_->GetGlobalConfigFromPolicy(std::string()); | |
| 780 if (!global_network_config) | |
| 781 return; | |
| 782 | |
| 783 applied_autoconnect_policy_ = true; | |
| 784 | |
| 785 bool only_policy_autoconnect = false; | |
| 786 global_network_config->GetBooleanWithoutPathExpansion( | |
| 787 ::onc::global_network_config::kAllowOnlyPolicyNetworksToAutoconnect, | |
| 788 &only_policy_autoconnect); | |
| 789 | |
| 790 if (only_policy_autoconnect) | |
| 791 DisconnectFromUnmanagedSharedWiFiNetworks(); | |
| 792 | |
| 793 ConnectToBestNetworkAfterLogin(); | |
| 794 } | |
| 795 | |
| 796 void NetworkConnectionHandler::DisconnectFromUnmanagedSharedWiFiNetworks() { | |
| 797 NET_LOG_DEBUG("DisconnectFromUnmanagedSharedWiFiNetworks", ""); | |
| 798 | |
| 799 NetworkStateHandler::NetworkStateList networks; | |
| 800 network_state_handler_->GetVisibleNetworkListByType( | |
| 801 NetworkTypePattern::Wireless(), &networks); | |
| 802 for (NetworkStateHandler::NetworkStateList::const_iterator it = | |
| 803 networks.begin(); | |
| 804 it != networks.end(); | |
| 805 ++it) { | |
| 806 const NetworkState* network = *it; | |
| 807 if (!(network->IsConnectingState() || network->IsConnectedState())) | |
| 808 break; // Connected and connecting networks are listed first. | |
| 809 | |
| 810 if (network->IsPrivate()) | |
| 811 continue; | |
| 812 | |
| 813 const bool network_is_policy_managed = | |
| 814 !network->profile_path().empty() && !network->guid().empty() && | |
| 815 managed_configuration_handler_->FindPolicyByGuidAndProfile( | |
| 816 network->guid(), network->profile_path()); | |
| 817 if (network_is_policy_managed) | |
| 818 continue; | |
| 819 | |
| 820 NET_LOG_EVENT("Disconnect Forced by Policy", network->path()); | |
| 821 CallShillDisconnect( | |
| 822 network->path(), base::Closure(), network_handler::ErrorCallback()); | |
| 823 } | |
| 824 } | |
| 825 | |
| 826 } // namespace chromeos | 755 } // namespace chromeos |
| OLD | NEW |