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