1 // Copyright 2024 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_DWA_DWA_SERVICE_H_ 6 #define COMPONENTS_METRICS_DWA_DWA_SERVICE_H_ 7 8 #include <cstdint> 9 #include <memory> 10 11 #include "base/component_export.h" 12 #include "base/memory/weak_ptr.h" 13 #include "base/sequence_checker.h" 14 #include "components/metrics/dwa/dwa_recorder.h" 15 #include "components/metrics/dwa/dwa_reporting_service.h" 16 #include "components/metrics/metrics_rotation_scheduler.h" 17 #include "components/metrics/metrics_service_client.h" 18 #include "components/metrics/unsent_log_store.h" 19 #include "components/prefs/pref_registry_simple.h" 20 #include "components/prefs/pref_service.h" 21 #include "third_party/metrics_proto/dwa/deidentified_web_analytics.pb.h" 22 23 namespace metrics::dwa { 24 25 // The DwaService is responsible for collecting and uploading deindentified web 26 // analytics events. COMPONENT_EXPORT(DWA)27class COMPONENT_EXPORT(DWA) DwaService { 28 public: 29 DwaService(MetricsServiceClient* client, PrefService* pref_service); 30 DwaService(const DwaService&) = delete; 31 DwaService& operator=(const DwaService&) = delete; 32 33 ~DwaService(); 34 35 void EnableReporting(); 36 void DisableReporting(); 37 38 // Flushes any event currently in the recorder to prefs. 39 void Flush(metrics::MetricsLogsEventManager::CreateReason reason); 40 41 // Clears all event and log data. 42 void Purge(); 43 44 // Records coarse system profile into CoarseSystemInfo of the deidentified web 45 // analytics report proto. 46 static void RecordCoarseSystemInformation( 47 MetricsServiceClient& client, 48 const PrefService& local_state, 49 ::dwa::CoarseSystemInfo* coarse_system_info); 50 51 // Generate client id which changes between days. We store this id in a 52 // uint64 instead of base::Uuid as it is eventually stored in a proto with 53 // this type. We are not concerned with id collisions as ids are only meant to 54 // be compared within single days and they are used for k-anonymity (where it 55 // would mean undercounting for k-anonymity). 56 static uint64_t GetEphemeralClientId(PrefService& local_state); 57 58 // Register prefs from `dwa_pref_names.h`. 59 static void RegisterPrefs(PrefRegistrySimple* registry); 60 61 metrics::UnsentLogStore* unsent_log_store(); 62 63 private: 64 SEQUENCE_CHECKER(sequence_checker_); 65 66 // Periodically called by |scheduler_| to advance processing of logs. 67 void RotateLog(); 68 69 // Constructs a new DeidentifiedWebAnalyticsReport from available data and 70 // stores it in |unsent_log_store_|. 71 void BuildAndStoreLog(metrics::MetricsLogsEventManager::CreateReason reason); 72 73 // Retrieves the storage parameters to control the reporting service. 74 static UnsentLogStore::UnsentLogStoreLimits GetLogStoreLimits(); 75 76 // Manages on-device recording of events. 77 raw_ptr<DwaRecorder> recorder_; 78 79 // The metrics client |this| is service is associated. 80 raw_ptr<MetricsServiceClient> client_; 81 82 // A weak pointer to the PrefService used to read and write preferences. 83 raw_ptr<PrefService> pref_service_; 84 85 // Service for uploading serialized logs. 86 DwaReportingService reporting_service_; 87 88 // The scheduler for determining when uploads should happen. 89 std::unique_ptr<MetricsRotationScheduler> scheduler_; 90 91 // Weak pointers factory used to post task on different threads. All weak 92 // pointers managed by this factory have the same lifetime as DwaService. 93 base::WeakPtrFactory<DwaService> self_ptr_factory_{this}; 94 }; 95 96 } // namespace metrics::dwa 97 98 #endif // COMPONENTS_METRICS_DWA_DWA_SERVICE_H_ 99