feature_discovery_duration_reporter_impl.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright 2022 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 ASH_METRICS_FEATURE_DISCOVERY_DURATION_REPORTER_IMPL_H_
  5. #define ASH_METRICS_FEATURE_DISCOVERY_DURATION_REPORTER_IMPL_H_
  6. #include <map>
  7. #include "ash/public/cpp/feature_discovery_duration_reporter.h"
  8. #include "ash/public/cpp/session/session_observer.h"
  9. #include "ash/session/session_controller_impl.h"
  10. #include "base/scoped_observation.h"
  11. class PrefRegistrySimple;
  12. class PrefService;
  13. namespace base {
  14. class TimeTicks;
  15. }
  16. namespace ash {
  17. class FeatureDiscoveryDurationReporterImpl
  18. : public FeatureDiscoveryDurationReporter,
  19. public SessionObserver {
  20. public:
  21. explicit FeatureDiscoveryDurationReporterImpl(
  22. SessionController* session_controller);
  23. FeatureDiscoveryDurationReporterImpl(
  24. const FeatureDiscoveryDurationReporterImpl&) = delete;
  25. FeatureDiscoveryDurationReporterImpl& operator=(
  26. const FeatureDiscoveryDurationReporterImpl&) = delete;
  27. ~FeatureDiscoveryDurationReporterImpl() override;
  28. static void RegisterProfilePrefs(PrefRegistrySimple* registry);
  29. // FeatureDiscoveryDurationReporter:
  30. void MaybeActivateObservation(
  31. feature_discovery::TrackableFeature feature) override;
  32. void MaybeFinishObservation(
  33. feature_discovery::TrackableFeature feature) override;
  34. void AddObserver(ReporterObserver* observer) override;
  35. void RemoveObserver(ReporterObserver* observer) override;
  36. private:
  37. friend class FeatureDiscoveryDurationReporterImplTest;
  38. FRIEND_TEST_ALL_PREFIXES(FeatureDiscoveryDurationReporterBrowserTest,
  39. SaveCumulatedTimeWhenSignout);
  40. // Activates/deactivates the reporter.
  41. void SetActive(bool active);
  42. // Implements activation/deactivation.
  43. void Activate();
  44. void Deactivate();
  45. // SessionObserver:
  46. void OnSessionStateChanged(session_manager::SessionState state) override;
  47. void OnActiveUserPrefServiceChanged(PrefService* pref_service) override;
  48. bool is_active() const { return is_active_; }
  49. base::ObserverList<ReporterObserver> observers_;
  50. // The mappings from trackable feature enum types to observation start
  51. // timestamps. It gets cleared when the reporter is deactivated. New entries
  52. // are added when:
  53. // 1. a new observation starts; or
  54. // 2. unfinished observations resume when the reporter is activated.
  55. std::map<feature_discovery::TrackableFeature, base::TimeTicks>
  56. active_time_recordings_;
  57. // Specifies the pref service whose data is read/written by the reporter. It
  58. // is set when the active user pref service changes.
  59. // NOTE: `active_pref_service_` is not reset when signing out. Because the
  60. // reporter instance should be destroyed in this scenario.
  61. base::raw_ptr<PrefService> active_pref_service_ = nullptr;
  62. // If true, starting observations on feature discovery is allowed.
  63. bool is_active_ = false;
  64. base::ScopedObservation<SessionController, SessionObserver>
  65. session_controller_observation_{this};
  66. };
  67. } // namespace ash
  68. #endif // ASH_METRICS_FEATURE_DISCOVERY_DURATION_REPORTER_IMPL_H_