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 <optional> 10 #include <string> 11 12 #include "base/metrics/histogram_base.h" 13 #include "base/sequence_checker.h" 14 #include "base/strings/string_piece.h" 15 #include "components/metrics/log_store.h" 16 #include "components/metrics/metrics_log.h" 17 #include "components/metrics/metrics_logs_event_manager.h" 18 #include "components/metrics/unsent_log_store.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 struct StorageLimits { 43 // Log store limits for |initial_log_queue_|. See 44 // comments at //components/metrics/unsent_log_store.h for more details. 45 UnsentLogStore::UnsentLogStoreLimits initial_log_queue_limits; 46 47 // Log store limits for |ongoing_log_queue_|.See 48 // comments at //components/metrics/unsent_log_store.h for more details. 49 UnsentLogStore::UnsentLogStoreLimits ongoing_log_queue_limits; 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 // |logs_event_manager| is used to notify observers of log events. Can be set 57 // to null if observing the events is not necessary. 58 MetricsLogStore(PrefService* local_state, 59 StorageLimits storage_limits, 60 const std::string& signing_key, 61 MetricsLogsEventManager* logs_event_manager); 62 63 MetricsLogStore(const MetricsLogStore&) = delete; 64 MetricsLogStore& operator=(const MetricsLogStore&) = delete; 65 66 ~MetricsLogStore() override; 67 68 // Registers local state prefs used by this class. 69 static void RegisterPrefs(PrefRegistrySimple* registry); 70 71 // Saves |log_data| as the given |log_type|. Before being stored, the data 72 // will be compressed, and a hash and signature will be computed. 73 // TODO(crbug/1052796): Remove this function, and use StoreLogInfo() 74 // everywhere instead. 75 void StoreLog(const std::string& log_data, 76 MetricsLog::LogType log_type, 77 const LogMetadata& log_metadata, 78 MetricsLogsEventManager::CreateReason reason); 79 80 // Saves a log, represented by a LogInfo object, as the given |log_type|. This 81 // is useful if the LogInfo instance needs to be created outside the main 82 // thread (since creating a LogInfo from log data requires heavy work). Note 83 // that we also pass the size of the log data before being compressed. This 84 // is simply for calculating and emitting some metrics, and is otherwise 85 // unused. 86 void StoreLogInfo(std::unique_ptr<UnsentLogStore::LogInfo> log_info, 87 size_t uncompressed_log_size, 88 MetricsLog::LogType log_type, 89 MetricsLogsEventManager::CreateReason reason); 90 91 // Deletes all logs, in memory and on disk. 92 void Purge(); 93 94 // Returns the signing key that should be used to create a signature for a 95 // log of the given |log_type|. We don't "simply" return the signing key that 96 // was passed during the construction of this object, because although 97 // |initial_log_queue_| and |ongoing_log_queue_| are also created with the 98 // that same signing key, |alternate_ongoing_log_queue_| is provided 99 // externally (see |SetAlternateOngoingLogStore()|), which means it could 100 // theoretically be created with a different signing key (although unlikely). 101 const std::string& GetSigningKeyForLogType(MetricsLog::LogType log_type); 102 103 // Binds an alternate log store to be managed by |this|. All ongoing logs 104 // after this call will be written to |log_store| until it is unset. Only one 105 // alternate log store can be bound at a time. Returns true if log store is 106 // bound successfully. 107 // 108 // If an alternate log store is already bound, this function will not bind 109 // |log_store| and return false. 110 // 111 // This should be called after |LoadPersistedUnsentLogs()| and after 112 // initialization. 113 void SetAlternateOngoingLogStore(std::unique_ptr<UnsentLogStore> log_store); 114 115 // Unsets the alternate log store by flushing all existing logs to persistent 116 // storage before destructing the alternate log store. 117 // 118 // If no alternate log store is bound, then this function no-ops. 119 void UnsetAlternateOngoingLogStore(); 120 121 // LogStore: 122 bool has_unsent_logs() const override; 123 bool has_staged_log() const override; 124 const std::string& staged_log() const override; 125 const std::string& staged_log_hash() const override; 126 const std::string& staged_log_signature() const override; 127 std::optional<uint64_t> staged_log_user_id() const override; 128 const LogMetadata staged_log_metadata() const override; 129 void StageNextLog() override; 130 void DiscardStagedLog(base::StringPiece reason = "") override; 131 void MarkStagedLogAsSent() override; 132 void TrimAndPersistUnsentLogs(bool overwrite_in_memory_store) override; 133 void LoadPersistedUnsentLogs() override; 134 135 // Inspection methods for tests. ongoing_log_count()136 size_t ongoing_log_count() const { return ongoing_log_queue_.size(); } initial_log_count()137 size_t initial_log_count() const { return initial_log_queue_.size(); } 138 139 // Returns true if alternate log store is set. 140 bool has_alternate_ongoing_log_store() const; 141 142 private: 143 // Returns the log queue of the staged log. 144 const UnsentLogStore* get_staged_log_queue() const; 145 146 // Returns true if alternate log store is set and it has unsent logs. 147 bool alternate_ongoing_log_store_has_unsent_logs() const; 148 149 // Returns true if alternate log store is set and it has a staged log. 150 bool alternate_ongoing_log_store_has_staged_log() const; 151 152 // Returns the log store for given a |log_type|. 153 UnsentLogStore* GetLogStoreForLogType(MetricsLog::LogType log_type); 154 155 // Tracks whether unsent logs (if any) have been loaded from the serializer. 156 bool unsent_logs_loaded_; 157 158 // Event manager to notify observers of log events. 159 const raw_ptr<MetricsLogsEventManager> logs_event_manager_; 160 161 // Logs stored with the INITIAL_STABILITY_LOG type that haven't been sent yet. 162 // These logs will be staged first when staging new logs. 163 UnsentLogStore initial_log_queue_; 164 // Logs stored with the ONGOING_LOG type that haven't been sent yet. 165 UnsentLogStore ongoing_log_queue_; 166 // Alternate place to store logs stored with ONGOING_LOG type that haven't 167 // been sent yet. If initialized, all logs of type ONGOING_LOG will be stored 168 // here instead of |ongoing_log_queue_|. 169 std::unique_ptr<UnsentLogStore> alternate_ongoing_log_queue_; 170 171 SEQUENCE_CHECKER(sequence_checker_); 172 }; 173 174 } // namespace metrics 175 176 #endif // COMPONENTS_METRICS_METRICS_LOG_STORE_H_ 177