metrics_provider.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Copyright 2014 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_METRICS_METRICS_PROVIDER_H_
  5. #define COMPONENTS_METRICS_METRICS_PROVIDER_H_
  6. #include "base/callback.h"
  7. #include "base/time/time.h"
  8. namespace base {
  9. class HistogramSnapshotManager;
  10. } // namespace base
  11. namespace metrics {
  12. class ChromeUserMetricsExtension;
  13. class SystemProfileProto;
  14. // MetricsProvider is an interface allowing different parts of the UMA protos to
  15. // be filled out by different classes.
  16. class MetricsProvider {
  17. public:
  18. MetricsProvider();
  19. MetricsProvider(const MetricsProvider&) = delete;
  20. MetricsProvider& operator=(const MetricsProvider&) = delete;
  21. virtual ~MetricsProvider();
  22. // Called after initialization of MetricsService and field trials.
  23. virtual void Init();
  24. // Called during service initialization to allow the provider to start any
  25. // async initialization tasks. The service will wait for the provider to
  26. // call |done_callback| before generating logs for the current session.
  27. virtual void AsyncInit(base::OnceClosure done_callback);
  28. // Called when a new MetricsLog is created.
  29. // This can be used to log a histogram that will appear in the log. Not safe
  30. // for some other uses, like user actions.
  31. // TODO(crbug.com/1171830): Improve this.
  32. virtual void OnDidCreateMetricsLog();
  33. // Called when metrics recording has been enabled.
  34. virtual void OnRecordingEnabled();
  35. // Called when metrics recording has been disabled.
  36. virtual void OnRecordingDisabled();
  37. // Called when metrics client identifiers have been reset.
  38. //
  39. // Metrics providers should clean up any persisted state that could be used to
  40. // associate the previous identifier with the new one.
  41. //
  42. // Currently this method is only invoked in UKM.
  43. virtual void OnClientStateCleared();
  44. // Called when the application is going into background mode, on platforms
  45. // where applications may be killed when going into the background (Android,
  46. // iOS). Providers that buffer histogram data in memory should persist
  47. // histograms in this callback, as the application may be killed without
  48. // further notification after this callback.
  49. virtual void OnAppEnterBackground();
  50. // Returns whether there are "independent" metrics that can be retrieved
  51. // with a call to ProvideIndependentMetrics().
  52. virtual bool HasIndependentMetrics();
  53. // Provides a complete and independent uma proto + metrics for uploading.
  54. // Called once every time HasIndependentMetrics() returns true. The passed in
  55. // |uma_proto| is by default filled with current session id and core system
  56. // profile information. This function is called on main thread, but the
  57. // provider can do async work to fill in |uma_proto| and run |done_callback|
  58. // on calling thread when complete. Ownership of the passed objects remains
  59. // with the caller and those objects will live until the callback is executed.
  60. virtual void ProvideIndependentMetrics(
  61. base::OnceCallback<void(bool)> done_callback,
  62. ChromeUserMetricsExtension* uma_proto,
  63. base::HistogramSnapshotManager* snapshot_manager);
  64. // Provides additional metrics into the system profile. This is a convenience
  65. // method over ProvideSystemProfileMetricsWithLogCreationTime() without the
  66. // |log_creation_time| param. Should not be called directly by services.
  67. virtual void ProvideSystemProfileMetrics(
  68. SystemProfileProto* system_profile_proto);
  69. // Provides additional metrics into the system profile. The log creation
  70. // time param provides a timestamp of when the log was opened, which is needed
  71. // for some metrics providers.
  72. virtual void ProvideSystemProfileMetricsWithLogCreationTime(
  73. base::TimeTicks log_creation_time,
  74. SystemProfileProto* system_profile_proto);
  75. // Called once at startup to see whether this provider has critical data
  76. // to provide about the previous session.
  77. // Returning true will trigger ProvidePreviousSessionData on all other
  78. // registered metrics providers.
  79. // Default implementation always returns false.
  80. virtual bool HasPreviousSessionData();
  81. // Called when building a log about the previous session, so the provider
  82. // can provide data about it. Stability metrics can be provided
  83. // directly into |stability_proto| fields or by logging stability histograms
  84. // via the UMA_STABILITY_HISTOGRAM_ENUMERATION() macro.
  85. virtual void ProvidePreviousSessionData(
  86. ChromeUserMetricsExtension* uma_proto);
  87. // Called when building a log about the current session, so the provider
  88. // can provide data about it.
  89. virtual void ProvideCurrentSessionData(ChromeUserMetricsExtension* uma_proto);
  90. // Called when building a UKM log about the current session. UKM-specific data
  91. // should generally only be emitted through this method, and UMA data should
  92. // be emitted through ProvideCurrentSessionData().
  93. virtual void ProvideCurrentSessionUKMData();
  94. // Provides additional stability metrics. Stability metrics can be provided
  95. // directly into |stability_proto| fields or by logging stability histograms
  96. // via the UMA_STABILITY_HISTOGRAM_ENUMERATION() macro.
  97. virtual void ProvideStabilityMetrics(
  98. SystemProfileProto* system_profile_proto);
  99. // Called to indicate that saved stability prefs should be cleared, e.g.
  100. // because they are from an old version and should not be kept.
  101. virtual void ClearSavedStabilityMetrics();
  102. // Called during regular collection to explicitly load histogram snapshots
  103. // using a snapshot manager. PrepareDeltas() will have already been called
  104. // and FinishDeltas() will be called later; calls to only PrepareDelta(),
  105. // not PrepareDeltas (plural), should be made.
  106. virtual void RecordHistogramSnapshots(
  107. base::HistogramSnapshotManager* snapshot_manager);
  108. // Called during collection of initial metrics to explicitly load histogram
  109. // snapshots using a snapshot manager. PrepareDeltas() will have already
  110. // been called and FinishDeltas() will be called later; calls to only
  111. // PrepareDelta(), not PrepareDeltas (plural), should be made.
  112. virtual void RecordInitialHistogramSnapshots(
  113. base::HistogramSnapshotManager* snapshot_manager);
  114. };
  115. } // namespace metrics
  116. #endif // COMPONENTS_METRICS_METRICS_PROVIDER_H_