blob: 0be1565240618627891cf43a85089f8a12033425 [file] [log] [blame]
[email protected]7f07db62014-05-15 01:12:451// 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 Sun227e27bb2019-05-10 23:53:055#ifndef COMPONENTS_METRICS_UNSENT_LOG_STORE_H_
6#define COMPONENTS_METRICS_UNSENT_LOG_STORE_H_
[email protected]7f07db62014-05-15 01:12:457
avi26062922015-12-26 00:14:188#include <stddef.h>
9
holte961fa392016-12-28 20:57:0610#include <memory>
[email protected]7f07db62014-05-15 01:12:4511#include <string>
12#include <vector>
13
[email protected]7f07db62014-05-15 01:12:4514#include "base/logging.h"
avi26062922015-12-26 00:14:1815#include "base/macros.h"
[email protected]7f07db62014-05-15 01:12:4516#include "base/values.h"
holteade6fdf2017-02-23 02:42:3917#include "components/metrics/log_store.h"
[email protected]7f07db62014-05-15 01:12:4518
19class PrefService;
20
21namespace metrics {
22
Yue Ru Sun227e27bb2019-05-10 23:53:0523class UnsentLogStoreMetrics;
holte961fa392016-12-28 20:57:0624
[email protected]7f07db62014-05-15 01:12:4525// Maintains a list of unsent logs that are written and restored from disk.
Yue Ru Sun227e27bb2019-05-10 23:53:0526class UnsentLogStore : public LogStore {
[email protected]7f07db62014-05-15 01:12:4527 public:
Yue Ru Sun227e27bb2019-05-10 23:53:0528 // Constructs an UnsentLogStore that stores data in |local_state| under the
[email protected]f61623e2014-08-19 16:25:5729 // preference |pref_name|.
[email protected]9706e1b2014-06-11 16:31:2430 // Calling code is responsible for ensuring that the lifetime of |local_state|
Yue Ru Sun227e27bb2019-05-10 23:53:0531 // is longer than the lifetime of UnsentLogStore.
[email protected]9706e1b2014-06-11 16:31:2432 //
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]17e820d2014-07-23 01:39:3937 // that limit will be skipped when writing to disk.
Jesse Dohertybf020162018-12-11 15:40:0138 //
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 Sun227e27bb2019-05-10 23:53:0542 UnsentLogStore(std::unique_ptr<UnsentLogStoreMetrics> metrics,
holte961fa392016-12-28 20:57:0643 PrefService* local_state,
[email protected]7f07db62014-05-15 01:12:4544 const char* pref_name,
45 size_t min_log_count,
46 size_t min_log_bytes,
Jesse Dohertybf020162018-12-11 15:40:0147 size_t max_log_size,
48 const std::string& signing_key);
Yue Ru Sun227e27bb2019-05-10 23:53:0549 ~UnsentLogStore();
[email protected]7f07db62014-05-15 01:12:4550
holteade6fdf2017-02-23 02:42:3951 // 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 Dohertye1c81482018-11-27 22:12:5756 const std::string& staged_log_signature() const override;
holteade6fdf2017-02-23 02:42:3957 void StageNextLog() override;
58 void DiscardStagedLog() override;
59 void PersistUnsentLogs() const override;
60 void LoadPersistedUnsentLogs() override;
[email protected]7f07db62014-05-15 01:12:4561
[email protected]9706e1b2014-06-11 16:31:2462 // Adds a log to the list.
63 void StoreLog(const std::string& log_data);
[email protected]7f07db62014-05-15 01:12:4564
holte7b74c622017-01-23 23:13:0765 // Delete all logs, in memory and on disk.
66 void Purge();
67
gayane8b523b872016-09-30 21:43:5968 // Returns the timestamp of the element in the front of the list.
holteade6fdf2017-02-23 02:42:3969 const std::string& staged_log_timestamp() const;
gayane8b523b872016-09-30 21:43:5970
[email protected]7f07db62014-05-15 01:12:4571 // The number of elements currently stored.
72 size_t size() const { return list_.size(); }
73
[email protected]7f07db62014-05-15 01:12:4574 private:
75 // Writes the list to the ListValue.
[email protected]17e820d2014-07-23 01:39:3976 void WriteLogsToPrefList(base::ListValue* list) const;
[email protected]7f07db62014-05-15 01:12:4577
78 // Reads the list from the ListValue.
holteade6fdf2017-02-23 02:42:3979 void ReadLogsFromPrefList(const base::ListValue& list);
[email protected]7f07db62014-05-15 01:12:4580
holte961fa392016-12-28 20:57:0681 // An object for recording UMA metrics.
Yue Ru Sun227e27bb2019-05-10 23:53:0582 std::unique_ptr<UnsentLogStoreMetrics> metrics_;
holte961fa392016-12-28 20:57:0683
[email protected]7f07db62014-05-15 01:12:4584 // 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 Sun227e27bb2019-05-10 23:53:0586 // lifetime of the UnsentLogStore object.
[email protected]7f07db62014-05-15 01:12:4587 PrefService* local_state_;
88
[email protected]9706e1b2014-06-11 16:31:2489 // The name of the preference to serialize logs to/from.
[email protected]7f07db62014-05-15 01:12:4590 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 Dohertybf020162018-12-11 15:40:01101 // Used to create a signature of log data, in order to verify reported data is
102 // authentic.
103 const std::string signing_key_;
104
gayane8b523b872016-09-30 21:43:59105 struct LogInfo {
Jesse Dohertye1c81482018-11-27 22:12:57106 LogInfo();
107 LogInfo(const LogInfo& other);
108 ~LogInfo();
109
Jesse Dohertybf020162018-12-11 15:40:01110 // 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 Sun227e27bb2019-05-10 23:53:05114 // will be compressed and stored in |compressed_log_data|. |log_timestamp|
Jesse Dohertybf020162018-12-11 15:40:01115 // is stored as is.
holte961fa392016-12-28 20:57:06116 // |metrics| is the parent's metrics_ object, and should not be held.
Yue Ru Sun227e27bb2019-05-10 23:53:05117 void Init(UnsentLogStoreMetrics* metrics,
holte961fa392016-12-28 20:57:06118 const std::string& log_data,
Jesse Dohertybf020162018-12-11 15:40:01119 const std::string& log_timestamp,
120 const std::string& signing_key);
[email protected]9706e1b2014-06-11 16:31:24121
[email protected]9706e1b2014-06-11 16:31:24122 // Compressed log data - a serialized protobuf that's been gzipped.
123 std::string compressed_log_data;
124
Jesse Dohertye1c81482018-11-27 22:12:57125 // The SHA1 hash of the log. Computed in Init and stored to catch errors
126 // from memory corruption.
[email protected]9706e1b2014-06-11 16:31:24127 std::string hash;
gayane8b523b872016-09-30 21:43:59128
Jesse Dohertye1c81482018-11-27 22:12:57129 // 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
gayane8b523b872016-09-30 21:43:59134 // The timestamp of when the log was created as a time_t value.
135 std::string timestamp;
[email protected]7f07db62014-05-15 01:12:45136 };
137 // A list of all of the stored logs, stored with SHA1 hashes to check for
138 // corruption while they are stored in memory.
gayane8b523b872016-09-30 21:43:59139 std::vector<LogInfo> list_;
[email protected]7f07db62014-05-15 01:12:45140
[email protected]faa8d222014-07-25 21:30:26141 // 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]7f07db62014-05-15 01:12:45144
Yue Ru Sun227e27bb2019-05-10 23:53:05145 DISALLOW_COPY_AND_ASSIGN(UnsentLogStore);
[email protected]7f07db62014-05-15 01:12:45146};
147
148} // namespace metrics
149
Yue Ru Sun227e27bb2019-05-10 23:53:05150#endif // COMPONENTS_METRICS_UNSENT_LOG_STORE_H_