| 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 void EnableLazyDetection() OVERRIDE; | 59 virtual void EnableLazyDetection() OVERRIDE; |
| 59 virtual void DisableLazyDetection() OVERRIDE; | 60 virtual void DisableLazyDetection() OVERRIDE; |
| 60 | 61 |
| 61 // NetworkLibrary::NetworkManagerObserver implementation: | 62 // NetworkStateHandlerObserver implementation: |
| 62 virtual void OnNetworkManagerChanged(chromeos::NetworkLibrary* cros) OVERRIDE; | 63 virtual void NetworkManagerChanged() OVERRIDE; |
| 63 | 64 virtual void DefaultNetworkChanged(const NetworkState* network) OVERRIDE; |
| 64 // NetworkLibrary::NetworkObserver implementation: | |
| 65 virtual void OnNetworkChanged(chromeos::NetworkLibrary* cros, | |
| 66 const chromeos::Network* network) OVERRIDE; | |
| 67 | 65 |
| 68 private: | 66 private: |
| 69 friend class NetworkPortalDetectorImplTest; | 67 friend class NetworkPortalDetectorImplTest; |
| 70 | 68 |
| 71 typedef std::string NetworkId; | 69 typedef std::string NetworkId; |
| 72 typedef base::hash_map<NetworkId, CaptivePortalState> CaptivePortalStateMap; | 70 typedef base::hash_map<NetworkId, CaptivePortalState> CaptivePortalStateMap; |
| 73 enum State { | 71 enum State { |
| 74 // No portal check is running. | 72 // No portal check is running. |
| 75 STATE_IDLE = 0, | 73 STATE_IDLE = 0, |
| 76 // Waiting for portal check. | 74 // Waiting for portal check. |
| (...skipping 27 matching lines...) Expand all Loading... |
| 104 const content::NotificationSource& source, | 102 const content::NotificationSource& source, |
| 105 const content::NotificationDetails& details) OVERRIDE; | 103 const content::NotificationDetails& details) OVERRIDE; |
| 106 | 104 |
| 107 // Returns true if we're waiting for portal check. | 105 // Returns true if we're waiting for portal check. |
| 108 bool IsPortalCheckPending() const; | 106 bool IsPortalCheckPending() const; |
| 109 | 107 |
| 110 // Returns true if portal check is in progress. | 108 // Returns true if portal check is in progress. |
| 111 bool IsCheckingForPortal() const; | 109 bool IsCheckingForPortal() const; |
| 112 | 110 |
| 113 // Stores captive portal state for a |network|. | 111 // Stores captive portal state for a |network|. |
| 114 void SetCaptivePortalState(const Network* network, | 112 void SetCaptivePortalState(const NetworkState* network, |
| 115 const CaptivePortalState& results); | 113 const CaptivePortalState& results); |
| 116 | 114 |
| 117 // Notifies observers that portal detection is completed for a |network|. | 115 // Notifies observers that portal detection is completed for a |network|. |
| 118 void NotifyPortalDetectionCompleted(const Network* network, | 116 void NotifyPortalDetectionCompleted(const NetworkState* network, |
| 119 const CaptivePortalState& state); | 117 const CaptivePortalState& state); |
| 120 | 118 |
| 121 // Returns the current TimeTicks. | 119 // Returns the current TimeTicks. |
| 122 base::TimeTicks GetCurrentTimeTicks() const; | 120 base::TimeTicks GetCurrentTimeTicks() const; |
| 123 | 121 |
| 124 State state() { return state_; } | 122 State state() { return state_; } |
| 125 | 123 |
| 126 // Returns current number of portal detection attempts. | 124 // Returns current number of portal detection attempts. |
| 127 // Used by unit tests. | 125 // Used by unit tests. |
| 128 int attempt_count_for_testing() { return attempt_count_; } | 126 int attempt_count_for_testing() { return attempt_count_; } |
| (...skipping 27 matching lines...) Expand all Loading... |
| 156 | 154 |
| 157 // Advances current test time ticks. Used by unit tests. | 155 // Advances current test time ticks. Used by unit tests. |
| 158 void advance_time_ticks_for_testing(const base::TimeDelta& delta) { | 156 void advance_time_ticks_for_testing(const base::TimeDelta& delta) { |
| 159 time_ticks_for_testing_ += delta; | 157 time_ticks_for_testing_ += delta; |
| 160 } | 158 } |
| 161 | 159 |
| 162 // Returns true if detection timeout callback isn't fired or | 160 // Returns true if detection timeout callback isn't fired or |
| 163 // cancelled. | 161 // cancelled. |
| 164 bool DetectionTimeoutIsCancelledForTesting() const; | 162 bool DetectionTimeoutIsCancelledForTesting() const; |
| 165 | 163 |
| 166 // Unique identifier of the active network. | 164 // Unique identifier of the default network. |
| 167 std::string active_network_id_; | 165 std::string default_network_id_; |
| 168 | 166 |
| 169 // Service path of the active network. | 167 // Service path of the default network. |
| 170 std::string active_service_path_; | 168 std::string default_service_path_; |
| 171 | 169 |
| 172 // Connection state of the active network. | 170 // Connection state of the default network. |
| 173 ConnectionState active_connection_state_; | 171 std::string default_connection_state_; |
| 174 | 172 |
| 175 State state_; | 173 State state_; |
| 176 CaptivePortalStateMap portal_state_map_; | 174 CaptivePortalStateMap portal_state_map_; |
| 177 ObserverList<Observer> observers_; | 175 ObserverList<Observer> observers_; |
| 178 | 176 |
| 179 base::CancelableClosure detection_task_; | 177 base::CancelableClosure detection_task_; |
| 180 base::CancelableClosure detection_timeout_; | 178 base::CancelableClosure detection_timeout_; |
| 181 | 179 |
| 182 // URL that returns a 204 response code when connected to the Internet. | 180 // URL that returns a 204 response code when connected to the Internet. |
| 183 GURL test_url_; | 181 GURL test_url_; |
| 184 | 182 |
| 185 // Detector for checking active network for a portal state. | 183 // Detector for checking default network for a portal state. |
| 186 scoped_ptr<captive_portal::CaptivePortalDetector> captive_portal_detector_; | 184 scoped_ptr<captive_portal::CaptivePortalDetector> captive_portal_detector_; |
| 187 | 185 |
| 188 // True if the NetworkPortalDetector is enabled. | 186 // True if the NetworkPortalDetector is enabled. |
| 189 bool enabled_; | 187 bool enabled_; |
| 190 | 188 |
| 191 base::WeakPtrFactory<NetworkPortalDetectorImpl> weak_ptr_factory_; | 189 base::WeakPtrFactory<NetworkPortalDetectorImpl> weak_ptr_factory_; |
| 192 | 190 |
| 193 // Number of portal detection attemps for an active network. | 191 // Number of portal detection attemps for a default network. |
| 194 int attempt_count_; | 192 int attempt_count_; |
| 195 | 193 |
| 196 // True if lazy detection is enabled. | 194 // True if lazy detection is enabled. |
| 197 bool lazy_detection_enabled_; | 195 bool lazy_detection_enabled_; |
| 198 | 196 |
| 199 // Time between consecutive portal checks for a network in lazy | 197 // Time between consecutive portal checks for a network in lazy |
| 200 // mode. | 198 // mode. |
| 201 base::TimeDelta lazy_check_interval_; | 199 base::TimeDelta lazy_check_interval_; |
| 202 | 200 |
| 203 // Minimum time between consecutive portal checks for the same | 201 // Minimum time between consecutive portal checks for the same |
| 204 // active network. | 202 // default network. |
| 205 base::TimeDelta min_time_between_attempts_; | 203 base::TimeDelta min_time_between_attempts_; |
| 206 | 204 |
| 207 // Start time of portal detection. | 205 // Start time of portal detection. |
| 208 base::TimeTicks detection_start_time_; | 206 base::TimeTicks detection_start_time_; |
| 209 | 207 |
| 210 // Start time of portal detection attempt. | 208 // Start time of portal detection attempt. |
| 211 base::TimeTicks attempt_start_time_; | 209 base::TimeTicks attempt_start_time_; |
| 212 | 210 |
| 213 // Timeout for a portal detection. | 211 // Timeout for a portal detection. |
| 214 base::TimeDelta request_timeout_; | 212 base::TimeDelta request_timeout_; |
| 215 | 213 |
| 216 // Delay before next portal detection. | 214 // Delay before next portal detection. |
| 217 base::TimeDelta next_attempt_delay_; | 215 base::TimeDelta next_attempt_delay_; |
| 218 | 216 |
| 219 // Test time ticks used by unit tests. | 217 // Test time ticks used by unit tests. |
| 220 base::TimeTicks time_ticks_for_testing_; | 218 base::TimeTicks time_ticks_for_testing_; |
| 221 | 219 |
| 222 content::NotificationRegistrar registrar_; | 220 content::NotificationRegistrar registrar_; |
| 223 | 221 |
| 224 DISALLOW_COPY_AND_ASSIGN(NetworkPortalDetectorImpl); | 222 DISALLOW_COPY_AND_ASSIGN(NetworkPortalDetectorImpl); |
| 225 }; | 223 }; |
| 226 | 224 |
| 227 } // namespace chromeos | 225 } // namespace chromeos |
| 228 | 226 |
| 229 #endif // CHROME_BROWSER_CHROMEOS_NET_NETWORK_PORTAL_DETECTOR_IMPL_H_ | 227 #endif // CHROME_BROWSER_CHROMEOS_NET_NETWORK_PORTAL_DETECTOR_IMPL_H_ |
| OLD | NEW |