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