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 // Helper function to serialize a ChromeUserMetricsExtension proto. 111 static std::string SerializeLog(const ChromeUserMetricsExtension& uma_proto); 112 113 // Retrieves the storage parameters to control the reporting service. 114 static UnsentLogStore::UnsentLogStoreLimits GetLogStoreLimits(); 115 116 // Manages on-device recording of events. 117 std::unique_ptr<StructuredMetricsRecorder> recorder_; 118 119 // Service for uploading completed logs. 120 std::unique_ptr<reporting::StructuredMetricsReportingService> 121 reporting_service_; 122 123 // Schedules when logs will be created. 124 std::unique_ptr<StructuredMetricsScheduler> scheduler_; 125 126 // Marks that initialization has completed. 127 bool initialize_complete_ = false; 128 129 // Represents if structured metrics and the service is enabled. This isn't 130 // to indicate if the service is recording. 131 bool structured_metrics_enabled_ = false; 132 133 // The metrics client |this| is service is associated. 134 raw_ptr<MetricsServiceClient> client_; 135 136 SEQUENCE_CHECKER(sequence_checker_); 137 138 base::WeakPtrFactory<StructuredMetricsService> weak_factory_{this}; 139 }; 140 141 } // namespace metrics::structured 142 143 #endif // COMPONENTS_METRICS_STRUCTURED_STRUCTURED_METRICS_SERVICE_H_ 144