legacymetrics_client.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright 2020 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_FUCHSIA_LEGACYMETRICS_LEGACYMETRICS_CLIENT_H_
  5. #define COMPONENTS_FUCHSIA_LEGACYMETRICS_LEGACYMETRICS_CLIENT_H_
  6. #include <fuchsia/legacymetrics/cpp/fidl.h>
  7. #include <memory>
  8. #include <vector>
  9. #include "base/callback.h"
  10. #include "base/memory/weak_ptr.h"
  11. #include "base/sequence_checker.h"
  12. #include "base/time/time.h"
  13. #include "base/timer/timer.h"
  14. #include "components/fuchsia_legacymetrics/legacymetrics_user_event_recorder.h"
  15. namespace fuchsia_legacymetrics {
  16. // Used to report events & histogram data to the
  17. // fuchsia.legacymetrics.MetricsRecorder service.
  18. // LegacyMetricsClient must be Start()ed on an IO-capable sequence.
  19. // Cannot be used in conjunction with other metrics reporting services.
  20. // Must be constructed, used, and destroyed on the same sequence.
  21. class LegacyMetricsClient {
  22. public:
  23. // Maximum number of Events to send to Record() at a time, so as to not exceed
  24. // the 64KB FIDL maximum message size.
  25. static constexpr size_t kMaxBatchSize = 50;
  26. // Constants for FIDL reconnection with exponential backoff.
  27. static constexpr base::TimeDelta kInitialReconnectDelay = base::Seconds(1);
  28. static constexpr base::TimeDelta kMaxReconnectDelay = base::Hours(1);
  29. static constexpr size_t kReconnectBackoffFactor = 2;
  30. using ReportAdditionalMetricsCallback = base::RepeatingCallback<void(
  31. base::OnceCallback<void(std::vector<fuchsia::legacymetrics::Event>)>)>;
  32. using NotifyFlushCallback =
  33. base::OnceCallback<void(base::OnceClosure completion_cb)>;
  34. LegacyMetricsClient();
  35. ~LegacyMetricsClient();
  36. explicit LegacyMetricsClient(const LegacyMetricsClient&) = delete;
  37. LegacyMetricsClient& operator=(const LegacyMetricsClient&) = delete;
  38. // Disables automatic MetricsRecorder connection. Caller will have to supply
  39. // MetricsRecorder by calling SetMetricsRecorder(). Must be called before
  40. // Start().
  41. void DisableAutoConnect();
  42. // Sets |metrics_recorder| to use. Should be called only after
  43. // DisableAutoConnect().
  44. void SetMetricsRecorder(
  45. fidl::InterfaceHandle<fuchsia::legacymetrics::MetricsRecorder>
  46. metrics_recorder);
  47. // Starts buffering data and schedules metric reporting after every
  48. // |report_interval|.
  49. void Start(base::TimeDelta report_interval);
  50. // Sets an asynchronous |callback| to be invoked just prior to reporting,
  51. // allowing users to asynchronously gather and provide additional custom
  52. // metrics. |callback| will receive the list of metrics when they are ready.
  53. // Reporting is paused until |callback| is fulfilled.
  54. // If used, then this method must be called before calling Start().
  55. void SetReportAdditionalMetricsCallback(
  56. ReportAdditionalMetricsCallback callback);
  57. // Sets a |callback| which is invoked to warn that the connection to the
  58. // remote MetricsRecorder will be terminated. The completion closure passed to
  59. // |callback| should be invoked to signal flush completion.
  60. void SetNotifyFlushCallback(NotifyFlushCallback callback);
  61. // Use when caller needs an explicit flush and then disconnect, such as before
  62. // termination. Caller will be notified when all events in the buffer are
  63. // sent.
  64. void FlushAndDisconnect(base::OnceClosure on_flush_complete);
  65. private:
  66. void ConnectFromComponentContext();
  67. void SetMetricsRecorderInternal(
  68. fidl::InterfaceHandle<fuchsia::legacymetrics::MetricsRecorder>
  69. metrics_recorder);
  70. void ScheduleNextReport();
  71. void StartReport();
  72. void Report(std::vector<fuchsia::legacymetrics::Event> additional_metrics);
  73. void OnMetricsRecorderDisconnected(zx_status_t status);
  74. void ReconnectMetricsRecorder();
  75. void OnCloseSoon();
  76. void CompleteFlush();
  77. void ResetMetricsRecorderState();
  78. // Incrementally sends the contents of |to_send_| to |metrics_recorder_|.
  79. void DrainBuffer();
  80. base::TimeDelta reconnect_delay_ = kInitialReconnectDelay;
  81. base::TimeDelta report_interval_;
  82. ReportAdditionalMetricsCallback report_additional_callback_;
  83. NotifyFlushCallback notify_flush_callback_;
  84. bool is_flushing_ = false;
  85. bool record_ack_pending_ = false;
  86. std::vector<fuchsia::legacymetrics::Event> to_send_;
  87. std::unique_ptr<LegacyMetricsUserActionRecorder> user_events_recorder_;
  88. bool auto_connect_ = true;
  89. base::RetainingOneShotTimer reconnect_timer_;
  90. fuchsia::legacymetrics::MetricsRecorderPtr metrics_recorder_;
  91. base::RetainingOneShotTimer report_timer_;
  92. SEQUENCE_CHECKER(sequence_checker_);
  93. std::vector<base::OnceClosure> on_flush_complete_closures_;
  94. // Prevents use-after-free if |report_additional_callback_| is invoked after
  95. // |this| is destroyed.
  96. base::WeakPtrFactory<LegacyMetricsClient> weak_factory_{this};
  97. };
  98. } // namespace fuchsia_legacymetrics
  99. #endif // COMPONENTS_FUCHSIA_LEGACYMETRICS_LEGACYMETRICS_CLIENT_H_