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/desktop_engagement_service.h " | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/metrics/histogram_macros.h" | |
| 9 #include "base/strings/string_number_conversions.h" | |
| 10 #include "components/variations/variations_associated_data.h" | |
| 11 | |
| 12 namespace metrics { | |
| 13 | |
| 14 namespace { | |
| 15 | |
| 16 DesktopEngagementService* g_instance = nullptr; | |
| 17 | |
| 18 } // namespace | |
| 19 | |
| 20 // static | |
| 21 void DesktopEngagementService::Initialize() { | |
| 22 g_instance = new DesktopEngagementService; | |
| 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_ && !is_first_session_) { | |
| 45 OnUserEvent(); | |
| 46 } else if (in_session_ && !is_audio_playing_) { | |
| 47 DVLOG(4) << "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 DVLOG(4) << "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 DVLOG(4) << "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 DVLOG(4) << "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 visibility_observer_(), | |
|
Alexei Svitkine (slow)
2016/07/29 19:58:08
Nit: You can omit this, the no argument constructo
gayane -on leave until 09-2017
2016/08/01 16:39:18
Done.
| |
| 88 audio_tracker_(this), | |
| 89 weak_factory_(this) { | |
| 90 InitInactivityTimeout(); | |
| 91 } | |
| 92 | |
| 93 DesktopEngagementService::~DesktopEngagementService() {} | |
| 94 | |
| 95 void DesktopEngagementService::OnTimerFired() { | |
| 96 base::TimeDelta remaining = | |
| 97 inactivity_timeout_ - (base::TimeTicks::Now() - last_user_event_); | |
| 98 if (remaining.ToInternalValue() > 0) { | |
| 99 StartTimer(remaining); | |
| 100 return; | |
| 101 } | |
| 102 | |
| 103 // No user events happened in the last 5 min. Terminate the session now. | |
| 104 if (!is_audio_playing_) { | |
| 105 DVLOG(4) << "Ending session after delay"; | |
| 106 EndSession(); | |
| 107 } | |
| 108 } | |
| 109 | |
| 110 void DesktopEngagementService::StartSession() { | |
| 111 in_session_ = true; | |
| 112 is_first_session_ = false; | |
| 113 session_start_ = base::TimeTicks::Now(); | |
| 114 StartTimer(inactivity_timeout_); | |
| 115 } | |
| 116 | |
| 117 void DesktopEngagementService::EndSession() { | |
| 118 in_session_ = false; | |
| 119 | |
| 120 base::TimeDelta delta = base::TimeTicks::Now() - session_start_; | |
| 121 DVLOG(4) << "Logging session length of " << delta.InSeconds() << " seconds."; | |
| 122 | |
| 123 // Note: This metric is recorded separately for Android in | |
| 124 // UmaSessionStats::UmaEndSession. | |
| 125 UMA_HISTOGRAM_LONG_TIMES("Session.TotalDuration", delta); | |
| 126 } | |
| 127 | |
| 128 void DesktopEngagementService::InitInactivityTimeout() { | |
| 129 const int kDefaultInactivityTimeoutMinutes = 5; | |
| 130 | |
| 131 int timeout_minutes = kDefaultInactivityTimeoutMinutes; | |
| 132 std::string param_value = variations::GetVariationParamValue( | |
| 133 "DesktopEngagement", "inactivity_timeout"); | |
| 134 if (!param_value.empty()) | |
| 135 base::StringToInt(param_value, &timeout_minutes); | |
| 136 | |
| 137 inactivity_timeout_ = base::TimeDelta::FromMinutes(timeout_minutes); | |
| 138 } | |
| 139 | |
| 140 } // namespace metrics | |
| OLD | NEW |