OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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_IMPL_H_ | 5 #ifndef CHROME_BROWSER_CHROMEOS_NET_NETWORK_PORTAL_DETECTOR_IMPL_H_ |
6 #define CHROME_BROWSER_CHROMEOS_NET_NETWORK_PORTAL_DETECTOR_IMPL_H_ | 6 #define CHROME_BROWSER_CHROMEOS_NET_NETWORK_PORTAL_DETECTOR_IMPL_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/cancelable_callback.h" | 11 #include "base/cancelable_callback.h" |
12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
13 #include "base/containers/hash_tables.h" | 13 #include "base/containers/hash_tables.h" |
14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
16 #include "base/memory/weak_ptr.h" | 16 #include "base/memory/weak_ptr.h" |
17 #include "base/observer_list.h" | 17 #include "base/observer_list.h" |
18 #include "base/threading/non_thread_safe.h" | 18 #include "base/threading/non_thread_safe.h" |
19 #include "base/time/time.h" | 19 #include "base/time/time.h" |
20 #include "chrome/browser/captive_portal/captive_portal_detector.h" | 20 #include "chrome/browser/captive_portal/captive_portal_detector.h" |
21 #include "chrome/browser/chromeos/net/network_portal_detector.h" | 21 #include "chrome/browser/chromeos/net/network_portal_detector.h" |
22 #include "chrome/browser/chromeos/net/network_portal_detector_strategy.h" | |
22 #include "chrome/browser/chromeos/net/network_portal_notification_controller.h" | 23 #include "chrome/browser/chromeos/net/network_portal_notification_controller.h" |
23 #include "chromeos/network/network_state_handler_observer.h" | 24 #include "chromeos/network/network_state_handler_observer.h" |
24 #include "content/public/browser/notification_observer.h" | 25 #include "content/public/browser/notification_observer.h" |
25 #include "content/public/browser/notification_registrar.h" | 26 #include "content/public/browser/notification_registrar.h" |
26 #include "net/url_request/url_fetcher.h" | 27 #include "net/url_request/url_fetcher.h" |
27 #include "url/gurl.h" | 28 #include "url/gurl.h" |
28 | 29 |
29 namespace net { | 30 namespace net { |
30 class URLRequestContextGetter; | 31 class URLRequestContextGetter; |
31 } | 32 } |
32 | 33 |
33 namespace chromeos { | 34 namespace chromeos { |
34 | 35 |
35 class NetworkState; | 36 class NetworkState; |
36 | 37 |
37 // This class handles all notifications about network changes from | 38 // This class handles all notifications about network changes from |
38 // NetworkStateHandler and delegates portal detection for the default | 39 // NetworkStateHandler and delegates portal detection for the default |
39 // network to CaptivePortalService. | 40 // network to CaptivePortalService. |
40 class NetworkPortalDetectorImpl | 41 class NetworkPortalDetectorImpl |
41 : public NetworkPortalDetector, | 42 : public NetworkPortalDetector, |
42 public base::NonThreadSafe, | 43 public base::NonThreadSafe, |
43 public chromeos::NetworkStateHandlerObserver, | 44 public chromeos::NetworkStateHandlerObserver, |
44 public content::NotificationObserver { | 45 public content::NotificationObserver, |
46 public PortalDetectorStrategy::Delegate { | |
45 public: | 47 public: |
46 static const char kDetectionResultHistogram[]; | 48 static const char kDetectionResultHistogram[]; |
47 static const char kDetectionDurationHistogram[]; | 49 static const char kDetectionDurationHistogram[]; |
48 static const char kShillOnlineHistogram[]; | 50 static const char kShillOnlineHistogram[]; |
49 static const char kShillPortalHistogram[]; | 51 static const char kShillPortalHistogram[]; |
50 static const char kShillOfflineHistogram[]; | 52 static const char kShillOfflineHistogram[]; |
51 | 53 |
52 explicit NetworkPortalDetectorImpl( | 54 explicit NetworkPortalDetectorImpl( |
53 const scoped_refptr<net::URLRequestContextGetter>& request_context); | 55 const scoped_refptr<net::URLRequestContextGetter>& request_context); |
54 virtual ~NetworkPortalDetectorImpl(); | 56 virtual ~NetworkPortalDetectorImpl(); |
55 | 57 |
56 // NetworkPortalDetector implementation: | 58 // NetworkPortalDetector implementation: |
57 virtual void AddObserver(Observer* observer) OVERRIDE; | 59 virtual void AddObserver(Observer* observer) OVERRIDE; |
58 virtual void AddAndFireObserver(Observer* observer) OVERRIDE; | 60 virtual void AddAndFireObserver(Observer* observer) OVERRIDE; |
59 virtual void RemoveObserver(Observer* observer) OVERRIDE; | 61 virtual void RemoveObserver(Observer* observer) OVERRIDE; |
60 virtual CaptivePortalState GetCaptivePortalState( | 62 virtual CaptivePortalState GetCaptivePortalState( |
61 const std::string& service_path) OVERRIDE; | 63 const std::string& service_path) OVERRIDE; |
62 virtual bool IsEnabled() OVERRIDE; | 64 virtual bool IsEnabled() OVERRIDE; |
63 virtual void Enable(bool start_detection) OVERRIDE; | 65 virtual void Enable(bool start_detection) OVERRIDE; |
64 virtual bool StartDetectionIfIdle() OVERRIDE; | 66 virtual bool StartDetectionIfIdle() OVERRIDE; |
65 virtual void EnableLazyDetection() OVERRIDE; | 67 virtual void EnableErrorScreenStrategy() OVERRIDE; |
66 virtual void DisableLazyDetection() OVERRIDE; | 68 virtual void DisableErrorScreenStrategy() OVERRIDE; |
67 | 69 |
68 // NetworkStateHandlerObserver implementation: | 70 // NetworkStateHandlerObserver implementation: |
69 virtual void DefaultNetworkChanged(const NetworkState* network) OVERRIDE; | 71 virtual void DefaultNetworkChanged(const NetworkState* network) OVERRIDE; |
70 | 72 |
73 // PortalDetectorStrategy::Delegate implementation: | |
74 virtual base::TimeTicks GetCurrentTimeTicks() OVERRIDE; | |
75 | |
71 private: | 76 private: |
72 friend class NetworkPortalDetectorImplTest; | 77 friend class NetworkPortalDetectorImplTest; |
73 | 78 |
74 typedef std::string NetworkId; | 79 typedef std::string NetworkId; |
75 typedef base::hash_map<NetworkId, CaptivePortalState> CaptivePortalStateMap; | 80 typedef base::hash_map<NetworkId, CaptivePortalState> CaptivePortalStateMap; |
76 | 81 |
77 enum State { | 82 enum State { |
78 // No portal check is running. | 83 // No portal check is running. |
79 STATE_IDLE = 0, | 84 STATE_IDLE = 0, |
80 // Waiting for portal check. | 85 // Waiting for portal check. |
81 STATE_PORTAL_CHECK_PENDING, | 86 STATE_PORTAL_CHECK_PENDING, |
82 // Portal check is in progress. | 87 // Portal check is in progress. |
83 STATE_CHECKING_FOR_PORTAL, | 88 STATE_CHECKING_FOR_PORTAL, |
84 }; | 89 }; |
85 | 90 |
86 // Basic unit used in detection timeout computation. | 91 // Starts detection process. |
87 static const int kBaseRequestTimeoutSec = 5; | 92 void StartDetection(); |
88 | 93 |
89 // Single detection attempt timeout in lazy mode. | 94 // Cancels whole detection process. |
90 static const int kLazyRequestTimeoutSec = 15; | 95 void CancelDetection(); |
Denis Kuznetsov (DE-MUC)
2014/03/05 16:59:09
StopDetection() ?
ygorshenin1
2014/03/06 11:59:33
Done.
| |
91 | 96 |
92 // Internal predicate which describes set of states from which | 97 // Internal predicate which describes set of states from which |
93 // DetectCaptivePortal() can be called. | 98 // DetectCaptivePortal() can be called. |
94 bool CanPerformDetection() const; | 99 bool CanPerformAttempt() const; |
95 | 100 |
96 // Initiates Captive Portal detection after |delay|. | 101 // Initiates Captive Portal detection attempt after |delay|. |
97 // CanPerformDetection() *must* be kept before call to this method. | 102 // CanPerformAttempt() *must* be kept before call to this method. |
Denis Kuznetsov (DE-MUC)
2014/03/05 16:59:09
Maybe reword it to "You should check CanPerformAtt
ygorshenin1
2014/03/06 11:59:33
Done.
| |
98 void DetectCaptivePortal(const base::TimeDelta& delay); | 103 void ScheduleAttempt(const base::TimeDelta& delay); |
99 | 104 |
100 void DetectCaptivePortalTask(); | 105 // Starts detection attempt. |
106 void StartAttempt(); | |
101 | 107 |
102 // Called when portal check is timed out. Cancels portal check and | 108 // Called when portal check is timed out. Cancels portal check and calls |
103 // calls OnPortalDetectionCompleted() with RESULT_NO_RESPONSE as | 109 // OnPortalDetectionCompleted() with RESULT_NO_RESPONSE as a result. |
104 // a result. | 110 void OnAttemptTimeout(); |
105 void PortalDetectionTimeout(); | |
106 | 111 |
107 void CancelPortalDetection(); | 112 // Called by CaptivePortalDetector when detection attempt completes. |
108 | 113 void OnAttemptCompleted( |
109 // Called by CaptivePortalDetector when detection completes. | |
110 void OnPortalDetectionCompleted( | |
111 const captive_portal::CaptivePortalDetector::Results& results); | 114 const captive_portal::CaptivePortalDetector::Results& results); |
112 | 115 |
113 // Tries to perform portal detection in "lazy" mode. Does nothing in | |
114 // the case of already pending/processing detection request. | |
115 void TryLazyDetection(); | |
116 | |
117 // content::NotificationObserver implementation: | 116 // content::NotificationObserver implementation: |
118 virtual void Observe(int type, | 117 virtual void Observe(int type, |
119 const content::NotificationSource& source, | 118 const content::NotificationSource& source, |
120 const content::NotificationDetails& details) OVERRIDE; | 119 const content::NotificationDetails& details) OVERRIDE; |
121 | 120 |
122 // Returns true if we're waiting for portal check. | 121 // Stores captive portal state for a |network| and notifies observers. |
123 bool IsPortalCheckPending() const; | 122 void OnDetectionCompleted(const NetworkState* network, |
124 | 123 const CaptivePortalState& results); |
125 // Returns true if portal check is in progress. | |
126 bool IsCheckingForPortal() const; | |
127 | |
128 // Stores captive portal state for a |network|. | |
129 void SetCaptivePortalState(const NetworkState* network, | |
130 const CaptivePortalState& results); | |
131 | 124 |
132 // Notifies observers that portal detection is completed for a |network|. | 125 // Notifies observers that portal detection is completed for a |network|. |
133 void NotifyPortalDetectionCompleted(const NetworkState* network, | 126 void NotifyDetectionCompleted(const NetworkState* network, |
134 const CaptivePortalState& state); | 127 const CaptivePortalState& state); |
135 | |
136 // Returns the current TimeTicks. | |
137 base::TimeTicks GetCurrentTimeTicks() const; | |
138 | 128 |
139 State state() const { return state_; } | 129 State state() const { return state_; } |
140 | 130 |
141 bool lazy_detection_enabled() const { return lazy_detection_enabled_; } | 131 bool is_idle() const { |
142 | 132 return state_ == STATE_IDLE; |
143 // Returns current number of portal detection attempts. | 133 } |
144 // Used by unit tests. | 134 bool is_portal_check_pending() const { |
145 int attempt_count_for_testing() const { return attempt_count_; } | 135 return state_ == STATE_PORTAL_CHECK_PENDING; |
146 | 136 } |
147 // Sets current number of detection attempts. | 137 bool is_checking_for_portal() const { |
148 // Used by unit tests. | 138 return state_ == STATE_CHECKING_FOR_PORTAL; |
149 void set_attempt_count_for_testing(int attempt_count) { | |
150 attempt_count_ = attempt_count; | |
151 } | 139 } |
152 | 140 |
153 // Sets minimum time between consecutive portal checks for the same | 141 // Sets minimum time between consecutive detection attempts for the |
154 // network. Used by unit tests. | 142 // same network. Used by unit tests. |
155 void set_min_time_between_attempts_for_testing(const base::TimeDelta& delta) { | 143 void set_min_time_between_attempts_for_testing(const base::TimeDelta& delta) { |
156 min_time_between_attempts_ = delta; | 144 min_time_between_attempts_ = delta; |
157 } | 145 } |
158 | 146 |
159 // Sets default interval between consecutive portal checks for a | |
160 // network in portal state. Used by unit tests. | |
161 void set_lazy_check_interval_for_testing(const base::TimeDelta& delta) { | |
162 lazy_check_interval_ = delta; | |
163 } | |
164 | |
165 // Sets portal detection timeout. Used by unit tests. | |
166 void set_request_timeout_for_testing(const base::TimeDelta& timeout) { | |
167 request_timeout_for_testing_ = timeout; | |
168 request_timeout_for_testing_initialized_ = true; | |
169 } | |
170 | |
171 // Returns delay before next portal check. Used by unit tests. | 147 // Returns delay before next portal check. Used by unit tests. |
172 const base::TimeDelta& next_attempt_delay_for_testing() const { | 148 const base::TimeDelta& next_attempt_delay_for_testing() const { |
173 return next_attempt_delay_; | 149 return next_attempt_delay_; |
174 } | 150 } |
175 | 151 |
152 // Returns true if attempt timeout callback isn't fired or | |
153 // cancelled. | |
154 bool AttemptTimeoutIsCancelledForTesting() const; | |
155 | |
156 // Record detection stats such as detection duration and detection | |
157 // result in UMA. | |
158 void RecordDetectionStats(const NetworkState* network, | |
159 CaptivePortalStatus status); | |
160 | |
176 // Sets current test time ticks. Used by unit tests. | 161 // Sets current test time ticks. Used by unit tests. |
177 void set_time_ticks_for_testing(const base::TimeTicks& time_ticks) { | 162 void set_time_ticks_for_testing(const base::TimeTicks& time_ticks) { |
178 time_ticks_for_testing_ = time_ticks; | 163 time_ticks_for_testing_ = time_ticks; |
179 } | 164 } |
180 | 165 |
181 // Advances current test time ticks. Used by unit tests. | 166 // Advances current test time ticks. Used by unit tests. |
182 void advance_time_ticks_for_testing(const base::TimeDelta& delta) { | 167 void advance_time_ticks_for_testing(const base::TimeDelta& delta) { |
183 time_ticks_for_testing_ += delta; | 168 time_ticks_for_testing_ += delta; |
184 } | 169 } |
185 | 170 |
186 // Returns true if detection timeout callback isn't fired or | |
187 // cancelled. | |
188 bool DetectionTimeoutIsCancelledForTesting() const; | |
189 | |
190 // Returns timeout for current (or immediate) detection attempt. | |
191 // The following rules are used for timeout computation: | |
192 // * if default (active) network is NULL, kBaseRequestTimeoutSec is used | |
193 // * if lazy detection mode is enabled, kLazyRequestTimeoutSec is used | |
194 // * otherwise, timeout equals to |attempt_count_| * kBaseRequestTimeoutSec | |
195 int GetRequestTimeoutSec() const; | |
196 | |
197 // Record detection stats such as detection duration and detection | |
198 // result in UMA. | |
199 void RecordDetectionStats(const NetworkState* network, | |
200 CaptivePortalStatus status); | |
201 | |
202 // Name of the default network. | 171 // Name of the default network. |
203 std::string default_network_name_; | 172 std::string default_network_name_; |
204 | 173 |
205 // Unique identifier of the default network. | 174 // Unique identifier of the default network. |
206 std::string default_network_id_; | 175 std::string default_network_id_; |
207 | 176 |
208 // Service path of the default network. | 177 // Service path of the default network. |
209 std::string default_service_path_; | 178 std::string default_service_path_; |
210 | 179 |
211 // Connection state of the default network. | 180 // Connection state of the default network. |
212 std::string default_connection_state_; | 181 std::string default_connection_state_; |
213 | 182 |
214 State state_; | 183 State state_; |
215 CaptivePortalStateMap portal_state_map_; | 184 CaptivePortalStateMap portal_state_map_; |
216 ObserverList<Observer> observers_; | 185 ObserverList<Observer> observers_; |
217 | 186 |
218 base::CancelableClosure detection_task_; | 187 base::CancelableClosure attempt_task_; |
219 base::CancelableClosure detection_timeout_; | 188 base::CancelableClosure attempt_timeout_; |
220 | 189 |
221 // URL that returns a 204 response code when connected to the Internet. | 190 // URL that returns a 204 response code when connected to the Internet. |
222 GURL test_url_; | 191 GURL test_url_; |
223 | 192 |
224 // Detector for checking default network for a portal state. | 193 // Detector for checking default network for a portal state. |
225 scoped_ptr<captive_portal::CaptivePortalDetector> captive_portal_detector_; | 194 scoped_ptr<captive_portal::CaptivePortalDetector> captive_portal_detector_; |
226 | 195 |
227 // True if the NetworkPortalDetector is enabled. | 196 // True if the NetworkPortalDetector is enabled. |
228 bool enabled_; | 197 bool enabled_; |
229 | 198 |
230 base::WeakPtrFactory<NetworkPortalDetectorImpl> weak_ptr_factory_; | 199 base::WeakPtrFactory<NetworkPortalDetectorImpl> weak_factory_; |
231 | |
232 // Number of portal detection attemps for a default network. | |
233 int attempt_count_; | |
234 | |
235 bool lazy_detection_enabled_; | |
236 | |
237 // Time between consecutive portal checks for a network in lazy | |
238 // mode. | |
239 base::TimeDelta lazy_check_interval_; | |
240 | 200 |
241 // Minimum time between consecutive portal checks for the same | 201 // Minimum time between consecutive portal checks for the same |
242 // default network. | 202 // default network. |
243 base::TimeDelta min_time_between_attempts_; | 203 base::TimeDelta min_time_between_attempts_; |
244 | 204 |
245 // Start time of portal detection. | 205 // Start time of portal detection. |
246 base::TimeTicks detection_start_time_; | 206 base::TimeTicks detection_start_time_; |
247 | 207 |
248 // Start time of portal detection attempt. | |
249 base::TimeTicks attempt_start_time_; | |
250 | |
251 // Delay before next portal detection. | 208 // Delay before next portal detection. |
252 base::TimeDelta next_attempt_delay_; | 209 base::TimeDelta next_attempt_delay_; |
253 | 210 |
254 // Test time ticks used by unit tests. | 211 // Available portal detection strategies. |
255 base::TimeTicks time_ticks_for_testing_; | 212 LoginScreenStrategy login_screen_strategy_; |
256 | 213 ErrorScreenStrategy error_screen_strategy_; |
257 // Test timeout for a portal detection used by unit tests. | 214 PortalDetectorStrategy* strategy_; |
Denis Kuznetsov (DE-MUC)
2014/03/05 16:59:09
I'd prefer if there was one scoped_ptr with curren
ygorshenin1
2014/03/06 11:59:33
Done.
| |
258 base::TimeDelta request_timeout_for_testing_; | |
259 | |
260 // True if |request_timeout_for_testing_| is initialized. | |
261 bool request_timeout_for_testing_initialized_; | |
262 | 215 |
263 // UI notification controller about captive portal state. | 216 // UI notification controller about captive portal state. |
264 NetworkPortalNotificationController notification_controller_; | 217 NetworkPortalNotificationController notification_controller_; |
265 | 218 |
266 content::NotificationRegistrar registrar_; | 219 content::NotificationRegistrar registrar_; |
267 | 220 |
221 // Test time ticks used by unit tests. | |
222 base::TimeTicks time_ticks_for_testing_; | |
223 | |
268 DISALLOW_COPY_AND_ASSIGN(NetworkPortalDetectorImpl); | 224 DISALLOW_COPY_AND_ASSIGN(NetworkPortalDetectorImpl); |
269 }; | 225 }; |
270 | 226 |
271 } // namespace chromeos | 227 } // namespace chromeos |
272 | 228 |
273 #endif // CHROME_BROWSER_CHROMEOS_NET_NETWORK_PORTAL_DETECTOR_IMPL_H_ | 229 #endif // CHROME_BROWSER_CHROMEOS_NET_NETWORK_PORTAL_DETECTOR_IMPL_H_ |
OLD | NEW |