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/hash_tables.h" | 13 #include "base/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.h" | 19 #include "base/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/cros/network_library.h" | |
22 #include "chrome/browser/chromeos/net/network_portal_detector.h" | 21 #include "chrome/browser/chromeos/net/network_portal_detector.h" |
| 22 #include "chromeos/network/network_state_handler_observer.h" |
23 #include "content/public/browser/notification_observer.h" | 23 #include "content/public/browser/notification_observer.h" |
24 #include "content/public/browser/notification_registrar.h" | 24 #include "content/public/browser/notification_registrar.h" |
25 #include "googleurl/src/gurl.h" | 25 #include "googleurl/src/gurl.h" |
26 #include "net/url_request/url_fetcher.h" | 26 #include "net/url_request/url_fetcher.h" |
27 | 27 |
28 namespace net { | 28 namespace net { |
29 class URLRequestContextGetter; | 29 class URLRequestContextGetter; |
30 } | 30 } |
31 | 31 |
32 namespace chromeos { | 32 namespace chromeos { |
33 | 33 |
| 34 class NetworkState; |
| 35 |
34 // This class handles all notifications about network changes from | 36 // This class handles all notifications about network changes from |
35 // NetworkLibrary and delegates portal detection for the active | 37 // NetworkLibrary and delegates portal detection for the default |
36 // network to CaptivePortalService. | 38 // network to CaptivePortalService. |
37 class NetworkPortalDetectorImpl | 39 class NetworkPortalDetectorImpl |
38 : public NetworkPortalDetector, | 40 : public NetworkPortalDetector, |
39 public base::NonThreadSafe, | 41 public base::NonThreadSafe, |
40 public chromeos::NetworkLibrary::NetworkManagerObserver, | 42 public chromeos::NetworkStateHandlerObserver, |
41 public chromeos::NetworkLibrary::NetworkObserver, | |
42 public content::NotificationObserver { | 43 public content::NotificationObserver { |
43 public: | 44 public: |
44 explicit NetworkPortalDetectorImpl( | 45 explicit NetworkPortalDetectorImpl( |
45 const scoped_refptr<net::URLRequestContextGetter>& request_context); | 46 const scoped_refptr<net::URLRequestContextGetter>& request_context); |
46 virtual ~NetworkPortalDetectorImpl(); | 47 virtual ~NetworkPortalDetectorImpl(); |
47 | 48 |
48 // NetworkPortalDetector implementation: | 49 // NetworkPortalDetector implementation: |
49 virtual void Init() OVERRIDE; | 50 virtual void Init() OVERRIDE; |
50 virtual void Shutdown() OVERRIDE; | 51 virtual void Shutdown() OVERRIDE; |
51 virtual void AddObserver(Observer* observer) OVERRIDE; | 52 virtual void AddObserver(Observer* observer) OVERRIDE; |
52 virtual void AddAndFireObserver(Observer* observer) OVERRIDE; | 53 virtual void AddAndFireObserver(Observer* observer) OVERRIDE; |
53 virtual void RemoveObserver(Observer* observer) OVERRIDE; | 54 virtual void RemoveObserver(Observer* observer) OVERRIDE; |
54 virtual CaptivePortalState GetCaptivePortalState( | 55 virtual CaptivePortalState GetCaptivePortalState( |
55 const chromeos::Network* network) OVERRIDE; | 56 const chromeos::NetworkState* network) OVERRIDE; |
56 virtual bool IsEnabled() OVERRIDE; | 57 virtual bool IsEnabled() OVERRIDE; |
57 virtual void Enable(bool start_detection) OVERRIDE; | 58 virtual void Enable(bool start_detection) OVERRIDE; |
58 virtual bool StartDetectionIfIdle() OVERRIDE; | 59 virtual bool StartDetectionIfIdle() OVERRIDE; |
59 virtual void EnableLazyDetection() OVERRIDE; | 60 virtual void EnableLazyDetection() OVERRIDE; |
60 virtual void DisableLazyDetection() OVERRIDE; | 61 virtual void DisableLazyDetection() OVERRIDE; |
61 | 62 |
62 // NetworkLibrary::NetworkManagerObserver implementation: | 63 // NetworkStateHandlerObserver implementation: |
63 virtual void OnNetworkManagerChanged(chromeos::NetworkLibrary* cros) OVERRIDE; | 64 virtual void NetworkManagerChanged() OVERRIDE; |
64 | 65 virtual void DefaultNetworkChanged(const NetworkState* network) OVERRIDE; |
65 // NetworkLibrary::NetworkObserver implementation: | |
66 virtual void OnNetworkChanged(chromeos::NetworkLibrary* cros, | |
67 const chromeos::Network* network) OVERRIDE; | |
68 | 66 |
69 private: | 67 private: |
70 friend class NetworkPortalDetectorImplTest; | 68 friend class NetworkPortalDetectorImplTest; |
71 | 69 |
72 typedef std::string NetworkId; | 70 typedef std::string NetworkId; |
73 typedef base::hash_map<NetworkId, CaptivePortalState> CaptivePortalStateMap; | 71 typedef base::hash_map<NetworkId, CaptivePortalState> CaptivePortalStateMap; |
74 enum State { | 72 enum State { |
75 // No portal check is running. | 73 // No portal check is running. |
76 STATE_IDLE = 0, | 74 STATE_IDLE = 0, |
77 // Waiting for portal check. | 75 // Waiting for portal check. |
(...skipping 27 matching lines...) Expand all Loading... |
105 const content::NotificationSource& source, | 103 const content::NotificationSource& source, |
106 const content::NotificationDetails& details) OVERRIDE; | 104 const content::NotificationDetails& details) OVERRIDE; |
107 | 105 |
108 // Returns true if we're waiting for portal check. | 106 // Returns true if we're waiting for portal check. |
109 bool IsPortalCheckPending() const; | 107 bool IsPortalCheckPending() const; |
110 | 108 |
111 // Returns true if portal check is in progress. | 109 // Returns true if portal check is in progress. |
112 bool IsCheckingForPortal() const; | 110 bool IsCheckingForPortal() const; |
113 | 111 |
114 // Stores captive portal state for a |network|. | 112 // Stores captive portal state for a |network|. |
115 void SetCaptivePortalState(const Network* network, | 113 void SetCaptivePortalState(const NetworkState* network, |
116 const CaptivePortalState& results); | 114 const CaptivePortalState& results); |
117 | 115 |
118 // Notifies observers that portal detection is completed for a |network|. | 116 // Notifies observers that portal detection is completed for a |network|. |
119 void NotifyPortalDetectionCompleted(const Network* network, | 117 void NotifyPortalDetectionCompleted(const NetworkState* network, |
120 const CaptivePortalState& state); | 118 const CaptivePortalState& state); |
121 | 119 |
122 // Returns the current TimeTicks. | 120 // Returns the current TimeTicks. |
123 base::TimeTicks GetCurrentTimeTicks() const; | 121 base::TimeTicks GetCurrentTimeTicks() const; |
124 | 122 |
125 State state() const { return state_; } | 123 State state() const { return state_; } |
126 | 124 |
127 bool lazy_detection_enabled() const { return lazy_detection_enabled_; } | 125 bool lazy_detection_enabled() const { return lazy_detection_enabled_; } |
128 | 126 |
129 // Returns current number of portal detection attempts. | 127 // Returns current number of portal detection attempts. |
(...skipping 29 matching lines...) Expand all Loading... |
159 | 157 |
160 // Advances current test time ticks. Used by unit tests. | 158 // Advances current test time ticks. Used by unit tests. |
161 void advance_time_ticks_for_testing(const base::TimeDelta& delta) { | 159 void advance_time_ticks_for_testing(const base::TimeDelta& delta) { |
162 time_ticks_for_testing_ += delta; | 160 time_ticks_for_testing_ += delta; |
163 } | 161 } |
164 | 162 |
165 // Returns true if detection timeout callback isn't fired or | 163 // Returns true if detection timeout callback isn't fired or |
166 // cancelled. | 164 // cancelled. |
167 bool DetectionTimeoutIsCancelledForTesting() const; | 165 bool DetectionTimeoutIsCancelledForTesting() const; |
168 | 166 |
169 // Unique identifier of the active network. | 167 // Unique identifier of the default network. |
170 std::string active_network_id_; | 168 std::string default_network_id_; |
171 | 169 |
172 // Service path of the active network. | 170 // Service path of the default network. |
173 std::string active_service_path_; | 171 std::string default_service_path_; |
174 | 172 |
175 // Connection state of the active network. | 173 // Connection state of the default network. |
176 ConnectionState active_connection_state_; | 174 std::string default_connection_state_; |
177 | 175 |
178 State state_; | 176 State state_; |
179 CaptivePortalStateMap portal_state_map_; | 177 CaptivePortalStateMap portal_state_map_; |
180 ObserverList<Observer> observers_; | 178 ObserverList<Observer> observers_; |
181 | 179 |
182 base::CancelableClosure detection_task_; | 180 base::CancelableClosure detection_task_; |
183 base::CancelableClosure detection_timeout_; | 181 base::CancelableClosure detection_timeout_; |
184 | 182 |
185 // URL that returns a 204 response code when connected to the Internet. | 183 // URL that returns a 204 response code when connected to the Internet. |
186 GURL test_url_; | 184 GURL test_url_; |
187 | 185 |
188 // Detector for checking active network for a portal state. | 186 // Detector for checking default network for a portal state. |
189 scoped_ptr<captive_portal::CaptivePortalDetector> captive_portal_detector_; | 187 scoped_ptr<captive_portal::CaptivePortalDetector> captive_portal_detector_; |
190 | 188 |
191 // True if the NetworkPortalDetector is enabled. | 189 // True if the NetworkPortalDetector is enabled. |
192 bool enabled_; | 190 bool enabled_; |
193 | 191 |
194 base::WeakPtrFactory<NetworkPortalDetectorImpl> weak_ptr_factory_; | 192 base::WeakPtrFactory<NetworkPortalDetectorImpl> weak_ptr_factory_; |
195 | 193 |
196 // Number of portal detection attemps for an active network. | 194 // Number of portal detection attemps for a default network. |
197 int attempt_count_; | 195 int attempt_count_; |
198 | 196 |
199 bool lazy_detection_enabled_; | 197 bool lazy_detection_enabled_; |
200 | 198 |
201 // Time between consecutive portal checks for a network in lazy | 199 // Time between consecutive portal checks for a network in lazy |
202 // mode. | 200 // mode. |
203 base::TimeDelta lazy_check_interval_; | 201 base::TimeDelta lazy_check_interval_; |
204 | 202 |
205 // Minimum time between consecutive portal checks for the same | 203 // Minimum time between consecutive portal checks for the same |
206 // active network. | 204 // default network. |
207 base::TimeDelta min_time_between_attempts_; | 205 base::TimeDelta min_time_between_attempts_; |
208 | 206 |
209 // Start time of portal detection. | 207 // Start time of portal detection. |
210 base::TimeTicks detection_start_time_; | 208 base::TimeTicks detection_start_time_; |
211 | 209 |
212 // Start time of portal detection attempt. | 210 // Start time of portal detection attempt. |
213 base::TimeTicks attempt_start_time_; | 211 base::TimeTicks attempt_start_time_; |
214 | 212 |
215 // Timeout for a portal detection. | 213 // Timeout for a portal detection. |
216 base::TimeDelta request_timeout_; | 214 base::TimeDelta request_timeout_; |
217 | 215 |
218 // Delay before next portal detection. | 216 // Delay before next portal detection. |
219 base::TimeDelta next_attempt_delay_; | 217 base::TimeDelta next_attempt_delay_; |
220 | 218 |
221 // Test time ticks used by unit tests. | 219 // Test time ticks used by unit tests. |
222 base::TimeTicks time_ticks_for_testing_; | 220 base::TimeTicks time_ticks_for_testing_; |
223 | 221 |
224 content::NotificationRegistrar registrar_; | 222 content::NotificationRegistrar registrar_; |
225 | 223 |
226 DISALLOW_COPY_AND_ASSIGN(NetworkPortalDetectorImpl); | 224 DISALLOW_COPY_AND_ASSIGN(NetworkPortalDetectorImpl); |
227 }; | 225 }; |
228 | 226 |
229 } // namespace chromeos | 227 } // namespace chromeos |
230 | 228 |
231 #endif // CHROME_BROWSER_CHROMEOS_NET_NETWORK_PORTAL_DETECTOR_IMPL_H_ | 229 #endif // CHROME_BROWSER_CHROMEOS_NET_NETWORK_PORTAL_DETECTOR_IMPL_H_ |
OLD | NEW |