| 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/ui/webui/chromeos/login/network_state_informer.h" | 5 #include "chrome/browser/ui/webui/chromeos/login/network_state_informer.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "chrome/browser/chromeos/cros/cros_library.h" | |
| 11 #include "chrome/browser/chromeos/proxy_config_service_impl.h" | 10 #include "chrome/browser/chromeos/proxy_config_service_impl.h" |
| 12 #include "chrome/common/chrome_notification_types.h" | 11 #include "chrome/common/chrome_notification_types.h" |
| 12 #include "chromeos/network/network_state.h" |
| 13 #include "chromeos/network/network_state_handler.h" |
| 13 #include "net/proxy/proxy_config.h" | 14 #include "net/proxy/proxy_config.h" |
| 15 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 14 | 16 |
| 15 namespace { | 17 namespace { |
| 16 | 18 |
| 17 // Timeout to smooth temporary network state transitions for flaky networks. | 19 // Timeout to smooth temporary network state transitions for flaky networks. |
| 18 const int kNetworkStateCheckDelaySec = 3; | 20 const int kNetworkStateCheckDelaySec = 3; |
| 19 | 21 |
| 20 } // namespace | 22 } // namespace |
| 21 | 23 |
| 22 namespace chromeos { | 24 namespace chromeos { |
| 23 | 25 |
| 24 NetworkStateInformer::NetworkStateInformer() | 26 NetworkStateInformer::NetworkStateInformer() |
| 25 : state_(OFFLINE), | 27 : state_(OFFLINE), |
| 26 delegate_(NULL), | 28 delegate_(NULL) { |
| 27 last_network_type_(TYPE_WIFI) { | |
| 28 } | 29 } |
| 29 | 30 |
| 30 NetworkStateInformer::~NetworkStateInformer() { | 31 NetworkStateInformer::~NetworkStateInformer() { |
| 31 CrosLibrary::Get()->GetNetworkLibrary()-> | 32 NetworkStateHandler::Get()->RemoveObserver(this); |
| 32 RemoveNetworkManagerObserver(this); | |
| 33 if (NetworkPortalDetector::IsEnabledInCommandLine() && | 33 if (NetworkPortalDetector::IsEnabledInCommandLine() && |
| 34 NetworkPortalDetector::GetInstance()) { | 34 NetworkPortalDetector::GetInstance()) { |
| 35 NetworkPortalDetector::GetInstance()->RemoveObserver(this); | 35 NetworkPortalDetector::GetInstance()->RemoveObserver(this); |
| 36 } | 36 } |
| 37 } | 37 } |
| 38 | 38 |
| 39 void NetworkStateInformer::Init() { | 39 void NetworkStateInformer::Init() { |
| 40 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary(); | 40 UpdateState(); |
| 41 UpdateState(cros); | 41 NetworkStateHandler::Get()->AddObserver(this); |
| 42 cros->AddNetworkManagerObserver(this); | |
| 43 | 42 |
| 44 if (NetworkPortalDetector::IsEnabledInCommandLine() && | 43 if (NetworkPortalDetector::IsEnabledInCommandLine() && |
| 45 NetworkPortalDetector::GetInstance()) { | 44 NetworkPortalDetector::GetInstance()) { |
| 46 NetworkPortalDetector::GetInstance()->AddAndFireObserver(this); | 45 NetworkPortalDetector::GetInstance()->AddAndFireObserver(this); |
| 47 } | 46 } |
| 48 | 47 |
| 49 registrar_.Add(this, | 48 registrar_.Add(this, |
| 50 chrome::NOTIFICATION_LOGIN_PROXY_CHANGED, | 49 chrome::NOTIFICATION_LOGIN_PROXY_CHANGED, |
| 51 content::NotificationService::AllSources()); | 50 content::NotificationService::AllSources()); |
| 52 registrar_.Add(this, | 51 registrar_.Add(this, |
| 53 chrome::NOTIFICATION_SESSION_STARTED, | 52 chrome::NOTIFICATION_SESSION_STARTED, |
| 54 content::NotificationService::AllSources()); | 53 content::NotificationService::AllSources()); |
| 55 } | 54 } |
| 56 | 55 |
| 57 void NetworkStateInformer::SetDelegate(NetworkStateInformerDelegate* delegate) { | 56 void NetworkStateInformer::SetDelegate(NetworkStateInformerDelegate* delegate) { |
| 58 delegate_ = delegate; | 57 delegate_ = delegate; |
| 59 } | 58 } |
| 60 | 59 |
| 61 void NetworkStateInformer::AddObserver(NetworkStateInformerObserver* observer) { | 60 void NetworkStateInformer::AddObserver(NetworkStateInformerObserver* observer) { |
| 62 if (!observers_.HasObserver(observer)) | 61 if (!observers_.HasObserver(observer)) |
| 63 observers_.AddObserver(observer); | 62 observers_.AddObserver(observer); |
| 64 } | 63 } |
| 65 | 64 |
| 66 void NetworkStateInformer::RemoveObserver( | 65 void NetworkStateInformer::RemoveObserver( |
| 67 NetworkStateInformerObserver* observer) { | 66 NetworkStateInformerObserver* observer) { |
| 68 observers_.RemoveObserver(observer); | 67 observers_.RemoveObserver(observer); |
| 69 } | 68 } |
| 70 | 69 |
| 71 void NetworkStateInformer::OnNetworkManagerChanged(NetworkLibrary* cros) { | 70 void NetworkStateInformer::NetworkManagerChanged() { |
| 72 const Network* active_network = cros->active_network(); | 71 const NetworkState* default_network = |
| 72 NetworkStateHandler::Get()->DefaultNetwork(); |
| 73 State new_state = OFFLINE; | 73 State new_state = OFFLINE; |
| 74 std::string new_network_service_path; | 74 std::string new_network_service_path; |
| 75 if (active_network) { | 75 if (default_network) { |
| 76 new_state = GetNetworkState(active_network); | 76 new_state = GetNetworkState(default_network); |
| 77 new_network_service_path = active_network->service_path(); | 77 new_network_service_path = default_network->path(); |
| 78 } | 78 } |
| 79 if ((state_ != ONLINE && (new_state == ONLINE || new_state == CONNECTING)) || | 79 if ((state_ != ONLINE && (new_state == ONLINE || new_state == CONNECTING)) || |
| 80 (state_ == ONLINE && (new_state == ONLINE || new_state == CONNECTING) && | 80 (state_ == ONLINE && (new_state == ONLINE || new_state == CONNECTING) && |
| 81 new_network_service_path != last_online_service_path_) || | 81 new_network_service_path != last_online_service_path_) || |
| 82 (new_state == CAPTIVE_PORTAL && | 82 (new_state == CAPTIVE_PORTAL && |
| 83 new_network_service_path == last_network_service_path_)) { | 83 new_network_service_path == last_network_service_path_)) { |
| 84 last_network_service_path_ = new_network_service_path; | 84 last_network_service_path_ = new_network_service_path; |
| 85 if (new_state == ONLINE) | 85 if (new_state == ONLINE) |
| 86 last_online_service_path_ = new_network_service_path; | 86 last_online_service_path_ = new_network_service_path; |
| 87 // Transitions {OFFLINE, PORTAL} -> ONLINE and connections to | 87 // Transitions {OFFLINE, PORTAL} -> ONLINE and connections to |
| 88 // different network are processed without delay. | 88 // different network are processed without delay. |
| 89 // Transitions {OFFLINE, ONLINE} -> PORTAL in the same network are | 89 // Transitions {OFFLINE, ONLINE} -> PORTAL in the same network are |
| 90 // also processed without delay. | 90 // also processed without delay. |
| 91 UpdateStateAndNotify(); | 91 UpdateStateAndNotify(); |
| 92 } else { | 92 } else { |
| 93 check_state_.Cancel(); | 93 check_state_.Cancel(); |
| 94 check_state_.Reset( | 94 check_state_.Reset( |
| 95 base::Bind(&NetworkStateInformer::UpdateStateAndNotify, | 95 base::Bind(&NetworkStateInformer::UpdateStateAndNotify, |
| 96 base::Unretained(this))); | 96 base::Unretained(this))); |
| 97 base::MessageLoop::current()->PostDelayedTask( | 97 base::MessageLoop::current()->PostDelayedTask( |
| 98 FROM_HERE, | 98 FROM_HERE, |
| 99 check_state_.callback(), | 99 check_state_.callback(), |
| 100 base::TimeDelta::FromSeconds(kNetworkStateCheckDelaySec)); | 100 base::TimeDelta::FromSeconds(kNetworkStateCheckDelaySec)); |
| 101 } | 101 } |
| 102 } | 102 } |
| 103 | 103 |
| 104 void NetworkStateInformer::DefaultNetworkChanged(const NetworkState* network) { |
| 105 NetworkManagerChanged(); |
| 106 } |
| 107 |
| 104 void NetworkStateInformer::OnPortalDetectionCompleted( | 108 void NetworkStateInformer::OnPortalDetectionCompleted( |
| 105 const Network* network, | 109 const NetworkState* network, |
| 106 const NetworkPortalDetector::CaptivePortalState& state) { | 110 const NetworkPortalDetector::CaptivePortalState& state) { |
| 107 if (CrosLibrary::Get() && network) { | 111 if (NetworkStateHandler::IsInitialized() && |
| 108 NetworkLibrary* network_library = CrosLibrary::Get()->GetNetworkLibrary(); | 112 NetworkStateHandler::Get()->DefaultNetwork() == network) |
| 109 if (network_library && network_library->active_network() == network) | 113 NetworkManagerChanged(); |
| 110 OnNetworkManagerChanged(network_library); | |
| 111 } | |
| 112 } | 114 } |
| 113 | 115 |
| 114 void NetworkStateInformer::Observe( | 116 void NetworkStateInformer::Observe( |
| 115 int type, | 117 int type, |
| 116 const content::NotificationSource& source, | 118 const content::NotificationSource& source, |
| 117 const content::NotificationDetails& details) { | 119 const content::NotificationDetails& details) { |
| 118 if (type == chrome::NOTIFICATION_SESSION_STARTED) | 120 if (type == chrome::NOTIFICATION_SESSION_STARTED) |
| 119 registrar_.RemoveAll(); | 121 registrar_.RemoveAll(); |
| 120 else if (type == chrome::NOTIFICATION_LOGIN_PROXY_CHANGED) | 122 else if (type == chrome::NOTIFICATION_LOGIN_PROXY_CHANGED) |
| 121 SendStateToObservers(ErrorScreenActor::ERROR_REASON_PROXY_CONFIG_CHANGED); | 123 SendStateToObservers(ErrorScreenActor::ERROR_REASON_PROXY_CONFIG_CHANGED); |
| 122 else | 124 else |
| 123 NOTREACHED() << "Unknown notification: " << type; | 125 NOTREACHED() << "Unknown notification: " << type; |
| 124 } | 126 } |
| 125 | 127 |
| 126 void NetworkStateInformer::OnPortalDetected() { | 128 void NetworkStateInformer::OnPortalDetected() { |
| 127 SendStateToObservers(ErrorScreenActor::ERROR_REASON_PORTAL_DETECTED); | 129 SendStateToObservers(ErrorScreenActor::ERROR_REASON_PORTAL_DETECTED); |
| 128 } | 130 } |
| 129 | 131 |
| 130 bool NetworkStateInformer::UpdateState(NetworkLibrary* cros) { | 132 bool NetworkStateInformer::UpdateState() { |
| 131 State new_state = OFFLINE; | 133 State new_state = OFFLINE; |
| 132 | 134 |
| 133 const Network* active_network = cros->active_network(); | 135 const NetworkState* default_network = |
| 134 if (active_network) { | 136 NetworkStateHandler::Get()->DefaultNetwork(); |
| 135 new_state = GetNetworkState(active_network); | 137 if (default_network) { |
| 136 last_network_service_path_ = active_network->service_path(); | 138 new_state = GetNetworkState(default_network); |
| 137 last_network_type_ = active_network->type(); | 139 last_network_service_path_ = default_network->path(); |
| 140 last_network_type_ = default_network->type(); |
| 138 } | 141 } |
| 139 | 142 |
| 140 bool updated = (new_state != state_) || | 143 bool updated = (new_state != state_) || |
| 141 (new_state != OFFLINE && | 144 (new_state != OFFLINE && |
| 142 last_network_service_path_ != last_connected_service_path_); | 145 last_network_service_path_ != last_connected_service_path_); |
| 143 state_ = new_state; | 146 state_ = new_state; |
| 144 if (state_ != OFFLINE) | 147 if (state_ != OFFLINE) |
| 145 last_connected_service_path_ = last_network_service_path_; | 148 last_connected_service_path_ = last_network_service_path_; |
| 146 | 149 |
| 147 if (updated && state_ == ONLINE && delegate_) | 150 if (updated && state_ == ONLINE && delegate_) |
| 148 delegate_->OnNetworkReady(); | 151 delegate_->OnNetworkReady(); |
| 149 | 152 |
| 150 return updated; | 153 return updated; |
| 151 } | 154 } |
| 152 | 155 |
| 153 void NetworkStateInformer::UpdateStateAndNotify() { | 156 void NetworkStateInformer::UpdateStateAndNotify() { |
| 154 // Cancel pending update request if any. | 157 // Cancel pending update request if any. |
| 155 check_state_.Cancel(); | 158 check_state_.Cancel(); |
| 156 | 159 |
| 157 if (UpdateState(CrosLibrary::Get()->GetNetworkLibrary())) | 160 if (UpdateState()) |
| 158 SendStateToObservers(ErrorScreenActor::ERROR_REASON_NETWORK_STATE_CHANGED); | 161 SendStateToObservers(ErrorScreenActor::ERROR_REASON_NETWORK_STATE_CHANGED); |
| 159 else | 162 else |
| 160 SendStateToObservers(ErrorScreenActor::ERROR_REASON_UPDATE); | 163 SendStateToObservers(ErrorScreenActor::ERROR_REASON_UPDATE); |
| 161 } | 164 } |
| 162 | 165 |
| 163 void NetworkStateInformer::SendStateToObservers( | 166 void NetworkStateInformer::SendStateToObservers( |
| 164 ErrorScreenActor::ErrorReason reason) { | 167 ErrorScreenActor::ErrorReason reason) { |
| 165 FOR_EACH_OBSERVER(NetworkStateInformerObserver, observers_, | 168 FOR_EACH_OBSERVER(NetworkStateInformerObserver, observers_, |
| 166 UpdateState(state_, reason)); | 169 UpdateState(state_, reason)); |
| 167 } | 170 } |
| 168 | 171 |
| 169 NetworkStateInformer::State NetworkStateInformer::GetNetworkState( | 172 NetworkStateInformer::State NetworkStateInformer::GetNetworkState( |
| 170 const Network* network) { | 173 const NetworkState* network) { |
| 171 DCHECK(network); | 174 DCHECK(network); |
| 172 if (NetworkPortalDetector::IsEnabledInCommandLine() && | 175 if (NetworkPortalDetector::IsEnabledInCommandLine() && |
| 173 NetworkPortalDetector::GetInstance()) { | 176 NetworkPortalDetector::GetInstance()) { |
| 174 NetworkPortalDetector::CaptivePortalState state = | 177 NetworkPortalDetector::CaptivePortalState state = |
| 175 NetworkPortalDetector::GetInstance()->GetCaptivePortalState(network); | 178 NetworkPortalDetector::GetInstance()->GetCaptivePortalState(network); |
| 176 NetworkPortalDetector::CaptivePortalStatus status = state.status; | 179 NetworkPortalDetector::CaptivePortalStatus status = state.status; |
| 177 if (status == NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_UNKNOWN && | 180 if (status == NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_UNKNOWN && |
| 178 network->connecting()) { | 181 NetworkState::StateIsConnecting(network->connection_state())) { |
| 179 return CONNECTING; | 182 return CONNECTING; |
| 180 } | 183 } |
| 181 // For proxy-less networks rely on shill's online state if | 184 // For proxy-less networks rely on shill's online state if |
| 182 // NetworkPortalDetector's state of current network is unknown. | 185 // NetworkPortalDetector's state of current network is unknown. |
| 183 if (status == NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE || | 186 if (status == NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE || |
| 184 (status == NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_UNKNOWN && | 187 (status == NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_UNKNOWN && |
| 185 !IsProxyConfigured(network) && network->online())) { | 188 !IsProxyConfigured(network) && |
| 189 network->connection_state() == flimflam::kStateOnline)) { |
| 186 return ONLINE; | 190 return ONLINE; |
| 187 } | 191 } |
| 188 if (status == | 192 if (status == |
| 189 NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PROXY_AUTH_REQUIRED && | 193 NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PROXY_AUTH_REQUIRED && |
| 190 IsProxyConfigured(network)) { | 194 IsProxyConfigured(network)) { |
| 191 return PROXY_AUTH_REQUIRED; | 195 return PROXY_AUTH_REQUIRED; |
| 192 } | 196 } |
| 193 if (status == NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL || | 197 if (status == NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL || |
| 194 (status == NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_UNKNOWN && | 198 (status == NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_UNKNOWN && |
| 195 network->restricted_pool())) | 199 network->connection_state() == flimflam::kStatePortal)) |
| 196 return CAPTIVE_PORTAL; | 200 return CAPTIVE_PORTAL; |
| 197 } else { | 201 } else { |
| 198 if (network->connecting()) | 202 if (NetworkState::StateIsConnecting(network->connection_state())) |
| 199 return CONNECTING; | 203 return CONNECTING; |
| 200 if (network->online()) | 204 if (network->connection_state() == flimflam::kStateOnline) |
| 201 return ONLINE; | 205 return ONLINE; |
| 202 if (network->restricted_pool()) | 206 if (network->connection_state() == flimflam::kStatePortal) |
| 203 return CAPTIVE_PORTAL; | 207 return CAPTIVE_PORTAL; |
| 204 } | 208 } |
| 205 return OFFLINE; | 209 return OFFLINE; |
| 206 } | 210 } |
| 207 | 211 |
| 208 bool NetworkStateInformer::IsProxyConfigured(const Network* network) { | 212 bool NetworkStateInformer::IsProxyConfigured(const NetworkState* network) { |
| 209 DCHECK(network); | 213 DCHECK(network); |
| 210 ProxyStateMap::iterator it = proxy_state_map_.find(network->unique_id()); | 214 |
| 215 ProxyStateMap::iterator it = proxy_state_map_.find(network->guid()); |
| 211 if (it != proxy_state_map_.end() && | 216 if (it != proxy_state_map_.end() && |
| 212 it->second.proxy_config == network->proxy_config()) { | 217 it->second.proxy_config == network->proxy_config()) { |
| 213 return it->second.configured; | 218 return it->second.configured; |
| 214 } | 219 } |
| 215 net::ProxyConfig proxy_config; | 220 net::ProxyConfig proxy_config; |
| 216 if (!ProxyConfigServiceImpl::ParseProxyConfig(network, &proxy_config)) | 221 if (!ProxyConfigServiceImpl::ParseProxyConfig(network->proxy_config(), |
| 222 &proxy_config)) |
| 217 return false; | 223 return false; |
| 218 bool configured = !proxy_config.proxy_rules().empty(); | 224 bool configured = !proxy_config.proxy_rules().empty(); |
| 219 proxy_state_map_[network->unique_id()] = | 225 proxy_state_map_[network->guid()] = |
| 220 ProxyState(network->proxy_config(), configured); | 226 ProxyState(network->proxy_config(), configured); |
| 221 return configured; | 227 return configured; |
| 222 } | 228 } |
| 223 | 229 |
| 224 } // namespace chromeos | 230 } // namespace chromeos |
| OLD | NEW |