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