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