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_CHROMEOS_NET_NETWORK_PORTAL_DETECTOR_H_ | 5 #ifndef CHROME_BROWSER_CHROMEOS_NET_NETWORK_PORTAL_DETECTOR_H_ |
6 #define CHROME_BROWSER_CHROMEOS_NET_NETWORK_PORTAL_DETECTOR_H_ | 6 #define CHROME_BROWSER_CHROMEOS_NET_NETWORK_PORTAL_DETECTOR_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/cancelable_callback.h" | |
10 #include "base/hash_tables.h" | 11 #include "base/hash_tables.h" |
11 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
12 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "base/memory/weak_ptr.h" | |
13 #include "base/observer_list.h" | 15 #include "base/observer_list.h" |
14 #include "base/threading/non_thread_safe.h" | 16 #include "base/threading/non_thread_safe.h" |
17 #include "base/time.h" | |
15 #include "chrome/browser/captive_portal/captive_portal_detector.h" | 18 #include "chrome/browser/captive_portal/captive_portal_detector.h" |
16 #include "chrome/browser/chromeos/cros/network_library.h" | 19 #include "chrome/browser/chromeos/cros/network_library.h" |
17 #include "googleurl/src/gurl.h" | 20 #include "googleurl/src/gurl.h" |
18 | 21 |
19 namespace net { | 22 namespace net { |
20 class URLRequestContextGetter; | 23 class URLRequestContextGetter; |
21 } | 24 } |
22 | 25 |
23 namespace chromeos { | 26 namespace chromeos { |
24 | 27 |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
70 | 73 |
71 private: | 74 private: |
72 friend class NetworkPortalDetectorTest; | 75 friend class NetworkPortalDetectorTest; |
73 | 76 |
74 typedef std::string NetworkId; | 77 typedef std::string NetworkId; |
75 typedef base::hash_map<NetworkId, CaptivePortalState> CaptivePortalStateMap; | 78 typedef base::hash_map<NetworkId, CaptivePortalState> CaptivePortalStateMap; |
76 | 79 |
77 enum State { | 80 enum State { |
78 // No portal check is running. | 81 // No portal check is running. |
79 STATE_IDLE, | 82 STATE_IDLE, |
83 // Waiting for portal check. | |
84 STATE_PORTAL_CHECK_PENDING, | |
80 // Portal check is in progress. | 85 // Portal check is in progress. |
81 STATE_CHECKING_FOR_PORTAL, | 86 STATE_CHECKING_FOR_PORTAL, |
82 // Portal check is in progress, but other portal detection request | |
83 // is pending. | |
84 STATE_CHECKING_FOR_PORTAL_NETWORK_CHANGED, | |
85 }; | 87 }; |
86 | 88 |
87 explicit NetworkPortalDetector( | 89 explicit NetworkPortalDetector( |
88 const scoped_refptr<net::URLRequestContextGetter>& request_context); | 90 const scoped_refptr<net::URLRequestContextGetter>& request_context); |
89 | 91 |
90 // Initiates Captive Portal detection. If currently portal detection | 92 // Initiates Captive Portal detection after |delay|. |
91 // in in progress, then delays portal detection. | 93 void DetectCaptivePortal(const base::TimeDelta& delay); |
92 void DetectCaptivePortal(); | 94 |
95 void DetectCaptivePortalTask(); | |
96 | |
97 void PortalDetectionTimeout(); | |
Nikita (slow)
2012/11/20 12:58:45
nit: please add short comment describing when this
ygorshenin1
2012/11/21 09:08:06
Done.
| |
98 | |
99 void CancelPortalDetection(); | |
93 | 100 |
94 // Called by CaptivePortalDetector when detection completes. | 101 // Called by CaptivePortalDetector when detection completes. |
95 void OnPortalDetectionCompleted( | 102 void OnPortalDetectionCompleted( |
96 const captive_portal::CaptivePortalDetector::Results& results); | 103 const captive_portal::CaptivePortalDetector::Results& results); |
97 | 104 |
105 // Returns true if we're waiting for portal check. | |
106 bool IsPortalCheckPending() const; | |
107 | |
98 // Returns true if portal check is in progress. | 108 // Returns true if portal check is in progress. |
99 bool IsCheckingForPortal() const; | 109 bool IsCheckingForPortal() const; |
100 | 110 |
101 // Stores captive portal state for a |network|. | 111 // Stores captive portal state for a |network|. |
102 void SetCaptivePortalState(const Network* network, | 112 void SetCaptivePortalState(const Network* network, |
103 CaptivePortalState state); | 113 CaptivePortalState state); |
104 | 114 |
105 // Notifies observers that portal state is changed for a |network|. | 115 // Notifies observers that portal state is changed for a |network|. |
106 void NotifyPortalStateChanged(const Network* network, | 116 void NotifyPortalStateChanged(const Network* network, |
107 CaptivePortalState state); | 117 CaptivePortalState state); |
108 | 118 |
119 // Returns the current TimeTicks. | |
120 base::TimeTicks GetCurrentTimeTicks() const; | |
121 | |
109 State state() { return state_; } | 122 State state() { return state_; } |
110 | 123 |
124 // Returns current number of portal detection attempts. | |
125 // Used by unit tests. | |
126 int attempt_count_for_testing() { return attempt_count_; } | |
127 | |
128 // Sets minimum time between consecutive portal checks for the same | |
129 // network. Used by unit tests. | |
130 void set_min_time_between_attempts_for_testing(const base::TimeDelta& delta) { | |
131 min_time_between_attempts_ = delta; | |
132 } | |
133 | |
134 // Sets portal detection timeout. Used by unit tests. | |
135 void set_request_timeout_for_testing(const base::TimeDelta& timeout) { | |
136 request_timeout_ = timeout; | |
137 } | |
138 | |
139 // Returns delay before next portal check. Used by unit tests. | |
140 const base::TimeDelta& next_attempt_delay_for_testing() { | |
141 return next_attempt_delay_; | |
142 } | |
143 | |
144 // Sets current test time ticks. Used by unit tests. | |
145 void set_time_ticks_for_testing(const base::TimeTicks& time_ticks) { | |
146 time_ticks_for_testing_ = time_ticks; | |
147 } | |
148 | |
149 // Advances current test time ticks. Used by unit tests. | |
150 void advance_time_ticks_for_testing(const base::TimeDelta& delta) { | |
151 time_ticks_for_testing_ += delta; | |
152 } | |
153 | |
111 std::string active_network_id_; | 154 std::string active_network_id_; |
112 State state_; | 155 State state_; |
113 CaptivePortalStateMap captive_portal_state_map_; | 156 CaptivePortalStateMap captive_portal_state_map_; |
114 ObserverList<Observer> observers_; | 157 ObserverList<Observer> observers_; |
115 | 158 |
159 base::CancelableClosure detection_task_; | |
160 base::CancelableClosure detection_timeout_; | |
161 | |
116 // URL that returns a 204 response code when connected to the Internet. | 162 // URL that returns a 204 response code when connected to the Internet. |
117 GURL test_url_; | 163 GURL test_url_; |
118 | 164 |
119 // Detector for checking active network for a portal state. | 165 // Detector for checking active network for a portal state. |
120 scoped_ptr<captive_portal::CaptivePortalDetector> captive_portal_detector_; | 166 scoped_ptr<captive_portal::CaptivePortalDetector> captive_portal_detector_; |
167 | |
168 base::WeakPtrFactory<NetworkPortalDetector> weak_ptr_factory_; | |
169 | |
170 // Number of portal detection attemps for an active network. | |
171 int attempt_count_; | |
172 | |
173 // Minimum time between consecutive portal checks for the same | |
174 // active network. | |
175 base::TimeDelta min_time_between_attempts_; | |
176 | |
177 // Start time of portal detection attempt. | |
178 base::TimeTicks attempt_start_time_; | |
179 | |
180 // Timeout for a portal detection. | |
181 base::TimeDelta request_timeout_; | |
182 | |
183 // Delay before next portal detection. | |
184 base::TimeDelta next_attempt_delay_; | |
185 | |
186 // Test time ticks used by unit tests. | |
187 base::TimeTicks time_ticks_for_testing_; | |
188 | |
189 DISALLOW_COPY_AND_ASSIGN(NetworkPortalDetector); | |
121 }; | 190 }; |
122 | 191 |
123 } // namespace chromeos | 192 } // namespace chromeos |
124 | 193 |
125 #endif // CHROME_BROWSER_CHROMEOS_NET_NETWORK_PORTAL_DETECTOR_H_ | 194 #endif // CHROME_BROWSER_CHROMEOS_NET_NETWORK_PORTAL_DETECTOR_H_ |
OLD | NEW |