| 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 { |
| 15 |
| 16 DesktopEngagementService* g_instance = nullptr; |
| 17 |
| 18 // Small adapter for observing AudibleContentsTracker notifications and |
| 19 // dispatching them to the DesktopEngagementService. |
| 20 class AudibleContentsObserver |
| 21 : public metrics::AudibleContentsTracker::Observer { |
| 22 public: |
| 23 AudibleContentsObserver() {} |
| 24 |
| 25 // A one way initialization function. This causes a tracker/observer pair to |
| 26 // be created, which live for the lifetime of the browser. They are |
| 27 // deliberately leaked at shutdown. |
| 28 static void Initialize() { |
| 29 DCHECK(!tracker_); |
| 30 DCHECK(!observer_); |
| 31 observer_ = new AudibleContentsObserver(); |
| 32 tracker_ = new metrics::AudibleContentsTracker(observer_); |
| 33 } |
| 34 |
| 35 // AudibleContentsTracker::Observer: |
| 36 void OnAudioStart() override { g_instance->OnAudioStart(); } |
| 37 void OnAudioEnd() override { g_instance->OnAudioEnd(); } |
| 38 |
| 39 private: |
| 40 // Singletons for performing the tracking and observing state changes. These |
| 41 // are deliberately leaked at shutdown. |
| 42 static metrics::AudibleContentsTracker* tracker_; |
| 43 static AudibleContentsObserver* observer_; |
| 44 |
| 45 DISALLOW_COPY_AND_ASSIGN(AudibleContentsObserver); |
| 46 }; |
| 47 |
| 48 metrics::AudibleContentsTracker* AudibleContentsObserver::tracker_ = nullptr; |
| 49 AudibleContentsObserver* AudibleContentsObserver::observer_ = nullptr; |
| 50 } // namespace |
| 51 |
| 52 // static |
| 53 void DesktopEngagementService::Initialize() { |
| 54 g_instance = new DesktopEngagementService; |
| 55 AudibleContentsObserver::Initialize(); |
| 56 metrics::ChromeVisibilityObserver::Initialize(); |
| 57 } |
| 58 |
| 59 // static |
| 60 bool DesktopEngagementService::IsInitialized() { |
| 61 return g_instance != nullptr; |
| 62 } |
| 63 |
| 64 // static |
| 65 DesktopEngagementService* DesktopEngagementService::Get() { |
| 66 DCHECK(g_instance); |
| 67 return g_instance; |
| 68 } |
| 69 |
| 70 void DesktopEngagementService::StartTimer(base::TimeDelta duration) { |
| 71 timer_.Start(FROM_HERE, duration, |
| 72 base::Bind(&DesktopEngagementService::OnTimerFired, |
| 73 weak_factory_.GetWeakPtr())); |
| 74 } |
| 75 |
| 76 void DesktopEngagementService::OnVisibilityChanged(bool visible) { |
| 77 is_visible_ = visible; |
| 78 if (is_visible_ && !is_first_session_) { |
| 79 OnUserEvent(); |
| 80 } else if (in_session_ && !is_audio_playing_) { |
| 81 DLOG(ERROR) << "Ending session due to visibility change"; |
| 82 EndSession(); |
| 83 } |
| 84 } |
| 85 |
| 86 void DesktopEngagementService::OnUserEvent() { |
| 87 if (!is_visible_) |
| 88 return; |
| 89 |
| 90 last_user_event_ = base::TimeTicks::Now(); |
| 91 // This may start session. |
| 92 if (!in_session_) { |
| 93 DLOG(ERROR) << "Starting session due to user event"; |
| 94 StartSession(); |
| 95 } |
| 96 } |
| 97 |
| 98 void DesktopEngagementService::OnAudioStart() { |
| 99 // This may start session. |
| 100 is_audio_playing_ = true; |
| 101 if (!in_session_) { |
| 102 DLOG(ERROR) << "Starting session due to audio start"; |
| 103 StartSession(); |
| 104 } |
| 105 } |
| 106 |
| 107 void DesktopEngagementService::OnAudioEnd() { |
| 108 is_audio_playing_ = false; |
| 109 |
| 110 // If the timer is not running, this means that no user events happened in the |
| 111 // last 5 minutes so the session can be terminated. |
| 112 if (!timer_.IsRunning()) { |
| 113 DLOG(ERROR) << "Ending session due to audio ending"; |
| 114 EndSession(); |
| 115 } |
| 116 } |
| 117 |
| 118 DesktopEngagementService::DesktopEngagementService() |
| 119 : session_start_(base::TimeTicks::Now()), |
| 120 last_user_event_(session_start_), |
| 121 weak_factory_(this) { |
| 122 InitInactivityTimeout(); |
| 123 } |
| 124 |
| 125 DesktopEngagementService::~DesktopEngagementService() {} |
| 126 |
| 127 void DesktopEngagementService::OnTimerFired() { |
| 128 base::TimeDelta remaining = |
| 129 inactivity_timeout_ - (base::TimeTicks::Now() - last_user_event_); |
| 130 if (remaining.ToInternalValue() > 0) { |
| 131 StartTimer(remaining); |
| 132 return; |
| 133 } |
| 134 |
| 135 // No user events happened in the last 5 min. Terminate the session now. |
| 136 if (!is_audio_playing_) { |
| 137 DLOG(ERROR) << "Ending session after delay"; |
| 138 EndSession(); |
| 139 } |
| 140 } |
| 141 |
| 142 void DesktopEngagementService::StartSession() { |
| 143 in_session_ = true; |
| 144 is_first_session_ = false; |
| 145 session_start_ = base::TimeTicks::Now(); |
| 146 StartTimer(inactivity_timeout_); |
| 147 } |
| 148 |
| 149 void DesktopEngagementService::EndSession() { |
| 150 in_session_ = false; |
| 151 base::TimeDelta delta = base::TimeTicks::Now() - session_start_; |
| 152 |
| 153 constexpr unsigned kNumTimeSlices = 60 / 5 * 24; |
| 154 DLOG(ERROR) << "Logging session length of " << delta.InSeconds() |
| 155 << " seconds."; |
| 156 UMA_HISTOGRAM_CUSTOM_TIMES("Session.TotalDuration", delta, |
| 157 base::TimeDelta::FromMilliseconds(1), |
| 158 base::TimeDelta::FromHours(24), kNumTimeSlices); |
| 159 } |
| 160 |
| 161 void DesktopEngagementService::InitInactivityTimeout() { |
| 162 const int kDefaultInactivityTimeoutMinutes = 5; |
| 163 |
| 164 int timeout_minutes = kDefaultInactivityTimeoutMinutes; |
| 165 std::string param_value = variations::GetVariationParamValue( |
| 166 "DesktopEngagement", "inactivity_timeout"); |
| 167 if (!param_value.empty()) |
| 168 base::StringToInt(param_value, &timeout_minutes); |
| 169 |
| 170 inactivity_timeout_ = base::TimeDelta::FromMinutes(timeout_minutes); |
| 171 } |
| OLD | NEW |