1 // Copyright 2017 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_METRICS_LOG_STORE_H_ 6 #define COMPONENTS_METRICS_METRICS_LOG_STORE_H_ 7 8 #include <memory> 9 #include <string> 10 11 #include "base/metrics/histogram_base.h" 12 #include "base/sequence_checker.h" 13 #include "base/strings/string_piece.h" 14 #include "components/metrics/log_store.h" 15 #include "components/metrics/metrics_log.h" 16 #include "components/metrics/metrics_logs_event_manager.h" 17 #include "components/metrics/unsent_log_store.h" 18 #include "third_party/abseil-cpp/absl/types/optional.h" 19 20 class PrefService; 21 class PrefRegistrySimple; 22 23 namespace metrics { 24 25 class MetricsServiceClient; 26 27 // A LogStore implementation for storing UMA logs. 28 // This implementation keeps track of two types of logs, initial and ongoing, 29 // each stored in UnsentLogStore. It prioritizes staging initial logs over 30 // ongoing logs. 31 // 32 // An alternate log store can be set to persist ongoing logs. For example, this 33 // can be used to separate user logs from device logs on Chrome OS. If set, all 34 // ongoing logs will be written to this alternate log store. Ongoing logs from 35 // the alternate log store will be prioritized over ongoing logs from the native 36 // ongoing log store when logs are staged. If an alternate log store is bound, 37 // then logs will be prioritized in the following order: initial, alternate 38 // ongoing, native ongoing. 39 class MetricsLogStore : public LogStore { 40 public: 41 // Configurable limits for ensuring and restricting local log storage. 42 // 43 // |min_{initial,ongoing}_log_queue_count| are the minimum numbers of unsent 44 // logs that UnsentLogStore must persist before deleting old logs. 45 // 46 // |min_{initial,ongoing}_log_queue_size| are the minimum numbers of bytes in 47 // total across all logs within the initial or ongoing log queue that 48 // UnsentLogStore must persist before deleting old logs. 49 // 50 // If both |min_..._log_queue_count| and |min_..._log_queue_size| are 0, then 51 // this LogStore won't persist unsent logs to local storage. 52 // 53 // |max_ongoing_log_size| is the maximum size of any individual ongoing log. 54 // When set to 0, no limits are imposed, i.e. individual logs can be any size. 55 struct StorageLimits { 56 size_t min_initial_log_queue_count = 0; 57 size_t min_initial_log_queue_size = 0; 58 size_t min_ongoing_log_queue_count = 0; 59 size_t min_ongoing_log_queue_size = 0; 60 size_t max_ongoing_log_size = 0; 61 }; 62 63 // Constructs a MetricsLogStore that persists data into |local_state|. 64 // |storage_limits| provides log count and size limits to enforce when 65 // persisting logs to local storage. |signing_key| is used to generate a 66 // signature of a log, which will be uploaded to validate data integrity. 67 // |logs_event_manager| is used to notify observers of log events. Can be set 68 // to null if observing the events is not necessary. 69 MetricsLogStore(PrefService* local_state, 70 StorageLimits storage_limits, 71 const std::string& signing_key, 72 MetricsLogsEventManager* logs_event_manager); 73 74 MetricsLogStore(const MetricsLogStore&) = delete; 75 MetricsLogStore& operator=(const MetricsLogStore&) = delete; 76 77 ~MetricsLogStore() override; 78 79 // Registers local state prefs used by this class. 80 static void RegisterPrefs(PrefRegistrySimple* registry); 81 82 // Saves |log_data| as the given |log_type|. Before being stored, the data 83 // will be compressed, and a hash and signature will be computed. 84 // TODO(crbug/1052796): Remove this function, and use StoreLogInfo() 85 // everywhere instead. 86 void StoreLog(const std::string& log_data, 87 MetricsLog::LogType log_type, 88 const LogMetadata& log_metadata, 89 MetricsLogsEventManager::CreateReason reason); 90 91 // Saves a log, represented by a LogInfo object, as the given |log_type|. This 92 // is useful if the LogInfo instance needs to be created outside the main 93 // thread (since creating a LogInfo from log data requires heavy work). Note 94 // that we also pass the size of the log data before being compressed. This 95 // is simply for calculating and emitting some metrics, and is otherwise 96 // unused. 97 void StoreLogInfo(std::unique_ptr<UnsentLogStore::LogInfo> log_info, 98 size_t uncompressed_log_size, 99 MetricsLog::LogType log_type, 100 MetricsLogsEventManager::CreateReason reason); 101 102 // Deletes all logs, in memory and on disk. 103 void Purge(); 104 105 // Returns the signing key that should be used to create a signature for a 106 // log of the given |log_type|. We don't "simply" return the signing key that 107 // was passed during the construction of this object, because although 108 // |initial_log_queue_| and |ongoing_log_queue_| are also created with the 109 // that same signing key, |alternate_ongoing_log_queue_| is provided 110 // externally (see |SetAlternateOngoingLogStore()|), which means it could 111 // theoretically be created with a different signing key (although unlikely). 112 const std::string& GetSigningKeyForLogType(MetricsLog::LogType log_type); 113 114 // Binds an alternate log store to be managed by |this|. All ongoing logs 115 // after this call will be written to |log_store| until it is unset. Only one 116 // alternate log store can be bound at a time. Returns true if log store is 117 // bound successfully. 118 // 119 // If an alternate log store is already bound, this function will not bind 120 // |log_store| and return false. 121 // 122 // This should be called after |LoadPersistedUnsentLogs()| and after 123 // initialization. 124 void SetAlternateOngoingLogStore(std::unique_ptr<UnsentLogStore> log_store); 125 126 // Unsets the alternate log store by flushing all existing logs to persistent 127 // storage before destructing the alternate log store. 128 // 129 // If no alternate log store is bound, then this function no-ops. 130 void UnsetAlternateOngoingLogStore(); 131 132 // LogStore: 133 bool has_unsent_logs() const override; 134 bool has_staged_log() const override; 135 const std::string& staged_log() const override; 136 const std::string& staged_log_hash() const override; 137 const std::string& staged_log_signature() const override; 138 absl::optional<uint64_t> staged_log_user_id() const override; 139 void StageNextLog() override; 140 void DiscardStagedLog(base::StringPiece reason = "") override; 141 void MarkStagedLogAsSent() override; 142 void TrimAndPersistUnsentLogs(bool overwrite_in_memory_store) override; 143 void LoadPersistedUnsentLogs() override; 144 145 // Inspection methods for tests. ongoing_log_count()146 size_t ongoing_log_count() const { return ongoing_log_queue_.size(); } initial_log_count()147 size_t initial_log_count() const { return initial_log_queue_.size(); } 148 149 // Returns true if alternate log store is set. 150 bool has_alternate_ongoing_log_store() const; 151 152 private: 153 // Returns the log queue of the staged log. 154 const UnsentLogStore* get_staged_log_queue() const; 155 156 // Returns true if alternate log store is set and it has unsent logs. 157 bool alternate_ongoing_log_store_has_unsent_logs() const; 158 159 // Returns true if alternate log store is set and it has a staged log. 160 bool alternate_ongoing_log_store_has_staged_log() const; 161 162 // Returns the log store for given a |log_type|. 163 UnsentLogStore* GetLogStoreForLogType(MetricsLog::LogType log_type); 164 165 // Tracks whether unsent logs (if any) have been loaded from the serializer. 166 bool unsent_logs_loaded_; 167 168 // Event manager to notify observers of log events. 169 const raw_ptr<MetricsLogsEventManager> logs_event_manager_; 170 171 // Logs stored with the INITIAL_STABILITY_LOG type that haven't been sent yet. 172 // These logs will be staged first when staging new logs. 173 UnsentLogStore initial_log_queue_; 174 // Logs stored with the ONGOING_LOG type that haven't been sent yet. 175 UnsentLogStore ongoing_log_queue_; 176 // Alternate place to store logs stored with ONGOING_LOG type that haven't 177 // been sent yet. If initialized, all logs of type ONGOING_LOG will be stored 178 // here instead of |ongoing_log_queue_|. 179 std::unique_ptr<UnsentLogStore> alternate_ongoing_log_queue_; 180 181 SEQUENCE_CHECKER(sequence_checker_); 182 }; 183 184 } // namespace metrics 185 186 #endif // COMPONENTS_METRICS_METRICS_LOG_STORE_H_ 187