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/tick_clock.h" |
12 #include "base/time/time.h" | 13 #include "base/time/time.h" |
13 #include "base/timer/timer.h" | 14 #include "base/timer/timer.h" |
14 #include "components/captive_portal/captive_portal_detector.h" | 15 #include "components/captive_portal/captive_portal_detector.h" |
15 #include "components/keyed_service/core/keyed_service.h" | 16 #include "components/keyed_service/core/keyed_service.h" |
16 #include "net/base/backoff_entry.h" | 17 #include "net/base/backoff_entry.h" |
17 #include "url/gurl.h" | 18 #include "url/gurl.h" |
18 | 19 |
19 class Profile; | 20 class Profile; |
20 | 21 |
21 // Service that checks for captive portals when queried, and sends a | 22 // Service that checks for captive portals when queried, and sends a |
(...skipping 19 matching lines...) Expand all Loading... |
41 // The result of the second most recent captive portal check. | 42 // The result of the second most recent captive portal check. |
42 captive_portal::CaptivePortalResult previous_result; | 43 captive_portal::CaptivePortalResult previous_result; |
43 // The result of the most recent captive portal check. | 44 // The result of the most recent captive portal check. |
44 captive_portal::CaptivePortalResult result; | 45 captive_portal::CaptivePortalResult result; |
45 // Landing url of the captive portal check ping. If behind a captive portal, | 46 // Landing url of the captive portal check ping. If behind a captive portal, |
46 // this points to the login page. | 47 // this points to the login page. |
47 GURL landing_url; | 48 GURL landing_url; |
48 }; | 49 }; |
49 | 50 |
50 explicit CaptivePortalService(Profile* profile); | 51 explicit CaptivePortalService(Profile* profile); |
| 52 CaptivePortalService(Profile* profile, base::TickClock* clock_for_testing); |
51 ~CaptivePortalService() override; | 53 ~CaptivePortalService() override; |
52 | 54 |
53 // Triggers a check for a captive portal. If there's already a check in | 55 // Triggers a check for a captive portal. If there's already a check in |
54 // progress, does nothing. Throttles the rate at which requests are sent. | 56 // progress, does nothing. Throttles the rate at which requests are sent. |
55 // Always sends the result notification asynchronously. | 57 // Always sends the result notification asynchronously. |
56 void DetectCaptivePortal(); | 58 void DetectCaptivePortal(); |
57 | 59 |
58 // Returns the URL used for captive portal testing. When a captive portal is | 60 // Returns the URL used for captive portal testing. When a captive portal is |
59 // detected, this URL will take us to the captive portal landing page. | 61 // detected, this URL will take us to the captive portal landing page. |
60 const GURL& test_url() const { return test_url_; } | 62 const GURL& test_url() const { return test_url_; } |
(...skipping 11 matching lines...) Expand all Loading... |
72 // tests. Should be called before the service is created. | 74 // tests. Should be called before the service is created. |
73 static void set_state_for_testing(TestingState testing_state) { | 75 static void set_state_for_testing(TestingState testing_state) { |
74 testing_state_ = testing_state; | 76 testing_state_ = testing_state; |
75 } | 77 } |
76 static TestingState get_state_for_testing() { return testing_state_; } | 78 static TestingState get_state_for_testing() { return testing_state_; } |
77 | 79 |
78 private: | 80 private: |
79 friend class CaptivePortalServiceTest; | 81 friend class CaptivePortalServiceTest; |
80 friend class CaptivePortalBrowserTest; | 82 friend class CaptivePortalBrowserTest; |
81 | 83 |
82 // Subclass of BackoffEntry that uses the CaptivePortalService's | |
83 // GetCurrentTime function, for unit testing. | |
84 class RecheckBackoffEntry; | |
85 | |
86 enum State { | 84 enum State { |
87 // No check is running or pending. | 85 // No check is running or pending. |
88 STATE_IDLE, | 86 STATE_IDLE, |
89 // The timer to check for a captive portal is running. | 87 // The timer to check for a captive portal is running. |
90 STATE_TIMER_RUNNING, | 88 STATE_TIMER_RUNNING, |
91 // There's an outstanding HTTP request to check for a captive portal. | 89 // There's an outstanding HTTP request to check for a captive portal. |
92 STATE_CHECKING_FOR_PORTAL, | 90 STATE_CHECKING_FOR_PORTAL, |
93 }; | 91 }; |
94 | 92 |
95 // Contains all the information about the minimum time allowed between two | 93 // Contains all the information about the minimum time allowed between two |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 void ResetBackoffEntry(captive_portal::CaptivePortalResult result); | 129 void ResetBackoffEntry(captive_portal::CaptivePortalResult result); |
132 | 130 |
133 // Updates |enabled_| based on command line flags and Profile preferences, | 131 // Updates |enabled_| based on command line flags and Profile preferences, |
134 // and sets |state_| to STATE_NONE if it's false. | 132 // and sets |state_| to STATE_NONE if it's false. |
135 // TODO(mmenke): Figure out on which platforms, if any, should not use | 133 // TODO(mmenke): Figure out on which platforms, if any, should not use |
136 // automatic captive portal detection. Currently it's enabled | 134 // automatic captive portal detection. Currently it's enabled |
137 // on all platforms, though this code is not compiled on | 135 // on all platforms, though this code is not compiled on |
138 // Android, since it lacks the Browser class. | 136 // Android, since it lacks the Browser class. |
139 void UpdateEnabledState(); | 137 void UpdateEnabledState(); |
140 | 138 |
141 // Returns the current TimeTicks. | |
142 base::TimeTicks GetCurrentTimeTicks() const; | 139 base::TimeTicks GetCurrentTimeTicks() const; |
143 | 140 |
144 bool DetectionInProgress() const; | 141 bool DetectionInProgress() const; |
145 | 142 |
146 // Returns true if the timer to try and detect a captive portal is running. | 143 // Returns true if the timer to try and detect a captive portal is running. |
147 bool TimerRunning() const; | 144 bool TimerRunning() const; |
148 | 145 |
149 State state() const { return state_; } | 146 State state() const { return state_; } |
150 | 147 |
151 RecheckPolicy& recheck_policy() { return recheck_policy_; } | 148 RecheckPolicy& recheck_policy() { return recheck_policy_; } |
152 | 149 |
153 void set_test_url(const GURL& test_url) { test_url_ = test_url; } | 150 void set_test_url(const GURL& test_url) { test_url_ = test_url; } |
154 | 151 |
155 // Sets current test time ticks. Used by unit tests. | |
156 void set_time_ticks_for_testing(const base::TimeTicks& time_ticks) { | |
157 time_ticks_for_testing_ = time_ticks; | |
158 } | |
159 | |
160 // Advances current test time ticks. Used by unit tests. | |
161 void advance_time_ticks_for_testing(const base::TimeDelta& delta) { | |
162 time_ticks_for_testing_ += delta; | |
163 } | |
164 | |
165 // The profile that owns this CaptivePortalService. | 152 // The profile that owns this CaptivePortalService. |
166 Profile* profile_; | 153 Profile* profile_; |
167 | 154 |
168 State state_; | 155 State state_; |
169 | 156 |
170 // Detector for checking active network for a portal state. | 157 // Detector for checking active network for a portal state. |
171 captive_portal::CaptivePortalDetector captive_portal_detector_; | 158 captive_portal::CaptivePortalDetector captive_portal_detector_; |
172 | 159 |
173 // True if the service is enabled. When not enabled, all checks will return | 160 // True if the service is enabled. When not enabled, all checks will return |
174 // RESULT_INTERNET_CONNECTED. | 161 // RESULT_INTERNET_CONNECTED. |
(...skipping 25 matching lines...) Expand all Loading... |
200 | 187 |
201 // The pref member for whether navigation errors should be resolved with a web | 188 // The pref member for whether navigation errors should be resolved with a web |
202 // service. Actually called "alternate_error_pages", since it's also used for | 189 // service. Actually called "alternate_error_pages", since it's also used for |
203 // the Link Doctor. | 190 // the Link Doctor. |
204 BooleanPrefMember resolve_errors_with_web_service_; | 191 BooleanPrefMember resolve_errors_with_web_service_; |
205 | 192 |
206 base::OneShotTimer<CaptivePortalService> check_captive_portal_timer_; | 193 base::OneShotTimer<CaptivePortalService> check_captive_portal_timer_; |
207 | 194 |
208 static TestingState testing_state_; | 195 static TestingState testing_state_; |
209 | 196 |
210 // Test time ticks used by unit tests. | 197 // Test tick clock used by unit tests. |
211 base::TimeTicks time_ticks_for_testing_; | 198 base::TickClock* tick_clock_for_testing_; // Not owned. |
212 | 199 |
213 DISALLOW_COPY_AND_ASSIGN(CaptivePortalService); | 200 DISALLOW_COPY_AND_ASSIGN(CaptivePortalService); |
214 }; | 201 }; |
215 | 202 |
216 #endif // CHROME_BROWSER_CAPTIVE_PORTAL_CAPTIVE_PORTAL_SERVICE_H_ | 203 #endif // CHROME_BROWSER_CAPTIVE_PORTAL_CAPTIVE_PORTAL_SERVICE_H_ |
OLD | NEW |