| 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 #include "chrome/browser/metrics/desktop_engagement_service.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/metrics/histogram_macros.h" |
| 9 #include "chrome/browser/metrics/chrome_visibility_observer.h" |
| 10 |
| 11 |
| 12 namespace { |
| 13 |
| 14 DesktopEngagementService* g_instance = nullptr; |
| 15 |
| 16 const int kActivityIntervalMinutes = 1; |
| 17 |
| 18 } // namespace |
| 19 |
| 20 // static |
| 21 void DesktopEngagementService::Initialize() { |
| 22 g_instance = new DesktopEngagementService; |
| 23 metrics::ChromeVisibilityObserver::Initialize(); |
| 24 } |
| 25 |
| 26 // static |
| 27 DesktopEngagementService* DesktopEngagementService::Get() { |
| 28 DCHECK(g_instance); |
| 29 return g_instance; |
| 30 } |
| 31 |
| 32 void DesktopEngagementService::StartTimer(base::TimeDelta duration) { |
| 33 timer_.Start(FROM_HERE, duration, |
| 34 base::Bind(&DesktopEngagementService::OnTimerFired, |
| 35 weak_factory_.GetWeakPtr())); |
| 36 } |
| 37 |
| 38 void DesktopEngagementService::OnVisibilityChanged(bool visible) { |
| 39 is_visible_ = visible; |
| 40 if (is_visible_) { |
| 41 OnUserEvent(); |
| 42 } else if (in_session_) { |
| 43 DLOG(ERROR) << "Ending session due to visibility change"; |
| 44 EndSession(); |
| 45 } |
| 46 } |
| 47 |
| 48 void DesktopEngagementService::OnUserEvent() { |
| 49 if (!is_visible_) |
| 50 return; |
| 51 |
| 52 last_user_event_ = base::TimeTicks::Now(); |
| 53 // This may start session. |
| 54 if (!in_session_) { |
| 55 DLOG(ERROR) << "Starting session due to user event"; |
| 56 StartSession(); |
| 57 } |
| 58 } |
| 59 |
| 60 void DesktopEngagementService::OnAudioStart() { |
| 61 // This may start session. |
| 62 is_audio_playing_ = true; |
| 63 if (!in_session_) { |
| 64 DLOG(ERROR) << "Starting session due to audio start"; |
| 65 StartSession(); |
| 66 } |
| 67 } |
| 68 |
| 69 void DesktopEngagementService::OnAudioEnd() { |
| 70 is_audio_playing_ = false; |
| 71 |
| 72 // If the timer is not running, this means that no user events happened in the |
| 73 // last 5 minutes so the session can be terminated. |
| 74 if (!timer_.IsRunning()) { |
| 75 DLOG(ERROR) << "Ending session due to audio ending"; |
| 76 EndSession(); |
| 77 } |
| 78 } |
| 79 |
| 80 DesktopEngagementService::DesktopEngagementService() |
| 81 : session_start_(base::TimeTicks::Now()), last_user_event_(session_start_), |
| 82 weak_factory_(this) { |
| 83 StartSession(); |
| 84 // TODO: Add user events observers here, or add a static Get() method to |
| 85 // DesktopEngagementService. |
| 86 } |
| 87 |
| 88 DesktopEngagementService::~DesktopEngagementService() { |
| 89 } |
| 90 |
| 91 void DesktopEngagementService::OnTimerFired() { |
| 92 base::TimeDelta remaining = base::TimeTicks::Now() - last_user_event_; |
| 93 if (remaining < base::TimeDelta::FromMinutes(kActivityIntervalMinutes)) { |
| 94 StartTimer(remaining); |
| 95 return; |
| 96 } |
| 97 |
| 98 // No user events happened in the last 5 min. Terminate the session now. |
| 99 if (!is_audio_playing_) { |
| 100 DLOG(ERROR) << "Ending session after delay"; |
| 101 EndSession(); |
| 102 } |
| 103 } |
| 104 |
| 105 void DesktopEngagementService::StartSession() { |
| 106 in_session_ = true; |
| 107 session_start_ = base::TimeTicks::Now(); |
| 108 StartTimer(base::TimeDelta::FromMinutes(kActivityIntervalMinutes)); |
| 109 } |
| 110 |
| 111 void DesktopEngagementService::EndSession() { |
| 112 in_session_ = false; |
| 113 base::TimeDelta delta = base::TimeTicks::Now() - session_start_; |
| 114 |
| 115 constexpr unsigned kNumTimeSlices = 60 / 5 * 24; |
| 116 DLOG(ERROR) << "Logging session length of " << delta.InSeconds() |
| 117 << " seconds."; |
| 118 UMA_HISTOGRAM_CUSTOM_TIMES("Session.TotalDuration", delta, |
| 119 base::TimeDelta::FromMilliseconds(1), |
| 120 base::TimeDelta::FromHours(24), kNumTimeSlices); |
| 121 } |
| OLD | NEW |