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 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 "CaptivePortal.Session.PortalToOnlineTransition"; | 149 "CaptivePortal.Session.PortalToOnlineTransition"; |
150 | 150 |
151 NetworkPortalDetectorImpl::NetworkPortalDetectorImpl( | 151 NetworkPortalDetectorImpl::NetworkPortalDetectorImpl( |
152 const scoped_refptr<net::URLRequestContextGetter>& request_context) | 152 const scoped_refptr<net::URLRequestContextGetter>& request_context) |
153 : state_(STATE_IDLE), | 153 : state_(STATE_IDLE), |
154 test_url_(CaptivePortalDetector::kDefaultURL), | 154 test_url_(CaptivePortalDetector::kDefaultURL), |
155 enabled_(false), | 155 enabled_(false), |
156 weak_factory_(this), | 156 weak_factory_(this), |
157 attempt_count_(0), | 157 attempt_count_(0), |
158 strategy_(PortalDetectorStrategy::CreateById( | 158 strategy_(PortalDetectorStrategy::CreateById( |
159 PortalDetectorStrategy::STRATEGY_ID_COUNT)) { | 159 PortalDetectorStrategy::STRATEGY_ID_LOGIN_SCREEN)), |
| 160 error_screen_displayed_(false) { |
160 captive_portal_detector_.reset(new CaptivePortalDetector(request_context)); | 161 captive_portal_detector_.reset(new CaptivePortalDetector(request_context)); |
161 strategy_->set_delegate(this); | 162 strategy_->set_delegate(this); |
162 | 163 |
163 registrar_.Add(this, | 164 registrar_.Add(this, |
164 chrome::NOTIFICATION_LOGIN_PROXY_CHANGED, | 165 chrome::NOTIFICATION_LOGIN_PROXY_CHANGED, |
165 content::NotificationService::AllSources()); | 166 content::NotificationService::AllSources()); |
166 registrar_.Add(this, | 167 registrar_.Add(this, |
167 chrome::NOTIFICATION_AUTH_SUPPLIED, | 168 chrome::NOTIFICATION_AUTH_SUPPLIED, |
168 content::NotificationService::AllSources()); | 169 content::NotificationService::AllSources()); |
169 registrar_.Add(this, | 170 registrar_.Add(this, |
170 chrome::NOTIFICATION_AUTH_CANCELLED, | 171 chrome::NOTIFICATION_AUTH_CANCELLED, |
171 content::NotificationService::AllSources()); | 172 content::NotificationService::AllSources()); |
| 173 registrar_.Add(this, |
| 174 chrome::NOTIFICATION_LOGIN_USER_CHANGED, |
| 175 content::NotificationService::AllSources()); |
172 | 176 |
173 NetworkHandler::Get()->network_state_handler()->AddObserver(this, FROM_HERE); | 177 NetworkHandler::Get()->network_state_handler()->AddObserver(this, FROM_HERE); |
174 SetStrategy(PortalDetectorStrategy::STRATEGY_ID_LOGIN_SCREEN); | 178 UpdateCurrentStrategy(); |
175 } | 179 } |
176 | 180 |
177 NetworkPortalDetectorImpl::~NetworkPortalDetectorImpl() { | 181 NetworkPortalDetectorImpl::~NetworkPortalDetectorImpl() { |
178 DCHECK(CalledOnValidThread()); | 182 DCHECK(CalledOnValidThread()); |
179 | 183 |
180 attempt_task_.Cancel(); | 184 attempt_task_.Cancel(); |
181 attempt_timeout_.Cancel(); | 185 attempt_timeout_.Cancel(); |
182 | 186 |
183 captive_portal_detector_->Cancel(); | 187 captive_portal_detector_->Cancel(); |
184 captive_portal_detector_.reset(); | 188 captive_portal_detector_.reset(); |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
241 return it->second; | 245 return it->second; |
242 } | 246 } |
243 | 247 |
244 bool NetworkPortalDetectorImpl::StartDetectionIfIdle() { | 248 bool NetworkPortalDetectorImpl::StartDetectionIfIdle() { |
245 if (!is_idle()) | 249 if (!is_idle()) |
246 return false; | 250 return false; |
247 StartDetection(); | 251 StartDetection(); |
248 return true; | 252 return true; |
249 } | 253 } |
250 | 254 |
251 void NetworkPortalDetectorImpl::SetStrategy( | |
252 PortalDetectorStrategy::StrategyId id) { | |
253 if (id == strategy_->Id()) | |
254 return; | |
255 strategy_.reset(PortalDetectorStrategy::CreateById(id).release()); | |
256 strategy_->set_delegate(this); | |
257 StopDetection(); | |
258 StartDetectionIfIdle(); | |
259 } | |
260 | |
261 void NetworkPortalDetectorImpl::DefaultNetworkChanged( | 255 void NetworkPortalDetectorImpl::DefaultNetworkChanged( |
262 const NetworkState* default_network) { | 256 const NetworkState* default_network) { |
263 DCHECK(CalledOnValidThread()); | 257 DCHECK(CalledOnValidThread()); |
264 | 258 |
265 if (!default_network) { | 259 if (!default_network) { |
266 default_network_name_.clear(); | 260 default_network_name_.clear(); |
267 default_network_id_.clear(); | 261 default_network_id_.clear(); |
268 | 262 |
269 StopDetection(); | 263 StopDetection(); |
270 | 264 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
306 base::TimeTicks NetworkPortalDetectorImpl::AttemptStartTime() { | 300 base::TimeTicks NetworkPortalDetectorImpl::AttemptStartTime() { |
307 return attempt_start_time_; | 301 return attempt_start_time_; |
308 } | 302 } |
309 | 303 |
310 base::TimeTicks NetworkPortalDetectorImpl::GetCurrentTimeTicks() { | 304 base::TimeTicks NetworkPortalDetectorImpl::GetCurrentTimeTicks() { |
311 if (time_ticks_for_testing_.is_null()) | 305 if (time_ticks_for_testing_.is_null()) |
312 return base::TimeTicks::Now(); | 306 return base::TimeTicks::Now(); |
313 return time_ticks_for_testing_; | 307 return time_ticks_for_testing_; |
314 } | 308 } |
315 | 309 |
| 310 void NetworkPortalDetectorImpl::OnErrorScreenShow() { |
| 311 error_screen_displayed_ = true; |
| 312 UpdateCurrentStrategy(); |
| 313 } |
| 314 |
| 315 void NetworkPortalDetectorImpl::OnErrorScreenHide() { |
| 316 error_screen_displayed_ = false; |
| 317 UpdateCurrentStrategy(); |
| 318 } |
316 | 319 |
317 //////////////////////////////////////////////////////////////////////////////// | 320 //////////////////////////////////////////////////////////////////////////////// |
318 // NetworkPortalDetectorImpl, private: | 321 // NetworkPortalDetectorImpl, private: |
319 | 322 |
320 void NetworkPortalDetectorImpl::StartDetection() { | 323 void NetworkPortalDetectorImpl::StartDetection() { |
321 attempt_count_ = 0; | 324 attempt_count_ = 0; |
322 DCHECK(CanPerformAttempt()); | 325 DCHECK(CanPerformAttempt()); |
323 detection_start_time_ = GetCurrentTimeTicks(); | 326 detection_start_time_ = GetCurrentTimeTicks(); |
324 ScheduleAttempt(base::TimeDelta()); | 327 ScheduleAttempt(base::TimeDelta()); |
325 } | 328 } |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
462 const content::NotificationDetails& details) { | 465 const content::NotificationDetails& details) { |
463 if (type == chrome::NOTIFICATION_LOGIN_PROXY_CHANGED || | 466 if (type == chrome::NOTIFICATION_LOGIN_PROXY_CHANGED || |
464 type == chrome::NOTIFICATION_AUTH_SUPPLIED || | 467 type == chrome::NOTIFICATION_AUTH_SUPPLIED || |
465 type == chrome::NOTIFICATION_AUTH_CANCELLED) { | 468 type == chrome::NOTIFICATION_AUTH_CANCELLED) { |
466 VLOG(1) << "Restarting portal detection due to proxy change."; | 469 VLOG(1) << "Restarting portal detection due to proxy change."; |
467 attempt_count_ = 0; | 470 attempt_count_ = 0; |
468 if (is_portal_check_pending()) | 471 if (is_portal_check_pending()) |
469 return; | 472 return; |
470 StopDetection(); | 473 StopDetection(); |
471 ScheduleAttempt(base::TimeDelta::FromSeconds(kProxyChangeDelaySec)); | 474 ScheduleAttempt(base::TimeDelta::FromSeconds(kProxyChangeDelaySec)); |
| 475 } else if (type == chrome::NOTIFICATION_LOGIN_USER_CHANGED) { |
| 476 UpdateCurrentStrategy(); |
472 } | 477 } |
473 } | 478 } |
474 | 479 |
475 void NetworkPortalDetectorImpl::OnDetectionCompleted( | 480 void NetworkPortalDetectorImpl::OnDetectionCompleted( |
476 const NetworkState* network, | 481 const NetworkState* network, |
477 const CaptivePortalState& state) { | 482 const CaptivePortalState& state) { |
478 if (!network) { | 483 if (!network) { |
479 NotifyDetectionCompleted(network, state); | 484 NotifyDetectionCompleted(network, state); |
480 return; | 485 return; |
481 } | 486 } |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
549 case NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PROXY_AUTH_REQUIRED: | 554 case NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PROXY_AUTH_REQUIRED: |
550 if (network->connection_state() != shill::kStateOnline) | 555 if (network->connection_state() != shill::kStateOnline) |
551 RecordDiscrepancyWithShill(network, status); | 556 RecordDiscrepancyWithShill(network, status); |
552 break; | 557 break; |
553 case NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_COUNT: | 558 case NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_COUNT: |
554 NOTREACHED(); | 559 NOTREACHED(); |
555 break; | 560 break; |
556 } | 561 } |
557 } | 562 } |
558 | 563 |
| 564 void NetworkPortalDetectorImpl::UpdateCurrentStrategy() { |
| 565 if (InSession()) { |
| 566 SetStrategy(PortalDetectorStrategy::STRATEGY_ID_SESSION); |
| 567 return; |
| 568 } |
| 569 if (error_screen_displayed_) { |
| 570 SetStrategy(PortalDetectorStrategy::STRATEGY_ID_ERROR_SCREEN); |
| 571 return; |
| 572 } |
| 573 SetStrategy(PortalDetectorStrategy::STRATEGY_ID_LOGIN_SCREEN); |
| 574 } |
| 575 |
| 576 void NetworkPortalDetectorImpl::SetStrategy( |
| 577 PortalDetectorStrategy::StrategyId id) { |
| 578 if (id == strategy_->Id()) |
| 579 return; |
| 580 strategy_.reset(PortalDetectorStrategy::CreateById(id).release()); |
| 581 strategy_->set_delegate(this); |
| 582 StopDetection(); |
| 583 StartDetectionIfIdle(); |
| 584 } |
| 585 |
559 } // namespace chromeos | 586 } // namespace chromeos |
OLD | NEW |