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 class CaptivePortalService::RecheckTickClock : public base::TickClock { |
142 public: | 143 public: |
143 explicit RecheckBackoffEntry(CaptivePortalService* captive_portal_service) | 144 explicit RecheckTickClock(CaptivePortalService* captive_portal_service) |
144 : net::BackoffEntry( | 145 : captive_portal_service_(captive_portal_service) { |
145 &captive_portal_service->recheck_policy().backoff_policy), | 146 } |
146 captive_portal_service_(captive_portal_service) { | 147 |
| 148 base::TimeTicks NowTicks() override { |
| 149 return captive_portal_service_->NowTicks(); |
147 } | 150 } |
148 | 151 |
149 private: | 152 private: |
150 base::TimeTicks ImplGetTimeNow() const override { | |
151 return captive_portal_service_->GetCurrentTimeTicks(); | |
152 } | |
153 | |
154 CaptivePortalService* captive_portal_service_; | 153 CaptivePortalService* captive_portal_service_; |
155 | 154 |
156 DISALLOW_COPY_AND_ASSIGN(RecheckBackoffEntry); | 155 DISALLOW_COPY_AND_ASSIGN(RecheckTickClock); |
157 }; | 156 }; |
158 | 157 |
159 CaptivePortalService::RecheckPolicy::RecheckPolicy() | 158 CaptivePortalService::RecheckPolicy::RecheckPolicy() |
160 : initial_backoff_no_portal_ms(600 * 1000), | 159 : initial_backoff_no_portal_ms(600 * 1000), |
161 initial_backoff_portal_ms(20 * 1000) { | 160 initial_backoff_portal_ms(20 * 1000) { |
162 // Receiving a new Result is considered a success. All subsequent requests | 161 // 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 | 162 // 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: | 163 // means exponential backoff starts after getting a result N + 2 times: |
165 // +1 for the initial success, and +1 because N failures are ignored. | 164 // +1 for the initial success, and +1 because N failures are ignored. |
166 // | 165 // |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
253 | 252 |
254 void CaptivePortalService::OnPortalDetectionCompleted( | 253 void CaptivePortalService::OnPortalDetectionCompleted( |
255 const captive_portal::CaptivePortalDetector::Results& results) { | 254 const captive_portal::CaptivePortalDetector::Results& results) { |
256 DCHECK(CalledOnValidThread()); | 255 DCHECK(CalledOnValidThread()); |
257 DCHECK_EQ(STATE_CHECKING_FOR_PORTAL, state_); | 256 DCHECK_EQ(STATE_CHECKING_FOR_PORTAL, state_); |
258 DCHECK(!TimerRunning()); | 257 DCHECK(!TimerRunning()); |
259 DCHECK(enabled_); | 258 DCHECK(enabled_); |
260 | 259 |
261 CaptivePortalResult result = results.result; | 260 CaptivePortalResult result = results.result; |
262 const base::TimeDelta& retry_after_delta = results.retry_after_delta; | 261 const base::TimeDelta& retry_after_delta = results.retry_after_delta; |
263 base::TimeTicks now = GetCurrentTimeTicks(); | 262 base::TimeTicks now = NowTicks(); |
264 | 263 |
265 // Record histograms. | 264 // Record histograms. |
266 UMA_HISTOGRAM_ENUMERATION("CaptivePortal.DetectResult", | 265 UMA_HISTOGRAM_ENUMERATION("CaptivePortal.DetectResult", |
267 GetHistogramEntryForDetectionResult(results), | 266 GetHistogramEntryForDetectionResult(results), |
268 DETECTION_RESULT_COUNT); | 267 DETECTION_RESULT_COUNT); |
269 | 268 |
270 // If this isn't the first captive portal result, record stats. | 269 // If this isn't the first captive portal result, record stats. |
271 if (!last_check_time_.is_null()) { | 270 if (!last_check_time_.is_null()) { |
272 UMA_HISTOGRAM_LONG_TIMES("CaptivePortal.TimeBetweenChecks", | 271 UMA_HISTOGRAM_LONG_TIMES("CaptivePortal.TimeBetweenChecks", |
273 now - last_check_time_); | 272 now - last_check_time_); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
309 | 308 |
310 OnResult(result, results.landing_url); | 309 OnResult(result, results.landing_url); |
311 } | 310 } |
312 | 311 |
313 void CaptivePortalService::Shutdown() { | 312 void CaptivePortalService::Shutdown() { |
314 DCHECK(CalledOnValidThread()); | 313 DCHECK(CalledOnValidThread()); |
315 if (enabled_) { | 314 if (enabled_) { |
316 RecordRepeatHistograms( | 315 RecordRepeatHistograms( |
317 last_detection_result_, | 316 last_detection_result_, |
318 num_checks_with_same_result_, | 317 num_checks_with_same_result_, |
319 GetCurrentTimeTicks() - first_check_time_with_same_result_); | 318 NowTicks() - first_check_time_with_same_result_); |
320 } | 319 } |
321 } | 320 } |
322 | 321 |
323 void CaptivePortalService::OnResult(CaptivePortalResult result, | 322 void CaptivePortalService::OnResult(CaptivePortalResult result, |
324 const GURL& landing_url) { | 323 const GURL& landing_url) { |
325 DCHECK_EQ(STATE_CHECKING_FOR_PORTAL, state_); | 324 DCHECK_EQ(STATE_CHECKING_FOR_PORTAL, state_); |
326 state_ = STATE_IDLE; | 325 state_ = STATE_IDLE; |
327 | 326 |
328 Results results; | 327 Results results; |
329 results.previous_result = last_detection_result_; | 328 results.previous_result = last_detection_result_; |
(...skipping 11 matching lines...) Expand all Loading... |
341 if (!enabled_ || result == captive_portal::RESULT_BEHIND_CAPTIVE_PORTAL) { | 340 if (!enabled_ || result == captive_portal::RESULT_BEHIND_CAPTIVE_PORTAL) { |
342 // Use the shorter time when the captive portal service is not enabled, or | 341 // Use the shorter time when the captive portal service is not enabled, or |
343 // behind a captive portal. | 342 // behind a captive portal. |
344 recheck_policy_.backoff_policy.initial_delay_ms = | 343 recheck_policy_.backoff_policy.initial_delay_ms = |
345 recheck_policy_.initial_backoff_portal_ms; | 344 recheck_policy_.initial_backoff_portal_ms; |
346 } else { | 345 } else { |
347 recheck_policy_.backoff_policy.initial_delay_ms = | 346 recheck_policy_.backoff_policy.initial_delay_ms = |
348 recheck_policy_.initial_backoff_no_portal_ms; | 347 recheck_policy_.initial_backoff_no_portal_ms; |
349 } | 348 } |
350 | 349 |
351 backoff_entry_.reset(new RecheckBackoffEntry(this)); | 350 backoff_entry_.reset(new net::BackoffEntry( |
| 351 &recheck_policy().backoff_policy, |
| 352 this)); |
352 } | 353 } |
353 | 354 |
354 void CaptivePortalService::UpdateEnabledState() { | 355 void CaptivePortalService::UpdateEnabledState() { |
355 DCHECK(CalledOnValidThread()); | 356 DCHECK(CalledOnValidThread()); |
356 bool enabled_before = enabled_; | 357 bool enabled_before = enabled_; |
357 enabled_ = testing_state_ != DISABLED_FOR_TESTING && | 358 enabled_ = testing_state_ != DISABLED_FOR_TESTING && |
358 resolve_errors_with_web_service_.GetValue(); | 359 resolve_errors_with_web_service_.GetValue(); |
359 | 360 |
360 if (testing_state_ != SKIP_OS_CHECK_FOR_TESTING && | 361 if (testing_state_ != SKIP_OS_CHECK_FOR_TESTING && |
361 testing_state_ != IGNORE_REQUESTS_FOR_TESTING && | 362 testing_state_ != IGNORE_REQUESTS_FOR_TESTING && |
(...skipping 17 matching lines...) Expand all Loading... |
379 check_captive_portal_timer_.Stop(); | 380 check_captive_portal_timer_.Stop(); |
380 captive_portal_detector_.Cancel(); | 381 captive_portal_detector_.Cancel(); |
381 state_ = STATE_IDLE; | 382 state_ = STATE_IDLE; |
382 | 383 |
383 // Since a captive portal request was queued or running, something may be | 384 // Since a captive portal request was queued or running, something may be |
384 // expecting to receive a captive portal result. | 385 // expecting to receive a captive portal result. |
385 DetectCaptivePortal(); | 386 DetectCaptivePortal(); |
386 } | 387 } |
387 } | 388 } |
388 | 389 |
389 base::TimeTicks CaptivePortalService::GetCurrentTimeTicks() const { | 390 base::TimeTicks CaptivePortalService::NowTicks() { |
390 if (time_ticks_for_testing_.is_null()) | 391 if (time_ticks_for_testing_.is_null()) |
391 return base::TimeTicks::Now(); | 392 return base::TimeTicks::Now(); |
392 else | 393 else |
393 return time_ticks_for_testing_; | 394 return time_ticks_for_testing_; |
394 } | 395 } |
395 | 396 |
396 bool CaptivePortalService::DetectionInProgress() const { | 397 bool CaptivePortalService::DetectionInProgress() const { |
397 return state_ == STATE_CHECKING_FOR_PORTAL; | 398 return state_ == STATE_CHECKING_FOR_PORTAL; |
398 } | 399 } |
399 | 400 |
400 bool CaptivePortalService::TimerRunning() const { | 401 bool CaptivePortalService::TimerRunning() const { |
401 return check_captive_portal_timer_.IsRunning(); | 402 return check_captive_portal_timer_.IsRunning(); |
402 } | 403 } |
OLD | NEW |