| 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 bool NetworkPortalDetectorImpl::StartDetectionIfIdle() { | 178 bool NetworkPortalDetectorImpl::StartDetectionIfIdle() { |
| 199 if (IsPortalCheckPending() || IsCheckingForPortal()) | 179 if (IsPortalCheckPending() || IsCheckingForPortal()) |
| 200 return false; | 180 return false; |
| 201 DetectCaptivePortal(base::TimeDelta()); | 181 DetectCaptivePortal(base::TimeDelta()); |
| 202 return true; | 182 return true; |
| 203 } | 183 } |
| 204 | 184 |
| 205 void NetworkPortalDetectorImpl::EnableLazyDetection() { | 185 void NetworkPortalDetectorImpl::EnableLazyDetection() { |
| 206 if (lazy_detection_enabled()) | 186 if (lazy_detection_enabled()) |
| 207 return; | 187 return; |
| 208 lazy_detection_enabled_ = true; | 188 lazy_detection_enabled_ = true; |
| 209 VLOG(1) << "Lazy detection mode enabled."; | 189 VLOG(1) << "Lazy detection mode enabled."; |
| 210 StartDetectionIfIdle(); | 190 StartDetectionIfIdle(); |
| 211 } | 191 } |
| 212 | 192 |
| 213 void NetworkPortalDetectorImpl::DisableLazyDetection() { | 193 void NetworkPortalDetectorImpl::DisableLazyDetection() { |
| 214 CHECK(lazy_detection_enabled()); | 194 CHECK(lazy_detection_enabled()); |
| 215 lazy_detection_enabled_ = false; | 195 lazy_detection_enabled_ = false; |
| 216 VLOG(1) << "Lazy detection mode disabled."; | 196 VLOG(1) << "Lazy detection mode disabled."; |
| 217 } | 197 } |
| 218 | 198 |
| 219 void NetworkPortalDetectorImpl::OnNetworkManagerChanged(NetworkLibrary* cros) { | 199 void NetworkPortalDetectorImpl::NetworkManagerChanged() { |
| 220 DCHECK(CalledOnValidThread()); | 200 DCHECK(CalledOnValidThread()); |
| 221 CHECK(cros); | 201 const NetworkState* default_network = |
| 222 const Network* active_network = cros->active_network(); | 202 NetworkStateHandler::Get()->DefaultNetwork(); |
| 223 if (!active_network) { | 203 if (!default_network) { |
| 224 active_network_id_.clear(); | 204 default_network_id_.clear(); |
| 225 return; | 205 return; |
| 226 } | 206 } |
| 227 | 207 |
| 228 active_network_id_ = active_network->unique_id(); | 208 default_network_id_ = default_network->guid(); |
| 229 | 209 |
| 230 bool network_changed = | 210 bool network_changed = (default_service_path_ != default_network->path()); |
| 231 (active_service_path_ != active_network->service_path()); | 211 default_service_path_ = default_network->path(); |
| 232 if (network_changed) { | |
| 233 if (!active_service_path_.empty()) | |
| 234 cros->RemoveNetworkObserver(active_service_path_, this); | |
| 235 active_service_path_ = active_network->service_path(); | |
| 236 cros->AddNetworkObserver(active_service_path_, this); | |
| 237 } | |
| 238 | 212 |
| 239 bool connection_state_changed = | 213 bool connection_state_changed = (default_connection_state_ != |
| 240 (active_connection_state_ != active_network->connection_state()); | 214 default_network->connection_state()); |
| 241 active_connection_state_ = active_network->connection_state(); | 215 default_connection_state_ = default_network->connection_state(); |
| 242 | 216 |
| 243 if (network_changed || connection_state_changed) { | 217 if (network_changed || connection_state_changed) { |
| 244 attempt_count_ = 0; | 218 attempt_count_ = 0; |
| 245 CancelPortalDetection(); | 219 CancelPortalDetection(); |
| 246 } | 220 } |
| 247 | 221 |
| 248 if (!IsCheckingForPortal() && !IsPortalCheckPending() && | 222 if (!IsCheckingForPortal() && !IsPortalCheckPending() && |
| 249 Network::IsConnectedState(active_connection_state_) && | 223 NetworkState::StateIsConnected(default_connection_state_) && |
| 250 (attempt_count_ < kMaxRequestAttempts || lazy_detection_enabled())) { | 224 (attempt_count_ < kMaxRequestAttempts || lazy_detection_enabled())) { |
| 251 DCHECK(active_network); | |
| 252 | |
| 253 // Initiate Captive Portal detection if network's captive | 225 // Initiate Captive Portal detection if network's captive |
| 254 // portal state is unknown (e.g. for freshly created networks), | 226 // portal state is unknown (e.g. for freshly created networks), |
| 255 // offline or if network connection state was changed. | 227 // offline or if network connection state was changed. |
| 256 CaptivePortalState state = GetCaptivePortalState(active_network); | 228 CaptivePortalState state = GetCaptivePortalState(default_network); |
| 257 if (state.status == CAPTIVE_PORTAL_STATUS_UNKNOWN || | 229 if (state.status == CAPTIVE_PORTAL_STATUS_UNKNOWN || |
| 258 state.status == CAPTIVE_PORTAL_STATUS_OFFLINE || | 230 state.status == CAPTIVE_PORTAL_STATUS_OFFLINE || |
| 259 (!network_changed && connection_state_changed)) { | 231 (!network_changed && connection_state_changed)) { |
| 260 DetectCaptivePortal(base::TimeDelta()); | 232 DetectCaptivePortal(base::TimeDelta()); |
| 261 } | 233 } |
| 262 } | 234 } |
| 263 } | 235 } |
| 264 | 236 |
| 265 void NetworkPortalDetectorImpl::OnNetworkChanged( | 237 void NetworkPortalDetectorImpl::DefaultNetworkChanged( |
| 266 chromeos::NetworkLibrary* cros, | 238 const NetworkState* network) { |
| 267 const chromeos::Network* network) { | 239 NetworkManagerChanged(); |
| 268 DCHECK(CalledOnValidThread()); | |
| 269 OnNetworkManagerChanged(cros); | |
| 270 } | 240 } |
| 271 | 241 |
| 272 void NetworkPortalDetectorImpl::DetectCaptivePortal( | 242 void NetworkPortalDetectorImpl::DetectCaptivePortal( |
| 273 const base::TimeDelta& delay) { | 243 const base::TimeDelta& delay) { |
| 274 DCHECK(!IsPortalCheckPending()); | 244 DCHECK(!IsPortalCheckPending()); |
| 275 DCHECK(!IsCheckingForPortal()); | 245 DCHECK(!IsCheckingForPortal()); |
| 276 DCHECK(attempt_count_ < kMaxRequestAttempts || lazy_detection_enabled()); | 246 DCHECK(attempt_count_ < kMaxRequestAttempts || lazy_detection_enabled()); |
| 277 | 247 |
| 278 if (!IsEnabled()) | 248 if (!IsEnabled()) |
| 279 return; | 249 return; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 311 | 281 |
| 312 void NetworkPortalDetectorImpl::DetectCaptivePortalTask() { | 282 void NetworkPortalDetectorImpl::DetectCaptivePortalTask() { |
| 313 DCHECK(IsPortalCheckPending()); | 283 DCHECK(IsPortalCheckPending()); |
| 314 | 284 |
| 315 state_ = STATE_CHECKING_FOR_PORTAL; | 285 state_ = STATE_CHECKING_FOR_PORTAL; |
| 316 attempt_start_time_ = GetCurrentTimeTicks(); | 286 attempt_start_time_ = GetCurrentTimeTicks(); |
| 317 | 287 |
| 318 if (attempt_count_ < kMaxRequestAttempts) { | 288 if (attempt_count_ < kMaxRequestAttempts) { |
| 319 ++attempt_count_; | 289 ++attempt_count_; |
| 320 VLOG(1) << "Portal detection started: " | 290 VLOG(1) << "Portal detection started: " |
| 321 << "network=" << active_network_id_ << ", " | 291 << "network=" << default_network_id_ << ", " |
| 322 << "attempt=" << attempt_count_ << " of " << kMaxRequestAttempts; | 292 << "attempt=" << attempt_count_ << " of " << kMaxRequestAttempts; |
| 323 } else { | 293 } else { |
| 324 DCHECK(lazy_detection_enabled()); | 294 DCHECK(lazy_detection_enabled()); |
| 325 VLOG(1) << "Lazy portal detection attempt started"; | 295 VLOG(1) << "Lazy portal detection attempt started"; |
| 326 } | 296 } |
| 327 | 297 |
| 328 captive_portal_detector_->DetectCaptivePortal( | 298 captive_portal_detector_->DetectCaptivePortal( |
| 329 test_url_, | 299 test_url_, |
| 330 base::Bind(&NetworkPortalDetectorImpl::OnPortalDetectionCompleted, | 300 base::Bind(&NetworkPortalDetectorImpl::OnPortalDetectionCompleted, |
| 331 weak_ptr_factory_.GetWeakPtr())); | 301 weak_ptr_factory_.GetWeakPtr())); |
| 332 detection_timeout_.Reset( | 302 detection_timeout_.Reset( |
| 333 base::Bind(&NetworkPortalDetectorImpl::PortalDetectionTimeout, | 303 base::Bind(&NetworkPortalDetectorImpl::PortalDetectionTimeout, |
| 334 weak_ptr_factory_.GetWeakPtr())); | 304 weak_ptr_factory_.GetWeakPtr())); |
| 335 MessageLoop::current()->PostDelayedTask(FROM_HERE, | 305 MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 336 detection_timeout_.callback(), | 306 detection_timeout_.callback(), |
| 337 request_timeout_); | 307 request_timeout_); |
| 338 } | 308 } |
| 339 | 309 |
| 340 void NetworkPortalDetectorImpl::PortalDetectionTimeout() { | 310 void NetworkPortalDetectorImpl::PortalDetectionTimeout() { |
| 341 DCHECK(CalledOnValidThread()); | 311 DCHECK(CalledOnValidThread()); |
| 342 DCHECK(IsCheckingForPortal()); | 312 DCHECK(IsCheckingForPortal()); |
| 343 | 313 |
| 344 VLOG(1) << "Portal detection timeout: network=" << active_network_id_; | 314 VLOG(1) << "Portal detection timeout: network=" << default_network_id_; |
| 345 | 315 |
| 346 captive_portal_detector_->Cancel(); | 316 captive_portal_detector_->Cancel(); |
| 347 CaptivePortalDetector::Results results; | 317 CaptivePortalDetector::Results results; |
| 348 results.result = captive_portal::RESULT_NO_RESPONSE; | 318 results.result = captive_portal::RESULT_NO_RESPONSE; |
| 349 OnPortalDetectionCompleted(results); | 319 OnPortalDetectionCompleted(results); |
| 350 } | 320 } |
| 351 | 321 |
| 352 void NetworkPortalDetectorImpl::CancelPortalDetection() { | 322 void NetworkPortalDetectorImpl::CancelPortalDetection() { |
| 353 if (IsPortalCheckPending()) | 323 if (IsPortalCheckPending()) |
| 354 detection_task_.Cancel(); | 324 detection_task_.Cancel(); |
| 355 else if (IsCheckingForPortal()) | 325 else if (IsCheckingForPortal()) |
| 356 captive_portal_detector_->Cancel(); | 326 captive_portal_detector_->Cancel(); |
| 357 detection_timeout_.Cancel(); | 327 detection_timeout_.Cancel(); |
| 358 state_ = STATE_IDLE; | 328 state_ = STATE_IDLE; |
| 359 } | 329 } |
| 360 | 330 |
| 361 void NetworkPortalDetectorImpl::OnPortalDetectionCompleted( | 331 void NetworkPortalDetectorImpl::OnPortalDetectionCompleted( |
| 362 const CaptivePortalDetector::Results& results) { | 332 const CaptivePortalDetector::Results& results) { |
| 363 DCHECK(CalledOnValidThread()); | 333 DCHECK(CalledOnValidThread()); |
| 364 DCHECK(IsCheckingForPortal()); | 334 DCHECK(IsCheckingForPortal()); |
| 365 | 335 |
| 366 VLOG(1) << "Portal detection completed: " | 336 VLOG(1) << "Portal detection completed: " |
| 367 << "network=" << active_network_id_ << ", " | 337 << "network=" << default_network_id_ << ", " |
| 368 << "result=" << CaptivePortalDetector::CaptivePortalResultToString( | 338 << "result=" << CaptivePortalDetector::CaptivePortalResultToString( |
| 369 results.result) << ", " | 339 results.result) << ", " |
| 370 << "response_code=" << results.response_code; | 340 << "response_code=" << results.response_code; |
| 371 | 341 |
| 372 state_ = STATE_IDLE; | 342 state_ = STATE_IDLE; |
| 373 detection_timeout_.Cancel(); | 343 detection_timeout_.Cancel(); |
| 374 | 344 |
| 375 NetworkLibrary* cros = GetNetworkLibrary(); | 345 const NetworkState* default_network = |
| 376 const Network* active_network = cros->active_network(); | 346 NetworkStateHandler::Get()->DefaultNetwork(); |
| 377 | 347 |
| 378 CaptivePortalState state; | 348 CaptivePortalState state; |
| 379 state.response_code = results.response_code; | 349 state.response_code = results.response_code; |
| 380 switch (results.result) { | 350 switch (results.result) { |
| 381 case captive_portal::RESULT_NO_RESPONSE: | 351 case captive_portal::RESULT_NO_RESPONSE: |
| 382 if (attempt_count_ >= kMaxRequestAttempts) { | 352 if (attempt_count_ >= kMaxRequestAttempts) { |
| 383 if (state.response_code == net::HTTP_PROXY_AUTHENTICATION_REQUIRED) { | 353 if (state.response_code == net::HTTP_PROXY_AUTHENTICATION_REQUIRED) { |
| 384 state.status = CAPTIVE_PORTAL_STATUS_PROXY_AUTH_REQUIRED; | 354 state.status = CAPTIVE_PORTAL_STATUS_PROXY_AUTH_REQUIRED; |
| 385 } else if (active_network && active_network->restricted_pool()) { | 355 } else if (default_network && (default_network->connection_state() == |
| 356 flimflam::kStatePortal)) { |
| 386 // Take into account shill's detection results. | 357 // Take into account shill's detection results. |
| 387 state.status = CAPTIVE_PORTAL_STATUS_PORTAL; | 358 state.status = CAPTIVE_PORTAL_STATUS_PORTAL; |
| 388 LOG(WARNING) << "Network " << active_network->unique_id() << " " | 359 LOG(WARNING) << "Network " << default_network->guid() << " " |
| 389 << "is marked as " | 360 << "is marked as " |
| 390 << CaptivePortalStatusString(state.status) << " " | 361 << CaptivePortalStatusString(state.status) << " " |
| 391 << "despite the fact that CaptivePortalDetector " | 362 << "despite the fact that CaptivePortalDetector " |
| 392 << "received no response"; | 363 << "received no response"; |
| 393 } else { | 364 } else { |
| 394 state.status = CAPTIVE_PORTAL_STATUS_OFFLINE; | 365 state.status = CAPTIVE_PORTAL_STATUS_OFFLINE; |
| 395 } | 366 } |
| 396 SetCaptivePortalState(active_network, state); | 367 SetCaptivePortalState(default_network, state); |
| 397 } else { | 368 } else { |
| 398 DetectCaptivePortal(results.retry_after_delta); | 369 DetectCaptivePortal(results.retry_after_delta); |
| 399 } | 370 } |
| 400 break; | 371 break; |
| 401 case captive_portal::RESULT_INTERNET_CONNECTED: | 372 case captive_portal::RESULT_INTERNET_CONNECTED: |
| 402 state.status = CAPTIVE_PORTAL_STATUS_ONLINE; | 373 state.status = CAPTIVE_PORTAL_STATUS_ONLINE; |
| 403 SetCaptivePortalState(active_network, state); | 374 SetCaptivePortalState(default_network, state); |
| 404 break; | 375 break; |
| 405 case captive_portal::RESULT_BEHIND_CAPTIVE_PORTAL: | 376 case captive_portal::RESULT_BEHIND_CAPTIVE_PORTAL: |
| 406 state.status = CAPTIVE_PORTAL_STATUS_PORTAL; | 377 state.status = CAPTIVE_PORTAL_STATUS_PORTAL; |
| 407 SetCaptivePortalState(active_network, state); | 378 SetCaptivePortalState(default_network, state); |
| 408 break; | 379 break; |
| 409 default: | 380 default: |
| 410 break; | 381 break; |
| 411 } | 382 } |
| 412 | 383 |
| 413 TryLazyDetection(); | 384 TryLazyDetection(); |
| 414 } | 385 } |
| 415 | 386 |
| 416 void NetworkPortalDetectorImpl::TryLazyDetection() { | 387 void NetworkPortalDetectorImpl::TryLazyDetection() { |
| 417 if (!IsPortalCheckPending() && !IsCheckingForPortal() && | 388 if (!IsPortalCheckPending() && !IsCheckingForPortal() && |
| (...skipping 20 matching lines...) Expand all Loading... |
| 438 | 409 |
| 439 bool NetworkPortalDetectorImpl::IsPortalCheckPending() const { | 410 bool NetworkPortalDetectorImpl::IsPortalCheckPending() const { |
| 440 return state_ == STATE_PORTAL_CHECK_PENDING; | 411 return state_ == STATE_PORTAL_CHECK_PENDING; |
| 441 } | 412 } |
| 442 | 413 |
| 443 bool NetworkPortalDetectorImpl::IsCheckingForPortal() const { | 414 bool NetworkPortalDetectorImpl::IsCheckingForPortal() const { |
| 444 return state_ == STATE_CHECKING_FOR_PORTAL; | 415 return state_ == STATE_CHECKING_FOR_PORTAL; |
| 445 } | 416 } |
| 446 | 417 |
| 447 void NetworkPortalDetectorImpl::SetCaptivePortalState( | 418 void NetworkPortalDetectorImpl::SetCaptivePortalState( |
| 448 const Network* network, | 419 const NetworkState* network, |
| 449 const CaptivePortalState& state) { | 420 const CaptivePortalState& state) { |
| 450 if (!detection_start_time_.is_null()) { | 421 if (!detection_start_time_.is_null()) { |
| 451 UMA_HISTOGRAM_TIMES("CaptivePortal.OOBE.DetectionDuration", | 422 UMA_HISTOGRAM_TIMES("CaptivePortal.OOBE.DetectionDuration", |
| 452 GetCurrentTimeTicks() - detection_start_time_); | 423 GetCurrentTimeTicks() - detection_start_time_); |
| 453 } | 424 } |
| 454 | 425 |
| 455 if (!network) { | 426 if (!network) { |
| 456 NotifyPortalDetectionCompleted(network, state); | 427 NotifyPortalDetectionCompleted(network, state); |
| 457 return; | 428 return; |
| 458 } | 429 } |
| 459 | 430 |
| 460 CaptivePortalStateMap::const_iterator it = | 431 CaptivePortalStateMap::const_iterator it = |
| 461 portal_state_map_.find(network->service_path()); | 432 portal_state_map_.find(network->path()); |
| 462 if (it == portal_state_map_.end() || | 433 if (it == portal_state_map_.end() || |
| 463 it->second.status != state.status || | 434 it->second.status != state.status || |
| 464 it->second.response_code != state.response_code) { | 435 it->second.response_code != state.response_code) { |
| 465 VLOG(1) << "Updating Chrome Captive Portal state: " | 436 VLOG(1) << "Updating Chrome Captive Portal state: " |
| 466 << "network=" << network->unique_id() << ", " | 437 << "network=" << network->guid() << ", " |
| 467 << "status=" << CaptivePortalStatusString(state.status) << ", " | 438 << "status=" << CaptivePortalStatusString(state.status) << ", " |
| 468 << "response_code=" << state.response_code; | 439 << "response_code=" << state.response_code; |
| 469 portal_state_map_[network->service_path()] = state; | 440 portal_state_map_[network->path()] = state; |
| 470 } | 441 } |
| 471 NotifyPortalDetectionCompleted(network, state); | 442 NotifyPortalDetectionCompleted(network, state); |
| 472 } | 443 } |
| 473 | 444 |
| 474 void NetworkPortalDetectorImpl::NotifyPortalDetectionCompleted( | 445 void NetworkPortalDetectorImpl::NotifyPortalDetectionCompleted( |
| 475 const Network* network, | 446 const NetworkState* network, |
| 476 const CaptivePortalState& state) { | 447 const CaptivePortalState& state) { |
| 477 FOR_EACH_OBSERVER(Observer, observers_, | 448 FOR_EACH_OBSERVER(Observer, observers_, |
| 478 OnPortalDetectionCompleted(network, state)); | 449 OnPortalDetectionCompleted(network, state)); |
| 479 } | 450 } |
| 480 | 451 |
| 481 base::TimeTicks NetworkPortalDetectorImpl::GetCurrentTimeTicks() const { | 452 base::TimeTicks NetworkPortalDetectorImpl::GetCurrentTimeTicks() const { |
| 482 if (time_ticks_for_testing_.is_null()) | 453 if (time_ticks_for_testing_.is_null()) |
| 483 return base::TimeTicks::Now(); | 454 return base::TimeTicks::Now(); |
| 484 else | 455 else |
| 485 return time_ticks_for_testing_; | 456 return time_ticks_for_testing_; |
| 486 } | 457 } |
| 487 | 458 |
| 488 bool NetworkPortalDetectorImpl::DetectionTimeoutIsCancelledForTesting() const { | 459 bool NetworkPortalDetectorImpl::DetectionTimeoutIsCancelledForTesting() const { |
| 489 return detection_timeout_.IsCancelled(); | 460 return detection_timeout_.IsCancelled(); |
| 490 } | 461 } |
| 491 | 462 |
| 492 } // namespace chromeos | 463 } // namespace chromeos |
| OLD | NEW |