| 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 #ifndef CHROME_BROWSER_CAPTIVE_PORTAL_CAPTIVE_PORTAL_SERVICE_H_ | 5 #ifndef CHROME_BROWSER_CAPTIVE_PORTAL_CAPTIVE_PORTAL_SERVICE_H_ |
| 6 #define CHROME_BROWSER_CAPTIVE_PORTAL_CAPTIVE_PORTAL_SERVICE_H_ | 6 #define CHROME_BROWSER_CAPTIVE_PORTAL_CAPTIVE_PORTAL_SERVICE_H_ |
| 7 | 7 |
| 8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/prefs/pref_member.h" | 10 #include "base/prefs/pref_member.h" |
| 11 #include "base/threading/non_thread_safe.h" | 11 #include "base/threading/non_thread_safe.h" |
| 12 #include "base/time/time.h" | 12 #include "base/time/time.h" |
| 13 #include "base/timer/timer.h" | 13 #include "base/timer/timer.h" |
| 14 #include "chrome/browser/captive_portal/captive_portal_detector.h" | 14 #include "components/captive_portal/captive_portal_detector.h" |
| 15 #include "components/keyed_service/core/keyed_service.h" | 15 #include "components/keyed_service/core/keyed_service.h" |
| 16 #include "net/base/backoff_entry.h" | 16 #include "net/base/backoff_entry.h" |
| 17 #include "url/gurl.h" | 17 #include "url/gurl.h" |
| 18 | 18 |
| 19 class Profile; | 19 class Profile; |
| 20 | 20 |
| 21 namespace captive_portal { | |
| 22 | |
| 23 // Service that checks for captive portals when queried, and sends a | 21 // Service that checks for captive portals when queried, and sends a |
| 24 // NOTIFICATION_CAPTIVE_PORTAL_CHECK_RESULT with the Profile as the source and | 22 // NOTIFICATION_CAPTIVE_PORTAL_CHECK_RESULT with the Profile as the source and |
| 25 // a CaptivePortalService::Results as the details. | 23 // a CaptivePortalService::Results as the details. |
| 26 // | 24 // |
| 27 // Captive portal checks are rate-limited. The CaptivePortalService may only | 25 // Captive portal checks are rate-limited. The CaptivePortalService may only |
| 28 // be accessed on the UI thread. | 26 // be accessed on the UI thread. |
| 29 // Design doc: https://docs.google.com/document/d/1k-gP2sswzYNvryu9NcgN7q5XrsMlU
dlUdoW9WRaEmfM/edit | 27 // Design doc: https://docs.google.com/document/d/1k-gP2sswzYNvryu9NcgN7q5XrsMlU
dlUdoW9WRaEmfM/edit |
| 30 class CaptivePortalService : public KeyedService, public base::NonThreadSafe { | 28 class CaptivePortalService : public KeyedService, public base::NonThreadSafe { |
| 31 public: | 29 public: |
| 32 enum TestingState { | 30 enum TestingState { |
| 33 NOT_TESTING, | 31 NOT_TESTING, |
| 34 DISABLED_FOR_TESTING, // The service is always disabled. | 32 DISABLED_FOR_TESTING, // The service is always disabled. |
| 35 SKIP_OS_CHECK_FOR_TESTING // The service can be enabled even if the OS has | 33 SKIP_OS_CHECK_FOR_TESTING // The service can be enabled even if the OS has |
| 36 // native captive portal detection. | 34 // native captive portal detection. |
| 37 }; | 35 }; |
| 38 | 36 |
| 39 // The details sent via a NOTIFICATION_CAPTIVE_PORTAL_CHECK_RESULT. | 37 // The details sent via a NOTIFICATION_CAPTIVE_PORTAL_CHECK_RESULT. |
| 40 struct Results { | 38 struct Results { |
| 41 // The result of the second most recent captive portal check. | 39 // The result of the second most recent captive portal check. |
| 42 Result previous_result; | 40 captive_portal::CaptivePortalResult previous_result; |
| 43 // The result of the most recent captive portal check. | 41 // The result of the most recent captive portal check. |
| 44 Result result; | 42 captive_portal::CaptivePortalResult result; |
| 45 }; | 43 }; |
| 46 | 44 |
| 47 explicit CaptivePortalService(Profile* profile); | 45 explicit CaptivePortalService(Profile* profile); |
| 48 virtual ~CaptivePortalService(); | 46 virtual ~CaptivePortalService(); |
| 49 | 47 |
| 50 // Triggers a check for a captive portal. If there's already a check in | 48 // Triggers a check for a captive portal. If there's already a check in |
| 51 // progress, does nothing. Throttles the rate at which requests are sent. | 49 // progress, does nothing. Throttles the rate at which requests are sent. |
| 52 // Always sends the result notification asynchronously. | 50 // Always sends the result notification asynchronously. |
| 53 void DetectCaptivePortal(); | 51 void DetectCaptivePortal(); |
| 54 | 52 |
| 55 // Returns the URL used for captive portal testing. When a captive portal is | 53 // Returns the URL used for captive portal testing. When a captive portal is |
| 56 // detected, this URL will take us to the captive portal landing page. | 54 // detected, this URL will take us to the captive portal landing page. |
| 57 const GURL& test_url() const { return test_url_; } | 55 const GURL& test_url() const { return test_url_; } |
| 58 | 56 |
| 59 // Result of the most recent captive portal check. | 57 // Result of the most recent captive portal check. |
| 60 Result last_detection_result() const { return last_detection_result_; } | 58 captive_portal::CaptivePortalResult last_detection_result() const { |
| 59 return last_detection_result_; |
| 60 } |
| 61 | 61 |
| 62 // Whether or not the CaptivePortalService is enabled. When disabled, all | 62 // Whether or not the CaptivePortalService is enabled. When disabled, all |
| 63 // checks return INTERNET_CONNECTED. | 63 // checks return INTERNET_CONNECTED. |
| 64 bool enabled() const { return enabled_; } | 64 bool enabled() const { return enabled_; } |
| 65 | 65 |
| 66 // Used to disable captive portal detection so it doesn't interfere with | 66 // Used to disable captive portal detection so it doesn't interfere with |
| 67 // tests. Should be called before the service is created. | 67 // tests. Should be called before the service is created. |
| 68 static void set_state_for_testing(TestingState testing_state) { | 68 static void set_state_for_testing(TestingState testing_state) { |
| 69 testing_state_ = testing_state; | 69 testing_state_ = testing_state; |
| 70 } | 70 } |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 104 | 104 |
| 105 net::BackoffEntry::Policy backoff_policy; | 105 net::BackoffEntry::Policy backoff_policy; |
| 106 }; | 106 }; |
| 107 | 107 |
| 108 // Initiates a captive portal check, without any throttling. If the service | 108 // Initiates a captive portal check, without any throttling. If the service |
| 109 // is disabled, just acts like there's an Internet connection. | 109 // is disabled, just acts like there's an Internet connection. |
| 110 void DetectCaptivePortalInternal(); | 110 void DetectCaptivePortalInternal(); |
| 111 | 111 |
| 112 // Called by CaptivePortalDetector when detection completes. | 112 // Called by CaptivePortalDetector when detection completes. |
| 113 void OnPortalDetectionCompleted( | 113 void OnPortalDetectionCompleted( |
| 114 const CaptivePortalDetector::Results& results); | 114 const captive_portal::CaptivePortalDetector::Results& results); |
| 115 | 115 |
| 116 // KeyedService: | 116 // KeyedService: |
| 117 virtual void Shutdown() OVERRIDE; | 117 virtual void Shutdown() OVERRIDE; |
| 118 | 118 |
| 119 // Called when a captive portal check completes. Passes the result to all | 119 // Called when a captive portal check completes. Passes the result to all |
| 120 // observers. | 120 // observers. |
| 121 void OnResult(Result result); | 121 void OnResult(captive_portal::CaptivePortalResult result); |
| 122 | 122 |
| 123 // Updates BackoffEntry::Policy and creates a new BackoffEntry, which | 123 // Updates BackoffEntry::Policy and creates a new BackoffEntry, which |
| 124 // resets the count used for throttling. | 124 // resets the count used for throttling. |
| 125 void ResetBackoffEntry(Result result); | 125 void ResetBackoffEntry(captive_portal::CaptivePortalResult result); |
| 126 | 126 |
| 127 // Updates |enabled_| based on command line flags and Profile preferences, | 127 // Updates |enabled_| based on command line flags and Profile preferences, |
| 128 // and sets |state_| to STATE_NONE if it's false. | 128 // and sets |state_| to STATE_NONE if it's false. |
| 129 // TODO(mmenke): Figure out on which platforms, if any, should not use | 129 // TODO(mmenke): Figure out on which platforms, if any, should not use |
| 130 // automatic captive portal detection. Currently it's enabled | 130 // automatic captive portal detection. Currently it's enabled |
| 131 // on all platforms, though this code is not compiled on | 131 // on all platforms, though this code is not compiled on |
| 132 // Android, since it lacks the Browser class. | 132 // Android, since it lacks the Browser class. |
| 133 void UpdateEnabledState(); | 133 void UpdateEnabledState(); |
| 134 | 134 |
| 135 // Returns the current TimeTicks. | 135 // Returns the current TimeTicks. |
| (...skipping 19 matching lines...) Expand all Loading... |
| 155 void advance_time_ticks_for_testing(const base::TimeDelta& delta) { | 155 void advance_time_ticks_for_testing(const base::TimeDelta& delta) { |
| 156 time_ticks_for_testing_ += delta; | 156 time_ticks_for_testing_ += delta; |
| 157 } | 157 } |
| 158 | 158 |
| 159 // The profile that owns this CaptivePortalService. | 159 // The profile that owns this CaptivePortalService. |
| 160 Profile* profile_; | 160 Profile* profile_; |
| 161 | 161 |
| 162 State state_; | 162 State state_; |
| 163 | 163 |
| 164 // Detector for checking active network for a portal state. | 164 // Detector for checking active network for a portal state. |
| 165 CaptivePortalDetector captive_portal_detector_; | 165 captive_portal::CaptivePortalDetector captive_portal_detector_; |
| 166 | 166 |
| 167 // True if the service is enabled. When not enabled, all checks will return | 167 // True if the service is enabled. When not enabled, all checks will return |
| 168 // RESULT_INTERNET_CONNECTED. | 168 // RESULT_INTERNET_CONNECTED. |
| 169 bool enabled_; | 169 bool enabled_; |
| 170 | 170 |
| 171 // The result of the most recent captive portal check. | 171 // The result of the most recent captive portal check. |
| 172 Result last_detection_result_; | 172 captive_portal::CaptivePortalResult last_detection_result_; |
| 173 | 173 |
| 174 // Number of sequential checks with the same captive portal result. | 174 // Number of sequential checks with the same captive portal result. |
| 175 int num_checks_with_same_result_; | 175 int num_checks_with_same_result_; |
| 176 | 176 |
| 177 // Time when |last_detection_result_| was first received. | 177 // Time when |last_detection_result_| was first received. |
| 178 base::TimeTicks first_check_time_with_same_result_; | 178 base::TimeTicks first_check_time_with_same_result_; |
| 179 | 179 |
| 180 // Time the last captive portal check completed. | 180 // Time the last captive portal check completed. |
| 181 base::TimeTicks last_check_time_; | 181 base::TimeTicks last_check_time_; |
| 182 | 182 |
| 183 // Policy for throttling portal checks. | 183 // Policy for throttling portal checks. |
| 184 RecheckPolicy recheck_policy_; | 184 RecheckPolicy recheck_policy_; |
| 185 | 185 |
| 186 // Implements behavior needed by |recheck_policy_|. Whenever there's a new | 186 // Implements behavior needed by |recheck_policy_|. Whenever there's a new |
| 187 // captive_portal::Result, BackoffEntry::Policy is updated and | 187 // captive_portal::CaptivePortalResult, BackoffEntry::Policy is updated and |
| 188 // |backoff_entry_| is recreated. Each check that returns the same Result | 188 // |backoff_entry_| is recreated. Each check that returns the same result |
| 189 // is considered a "failure", to trigger throttling. | 189 // is considered a "failure", to trigger throttling. |
| 190 scoped_ptr<net::BackoffEntry> backoff_entry_; | 190 scoped_ptr<net::BackoffEntry> backoff_entry_; |
| 191 | 191 |
| 192 // URL that returns a 204 response code when connected to the Internet. | 192 // URL that returns a 204 response code when connected to the Internet. |
| 193 GURL test_url_; | 193 GURL test_url_; |
| 194 | 194 |
| 195 // The pref member for whether navigation errors should be resolved with a web | 195 // The pref member for whether navigation errors should be resolved with a web |
| 196 // service. Actually called "alternate_error_pages", since it's also used for | 196 // service. Actually called "alternate_error_pages", since it's also used for |
| 197 // the Link Doctor. | 197 // the Link Doctor. |
| 198 BooleanPrefMember resolve_errors_with_web_service_; | 198 BooleanPrefMember resolve_errors_with_web_service_; |
| 199 | 199 |
| 200 base::OneShotTimer<CaptivePortalService> check_captive_portal_timer_; | 200 base::OneShotTimer<CaptivePortalService> check_captive_portal_timer_; |
| 201 | 201 |
| 202 static TestingState testing_state_; | 202 static TestingState testing_state_; |
| 203 | 203 |
| 204 // Test time ticks used by unit tests. | 204 // Test time ticks used by unit tests. |
| 205 base::TimeTicks time_ticks_for_testing_; | 205 base::TimeTicks time_ticks_for_testing_; |
| 206 | 206 |
| 207 DISALLOW_COPY_AND_ASSIGN(CaptivePortalService); | 207 DISALLOW_COPY_AND_ASSIGN(CaptivePortalService); |
| 208 }; | 208 }; |
| 209 | 209 |
| 210 } // namespace captive_portal | |
| 211 | |
| 212 #endif // CHROME_BROWSER_CAPTIVE_PORTAL_CAPTIVE_PORTAL_SERVICE_H_ | 210 #endif // CHROME_BROWSER_CAPTIVE_PORTAL_CAPTIVE_PORTAL_SERVICE_H_ |
| OLD | NEW |