network_portal_detector_impl.h revision 116680a4aac90f2aa7413d9095a592090648e557
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/chromeos/net/network_portal_notification_controller.h" 21#include "chromeos/network/network_state_handler_observer.h" 22#include "chromeos/network/portal_detector/network_portal_detector.h" 23#include "chromeos/network/portal_detector/network_portal_detector_strategy.h" 24#include "components/captive_portal/captive_portal_detector.h" 25#include "components/captive_portal/captive_portal_types.h" 26#include "content/public/browser/notification_observer.h" 27#include "content/public/browser/notification_registrar.h" 28#include "net/url_request/url_fetcher.h" 29#include "url/gurl.h" 30 31namespace net { 32class URLRequestContextGetter; 33} 34 35namespace chromeos { 36 37class NetworkState; 38 39// This class handles all notifications about network changes from 40// NetworkStateHandler and delegates portal detection for the default 41// network to CaptivePortalService. 42class NetworkPortalDetectorImpl 43 : public NetworkPortalDetector, 44 public base::NonThreadSafe, 45 public chromeos::NetworkStateHandlerObserver, 46 public content::NotificationObserver, 47 public PortalDetectorStrategy::Delegate { 48 public: 49 static const char kOobeDetectionResultHistogram[]; 50 static const char kOobeDetectionDurationHistogram[]; 51 static const char kOobeShillOnlineHistogram[]; 52 static const char kOobeShillPortalHistogram[]; 53 static const char kOobeShillOfflineHistogram[]; 54 static const char kOobePortalToOnlineHistogram[]; 55 56 static const char kSessionDetectionResultHistogram[]; 57 static const char kSessionDetectionDurationHistogram[]; 58 static const char kSessionShillOnlineHistogram[]; 59 static const char kSessionShillPortalHistogram[]; 60 static const char kSessionShillOfflineHistogram[]; 61 static const char kSessionPortalToOnlineHistogram[]; 62 63 // Creates an instance of NetworkPortalDetectorImpl. 64 static void Initialize(net::URLRequestContextGetter* url_context); 65 66 explicit NetworkPortalDetectorImpl( 67 const scoped_refptr<net::URLRequestContextGetter>& request_context); 68 virtual ~NetworkPortalDetectorImpl(); 69 70 // NetworkPortalDetector implementation: 71 virtual void AddObserver(Observer* observer) OVERRIDE; 72 virtual void AddAndFireObserver(Observer* observer) OVERRIDE; 73 virtual void RemoveObserver(Observer* observer) OVERRIDE; 74 virtual CaptivePortalState GetCaptivePortalState( 75 const std::string& guid) OVERRIDE; 76 virtual bool IsEnabled() OVERRIDE; 77 virtual void Enable(bool start_detection) OVERRIDE; 78 virtual bool StartDetectionIfIdle() OVERRIDE; 79 virtual void SetStrategy(PortalDetectorStrategy::StrategyId id) OVERRIDE; 80 81 // NetworkStateHandlerObserver implementation: 82 virtual void DefaultNetworkChanged(const NetworkState* network) OVERRIDE; 83 84 // PortalDetectorStrategy::Delegate implementation: 85 virtual int AttemptCount() OVERRIDE; 86 virtual base::TimeTicks AttemptStartTime() OVERRIDE; 87 virtual base::TimeTicks GetCurrentTimeTicks() OVERRIDE; 88 89 private: 90 friend class NetworkPortalDetectorImplTest; 91 friend class NetworkPortalDetectorImplBrowserTest; 92 93 struct DetectionAttemptCompletedReport { 94 DetectionAttemptCompletedReport(); 95 96 DetectionAttemptCompletedReport(const std::string network_name, 97 const std::string network_id, 98 captive_portal::CaptivePortalResult result, 99 int response_code); 100 101 void Report() const; 102 103 bool Equals(const DetectionAttemptCompletedReport& o) const; 104 105 std::string network_name; 106 std::string network_id; 107 captive_portal::CaptivePortalResult result; 108 int response_code; 109 }; 110 111 typedef std::string NetworkId; 112 typedef base::hash_map<NetworkId, CaptivePortalState> CaptivePortalStateMap; 113 114 enum State { 115 // No portal check is running. 116 STATE_IDLE = 0, 117 // Waiting for portal check. 118 STATE_PORTAL_CHECK_PENDING, 119 // Portal check is in progress. 120 STATE_CHECKING_FOR_PORTAL, 121 }; 122 123 // Starts detection process. 124 void StartDetection(); 125 126 // Stops whole detection process. 127 void StopDetection(); 128 129 // Internal predicate which describes set of states from which 130 // DetectCaptivePortal() can be called. 131 bool CanPerformAttempt() const; 132 133 // Initiates Captive Portal detection attempt after |delay|. 134 // You should check CanPerformAttempt() before calling this method. 135 void ScheduleAttempt(const base::TimeDelta& delay); 136 137 // Starts detection attempt. 138 void StartAttempt(); 139 140 // Called when portal check is timed out. Cancels portal check and calls 141 // OnPortalDetectionCompleted() with RESULT_NO_RESPONSE as a result. 142 void OnAttemptTimeout(); 143 144 // Called by CaptivePortalDetector when detection attempt completes. 145 void OnAttemptCompleted( 146 const captive_portal::CaptivePortalDetector::Results& results); 147 148 // content::NotificationObserver implementation: 149 virtual void Observe(int type, 150 const content::NotificationSource& source, 151 const content::NotificationDetails& details) OVERRIDE; 152 153 // Stores captive portal state for a |network| and notifies observers. 154 void OnDetectionCompleted(const NetworkState* network, 155 const CaptivePortalState& results); 156 157 // Notifies observers that portal detection is completed for a |network|. 158 void NotifyDetectionCompleted(const NetworkState* network, 159 const CaptivePortalState& state); 160 161 State state() const { return state_; } 162 163 bool is_idle() const { 164 return state_ == STATE_IDLE; 165 } 166 bool is_portal_check_pending() const { 167 return state_ == STATE_PORTAL_CHECK_PENDING; 168 } 169 bool is_checking_for_portal() const { 170 return state_ == STATE_CHECKING_FOR_PORTAL; 171 } 172 173 int attempt_count_for_testing() { 174 return attempt_count_; 175 } 176 177 void set_attempt_count_for_testing(int attempt_count) { 178 attempt_count_ = attempt_count; 179 } 180 181 // Returns delay before next portal check. Used by unit tests. 182 const base::TimeDelta& next_attempt_delay_for_testing() const { 183 return next_attempt_delay_; 184 } 185 186 // Returns true if attempt timeout callback isn't fired or 187 // cancelled. 188 bool AttemptTimeoutIsCancelledForTesting() const; 189 190 // Record detection stats such as detection duration and detection 191 // result in UMA. 192 void RecordDetectionStats(const NetworkState* network, 193 CaptivePortalStatus status); 194 195 // Sets current test time ticks. Used by unit tests. 196 void set_time_ticks_for_testing(const base::TimeTicks& time_ticks) { 197 time_ticks_for_testing_ = time_ticks; 198 } 199 200 // Advances current test time ticks. Used by unit tests. 201 void advance_time_ticks_for_testing(const base::TimeDelta& delta) { 202 time_ticks_for_testing_ += delta; 203 } 204 205 // Name of the default network. 206 std::string default_network_name_; 207 208 // Unique identifier of the default network. 209 std::string default_network_id_; 210 211 // Connection state of the default network. 212 std::string default_connection_state_; 213 214 State state_; 215 CaptivePortalStateMap portal_state_map_; 216 ObserverList<Observer> observers_; 217 218 base::CancelableClosure attempt_task_; 219 base::CancelableClosure attempt_timeout_; 220 221 // URL that returns a 204 response code when connected to the Internet. 222 GURL test_url_; 223 224 // Detector for checking default network for a portal state. 225 scoped_ptr<captive_portal::CaptivePortalDetector> captive_portal_detector_; 226 227 // True if the NetworkPortalDetector is enabled. 228 bool enabled_; 229 230 // Start time of portal detection. 231 base::TimeTicks detection_start_time_; 232 233 // Start time of detection attempt. 234 base::TimeTicks attempt_start_time_; 235 236 // Number of already performed detection attempts. 237 int attempt_count_; 238 239 // Delay before next portal detection. 240 base::TimeDelta next_attempt_delay_; 241 242 // Current detection strategy. 243 scoped_ptr<PortalDetectorStrategy> strategy_; 244 245 // UI notification controller about captive portal state. 246 NetworkPortalNotificationController notification_controller_; 247 248 content::NotificationRegistrar registrar_; 249 250 // Test time ticks used by unit tests. 251 base::TimeTicks time_ticks_for_testing_; 252 253 // Contents of a last log message about completed detection attempt. 254 DetectionAttemptCompletedReport attempt_completed_report_; 255 256 base::WeakPtrFactory<NetworkPortalDetectorImpl> weak_factory_; 257 258 DISALLOW_COPY_AND_ASSIGN(NetworkPortalDetectorImpl); 259}; 260 261} // namespace chromeos 262 263#endif // CHROME_BROWSER_CHROMEOS_NET_NETWORK_PORTAL_DETECTOR_IMPL_H_ 264