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