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