ambient_multi_screen_metrics_recorder.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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_AMBIENT_METRICS_AMBIENT_MULTI_SCREEN_METRICS_RECORDER_H_
  5. #define ASH_AMBIENT_METRICS_AMBIENT_MULTI_SCREEN_METRICS_RECORDER_H_
  6. #include "ash/ash_export.h"
  7. #include "ash/constants/ambient_animation_theme.h"
  8. #include "base/containers/flat_set.h"
  9. #include "base/scoped_multi_source_observation.h"
  10. #include "base/time/time.h"
  11. #include "base/timer/timer.h"
  12. #include "third_party/abseil-cpp/absl/types/optional.h"
  13. #include "ui/lottie/animation.h"
  14. #include "ui/lottie/animation_observer.h"
  15. namespace ash {
  16. // Records metrics for multi-screen usage in ambient mode:
  17. // * The number of screens active during ambient mode.
  18. // * The average instantaneous offset in timestamp between the animations
  19. // playing on each screen. This metric is not recorded for single-screen
  20. // ambient mode sessions.
  21. // AmbientMultiScreenMetricsRecorder's lifetime is meant to match that of a
  22. // single ambient mode session. Per-session metrics are recorded in its
  23. // destructor.
  24. class ASH_EXPORT AmbientMultiScreenMetricsRecorder
  25. : public lottie::AnimationObserver {
  26. public:
  27. explicit AmbientMultiScreenMetricsRecorder(AmbientAnimationTheme theme);
  28. AmbientMultiScreenMetricsRecorder(const AmbientMultiScreenMetricsRecorder&) =
  29. delete;
  30. AmbientMultiScreenMetricsRecorder& operator=(
  31. const AmbientMultiScreenMetricsRecorder&) = delete;
  32. ~AmbientMultiScreenMetricsRecorder() override;
  33. // Registers a screen and its corresponding |animation|. |animation| may be
  34. // null if the ambient UI does not have an associated animation (ex: slideshow
  35. // mode). AmbientMultiScreenMetricsRecorder may outlive the incoming
  36. // |animation| if desired.
  37. void RegisterScreen(lottie::Animation* animation);
  38. private:
  39. // Period at which we calculate the mean animation timestamp offset and
  40. // record it. All samples are cleared for the next recording.
  41. static constexpr base::TimeDelta kMeanTimestampOffsetFlushPeriod =
  42. base::Minutes(1);
  43. static bool IsPlaybackConfigValid(
  44. const absl::optional<lottie::Animation::PlaybackConfig>& playback_config);
  45. // lottie::AnimationObserver implementation:
  46. void AnimationFramePainted(const lottie::Animation* animation,
  47. float t) override;
  48. void AnimationIsDeleting(const lottie::Animation* animation) override;
  49. absl::optional<base::TimeDelta> GetOffsetBetweenAnimations(
  50. const lottie::Animation& animation_l,
  51. const lottie::Animation& animation_r) const;
  52. const AmbientAnimationTheme theme_;
  53. int num_registered_screens_ = 0;
  54. base::flat_set<const lottie::Animation*> registered_animations_;
  55. base::ScopedMultiSourceObservation<lottie::Animation,
  56. lottie::AnimationObserver>
  57. animation_observations_{this};
  58. };
  59. } // namespace ash
  60. #endif // ASH_AMBIENT_METRICS_AMBIENT_MULTI_SCREEN_METRICS_RECORDER_H_