guest_os_engagement_metrics.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Copyright 2019 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. #ifndef COMPONENTS_GUEST_OS_GUEST_OS_ENGAGEMENT_METRICS_H_
  5. #define COMPONENTS_GUEST_OS_GUEST_OS_ENGAGEMENT_METRICS_H_
  6. #include <memory>
  7. #include "base/time/time.h"
  8. #include "base/timer/timer.h"
  9. #include "chromeos/dbus/power/power_manager_client.h"
  10. #include "components/session_manager/core/session_manager_observer.h"
  11. #include "ui/wm/public/activation_change_observer.h"
  12. class PrefService;
  13. namespace aura {
  14. class Window;
  15. } // namespace aura
  16. namespace base {
  17. class Clock;
  18. class TickClock;
  19. } // namespace base
  20. namespace guest_os {
  21. // A class for recording engagement metrics. Calculates and reports daily
  22. // totals for the following metrics:
  23. // - Foo.EngagementTime.Total: Engaged session time, i.e. excluding when the
  24. // screen is locked or dim due to user idle. To allow comparisons with the
  25. // other metrics, this class should only be instantiated when the relevant
  26. // Guest OS is supported.
  27. // - Foo.EngagementTime.Foreground: Time when the user is engaged and focused
  28. // on a matching window.
  29. // - Foo.EngagementTime.Background: Time when the user is engaged and not
  30. // focused on a matching window, but the Guest OS is otherwise active in the
  31. // background.
  32. // - Foo.Engagement.FooTotal: Total of Foreground and Background.
  33. class GuestOsEngagementMetrics : public wm::ActivationChangeObserver,
  34. public session_manager::SessionManagerObserver,
  35. public chromeos::PowerManagerClient::Observer {
  36. public:
  37. using WindowMatcher = base::RepeatingCallback<bool(const aura::Window*)>;
  38. GuestOsEngagementMetrics(PrefService* pref_service,
  39. WindowMatcher window_matcher,
  40. const std::string& pref_prefix,
  41. const std::string& uma_name);
  42. GuestOsEngagementMetrics(const GuestOsEngagementMetrics&) = delete;
  43. GuestOsEngagementMetrics& operator=(const GuestOsEngagementMetrics&) = delete;
  44. ~GuestOsEngagementMetrics() override;
  45. // Instead of using |window_matcher_|, we let consumers define when the Guest
  46. // OS is active in the background. This function should be called whenever
  47. // changes.
  48. void SetBackgroundActive(bool background_active);
  49. // wm::ActivationChangeObserver:
  50. void OnWindowActivated(wm::ActivationChangeObserver::ActivationReason reason,
  51. aura::Window* gained_active,
  52. aura::Window* lost_active) override;
  53. // session_manager::SessionManagerObserver:
  54. void OnSessionStateChanged() override;
  55. // chromeos::PowerManagerClient::Observer:
  56. void ScreenIdleStateChanged(
  57. const power_manager::ScreenIdleState& proto) override;
  58. static std::unique_ptr<GuestOsEngagementMetrics>
  59. GetEngagementMetricsForTesting(PrefService* pref_service,
  60. WindowMatcher window_matcher,
  61. const std::string& pref_prefix,
  62. const std::string& uma_name,
  63. const base::Clock* clock,
  64. const base::TickClock* tick_clock);
  65. private:
  66. // Private, for testing use only
  67. GuestOsEngagementMetrics(PrefService* pref_service,
  68. WindowMatcher window_matcher,
  69. const std::string& pref_prefix,
  70. const std::string& uma_name,
  71. const base::Clock* clock,
  72. const base::TickClock* tick_clock);
  73. // Restores accumulated engagement time in previous sessions from profile
  74. // preferences.
  75. void RestoreEngagementTimeFromPrefs();
  76. // Called periodically to save accumulated results to profile preferences.
  77. void SaveEngagementTimeToPrefs();
  78. // Called whenever engagement state is changed. Time spent in last state is
  79. // accumulated to corresponding metrics.
  80. void UpdateEngagementTime();
  81. // Records accumulated engagement time metrics to UMA if necessary (i.e. day
  82. // has changed).
  83. void RecordEngagementTimeToUmaIfNeeded();
  84. // Resets accumulated engagement times to zero, and updates both OS version
  85. // and day ID.
  86. void ResetEngagementTimePrefs();
  87. bool ShouldAccumulateEngagementTotalTime() const;
  88. bool ShouldAccumulateEngagementForegroundTime() const;
  89. bool ShouldAccumulateEngagementBackgroundTime() const;
  90. bool ShouldRecordEngagementTimeToUma() const;
  91. PrefService* const pref_service_;
  92. WindowMatcher window_matcher_;
  93. std::string pref_prefix_;
  94. std::string uma_name_;
  95. // |clock_| is used for determining when to log to UMA, while |tick_clock_|
  96. // is used to calculate elapsed time.
  97. const base::Clock* clock_;
  98. const base::TickClock* tick_clock_;
  99. base::RepeatingTimer update_engagement_time_timer_;
  100. base::RepeatingTimer save_engagement_time_to_prefs_timer_;
  101. base::TimeTicks last_update_ticks_;
  102. // States for determining which engagement metrics should we accumulate to.
  103. bool session_active_ = false;
  104. bool screen_dimmed_ = false;
  105. bool background_active_ = false;
  106. bool matched_window_active_ = false;
  107. // Accumulated results and associated state which are saved to profile
  108. // preferences at fixed interval.
  109. int day_id_ = 0;
  110. base::TimeDelta engagement_time_total_;
  111. base::TimeDelta engagement_time_foreground_;
  112. base::TimeDelta engagement_time_background_;
  113. };
  114. } // namespace guest_os
  115. #endif // COMPONENTS_GUEST_OS_GUEST_OS_ENGAGEMENT_METRICS_H_