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 #ifndef CHROME_BROWSER_CHROMEOS_NET_NETWORK_PORTAL_DETECTOR_STRATEGY_H_ |
| 6 #define CHROME_BROWSER_CHROMEOS_NET_NETWORK_PORTAL_DETECTOR_STRATEGY_H_ |
| 7 |
| 8 #include "base/compiler_specific.h" |
| 9 #include "base/macros.h" |
| 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/time/time.h" |
| 12 |
| 13 namespace chromeos { |
| 14 |
| 15 class PortalDetectorStrategy { |
| 16 public: |
| 17 enum StrategyId { |
| 18 STRATEGY_ID_LOGIN_SCREEN, |
| 19 STRATEGY_ID_ERROR_SCREEN |
| 20 }; |
| 21 |
| 22 class Delegate { |
| 23 public: |
| 24 virtual ~Delegate() {} |
| 25 |
| 26 // Returns number of performed attempts. |
| 27 virtual int AttemptCount() = 0; |
| 28 |
| 29 // Returns time when current attempt was started. |
| 30 virtual base::TimeTicks AttemptStartTime() = 0; |
| 31 |
| 32 // Returns current TimeTicks. |
| 33 virtual base::TimeTicks GetCurrentTimeTicks() = 0; |
| 34 }; |
| 35 |
| 36 virtual ~PortalDetectorStrategy(); |
| 37 |
| 38 static scoped_ptr<PortalDetectorStrategy> CreateById(StrategyId id); |
| 39 |
| 40 void set_delegate(Delegate* delegate) { delegate_ = delegate; } |
| 41 |
| 42 // Returns true when detection attempt can be performed according to |
| 43 // current strategy. |
| 44 bool CanPerformAttempt(); |
| 45 |
| 46 // Returns true if additional attempt could be scheduled after |
| 47 // detection. |
| 48 bool CanPerformAttemptAfterDetection(); |
| 49 |
| 50 // Returns delay before next detection attempt. This delay is needed |
| 51 // to separate detection attempts in time. |
| 52 base::TimeDelta GetDelayTillNextAttempt(); |
| 53 |
| 54 // Returns timeout for the next detection attempt. |
| 55 base::TimeDelta GetNextAttemptTimeout(); |
| 56 |
| 57 virtual StrategyId Id() const = 0; |
| 58 |
| 59 protected: |
| 60 PortalDetectorStrategy(); |
| 61 |
| 62 // Interface for subclasses: |
| 63 virtual bool CanPerformAttemptImpl(); |
| 64 virtual bool CanPerformAttemptAfterDetectionImpl(); |
| 65 virtual base::TimeDelta GetDelayTillNextAttemptImpl(); |
| 66 virtual base::TimeDelta GetNextAttemptTimeoutImpl(); |
| 67 |
| 68 // Adjusts |delay| according to current attempt count and elapsed time |
| 69 // since previous attempt. |
| 70 base::TimeDelta AdjustDelay(const base::TimeDelta& delay); |
| 71 |
| 72 Delegate* delegate_; |
| 73 |
| 74 private: |
| 75 friend class NetworkPortalDetectorImplTest; |
| 76 |
| 77 static void set_delay_till_next_attempt_for_testing( |
| 78 const base::TimeDelta& timeout) { |
| 79 delay_till_next_attempt_for_testing_ = timeout; |
| 80 delay_till_next_attempt_for_testing_initialized_ = true; |
| 81 } |
| 82 |
| 83 static void set_next_attempt_timeout_for_testing( |
| 84 const base::TimeDelta& timeout) { |
| 85 next_attempt_timeout_for_testing_ = timeout; |
| 86 next_attempt_timeout_for_testing_initialized_ = true; |
| 87 } |
| 88 |
| 89 static void reset_fields_for_testing() { |
| 90 delay_till_next_attempt_for_testing_initialized_ = false; |
| 91 next_attempt_timeout_for_testing_initialized_ = false; |
| 92 } |
| 93 |
| 94 // Test delay before detection attempt, used by unit tests. |
| 95 static base::TimeDelta delay_till_next_attempt_for_testing_; |
| 96 |
| 97 // True when |min_time_between_attempts_for_testing_| is initialized. |
| 98 static bool delay_till_next_attempt_for_testing_initialized_; |
| 99 |
| 100 // Test timeout for a detection attempt, used by unit tests. |
| 101 static base::TimeDelta next_attempt_timeout_for_testing_; |
| 102 |
| 103 // True when |next_attempt_timeout_for_testing_| is initialized. |
| 104 static bool next_attempt_timeout_for_testing_initialized_; |
| 105 |
| 106 DISALLOW_COPY_AND_ASSIGN(PortalDetectorStrategy); |
| 107 }; |
| 108 |
| 109 } // namespace chromeos |
| 110 |
| 111 #endif // CHROME_BROWSER_CHROMEOS_NET_NETWORK_PORTAL_DETECTOR_STRATEGY_H_ |
OLD | NEW |