1 // Copyright (c) 2016 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_TEST_CHROMEDRIVER_NET_TIMEOUT_H_
6 #define CHROME_TEST_CHROMEDRIVER_NET_TIMEOUT_H_
7 
8 #include "base/time/time.h"
9 
10 // A helper for tracking the time spent executing a task.  Creating a new
11 // instance marks the beginning of the task.  Task duration limit may be set
12 // in the contructor or at any time later, but only once.
13 class Timeout {
14  public:
15   Timeout();
16   explicit Timeout(const base::TimeDelta& duration);
17 
18   // To be used when executing a sub-task with its own timeout, keeping track
19   // of the parent task timeout at the same time. Creates a new Timeout whose
20   // deadline is either Now() + |duration| or deadline of |outer|, whichever is
21   // smaller. |outer| may be nullptr. Note: setting duration on |outer| won't
22   // affect sub-timeouts that were created earlier!
23   Timeout(const base::TimeDelta& duration, const Timeout* outer);
24 
25   // Sets the deadline by adding |duration| to the start time recored at
26   // contruction.  Should not be called if the deadline is already set, unless
27   // the duration is exactly the same.
28   void SetDuration(const base::TimeDelta& duration);
29 
is_set()30   bool is_set() const { return !deadline_.is_null(); }
31 
32   // Whether the remaining time delta is less than or equal to zero.
33   bool IsExpired() const;
34 
35   // Returns the duration if set, otherwise returns TimeDelta::Max().
36   base::TimeDelta GetDuration() const;
37 
38   // Returns the remaining time if duration is set, otherwise returns
39   // TimeDelta::Max().
40   base::TimeDelta GetRemainingTime() const;
41 
42  private:
43   base::TimeTicks start_;
44   base::TimeTicks deadline_;
45 };
46 
47 #endif  // CHROME_TEST_CHROMEDRIVER_NET_TIMEOUT_H_
48