Chromium Code Reviews| Index: chrome/browser/metrics/desktop_engagement_service.cc |
| diff --git a/chrome/browser/metrics/desktop_engagement_service.cc b/chrome/browser/metrics/desktop_engagement_service.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f3f56a0b0067f2a368d0f570d837bd5a7cfc9d29 |
| --- /dev/null |
| +++ b/chrome/browser/metrics/desktop_engagement_service.cc |
| @@ -0,0 +1,83 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/metrics/desktop_engagement_service.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/metrics/histogram_macros.h" |
| + |
| +namespace { |
| + |
| +DesktopEngagementService* g_instance = nullptr; |
| + |
| +} // namespace |
| + |
| +// static |
| +void DesktopEngagementService::Initialize() { |
| + g_instance = new DesktopEngagementService; |
| +} |
| + |
| +void DesktopEngagementService::StartTimer(base::TimeDelta duration) { |
| + timer_.Start(FROM_HERE, duration, |
| + base::Bind(&DesktopEngagementService::OnTimerFired, |
| + base::Unretained(this))); |
|
Alexei Svitkine (slow)
2016/06/29 18:27:22
Use a WeakPointerFactory member and pass a weak po
|
| +} |
| + |
| +void DesktopEngagementService::OnUserEvent() { |
| + last_user_event_ = base::TimeTicks::Now(); |
| + // This may start session. |
| + if (!in_session) |
| + StartSession(); |
| +} |
| + |
| +void DesktopEngagementService::OnAudioStart() { |
| + // This may start session. |
| + is_audio_playing_ = true; |
| + if (!in_session) |
| + StartSession(); |
| +} |
| + |
| +void DesktopEngagementService::OnAudioEnd() { |
| + is_audio_playing_ = false; |
| + |
| + // If the timer is not running, this means that no user events happened in the |
| + // last 5 minutes so the session can be terminated. |
| + if (!timer_.IsRunning()) |
| + EndSession(); |
| +} |
| + |
| +DesktopEngagementService::DesktopEngagementService() |
| + : session_start_(base::TimeTicks::Now()), last_user_event_(session_start_) { |
| + StartSession(); |
| + // TODO: Add user events observers here, or add a static Get() method to |
| + // DesktopEngagementService. |
| +} |
| + |
| +void DesktopEngagementService::OnTimerFired() { |
| + base::TimeDelta remaining_timer = base::TimeTicks::Now() - last_user_event_; |
| + if (remaining_timer < base::TimeDelta::FromMinutes(5)) { |
| + StartTimer(remaining_timer); |
| + return; |
| + } |
| + |
| + // No user events happened in the last 5 min. Terminate the session now. |
| + if (!is_audio_playing_) |
| + EndSession(); |
| +} |
| + |
| +void DesktopEngagementService::StartSession() { |
| + in_session = true; |
| + session_start_ = base::TimeTicks::Now(); |
| + StartTimer(base::TimeDelta::FromMinutes(5)); |
| +} |
| + |
| +void DesktopEngagementService::EndSession() { |
| + in_session = false; |
| + base::TimeDelta delta = base::TimeTicks::Now() - session_start_; |
| + |
| + constexpr unsigned kNumTimeSlices = 60 / 5 * 24; |
| + UMA_HISTOGRAM_CUSTOM_TIMES("Session.TotalDuration.Desktop", delta, |
|
Alexei Svitkine (slow)
2016/06/29 18:27:22
Add to histograms.xml please. :)
Or just use Sess
|
| + base::TimeDelta::FromMilliseconds(1), |
| + base::TimeDelta::FromHours(24), kNumTimeSlices); |
| +} |