| 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 int AttemptCount() OVERRIDE; |
| 75 virtual base::TimeTicks AttemptStartTime() OVERRIDE; |
| 76 virtual base::TimeTicks GetCurrentTimeTicks() OVERRIDE; |
| 77 |
| 71 private: | 78 private: |
| 72 friend class NetworkPortalDetectorImplTest; | 79 friend class NetworkPortalDetectorImplTest; |
| 73 | 80 |
| 74 typedef std::string NetworkId; | 81 typedef std::string NetworkId; |
| 75 typedef base::hash_map<NetworkId, CaptivePortalState> CaptivePortalStateMap; | 82 typedef base::hash_map<NetworkId, CaptivePortalState> CaptivePortalStateMap; |
| 76 | 83 |
| 77 enum State { | 84 enum State { |
| 78 // No portal check is running. | 85 // No portal check is running. |
| 79 STATE_IDLE = 0, | 86 STATE_IDLE = 0, |
| 80 // Waiting for portal check. | 87 // Waiting for portal check. |
| 81 STATE_PORTAL_CHECK_PENDING, | 88 STATE_PORTAL_CHECK_PENDING, |
| 82 // Portal check is in progress. | 89 // Portal check is in progress. |
| 83 STATE_CHECKING_FOR_PORTAL, | 90 STATE_CHECKING_FOR_PORTAL, |
| 84 }; | 91 }; |
| 85 | 92 |
| 86 // Basic unit used in detection timeout computation. | 93 // Starts detection process. |
| 87 static const int kBaseRequestTimeoutSec = 5; | 94 void StartDetection(); |
| 88 | 95 |
| 89 // Single detection attempt timeout in lazy mode. | 96 // Stops whole detection process. |
| 90 static const int kLazyRequestTimeoutSec = 15; | 97 void StopDetection(); |
| 91 | 98 |
| 92 // Internal predicate which describes set of states from which | 99 // Internal predicate which describes set of states from which |
| 93 // DetectCaptivePortal() can be called. | 100 // DetectCaptivePortal() can be called. |
| 94 bool CanPerformDetection() const; | 101 bool CanPerformAttempt() const; |
| 95 | 102 |
| 96 // Initiates Captive Portal detection after |delay|. | 103 // Initiates Captive Portal detection attempt after |delay|. |
| 97 // CanPerformDetection() *must* be kept before call to this method. | 104 // You should check CanPerformAttempt() before calling this method. |
| 98 void DetectCaptivePortal(const base::TimeDelta& delay); | 105 void ScheduleAttempt(const base::TimeDelta& delay); |
| 99 | 106 |
| 100 void DetectCaptivePortalTask(); | 107 // Starts detection attempt. |
| 108 void StartAttempt(); |
| 101 | 109 |
| 102 // Called when portal check is timed out. Cancels portal check and | 110 // Called when portal check is timed out. Cancels portal check and calls |
| 103 // calls OnPortalDetectionCompleted() with RESULT_NO_RESPONSE as | 111 // OnPortalDetectionCompleted() with RESULT_NO_RESPONSE as a result. |
| 104 // a result. | 112 void OnAttemptTimeout(); |
| 105 void PortalDetectionTimeout(); | |
| 106 | 113 |
| 107 void CancelPortalDetection(); | 114 // Called by CaptivePortalDetector when detection attempt completes. |
| 108 | 115 void OnAttemptCompleted( |
| 109 // Called by CaptivePortalDetector when detection completes. | |
| 110 void OnPortalDetectionCompleted( | |
| 111 const captive_portal::CaptivePortalDetector::Results& results); | 116 const captive_portal::CaptivePortalDetector::Results& results); |
| 112 | 117 |
| 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: | 118 // content::NotificationObserver implementation: |
| 118 virtual void Observe(int type, | 119 virtual void Observe(int type, |
| 119 const content::NotificationSource& source, | 120 const content::NotificationSource& source, |
| 120 const content::NotificationDetails& details) OVERRIDE; | 121 const content::NotificationDetails& details) OVERRIDE; |
| 121 | 122 |
| 122 // Returns true if we're waiting for portal check. | 123 // Stores captive portal state for a |network| and notifies observers. |
| 123 bool IsPortalCheckPending() const; | 124 void OnDetectionCompleted(const NetworkState* network, |
| 124 | 125 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 | 126 |
| 132 // Notifies observers that portal detection is completed for a |network|. | 127 // Notifies observers that portal detection is completed for a |network|. |
| 133 void NotifyPortalDetectionCompleted(const NetworkState* network, | 128 void NotifyDetectionCompleted(const NetworkState* network, |
| 134 const CaptivePortalState& state); | 129 const CaptivePortalState& state); |
| 135 | |
| 136 // Returns the current TimeTicks. | |
| 137 base::TimeTicks GetCurrentTimeTicks() const; | |
| 138 | 130 |
| 139 State state() const { return state_; } | 131 State state() const { return state_; } |
| 140 | 132 |
| 141 bool lazy_detection_enabled() const { return lazy_detection_enabled_; } | 133 bool is_idle() const { |
| 134 return state_ == STATE_IDLE; |
| 135 } |
| 136 bool is_portal_check_pending() const { |
| 137 return state_ == STATE_PORTAL_CHECK_PENDING; |
| 138 } |
| 139 bool is_checking_for_portal() const { |
| 140 return state_ == STATE_CHECKING_FOR_PORTAL; |
| 141 } |
| 142 | 142 |
| 143 // Returns current number of portal detection attempts. | 143 int attempt_count_for_testing() { |
| 144 // Used by unit tests. | 144 return attempt_count_; |
| 145 int attempt_count_for_testing() const { return attempt_count_; } | 145 } |
| 146 | 146 |
| 147 // Sets current number of detection attempts. | |
| 148 // Used by unit tests. | |
| 149 void set_attempt_count_for_testing(int attempt_count) { | 147 void set_attempt_count_for_testing(int attempt_count) { |
| 150 attempt_count_ = attempt_count; | 148 attempt_count_ = attempt_count; |
| 151 } | 149 } |
| 152 | 150 |
| 153 // Sets minimum time between consecutive portal checks for the same | |
| 154 // network. Used by unit tests. | |
| 155 void set_min_time_between_attempts_for_testing(const base::TimeDelta& delta) { | |
| 156 min_time_between_attempts_ = delta; | |
| 157 } | |
| 158 | |
| 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. | 151 // Returns delay before next portal check. Used by unit tests. |
| 172 const base::TimeDelta& next_attempt_delay_for_testing() const { | 152 const base::TimeDelta& next_attempt_delay_for_testing() const { |
| 173 return next_attempt_delay_; | 153 return next_attempt_delay_; |
| 174 } | 154 } |
| 175 | 155 |
| 156 // Returns true if attempt timeout callback isn't fired or |
| 157 // cancelled. |
| 158 bool AttemptTimeoutIsCancelledForTesting() const; |
| 159 |
| 160 // Record detection stats such as detection duration and detection |
| 161 // result in UMA. |
| 162 void RecordDetectionStats(const NetworkState* network, |
| 163 CaptivePortalStatus status); |
| 164 |
| 176 // Sets current test time ticks. Used by unit tests. | 165 // Sets current test time ticks. Used by unit tests. |
| 177 void set_time_ticks_for_testing(const base::TimeTicks& time_ticks) { | 166 void set_time_ticks_for_testing(const base::TimeTicks& time_ticks) { |
| 178 time_ticks_for_testing_ = time_ticks; | 167 time_ticks_for_testing_ = time_ticks; |
| 179 } | 168 } |
| 180 | 169 |
| 181 // Advances current test time ticks. Used by unit tests. | 170 // Advances current test time ticks. Used by unit tests. |
| 182 void advance_time_ticks_for_testing(const base::TimeDelta& delta) { | 171 void advance_time_ticks_for_testing(const base::TimeDelta& delta) { |
| 183 time_ticks_for_testing_ += delta; | 172 time_ticks_for_testing_ += delta; |
| 184 } | 173 } |
| 185 | 174 |
| 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. | 175 // Name of the default network. |
| 203 std::string default_network_name_; | 176 std::string default_network_name_; |
| 204 | 177 |
| 205 // Unique identifier of the default network. | 178 // Unique identifier of the default network. |
| 206 std::string default_network_id_; | 179 std::string default_network_id_; |
| 207 | 180 |
| 208 // Service path of the default network. | 181 // Service path of the default network. |
| 209 std::string default_service_path_; | 182 std::string default_service_path_; |
| 210 | 183 |
| 211 // Connection state of the default network. | 184 // Connection state of the default network. |
| 212 std::string default_connection_state_; | 185 std::string default_connection_state_; |
| 213 | 186 |
| 214 State state_; | 187 State state_; |
| 215 CaptivePortalStateMap portal_state_map_; | 188 CaptivePortalStateMap portal_state_map_; |
| 216 ObserverList<Observer> observers_; | 189 ObserverList<Observer> observers_; |
| 217 | 190 |
| 218 base::CancelableClosure detection_task_; | 191 base::CancelableClosure attempt_task_; |
| 219 base::CancelableClosure detection_timeout_; | 192 base::CancelableClosure attempt_timeout_; |
| 220 | 193 |
| 221 // URL that returns a 204 response code when connected to the Internet. | 194 // URL that returns a 204 response code when connected to the Internet. |
| 222 GURL test_url_; | 195 GURL test_url_; |
| 223 | 196 |
| 224 // Detector for checking default network for a portal state. | 197 // Detector for checking default network for a portal state. |
| 225 scoped_ptr<captive_portal::CaptivePortalDetector> captive_portal_detector_; | 198 scoped_ptr<captive_portal::CaptivePortalDetector> captive_portal_detector_; |
| 226 | 199 |
| 227 // True if the NetworkPortalDetector is enabled. | 200 // True if the NetworkPortalDetector is enabled. |
| 228 bool enabled_; | 201 bool enabled_; |
| 229 | 202 |
| 230 base::WeakPtrFactory<NetworkPortalDetectorImpl> weak_ptr_factory_; | 203 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 | |
| 241 // Minimum time between consecutive portal checks for the same | |
| 242 // default network. | |
| 243 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. | 208 // Start time of detection attempt. |
| 249 base::TimeTicks attempt_start_time_; | 209 base::TimeTicks attempt_start_time_; |
| 250 | 210 |
| 211 // Number of already performed detection attempts. |
| 212 int attempt_count_; |
| 213 |
| 251 // Delay before next portal detection. | 214 // Delay before next portal detection. |
| 252 base::TimeDelta next_attempt_delay_; | 215 base::TimeDelta next_attempt_delay_; |
| 253 | 216 |
| 254 // Test time ticks used by unit tests. | 217 // Current detection strategy. |
| 255 base::TimeTicks time_ticks_for_testing_; | 218 scoped_ptr<PortalDetectorStrategy> strategy_; |
| 256 | |
| 257 // Test timeout for a portal detection used by unit tests. | |
| 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 | 219 |
| 263 // UI notification controller about captive portal state. | 220 // UI notification controller about captive portal state. |
| 264 NetworkPortalNotificationController notification_controller_; | 221 NetworkPortalNotificationController notification_controller_; |
| 265 | 222 |
| 266 content::NotificationRegistrar registrar_; | 223 content::NotificationRegistrar registrar_; |
| 267 | 224 |
| 225 // Test time ticks used by unit tests. |
| 226 base::TimeTicks time_ticks_for_testing_; |
| 227 |
| 268 DISALLOW_COPY_AND_ASSIGN(NetworkPortalDetectorImpl); | 228 DISALLOW_COPY_AND_ASSIGN(NetworkPortalDetectorImpl); |
| 269 }; | 229 }; |
| 270 | 230 |
| 271 } // namespace chromeos | 231 } // namespace chromeos |
| 272 | 232 |
| 273 #endif // CHROME_BROWSER_CHROMEOS_NET_NETWORK_PORTAL_DETECTOR_IMPL_H_ | 233 #endif // CHROME_BROWSER_CHROMEOS_NET_NETWORK_PORTAL_DETECTOR_IMPL_H_ |
| OLD | NEW |