| 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" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
| 13 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
| 14 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
| 15 #include "chrome/browser/chrome_notification_types.h" | 15 #include "chrome/browser/chrome_notification_types.h" |
| 16 #include "chromeos/dbus/dbus_thread_manager.h" | 16 #include "chromeos/dbus/dbus_thread_manager.h" |
| 17 #include "chromeos/dbus/shill_profile_client.h" | 17 #include "chromeos/dbus/shill_profile_client.h" |
| 18 #include "chromeos/device_event_log.h" |
| 18 #include "chromeos/login/login_state.h" | 19 #include "chromeos/login/login_state.h" |
| 19 #include "chromeos/network/network_event_log.h" | |
| 20 #include "chromeos/network/network_state.h" | 20 #include "chromeos/network/network_state.h" |
| 21 #include "chromeos/network/network_state_handler.h" | 21 #include "chromeos/network/network_state_handler.h" |
| 22 #include "content/public/browser/notification_service.h" | 22 #include "content/public/browser/notification_service.h" |
| 23 #include "content/public/common/content_switches.h" | 23 #include "content/public/common/content_switches.h" |
| 24 #include "net/http/http_status_code.h" | 24 #include "net/http/http_status_code.h" |
| 25 #include "third_party/cros_system_api/dbus/service_constants.h" | 25 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 26 | 26 |
| 27 using base::StringPrintf; | 27 using base::StringPrintf; |
| 28 using captive_portal::CaptivePortalDetector; | 28 using captive_portal::CaptivePortalDetector; |
| 29 | 29 |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 captive_portal::CaptivePortalResult result, | 155 captive_portal::CaptivePortalResult result, |
| 156 int response_code) | 156 int response_code) |
| 157 : network_name(network_name), | 157 : network_name(network_name), |
| 158 network_id(network_id), | 158 network_id(network_id), |
| 159 result(result), | 159 result(result), |
| 160 response_code(response_code) { | 160 response_code(response_code) { |
| 161 } | 161 } |
| 162 | 162 |
| 163 void NetworkPortalDetectorImpl::DetectionAttemptCompletedReport::Report() | 163 void NetworkPortalDetectorImpl::DetectionAttemptCompletedReport::Report() |
| 164 const { | 164 const { |
| 165 // TODO (ygorshenin@): remove VLOG as soon as NET_LOG_EVENT will be dumped on | 165 // To see NET_LOG output, use '--vmodule=device_event_log*=1' |
| 166 // a disk, crbug.com/293739. | 166 NET_LOG(EVENT) << "Detection attempt completed: " |
| 167 VLOG(1) << "Detection attempt completed: " | 167 << "name=" << network_name << ", " |
| 168 << "name=" << network_name << ", " | 168 << "id=" << network_id << ", " |
| 169 << "id=" << network_id << ", " | 169 << "result=" |
| 170 << "result=" << captive_portal::CaptivePortalResultToString(result) | 170 << captive_portal::CaptivePortalResultToString(result) << ", " |
| 171 << ", " | 171 << "response_code=" << response_code; |
| 172 << "response_code=" << response_code; | |
| 173 NET_LOG_EVENT(StringPrintf( | |
| 174 "Portal detection completed: network_id=%s, result=%s, " | |
| 175 "response_code=%d", | |
| 176 network_id.c_str(), | |
| 177 captive_portal::CaptivePortalResultToString(result).c_str(), | |
| 178 response_code), | |
| 179 network_name); | |
| 180 } | 172 } |
| 181 | 173 |
| 182 bool NetworkPortalDetectorImpl::DetectionAttemptCompletedReport::Equals( | 174 bool NetworkPortalDetectorImpl::DetectionAttemptCompletedReport::Equals( |
| 183 const DetectionAttemptCompletedReport& o) const { | 175 const DetectionAttemptCompletedReport& o) const { |
| 184 return network_name == o.network_name && network_id == o.network_id && | 176 return network_name == o.network_name && network_id == o.network_id && |
| 185 result == o.result && response_code == o.response_code; | 177 result == o.result && response_code == o.response_code; |
| 186 } | 178 } |
| 187 | 179 |
| 188 //////////////////////////////////////////////////////////////////////////////// | 180 //////////////////////////////////////////////////////////////////////////////// |
| 189 // NetworkPortalDetectorImpl, public: | 181 // NetworkPortalDetectorImpl, public: |
| (...skipping 24 matching lines...) Expand all Loading... |
| 214 const char NetworkPortalDetectorImpl::kSessionPortalToOnlineHistogram[] = | 206 const char NetworkPortalDetectorImpl::kSessionPortalToOnlineHistogram[] = |
| 215 "CaptivePortal.Session.PortalToOnlineTransition"; | 207 "CaptivePortal.Session.PortalToOnlineTransition"; |
| 216 | 208 |
| 217 // static | 209 // static |
| 218 void NetworkPortalDetectorImpl::Initialize( | 210 void NetworkPortalDetectorImpl::Initialize( |
| 219 net::URLRequestContextGetter* url_context) { | 211 net::URLRequestContextGetter* url_context) { |
| 220 if (NetworkPortalDetector::set_for_testing()) | 212 if (NetworkPortalDetector::set_for_testing()) |
| 221 return; | 213 return; |
| 222 CHECK(!NetworkPortalDetector::network_portal_detector()) | 214 CHECK(!NetworkPortalDetector::network_portal_detector()) |
| 223 << "NetworkPortalDetector was initialized twice."; | 215 << "NetworkPortalDetector was initialized twice."; |
| 216 NET_LOG(EVENT) << "NetworkPortalDetectorImpl::Initialize()"; |
| 224 if (base::CommandLine::ForCurrentProcess()->HasSwitch(::switches::kTestType)) | 217 if (base::CommandLine::ForCurrentProcess()->HasSwitch(::switches::kTestType)) |
| 225 set_network_portal_detector(new NetworkPortalDetectorStubImpl()); | 218 set_network_portal_detector(new NetworkPortalDetectorStubImpl()); |
| 226 else | 219 else |
| 227 set_network_portal_detector(new NetworkPortalDetectorImpl(url_context)); | 220 set_network_portal_detector(new NetworkPortalDetectorImpl(url_context)); |
| 228 } | 221 } |
| 229 | 222 |
| 230 NetworkPortalDetectorImpl::NetworkPortalDetectorImpl( | 223 NetworkPortalDetectorImpl::NetworkPortalDetectorImpl( |
| 231 const scoped_refptr<net::URLRequestContextGetter>& request_context) | 224 const scoped_refptr<net::URLRequestContextGetter>& request_context) |
| 232 : state_(STATE_IDLE), | 225 : state_(STATE_IDLE), |
| 233 test_url_(CaptivePortalDetector::kDefaultURL), | 226 test_url_(CaptivePortalDetector::kDefaultURL), |
| 234 enabled_(false), | 227 enabled_(false), |
| 235 strategy_(PortalDetectorStrategy::CreateById( | 228 strategy_(PortalDetectorStrategy::CreateById( |
| 236 PortalDetectorStrategy::STRATEGY_ID_LOGIN_SCREEN, this)), | 229 PortalDetectorStrategy::STRATEGY_ID_LOGIN_SCREEN, this)), |
| 237 last_detection_result_(CAPTIVE_PORTAL_STATUS_UNKNOWN), | 230 last_detection_result_(CAPTIVE_PORTAL_STATUS_UNKNOWN), |
| 238 same_detection_result_count_(0), | 231 same_detection_result_count_(0), |
| 239 no_response_result_count_(0), | 232 no_response_result_count_(0), |
| 240 weak_factory_(this) { | 233 weak_factory_(this) { |
| 234 NET_LOG(EVENT) << "NetworkPortalDetectorImpl::NetworkPortalDetectorImpl()"; |
| 241 captive_portal_detector_.reset(new CaptivePortalDetector(request_context)); | 235 captive_portal_detector_.reset(new CaptivePortalDetector(request_context)); |
| 242 | 236 |
| 243 registrar_.Add(this, | 237 registrar_.Add(this, |
| 244 chrome::NOTIFICATION_LOGIN_PROXY_CHANGED, | 238 chrome::NOTIFICATION_LOGIN_PROXY_CHANGED, |
| 245 content::NotificationService::AllSources()); | 239 content::NotificationService::AllSources()); |
| 246 registrar_.Add(this, | 240 registrar_.Add(this, |
| 247 chrome::NOTIFICATION_AUTH_SUPPLIED, | 241 chrome::NOTIFICATION_AUTH_SUPPLIED, |
| 248 content::NotificationService::AllSources()); | 242 content::NotificationService::AllSources()); |
| 249 registrar_.Add(this, | 243 registrar_.Add(this, |
| 250 chrome::NOTIFICATION_AUTH_CANCELLED, | 244 chrome::NOTIFICATION_AUTH_CANCELLED, |
| 251 content::NotificationService::AllSources()); | 245 content::NotificationService::AllSources()); |
| 252 | 246 |
| 253 NetworkHandler::Get()->network_state_handler()->AddObserver(this, FROM_HERE); | 247 NetworkHandler::Get()->network_state_handler()->AddObserver(this, FROM_HERE); |
| 254 StartDetectionIfIdle(); | 248 StartDetectionIfIdle(); |
| 255 } | 249 } |
| 256 | 250 |
| 257 NetworkPortalDetectorImpl::~NetworkPortalDetectorImpl() { | 251 NetworkPortalDetectorImpl::~NetworkPortalDetectorImpl() { |
| 252 NET_LOG(EVENT) << "NetworkPortalDetectorImpl::~NetworkPortalDetectorImpl()"; |
| 258 DCHECK(CalledOnValidThread()); | 253 DCHECK(CalledOnValidThread()); |
| 259 | 254 |
| 260 attempt_task_.Cancel(); | 255 attempt_task_.Cancel(); |
| 261 attempt_timeout_.Cancel(); | 256 attempt_timeout_.Cancel(); |
| 262 | 257 |
| 263 captive_portal_detector_->Cancel(); | 258 captive_portal_detector_->Cancel(); |
| 264 captive_portal_detector_.reset(); | 259 captive_portal_detector_.reset(); |
| 265 observers_.Clear(); | 260 observers_.Clear(); |
| 266 if (NetworkHandler::IsInitialized()) { | 261 if (NetworkHandler::IsInitialized()) { |
| 267 NetworkHandler::Get()->network_state_handler()->RemoveObserver(this, | 262 NetworkHandler::Get()->network_state_handler()->RemoveObserver(this, |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 299 DCHECK(CalledOnValidThread()); | 294 DCHECK(CalledOnValidThread()); |
| 300 if (enabled_) | 295 if (enabled_) |
| 301 return; | 296 return; |
| 302 | 297 |
| 303 DCHECK(is_idle()); | 298 DCHECK(is_idle()); |
| 304 enabled_ = true; | 299 enabled_ = true; |
| 305 | 300 |
| 306 const NetworkState* network = DefaultNetwork(); | 301 const NetworkState* network = DefaultNetwork(); |
| 307 if (!start_detection || !network) | 302 if (!start_detection || !network) |
| 308 return; | 303 return; |
| 309 NET_LOG_EVENT(StringPrintf("Starting detection attempt: network_id=%s", | 304 NET_LOG(EVENT) << "Starting detection attempt:" |
| 310 network->guid().c_str()), | 305 << " name=" << network->name() << " id=" << network->guid(); |
| 311 network->name()); | |
| 312 portal_state_map_.erase(network->guid()); | 306 portal_state_map_.erase(network->guid()); |
| 313 StartDetection(); | 307 StartDetection(); |
| 314 } | 308 } |
| 315 | 309 |
| 316 NetworkPortalDetectorImpl::CaptivePortalState | 310 NetworkPortalDetectorImpl::CaptivePortalState |
| 317 NetworkPortalDetectorImpl::GetCaptivePortalState(const std::string& guid) { | 311 NetworkPortalDetectorImpl::GetCaptivePortalState(const std::string& guid) { |
| 318 DCHECK(CalledOnValidThread()); | 312 DCHECK(CalledOnValidThread()); |
| 319 CaptivePortalStateMap::const_iterator it = portal_state_map_.find(guid); | 313 CaptivePortalStateMap::const_iterator it = portal_state_map_.find(guid); |
| 320 if (it == portal_state_map_.end()) | 314 if (it == portal_state_map_.end()) |
| 321 return CaptivePortalState(); | 315 return CaptivePortalState(); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 336 strategy_ = PortalDetectorStrategy::CreateById(id, this).Pass(); | 330 strategy_ = PortalDetectorStrategy::CreateById(id, this).Pass(); |
| 337 StopDetection(); | 331 StopDetection(); |
| 338 StartDetectionIfIdle(); | 332 StartDetectionIfIdle(); |
| 339 } | 333 } |
| 340 | 334 |
| 341 void NetworkPortalDetectorImpl::DefaultNetworkChanged( | 335 void NetworkPortalDetectorImpl::DefaultNetworkChanged( |
| 342 const NetworkState* default_network) { | 336 const NetworkState* default_network) { |
| 343 DCHECK(CalledOnValidThread()); | 337 DCHECK(CalledOnValidThread()); |
| 344 | 338 |
| 345 if (!default_network) { | 339 if (!default_network) { |
| 346 NET_LOG_EVENT("Default network changed", "None"); | 340 NET_LOG(EVENT) << "Default network changed: None"; |
| 347 | 341 |
| 348 default_network_name_.clear(); | 342 default_network_name_.clear(); |
| 349 | 343 |
| 350 StopDetection(); | 344 StopDetection(); |
| 351 | 345 |
| 352 CaptivePortalState state; | 346 CaptivePortalState state; |
| 353 state.status = CAPTIVE_PORTAL_STATUS_OFFLINE; | 347 state.status = CAPTIVE_PORTAL_STATUS_OFFLINE; |
| 354 OnDetectionCompleted(NULL, state); | 348 OnDetectionCompleted(NULL, state); |
| 355 return; | 349 return; |
| 356 } | 350 } |
| 357 | 351 |
| 358 default_network_name_ = default_network->name(); | 352 default_network_name_ = default_network->name(); |
| 359 | 353 |
| 360 bool network_changed = (default_network_id_ != default_network->guid()); | 354 bool network_changed = (default_network_id_ != default_network->guid()); |
| 361 default_network_id_ = default_network->guid(); | 355 default_network_id_ = default_network->guid(); |
| 362 | 356 |
| 363 bool connection_state_changed = | 357 bool connection_state_changed = |
| 364 (default_connection_state_ != default_network->connection_state()); | 358 (default_connection_state_ != default_network->connection_state()); |
| 365 default_connection_state_ = default_network->connection_state(); | 359 default_connection_state_ = default_network->connection_state(); |
| 366 | 360 |
| 367 NET_LOG_EVENT(StringPrintf( | 361 NET_LOG(EVENT) << "Default network changed:" |
| 368 "Default network changed: network_id=%s, state=%s, " | 362 << " name=" << default_network_name_ |
| 369 "changed=%d, state_changed=%d", | 363 << " id=" << default_network_id_ |
| 370 default_network_id_.c_str(), | 364 << " state=" << default_connection_state_ |
| 371 default_connection_state_.c_str(), | 365 << " changed=" << network_changed |
| 372 network_changed, | 366 << " state_changed=" << connection_state_changed; |
| 373 connection_state_changed), | |
| 374 default_network_name_); | |
| 375 | 367 |
| 376 if (network_changed || connection_state_changed) | 368 if (network_changed || connection_state_changed) |
| 377 StopDetection(); | 369 StopDetection(); |
| 378 | 370 |
| 379 if (is_idle() && NetworkState::StateIsConnected(default_connection_state_)) { | 371 if (is_idle() && NetworkState::StateIsConnected(default_connection_state_)) { |
| 380 // Initiate Captive Portal detection if network's captive | 372 // Initiate Captive Portal detection if network's captive |
| 381 // portal state is unknown (e.g. for freshly created networks), | 373 // portal state is unknown (e.g. for freshly created networks), |
| 382 // offline or if network connection state was changed. | 374 // offline or if network connection state was changed. |
| 383 CaptivePortalState state = GetCaptivePortalState(default_network->guid()); | 375 CaptivePortalState state = GetCaptivePortalState(default_network->guid()); |
| 384 if (state.status == CAPTIVE_PORTAL_STATUS_UNKNOWN || | 376 if (state.status == CAPTIVE_PORTAL_STATUS_UNKNOWN || |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 457 base::MessageLoop::current()->PostDelayedTask( | 449 base::MessageLoop::current()->PostDelayedTask( |
| 458 FROM_HERE, | 450 FROM_HERE, |
| 459 attempt_timeout_.callback(), | 451 attempt_timeout_.callback(), |
| 460 strategy_->GetNextAttemptTimeout()); | 452 strategy_->GetNextAttemptTimeout()); |
| 461 } | 453 } |
| 462 | 454 |
| 463 void NetworkPortalDetectorImpl::OnAttemptTimeout() { | 455 void NetworkPortalDetectorImpl::OnAttemptTimeout() { |
| 464 DCHECK(CalledOnValidThread()); | 456 DCHECK(CalledOnValidThread()); |
| 465 DCHECK(is_checking_for_portal()); | 457 DCHECK(is_checking_for_portal()); |
| 466 | 458 |
| 467 NET_LOG_ERROR(StringPrintf("Portal detection timeout: network_id=%s", | 459 NET_LOG(ERROR) << "Portal detection timeout: " |
| 468 default_network_id_.c_str()), | 460 << " name=" << default_network_name_ |
| 469 default_network_name_); | 461 << " id=" << default_network_id_; |
| 470 | 462 |
| 471 captive_portal_detector_->Cancel(); | 463 captive_portal_detector_->Cancel(); |
| 472 CaptivePortalDetector::Results results; | 464 CaptivePortalDetector::Results results; |
| 473 results.result = captive_portal::RESULT_NO_RESPONSE; | 465 results.result = captive_portal::RESULT_NO_RESPONSE; |
| 474 OnAttemptCompleted(results); | 466 OnAttemptCompleted(results); |
| 475 } | 467 } |
| 476 | 468 |
| 477 void NetworkPortalDetectorImpl::OnAttemptCompleted( | 469 void NetworkPortalDetectorImpl::OnAttemptCompleted( |
| 478 const CaptivePortalDetector::Results& results) { | 470 const CaptivePortalDetector::Results& results) { |
| 479 DCHECK(CalledOnValidThread()); | 471 DCHECK(CalledOnValidThread()); |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 557 ScheduleAttempt(results.retry_after_delta); | 549 ScheduleAttempt(results.retry_after_delta); |
| 558 } | 550 } |
| 559 | 551 |
| 560 void NetworkPortalDetectorImpl::Observe( | 552 void NetworkPortalDetectorImpl::Observe( |
| 561 int type, | 553 int type, |
| 562 const content::NotificationSource& source, | 554 const content::NotificationSource& source, |
| 563 const content::NotificationDetails& details) { | 555 const content::NotificationDetails& details) { |
| 564 if (type == chrome::NOTIFICATION_LOGIN_PROXY_CHANGED || | 556 if (type == chrome::NOTIFICATION_LOGIN_PROXY_CHANGED || |
| 565 type == chrome::NOTIFICATION_AUTH_SUPPLIED || | 557 type == chrome::NOTIFICATION_AUTH_SUPPLIED || |
| 566 type == chrome::NOTIFICATION_AUTH_CANCELLED) { | 558 type == chrome::NOTIFICATION_AUTH_CANCELLED) { |
| 567 NET_LOG_EVENT( | 559 NET_LOG(EVENT) << "Restarting portal detection due to proxy change" |
| 568 "Restarting portal detection due to proxy change", | 560 << " name=" << default_network_name_; |
| 569 default_network_name_.empty() ? "None" : default_network_name_); | |
| 570 StopDetection(); | 561 StopDetection(); |
| 571 ScheduleAttempt(base::TimeDelta::FromSeconds(kProxyChangeDelaySec)); | 562 ScheduleAttempt(base::TimeDelta::FromSeconds(kProxyChangeDelaySec)); |
| 572 } | 563 } |
| 573 } | 564 } |
| 574 | 565 |
| 575 void NetworkPortalDetectorImpl::OnDetectionCompleted( | 566 void NetworkPortalDetectorImpl::OnDetectionCompleted( |
| 576 const NetworkState* network, | 567 const NetworkState* network, |
| 577 const CaptivePortalState& state) { | 568 const CaptivePortalState& state) { |
| 578 if (!network) { | 569 if (!network) { |
| 579 NotifyDetectionCompleted(network, state); | 570 NotifyDetectionCompleted(network, state); |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 651 } | 642 } |
| 652 | 643 |
| 653 void NetworkPortalDetectorImpl::ResetStrategyAndCounters() { | 644 void NetworkPortalDetectorImpl::ResetStrategyAndCounters() { |
| 654 last_detection_result_ = CAPTIVE_PORTAL_STATUS_UNKNOWN; | 645 last_detection_result_ = CAPTIVE_PORTAL_STATUS_UNKNOWN; |
| 655 same_detection_result_count_ = 0; | 646 same_detection_result_count_ = 0; |
| 656 no_response_result_count_ = 0; | 647 no_response_result_count_ = 0; |
| 657 strategy_->Reset(); | 648 strategy_->Reset(); |
| 658 } | 649 } |
| 659 | 650 |
| 660 } // namespace chromeos | 651 } // namespace chromeos |
| OLD | NEW |