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