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 class LoginScreenStrategy : public PortalDetectorStrategy { |
| 21 public: |
| 22 static const int kMaxAttempts = 3; |
| 23 static const int kDelayBetweenAttemptsSec = 3; |
| 24 static const int kBaseAttemptTimeoutSec = 5; |
| 25 |
| 26 LoginScreenStrategy() {} |
| 27 virtual ~LoginScreenStrategy() {} |
| 28 |
| 29 protected: |
| 30 // PortalDetectorStrategy overrides: |
| 31 virtual StrategyId Id() const OVERRIDE { return STRATEGY_ID_LOGIN_SCREEN; } |
| 32 virtual bool CanPerformAttemptImpl() OVERRIDE { |
| 33 return delegate_->AttemptCount() < kMaxAttempts; |
| 34 } |
| 35 virtual base::TimeDelta GetDelayTillNextAttemptImpl() OVERRIDE { |
| 36 return AdjustDelay(base::TimeDelta::FromSeconds(kDelayBetweenAttemptsSec)); |
| 37 } |
| 38 virtual base::TimeDelta GetNextAttemptTimeoutImpl() OVERRIDE { |
| 39 int timeout = DefaultNetwork() |
| 40 ? (delegate_->AttemptCount() + 1) * kBaseAttemptTimeoutSec |
| 41 : kBaseAttemptTimeoutSec; |
| 42 return base::TimeDelta::FromSeconds(timeout); |
| 43 } |
| 44 |
| 45 private: |
| 46 DISALLOW_COPY_AND_ASSIGN(LoginScreenStrategy); |
| 47 }; |
| 48 |
| 49 class ErrorScreenStrategy : public PortalDetectorStrategy { |
| 50 public: |
| 51 static const int kDelayBetweenAttemptsSec = 3; |
| 52 static const int kAttemptTimeoutSec = 15; |
| 53 |
| 54 ErrorScreenStrategy() {} |
| 55 virtual ~ErrorScreenStrategy() {} |
| 56 |
| 57 protected: |
| 58 // PortalDetectorStrategy overrides: |
| 59 virtual StrategyId Id() const OVERRIDE { return STRATEGY_ID_ERROR_SCREEN; } |
| 60 virtual bool CanPerformAttemptImpl() OVERRIDE { return true; } |
| 61 virtual bool CanPerformAttemptAfterDetectionImpl() OVERRIDE { return true; } |
| 62 virtual base::TimeDelta GetDelayTillNextAttemptImpl() OVERRIDE { |
| 63 return AdjustDelay(base::TimeDelta::FromSeconds(kDelayBetweenAttemptsSec)); |
| 64 } |
| 65 virtual base::TimeDelta GetNextAttemptTimeoutImpl() OVERRIDE { |
| 66 return base::TimeDelta::FromSeconds(kAttemptTimeoutSec); |
| 67 } |
| 68 |
| 69 private: |
| 70 DISALLOW_COPY_AND_ASSIGN(ErrorScreenStrategy); |
| 71 }; |
| 72 |
| 73 } // namespace |
| 74 |
| 75 // PortalDetectorStrategy ----------------------------------------------------- |
| 76 |
| 77 // static |
| 78 base::TimeDelta PortalDetectorStrategy::delay_till_next_attempt_for_testing_; |
| 79 |
| 80 // static |
| 81 bool PortalDetectorStrategy::delay_till_next_attempt_for_testing_initialized_ = |
| 82 false; |
| 83 |
| 84 // static |
| 85 base::TimeDelta PortalDetectorStrategy::next_attempt_timeout_for_testing_; |
| 86 |
| 87 // static |
| 88 bool PortalDetectorStrategy::next_attempt_timeout_for_testing_initialized_ = |
| 89 false; |
| 90 |
| 91 PortalDetectorStrategy::PortalDetectorStrategy() : delegate_(NULL) {} |
| 92 |
| 93 PortalDetectorStrategy::~PortalDetectorStrategy() {} |
| 94 |
| 95 // statc |
| 96 scoped_ptr<PortalDetectorStrategy> PortalDetectorStrategy::CreateById( |
| 97 StrategyId id) { |
| 98 switch (id) { |
| 99 case STRATEGY_ID_LOGIN_SCREEN: |
| 100 return scoped_ptr<PortalDetectorStrategy>(new LoginScreenStrategy()); |
| 101 case STRATEGY_ID_ERROR_SCREEN: |
| 102 return scoped_ptr<PortalDetectorStrategy>(new ErrorScreenStrategy()); |
| 103 default: |
| 104 NOTREACHED(); |
| 105 return scoped_ptr<PortalDetectorStrategy>( |
| 106 static_cast<PortalDetectorStrategy*>(NULL)); |
| 107 } |
| 108 } |
| 109 |
| 110 bool PortalDetectorStrategy::CanPerformAttempt() { |
| 111 return CanPerformAttemptImpl(); |
| 112 } |
| 113 |
| 114 bool PortalDetectorStrategy::CanPerformAttemptAfterDetection() { |
| 115 return CanPerformAttemptAfterDetectionImpl(); |
| 116 } |
| 117 |
| 118 base::TimeDelta PortalDetectorStrategy::GetDelayTillNextAttempt() { |
| 119 if (delay_till_next_attempt_for_testing_initialized_) |
| 120 return delay_till_next_attempt_for_testing_; |
| 121 return GetDelayTillNextAttemptImpl(); |
| 122 } |
| 123 |
| 124 base::TimeDelta PortalDetectorStrategy::GetNextAttemptTimeout() { |
| 125 if (next_attempt_timeout_for_testing_initialized_) |
| 126 return next_attempt_timeout_for_testing_; |
| 127 return GetNextAttemptTimeoutImpl(); |
| 128 } |
| 129 |
| 130 bool PortalDetectorStrategy::CanPerformAttemptImpl() { return false; } |
| 131 |
| 132 bool PortalDetectorStrategy::CanPerformAttemptAfterDetectionImpl() { |
| 133 return false; |
| 134 } |
| 135 |
| 136 base::TimeDelta PortalDetectorStrategy::GetDelayTillNextAttemptImpl() { |
| 137 return base::TimeDelta(); |
| 138 } |
| 139 |
| 140 base::TimeDelta PortalDetectorStrategy::GetNextAttemptTimeoutImpl() { |
| 141 return base::TimeDelta(); |
| 142 } |
| 143 |
| 144 base::TimeDelta PortalDetectorStrategy::AdjustDelay( |
| 145 const base::TimeDelta& delay) { |
| 146 if (!delegate_->AttemptCount()) |
| 147 return base::TimeDelta(); |
| 148 |
| 149 base::TimeTicks now = delegate_->GetCurrentTimeTicks(); |
| 150 base::TimeDelta elapsed; |
| 151 if (now > delegate_->AttemptStartTime()) |
| 152 elapsed = now - delegate_->AttemptStartTime(); |
| 153 if (delay > elapsed) |
| 154 return delay - elapsed; |
| 155 return base::TimeDelta(); |
| 156 } |
| 157 |
| 158 } // namespace chromeos |
OLD | NEW |