OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/metrics/desktop_engagement/desktop_engagement_service.h
" | 5 #include "chrome/browser/metrics/desktop_session/desktop_session_service.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/metrics/histogram_macros.h" | 8 #include "base/metrics/histogram_macros.h" |
9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
10 #include "components/variations/variations_associated_data.h" | 10 #include "components/variations/variations_associated_data.h" |
11 | 11 |
12 namespace metrics { | 12 namespace metrics { |
13 | 13 |
14 namespace { | 14 namespace { |
15 | 15 |
16 DesktopEngagementService* g_instance = nullptr; | 16 DesktopSessionService* g_instance = nullptr; |
17 | 17 |
18 } // namespace | 18 } // namespace |
19 | 19 |
20 // static | 20 // static |
21 void DesktopEngagementService::Initialize() { | 21 void DesktopSessionService::Initialize() { |
22 g_instance = new DesktopEngagementService; | 22 g_instance = new DesktopSessionService; |
23 } | 23 } |
24 | 24 |
25 // static | 25 // static |
26 bool DesktopEngagementService::IsInitialized() { | 26 bool DesktopSessionService::IsInitialized() { |
27 return g_instance != nullptr; | 27 return g_instance != nullptr; |
28 } | 28 } |
29 | 29 |
30 // static | 30 // static |
31 DesktopEngagementService* DesktopEngagementService::Get() { | 31 DesktopSessionService* DesktopSessionService::Get() { |
32 DCHECK(g_instance); | 32 DCHECK(g_instance); |
33 return g_instance; | 33 return g_instance; |
34 } | 34 } |
35 | 35 |
36 void DesktopEngagementService::StartTimer(base::TimeDelta duration) { | 36 void DesktopSessionService::StartTimer(base::TimeDelta duration) { |
37 timer_.Start(FROM_HERE, duration, | 37 timer_.Start(FROM_HERE, duration, |
38 base::Bind(&DesktopEngagementService::OnTimerFired, | 38 base::Bind(&DesktopSessionService::OnTimerFired, |
39 weak_factory_.GetWeakPtr())); | 39 weak_factory_.GetWeakPtr())); |
40 } | 40 } |
41 | 41 |
42 void DesktopEngagementService::OnVisibilityChanged(bool visible) { | 42 void DesktopSessionService::OnVisibilityChanged(bool visible) { |
43 is_visible_ = visible; | 43 is_visible_ = visible; |
44 if (is_visible_ && !is_first_session_) { | 44 if (is_visible_ && !is_first_session_) { |
45 OnUserEvent(); | 45 OnUserEvent(); |
46 } else if (in_session_ && !is_audio_playing_) { | 46 } else if (in_session_ && !is_audio_playing_) { |
47 DVLOG(4) << "Ending session due to visibility change"; | 47 DVLOG(4) << "Ending session due to visibility change"; |
48 EndSession(); | 48 EndSession(); |
49 } | 49 } |
50 } | 50 } |
51 | 51 |
52 void DesktopEngagementService::OnUserEvent() { | 52 void DesktopSessionService::OnUserEvent() { |
53 if (!is_visible_) | 53 if (!is_visible_) |
54 return; | 54 return; |
55 | 55 |
56 last_user_event_ = base::TimeTicks::Now(); | 56 last_user_event_ = base::TimeTicks::Now(); |
57 // This may start session. | 57 // This may start session. |
58 if (!in_session_) { | 58 if (!in_session_) { |
59 DVLOG(4) << "Starting session due to user event"; | 59 DVLOG(4) << "Starting session due to user event"; |
60 StartSession(); | 60 StartSession(); |
61 } | 61 } |
62 } | 62 } |
63 | 63 |
64 void DesktopEngagementService::OnAudioStart() { | 64 void DesktopSessionService::OnAudioStart() { |
65 // This may start session. | 65 // This may start session. |
66 is_audio_playing_ = true; | 66 is_audio_playing_ = true; |
67 if (!in_session_) { | 67 if (!in_session_) { |
68 DVLOG(4) << "Starting session due to audio start"; | 68 DVLOG(4) << "Starting session due to audio start"; |
69 StartSession(); | 69 StartSession(); |
70 } | 70 } |
71 } | 71 } |
72 | 72 |
73 void DesktopEngagementService::OnAudioEnd() { | 73 void DesktopSessionService::OnAudioEnd() { |
74 is_audio_playing_ = false; | 74 is_audio_playing_ = false; |
75 | 75 |
76 // If the timer is not running, this means that no user events happened in the | 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. | 77 // last 5 minutes so the session can be terminated. |
78 if (!timer_.IsRunning()) { | 78 if (!timer_.IsRunning()) { |
79 DVLOG(4) << "Ending session due to audio ending"; | 79 DVLOG(4) << "Ending session due to audio ending"; |
80 EndSession(); | 80 EndSession(); |
81 } | 81 } |
82 } | 82 } |
83 | 83 |
84 DesktopEngagementService::DesktopEngagementService() | 84 DesktopSessionService::DesktopSessionService() |
85 : session_start_(base::TimeTicks::Now()), | 85 : session_start_(base::TimeTicks::Now()), |
86 last_user_event_(session_start_), | 86 last_user_event_(session_start_), |
87 audio_tracker_(this), | 87 audio_tracker_(this), |
88 weak_factory_(this) { | 88 weak_factory_(this) { |
89 InitInactivityTimeout(); | 89 InitInactivityTimeout(); |
90 } | 90 } |
91 | 91 |
92 DesktopEngagementService::~DesktopEngagementService() {} | 92 DesktopSessionService::~DesktopSessionService() {} |
93 | 93 |
94 void DesktopEngagementService::OnTimerFired() { | 94 void DesktopSessionService::OnTimerFired() { |
95 base::TimeDelta remaining = | 95 base::TimeDelta remaining = |
96 inactivity_timeout_ - (base::TimeTicks::Now() - last_user_event_); | 96 inactivity_timeout_ - (base::TimeTicks::Now() - last_user_event_); |
97 if (remaining.ToInternalValue() > 0) { | 97 if (remaining.ToInternalValue() > 0) { |
98 StartTimer(remaining); | 98 StartTimer(remaining); |
99 return; | 99 return; |
100 } | 100 } |
101 | 101 |
102 // No user events happened in the last 5 min. Terminate the session now. | 102 // No user events happened in the last 5 min. Terminate the session now. |
103 if (!is_audio_playing_) { | 103 if (!is_audio_playing_) { |
104 DVLOG(4) << "Ending session after delay"; | 104 DVLOG(4) << "Ending session after delay"; |
105 EndSession(); | 105 EndSession(); |
106 } | 106 } |
107 } | 107 } |
108 | 108 |
109 void DesktopEngagementService::StartSession() { | 109 void DesktopSessionService::StartSession() { |
110 in_session_ = true; | 110 in_session_ = true; |
111 is_first_session_ = false; | 111 is_first_session_ = false; |
112 session_start_ = base::TimeTicks::Now(); | 112 session_start_ = base::TimeTicks::Now(); |
113 StartTimer(inactivity_timeout_); | 113 StartTimer(inactivity_timeout_); |
114 } | 114 } |
115 | 115 |
116 void DesktopEngagementService::EndSession() { | 116 void DesktopSessionService::EndSession() { |
117 in_session_ = false; | 117 in_session_ = false; |
118 | 118 |
119 base::TimeDelta delta = base::TimeTicks::Now() - session_start_; | 119 base::TimeDelta delta = base::TimeTicks::Now() - session_start_; |
120 DVLOG(4) << "Logging session length of " << delta.InSeconds() << " seconds."; | 120 DVLOG(4) << "Logging session length of " << delta.InSeconds() << " seconds."; |
121 | 121 |
122 // Note: This metric is recorded separately for Android in | 122 // Note: This metric is recorded separately for Android in |
123 // UmaSessionStats::UmaEndSession. | 123 // UmaSessionStats::UmaEndSession. |
124 UMA_HISTOGRAM_LONG_TIMES("Session.TotalDuration", delta); | 124 UMA_HISTOGRAM_LONG_TIMES("Session.TotalDuration", delta); |
125 } | 125 } |
126 | 126 |
127 void DesktopEngagementService::InitInactivityTimeout() { | 127 void DesktopSessionService::InitInactivityTimeout() { |
128 const int kDefaultInactivityTimeoutMinutes = 5; | 128 const int kDefaultInactivityTimeoutMinutes = 5; |
129 | 129 |
130 int timeout_minutes = kDefaultInactivityTimeoutMinutes; | 130 int timeout_minutes = kDefaultInactivityTimeoutMinutes; |
131 std::string param_value = variations::GetVariationParamValue( | 131 std::string param_value = variations::GetVariationParamValue( |
132 "DesktopEngagement", "inactivity_timeout"); | 132 "DesktopSession", "inactivity_timeout"); |
133 if (!param_value.empty()) | 133 if (!param_value.empty()) |
134 base::StringToInt(param_value, &timeout_minutes); | 134 base::StringToInt(param_value, &timeout_minutes); |
135 | 135 |
136 inactivity_timeout_ = base::TimeDelta::FromMinutes(timeout_minutes); | 136 inactivity_timeout_ = base::TimeDelta::FromMinutes(timeout_minutes); |
137 } | 137 } |
138 | 138 |
139 } // namespace metrics | 139 } // namespace metrics |
OLD | NEW |