| 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 "chrome/browser/chromeos/net/network_portal_detector_impl.h" | 5 #include "chrome/browser/chromeos/net/network_portal_detector_impl.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| 11 #include "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
| 12 #include "chrome/browser/chromeos/cros/cros_library.h" | 12 #include "chrome/browser/chromeos/cros/cros_library.h" |
| 13 #include "chrome/common/chrome_notification_types.h" | 13 #include "chrome/common/chrome_notification_types.h" |
| 14 #include "chrome/common/chrome_switches.h" | 14 #include "chrome/common/chrome_switches.h" |
| 15 #include "chromeos/network/network_state.h" |
| 16 #include "chromeos/network/network_state_handler.h" |
| 15 #include "content/public/browser/notification_service.h" | 17 #include "content/public/browser/notification_service.h" |
| 16 #include "grit/generated_resources.h" | 18 #include "grit/generated_resources.h" |
| 17 #include "net/http/http_status_code.h" | 19 #include "net/http/http_status_code.h" |
| 20 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 18 #include "ui/base/l10n/l10n_util.h" | 21 #include "ui/base/l10n/l10n_util.h" |
| 19 | 22 |
| 20 using captive_portal::CaptivePortalDetector; | 23 using captive_portal::CaptivePortalDetector; |
| 21 | 24 |
| 22 namespace chromeos { | 25 namespace chromeos { |
| 23 | 26 |
| 24 namespace { | 27 namespace { |
| 25 | 28 |
| 26 // Maximum number of portal detections for the same active network | 29 // Maximum number of portal detections for the same default network |
| 27 // after network change. | 30 // after network change. |
| 28 const int kMaxRequestAttempts = 3; | 31 const int kMaxRequestAttempts = 3; |
| 29 | 32 |
| 30 // Minimum timeout between consecutive portal checks for the same | 33 // Minimum timeout between consecutive portal checks for the same |
| 31 // network. | 34 // network. |
| 32 const int kMinTimeBetweenAttemptsSec = 3; | 35 const int kMinTimeBetweenAttemptsSec = 3; |
| 33 | 36 |
| 34 // Timeout for a portal check. | 37 // Timeout for a portal check. |
| 35 const int kRequestTimeoutSec = 5; | 38 const int kRequestTimeoutSec = 5; |
| 36 | 39 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 58 case NetworkPortalDetectorImpl::CAPTIVE_PORTAL_STATUS_PROXY_AUTH_REQUIRED: | 61 case NetworkPortalDetectorImpl::CAPTIVE_PORTAL_STATUS_PROXY_AUTH_REQUIRED: |
| 59 return l10n_util::GetStringUTF8( | 62 return l10n_util::GetStringUTF8( |
| 60 IDS_CHROMEOS_CAPTIVE_PORTAL_STATUS_PROXY_AUTH_REQUIRED); | 63 IDS_CHROMEOS_CAPTIVE_PORTAL_STATUS_PROXY_AUTH_REQUIRED); |
| 61 case NetworkPortalDetectorImpl::CAPTIVE_PORTAL_STATUS_COUNT: | 64 case NetworkPortalDetectorImpl::CAPTIVE_PORTAL_STATUS_COUNT: |
| 62 NOTREACHED(); | 65 NOTREACHED(); |
| 63 } | 66 } |
| 64 return l10n_util::GetStringUTF8( | 67 return l10n_util::GetStringUTF8( |
| 65 IDS_CHROMEOS_CAPTIVE_PORTAL_STATUS_UNRECOGNIZED); | 68 IDS_CHROMEOS_CAPTIVE_PORTAL_STATUS_UNRECOGNIZED); |
| 66 } | 69 } |
| 67 | 70 |
| 68 NetworkLibrary* GetNetworkLibrary() { | |
| 69 CHECK(CrosLibrary::Get()); | |
| 70 return CrosLibrary::Get()->GetNetworkLibrary(); | |
| 71 } | |
| 72 | |
| 73 const Network* GetActiveNetwork() { | |
| 74 NetworkLibrary* cros = GetNetworkLibrary(); | |
| 75 if (!cros) | |
| 76 return NULL; | |
| 77 return cros->active_network(); | |
| 78 } | |
| 79 | |
| 80 const Network* FindNetworkByPath(const std::string& service_path) { | |
| 81 NetworkLibrary* cros = GetNetworkLibrary(); | |
| 82 if (!cros) | |
| 83 return NULL; | |
| 84 return cros->FindNetworkByPath(service_path); | |
| 85 } | |
| 86 | |
| 87 } // namespace | 71 } // namespace |
| 88 | 72 |
| 89 NetworkPortalDetectorImpl::NetworkPortalDetectorImpl( | 73 NetworkPortalDetectorImpl::NetworkPortalDetectorImpl( |
| 90 const scoped_refptr<net::URLRequestContextGetter>& request_context) | 74 const scoped_refptr<net::URLRequestContextGetter>& request_context) |
| 91 : active_connection_state_(STATE_UNKNOWN), | 75 : test_url_(CaptivePortalDetector::kDefaultURL), |
| 92 test_url_(CaptivePortalDetector::kDefaultURL), | |
| 93 enabled_(false), | 76 enabled_(false), |
| 94 weak_ptr_factory_(this), | 77 weak_ptr_factory_(this), |
| 95 attempt_count_(0), | 78 attempt_count_(0), |
| 96 lazy_detection_enabled_(false), | 79 lazy_detection_enabled_(false), |
| 97 lazy_check_interval_(base::TimeDelta::FromSeconds(kLazyCheckIntervalSec)), | 80 lazy_check_interval_(base::TimeDelta::FromSeconds(kLazyCheckIntervalSec)), |
| 98 min_time_between_attempts_( | 81 min_time_between_attempts_( |
| 99 base::TimeDelta::FromSeconds(kMinTimeBetweenAttemptsSec)), | 82 base::TimeDelta::FromSeconds(kMinTimeBetweenAttemptsSec)), |
| 100 request_timeout_(base::TimeDelta::FromSeconds(kRequestTimeoutSec)) { | 83 request_timeout_(base::TimeDelta::FromSeconds(kRequestTimeoutSec)) { |
| 101 captive_portal_detector_.reset(new CaptivePortalDetector(request_context)); | 84 captive_portal_detector_.reset(new CaptivePortalDetector(request_context)); |
| 102 | 85 |
| 103 registrar_.Add(this, | 86 registrar_.Add(this, |
| 104 chrome::NOTIFICATION_LOGIN_PROXY_CHANGED, | 87 chrome::NOTIFICATION_LOGIN_PROXY_CHANGED, |
| 105 content::NotificationService::AllSources()); | 88 content::NotificationService::AllSources()); |
| 106 registrar_.Add(this, | 89 registrar_.Add(this, |
| 107 chrome::NOTIFICATION_AUTH_SUPPLIED, | 90 chrome::NOTIFICATION_AUTH_SUPPLIED, |
| 108 content::NotificationService::AllSources()); | 91 content::NotificationService::AllSources()); |
| 109 registrar_.Add(this, | 92 registrar_.Add(this, |
| 110 chrome::NOTIFICATION_AUTH_CANCELLED, | 93 chrome::NOTIFICATION_AUTH_CANCELLED, |
| 111 content::NotificationService::AllSources()); | 94 content::NotificationService::AllSources()); |
| 112 } | 95 } |
| 113 | 96 |
| 114 NetworkPortalDetectorImpl::~NetworkPortalDetectorImpl() { | 97 NetworkPortalDetectorImpl::~NetworkPortalDetectorImpl() { |
| 115 } | 98 } |
| 116 | 99 |
| 117 void NetworkPortalDetectorImpl::Init() { | 100 void NetworkPortalDetectorImpl::Init() { |
| 118 DCHECK(CalledOnValidThread()); | 101 DCHECK(CalledOnValidThread()); |
| 119 | 102 |
| 120 state_ = STATE_IDLE; | 103 state_ = STATE_IDLE; |
| 121 chromeos::NetworkLibrary* network_library = GetNetworkLibrary(); | 104 DCHECK(NetworkStateHandler::Get()); |
| 122 DCHECK(network_library); | 105 NetworkStateHandler::Get()->AddObserver(this); |
| 123 network_library->AddNetworkManagerObserver(this); | |
| 124 network_library->RemoveObserverForAllNetworks(this); | |
| 125 } | 106 } |
| 126 | 107 |
| 127 void NetworkPortalDetectorImpl::Shutdown() { | 108 void NetworkPortalDetectorImpl::Shutdown() { |
| 128 DCHECK(CalledOnValidThread()); | 109 DCHECK(CalledOnValidThread()); |
| 129 | 110 |
| 130 detection_task_.Cancel(); | 111 detection_task_.Cancel(); |
| 131 detection_timeout_.Cancel(); | 112 detection_timeout_.Cancel(); |
| 132 | 113 |
| 133 captive_portal_detector_->Cancel(); | 114 captive_portal_detector_->Cancel(); |
| 134 captive_portal_detector_.reset(); | 115 captive_portal_detector_.reset(); |
| 135 observers_.Clear(); | 116 observers_.Clear(); |
| 136 chromeos::NetworkLibrary* network_library = GetNetworkLibrary(); | 117 if (NetworkStateHandler::Get()) |
| 137 if (network_library) | 118 NetworkStateHandler::Get()->RemoveObserver(this); |
| 138 network_library->RemoveNetworkManagerObserver(this); | |
| 139 } | 119 } |
| 140 | 120 |
| 141 void NetworkPortalDetectorImpl::AddObserver(Observer* observer) { | 121 void NetworkPortalDetectorImpl::AddObserver(Observer* observer) { |
| 142 DCHECK(CalledOnValidThread()); | 122 DCHECK(CalledOnValidThread()); |
| 143 if (!observer || observers_.HasObserver(observer)) | 123 if (!observer || observers_.HasObserver(observer)) |
| 144 return; | 124 return; |
| 145 observers_.AddObserver(observer); | 125 observers_.AddObserver(observer); |
| 146 } | 126 } |
| 147 | 127 |
| 148 void NetworkPortalDetectorImpl::AddAndFireObserver(Observer* observer) { | 128 void NetworkPortalDetectorImpl::AddAndFireObserver(Observer* observer) { |
| 149 DCHECK(CalledOnValidThread()); | 129 DCHECK(CalledOnValidThread()); |
| 150 if (!observer) | 130 if (!observer) |
| 151 return; | 131 return; |
| 152 AddObserver(observer); | 132 AddObserver(observer); |
| 153 const Network* network = GetActiveNetwork(); | 133 const NetworkState* network = NetworkStateHandler::Get()->DefaultNetwork(); |
| 154 observer->OnPortalDetectionCompleted(network, GetCaptivePortalState(network)); | 134 observer->OnPortalDetectionCompleted(network, GetCaptivePortalState(network)); |
| 155 } | 135 } |
| 156 | 136 |
| 157 void NetworkPortalDetectorImpl::RemoveObserver(Observer* observer) { | 137 void NetworkPortalDetectorImpl::RemoveObserver(Observer* observer) { |
| 158 DCHECK(CalledOnValidThread()); | 138 DCHECK(CalledOnValidThread()); |
| 159 if (observer) | 139 if (observer) |
| 160 observers_.RemoveObserver(observer); | 140 observers_.RemoveObserver(observer); |
| 161 } | 141 } |
| 162 | 142 |
| 163 bool NetworkPortalDetectorImpl::IsEnabled() { | 143 bool NetworkPortalDetectorImpl::IsEnabled() { |
| 164 return enabled_; | 144 return enabled_; |
| 165 } | 145 } |
| 166 | 146 |
| 167 void NetworkPortalDetectorImpl::Enable(bool start_detection) { | 147 void NetworkPortalDetectorImpl::Enable(bool start_detection) { |
| 168 DCHECK(CalledOnValidThread()); | 148 DCHECK(CalledOnValidThread()); |
| 169 if (enabled_) | 149 if (enabled_) |
| 170 return; | 150 return; |
| 171 enabled_ = true; | 151 enabled_ = true; |
| 172 DCHECK(!IsPortalCheckPending()); | 152 DCHECK(!IsPortalCheckPending()); |
| 173 DCHECK(!IsCheckingForPortal()); | 153 DCHECK(!IsCheckingForPortal()); |
| 174 DCHECK(!lazy_detection_enabled_); | 154 DCHECK(!lazy_detection_enabled_); |
| 175 if (!start_detection) | 155 if (!start_detection) |
| 176 return; | 156 return; |
| 177 state_ = STATE_IDLE; | 157 state_ = STATE_IDLE; |
| 178 attempt_count_ = 0; | 158 attempt_count_ = 0; |
| 179 const Network* active_network = GetActiveNetwork(); | 159 const NetworkState* default_network = |
| 180 if (!active_network) | 160 NetworkStateHandler::Get()->DefaultNetwork(); |
| 161 if (!default_network) |
| 181 return; | 162 return; |
| 182 portal_state_map_.erase(active_network->service_path()); | 163 portal_state_map_.erase(default_network->path()); |
| 183 DetectCaptivePortal(base::TimeDelta()); | 164 DetectCaptivePortal(base::TimeDelta()); |
| 184 } | 165 } |
| 185 | 166 |
| 186 NetworkPortalDetectorImpl::CaptivePortalState | 167 NetworkPortalDetectorImpl::CaptivePortalState |
| 187 NetworkPortalDetectorImpl::GetCaptivePortalState(const Network* network) { | 168 NetworkPortalDetectorImpl::GetCaptivePortalState(const NetworkState* network) { |
| 188 DCHECK(CalledOnValidThread()); | 169 DCHECK(CalledOnValidThread()); |
| 189 if (!network) | 170 if (!network) |
| 190 return CaptivePortalState(); | 171 return CaptivePortalState(); |
| 191 CaptivePortalStateMap::const_iterator it = | 172 CaptivePortalStateMap::const_iterator it = |
| 192 portal_state_map_.find(network->service_path()); | 173 portal_state_map_.find(network->path()); |
| 193 if (it == portal_state_map_.end()) | 174 if (it == portal_state_map_.end()) |
| 194 return CaptivePortalState(); | 175 return CaptivePortalState(); |
| 195 return it->second; | 176 return it->second; |
| 196 } | 177 } |
| 197 | 178 |
| 198 void NetworkPortalDetectorImpl::EnableLazyDetection() { | 179 void NetworkPortalDetectorImpl::EnableLazyDetection() { |
| 199 if (lazy_detection_enabled_) | 180 if (lazy_detection_enabled_) |
| 200 return; | 181 return; |
| 201 VLOG(1) << "Lazy detection mode enabled."; | 182 VLOG(1) << "Lazy detection mode enabled."; |
| 202 lazy_detection_enabled_ = true; | 183 lazy_detection_enabled_ = true; |
| 203 if (!IsPortalCheckPending() && !IsCheckingForPortal()) | 184 if (!IsPortalCheckPending() && !IsCheckingForPortal()) |
| 204 DetectCaptivePortal(base::TimeDelta()); | 185 DetectCaptivePortal(base::TimeDelta()); |
| 205 } | 186 } |
| 206 | 187 |
| 207 void NetworkPortalDetectorImpl::DisableLazyDetection() { | 188 void NetworkPortalDetectorImpl::DisableLazyDetection() { |
| 208 if (!lazy_detection_enabled_) | 189 if (!lazy_detection_enabled_) |
| 209 return; | 190 return; |
| 210 VLOG(1) << "Lazy detection mode disabled."; | 191 VLOG(1) << "Lazy detection mode disabled."; |
| 211 lazy_detection_enabled_ = false; | 192 lazy_detection_enabled_ = false; |
| 212 } | 193 } |
| 213 | 194 |
| 214 void NetworkPortalDetectorImpl::OnNetworkManagerChanged(NetworkLibrary* cros) { | 195 void NetworkPortalDetectorImpl::NetworkManagerChanged() { |
| 215 DCHECK(CalledOnValidThread()); | 196 DCHECK(CalledOnValidThread()); |
| 216 CHECK(cros); | 197 const NetworkState* default_network = |
| 217 const Network* active_network = cros->active_network(); | 198 NetworkStateHandler::Get()->DefaultNetwork(); |
| 218 if (!active_network) | 199 if (!default_network) |
| 219 return; | 200 return; |
| 220 | 201 |
| 221 active_network_id_ = active_network->unique_id(); | 202 default_network_id_ = default_network->guid(); |
| 222 | 203 |
| 223 bool network_changed = | 204 bool network_changed = (default_service_path_ != default_network->path()); |
| 224 (active_service_path_ != active_network->service_path()); | 205 if (network_changed) |
| 225 if (network_changed) { | 206 default_service_path_ = default_network->path(); |
| 226 if (!active_service_path_.empty()) | |
| 227 cros->RemoveNetworkObserver(active_service_path_, this); | |
| 228 active_service_path_ = active_network->service_path(); | |
| 229 cros->AddNetworkObserver(active_service_path_, this); | |
| 230 } | |
| 231 | 207 |
| 232 bool connection_state_changed = | 208 bool connection_state_changed = (default_connection_state_ != |
| 233 (active_connection_state_ != active_network->connection_state()); | 209 default_network->connection_state()); |
| 234 active_connection_state_ = active_network->connection_state(); | 210 default_connection_state_ = default_network->connection_state(); |
| 235 | 211 |
| 236 if (network_changed || connection_state_changed) { | 212 if (network_changed || connection_state_changed) { |
| 237 attempt_count_ = 0; | 213 attempt_count_ = 0; |
| 238 CancelPortalDetection(); | 214 CancelPortalDetection(); |
| 239 } | 215 } |
| 240 | 216 |
| 241 if (!IsCheckingForPortal() && !IsPortalCheckPending() && | 217 if (!IsCheckingForPortal() && !IsPortalCheckPending() && |
| 242 Network::IsConnectedState(active_connection_state_) && | 218 NetworkState::StateIsConnected(default_connection_state_) && |
| 243 (attempt_count_ < kMaxRequestAttempts || lazy_detection_enabled_)) { | 219 (attempt_count_ < kMaxRequestAttempts || lazy_detection_enabled_)) { |
| 244 DCHECK(active_network); | 220 DCHECK(default_network); |
| 245 | 221 |
| 246 // Initiate Captive Portal detection if network's captive | 222 // Initiate Captive Portal detection if network's captive |
| 247 // portal state is unknown (e.g. for freshly created networks), | 223 // portal state is unknown (e.g. for freshly created networks), |
| 248 // offline or if network connection state was changed. | 224 // offline or if network connection state was changed. |
| 249 CaptivePortalState state = GetCaptivePortalState(active_network); | 225 CaptivePortalState state = GetCaptivePortalState(default_network); |
| 250 if (state.status == CAPTIVE_PORTAL_STATUS_UNKNOWN || | 226 if (state.status == CAPTIVE_PORTAL_STATUS_UNKNOWN || |
| 251 state.status == CAPTIVE_PORTAL_STATUS_OFFLINE || | 227 state.status == CAPTIVE_PORTAL_STATUS_OFFLINE || |
| 252 (!network_changed && connection_state_changed)) { | 228 (!network_changed && connection_state_changed)) { |
| 253 DetectCaptivePortal(base::TimeDelta()); | 229 DetectCaptivePortal(base::TimeDelta()); |
| 254 } | 230 } |
| 255 } | 231 } |
| 256 } | 232 } |
| 257 | 233 |
| 258 void NetworkPortalDetectorImpl::OnNetworkChanged( | 234 void NetworkPortalDetectorImpl::DefaultNetworkChanged( |
| 259 chromeos::NetworkLibrary* cros, | 235 const NetworkState* network) { |
| 260 const chromeos::Network* network) { | 236 NetworkManagerChanged(); |
| 261 DCHECK(CalledOnValidThread()); | |
| 262 OnNetworkManagerChanged(cros); | |
| 263 } | 237 } |
| 264 | 238 |
| 265 void NetworkPortalDetectorImpl::DetectCaptivePortal( | 239 void NetworkPortalDetectorImpl::DetectCaptivePortal( |
| 266 const base::TimeDelta& delay) { | 240 const base::TimeDelta& delay) { |
| 267 DCHECK(!IsPortalCheckPending()); | 241 DCHECK(!IsPortalCheckPending()); |
| 268 DCHECK(!IsCheckingForPortal()); | 242 DCHECK(!IsCheckingForPortal()); |
| 269 DCHECK(attempt_count_ < kMaxRequestAttempts || lazy_detection_enabled_); | 243 DCHECK(attempt_count_ < kMaxRequestAttempts || lazy_detection_enabled_); |
| 270 | 244 |
| 271 if (!IsEnabled()) | 245 if (!IsEnabled()) |
| 272 return; | 246 return; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 304 | 278 |
| 305 void NetworkPortalDetectorImpl::DetectCaptivePortalTask() { | 279 void NetworkPortalDetectorImpl::DetectCaptivePortalTask() { |
| 306 DCHECK(IsPortalCheckPending()); | 280 DCHECK(IsPortalCheckPending()); |
| 307 | 281 |
| 308 state_ = STATE_CHECKING_FOR_PORTAL; | 282 state_ = STATE_CHECKING_FOR_PORTAL; |
| 309 attempt_start_time_ = GetCurrentTimeTicks(); | 283 attempt_start_time_ = GetCurrentTimeTicks(); |
| 310 | 284 |
| 311 if (attempt_count_ < kMaxRequestAttempts) { | 285 if (attempt_count_ < kMaxRequestAttempts) { |
| 312 ++attempt_count_; | 286 ++attempt_count_; |
| 313 VLOG(1) << "Portal detection started: " | 287 VLOG(1) << "Portal detection started: " |
| 314 << "network=" << active_network_id_ << ", " | 288 << "network=" << default_network_id_ << ", " |
| 315 << "attempt=" << attempt_count_ << " of " << kMaxRequestAttempts; | 289 << "attempt=" << attempt_count_ << " of " << kMaxRequestAttempts; |
| 316 } else { | 290 } else { |
| 317 DCHECK(lazy_detection_enabled_); | 291 DCHECK(lazy_detection_enabled_); |
| 318 VLOG(1) << "Lazy portal detection attempt started"; | 292 VLOG(1) << "Lazy portal detection attempt started"; |
| 319 } | 293 } |
| 320 | 294 |
| 321 captive_portal_detector_->DetectCaptivePortal( | 295 captive_portal_detector_->DetectCaptivePortal( |
| 322 test_url_, | 296 test_url_, |
| 323 base::Bind(&NetworkPortalDetectorImpl::OnPortalDetectionCompleted, | 297 base::Bind(&NetworkPortalDetectorImpl::OnPortalDetectionCompleted, |
| 324 weak_ptr_factory_.GetWeakPtr())); | 298 weak_ptr_factory_.GetWeakPtr())); |
| 325 detection_timeout_.Reset( | 299 detection_timeout_.Reset( |
| 326 base::Bind(&NetworkPortalDetectorImpl::PortalDetectionTimeout, | 300 base::Bind(&NetworkPortalDetectorImpl::PortalDetectionTimeout, |
| 327 weak_ptr_factory_.GetWeakPtr())); | 301 weak_ptr_factory_.GetWeakPtr())); |
| 328 MessageLoop::current()->PostDelayedTask(FROM_HERE, | 302 MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 329 detection_timeout_.callback(), | 303 detection_timeout_.callback(), |
| 330 request_timeout_); | 304 request_timeout_); |
| 331 } | 305 } |
| 332 | 306 |
| 333 void NetworkPortalDetectorImpl::PortalDetectionTimeout() { | 307 void NetworkPortalDetectorImpl::PortalDetectionTimeout() { |
| 334 DCHECK(CalledOnValidThread()); | 308 DCHECK(CalledOnValidThread()); |
| 335 DCHECK(IsCheckingForPortal()); | 309 DCHECK(IsCheckingForPortal()); |
| 336 | 310 |
| 337 VLOG(1) << "Portal detection timeout: network=" << active_network_id_; | 311 VLOG(1) << "Portal detection timeout: network=" << default_network_id_; |
| 338 | 312 |
| 339 captive_portal_detector_->Cancel(); | 313 captive_portal_detector_->Cancel(); |
| 340 CaptivePortalDetector::Results results; | 314 CaptivePortalDetector::Results results; |
| 341 results.result = captive_portal::RESULT_NO_RESPONSE; | 315 results.result = captive_portal::RESULT_NO_RESPONSE; |
| 342 OnPortalDetectionCompleted(results); | 316 OnPortalDetectionCompleted(results); |
| 343 } | 317 } |
| 344 | 318 |
| 345 void NetworkPortalDetectorImpl::CancelPortalDetection() { | 319 void NetworkPortalDetectorImpl::CancelPortalDetection() { |
| 346 if (IsPortalCheckPending()) | 320 if (IsPortalCheckPending()) |
| 347 detection_task_.Cancel(); | 321 detection_task_.Cancel(); |
| 348 else if (IsCheckingForPortal()) | 322 else if (IsCheckingForPortal()) |
| 349 captive_portal_detector_->Cancel(); | 323 captive_portal_detector_->Cancel(); |
| 350 detection_timeout_.Cancel(); | 324 detection_timeout_.Cancel(); |
| 351 state_ = STATE_IDLE; | 325 state_ = STATE_IDLE; |
| 352 } | 326 } |
| 353 | 327 |
| 354 void NetworkPortalDetectorImpl::OnPortalDetectionCompleted( | 328 void NetworkPortalDetectorImpl::OnPortalDetectionCompleted( |
| 355 const CaptivePortalDetector::Results& results) { | 329 const CaptivePortalDetector::Results& results) { |
| 356 DCHECK(CalledOnValidThread()); | 330 DCHECK(CalledOnValidThread()); |
| 357 DCHECK(IsCheckingForPortal()); | 331 DCHECK(IsCheckingForPortal()); |
| 358 | 332 |
| 359 VLOG(1) << "Portal detection completed: " | 333 VLOG(1) << "Portal detection completed: " |
| 360 << "network=" << active_network_id_ << ", " | 334 << "network=" << default_network_id_ << ", " |
| 361 << "result=" << CaptivePortalDetector::CaptivePortalResultToString( | 335 << "result=" << CaptivePortalDetector::CaptivePortalResultToString( |
| 362 results.result) << ", " | 336 results.result) << ", " |
| 363 << "response_code=" << results.response_code; | 337 << "response_code=" << results.response_code; |
| 364 | 338 |
| 365 state_ = STATE_IDLE; | 339 state_ = STATE_IDLE; |
| 366 detection_timeout_.Cancel(); | 340 detection_timeout_.Cancel(); |
| 367 | 341 |
| 368 NetworkLibrary* cros = GetNetworkLibrary(); | 342 const NetworkState* default_network = |
| 369 const Network* active_network = cros->active_network(); | 343 NetworkStateHandler::Get()->DefaultNetwork(); |
| 370 if (!active_network) { | 344 if (!default_network) { |
| 371 TryLazyDetection(); | 345 TryLazyDetection(); |
| 372 return; | 346 return; |
| 373 } | 347 } |
| 374 | 348 |
| 375 CaptivePortalState state; | 349 CaptivePortalState state; |
| 376 state.response_code = results.response_code; | 350 state.response_code = results.response_code; |
| 377 switch (results.result) { | 351 switch (results.result) { |
| 378 case captive_portal::RESULT_NO_RESPONSE: | 352 case captive_portal::RESULT_NO_RESPONSE: |
| 379 if (attempt_count_ >= kMaxRequestAttempts) { | 353 if (attempt_count_ >= kMaxRequestAttempts) { |
| 380 if (state.response_code == net::HTTP_PROXY_AUTHENTICATION_REQUIRED) { | 354 if (state.response_code == net::HTTP_PROXY_AUTHENTICATION_REQUIRED) { |
| 381 state.status = CAPTIVE_PORTAL_STATUS_PROXY_AUTH_REQUIRED; | 355 state.status = CAPTIVE_PORTAL_STATUS_PROXY_AUTH_REQUIRED; |
| 382 } else if (active_network->restricted_pool()) { | 356 } else if (default_network->connection_state() == |
| 357 flimflam::kStatePortal) { |
| 383 // Take into account shill's detection results. | 358 // Take into account shill's detection results. |
| 384 state.status = CAPTIVE_PORTAL_STATUS_PORTAL; | 359 state.status = CAPTIVE_PORTAL_STATUS_PORTAL; |
| 385 LOG(WARNING) << "Network " << active_network->unique_id() << " " | 360 LOG(WARNING) << "Network " << default_network->guid() << " " |
| 386 << "is marked as " | 361 << "is marked as " |
| 387 << CaptivePortalStatusString(state.status) << " " | 362 << CaptivePortalStatusString(state.status) << " " |
| 388 << "despite the fact that CaptivePortalDetector " | 363 << "despite the fact that CaptivePortalDetector " |
| 389 << "received no response"; | 364 << "received no response"; |
| 390 } else { | 365 } else { |
| 391 state.status = CAPTIVE_PORTAL_STATUS_OFFLINE; | 366 state.status = CAPTIVE_PORTAL_STATUS_OFFLINE; |
| 392 } | 367 } |
| 393 SetCaptivePortalState(active_network, state); | 368 SetCaptivePortalState(default_network, state); |
| 394 } else { | 369 } else { |
| 395 DetectCaptivePortal(results.retry_after_delta); | 370 DetectCaptivePortal(results.retry_after_delta); |
| 396 } | 371 } |
| 397 break; | 372 break; |
| 398 case captive_portal::RESULT_INTERNET_CONNECTED: | 373 case captive_portal::RESULT_INTERNET_CONNECTED: |
| 399 state.status = CAPTIVE_PORTAL_STATUS_ONLINE; | 374 state.status = CAPTIVE_PORTAL_STATUS_ONLINE; |
| 400 SetCaptivePortalState(active_network, state); | 375 SetCaptivePortalState(default_network, state); |
| 401 break; | 376 break; |
| 402 case captive_portal::RESULT_BEHIND_CAPTIVE_PORTAL: | 377 case captive_portal::RESULT_BEHIND_CAPTIVE_PORTAL: |
| 403 state.status = CAPTIVE_PORTAL_STATUS_PORTAL; | 378 state.status = CAPTIVE_PORTAL_STATUS_PORTAL; |
| 404 SetCaptivePortalState(active_network, state); | 379 SetCaptivePortalState(default_network, state); |
| 405 break; | 380 break; |
| 406 default: | 381 default: |
| 407 break; | 382 break; |
| 408 } | 383 } |
| 409 | 384 |
| 410 TryLazyDetection(); | 385 TryLazyDetection(); |
| 411 } | 386 } |
| 412 | 387 |
| 413 void NetworkPortalDetectorImpl::TryLazyDetection() { | 388 void NetworkPortalDetectorImpl::TryLazyDetection() { |
| 414 if (!IsPortalCheckPending() && !IsCheckingForPortal() && | 389 if (!IsPortalCheckPending() && !IsCheckingForPortal() && |
| (...skipping 20 matching lines...) Expand all Loading... |
| 435 | 410 |
| 436 bool NetworkPortalDetectorImpl::IsPortalCheckPending() const { | 411 bool NetworkPortalDetectorImpl::IsPortalCheckPending() const { |
| 437 return state_ == STATE_PORTAL_CHECK_PENDING; | 412 return state_ == STATE_PORTAL_CHECK_PENDING; |
| 438 } | 413 } |
| 439 | 414 |
| 440 bool NetworkPortalDetectorImpl::IsCheckingForPortal() const { | 415 bool NetworkPortalDetectorImpl::IsCheckingForPortal() const { |
| 441 return state_ == STATE_CHECKING_FOR_PORTAL; | 416 return state_ == STATE_CHECKING_FOR_PORTAL; |
| 442 } | 417 } |
| 443 | 418 |
| 444 void NetworkPortalDetectorImpl::SetCaptivePortalState( | 419 void NetworkPortalDetectorImpl::SetCaptivePortalState( |
| 445 const Network* network, | 420 const NetworkState* network, |
| 446 const CaptivePortalState& state) { | 421 const CaptivePortalState& state) { |
| 447 DCHECK(network); | 422 DCHECK(network); |
| 448 | 423 |
| 449 if (!detection_start_time_.is_null()) { | 424 if (!detection_start_time_.is_null()) { |
| 450 UMA_HISTOGRAM_TIMES("CaptivePortal.OOBE.DetectionDuration", | 425 UMA_HISTOGRAM_TIMES("CaptivePortal.OOBE.DetectionDuration", |
| 451 GetCurrentTimeTicks() - detection_start_time_); | 426 GetCurrentTimeTicks() - detection_start_time_); |
| 452 } | 427 } |
| 453 | 428 |
| 454 CaptivePortalStateMap::const_iterator it = | 429 CaptivePortalStateMap::const_iterator it = |
| 455 portal_state_map_.find(network->service_path()); | 430 portal_state_map_.find(network->path()); |
| 456 if (it == portal_state_map_.end() || | 431 if (it == portal_state_map_.end() || |
| 457 it->second.status != state.status || | 432 it->second.status != state.status || |
| 458 it->second.response_code != state.response_code) { | 433 it->second.response_code != state.response_code) { |
| 459 VLOG(1) << "Updating Chrome Captive Portal state: " | 434 VLOG(1) << "Updating Chrome Captive Portal state: " |
| 460 << "network=" << network->unique_id() << ", " | 435 << "network=" << network->guid() << ", " |
| 461 << "status=" << CaptivePortalStatusString(state.status) << ", " | 436 << "status=" << CaptivePortalStatusString(state.status) << ", " |
| 462 << "response_code=" << state.response_code; | 437 << "response_code=" << state.response_code; |
| 463 portal_state_map_[network->service_path()] = state; | 438 portal_state_map_[network->path()] = state; |
| 464 } | 439 } |
| 465 NotifyPortalDetectionCompleted(network, state); | 440 NotifyPortalDetectionCompleted(network, state); |
| 466 } | 441 } |
| 467 | 442 |
| 468 void NetworkPortalDetectorImpl::NotifyPortalDetectionCompleted( | 443 void NetworkPortalDetectorImpl::NotifyPortalDetectionCompleted( |
| 469 const Network* network, | 444 const NetworkState* network, |
| 470 const CaptivePortalState& state) { | 445 const CaptivePortalState& state) { |
| 471 FOR_EACH_OBSERVER(Observer, observers_, | 446 FOR_EACH_OBSERVER(Observer, observers_, |
| 472 OnPortalDetectionCompleted(network, state)); | 447 OnPortalDetectionCompleted(network, state)); |
| 473 } | 448 } |
| 474 | 449 |
| 475 base::TimeTicks NetworkPortalDetectorImpl::GetCurrentTimeTicks() const { | 450 base::TimeTicks NetworkPortalDetectorImpl::GetCurrentTimeTicks() const { |
| 476 if (time_ticks_for_testing_.is_null()) | 451 if (time_ticks_for_testing_.is_null()) |
| 477 return base::TimeTicks::Now(); | 452 return base::TimeTicks::Now(); |
| 478 else | 453 else |
| 479 return time_ticks_for_testing_; | 454 return time_ticks_for_testing_; |
| 480 } | 455 } |
| 481 | 456 |
| 482 bool NetworkPortalDetectorImpl::DetectionTimeoutIsCancelledForTesting() const { | 457 bool NetworkPortalDetectorImpl::DetectionTimeoutIsCancelledForTesting() const { |
| 483 return detection_timeout_.IsCancelled(); | 458 return detection_timeout_.IsCancelled(); |
| 484 } | 459 } |
| 485 | 460 |
| 486 } // namespace chromeos | 461 } // namespace chromeos |
| OLD | NEW |