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