blob: fdae73f28c326bc3678569fb5a53664248f1b418 [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
5#ifndef COMPONENTS_METRICS_PERSISTED_LOGS_H_
6#define COMPONENTS_METRICS_PERSISTED_LOGS_H_
7
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
holte961fa392016-12-28 20:57:0623class PersistedLogsMetrics;
24
[email protected]7f07db62014-05-15 01:12:4525// Maintains a list of unsent logs that are written and restored from disk.
holteade6fdf2017-02-23 02:42:3926class PersistedLogs : public LogStore {
[email protected]7f07db62014-05-15 01:12:4527 public:
[email protected]9706e1b2014-06-11 16:31:2428 // Constructs a PersistedLogs 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|
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]17e820d2014-07-23 01:39:3937 // that limit will be skipped when writing to disk.
holte961fa392016-12-28 20:57:0638 PersistedLogs(std::unique_ptr<PersistedLogsMetrics> metrics,
39 PrefService* local_state,
[email protected]7f07db62014-05-15 01:12:4540 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
holteade6fdf2017-02-23 02:42:3946 // 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 Dohertye1c81482018-11-27 22:12:5751 const std::string& staged_log_signature() const override;
holteade6fdf2017-02-23 02:42:3952 void StageNextLog() override;
53 void DiscardStagedLog() override;
54 void PersistUnsentLogs() const override;
55 void LoadPersistedUnsentLogs() override;
[email protected]7f07db62014-05-15 01:12:4556
[email protected]9706e1b2014-06-11 16:31:2457 // Adds a log to the list.
58 void StoreLog(const std::string& log_data);
[email protected]7f07db62014-05-15 01:12:4559
holte7b74c622017-01-23 23:13:0760 // Delete all logs, in memory and on disk.
61 void Purge();
62
gayane8b523b872016-09-30 21:43:5963 // Returns the timestamp of the element in the front of the list.
holteade6fdf2017-02-23 02:42:3964 const std::string& staged_log_timestamp() const;
gayane8b523b872016-09-30 21:43:5965
[email protected]7f07db62014-05-15 01:12:4566 // The number of elements currently stored.
67 size_t size() const { return list_.size(); }
68
[email protected]7f07db62014-05-15 01:12:4569 private:
70 // Writes the list to the ListValue.
[email protected]17e820d2014-07-23 01:39:3971 void WriteLogsToPrefList(base::ListValue* list) const;
[email protected]7f07db62014-05-15 01:12:4572
73 // Reads the list from the ListValue.
holteade6fdf2017-02-23 02:42:3974 void ReadLogsFromPrefList(const base::ListValue& list);
[email protected]7f07db62014-05-15 01:12:4575
holte961fa392016-12-28 20:57:0676 // An object for recording UMA metrics.
77 std::unique_ptr<PersistedLogsMetrics> metrics_;
78
[email protected]7f07db62014-05-15 01:12:4579 // 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]9706e1b2014-06-11 16:31:2484 // The name of the preference to serialize logs to/from.
[email protected]7f07db62014-05-15 01:12:4585 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
gayane8b523b872016-09-30 21:43:5996 struct LogInfo {
Jesse Dohertye1c81482018-11-27 22:12:5797 LogInfo();
98 LogInfo(const LogInfo& other);
99 ~LogInfo();
100
gayane8b523b872016-09-30 21:43:59101 // Initializes the members based on uncompressed |log_data| and
102 // |log_timestamp|.
holte961fa392016-12-28 20:57:06103 // |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]9706e1b2014-06-11 16:31:24107
[email protected]9706e1b2014-06-11 16:31:24108 // Compressed log data - a serialized protobuf that's been gzipped.
109 std::string compressed_log_data;
110
Jesse Dohertye1c81482018-11-27 22:12:57111 // The SHA1 hash of the log. Computed in Init and stored to catch errors
112 // from memory corruption.
[email protected]9706e1b2014-06-11 16:31:24113 std::string hash;
gayane8b523b872016-09-30 21:43:59114
Jesse Dohertye1c81482018-11-27 22:12:57115 // 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
gayane8b523b872016-09-30 21:43:59120 // The timestamp of when the log was created as a time_t value.
121 std::string timestamp;
[email protected]7f07db62014-05-15 01:12:45122 };
123 // A list of all of the stored logs, stored with SHA1 hashes to check for
124 // corruption while they are stored in memory.
gayane8b523b872016-09-30 21:43:59125 std::vector<LogInfo> list_;
[email protected]7f07db62014-05-15 01:12:45126
[email protected]faa8d222014-07-25 21:30:26127 // 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]7f07db62014-05-15 01:12:45130
131 DISALLOW_COPY_AND_ASSIGN(PersistedLogs);
132};
133
134} // namespace metrics
135
136#endif // COMPONENTS_METRICS_PERSISTED_LOGS_H_