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