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