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 CHROMEOS_NETWORK_PORTAL_DETECTOR_NETWORK_PORTAL_DETECTOR_STRATEGY_H_ 6 #define CHROMEOS_NETWORK_PORTAL_DETECTOR_NETWORK_PORTAL_DETECTOR_STRATEGY_H_ 7 8 #include <memory> 9 10 #include "base/compiler_specific.h" 11 #include "base/component_export.h" 12 #include "base/macros.h" 13 #include "base/time/tick_clock.h" 14 #include "base/time/time.h" 15 #include "net/base/backoff_entry.h" 16 17 namespace chromeos { 18 COMPONENT_EXPORT(CHROMEOS_NETWORK)19class COMPONENT_EXPORT(CHROMEOS_NETWORK) PortalDetectorStrategy { 20 public: 21 enum StrategyId { 22 STRATEGY_ID_LOGIN_SCREEN, 23 STRATEGY_ID_ERROR_SCREEN, 24 STRATEGY_ID_SESSION 25 }; 26 27 class Delegate : public base::TickClock { 28 public: 29 ~Delegate() override; 30 31 // Returns number of attempts in a row with NO RESPONSE result. 32 // If last detection attempt has different result, returns 0. 33 virtual int NoResponseResultCount() = 0; 34 35 // Returns time when current attempt was started. 36 virtual base::TimeTicks AttemptStartTime() = 0; 37 }; 38 39 virtual ~PortalDetectorStrategy(); 40 41 // Lifetime of delegate must enclose lifetime of PortalDetectorStrategy. 42 static std::unique_ptr<PortalDetectorStrategy> CreateById(StrategyId id, 43 Delegate* delegate); 44 45 // Returns delay before next detection attempt. This delay is needed 46 // to separate detection attempts in time. 47 base::TimeDelta GetDelayTillNextAttempt(); 48 49 // Returns timeout for the next detection attempt. 50 base::TimeDelta GetNextAttemptTimeout(); 51 52 virtual StrategyId Id() const = 0; 53 54 // Resets strategy to the initial state. 55 void Reset(); 56 57 const net::BackoffEntry::Policy& policy() const { return policy_; } 58 59 // Resets strategy to the initial stater and sets custom policy. 60 void SetPolicyAndReset(const net::BackoffEntry::Policy& policy); 61 62 // Should be called when portal detection is completed and timeout before next 63 // attempt should be adjusted. 64 void OnDetectionCompleted(); 65 66 protected: 67 // Lifetime of delegate must enclose lifetime of PortalDetectorStrategy. 68 explicit PortalDetectorStrategy(Delegate* delegate); 69 70 // Interface for subclasses: 71 virtual base::TimeDelta GetNextAttemptTimeoutImpl() = 0; 72 73 Delegate* delegate_; 74 net::BackoffEntry::Policy policy_; 75 std::unique_ptr<net::BackoffEntry> backoff_entry_; 76 77 private: 78 friend class NetworkPortalDetectorImplTest; 79 friend class NetworkPortalDetectorImplBrowserTest; 80 81 static void set_delay_till_next_attempt_for_testing( 82 const base::TimeDelta& timeout) { 83 delay_till_next_attempt_for_testing_ = timeout; 84 delay_till_next_attempt_for_testing_initialized_ = true; 85 } 86 87 static void set_next_attempt_timeout_for_testing( 88 const base::TimeDelta& timeout) { 89 next_attempt_timeout_for_testing_ = timeout; 90 next_attempt_timeout_for_testing_initialized_ = true; 91 } 92 93 static void reset_fields_for_testing() { 94 delay_till_next_attempt_for_testing_initialized_ = false; 95 next_attempt_timeout_for_testing_initialized_ = false; 96 } 97 98 // Test delay before detection attempt, used by unit tests. 99 static base::TimeDelta delay_till_next_attempt_for_testing_; 100 101 // True when |min_time_between_attempts_for_testing_| is initialized. 102 static bool delay_till_next_attempt_for_testing_initialized_; 103 104 // Test timeout for a detection attempt, used by unit tests. 105 static base::TimeDelta next_attempt_timeout_for_testing_; 106 107 // True when |next_attempt_timeout_for_testing_| is initialized. 108 static bool next_attempt_timeout_for_testing_initialized_; 109 110 DISALLOW_COPY_AND_ASSIGN(PortalDetectorStrategy); 111 }; 112 113 } // namespace chromeos 114 115 #endif // CHROMEOS_NETWORK_PORTAL_DETECTOR_NETWORK_PORTAL_DETECTOR_STRATEGY_H_ 116