Chromium Code Reviews| 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 | |
| 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 } | |
| OLD | NEW |