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

Side by Side 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, 5 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 unified diff | 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 »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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
10 namespace {
11
12 DesktopEngagementService* g_instance = nullptr;
13
14 } // namespace
15
16 // static
17 void DesktopEngagementService::Initialize() {
18 g_instance = new DesktopEngagementService;
19 }
20
21 void DesktopEngagementService::StartTimer(base::TimeDelta duration) {
22 timer_.Start(FROM_HERE, duration,
23 base::Bind(&DesktopEngagementService::OnTimerFired,
24 base::Unretained(this)));
Alexei Svitkine (slow) 2016/06/29 18:27:22 Use a WeakPointerFactory member and pass a weak po
25 }
26
27 void DesktopEngagementService::OnUserEvent() {
28 last_user_event_ = base::TimeTicks::Now();
29 // This may start session.
30 if (!in_session)
31 StartSession();
32 }
33
34 void DesktopEngagementService::OnAudioStart() {
35 // This may start session.
36 is_audio_playing_ = true;
37 if (!in_session)
38 StartSession();
39 }
40
41 void DesktopEngagementService::OnAudioEnd() {
42 is_audio_playing_ = false;
43
44 // If the timer is not running, this means that no user events happened in the
45 // last 5 minutes so the session can be terminated.
46 if (!timer_.IsRunning())
47 EndSession();
48 }
49
50 DesktopEngagementService::DesktopEngagementService()
51 : session_start_(base::TimeTicks::Now()), last_user_event_(session_start_) {
52 StartSession();
53 // TODO: Add user events observers here, or add a static Get() method to
54 // DesktopEngagementService.
55 }
56
57 void DesktopEngagementService::OnTimerFired() {
58 base::TimeDelta remaining_timer = base::TimeTicks::Now() - last_user_event_;
59 if (remaining_timer < base::TimeDelta::FromMinutes(5)) {
60 StartTimer(remaining_timer);
61 return;
62 }
63
64 // No user events happened in the last 5 min. Terminate the session now.
65 if (!is_audio_playing_)
66 EndSession();
67 }
68
69 void DesktopEngagementService::StartSession() {
70 in_session = true;
71 session_start_ = base::TimeTicks::Now();
72 StartTimer(base::TimeDelta::FromMinutes(5));
73 }
74
75 void DesktopEngagementService::EndSession() {
76 in_session = false;
77 base::TimeDelta delta = base::TimeTicks::Now() - session_start_;
78
79 constexpr unsigned kNumTimeSlices = 60 / 5 * 24;
80 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
81 base::TimeDelta::FromMilliseconds(1),
82 base::TimeDelta::FromHours(24), kNumTimeSlices);
83 }
OLDNEW
« 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