1 // Copyright 2018 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_RESOURCE_COORDINATOR_USAGE_CLOCK_H_
6 #define CHROME_BROWSER_RESOURCE_COORDINATOR_USAGE_CLOCK_H_
7 
8 #include "base/macros.h"
9 #include "base/time/time.h"
10 #include "chrome/browser/metrics/desktop_session_duration/desktop_session_duration_tracker.h"
11 
12 namespace resource_coordinator {
13 
14 // A clock that advances when Chrome is in use.
15 //
16 // See metrics::DesktopSessionDurationTracker for how Chrome usage is tracked.
17 // If metrics::DesktopSessionDurationTracker isn't initialized before this, the
18 // clock will advance continuously, regardless of Chrome usage. This avoids
19 // forcing all tests that indirectly depend on this to initialize
20 // metrics::DesktopSessionDurationTracker.
21 class UsageClock
22     : public metrics::DesktopSessionDurationTracker::Observer
23 {
24  public:
25   UsageClock();
26   ~UsageClock() override;
27 
28   // Returns the amount of Chrome usage time since this was instantiated.
29   base::TimeDelta GetTotalUsageTime() const;
30 
31   // Returns true if Chrome is currently considered to be in use.
32   bool IsInUse() const;
33 
34  private:
35   void OnSessionStarted(base::TimeTicks session_start) override;
36   void OnSessionEnded(base::TimeDelta session_length,
37                       base::TimeTicks session_end) override;
38 
39   // The total time elapsed in completed usage sessions. The duration of the
40   // current usage session, if any, must be added to this to get the total usage
41   // time of Chrome.
42   base::TimeDelta usage_time_in_completed_sessions_;
43 
44   // The time at which the current session started, or a null TimeTicks if not
45   // currently in a session.
46   base::TimeTicks current_usage_session_start_time_;
47 
48   DISALLOW_COPY_AND_ASSIGN(UsageClock);
49 };
50 
51 }  // namespace resource_coordinator
52 
53 #endif  // CHROME_BROWSER_RESOURCE_COORDINATOR_USAGE_CLOCK_H_
54