[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 1 | // Copyright 2014 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 | |
Yue Ru Sun | 227e27bb | 2019-05-10 23:53:05 | [diff] [blame] | 5 | #ifndef COMPONENTS_METRICS_UNSENT_LOG_STORE_H_ |
| 6 | #define COMPONENTS_METRICS_UNSENT_LOG_STORE_H_ |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 7 | |
avi | 2606292 | 2015-12-26 00:14:18 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | |
holte | 961fa39 | 2016-12-28 20:57:06 | [diff] [blame] | 10 | #include <memory> |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 11 | #include <string> |
| 12 | #include <vector> |
| 13 | |
Tao Bai | afdf8dfd40 | 2020-06-06 03:38:01 | [diff] [blame] | 14 | #include "base/gtest_prod_util.h" |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 15 | #include "base/logging.h" |
avi | 2606292 | 2015-12-26 00:14:18 | [diff] [blame] | 16 | #include "base/macros.h" |
Tao Bai | afdf8dfd40 | 2020-06-06 03:38:01 | [diff] [blame] | 17 | #include "base/metrics/histogram_base.h" |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 18 | #include "base/values.h" |
holte | ade6fdf | 2017-02-23 02:42:39 | [diff] [blame] | 19 | #include "components/metrics/log_store.h" |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame^] | 20 | #include "third_party/abseil-cpp/absl/types/optional.h" |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 21 | |
| 22 | class PrefService; |
| 23 | |
| 24 | namespace metrics { |
| 25 | |
Yue Ru Sun | 227e27bb | 2019-05-10 23:53:05 | [diff] [blame] | 26 | class UnsentLogStoreMetrics; |
holte | 961fa39 | 2016-12-28 20:57:06 | [diff] [blame] | 27 | |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 28 | // Maintains a list of unsent logs that are written and restored from disk. |
Yue Ru Sun | 227e27bb | 2019-05-10 23:53:05 | [diff] [blame] | 29 | class UnsentLogStore : public LogStore { |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 30 | public: |
Yue Ru Sun | 227e27bb | 2019-05-10 23:53:05 | [diff] [blame] | 31 | // Constructs an UnsentLogStore that stores data in |local_state| under the |
Tao Bai | afdf8dfd40 | 2020-06-06 03:38:01 | [diff] [blame] | 32 | // preference |log_data_pref_name|. |
[email protected] | 9706e1b | 2014-06-11 16:31:24 | [diff] [blame] | 33 | // Calling code is responsible for ensuring that the lifetime of |local_state| |
Yue Ru Sun | 227e27bb | 2019-05-10 23:53:05 | [diff] [blame] | 34 | // is longer than the lifetime of UnsentLogStore. |
[email protected] | 9706e1b | 2014-06-11 16:31:24 | [diff] [blame] | 35 | // |
Tao Bai | afdf8dfd40 | 2020-06-06 03:38:01 | [diff] [blame] | 36 | // The optional |metadata_pref_name| is the preference that is used to store |
| 37 | // the unsent logs info while the unset logs are persisted. That info will be |
| 38 | // recorded as UMA metrics in next browser startup. |
| 39 | // |
[email protected] | 9706e1b | 2014-06-11 16:31:24 | [diff] [blame] | 40 | // When saving logs to disk, stores either the first |min_log_count| logs, or |
| 41 | // at least |min_log_bytes| bytes of logs, whichever is greater. |
| 42 | // |
| 43 | // If the optional |max_log_size| parameter is non-zero, all logs larger than |
[email protected] | 17e820d | 2014-07-23 01:39:39 | [diff] [blame] | 44 | // that limit will be skipped when writing to disk. |
Jesse Doherty | bf02016 | 2018-12-11 15:40:01 | [diff] [blame] | 45 | // |
| 46 | // |signing_key| is used to produce an HMAC-SHA256 signature of the logged |
| 47 | // data, which will be uploaded with the log and used to validate data |
| 48 | // integrity. |
Yue Ru Sun | 227e27bb | 2019-05-10 23:53:05 | [diff] [blame] | 49 | UnsentLogStore(std::unique_ptr<UnsentLogStoreMetrics> metrics, |
Tao Bai | afdf8dfd40 | 2020-06-06 03:38:01 | [diff] [blame] | 50 | PrefService* local_state, |
| 51 | const char* log_data_pref_name, |
| 52 | const char* metadata_pref_name, |
| 53 | size_t min_log_count, |
| 54 | size_t min_log_bytes, |
| 55 | size_t max_log_size, |
| 56 | const std::string& signing_key); |
Yue Ru Sun | 227e27bb | 2019-05-10 23:53:05 | [diff] [blame] | 57 | ~UnsentLogStore(); |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 58 | |
holte | ade6fdf | 2017-02-23 02:42:39 | [diff] [blame] | 59 | // LogStore: |
| 60 | bool has_unsent_logs() const override; |
| 61 | bool has_staged_log() const override; |
| 62 | const std::string& staged_log() const override; |
| 63 | const std::string& staged_log_hash() const override; |
Jesse Doherty | e1c8148 | 2018-11-27 22:12:57 | [diff] [blame] | 64 | const std::string& staged_log_signature() const override; |
holte | ade6fdf | 2017-02-23 02:42:39 | [diff] [blame] | 65 | void StageNextLog() override; |
| 66 | void DiscardStagedLog() override; |
Tao Bai | afdf8dfd40 | 2020-06-06 03:38:01 | [diff] [blame] | 67 | void MarkStagedLogAsSent() override; |
Robert Kaplow | 96ecc0a | 2020-07-16 15:29:09 | [diff] [blame] | 68 | void TrimAndPersistUnsentLogs() override; |
holte | ade6fdf | 2017-02-23 02:42:39 | [diff] [blame] | 69 | void LoadPersistedUnsentLogs() override; |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 70 | |
Tao Bai | afdf8dfd40 | 2020-06-06 03:38:01 | [diff] [blame] | 71 | // Adds a UMA log to the list, |samples_count| is the total number of samples |
| 72 | // in the log (if available). |
| 73 | void StoreLog(const std::string& log_data, |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame^] | 74 | absl::optional<base::HistogramBase::Count> samples_count); |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 75 | |
Yue Ru Sun | 39255c61 | 2019-10-21 16:31:59 | [diff] [blame] | 76 | // Gets log data at the given index in the list. |
| 77 | const std::string& GetLogAtIndex(size_t index); |
| 78 | |
| 79 | // Replaces the compressed log at |index| in the store with given log data |
| 80 | // reusing the same timestamp from the original log, and returns old log data. |
Tao Bai | afdf8dfd40 | 2020-06-06 03:38:01 | [diff] [blame] | 81 | std::string ReplaceLogAtIndex( |
| 82 | size_t index, |
| 83 | const std::string& new_log_data, |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame^] | 84 | absl::optional<base::HistogramBase::Count> samples_count); |
Yue Ru Sun | 39255c61 | 2019-10-21 16:31:59 | [diff] [blame] | 85 | |
| 86 | // Deletes all logs, in memory and on disk. |
holte | 7b74c62 | 2017-01-23 23:13:07 | [diff] [blame] | 87 | void Purge(); |
| 88 | |
gayane | 8b523b87 | 2016-09-30 21:43:59 | [diff] [blame] | 89 | // Returns the timestamp of the element in the front of the list. |
holte | ade6fdf | 2017-02-23 02:42:39 | [diff] [blame] | 90 | const std::string& staged_log_timestamp() const; |
gayane | 8b523b87 | 2016-09-30 21:43:59 | [diff] [blame] | 91 | |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 92 | // The number of elements currently stored. |
| 93 | size_t size() const { return list_.size(); } |
| 94 | |
Weilun Shi | f6c84fe | 2021-04-07 20:04:45 | [diff] [blame] | 95 | // Computes the HMAC for |log_data| using the |signing_key| and returns a bool |
| 96 | // indicating whether the signing succeeded. The returned HMAC is written to |
| 97 | // the |signature|. |
| 98 | static bool ComputeHMACForLog(const std::string& log_data, |
| 99 | const std::string& signing_key, |
| 100 | std::string* signature); |
| 101 | |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 102 | private: |
Tao Bai | afdf8dfd40 | 2020-06-06 03:38:01 | [diff] [blame] | 103 | FRIEND_TEST_ALL_PREFIXES(UnsentLogStoreTest, UnsentLogMetadataMetrics); |
| 104 | |
Robert Kaplow | 96ecc0a | 2020-07-16 15:29:09 | [diff] [blame] | 105 | // Keep the most recent logs which are smaller than |max_log_size_|. |
| 106 | // We keep at least |min_log_bytes_| and |min_log_count_| of logs before |
| 107 | // discarding older logs. |
| 108 | void TrimLogs(); |
| 109 | |
| 110 | // Writes the list of logs to |list|. |
[email protected] | 17e820d | 2014-07-23 01:39:39 | [diff] [blame] | 111 | void WriteLogsToPrefList(base::ListValue* list) const; |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 112 | |
Robert Kaplow | 96ecc0a | 2020-07-16 15:29:09 | [diff] [blame] | 113 | // Reads the list of logs from |list|. |
holte | ade6fdf | 2017-02-23 02:42:39 | [diff] [blame] | 114 | void ReadLogsFromPrefList(const base::ListValue& list); |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 115 | |
Tao Bai | afdf8dfd40 | 2020-06-06 03:38:01 | [diff] [blame] | 116 | // Writes the unsent log info to the |metadata_pref_name_| preference. |
| 117 | void WriteToMetricsPref(base::HistogramBase::Count unsent_samples_count, |
| 118 | base::HistogramBase::Count sent_samples_count, |
| 119 | size_t persisted_size) const; |
| 120 | |
| 121 | // Records the info in |metadata_pref_name_| as UMA metrics. |
| 122 | void RecordMetaDataMertics(); |
| 123 | |
holte | 961fa39 | 2016-12-28 20:57:06 | [diff] [blame] | 124 | // An object for recording UMA metrics. |
Yue Ru Sun | 227e27bb | 2019-05-10 23:53:05 | [diff] [blame] | 125 | std::unique_ptr<UnsentLogStoreMetrics> metrics_; |
holte | 961fa39 | 2016-12-28 20:57:06 | [diff] [blame] | 126 | |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 127 | // A weak pointer to the PrefService object to read and write the preference |
| 128 | // from. Calling code should ensure this object continues to exist for the |
Yue Ru Sun | 227e27bb | 2019-05-10 23:53:05 | [diff] [blame] | 129 | // lifetime of the UnsentLogStore object. |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 130 | PrefService* local_state_; |
| 131 | |
[email protected] | 9706e1b | 2014-06-11 16:31:24 | [diff] [blame] | 132 | // The name of the preference to serialize logs to/from. |
Tao Bai | afdf8dfd40 | 2020-06-06 03:38:01 | [diff] [blame] | 133 | const char* log_data_pref_name_; |
| 134 | |
| 135 | // The name of the preference to store the unsent logs info, it could be |
| 136 | // nullptr if the metadata isn't desired. |
| 137 | const char* metadata_pref_name_; |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 138 | |
| 139 | // We will keep at least this |min_log_count_| logs or |min_log_bytes_| bytes |
Robert Kaplow | 96ecc0a | 2020-07-16 15:29:09 | [diff] [blame] | 140 | // of logs, whichever is greater, when trimming logs. These apply after |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 141 | // skipping logs greater than |max_log_size_|. |
| 142 | const size_t min_log_count_; |
| 143 | const size_t min_log_bytes_; |
| 144 | |
| 145 | // Logs greater than this size will not be written to disk. |
| 146 | const size_t max_log_size_; |
| 147 | |
Jesse Doherty | bf02016 | 2018-12-11 15:40:01 | [diff] [blame] | 148 | // Used to create a signature of log data, in order to verify reported data is |
| 149 | // authentic. |
| 150 | const std::string signing_key_; |
| 151 | |
gayane | 8b523b87 | 2016-09-30 21:43:59 | [diff] [blame] | 152 | struct LogInfo { |
Jesse Doherty | e1c8148 | 2018-11-27 22:12:57 | [diff] [blame] | 153 | LogInfo(); |
| 154 | LogInfo(const LogInfo& other); |
| 155 | ~LogInfo(); |
| 156 | |
Jesse Doherty | bf02016 | 2018-12-11 15:40:01 | [diff] [blame] | 157 | // Initializes the members based on uncompressed |log_data|, |
| 158 | // |log_timestamp|, and |signing_key|. |log_data| is the uncompressed |
| 159 | // serialized log protobuf. A hash and a signature are computed from |
| 160 | // |log_data|. The signature is produced using |signing_key|. |log_data| |
Yue Ru Sun | 227e27bb | 2019-05-10 23:53:05 | [diff] [blame] | 161 | // will be compressed and stored in |compressed_log_data|. |log_timestamp| |
Jesse Doherty | bf02016 | 2018-12-11 15:40:01 | [diff] [blame] | 162 | // is stored as is. |
holte | 961fa39 | 2016-12-28 20:57:06 | [diff] [blame] | 163 | // |metrics| is the parent's metrics_ object, and should not be held. |
Yue Ru Sun | 227e27bb | 2019-05-10 23:53:05 | [diff] [blame] | 164 | void Init(UnsentLogStoreMetrics* metrics, |
holte | 961fa39 | 2016-12-28 20:57:06 | [diff] [blame] | 165 | const std::string& log_data, |
Jesse Doherty | bf02016 | 2018-12-11 15:40:01 | [diff] [blame] | 166 | const std::string& log_timestamp, |
Tao Bai | afdf8dfd40 | 2020-06-06 03:38:01 | [diff] [blame] | 167 | const std::string& signing_key, |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame^] | 168 | absl::optional<base::HistogramBase::Count> samples_count); |
[email protected] | 9706e1b | 2014-06-11 16:31:24 | [diff] [blame] | 169 | |
[email protected] | 9706e1b | 2014-06-11 16:31:24 | [diff] [blame] | 170 | // Compressed log data - a serialized protobuf that's been gzipped. |
| 171 | std::string compressed_log_data; |
| 172 | |
Jesse Doherty | e1c8148 | 2018-11-27 22:12:57 | [diff] [blame] | 173 | // The SHA1 hash of the log. Computed in Init and stored to catch errors |
| 174 | // from memory corruption. |
[email protected] | 9706e1b | 2014-06-11 16:31:24 | [diff] [blame] | 175 | std::string hash; |
gayane | 8b523b87 | 2016-09-30 21:43:59 | [diff] [blame] | 176 | |
Jesse Doherty | e1c8148 | 2018-11-27 22:12:57 | [diff] [blame] | 177 | // The HMAC-SHA256 signature of the log, used to validate the log came from |
| 178 | // Chrome. It's computed in Init and stored, instead of computed on demand, |
| 179 | // to catch errors from memory corruption. |
| 180 | std::string signature; |
| 181 | |
gayane | 8b523b87 | 2016-09-30 21:43:59 | [diff] [blame] | 182 | // The timestamp of when the log was created as a time_t value. |
| 183 | std::string timestamp; |
Tao Bai | afdf8dfd40 | 2020-06-06 03:38:01 | [diff] [blame] | 184 | |
| 185 | // The total number of samples in this log if applicable. |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame^] | 186 | absl::optional<base::HistogramBase::Count> samples_count; |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 187 | }; |
| 188 | // A list of all of the stored logs, stored with SHA1 hashes to check for |
| 189 | // corruption while they are stored in memory. |
Robert Kaplow | 96ecc0a | 2020-07-16 15:29:09 | [diff] [blame] | 190 | std::vector<std::unique_ptr<LogInfo>> list_; |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 191 | |
[email protected] | faa8d22 | 2014-07-25 21:30:26 | [diff] [blame] | 192 | // The index and type of the log staged for upload. If nothing has been |
| 193 | // staged, the index will be -1. |
| 194 | int staged_log_index_; |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 195 | |
Tao Bai | afdf8dfd40 | 2020-06-06 03:38:01 | [diff] [blame] | 196 | // The total number of samples that have been sent from this LogStore. |
| 197 | base::HistogramBase::Count total_samples_sent_ = 0; |
| 198 | |
Yue Ru Sun | 227e27bb | 2019-05-10 23:53:05 | [diff] [blame] | 199 | DISALLOW_COPY_AND_ASSIGN(UnsentLogStore); |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 200 | }; |
| 201 | |
| 202 | } // namespace metrics |
| 203 | |
Yue Ru Sun | 227e27bb | 2019-05-10 23:53:05 | [diff] [blame] | 204 | #endif // COMPONENTS_METRICS_UNSENT_LOG_STORE_H_ |