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