background_sync_metrics.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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_BACKGROUND_SYNC_BACKGROUND_SYNC_METRICS_H_
  5. #define COMPONENTS_BACKGROUND_SYNC_BACKGROUND_SYNC_METRICS_H_
  6. #include "base/callback.h"
  7. #include "base/memory/raw_ptr.h"
  8. #include "base/memory/weak_ptr.h"
  9. #include "services/metrics/public/cpp/ukm_source_id.h"
  10. #include "third_party/abseil-cpp/absl/types/optional.h"
  11. #include "third_party/blink/public/common/service_worker/service_worker_status_code.h"
  12. namespace {
  13. // Exponential bucket spacing for UKM event data.
  14. constexpr double kUkmEventDataBucketSpacing = 2.0;
  15. } // namespace
  16. namespace background_sync {
  17. class BackgroundSyncDelegate;
  18. } // namespace background_sync
  19. namespace url {
  20. class Origin;
  21. } // namespace url
  22. // Lives entirely on the UI thread.
  23. class BackgroundSyncMetrics {
  24. public:
  25. using RecordCallback = base::OnceCallback<void(ukm::SourceId)>;
  26. explicit BackgroundSyncMetrics(
  27. background_sync::BackgroundSyncDelegate* delegate);
  28. BackgroundSyncMetrics(const BackgroundSyncMetrics&) = delete;
  29. BackgroundSyncMetrics& operator=(const BackgroundSyncMetrics&) = delete;
  30. ~BackgroundSyncMetrics();
  31. void MaybeRecordOneShotSyncRegistrationEvent(const url::Origin& origin,
  32. bool can_fire,
  33. bool is_reregistered);
  34. void MaybeRecordPeriodicSyncRegistrationEvent(const url::Origin& origin,
  35. int min_interval,
  36. bool is_reregistered);
  37. void MaybeRecordOneShotSyncCompletionEvent(
  38. const url::Origin& origin,
  39. blink::ServiceWorkerStatusCode status_code,
  40. int num_attempts,
  41. int max_attempts);
  42. void MaybeRecordPeriodicSyncEventCompletion(
  43. const url::Origin& origin,
  44. blink::ServiceWorkerStatusCode status_code,
  45. int num_attempts,
  46. int max_attempts);
  47. private:
  48. friend class BackgroundSyncMetricsBrowserTest;
  49. void DidGetBackgroundSourceId(RecordCallback record_callback,
  50. absl::optional<ukm::SourceId> source_id);
  51. void RecordOneShotSyncRegistrationEvent(bool can_fire,
  52. bool is_reregistered,
  53. ukm::SourceId source_id);
  54. void RecordPeriodicSyncRegistrationEvent(int min_interval,
  55. bool is_reregistered,
  56. ukm::SourceId source_id);
  57. void RecordOneShotSyncCompletionEvent(
  58. blink::ServiceWorkerStatusCode status_code,
  59. int num_attempts,
  60. int max_attempts,
  61. ukm::SourceId source_id);
  62. void RecordPeriodicSyncEventCompletion(
  63. blink::ServiceWorkerStatusCode status_code,
  64. int num_attempts,
  65. int max_attempts,
  66. ukm::SourceId source_id);
  67. raw_ptr<background_sync::BackgroundSyncDelegate> delegate_;
  68. // Used to signal tests that a UKM event has been recorded.
  69. base::OnceClosure ukm_event_recorded_for_testing_;
  70. base::WeakPtrFactory<BackgroundSyncMetrics> weak_ptr_factory_{this};
  71. };
  72. #endif // COMPONENTS_BACKGROUND_SYNC_BACKGROUND_SYNC_METRICS_H_