| 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 <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| (...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 238 registrar_.Add(this, | 238 registrar_.Add(this, |
| 239 chrome::NOTIFICATION_AUTH_CANCELLED, | 239 chrome::NOTIFICATION_AUTH_CANCELLED, |
| 240 content::NotificationService::AllSources()); | 240 content::NotificationService::AllSources()); |
| 241 | 241 |
| 242 NetworkHandler::Get()->network_state_handler()->AddObserver(this, FROM_HERE); | 242 NetworkHandler::Get()->network_state_handler()->AddObserver(this, FROM_HERE); |
| 243 StartDetectionIfIdle(); | 243 StartDetectionIfIdle(); |
| 244 } | 244 } |
| 245 | 245 |
| 246 NetworkPortalDetectorImpl::~NetworkPortalDetectorImpl() { | 246 NetworkPortalDetectorImpl::~NetworkPortalDetectorImpl() { |
| 247 NET_LOG(EVENT) << "NetworkPortalDetectorImpl::~NetworkPortalDetectorImpl()"; | 247 NET_LOG(EVENT) << "NetworkPortalDetectorImpl::~NetworkPortalDetectorImpl()"; |
| 248 DCHECK(CalledOnValidThread()); | 248 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 249 | 249 |
| 250 attempt_task_.Cancel(); | 250 attempt_task_.Cancel(); |
| 251 attempt_timeout_.Cancel(); | 251 attempt_timeout_.Cancel(); |
| 252 | 252 |
| 253 captive_portal_detector_->Cancel(); | 253 captive_portal_detector_->Cancel(); |
| 254 captive_portal_detector_.reset(); | 254 captive_portal_detector_.reset(); |
| 255 observers_.Clear(); | 255 observers_.Clear(); |
| 256 if (NetworkHandler::IsInitialized()) { | 256 if (NetworkHandler::IsInitialized()) { |
| 257 NetworkHandler::Get()->network_state_handler()->RemoveObserver(this, | 257 NetworkHandler::Get()->network_state_handler()->RemoveObserver(this, |
| 258 FROM_HERE); | 258 FROM_HERE); |
| 259 } | 259 } |
| 260 } | 260 } |
| 261 | 261 |
| 262 void NetworkPortalDetectorImpl::AddObserver(Observer* observer) { | 262 void NetworkPortalDetectorImpl::AddObserver(Observer* observer) { |
| 263 DCHECK(CalledOnValidThread()); | 263 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 264 if (observer && !observers_.HasObserver(observer)) | 264 if (observer && !observers_.HasObserver(observer)) |
| 265 observers_.AddObserver(observer); | 265 observers_.AddObserver(observer); |
| 266 } | 266 } |
| 267 | 267 |
| 268 void NetworkPortalDetectorImpl::AddAndFireObserver(Observer* observer) { | 268 void NetworkPortalDetectorImpl::AddAndFireObserver(Observer* observer) { |
| 269 DCHECK(CalledOnValidThread()); | 269 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 270 if (!observer) | 270 if (!observer) |
| 271 return; | 271 return; |
| 272 AddObserver(observer); | 272 AddObserver(observer); |
| 273 CaptivePortalState portal_state; | 273 CaptivePortalState portal_state; |
| 274 const NetworkState* network = DefaultNetwork(); | 274 const NetworkState* network = DefaultNetwork(); |
| 275 if (network) | 275 if (network) |
| 276 portal_state = GetCaptivePortalState(network->guid()); | 276 portal_state = GetCaptivePortalState(network->guid()); |
| 277 observer->OnPortalDetectionCompleted(network, portal_state); | 277 observer->OnPortalDetectionCompleted(network, portal_state); |
| 278 } | 278 } |
| 279 | 279 |
| 280 void NetworkPortalDetectorImpl::RemoveObserver(Observer* observer) { | 280 void NetworkPortalDetectorImpl::RemoveObserver(Observer* observer) { |
| 281 DCHECK(CalledOnValidThread()); | 281 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 282 if (observer) | 282 if (observer) |
| 283 observers_.RemoveObserver(observer); | 283 observers_.RemoveObserver(observer); |
| 284 } | 284 } |
| 285 | 285 |
| 286 bool NetworkPortalDetectorImpl::IsEnabled() { return enabled_; } | 286 bool NetworkPortalDetectorImpl::IsEnabled() { return enabled_; } |
| 287 | 287 |
| 288 void NetworkPortalDetectorImpl::Enable(bool start_detection) { | 288 void NetworkPortalDetectorImpl::Enable(bool start_detection) { |
| 289 DCHECK(CalledOnValidThread()); | 289 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 290 if (enabled_) | 290 if (enabled_) |
| 291 return; | 291 return; |
| 292 | 292 |
| 293 DCHECK(is_idle()); | 293 DCHECK(is_idle()); |
| 294 enabled_ = true; | 294 enabled_ = true; |
| 295 | 295 |
| 296 const NetworkState* network = DefaultNetwork(); | 296 const NetworkState* network = DefaultNetwork(); |
| 297 if (!start_detection || !network) | 297 if (!start_detection || !network) |
| 298 return; | 298 return; |
| 299 NET_LOG(EVENT) << "Starting detection attempt:" | 299 NET_LOG(EVENT) << "Starting detection attempt:" |
| 300 << " name=" << network->name() << " id=" << network->guid(); | 300 << " name=" << network->name() << " id=" << network->guid(); |
| 301 portal_state_map_.erase(network->guid()); | 301 portal_state_map_.erase(network->guid()); |
| 302 StartDetection(); | 302 StartDetection(); |
| 303 } | 303 } |
| 304 | 304 |
| 305 NetworkPortalDetectorImpl::CaptivePortalState | 305 NetworkPortalDetectorImpl::CaptivePortalState |
| 306 NetworkPortalDetectorImpl::GetCaptivePortalState(const std::string& guid) { | 306 NetworkPortalDetectorImpl::GetCaptivePortalState(const std::string& guid) { |
| 307 DCHECK(CalledOnValidThread()); | 307 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 308 CaptivePortalStateMap::const_iterator it = portal_state_map_.find(guid); | 308 CaptivePortalStateMap::const_iterator it = portal_state_map_.find(guid); |
| 309 if (it == portal_state_map_.end()) | 309 if (it == portal_state_map_.end()) |
| 310 return CaptivePortalState(); | 310 return CaptivePortalState(); |
| 311 return it->second; | 311 return it->second; |
| 312 } | 312 } |
| 313 | 313 |
| 314 bool NetworkPortalDetectorImpl::StartDetectionIfIdle() { | 314 bool NetworkPortalDetectorImpl::StartDetectionIfIdle() { |
| 315 if (!is_idle()) | 315 if (!is_idle()) |
| 316 return false; | 316 return false; |
| 317 StartDetection(); | 317 StartDetection(); |
| 318 return true; | 318 return true; |
| 319 } | 319 } |
| 320 | 320 |
| 321 void NetworkPortalDetectorImpl::SetStrategy( | 321 void NetworkPortalDetectorImpl::SetStrategy( |
| 322 PortalDetectorStrategy::StrategyId id) { | 322 PortalDetectorStrategy::StrategyId id) { |
| 323 if (id == strategy_->Id()) | 323 if (id == strategy_->Id()) |
| 324 return; | 324 return; |
| 325 strategy_ = PortalDetectorStrategy::CreateById(id, this); | 325 strategy_ = PortalDetectorStrategy::CreateById(id, this); |
| 326 StopDetection(); | 326 StopDetection(); |
| 327 StartDetectionIfIdle(); | 327 StartDetectionIfIdle(); |
| 328 } | 328 } |
| 329 | 329 |
| 330 void NetworkPortalDetectorImpl::OnLockScreenRequest() { | 330 void NetworkPortalDetectorImpl::OnLockScreenRequest() { |
| 331 if (notification_controller_) | 331 if (notification_controller_) |
| 332 notification_controller_->CloseDialog(); | 332 notification_controller_->CloseDialog(); |
| 333 } | 333 } |
| 334 | 334 |
| 335 void NetworkPortalDetectorImpl::DefaultNetworkChanged( | 335 void NetworkPortalDetectorImpl::DefaultNetworkChanged( |
| 336 const NetworkState* default_network) { | 336 const NetworkState* default_network) { |
| 337 DCHECK(CalledOnValidThread()); | 337 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 338 | 338 |
| 339 if (!default_network) { | 339 if (!default_network) { |
| 340 NET_LOG(EVENT) << "Default network changed: None"; | 340 NET_LOG(EVENT) << "Default network changed: None"; |
| 341 | 341 |
| 342 default_network_name_.clear(); | 342 default_network_name_.clear(); |
| 343 | 343 |
| 344 StopDetection(); | 344 StopDetection(); |
| 345 | 345 |
| 346 CaptivePortalState state; | 346 CaptivePortalState state; |
| 347 state.status = CAPTIVE_PORTAL_STATUS_OFFLINE; | 347 state.status = CAPTIVE_PORTAL_STATUS_OFFLINE; |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 452 attempt_timeout_.Reset( | 452 attempt_timeout_.Reset( |
| 453 base::Bind(&NetworkPortalDetectorImpl::OnAttemptTimeout, | 453 base::Bind(&NetworkPortalDetectorImpl::OnAttemptTimeout, |
| 454 weak_factory_.GetWeakPtr())); | 454 weak_factory_.GetWeakPtr())); |
| 455 | 455 |
| 456 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 456 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 457 FROM_HERE, attempt_timeout_.callback(), | 457 FROM_HERE, attempt_timeout_.callback(), |
| 458 strategy_->GetNextAttemptTimeout()); | 458 strategy_->GetNextAttemptTimeout()); |
| 459 } | 459 } |
| 460 | 460 |
| 461 void NetworkPortalDetectorImpl::OnAttemptTimeout() { | 461 void NetworkPortalDetectorImpl::OnAttemptTimeout() { |
| 462 DCHECK(CalledOnValidThread()); | 462 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 463 DCHECK(is_checking_for_portal()); | 463 DCHECK(is_checking_for_portal()); |
| 464 | 464 |
| 465 NET_LOG(ERROR) << "Portal detection timeout: " | 465 NET_LOG(ERROR) << "Portal detection timeout: " |
| 466 << " name=" << default_network_name_ | 466 << " name=" << default_network_name_ |
| 467 << " id=" << default_network_id_; | 467 << " id=" << default_network_id_; |
| 468 | 468 |
| 469 captive_portal_detector_->Cancel(); | 469 captive_portal_detector_->Cancel(); |
| 470 CaptivePortalDetector::Results results; | 470 CaptivePortalDetector::Results results; |
| 471 results.result = captive_portal::RESULT_NO_RESPONSE; | 471 results.result = captive_portal::RESULT_NO_RESPONSE; |
| 472 OnAttemptCompleted(results); | 472 OnAttemptCompleted(results); |
| 473 } | 473 } |
| 474 | 474 |
| 475 void NetworkPortalDetectorImpl::OnAttemptCompleted( | 475 void NetworkPortalDetectorImpl::OnAttemptCompleted( |
| 476 const CaptivePortalDetector::Results& results) { | 476 const CaptivePortalDetector::Results& results) { |
| 477 DCHECK(CalledOnValidThread()); | 477 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 478 DCHECK(is_checking_for_portal()); | 478 DCHECK(is_checking_for_portal()); |
| 479 | 479 |
| 480 captive_portal::CaptivePortalResult result = results.result; | 480 captive_portal::CaptivePortalResult result = results.result; |
| 481 int response_code = results.response_code; | 481 int response_code = results.response_code; |
| 482 | 482 |
| 483 const NetworkState* network = DefaultNetwork(); | 483 const NetworkState* network = DefaultNetwork(); |
| 484 | 484 |
| 485 // If using a fake profile client, also fake being behind a captive portal | 485 // If using a fake profile client, also fake being behind a captive portal |
| 486 // if the default network is in portal state. | 486 // if the default network is in portal state. |
| 487 if (result != captive_portal::RESULT_NO_RESPONSE && | 487 if (result != captive_portal::RESULT_NO_RESPONSE && |
| (...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 647 } | 647 } |
| 648 | 648 |
| 649 void NetworkPortalDetectorImpl::ResetStrategyAndCounters() { | 649 void NetworkPortalDetectorImpl::ResetStrategyAndCounters() { |
| 650 last_detection_result_ = CAPTIVE_PORTAL_STATUS_UNKNOWN; | 650 last_detection_result_ = CAPTIVE_PORTAL_STATUS_UNKNOWN; |
| 651 same_detection_result_count_ = 0; | 651 same_detection_result_count_ = 0; |
| 652 no_response_result_count_ = 0; | 652 no_response_result_count_ = 0; |
| 653 strategy_->Reset(); | 653 strategy_->Reset(); |
| 654 } | 654 } |
| 655 | 655 |
| 656 } // namespace chromeos | 656 } // namespace chromeos |
| OLD | NEW |