1 // Copyright 2023 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef COMPONENTS_METRICS_STRUCTURED_STRUCTURED_METRICS_SERVICE_H_ 6 #define COMPONENTS_METRICS_STRUCTURED_STRUCTURED_METRICS_SERVICE_H_ 7 8 #include <memory> 9 10 #include "base/memory/weak_ptr.h" 11 #include "components/metrics/structured/reporting/structured_metrics_reporting_service.h" 12 #include "components/metrics/structured/structured_metrics_recorder.h" 13 #include "components/metrics/structured/structured_metrics_scheduler.h" 14 #include "components/metrics/unsent_log_store.h" 15 16 FORWARD_DECLARE_TEST(StructuredMetricsServiceTest, RotateLogs); 17 18 class PrefRegistrySimple; 19 20 namespace metrics { 21 class StructuredMetricsServiceTestBase; 22 class TestStructuredMetricsServiceDisabled; 23 24 FORWARD_DECLARE_TEST(TestStructuredMetricsServiceDisabled, 25 ValidStateWhenDisabled); 26 } // namespace metrics 27 28 namespace metrics::structured { 29 30 class OobeStructuredMetricsWatcher; 31 class StructuredMetricsServiceTest; 32 class StructuredMetricsMixin; 33 34 FORWARD_DECLARE_TEST(StructuredMetricsServiceTest, RotateLogs); 35 36 // The Structured Metrics Service is responsible for collecting and uploading 37 // Structured Metric events. 38 class StructuredMetricsService final { 39 public: 40 StructuredMetricsService(MetricsServiceClient* client, 41 PrefService* local_state, 42 std::unique_ptr<StructuredMetricsRecorder> recorder); 43 44 ~StructuredMetricsService(); 45 46 StructuredMetricsService(const StructuredMetricsService&) = delete; 47 StructuredMetricsService& operator=(StructuredMetricsService&) = delete; 48 49 void EnableRecording(); 50 void DisableRecording(); 51 52 void EnableReporting(); 53 void DisableReporting(); 54 55 // Flushes any event currently in the recorder to prefs. 56 void Flush(metrics::MetricsLogsEventManager::CreateReason reason); 57 58 // Clears all event and log data. 59 void Purge(); 60 61 MetricsServiceClient* GetMetricsServiceClient() const; 62 reporting_active()63 bool reporting_active() const { 64 return reporting_service_->reporting_active(); 65 } 66 recording_enabled()67 bool recording_enabled() const { return recorder_->recording_enabled(); } 68 recorder()69 StructuredMetricsRecorder* recorder() { return recorder_.get(); } 70 71 static void RegisterPrefs(PrefRegistrySimple* registry); 72 log_store()73 metrics::LogStore* log_store() { return reporting_service_->log_store(); } 74 75 private: 76 friend class StructuredMetricsServiceTest; 77 friend class StructuredMetricsMixin; 78 #if BUILDFLAG(IS_CHROMEOS) 79 friend class OobeStructuredMetricsWatcher; 80 #endif 81 friend class metrics::StructuredMetricsServiceTestBase; 82 83 FRIEND_TEST_ALL_PREFIXES(metrics::structured::StructuredMetricsServiceTest, 84 RotateLogs); 85 FRIEND_TEST_ALL_PREFIXES(metrics::TestStructuredMetricsServiceDisabled, 86 ValidStateWhenDisabled); 87 88 // Sets the instance of the recorder used for test. 89 void SetRecorderForTest(std::unique_ptr<StructuredMetricsRecorder> recorder); 90 91 // Callback function to get the upload interval. 92 base::TimeDelta GetUploadTimeInterval(); 93 94 // Creates a new log and sends any currently stages logs. 95 void RotateLogsAndSend(); 96 97 // Collects the events from the recorder and builds a new log. 98 void BuildAndStoreLog(metrics::MetricsLogsEventManager::CreateReason reason); 99 100 // Starts the initialization process for |this|. 101 void Initialize(); 102 103 // Fills out the UMA proto to be sent. 104 void InitializeUmaProto(ChromeUserMetricsExtension& uma_proto); 105 106 // Triggers an upload of recorded events outside of the normal cadence. 107 // This doesn't interfere with the normal cadence. 108 void ManualUpload(); 109 110 // Queue an upload if there are logs stored in the log store. This is meant to 111 // be used to start an upload when the service starts, so we do not have to 112 // wait until first upload to send events from the previous session. 113 // 114 // Reporting is assumed to be enabled by function. Must be checked before 115 // called. 116 void MaybeStartUpload(); 117 118 // Helper function to serialize a ChromeUserMetricsExtension proto. 119 static std::string SerializeLog(const ChromeUserMetricsExtension& uma_proto); 120 121 // Retrieves the storage parameters to control the reporting service. 122 static UnsentLogStore::UnsentLogStoreLimits GetLogStoreLimits(); 123 124 // Manages on-device recording of events. 125 std::unique_ptr<StructuredMetricsRecorder> recorder_; 126 127 // Service for uploading completed logs. 128 std::unique_ptr<reporting::StructuredMetricsReportingService> 129 reporting_service_; 130 131 // Schedules when logs will be created. 132 std::unique_ptr<StructuredMetricsScheduler> scheduler_; 133 134 // Marks that initialization has completed. 135 bool initialize_complete_ = false; 136 137 // Represents if structured metrics and the service is enabled. This isn't 138 // to indicate if the service is recording. 139 bool structured_metrics_enabled_ = false; 140 141 // Flag to make sure MaybeStartUpload() isn't called twice. 142 bool initial_upload_started_ = false; 143 144 // The metrics client |this| is service is associated. 145 raw_ptr<MetricsServiceClient> client_; 146 147 SEQUENCE_CHECKER(sequence_checker_); 148 149 base::WeakPtrFactory<StructuredMetricsService> weak_factory_{this}; 150 }; 151 152 } // namespace metrics::structured 153 154 #endif // COMPONENTS_METRICS_STRUCTURED_STRUCTURED_METRICS_SERVICE_H_ 155