Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(658)

Unified Diff: chrome/browser/metrics/desktop_engagement_service.cc

Issue 2102263002: Add Desktop Engagement Service (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: V2 Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chrome/browser/metrics/desktop_engagement_service.h ('k') | chrome/chrome_browser.gypi » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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);
+}
« no previous file with comments | « chrome/browser/metrics/desktop_engagement_service.h ('k') | chrome/chrome_browser.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698