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 5 #ifndef COMPONENTS_METRICS_METRICS_LOG_STORE_H_ 6 #define COMPONENTS_METRICS_METRICS_LOG_STORE_H_ 7 8 #include <string> 9 10 #include "base/macros.h" 11 #include "base/metrics/histogram_base.h" 12 #include "base/optional.h" 13 #include "components/metrics/log_store.h" 14 #include "components/metrics/metrics_log.h" 15 #include "components/metrics/unsent_log_store.h" 16 17 class PrefService; 18 class PrefRegistrySimple; 19 20 namespace metrics { 21 22 class MetricsServiceClient; 23 24 // A LogStore implementation for storing UMA logs. 25 // This implementation keeps track of two types of logs, initial and ongoing, 26 // each stored in UnsentLogStore. It prioritizes staging initial logs over 27 // ongoing logs. 28 class MetricsLogStore : public LogStore { 29 public: 30 // Configurable limits for ensuring and restricting local log storage. 31 // 32 // |min_{initial,ongoing}_log_queue_count| are the minimum numbers of unsent 33 // logs that UnsentLogStore must persist before deleting old logs. 34 // 35 // |min_{initial,ongoing}_log_queue_size| are the minimum numbers of bytes in 36 // total across all logs within the initial or ongoing log queue that 37 // UnsentLogStore must persist before deleting old logs. 38 // 39 // If both |min_..._log_queue_count| and |min_..._log_queue_size| are 0, then 40 // this LogStore won't persist unsent logs to local storage. 41 // 42 // |max_ongoing_log_size| is the maximum size of any individual ongoing log. 43 // When set to 0, no limits are imposed, i.e. individual logs can be any size. 44 struct StorageLimits { 45 size_t min_initial_log_queue_count = 0; 46 size_t min_initial_log_queue_size = 0; 47 size_t min_ongoing_log_queue_count = 0; 48 size_t min_ongoing_log_queue_size = 0; 49 size_t max_ongoing_log_size = 0; 50 }; 51 52 // Constructs a MetricsLogStore that persists data into |local_state|. 53 // |storage_limits| provides log count and size limits to enforce when 54 // persisting logs to local storage. |signing_key| is used to generate a 55 // signature of a log, which will be uploaded to validate data integrity. 56 MetricsLogStore(PrefService* local_state, 57 StorageLimits storage_limits, 58 const std::string& signing_key); 59 ~MetricsLogStore(); 60 61 // Registers local state prefs used by this class. 62 static void RegisterPrefs(PrefRegistrySimple* registry); 63 64 // Saves |log_data| as the given type. 65 void StoreLog(const std::string& log_data, 66 MetricsLog::LogType log_type, 67 base::Optional<base::HistogramBase::Count> samples_count); 68 69 // LogStore: 70 bool has_unsent_logs() const override; 71 bool has_staged_log() const override; 72 const std::string& staged_log() const override; 73 const std::string& staged_log_hash() const override; 74 const std::string& staged_log_signature() const override; 75 void StageNextLog() override; 76 void DiscardStagedLog() override; 77 void MarkStagedLogAsSent() override; 78 void TrimAndPersistUnsentLogs() override; 79 void LoadPersistedUnsentLogs() override; 80 81 // Inspection methods for tests. ongoing_log_count()82 size_t ongoing_log_count() const { return ongoing_log_queue_.size(); } initial_log_count()83 size_t initial_log_count() const { return initial_log_queue_.size(); } 84 85 private: 86 // Tracks whether unsent logs (if any) have been loaded from the serializer. 87 bool unsent_logs_loaded_; 88 89 // Logs stored with the INITIAL_STABILITY_LOG type that haven't been sent yet. 90 // These logs will be staged first when staging new logs. 91 UnsentLogStore initial_log_queue_; 92 // Logs stored with the ONGOING_LOG type that haven't been sent yet. 93 UnsentLogStore ongoing_log_queue_; 94 95 DISALLOW_COPY_AND_ASSIGN(MetricsLogStore); 96 }; 97 98 } // namespace metrics 99 100 #endif // COMPONENTS_METRICS_METRICS_LOG_STORE_H_ 101