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