| 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/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 // Minimum timeout between consecutive portal checks for the same | 32 // Minimum timeout between consecutive portal checks for the same |
| 33 // network. | 33 // network. |
| 34 const int kMinTimeBetweenAttemptsSec = 3; | 34 const int kMinTimeBetweenAttemptsSec = 3; |
| 35 | 35 |
| 36 // Delay before portal detection caused by changes in proxy settings. | 36 // Delay before portal detection caused by changes in proxy settings. |
| 37 const int kProxyChangeDelaySec = 1; | 37 const int kProxyChangeDelaySec = 1; |
| 38 | 38 |
| 39 // Delay between consecutive portal checks for a network in lazy mode. | 39 // Delay between consecutive portal checks for a network in lazy mode. |
| 40 const int kLazyCheckIntervalSec = 5; | 40 const int kLazyCheckIntervalSec = 5; |
| 41 | 41 |
| 42 const char kCaptivePortalStatusUnknown[] = "Unknown"; |
| 43 const char kCaptivePortalStatusOffline[] = "Offline"; |
| 44 const char kCaptivePortalStatusOnline[] = "Online"; |
| 45 const char kCaptivePortalStatusPortal[] = "Portal"; |
| 46 const char kCaptivePortalStatusProxyAuthRequired[] = |
| 47 "Proxy authentication required"; |
| 48 const char kCaptivePortalStatusUnrecognized[] = "Unrecognized"; |
| 49 |
| 42 std::string CaptivePortalStatusString( | 50 std::string CaptivePortalStatusString( |
| 43 NetworkPortalDetectorImpl::CaptivePortalStatus status) { | 51 NetworkPortalDetectorImpl::CaptivePortalStatus status) { |
| 44 switch (status) { | 52 switch (status) { |
| 45 case NetworkPortalDetectorImpl::CAPTIVE_PORTAL_STATUS_UNKNOWN: | 53 case NetworkPortalDetectorImpl::CAPTIVE_PORTAL_STATUS_UNKNOWN: |
| 46 return l10n_util::GetStringUTF8( | 54 return kCaptivePortalStatusUnknown; |
| 47 IDS_CHROMEOS_CAPTIVE_PORTAL_STATUS_UNKNOWN); | |
| 48 case NetworkPortalDetectorImpl::CAPTIVE_PORTAL_STATUS_OFFLINE: | 55 case NetworkPortalDetectorImpl::CAPTIVE_PORTAL_STATUS_OFFLINE: |
| 49 return l10n_util::GetStringUTF8( | 56 return kCaptivePortalStatusOffline; |
| 50 IDS_CHROMEOS_CAPTIVE_PORTAL_STATUS_OFFLINE); | |
| 51 case NetworkPortalDetectorImpl::CAPTIVE_PORTAL_STATUS_ONLINE: | 57 case NetworkPortalDetectorImpl::CAPTIVE_PORTAL_STATUS_ONLINE: |
| 52 return l10n_util::GetStringUTF8( | 58 return kCaptivePortalStatusOnline; |
| 53 IDS_CHROMEOS_CAPTIVE_PORTAL_STATUS_ONLINE); | |
| 54 case NetworkPortalDetectorImpl::CAPTIVE_PORTAL_STATUS_PORTAL: | 59 case NetworkPortalDetectorImpl::CAPTIVE_PORTAL_STATUS_PORTAL: |
| 55 return l10n_util::GetStringUTF8( | 60 return kCaptivePortalStatusPortal; |
| 56 IDS_CHROMEOS_CAPTIVE_PORTAL_STATUS_PORTAL); | |
| 57 case NetworkPortalDetectorImpl::CAPTIVE_PORTAL_STATUS_PROXY_AUTH_REQUIRED: | 61 case NetworkPortalDetectorImpl::CAPTIVE_PORTAL_STATUS_PROXY_AUTH_REQUIRED: |
| 58 return l10n_util::GetStringUTF8( | 62 return kCaptivePortalStatusProxyAuthRequired; |
| 59 IDS_CHROMEOS_CAPTIVE_PORTAL_STATUS_PROXY_AUTH_REQUIRED); | |
| 60 case NetworkPortalDetectorImpl::CAPTIVE_PORTAL_STATUS_COUNT: | 63 case NetworkPortalDetectorImpl::CAPTIVE_PORTAL_STATUS_COUNT: |
| 61 NOTREACHED(); | 64 NOTREACHED(); |
| 62 } | 65 } |
| 63 return l10n_util::GetStringUTF8( | 66 return kCaptivePortalStatusUnrecognized; |
| 64 IDS_CHROMEOS_CAPTIVE_PORTAL_STATUS_UNRECOGNIZED); | |
| 65 } | 67 } |
| 66 | 68 |
| 67 } // namespace | 69 } // namespace |
| 68 | 70 |
| 69 //////////////////////////////////////////////////////////////////////////////// | 71 //////////////////////////////////////////////////////////////////////////////// |
| 70 // NetworkPortalDetectorImpl, public: | 72 // NetworkPortalDetectorImpl, public: |
| 71 | 73 |
| 72 NetworkPortalDetectorImpl::NetworkPortalDetectorImpl( | 74 NetworkPortalDetectorImpl::NetworkPortalDetectorImpl( |
| 73 const scoped_refptr<net::URLRequestContextGetter>& request_context) | 75 const scoped_refptr<net::URLRequestContextGetter>& request_context) |
| 74 : state_(STATE_IDLE), | 76 : state_(STATE_IDLE), |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 196 lazy_detection_enabled_ = false; | 198 lazy_detection_enabled_ = false; |
| 197 if (attempt_count_ == kMaxRequestAttempts && IsPortalCheckPending()) | 199 if (attempt_count_ == kMaxRequestAttempts && IsPortalCheckPending()) |
| 198 CancelPortalDetection(); | 200 CancelPortalDetection(); |
| 199 VLOG(1) << "Lazy detection mode disabled."; | 201 VLOG(1) << "Lazy detection mode disabled."; |
| 200 } | 202 } |
| 201 | 203 |
| 202 void NetworkPortalDetectorImpl::DefaultNetworkChanged( | 204 void NetworkPortalDetectorImpl::DefaultNetworkChanged( |
| 203 const NetworkState* default_network) { | 205 const NetworkState* default_network) { |
| 204 DCHECK(CalledOnValidThread()); | 206 DCHECK(CalledOnValidThread()); |
| 205 if (!default_network) { | 207 if (!default_network) { |
| 208 default_network_name_.clear(); |
| 206 default_network_id_.clear(); | 209 default_network_id_.clear(); |
| 207 return; | 210 return; |
| 208 } | 211 } |
| 209 | 212 |
| 213 default_network_name_ = default_network->name(); |
| 210 default_network_id_ = default_network->guid(); | 214 default_network_id_ = default_network->guid(); |
| 211 | 215 |
| 212 bool network_changed = (default_service_path_ != default_network->path()); | 216 bool network_changed = (default_service_path_ != default_network->path()); |
| 213 default_service_path_ = default_network->path(); | 217 default_service_path_ = default_network->path(); |
| 214 | 218 |
| 215 bool connection_state_changed = (default_connection_state_ != | 219 bool connection_state_changed = (default_connection_state_ != |
| 216 default_network->connection_state()); | 220 default_network->connection_state()); |
| 217 default_connection_state_ = default_network->connection_state(); | 221 default_connection_state_ = default_network->connection_state(); |
| 218 | 222 |
| 219 if (network_changed || connection_state_changed) { | 223 if (network_changed || connection_state_changed) { |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 283 | 287 |
| 284 void NetworkPortalDetectorImpl::DetectCaptivePortalTask() { | 288 void NetworkPortalDetectorImpl::DetectCaptivePortalTask() { |
| 285 DCHECK(IsPortalCheckPending()); | 289 DCHECK(IsPortalCheckPending()); |
| 286 | 290 |
| 287 state_ = STATE_CHECKING_FOR_PORTAL; | 291 state_ = STATE_CHECKING_FOR_PORTAL; |
| 288 attempt_start_time_ = GetCurrentTimeTicks(); | 292 attempt_start_time_ = GetCurrentTimeTicks(); |
| 289 | 293 |
| 290 if (attempt_count_ < kMaxRequestAttempts) { | 294 if (attempt_count_ < kMaxRequestAttempts) { |
| 291 ++attempt_count_; | 295 ++attempt_count_; |
| 292 VLOG(1) << "Portal detection started: " | 296 VLOG(1) << "Portal detection started: " |
| 293 << "network=" << default_network_id_ << ", " | 297 << "name=" << default_network_name_ << ", " |
| 298 << "id=" << default_network_id_ << ", " |
| 294 << "attempt=" << attempt_count_ << " of " << kMaxRequestAttempts; | 299 << "attempt=" << attempt_count_ << " of " << kMaxRequestAttempts; |
| 295 } else { | 300 } else { |
| 296 DCHECK(lazy_detection_enabled()); | 301 DCHECK(lazy_detection_enabled()); |
| 297 VLOG(1) << "Lazy portal detection attempt started"; | 302 VLOG(1) << "Lazy portal detection attempt started"; |
| 298 } | 303 } |
| 299 | 304 |
| 300 captive_portal_detector_->DetectCaptivePortal( | 305 captive_portal_detector_->DetectCaptivePortal( |
| 301 test_url_, | 306 test_url_, |
| 302 base::Bind(&NetworkPortalDetectorImpl::OnPortalDetectionCompleted, | 307 base::Bind(&NetworkPortalDetectorImpl::OnPortalDetectionCompleted, |
| 303 weak_ptr_factory_.GetWeakPtr())); | 308 weak_ptr_factory_.GetWeakPtr())); |
| 304 detection_timeout_.Reset( | 309 detection_timeout_.Reset( |
| 305 base::Bind(&NetworkPortalDetectorImpl::PortalDetectionTimeout, | 310 base::Bind(&NetworkPortalDetectorImpl::PortalDetectionTimeout, |
| 306 weak_ptr_factory_.GetWeakPtr())); | 311 weak_ptr_factory_.GetWeakPtr())); |
| 307 base::TimeDelta request_timeout; | 312 base::TimeDelta request_timeout; |
| 308 | 313 |
| 309 // For easier unit testing check for testing state is performed here | 314 // For easier unit testing check for testing state is performed here |
| 310 // and not in the GetRequestTimeoutSec(). | 315 // and not in the GetRequestTimeoutSec(). |
| 311 if (request_timeout_for_testing_initialized_) | 316 if (request_timeout_for_testing_initialized_) |
| 312 request_timeout = request_timeout_for_testing_; | 317 request_timeout = request_timeout_for_testing_; |
| 313 else | 318 else |
| 314 request_timeout = base::TimeDelta::FromSeconds(GetRequestTimeoutSec()); | 319 request_timeout = base::TimeDelta::FromSeconds(GetRequestTimeoutSec()); |
| 315 base::MessageLoop::current()->PostDelayedTask( | 320 base::MessageLoop::current()->PostDelayedTask( |
| 316 FROM_HERE, detection_timeout_.callback(), request_timeout); | 321 FROM_HERE, detection_timeout_.callback(), request_timeout); |
| 317 } | 322 } |
| 318 | 323 |
| 319 void NetworkPortalDetectorImpl::PortalDetectionTimeout() { | 324 void NetworkPortalDetectorImpl::PortalDetectionTimeout() { |
| 320 DCHECK(CalledOnValidThread()); | 325 DCHECK(CalledOnValidThread()); |
| 321 DCHECK(IsCheckingForPortal()); | 326 DCHECK(IsCheckingForPortal()); |
| 322 | 327 |
| 323 VLOG(1) << "Portal detection timeout: network=" << default_network_id_; | 328 VLOG(1) << "Portal detection timeout: name=" << default_network_name_ << ", " |
| 329 << "id=" << default_network_id_; |
| 324 | 330 |
| 325 captive_portal_detector_->Cancel(); | 331 captive_portal_detector_->Cancel(); |
| 326 CaptivePortalDetector::Results results; | 332 CaptivePortalDetector::Results results; |
| 327 results.result = captive_portal::RESULT_NO_RESPONSE; | 333 results.result = captive_portal::RESULT_NO_RESPONSE; |
| 328 OnPortalDetectionCompleted(results); | 334 OnPortalDetectionCompleted(results); |
| 329 } | 335 } |
| 330 | 336 |
| 331 void NetworkPortalDetectorImpl::CancelPortalDetection() { | 337 void NetworkPortalDetectorImpl::CancelPortalDetection() { |
| 332 if (IsPortalCheckPending()) | 338 if (IsPortalCheckPending()) |
| 333 detection_task_.Cancel(); | 339 detection_task_.Cancel(); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 344 | 350 |
| 345 if (shill_stub_helper::IsStubPortalledWifiEnabled(default_service_path_)) { | 351 if (shill_stub_helper::IsStubPortalledWifiEnabled(default_service_path_)) { |
| 346 result = captive_portal::RESULT_BEHIND_CAPTIVE_PORTAL; | 352 result = captive_portal::RESULT_BEHIND_CAPTIVE_PORTAL; |
| 347 response_code = 200; | 353 response_code = 200; |
| 348 } | 354 } |
| 349 | 355 |
| 350 DCHECK(CalledOnValidThread()); | 356 DCHECK(CalledOnValidThread()); |
| 351 DCHECK(IsCheckingForPortal()); | 357 DCHECK(IsCheckingForPortal()); |
| 352 | 358 |
| 353 VLOG(1) << "Portal detection completed: " | 359 VLOG(1) << "Portal detection completed: " |
| 354 << "network=" << default_network_id_ << ", " | 360 << "name=" << default_network_name_ << ", " |
| 361 << "id=" << default_network_id_ << ", " |
| 355 << "result=" << CaptivePortalDetector::CaptivePortalResultToString( | 362 << "result=" << CaptivePortalDetector::CaptivePortalResultToString( |
| 356 results.result) << ", " | 363 results.result) << ", " |
| 357 << "response_code=" << results.response_code; | 364 << "response_code=" << results.response_code; |
| 358 | 365 |
| 359 state_ = STATE_IDLE; | 366 state_ = STATE_IDLE; |
| 360 detection_timeout_.Cancel(); | 367 detection_timeout_.Cancel(); |
| 361 | 368 |
| 362 const NetworkState* default_network = | 369 const NetworkState* default_network = |
| 363 NetworkHandler::Get()->network_state_handler()->DefaultNetwork(); | 370 NetworkHandler::Get()->network_state_handler()->DefaultNetwork(); |
| 364 | 371 |
| 365 CaptivePortalState state; | 372 CaptivePortalState state; |
| 366 state.response_code = response_code; | 373 state.response_code = response_code; |
| 367 switch (result) { | 374 switch (result) { |
| 368 case captive_portal::RESULT_NO_RESPONSE: | 375 case captive_portal::RESULT_NO_RESPONSE: |
| 369 if (attempt_count_ >= kMaxRequestAttempts) { | 376 if (attempt_count_ >= kMaxRequestAttempts) { |
| 370 if (state.response_code == net::HTTP_PROXY_AUTHENTICATION_REQUIRED) { | 377 if (state.response_code == net::HTTP_PROXY_AUTHENTICATION_REQUIRED) { |
| 371 state.status = CAPTIVE_PORTAL_STATUS_PROXY_AUTH_REQUIRED; | 378 state.status = CAPTIVE_PORTAL_STATUS_PROXY_AUTH_REQUIRED; |
| 372 } else if (default_network && (default_network->connection_state() == | 379 } else if (default_network && (default_network->connection_state() == |
| 373 shill::kStatePortal)) { | 380 shill::kStatePortal)) { |
| 374 // Take into account shill's detection results. | 381 // Take into account shill's detection results. |
| 375 state.status = CAPTIVE_PORTAL_STATUS_PORTAL; | 382 state.status = CAPTIVE_PORTAL_STATUS_PORTAL; |
| 376 LOG(WARNING) << "Network " << default_network->guid() << " " | 383 LOG(WARNING) << "Network name=" << default_network->name() << ", " |
| 384 << "id=" << default_network->guid() << " " |
| 377 << "is marked as " | 385 << "is marked as " |
| 378 << CaptivePortalStatusString(state.status) << " " | 386 << CaptivePortalStatusString(state.status) << " " |
| 379 << "despite the fact that CaptivePortalDetector " | 387 << "despite the fact that CaptivePortalDetector " |
| 380 << "received no response"; | 388 << "received no response"; |
| 381 } else { | 389 } else { |
| 382 state.status = CAPTIVE_PORTAL_STATUS_OFFLINE; | 390 state.status = CAPTIVE_PORTAL_STATUS_OFFLINE; |
| 383 } | 391 } |
| 384 SetCaptivePortalState(default_network, state); | 392 SetCaptivePortalState(default_network, state); |
| 385 } else { | 393 } else { |
| 386 DCHECK(CanPerformDetection()); | 394 DCHECK(CanPerformDetection()); |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 444 NotifyPortalDetectionCompleted(network, state); | 452 NotifyPortalDetectionCompleted(network, state); |
| 445 return; | 453 return; |
| 446 } | 454 } |
| 447 | 455 |
| 448 CaptivePortalStateMap::const_iterator it = | 456 CaptivePortalStateMap::const_iterator it = |
| 449 portal_state_map_.find(network->path()); | 457 portal_state_map_.find(network->path()); |
| 450 if (it == portal_state_map_.end() || | 458 if (it == portal_state_map_.end() || |
| 451 it->second.status != state.status || | 459 it->second.status != state.status || |
| 452 it->second.response_code != state.response_code) { | 460 it->second.response_code != state.response_code) { |
| 453 VLOG(1) << "Updating Chrome Captive Portal state: " | 461 VLOG(1) << "Updating Chrome Captive Portal state: " |
| 454 << "network=" << network->guid() << ", " | 462 << "name=" << network->name() << ", " |
| 463 << "id=" << network->guid() << ", " |
| 455 << "status=" << CaptivePortalStatusString(state.status) << ", " | 464 << "status=" << CaptivePortalStatusString(state.status) << ", " |
| 456 << "response_code=" << state.response_code; | 465 << "response_code=" << state.response_code; |
| 457 portal_state_map_[network->path()] = state; | 466 portal_state_map_[network->path()] = state; |
| 458 } | 467 } |
| 459 NotifyPortalDetectionCompleted(network, state); | 468 NotifyPortalDetectionCompleted(network, state); |
| 460 } | 469 } |
| 461 | 470 |
| 462 void NetworkPortalDetectorImpl::NotifyPortalDetectionCompleted( | 471 void NetworkPortalDetectorImpl::NotifyPortalDetectionCompleted( |
| 463 const NetworkState* network, | 472 const NetworkState* network, |
| 464 const CaptivePortalState& state) { | 473 const CaptivePortalState& state) { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 481 const NetworkState* network = | 490 const NetworkState* network = |
| 482 NetworkHandler::Get()->network_state_handler()->DefaultNetwork(); | 491 NetworkHandler::Get()->network_state_handler()->DefaultNetwork(); |
| 483 if (!network) | 492 if (!network) |
| 484 return kBaseRequestTimeoutSec; | 493 return kBaseRequestTimeoutSec; |
| 485 if (lazy_detection_enabled_) | 494 if (lazy_detection_enabled_) |
| 486 return kLazyRequestTimeoutSec; | 495 return kLazyRequestTimeoutSec; |
| 487 return attempt_count_ * kBaseRequestTimeoutSec; | 496 return attempt_count_ * kBaseRequestTimeoutSec; |
| 488 } | 497 } |
| 489 | 498 |
| 490 } // namespace chromeos | 499 } // namespace chromeos |
| OLD | NEW |