OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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 #include "chrome/browser/chromeos/net/network_portal_detector_strategy.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "chromeos/network/network_handler.h" |
| 9 #include "chromeos/network/network_state.h" |
| 10 #include "chromeos/network/network_state_handler.h" |
| 11 |
| 12 namespace chromeos { |
| 13 |
| 14 namespace { |
| 15 |
| 16 const NetworkState* DefaultNetwork() { |
| 17 return NetworkHandler::Get()->network_state_handler()->DefaultNetwork(); |
| 18 } |
| 19 |
| 20 std::string DefaultNetworkName() { |
| 21 const NetworkState* network = DefaultNetwork(); |
| 22 return network ? network->name() : std::string(); |
| 23 } |
| 24 |
| 25 } // namespace |
| 26 |
| 27 // PortalDetectorStrategy ----------------------------------------------------- |
| 28 |
| 29 // static |
| 30 base::TimeDelta PortalDetectorStrategy::request_timeout_for_testing_; |
| 31 |
| 32 // static |
| 33 bool PortalDetectorStrategy::request_timeout_for_testing_initialized_ = false; |
| 34 |
| 35 // static |
| 36 base::TimeDelta PortalDetectorStrategy::min_time_between_attempts_for_testing_; |
| 37 |
| 38 // static |
| 39 bool |
| 40 PortalDetectorStrategy::min_time_between_attempts_for_testing_initialized_ = |
| 41 false; |
| 42 |
| 43 PortalDetectorStrategy::PortalDetectorStrategy(Delegate* delegate) |
| 44 : delegate_(delegate) {} |
| 45 PortalDetectorStrategy::~PortalDetectorStrategy() {} |
| 46 |
| 47 void PortalDetectorStrategy::Reset() { ResetImpl(); } |
| 48 |
| 49 bool PortalDetectorStrategy::CanPerformAttempt() { |
| 50 return CanPerformAttemptImpl(); |
| 51 } |
| 52 |
| 53 base::TimeDelta PortalDetectorStrategy::GetDelayBeforeCurrentAttempt() { |
| 54 if (min_time_between_attempts_for_testing_initialized_) |
| 55 return min_time_between_attempts_for_testing_; |
| 56 return GetDelayBeforeCurrentAttemptImpl(); |
| 57 } |
| 58 |
| 59 base::TimeDelta PortalDetectorStrategy::GetCurrentAttemptTimeout() { |
| 60 if (request_timeout_for_testing_initialized_) |
| 61 return request_timeout_for_testing_; |
| 62 return GetCurrentAttemptTimeoutImpl(); |
| 63 } |
| 64 |
| 65 void PortalDetectorStrategy::OnAttemptStarted() { |
| 66 attempt_start_time_ = GetCurrentTimeTicks(); |
| 67 OnAttemptStartedImpl(); |
| 68 } |
| 69 |
| 70 void PortalDetectorStrategy::OnDetectionCompleted() { |
| 71 return OnDetectionCompletedImpl(); |
| 72 } |
| 73 |
| 74 void PortalDetectorStrategy::ResetImpl() {} |
| 75 |
| 76 bool PortalDetectorStrategy::CanPerformAttemptImpl() { return false; } |
| 77 |
| 78 base::TimeDelta PortalDetectorStrategy::GetDelayBeforeCurrentAttemptImpl() { |
| 79 return base::TimeDelta(); |
| 80 } |
| 81 |
| 82 base::TimeDelta PortalDetectorStrategy::GetCurrentAttemptTimeoutImpl() { |
| 83 return base::TimeDelta(); |
| 84 } |
| 85 |
| 86 void PortalDetectorStrategy::OnAttemptStartedImpl() {} |
| 87 |
| 88 void PortalDetectorStrategy::OnDetectionCompletedImpl() {} |
| 89 |
| 90 // LoginScreenStrategy --------------------------------------------------------- |
| 91 |
| 92 LoginScreenStrategy::LoginScreenStrategy( |
| 93 PortalDetectorStrategy::Delegate* delegate) |
| 94 : PortalDetectorStrategy(delegate), |
| 95 attempt_count_(0), |
| 96 detection_completed_(false) {} |
| 97 |
| 98 LoginScreenStrategy::~LoginScreenStrategy() {} |
| 99 |
| 100 void LoginScreenStrategy::ResetImpl() { |
| 101 attempt_count_ = 0; |
| 102 detection_completed_ = false; |
| 103 } |
| 104 |
| 105 bool LoginScreenStrategy::CanPerformAttemptImpl() { |
| 106 return !detection_completed_ && attempt_count_ < kMaxRequestAttempts; |
| 107 } |
| 108 |
| 109 base::TimeDelta LoginScreenStrategy::GetDelayBeforeCurrentAttemptImpl() { |
| 110 DCHECK(CanPerformAttempt()); |
| 111 if (!attempt_count_) |
| 112 return base::TimeDelta(); |
| 113 |
| 114 base::TimeTicks now = GetCurrentTimeTicks(); |
| 115 base::TimeDelta delay = |
| 116 base::TimeDelta::FromSeconds(kMinTimeBetweenAttemptsSec); |
| 117 base::TimeDelta elapsed; |
| 118 if (now > attempt_start_time_) |
| 119 elapsed = now - attempt_start_time_; |
| 120 if (delay > elapsed) |
| 121 return delay - elapsed; |
| 122 return base::TimeDelta(); |
| 123 } |
| 124 |
| 125 base::TimeDelta LoginScreenStrategy::GetCurrentAttemptTimeoutImpl() { |
| 126 int timeout = DefaultNetwork() ? attempt_count_ * kBaseRequestTimeoutSec |
| 127 : kBaseRequestTimeoutSec; |
| 128 return base::TimeDelta::FromSeconds(timeout); |
| 129 } |
| 130 |
| 131 void LoginScreenStrategy::OnAttemptStartedImpl() { |
| 132 ++attempt_count_; |
| 133 VLOG(1) << "Detection attempt on login screen started: " |
| 134 << "name=" << DefaultNetworkName() << ", " |
| 135 << "attempt=" << attempt_count_ << " of " << kMaxRequestAttempts |
| 136 << "."; |
| 137 } |
| 138 |
| 139 void LoginScreenStrategy::OnDetectionCompletedImpl() { |
| 140 detection_completed_ = true; |
| 141 } |
| 142 |
| 143 // ErrorScreenStrategy -------------------------------------------------------- |
| 144 |
| 145 ErrorScreenStrategy::ErrorScreenStrategy( |
| 146 PortalDetectorStrategy::Delegate* delegate) |
| 147 : PortalDetectorStrategy(delegate), |
| 148 delay_between_attempts_( |
| 149 base::TimeDelta::FromSeconds(kMinTimeBetweenAttemptsSec)) {} |
| 150 |
| 151 ErrorScreenStrategy::~ErrorScreenStrategy() {} |
| 152 |
| 153 bool ErrorScreenStrategy::CanPerformAttemptImpl() { return true; } |
| 154 |
| 155 base::TimeDelta ErrorScreenStrategy::GetDelayBeforeCurrentAttemptImpl() { |
| 156 return base::TimeDelta::FromSeconds(kMinTimeBetweenAttemptsSec); |
| 157 } |
| 158 |
| 159 base::TimeDelta ErrorScreenStrategy::GetCurrentAttemptTimeoutImpl() { |
| 160 return base::TimeDelta::FromSeconds(kRequestTimeoutSec); |
| 161 } |
| 162 |
| 163 void ErrorScreenStrategy::OnAttemptStartedImpl() { |
| 164 VLOG(1) << "Detection attempt on error screen started: " |
| 165 << "name=" << DefaultNetworkName(); |
| 166 } |
| 167 |
| 168 } // namespace chromeos |
OLD | NEW |