| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/captive_portal/captive_portal_service.h" | 5 #include "chrome/browser/captive_portal/captive_portal_service.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.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" |
| 11 #include "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
| 12 #include "base/prefs/pref_service.h" | 12 #include "base/prefs/pref_service.h" |
| 13 #include "base/time/tick_clock.h" |
| 13 #include "chrome/browser/chrome_notification_types.h" | 14 #include "chrome/browser/chrome_notification_types.h" |
| 14 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/common/pref_names.h" | 16 #include "chrome/common/pref_names.h" |
| 16 #include "components/captive_portal/captive_portal_types.h" | 17 #include "components/captive_portal/captive_portal_types.h" |
| 17 #include "content/public/browser/notification_service.h" | 18 #include "content/public/browser/notification_service.h" |
| 18 | 19 |
| 19 #if defined(OS_WIN) | 20 #if defined(OS_WIN) |
| 20 #include "base/win/windows_version.h" | 21 #include "base/win/windows_version.h" |
| 21 #endif | 22 #endif |
| 22 | 23 |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 #else | 132 #else |
| 132 return false; | 133 return false; |
| 133 #endif | 134 #endif |
| 134 } | 135 } |
| 135 | 136 |
| 136 } // namespace | 137 } // namespace |
| 137 | 138 |
| 138 CaptivePortalService::TestingState CaptivePortalService::testing_state_ = | 139 CaptivePortalService::TestingState CaptivePortalService::testing_state_ = |
| 139 NOT_TESTING; | 140 NOT_TESTING; |
| 140 | 141 |
| 141 class CaptivePortalService::RecheckBackoffEntry : public net::BackoffEntry { | |
| 142 public: | |
| 143 explicit RecheckBackoffEntry(CaptivePortalService* captive_portal_service) | |
| 144 : net::BackoffEntry( | |
| 145 &captive_portal_service->recheck_policy().backoff_policy), | |
| 146 captive_portal_service_(captive_portal_service) { | |
| 147 } | |
| 148 | |
| 149 private: | |
| 150 base::TimeTicks ImplGetTimeNow() const override { | |
| 151 return captive_portal_service_->GetCurrentTimeTicks(); | |
| 152 } | |
| 153 | |
| 154 CaptivePortalService* captive_portal_service_; | |
| 155 | |
| 156 DISALLOW_COPY_AND_ASSIGN(RecheckBackoffEntry); | |
| 157 }; | |
| 158 | |
| 159 CaptivePortalService::RecheckPolicy::RecheckPolicy() | 142 CaptivePortalService::RecheckPolicy::RecheckPolicy() |
| 160 : initial_backoff_no_portal_ms(600 * 1000), | 143 : initial_backoff_no_portal_ms(600 * 1000), |
| 161 initial_backoff_portal_ms(20 * 1000) { | 144 initial_backoff_portal_ms(20 * 1000) { |
| 162 // Receiving a new Result is considered a success. All subsequent requests | 145 // Receiving a new Result is considered a success. All subsequent requests |
| 163 // that get the same Result are considered "failures", so a value of N | 146 // that get the same Result are considered "failures", so a value of N |
| 164 // means exponential backoff starts after getting a result N + 2 times: | 147 // means exponential backoff starts after getting a result N + 2 times: |
| 165 // +1 for the initial success, and +1 because N failures are ignored. | 148 // +1 for the initial success, and +1 because N failures are ignored. |
| 166 // | 149 // |
| 167 // A value of 6 means to start backoff on the 7th failure, which is the 8th | 150 // A value of 6 means to start backoff on the 7th failure, which is the 8th |
| 168 // time the same result is received. | 151 // time the same result is received. |
| 169 backoff_policy.num_errors_to_ignore = 6; | 152 backoff_policy.num_errors_to_ignore = 6; |
| 170 | 153 |
| 171 // It doesn't matter what this is initialized to. It will be overwritten | 154 // It doesn't matter what this is initialized to. It will be overwritten |
| 172 // after the first captive portal detection request. | 155 // after the first captive portal detection request. |
| 173 backoff_policy.initial_delay_ms = initial_backoff_no_portal_ms; | 156 backoff_policy.initial_delay_ms = initial_backoff_no_portal_ms; |
| 174 | 157 |
| 175 backoff_policy.multiply_factor = 2.0; | 158 backoff_policy.multiply_factor = 2.0; |
| 176 backoff_policy.jitter_factor = 0.3; | 159 backoff_policy.jitter_factor = 0.3; |
| 177 backoff_policy.maximum_backoff_ms = 2 * 60 * 1000; | 160 backoff_policy.maximum_backoff_ms = 2 * 60 * 1000; |
| 178 | 161 |
| 179 // -1 means the entry never expires. This doesn't really matter, as the | 162 // -1 means the entry never expires. This doesn't really matter, as the |
| 180 // service never checks for its expiration. | 163 // service never checks for its expiration. |
| 181 backoff_policy.entry_lifetime_ms = -1; | 164 backoff_policy.entry_lifetime_ms = -1; |
| 182 | 165 |
| 183 backoff_policy.always_use_initial_delay = true; | 166 backoff_policy.always_use_initial_delay = true; |
| 184 } | 167 } |
| 185 | 168 |
| 186 CaptivePortalService::CaptivePortalService(Profile* profile) | 169 CaptivePortalService::CaptivePortalService(Profile* profile) |
| 170 : CaptivePortalService(profile, nullptr) { |
| 171 } |
| 172 |
| 173 CaptivePortalService::CaptivePortalService(Profile* profile, |
| 174 base::TickClock* clock_for_testing) |
| 187 : profile_(profile), | 175 : profile_(profile), |
| 188 state_(STATE_IDLE), | 176 state_(STATE_IDLE), |
| 189 captive_portal_detector_(profile->GetRequestContext()), | 177 captive_portal_detector_(profile->GetRequestContext()), |
| 190 enabled_(false), | 178 enabled_(false), |
| 191 last_detection_result_(captive_portal::RESULT_INTERNET_CONNECTED), | 179 last_detection_result_(captive_portal::RESULT_INTERNET_CONNECTED), |
| 192 num_checks_with_same_result_(0), | 180 num_checks_with_same_result_(0), |
| 193 test_url_(captive_portal::CaptivePortalDetector::kDefaultURL) { | 181 test_url_(captive_portal::CaptivePortalDetector::kDefaultURL), |
| 182 tick_clock_for_testing_(clock_for_testing) { |
| 194 // The order matters here: | 183 // The order matters here: |
| 195 // |resolve_errors_with_web_service_| must be initialized and |backoff_entry_| | 184 // |resolve_errors_with_web_service_| must be initialized and |backoff_entry_| |
| 196 // created before the call to UpdateEnabledState. | 185 // created before the call to UpdateEnabledState. |
| 197 resolve_errors_with_web_service_.Init( | 186 resolve_errors_with_web_service_.Init( |
| 198 prefs::kAlternateErrorPagesEnabled, | 187 prefs::kAlternateErrorPagesEnabled, |
| 199 profile_->GetPrefs(), | 188 profile_->GetPrefs(), |
| 200 base::Bind(&CaptivePortalService::UpdateEnabledState, | 189 base::Bind(&CaptivePortalService::UpdateEnabledState, |
| 201 base::Unretained(this))); | 190 base::Unretained(this))); |
| 202 ResetBackoffEntry(last_detection_result_); | 191 ResetBackoffEntry(last_detection_result_); |
| 203 | 192 |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 341 if (!enabled_ || result == captive_portal::RESULT_BEHIND_CAPTIVE_PORTAL) { | 330 if (!enabled_ || result == captive_portal::RESULT_BEHIND_CAPTIVE_PORTAL) { |
| 342 // Use the shorter time when the captive portal service is not enabled, or | 331 // Use the shorter time when the captive portal service is not enabled, or |
| 343 // behind a captive portal. | 332 // behind a captive portal. |
| 344 recheck_policy_.backoff_policy.initial_delay_ms = | 333 recheck_policy_.backoff_policy.initial_delay_ms = |
| 345 recheck_policy_.initial_backoff_portal_ms; | 334 recheck_policy_.initial_backoff_portal_ms; |
| 346 } else { | 335 } else { |
| 347 recheck_policy_.backoff_policy.initial_delay_ms = | 336 recheck_policy_.backoff_policy.initial_delay_ms = |
| 348 recheck_policy_.initial_backoff_no_portal_ms; | 337 recheck_policy_.initial_backoff_no_portal_ms; |
| 349 } | 338 } |
| 350 | 339 |
| 351 backoff_entry_.reset(new RecheckBackoffEntry(this)); | 340 backoff_entry_.reset(new net::BackoffEntry(&recheck_policy().backoff_policy, |
| 341 tick_clock_for_testing_)); |
| 352 } | 342 } |
| 353 | 343 |
| 354 void CaptivePortalService::UpdateEnabledState() { | 344 void CaptivePortalService::UpdateEnabledState() { |
| 355 DCHECK(CalledOnValidThread()); | 345 DCHECK(CalledOnValidThread()); |
| 356 bool enabled_before = enabled_; | 346 bool enabled_before = enabled_; |
| 357 enabled_ = testing_state_ != DISABLED_FOR_TESTING && | 347 enabled_ = testing_state_ != DISABLED_FOR_TESTING && |
| 358 resolve_errors_with_web_service_.GetValue(); | 348 resolve_errors_with_web_service_.GetValue(); |
| 359 | 349 |
| 360 if (testing_state_ != SKIP_OS_CHECK_FOR_TESTING && | 350 if (testing_state_ != SKIP_OS_CHECK_FOR_TESTING && |
| 361 testing_state_ != IGNORE_REQUESTS_FOR_TESTING && | 351 testing_state_ != IGNORE_REQUESTS_FOR_TESTING && |
| (...skipping 18 matching lines...) Expand all Loading... |
| 380 captive_portal_detector_.Cancel(); | 370 captive_portal_detector_.Cancel(); |
| 381 state_ = STATE_IDLE; | 371 state_ = STATE_IDLE; |
| 382 | 372 |
| 383 // Since a captive portal request was queued or running, something may be | 373 // Since a captive portal request was queued or running, something may be |
| 384 // expecting to receive a captive portal result. | 374 // expecting to receive a captive portal result. |
| 385 DetectCaptivePortal(); | 375 DetectCaptivePortal(); |
| 386 } | 376 } |
| 387 } | 377 } |
| 388 | 378 |
| 389 base::TimeTicks CaptivePortalService::GetCurrentTimeTicks() const { | 379 base::TimeTicks CaptivePortalService::GetCurrentTimeTicks() const { |
| 390 if (time_ticks_for_testing_.is_null()) | 380 if (tick_clock_for_testing_) |
| 381 return tick_clock_for_testing_->NowTicks(); |
| 382 else |
| 391 return base::TimeTicks::Now(); | 383 return base::TimeTicks::Now(); |
| 392 else | |
| 393 return time_ticks_for_testing_; | |
| 394 } | 384 } |
| 395 | 385 |
| 396 bool CaptivePortalService::DetectionInProgress() const { | 386 bool CaptivePortalService::DetectionInProgress() const { |
| 397 return state_ == STATE_CHECKING_FOR_PORTAL; | 387 return state_ == STATE_CHECKING_FOR_PORTAL; |
| 398 } | 388 } |
| 399 | 389 |
| 400 bool CaptivePortalService::TimerRunning() const { | 390 bool CaptivePortalService::TimerRunning() const { |
| 401 return check_captive_portal_timer_.IsRunning(); | 391 return check_captive_portal_timer_.IsRunning(); |
| 402 } | 392 } |
| OLD | NEW |