OLD | NEW |
| (Empty) |
1 // Copyright 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_BROWSER_METRICS_DESKTOP_ENGAGEMENT_DESKTOP_ENGAGEMENT_SERVICE_H_ | |
6 #define CHROME_BROWSER_METRICS_DESKTOP_ENGAGEMENT_DESKTOP_ENGAGEMENT_SERVICE_H_ | |
7 | |
8 #include "base/macros.h" | |
9 #include "base/memory/weak_ptr.h" | |
10 #include "base/time/time.h" | |
11 #include "base/timer/timer.h" | |
12 #include "chrome/browser/metrics/desktop_engagement/audible_contents_tracker.h" | |
13 #include "chrome/browser/metrics/desktop_engagement/chrome_visibility_observer.h
" | |
14 | |
15 namespace metrics { | |
16 | |
17 // Class for tracking and recording user engagement based on browser visibility, | |
18 // audio and user interaction. | |
19 class DesktopEngagementService : public AudibleContentsTracker::Observer { | |
20 public: | |
21 // Creates the |DesktopEngagementService| instance and initializes the | |
22 // observes that notify to it. | |
23 static void Initialize(); | |
24 | |
25 // Returns true if the |DesktopEngagementService| instance has been created. | |
26 static bool IsInitialized(); | |
27 | |
28 // Returns the |DesktopEngagementService| instance. | |
29 static DesktopEngagementService* Get(); | |
30 | |
31 // Called when user interaction with the browser is caught. | |
32 void OnUserEvent(); | |
33 | |
34 // Called when visibility of the browser changes. | |
35 void OnVisibilityChanged(bool visible); | |
36 | |
37 bool is_visible() const { return is_visible_; } | |
38 bool in_session() const { return in_session_; } | |
39 bool is_audio_playing() const { return is_audio_playing_; } | |
40 | |
41 void SetInactivityTimeoutForTesting(int seconds) { | |
42 inactivity_timeout_ = base::TimeDelta::FromSeconds(seconds); | |
43 } | |
44 | |
45 protected: | |
46 DesktopEngagementService(); | |
47 ~DesktopEngagementService() override; | |
48 | |
49 // AudibleContentsTracker::Observer | |
50 void OnAudioStart() override; | |
51 void OnAudioEnd() override; | |
52 | |
53 // Decides whether session should be ended. Called when timer for inactivity | |
54 // timeout was fired. Overridden by tests. | |
55 virtual void OnTimerFired(); | |
56 | |
57 private: | |
58 // Starts timer based on |inactivity_timeout_|. | |
59 void StartTimer(base::TimeDelta duration); | |
60 | |
61 // Marks the start of the session. | |
62 void StartSession(); | |
63 | |
64 // Ends the session and saves session information into histograms. | |
65 void EndSession(); | |
66 | |
67 // Sets |inactivity_timeout_| based on variation params. | |
68 void InitInactivityTimeout(); | |
69 | |
70 // Used for marking start if the session. | |
71 base::TimeTicks session_start_; | |
72 | |
73 // Used for marking last user interaction. | |
74 base::TimeTicks last_user_event_; | |
75 | |
76 // Used for marking current state of the user engagement. | |
77 bool is_visible_ = false; | |
78 bool in_session_ = false; | |
79 bool is_audio_playing_ = false; | |
80 bool is_first_session_ = true; | |
81 | |
82 // Timeout for waiting for user interaction. | |
83 base::TimeDelta inactivity_timeout_; | |
84 | |
85 base::OneShotTimer timer_; | |
86 | |
87 ChromeVisibilityObserver visibility_observer_; | |
88 AudibleContentsTracker audio_tracker_; | |
89 | |
90 base::WeakPtrFactory<DesktopEngagementService> weak_factory_; | |
91 | |
92 DISALLOW_COPY_AND_ASSIGN(DesktopEngagementService); | |
93 }; | |
94 | |
95 } // namespace metrics | |
96 | |
97 #endif // CHROME_BROWSER_METRICS_DESKTOP_ENGAGEMENT_DESKTOP_ENGAGEMENT_SERVICE_
H_ | |
OLD | NEW |