reporting_service.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright 2017 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. // This file defines a service that sends metrics logs to a server.
  5. #ifndef COMPONENTS_METRICS_REPORTING_SERVICE_H_
  6. #define COMPONENTS_METRICS_REPORTING_SERVICE_H_
  7. #include <stdint.h>
  8. #include <string>
  9. #include "base/memory/raw_ptr.h"
  10. #include "base/time/time.h"
  11. #include "build/build_config.h"
  12. #include "components/metrics/data_use_tracker.h"
  13. #include "components/metrics/metrics_log_uploader.h"
  14. #include "third_party/metrics_proto/reporting_info.pb.h"
  15. #include "url/gurl.h"
  16. class PrefService;
  17. class PrefRegistrySimple;
  18. namespace metrics {
  19. class LogStore;
  20. class MetricsUploadScheduler;
  21. class MetricsServiceClient;
  22. // ReportingService is an abstract class which uploads serialized logs from a
  23. // LogStore to a remote server. A concrete implementation of this class must
  24. // provide the specific LogStore and parameters for the MetricsLogUploader, and
  25. // can also implement hooks to record histograms based on certain events that
  26. // occur while attempting to upload logs.
  27. class ReportingService {
  28. public:
  29. // Creates a ReportingService with the given |client|, |local_state|, and
  30. // |max_retransmit_size|. Does not take ownership of the parameters; instead
  31. // it stores a weak pointer to each. Caller should ensure that the parameters
  32. // are valid for the lifetime of this class.
  33. ReportingService(MetricsServiceClient* client,
  34. PrefService* local_state,
  35. size_t max_retransmit_size);
  36. ReportingService(const ReportingService&) = delete;
  37. ReportingService& operator=(const ReportingService&) = delete;
  38. virtual ~ReportingService();
  39. // Completes setup tasks that can't be done at construction time.
  40. // Loads persisted logs and creates the MetricsUploadScheduler.
  41. void Initialize();
  42. // Starts the metrics reporting system.
  43. // Should be called when metrics enabled or new logs are created.
  44. // When the service is already running, this is a safe no-op.
  45. void Start();
  46. // Shuts down the metrics system. Should be called at shutdown, or if metrics
  47. // are turned off.
  48. void Stop();
  49. // Enable/disable transmission of accumulated logs and crash reports (dumps).
  50. // Calling Start() automatically enables reporting, but sending is
  51. // asyncronous so this can be called immediately after Start() to prevent
  52. // any uploading.
  53. void EnableReporting();
  54. void DisableReporting();
  55. // True iff reporting is currently enabled.
  56. bool reporting_active() const;
  57. // Registers local state prefs used by this class. This should only be called
  58. // once.
  59. static void RegisterPrefs(PrefRegistrySimple* registry);
  60. protected:
  61. MetricsServiceClient* client() const { return client_; }
  62. private:
  63. // Retrieves the log store backing this service.
  64. virtual LogStore* log_store() = 0;
  65. // Getters for MetricsLogUploader parameters.
  66. virtual GURL GetUploadUrl() const = 0;
  67. virtual GURL GetInsecureUploadUrl() const = 0;
  68. virtual base::StringPiece upload_mime_type() const = 0;
  69. virtual MetricsLogUploader::MetricServiceType service_type() const = 0;
  70. // Methods for recording data to histograms.
  71. virtual void LogActualUploadInterval(base::TimeDelta interval) {}
  72. virtual void LogCellularConstraint(bool upload_canceled) {}
  73. virtual void LogResponseOrErrorCode(int response_code,
  74. int error_code,
  75. bool was_https) {}
  76. virtual void LogSuccessLogSize(size_t log_size) {}
  77. virtual void LogSuccessMetadata(const std::string& staged_log) {}
  78. virtual void LogLargeRejection(size_t log_size) {}
  79. // If recording is enabled, begins uploading the next completed log from
  80. // the log manager, staging it if necessary.
  81. void SendNextLog();
  82. // Uploads the currently staged log (which must be non-null).
  83. void SendStagedLog();
  84. // Called after transmission completes (either successfully or with failure).
  85. void OnLogUploadComplete(int response_code, int error_code, bool was_https);
  86. // Used to interact with the embedder. Weak pointer; must outlive |this|
  87. // instance.
  88. const raw_ptr<MetricsServiceClient> client_;
  89. // Largest log size to attempt to retransmit.
  90. size_t max_retransmit_size_;
  91. // Indicate whether recording and reporting are currently happening.
  92. // These should not be set directly, but by calling SetRecording and
  93. // SetReporting.
  94. bool reporting_active_;
  95. // Instance of the helper class for uploading logs.
  96. std::unique_ptr<MetricsLogUploader> log_uploader_;
  97. // Whether there is a current log upload in progress.
  98. bool log_upload_in_progress_;
  99. // The scheduler for determining when uploads should happen.
  100. std::unique_ptr<MetricsUploadScheduler> upload_scheduler_;
  101. // Pointer used for obtaining data use pref updater callback on above layers.
  102. std::unique_ptr<DataUseTracker> data_use_tracker_;
  103. // The tick count of the last time log upload has been finished and null if no
  104. // upload has been done yet.
  105. base::TimeTicks last_upload_finish_time_;
  106. // Info on current reporting state to send along with reports.
  107. ReportingInfo reporting_info_;
  108. SEQUENCE_CHECKER(sequence_checker_);
  109. // Weak pointers factory used to post task on different threads. All weak
  110. // pointers managed by this factory have the same lifetime as
  111. // ReportingService.
  112. base::WeakPtrFactory<ReportingService> self_ptr_factory_{this};
  113. };
  114. } // namespace metrics
  115. #endif // COMPONENTS_METRICS_REPORTING_SERVICE_H_