| 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 #include "chrome/browser/chromeos/net/network_portal_detector_impl.h" | 5 #include "chrome/browser/chromeos/net/network_portal_detector_impl.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| 11 #include "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
| 12 #include "chrome/browser/chromeos/cros/cros_library.h" | 12 #include "chrome/browser/chromeos/cros/cros_library.h" |
| 13 #include "chrome/common/chrome_notification_types.h" | 13 #include "chrome/common/chrome_notification_types.h" |
| 14 #include "chrome/common/chrome_switches.h" | 14 #include "chrome/common/chrome_switches.h" |
| 15 #include "chromeos/network/network_state.h" |
| 16 #include "chromeos/network/network_state_handler.h" |
| 15 #include "content/public/browser/notification_service.h" | 17 #include "content/public/browser/notification_service.h" |
| 16 #include "grit/generated_resources.h" | 18 #include "grit/generated_resources.h" |
| 17 #include "net/http/http_status_code.h" | 19 #include "net/http/http_status_code.h" |
| 20 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 18 #include "ui/base/l10n/l10n_util.h" | 21 #include "ui/base/l10n/l10n_util.h" |
| 19 | 22 |
| 20 using captive_portal::CaptivePortalDetector; | 23 using captive_portal::CaptivePortalDetector; |
| 21 | 24 |
| 22 namespace chromeos { | 25 namespace chromeos { |
| 23 | 26 |
| 24 namespace { | 27 namespace { |
| 25 | 28 |
| 26 // Maximum number of portal detections for the same active network | 29 // Maximum number of portal detections for the same default network |
| 27 // after network change. | 30 // after network change. |
| 28 const int kMaxRequestAttempts = 3; | 31 const int kMaxRequestAttempts = 3; |
| 29 | 32 |
| 30 // Minimum timeout between consecutive portal checks for the same | 33 // Minimum timeout between consecutive portal checks for the same |
| 31 // network. | 34 // network. |
| 32 const int kMinTimeBetweenAttemptsSec = 3; | 35 const int kMinTimeBetweenAttemptsSec = 3; |
| 33 | 36 |
| 34 // Timeout for a portal check. | 37 // Timeout for a portal check. |
| 35 const int kRequestTimeoutSec = 5; | 38 const int kRequestTimeoutSec = 5; |
| 36 | 39 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 58 case NetworkPortalDetectorImpl::CAPTIVE_PORTAL_STATUS_PROXY_AUTH_REQUIRED: | 61 case NetworkPortalDetectorImpl::CAPTIVE_PORTAL_STATUS_PROXY_AUTH_REQUIRED: |
| 59 return l10n_util::GetStringUTF8( | 62 return l10n_util::GetStringUTF8( |
| 60 IDS_CHROMEOS_CAPTIVE_PORTAL_STATUS_PROXY_AUTH_REQUIRED); | 63 IDS_CHROMEOS_CAPTIVE_PORTAL_STATUS_PROXY_AUTH_REQUIRED); |
| 61 case NetworkPortalDetectorImpl::CAPTIVE_PORTAL_STATUS_COUNT: | 64 case NetworkPortalDetectorImpl::CAPTIVE_PORTAL_STATUS_COUNT: |
| 62 NOTREACHED(); | 65 NOTREACHED(); |
| 63 } | 66 } |
| 64 return l10n_util::GetStringUTF8( | 67 return l10n_util::GetStringUTF8( |
| 65 IDS_CHROMEOS_CAPTIVE_PORTAL_STATUS_UNRECOGNIZED); | 68 IDS_CHROMEOS_CAPTIVE_PORTAL_STATUS_UNRECOGNIZED); |
| 66 } | 69 } |
| 67 | 70 |
| 68 NetworkLibrary* GetNetworkLibrary() { | |
| 69 CHECK(CrosLibrary::Get()); | |
| 70 return CrosLibrary::Get()->GetNetworkLibrary(); | |
| 71 } | |
| 72 | |
| 73 const Network* GetActiveNetwork() { | |
| 74 NetworkLibrary* cros = GetNetworkLibrary(); | |
| 75 if (!cros) | |
| 76 return NULL; | |
| 77 return cros->active_network(); | |
| 78 } | |
| 79 | |
| 80 const Network* FindNetworkByPath(const std::string& service_path) { | |
| 81 NetworkLibrary* cros = GetNetworkLibrary(); | |
| 82 if (!cros) | |
| 83 return NULL; | |
| 84 return cros->FindNetworkByPath(service_path); | |
| 85 } | |
| 86 | |
| 87 } // namespace | 71 } // namespace |
| 88 | 72 |
| 89 NetworkPortalDetectorImpl::NetworkPortalDetectorImpl( | 73 NetworkPortalDetectorImpl::NetworkPortalDetectorImpl( |
| 90 const scoped_refptr<net::URLRequestContextGetter>& request_context) | 74 const scoped_refptr<net::URLRequestContextGetter>& request_context) |
| 91 : active_connection_state_(STATE_UNKNOWN), | 75 : test_url_(CaptivePortalDetector::kDefaultURL), |
| 92 test_url_(CaptivePortalDetector::kDefaultURL), | |
| 93 enabled_(false), | 76 enabled_(false), |
| 94 weak_ptr_factory_(this), | 77 weak_ptr_factory_(this), |
| 95 attempt_count_(0), | 78 attempt_count_(0), |
| 96 lazy_detection_enabled_(false), | 79 lazy_detection_enabled_(false), |
| 97 lazy_check_interval_(base::TimeDelta::FromSeconds(kLazyCheckIntervalSec)), | 80 lazy_check_interval_(base::TimeDelta::FromSeconds(kLazyCheckIntervalSec)), |
| 98 min_time_between_attempts_( | 81 min_time_between_attempts_( |
| 99 base::TimeDelta::FromSeconds(kMinTimeBetweenAttemptsSec)), | 82 base::TimeDelta::FromSeconds(kMinTimeBetweenAttemptsSec)), |
| 100 request_timeout_(base::TimeDelta::FromSeconds(kRequestTimeoutSec)) { | 83 request_timeout_(base::TimeDelta::FromSeconds(kRequestTimeoutSec)) { |
| 101 captive_portal_detector_.reset(new CaptivePortalDetector(request_context)); | 84 captive_portal_detector_.reset(new CaptivePortalDetector(request_context)); |
| 102 | 85 |
| 103 registrar_.Add(this, | 86 registrar_.Add(this, |
| 104 chrome::NOTIFICATION_LOGIN_PROXY_CHANGED, | 87 chrome::NOTIFICATION_LOGIN_PROXY_CHANGED, |
| 105 content::NotificationService::AllSources()); | 88 content::NotificationService::AllSources()); |
| 106 registrar_.Add(this, | 89 registrar_.Add(this, |
| 107 chrome::NOTIFICATION_AUTH_SUPPLIED, | 90 chrome::NOTIFICATION_AUTH_SUPPLIED, |
| 108 content::NotificationService::AllSources()); | 91 content::NotificationService::AllSources()); |
| 109 registrar_.Add(this, | 92 registrar_.Add(this, |
| 110 chrome::NOTIFICATION_AUTH_CANCELLED, | 93 chrome::NOTIFICATION_AUTH_CANCELLED, |
| 111 content::NotificationService::AllSources()); | 94 content::NotificationService::AllSources()); |
| 112 } | 95 } |
| 113 | 96 |
| 114 NetworkPortalDetectorImpl::~NetworkPortalDetectorImpl() { | 97 NetworkPortalDetectorImpl::~NetworkPortalDetectorImpl() { |
| 115 } | 98 } |
| 116 | 99 |
| 117 void NetworkPortalDetectorImpl::Init() { | 100 void NetworkPortalDetectorImpl::Init() { |
| 118 DCHECK(CalledOnValidThread()); | 101 DCHECK(CalledOnValidThread()); |
| 119 | 102 |
| 120 state_ = STATE_IDLE; | 103 state_ = STATE_IDLE; |
| 121 chromeos::NetworkLibrary* network_library = GetNetworkLibrary(); | 104 NetworkStateHandler::Get()->AddObserver(this); |
| 122 DCHECK(network_library); | |
| 123 network_library->AddNetworkManagerObserver(this); | |
| 124 network_library->RemoveObserverForAllNetworks(this); | |
| 125 } | 105 } |
| 126 | 106 |
| 127 void NetworkPortalDetectorImpl::Shutdown() { | 107 void NetworkPortalDetectorImpl::Shutdown() { |
| 128 DCHECK(CalledOnValidThread()); | 108 DCHECK(CalledOnValidThread()); |
| 129 | 109 |
| 130 detection_task_.Cancel(); | 110 detection_task_.Cancel(); |
| 131 detection_timeout_.Cancel(); | 111 detection_timeout_.Cancel(); |
| 132 | 112 |
| 133 captive_portal_detector_->Cancel(); | 113 captive_portal_detector_->Cancel(); |
| 134 captive_portal_detector_.reset(); | 114 captive_portal_detector_.reset(); |
| 135 observers_.Clear(); | 115 observers_.Clear(); |
| 136 chromeos::NetworkLibrary* network_library = GetNetworkLibrary(); | 116 if (NetworkStateHandler::IsInitialized()) |
| 137 if (network_library) | 117 NetworkStateHandler::Get()->RemoveObserver(this); |
| 138 network_library->RemoveNetworkManagerObserver(this); | |
| 139 } | 118 } |
| 140 | 119 |
| 141 void NetworkPortalDetectorImpl::AddObserver(Observer* observer) { | 120 void NetworkPortalDetectorImpl::AddObserver(Observer* observer) { |
| 142 DCHECK(CalledOnValidThread()); | 121 DCHECK(CalledOnValidThread()); |
| 143 if (!observer || observers_.HasObserver(observer)) | 122 if (!observer || observers_.HasObserver(observer)) |
| 144 return; | 123 return; |
| 145 observers_.AddObserver(observer); | 124 observers_.AddObserver(observer); |
| 146 } | 125 } |
| 147 | 126 |
| 148 void NetworkPortalDetectorImpl::AddAndFireObserver(Observer* observer) { | 127 void NetworkPortalDetectorImpl::AddAndFireObserver(Observer* observer) { |
| 149 DCHECK(CalledOnValidThread()); | 128 DCHECK(CalledOnValidThread()); |
| 150 if (!observer) | 129 if (!observer) |
| 151 return; | 130 return; |
| 152 AddObserver(observer); | 131 AddObserver(observer); |
| 153 const Network* network = GetActiveNetwork(); | 132 const NetworkState* network = NetworkStateHandler::Get()->DefaultNetwork(); |
| 154 observer->OnPortalDetectionCompleted(network, GetCaptivePortalState(network)); | 133 observer->OnPortalDetectionCompleted(network, GetCaptivePortalState(network)); |
| 155 } | 134 } |
| 156 | 135 |
| 157 void NetworkPortalDetectorImpl::RemoveObserver(Observer* observer) { | 136 void NetworkPortalDetectorImpl::RemoveObserver(Observer* observer) { |
| 158 DCHECK(CalledOnValidThread()); | 137 DCHECK(CalledOnValidThread()); |
| 159 if (observer) | 138 if (observer) |
| 160 observers_.RemoveObserver(observer); | 139 observers_.RemoveObserver(observer); |
| 161 } | 140 } |
| 162 | 141 |
| 163 bool NetworkPortalDetectorImpl::IsEnabled() { | 142 bool NetworkPortalDetectorImpl::IsEnabled() { |
| 164 return enabled_; | 143 return enabled_; |
| 165 } | 144 } |
| 166 | 145 |
| 167 void NetworkPortalDetectorImpl::Enable(bool start_detection) { | 146 void NetworkPortalDetectorImpl::Enable(bool start_detection) { |
| 168 DCHECK(CalledOnValidThread()); | 147 DCHECK(CalledOnValidThread()); |
| 169 if (enabled_) | 148 if (enabled_) |
| 170 return; | 149 return; |
| 171 enabled_ = true; | 150 enabled_ = true; |
| 172 DCHECK(!IsPortalCheckPending()); | 151 DCHECK(!IsPortalCheckPending()); |
| 173 DCHECK(!IsCheckingForPortal()); | 152 DCHECK(!IsCheckingForPortal()); |
| 174 DCHECK(!lazy_detection_enabled_); | 153 DCHECK(!lazy_detection_enabled_); |
| 175 if (!start_detection) | 154 if (!start_detection) |
| 176 return; | 155 return; |
| 177 state_ = STATE_IDLE; | 156 state_ = STATE_IDLE; |
| 178 attempt_count_ = 0; | 157 attempt_count_ = 0; |
| 179 const Network* active_network = GetActiveNetwork(); | 158 const NetworkState* default_network = |
| 180 if (!active_network) | 159 NetworkStateHandler::Get()->DefaultNetwork(); |
| 160 if (!default_network) |
| 181 return; | 161 return; |
| 182 portal_state_map_.erase(active_network->service_path()); | 162 portal_state_map_.erase(default_network->path()); |
| 183 DetectCaptivePortal(base::TimeDelta()); | 163 DetectCaptivePortal(base::TimeDelta()); |
| 184 } | 164 } |
| 185 | 165 |
| 186 NetworkPortalDetectorImpl::CaptivePortalState | 166 NetworkPortalDetectorImpl::CaptivePortalState |
| 187 NetworkPortalDetectorImpl::GetCaptivePortalState(const Network* network) { | 167 NetworkPortalDetectorImpl::GetCaptivePortalState(const NetworkState* network) { |
| 188 DCHECK(CalledOnValidThread()); | 168 DCHECK(CalledOnValidThread()); |
| 189 if (!network) | 169 if (!network) |
| 190 return CaptivePortalState(); | 170 return CaptivePortalState(); |
| 191 CaptivePortalStateMap::const_iterator it = | 171 CaptivePortalStateMap::const_iterator it = |
| 192 portal_state_map_.find(network->service_path()); | 172 portal_state_map_.find(network->path()); |
| 193 if (it == portal_state_map_.end()) | 173 if (it == portal_state_map_.end()) |
| 194 return CaptivePortalState(); | 174 return CaptivePortalState(); |
| 195 return it->second; | 175 return it->second; |
| 196 } | 176 } |
| 197 | 177 |
| 198 void NetworkPortalDetectorImpl::EnableLazyDetection() { | 178 void NetworkPortalDetectorImpl::EnableLazyDetection() { |
| 199 if (lazy_detection_enabled_) | 179 if (lazy_detection_enabled_) |
| 200 return; | 180 return; |
| 201 VLOG(1) << "Lazy detection mode enabled."; | 181 VLOG(1) << "Lazy detection mode enabled."; |
| 202 lazy_detection_enabled_ = true; | 182 lazy_detection_enabled_ = true; |
| 203 if (!IsPortalCheckPending() && !IsCheckingForPortal()) | 183 if (!IsPortalCheckPending() && !IsCheckingForPortal()) |
| 204 DetectCaptivePortal(base::TimeDelta()); | 184 DetectCaptivePortal(base::TimeDelta()); |
| 205 } | 185 } |
| 206 | 186 |
| 207 void NetworkPortalDetectorImpl::DisableLazyDetection() { | 187 void NetworkPortalDetectorImpl::DisableLazyDetection() { |
| 208 if (!lazy_detection_enabled_) | 188 if (!lazy_detection_enabled_) |
| 209 return; | 189 return; |
| 210 VLOG(1) << "Lazy detection mode disabled."; | 190 VLOG(1) << "Lazy detection mode disabled."; |
| 211 lazy_detection_enabled_ = false; | 191 lazy_detection_enabled_ = false; |
| 212 } | 192 } |
| 213 | 193 |
| 214 void NetworkPortalDetectorImpl::OnNetworkManagerChanged(NetworkLibrary* cros) { | 194 void NetworkPortalDetectorImpl::NetworkManagerChanged() { |
| 215 DCHECK(CalledOnValidThread()); | 195 DCHECK(CalledOnValidThread()); |
| 216 CHECK(cros); | 196 const NetworkState* default_network = |
| 217 const Network* active_network = cros->active_network(); | 197 NetworkStateHandler::Get()->DefaultNetwork(); |
| 218 if (!active_network) | 198 if (!default_network) |
| 219 return; | 199 return; |
| 220 | 200 |
| 221 active_network_id_ = active_network->unique_id(); | 201 default_network_id_ = default_network->guid(); |
| 222 | 202 |
| 223 bool network_changed = | 203 bool network_changed = (default_service_path_ != default_network->path()); |
| 224 (active_service_path_ != active_network->service_path()); | 204 default_service_path_ = default_network->path(); |
| 225 if (network_changed) { | |
| 226 if (!active_service_path_.empty()) | |
| 227 cros->RemoveNetworkObserver(active_service_path_, this); | |
| 228 active_service_path_ = active_network->service_path(); | |
| 229 cros->AddNetworkObserver(active_service_path_, this); | |
| 230 } | |
| 231 | 205 |
| 232 bool connection_state_changed = | 206 bool connection_state_changed = (default_connection_state_ != |
| 233 (active_connection_state_ != active_network->connection_state()); | 207 default_network->connection_state()); |
| 234 active_connection_state_ = active_network->connection_state(); | 208 default_connection_state_ = default_network->connection_state(); |
| 235 | 209 |
| 236 if (network_changed || connection_state_changed) { | 210 if (network_changed || connection_state_changed) { |
| 237 attempt_count_ = 0; | 211 attempt_count_ = 0; |
| 238 CancelPortalDetection(); | 212 CancelPortalDetection(); |
| 239 } | 213 } |
| 240 | 214 |
| 241 if (!IsCheckingForPortal() && !IsPortalCheckPending() && | 215 if (!IsCheckingForPortal() && !IsPortalCheckPending() && |
| 242 Network::IsConnectedState(active_connection_state_) && | 216 NetworkState::StateIsConnected(default_connection_state_) && |
| 243 (attempt_count_ < kMaxRequestAttempts || lazy_detection_enabled_)) { | 217 (attempt_count_ < kMaxRequestAttempts || lazy_detection_enabled_)) { |
| 244 DCHECK(active_network); | |
| 245 | |
| 246 // Initiate Captive Portal detection if network's captive | 218 // Initiate Captive Portal detection if network's captive |
| 247 // portal state is unknown (e.g. for freshly created networks), | 219 // portal state is unknown (e.g. for freshly created networks), |
| 248 // offline or if network connection state was changed. | 220 // offline or if network connection state was changed. |
| 249 CaptivePortalState state = GetCaptivePortalState(active_network); | 221 CaptivePortalState state = GetCaptivePortalState(default_network); |
| 250 if (state.status == CAPTIVE_PORTAL_STATUS_UNKNOWN || | 222 if (state.status == CAPTIVE_PORTAL_STATUS_UNKNOWN || |
| 251 state.status == CAPTIVE_PORTAL_STATUS_OFFLINE || | 223 state.status == CAPTIVE_PORTAL_STATUS_OFFLINE || |
| 252 (!network_changed && connection_state_changed)) { | 224 (!network_changed && connection_state_changed)) { |
| 253 DetectCaptivePortal(base::TimeDelta()); | 225 DetectCaptivePortal(base::TimeDelta()); |
| 254 } | 226 } |
| 255 } | 227 } |
| 256 } | 228 } |
| 257 | 229 |
| 258 void NetworkPortalDetectorImpl::OnNetworkChanged( | 230 void NetworkPortalDetectorImpl::DefaultNetworkChanged( |
| 259 chromeos::NetworkLibrary* cros, | 231 const NetworkState* network) { |
| 260 const chromeos::Network* network) { | 232 NetworkManagerChanged(); |
| 261 DCHECK(CalledOnValidThread()); | |
| 262 OnNetworkManagerChanged(cros); | |
| 263 } | 233 } |
| 264 | 234 |
| 265 void NetworkPortalDetectorImpl::DetectCaptivePortal( | 235 void NetworkPortalDetectorImpl::DetectCaptivePortal( |
| 266 const base::TimeDelta& delay) { | 236 const base::TimeDelta& delay) { |
| 267 DCHECK(!IsPortalCheckPending()); | 237 DCHECK(!IsPortalCheckPending()); |
| 268 DCHECK(!IsCheckingForPortal()); | 238 DCHECK(!IsCheckingForPortal()); |
| 269 DCHECK(attempt_count_ < kMaxRequestAttempts || lazy_detection_enabled_); | 239 DCHECK(attempt_count_ < kMaxRequestAttempts || lazy_detection_enabled_); |
| 270 | 240 |
| 271 if (!IsEnabled()) | 241 if (!IsEnabled()) |
| 272 return; | 242 return; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 304 | 274 |
| 305 void NetworkPortalDetectorImpl::DetectCaptivePortalTask() { | 275 void NetworkPortalDetectorImpl::DetectCaptivePortalTask() { |
| 306 DCHECK(IsPortalCheckPending()); | 276 DCHECK(IsPortalCheckPending()); |
| 307 | 277 |
| 308 state_ = STATE_CHECKING_FOR_PORTAL; | 278 state_ = STATE_CHECKING_FOR_PORTAL; |
| 309 attempt_start_time_ = GetCurrentTimeTicks(); | 279 attempt_start_time_ = GetCurrentTimeTicks(); |
| 310 | 280 |
| 311 if (attempt_count_ < kMaxRequestAttempts) { | 281 if (attempt_count_ < kMaxRequestAttempts) { |
| 312 ++attempt_count_; | 282 ++attempt_count_; |
| 313 VLOG(1) << "Portal detection started: " | 283 VLOG(1) << "Portal detection started: " |
| 314 << "network=" << active_network_id_ << ", " | 284 << "network=" << default_network_id_ << ", " |
| 315 << "attempt=" << attempt_count_ << " of " << kMaxRequestAttempts; | 285 << "attempt=" << attempt_count_ << " of " << kMaxRequestAttempts; |
| 316 } else { | 286 } else { |
| 317 DCHECK(lazy_detection_enabled_); | 287 DCHECK(lazy_detection_enabled_); |
| 318 VLOG(1) << "Lazy portal detection attempt started"; | 288 VLOG(1) << "Lazy portal detection attempt started"; |
| 319 } | 289 } |
| 320 | 290 |
| 321 captive_portal_detector_->DetectCaptivePortal( | 291 captive_portal_detector_->DetectCaptivePortal( |
| 322 test_url_, | 292 test_url_, |
| 323 base::Bind(&NetworkPortalDetectorImpl::OnPortalDetectionCompleted, | 293 base::Bind(&NetworkPortalDetectorImpl::OnPortalDetectionCompleted, |
| 324 weak_ptr_factory_.GetWeakPtr())); | 294 weak_ptr_factory_.GetWeakPtr())); |
| 325 detection_timeout_.Reset( | 295 detection_timeout_.Reset( |
| 326 base::Bind(&NetworkPortalDetectorImpl::PortalDetectionTimeout, | 296 base::Bind(&NetworkPortalDetectorImpl::PortalDetectionTimeout, |
| 327 weak_ptr_factory_.GetWeakPtr())); | 297 weak_ptr_factory_.GetWeakPtr())); |
| 328 MessageLoop::current()->PostDelayedTask(FROM_HERE, | 298 MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 329 detection_timeout_.callback(), | 299 detection_timeout_.callback(), |
| 330 request_timeout_); | 300 request_timeout_); |
| 331 } | 301 } |
| 332 | 302 |
| 333 void NetworkPortalDetectorImpl::PortalDetectionTimeout() { | 303 void NetworkPortalDetectorImpl::PortalDetectionTimeout() { |
| 334 DCHECK(CalledOnValidThread()); | 304 DCHECK(CalledOnValidThread()); |
| 335 DCHECK(IsCheckingForPortal()); | 305 DCHECK(IsCheckingForPortal()); |
| 336 | 306 |
| 337 VLOG(1) << "Portal detection timeout: network=" << active_network_id_; | 307 VLOG(1) << "Portal detection timeout: network=" << default_network_id_; |
| 338 | 308 |
| 339 captive_portal_detector_->Cancel(); | 309 captive_portal_detector_->Cancel(); |
| 340 CaptivePortalDetector::Results results; | 310 CaptivePortalDetector::Results results; |
| 341 results.result = captive_portal::RESULT_NO_RESPONSE; | 311 results.result = captive_portal::RESULT_NO_RESPONSE; |
| 342 OnPortalDetectionCompleted(results); | 312 OnPortalDetectionCompleted(results); |
| 343 } | 313 } |
| 344 | 314 |
| 345 void NetworkPortalDetectorImpl::CancelPortalDetection() { | 315 void NetworkPortalDetectorImpl::CancelPortalDetection() { |
| 346 if (IsPortalCheckPending()) | 316 if (IsPortalCheckPending()) |
| 347 detection_task_.Cancel(); | 317 detection_task_.Cancel(); |
| 348 else if (IsCheckingForPortal()) | 318 else if (IsCheckingForPortal()) |
| 349 captive_portal_detector_->Cancel(); | 319 captive_portal_detector_->Cancel(); |
| 350 detection_timeout_.Cancel(); | 320 detection_timeout_.Cancel(); |
| 351 state_ = STATE_IDLE; | 321 state_ = STATE_IDLE; |
| 352 } | 322 } |
| 353 | 323 |
| 354 void NetworkPortalDetectorImpl::OnPortalDetectionCompleted( | 324 void NetworkPortalDetectorImpl::OnPortalDetectionCompleted( |
| 355 const CaptivePortalDetector::Results& results) { | 325 const CaptivePortalDetector::Results& results) { |
| 356 DCHECK(CalledOnValidThread()); | 326 DCHECK(CalledOnValidThread()); |
| 357 DCHECK(IsCheckingForPortal()); | 327 DCHECK(IsCheckingForPortal()); |
| 358 | 328 |
| 359 VLOG(1) << "Portal detection completed: " | 329 VLOG(1) << "Portal detection completed: " |
| 360 << "network=" << active_network_id_ << ", " | 330 << "network=" << default_network_id_ << ", " |
| 361 << "result=" << CaptivePortalDetector::CaptivePortalResultToString( | 331 << "result=" << CaptivePortalDetector::CaptivePortalResultToString( |
| 362 results.result) << ", " | 332 results.result) << ", " |
| 363 << "response_code=" << results.response_code; | 333 << "response_code=" << results.response_code; |
| 364 | 334 |
| 365 state_ = STATE_IDLE; | 335 state_ = STATE_IDLE; |
| 366 detection_timeout_.Cancel(); | 336 detection_timeout_.Cancel(); |
| 367 | 337 |
| 368 NetworkLibrary* cros = GetNetworkLibrary(); | 338 const NetworkState* default_network = |
| 369 const Network* active_network = cros->active_network(); | 339 NetworkStateHandler::Get()->DefaultNetwork(); |
| 370 if (!active_network) { | 340 if (!default_network) { |
| 371 TryLazyDetection(); | 341 TryLazyDetection(); |
| 372 return; | 342 return; |
| 373 } | 343 } |
| 374 | 344 |
| 375 CaptivePortalState state; | 345 CaptivePortalState state; |
| 376 state.response_code = results.response_code; | 346 state.response_code = results.response_code; |
| 377 switch (results.result) { | 347 switch (results.result) { |
| 378 case captive_portal::RESULT_NO_RESPONSE: | 348 case captive_portal::RESULT_NO_RESPONSE: |
| 379 if (attempt_count_ >= kMaxRequestAttempts) { | 349 if (attempt_count_ >= kMaxRequestAttempts) { |
| 380 if (state.response_code == net::HTTP_PROXY_AUTHENTICATION_REQUIRED) { | 350 if (state.response_code == net::HTTP_PROXY_AUTHENTICATION_REQUIRED) { |
| 381 state.status = CAPTIVE_PORTAL_STATUS_PROXY_AUTH_REQUIRED; | 351 state.status = CAPTIVE_PORTAL_STATUS_PROXY_AUTH_REQUIRED; |
| 382 } else if (active_network->restricted_pool()) { | 352 } else if (default_network->connection_state() == |
| 353 flimflam::kStatePortal) { |
| 383 // Take into account shill's detection results. | 354 // Take into account shill's detection results. |
| 384 state.status = CAPTIVE_PORTAL_STATUS_PORTAL; | 355 state.status = CAPTIVE_PORTAL_STATUS_PORTAL; |
| 385 LOG(WARNING) << "Network " << active_network->unique_id() << " " | 356 LOG(WARNING) << "Network " << default_network->guid() << " " |
| 386 << "is marked as " | 357 << "is marked as " |
| 387 << CaptivePortalStatusString(state.status) << " " | 358 << CaptivePortalStatusString(state.status) << " " |
| 388 << "despite the fact that CaptivePortalDetector " | 359 << "despite the fact that CaptivePortalDetector " |
| 389 << "received no response"; | 360 << "received no response"; |
| 390 } else { | 361 } else { |
| 391 state.status = CAPTIVE_PORTAL_STATUS_OFFLINE; | 362 state.status = CAPTIVE_PORTAL_STATUS_OFFLINE; |
| 392 } | 363 } |
| 393 SetCaptivePortalState(active_network, state); | 364 SetCaptivePortalState(default_network, state); |
| 394 } else { | 365 } else { |
| 395 DetectCaptivePortal(results.retry_after_delta); | 366 DetectCaptivePortal(results.retry_after_delta); |
| 396 } | 367 } |
| 397 break; | 368 break; |
| 398 case captive_portal::RESULT_INTERNET_CONNECTED: | 369 case captive_portal::RESULT_INTERNET_CONNECTED: |
| 399 state.status = CAPTIVE_PORTAL_STATUS_ONLINE; | 370 state.status = CAPTIVE_PORTAL_STATUS_ONLINE; |
| 400 SetCaptivePortalState(active_network, state); | 371 SetCaptivePortalState(default_network, state); |
| 401 break; | 372 break; |
| 402 case captive_portal::RESULT_BEHIND_CAPTIVE_PORTAL: | 373 case captive_portal::RESULT_BEHIND_CAPTIVE_PORTAL: |
| 403 state.status = CAPTIVE_PORTAL_STATUS_PORTAL; | 374 state.status = CAPTIVE_PORTAL_STATUS_PORTAL; |
| 404 SetCaptivePortalState(active_network, state); | 375 SetCaptivePortalState(default_network, state); |
| 405 break; | 376 break; |
| 406 default: | 377 default: |
| 407 break; | 378 break; |
| 408 } | 379 } |
| 409 | 380 |
| 410 TryLazyDetection(); | 381 TryLazyDetection(); |
| 411 } | 382 } |
| 412 | 383 |
| 413 void NetworkPortalDetectorImpl::TryLazyDetection() { | 384 void NetworkPortalDetectorImpl::TryLazyDetection() { |
| 414 if (!IsPortalCheckPending() && !IsCheckingForPortal() && | 385 if (!IsPortalCheckPending() && !IsCheckingForPortal() && |
| (...skipping 20 matching lines...) Expand all Loading... |
| 435 | 406 |
| 436 bool NetworkPortalDetectorImpl::IsPortalCheckPending() const { | 407 bool NetworkPortalDetectorImpl::IsPortalCheckPending() const { |
| 437 return state_ == STATE_PORTAL_CHECK_PENDING; | 408 return state_ == STATE_PORTAL_CHECK_PENDING; |
| 438 } | 409 } |
| 439 | 410 |
| 440 bool NetworkPortalDetectorImpl::IsCheckingForPortal() const { | 411 bool NetworkPortalDetectorImpl::IsCheckingForPortal() const { |
| 441 return state_ == STATE_CHECKING_FOR_PORTAL; | 412 return state_ == STATE_CHECKING_FOR_PORTAL; |
| 442 } | 413 } |
| 443 | 414 |
| 444 void NetworkPortalDetectorImpl::SetCaptivePortalState( | 415 void NetworkPortalDetectorImpl::SetCaptivePortalState( |
| 445 const Network* network, | 416 const NetworkState* network, |
| 446 const CaptivePortalState& state) { | 417 const CaptivePortalState& state) { |
| 447 DCHECK(network); | 418 DCHECK(network); |
| 448 | 419 |
| 449 if (!detection_start_time_.is_null()) { | 420 if (!detection_start_time_.is_null()) { |
| 450 UMA_HISTOGRAM_TIMES("CaptivePortal.OOBE.DetectionDuration", | 421 UMA_HISTOGRAM_TIMES("CaptivePortal.OOBE.DetectionDuration", |
| 451 GetCurrentTimeTicks() - detection_start_time_); | 422 GetCurrentTimeTicks() - detection_start_time_); |
| 452 } | 423 } |
| 453 | 424 |
| 454 CaptivePortalStateMap::const_iterator it = | 425 CaptivePortalStateMap::const_iterator it = |
| 455 portal_state_map_.find(network->service_path()); | 426 portal_state_map_.find(network->path()); |
| 456 if (it == portal_state_map_.end() || | 427 if (it == portal_state_map_.end() || |
| 457 it->second.status != state.status || | 428 it->second.status != state.status || |
| 458 it->second.response_code != state.response_code) { | 429 it->second.response_code != state.response_code) { |
| 459 VLOG(1) << "Updating Chrome Captive Portal state: " | 430 VLOG(1) << "Updating Chrome Captive Portal state: " |
| 460 << "network=" << network->unique_id() << ", " | 431 << "network=" << network->guid() << ", " |
| 461 << "status=" << CaptivePortalStatusString(state.status) << ", " | 432 << "status=" << CaptivePortalStatusString(state.status) << ", " |
| 462 << "response_code=" << state.response_code; | 433 << "response_code=" << state.response_code; |
| 463 portal_state_map_[network->service_path()] = state; | 434 portal_state_map_[network->path()] = state; |
| 464 } | 435 } |
| 465 NotifyPortalDetectionCompleted(network, state); | 436 NotifyPortalDetectionCompleted(network, state); |
| 466 } | 437 } |
| 467 | 438 |
| 468 void NetworkPortalDetectorImpl::NotifyPortalDetectionCompleted( | 439 void NetworkPortalDetectorImpl::NotifyPortalDetectionCompleted( |
| 469 const Network* network, | 440 const NetworkState* network, |
| 470 const CaptivePortalState& state) { | 441 const CaptivePortalState& state) { |
| 471 FOR_EACH_OBSERVER(Observer, observers_, | 442 FOR_EACH_OBSERVER(Observer, observers_, |
| 472 OnPortalDetectionCompleted(network, state)); | 443 OnPortalDetectionCompleted(network, state)); |
| 473 } | 444 } |
| 474 | 445 |
| 475 base::TimeTicks NetworkPortalDetectorImpl::GetCurrentTimeTicks() const { | 446 base::TimeTicks NetworkPortalDetectorImpl::GetCurrentTimeTicks() const { |
| 476 if (time_ticks_for_testing_.is_null()) | 447 if (time_ticks_for_testing_.is_null()) |
| 477 return base::TimeTicks::Now(); | 448 return base::TimeTicks::Now(); |
| 478 else | 449 else |
| 479 return time_ticks_for_testing_; | 450 return time_ticks_for_testing_; |
| 480 } | 451 } |
| 481 | 452 |
| 482 bool NetworkPortalDetectorImpl::DetectionTimeoutIsCancelledForTesting() const { | 453 bool NetworkPortalDetectorImpl::DetectionTimeoutIsCancelledForTesting() const { |
| 483 return detection_timeout_.IsCancelled(); | 454 return detection_timeout_.IsCancelled(); |
| 484 } | 455 } |
| 485 | 456 |
| 486 } // namespace chromeos | 457 } // namespace chromeos |
| OLD | NEW |