[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 | |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 14 | #include "base/logging.h" |
avi | 2606292 | 2015-12-26 00:14:18 | [diff] [blame] | 15 | #include "base/macros.h" |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 16 | #include "base/values.h" |
holte | ade6fdf | 2017-02-23 02:42:39 | [diff] [blame] | 17 | #include "components/metrics/log_store.h" |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 18 | |
| 19 | class PrefService; |
| 20 | |
| 21 | namespace metrics { |
| 22 | |
Yue Ru Sun | 227e27bb | 2019-05-10 23:53:05 | [diff] [blame^] | 23 | class UnsentLogStoreMetrics; |
holte | 961fa39 | 2016-12-28 20:57:06 | [diff] [blame] | 24 | |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 25 | // 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^] | 26 | class UnsentLogStore : public LogStore { |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 27 | public: |
Yue Ru Sun | 227e27bb | 2019-05-10 23:53:05 | [diff] [blame^] | 28 | // Constructs an UnsentLogStore that stores data in |local_state| under the |
[email protected] | f61623e | 2014-08-19 16:25:57 | [diff] [blame] | 29 | // preference |pref_name|. |
[email protected] | 9706e1b | 2014-06-11 16:31:24 | [diff] [blame] | 30 | // Calling code is responsible for ensuring that the lifetime of |local_state| |
Yue Ru Sun | 227e27bb | 2019-05-10 23:53:05 | [diff] [blame^] | 31 | // is longer than the lifetime of UnsentLogStore. |
[email protected] | 9706e1b | 2014-06-11 16:31:24 | [diff] [blame] | 32 | // |
| 33 | // When saving logs to disk, stores either the first |min_log_count| logs, or |
| 34 | // at least |min_log_bytes| bytes of logs, whichever is greater. |
| 35 | // |
| 36 | // 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] | 37 | // that limit will be skipped when writing to disk. |
Jesse Doherty | bf02016 | 2018-12-11 15:40:01 | [diff] [blame] | 38 | // |
| 39 | // |signing_key| is used to produce an HMAC-SHA256 signature of the logged |
| 40 | // data, which will be uploaded with the log and used to validate data |
| 41 | // integrity. |
Yue Ru Sun | 227e27bb | 2019-05-10 23:53:05 | [diff] [blame^] | 42 | UnsentLogStore(std::unique_ptr<UnsentLogStoreMetrics> metrics, |
holte | 961fa39 | 2016-12-28 20:57:06 | [diff] [blame] | 43 | PrefService* local_state, |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 44 | const char* pref_name, |
| 45 | size_t min_log_count, |
| 46 | size_t min_log_bytes, |
Jesse Doherty | bf02016 | 2018-12-11 15:40:01 | [diff] [blame] | 47 | size_t max_log_size, |
| 48 | const std::string& signing_key); |
Yue Ru Sun | 227e27bb | 2019-05-10 23:53:05 | [diff] [blame^] | 49 | ~UnsentLogStore(); |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 50 | |
holte | ade6fdf | 2017-02-23 02:42:39 | [diff] [blame] | 51 | // LogStore: |
| 52 | bool has_unsent_logs() const override; |
| 53 | bool has_staged_log() const override; |
| 54 | const std::string& staged_log() const override; |
| 55 | const std::string& staged_log_hash() const override; |
Jesse Doherty | e1c8148 | 2018-11-27 22:12:57 | [diff] [blame] | 56 | const std::string& staged_log_signature() const override; |
holte | ade6fdf | 2017-02-23 02:42:39 | [diff] [blame] | 57 | void StageNextLog() override; |
| 58 | void DiscardStagedLog() override; |
| 59 | void PersistUnsentLogs() const override; |
| 60 | void LoadPersistedUnsentLogs() override; |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 61 | |
[email protected] | 9706e1b | 2014-06-11 16:31:24 | [diff] [blame] | 62 | // Adds a log to the list. |
| 63 | void StoreLog(const std::string& log_data); |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 64 | |
holte | 7b74c62 | 2017-01-23 23:13:07 | [diff] [blame] | 65 | // Delete all logs, in memory and on disk. |
| 66 | void Purge(); |
| 67 | |
gayane | 8b523b87 | 2016-09-30 21:43:59 | [diff] [blame] | 68 | // Returns the timestamp of the element in the front of the list. |
holte | ade6fdf | 2017-02-23 02:42:39 | [diff] [blame] | 69 | const std::string& staged_log_timestamp() const; |
gayane | 8b523b87 | 2016-09-30 21:43:59 | [diff] [blame] | 70 | |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 71 | // The number of elements currently stored. |
| 72 | size_t size() const { return list_.size(); } |
| 73 | |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 74 | private: |
| 75 | // Writes the list to the ListValue. |
[email protected] | 17e820d | 2014-07-23 01:39:39 | [diff] [blame] | 76 | void WriteLogsToPrefList(base::ListValue* list) const; |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 77 | |
| 78 | // Reads the list from the ListValue. |
holte | ade6fdf | 2017-02-23 02:42:39 | [diff] [blame] | 79 | void ReadLogsFromPrefList(const base::ListValue& list); |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 80 | |
holte | 961fa39 | 2016-12-28 20:57:06 | [diff] [blame] | 81 | // An object for recording UMA metrics. |
Yue Ru Sun | 227e27bb | 2019-05-10 23:53:05 | [diff] [blame^] | 82 | std::unique_ptr<UnsentLogStoreMetrics> metrics_; |
holte | 961fa39 | 2016-12-28 20:57:06 | [diff] [blame] | 83 | |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 84 | // A weak pointer to the PrefService object to read and write the preference |
| 85 | // from. Calling code should ensure this object continues to exist for the |
Yue Ru Sun | 227e27bb | 2019-05-10 23:53:05 | [diff] [blame^] | 86 | // lifetime of the UnsentLogStore object. |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 87 | PrefService* local_state_; |
| 88 | |
[email protected] | 9706e1b | 2014-06-11 16:31:24 | [diff] [blame] | 89 | // The name of the preference to serialize logs to/from. |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 90 | const char* pref_name_; |
| 91 | |
| 92 | // We will keep at least this |min_log_count_| logs or |min_log_bytes_| bytes |
| 93 | // of logs, whichever is greater, when writing to disk. These apply after |
| 94 | // skipping logs greater than |max_log_size_|. |
| 95 | const size_t min_log_count_; |
| 96 | const size_t min_log_bytes_; |
| 97 | |
| 98 | // Logs greater than this size will not be written to disk. |
| 99 | const size_t max_log_size_; |
| 100 | |
Jesse Doherty | bf02016 | 2018-12-11 15:40:01 | [diff] [blame] | 101 | // Used to create a signature of log data, in order to verify reported data is |
| 102 | // authentic. |
| 103 | const std::string signing_key_; |
| 104 | |
gayane | 8b523b87 | 2016-09-30 21:43:59 | [diff] [blame] | 105 | struct LogInfo { |
Jesse Doherty | e1c8148 | 2018-11-27 22:12:57 | [diff] [blame] | 106 | LogInfo(); |
| 107 | LogInfo(const LogInfo& other); |
| 108 | ~LogInfo(); |
| 109 | |
Jesse Doherty | bf02016 | 2018-12-11 15:40:01 | [diff] [blame] | 110 | // Initializes the members based on uncompressed |log_data|, |
| 111 | // |log_timestamp|, and |signing_key|. |log_data| is the uncompressed |
| 112 | // serialized log protobuf. A hash and a signature are computed from |
| 113 | // |log_data|. The signature is produced using |signing_key|. |log_data| |
Yue Ru Sun | 227e27bb | 2019-05-10 23:53:05 | [diff] [blame^] | 114 | // will be compressed and stored in |compressed_log_data|. |log_timestamp| |
Jesse Doherty | bf02016 | 2018-12-11 15:40:01 | [diff] [blame] | 115 | // is stored as is. |
holte | 961fa39 | 2016-12-28 20:57:06 | [diff] [blame] | 116 | // |metrics| is the parent's metrics_ object, and should not be held. |
Yue Ru Sun | 227e27bb | 2019-05-10 23:53:05 | [diff] [blame^] | 117 | void Init(UnsentLogStoreMetrics* metrics, |
holte | 961fa39 | 2016-12-28 20:57:06 | [diff] [blame] | 118 | const std::string& log_data, |
Jesse Doherty | bf02016 | 2018-12-11 15:40:01 | [diff] [blame] | 119 | const std::string& log_timestamp, |
| 120 | const std::string& signing_key); |
[email protected] | 9706e1b | 2014-06-11 16:31:24 | [diff] [blame] | 121 | |
[email protected] | 9706e1b | 2014-06-11 16:31:24 | [diff] [blame] | 122 | // Compressed log data - a serialized protobuf that's been gzipped. |
| 123 | std::string compressed_log_data; |
| 124 | |
Jesse Doherty | e1c8148 | 2018-11-27 22:12:57 | [diff] [blame] | 125 | // The SHA1 hash of the log. Computed in Init and stored to catch errors |
| 126 | // from memory corruption. |
[email protected] | 9706e1b | 2014-06-11 16:31:24 | [diff] [blame] | 127 | std::string hash; |
gayane | 8b523b87 | 2016-09-30 21:43:59 | [diff] [blame] | 128 | |
Jesse Doherty | e1c8148 | 2018-11-27 22:12:57 | [diff] [blame] | 129 | // The HMAC-SHA256 signature of the log, used to validate the log came from |
| 130 | // Chrome. It's computed in Init and stored, instead of computed on demand, |
| 131 | // to catch errors from memory corruption. |
| 132 | std::string signature; |
| 133 | |
gayane | 8b523b87 | 2016-09-30 21:43:59 | [diff] [blame] | 134 | // The timestamp of when the log was created as a time_t value. |
| 135 | std::string timestamp; |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 136 | }; |
| 137 | // A list of all of the stored logs, stored with SHA1 hashes to check for |
| 138 | // corruption while they are stored in memory. |
gayane | 8b523b87 | 2016-09-30 21:43:59 | [diff] [blame] | 139 | std::vector<LogInfo> list_; |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 140 | |
[email protected] | faa8d22 | 2014-07-25 21:30:26 | [diff] [blame] | 141 | // The index and type of the log staged for upload. If nothing has been |
| 142 | // staged, the index will be -1. |
| 143 | int staged_log_index_; |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 144 | |
Yue Ru Sun | 227e27bb | 2019-05-10 23:53:05 | [diff] [blame^] | 145 | DISALLOW_COPY_AND_ASSIGN(UnsentLogStore); |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 146 | }; |
| 147 | |
| 148 | } // namespace metrics |
| 149 | |
Yue Ru Sun | 227e27bb | 2019-05-10 23:53:05 | [diff] [blame^] | 150 | #endif // COMPONENTS_METRICS_UNSENT_LOG_STORE_H_ |