[M120 Migration][VD] Enable direct rendering for TVPlus
[platform/framework/web/chromium-efl.git] / components / metrics / unsent_log_store.h
1 // Copyright 2014 The Chromium Authors
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_UNSENT_LOG_STORE_H_
6 #define COMPONENTS_METRICS_UNSENT_LOG_STORE_H_
7
8 #include <stddef.h>
9
10 #include <memory>
11 #include <string>
12 #include <vector>
13
14 #include "base/gtest_prod_util.h"
15 #include "base/logging.h"
16 #include "base/memory/raw_ptr.h"
17 #include "base/metrics/histogram_base.h"
18 #include "base/strings/string_piece.h"
19 #include "base/values.h"
20 #include "components/metrics/log_store.h"
21 #include "components/metrics/metrics_log.h"
22 #include "components/metrics/metrics_logs_event_manager.h"
23 #include "third_party/abseil-cpp/absl/types/optional.h"
24
25 class PrefService;
26
27 namespace metrics {
28
29 class UnsentLogStoreMetrics;
30
31 // Maintains a list of unsent logs that are written and restored from disk.
32 class UnsentLogStore : public LogStore {
33  public:
34   // Configurable capacities for unsent log store.
35   //
36   // When saving logs to disk, stores either the first |min_log_count| logs, or
37   // at least |min_queue_size_bytes| bytes of logs. If |this| contains more than
38   // |min_log_count| logs AND a total bytes larger than |min_queue_size_bytes|,
39   // older logs will be dropped for newer logs.
40   //
41   // Either |min_queue_size_bytes| or |min_log_count| must be greater than 0.
42   //
43   // Individual logs greater than |max_log_size_bytes| will not be written to
44   // disk. If |max_log_size_bytes| is zero, logs of any size will be written to
45   // disk.
46   struct UnsentLogStoreLimits {
47     // Minimum number of unsent logs persisted before older logs are trimmed.
48     //
49     // log_count >= |min_log_count| AND total_queue_bytes >=
50     // |min_queue_size_bytes| for logs to be dropped. See comments for
51     // UnsentLogStoreLimits for more details.
52     size_t min_log_count = 0;
53
54     // Minimum bytes that the queue can hold before older logs are trimmed.
55     //
56     // Number of logs >= |min_log_count| AND total_queue_size >=
57     // |min_queue_size_bytes| for logs to be dropped. See comments for
58     // UnsentLogStoreLimits for more details.
59     size_t min_queue_size_bytes = 0;
60
61     // Logs greater than this size will not be written to disk.
62     size_t max_log_size_bytes = 0;
63   };
64
65   // Constructs an UnsentLogStore that stores data in |local_state| under the
66   // preference |log_data_pref_name|.
67   // Calling code is responsible for ensuring that the lifetime of |local_state|
68   // is longer than the lifetime of UnsentLogStore.
69   //
70   // The optional |metadata_pref_name| is the preference that is used to store
71   // the unsent logs info while the unset logs are persisted. That info will be
72   // recorded as UMA metrics in next browser startup.
73   //
74   // |signing_key| is used to produce an HMAC-SHA256 signature of the logged
75   // data, which will be uploaded with the log and used to validate data
76   // integrity.
77   //
78   // |logs_event_manager| is used to notify observers of log events. Can be set
79   // to null if observing the events is not necessary.
80   UnsentLogStore(std::unique_ptr<UnsentLogStoreMetrics> metrics,
81                  PrefService* local_state,
82                  const char* log_data_pref_name,
83                  const char* metadata_pref_name,
84                  UnsentLogStoreLimits log_store_limits,
85                  const std::string& signing_key,
86                  MetricsLogsEventManager* logs_event_manager);
87
88   UnsentLogStore(const UnsentLogStore&) = delete;
89   UnsentLogStore& operator=(const UnsentLogStore&) = delete;
90
91   ~UnsentLogStore() override;
92
93   struct LogInfo {
94     LogInfo();
95
96     LogInfo(const LogInfo&) = delete;
97     LogInfo& operator=(const LogInfo&) = delete;
98
99     ~LogInfo();
100
101     // Initializes the members based on uncompressed |log_data|,
102     // |log_timestamp|, and |signing_key|. |log_data| is the uncompressed
103     // serialized log protobuf. A hash and a signature are computed from
104     // |log_data|. The signature is produced using |signing_key|. |log_data|
105     // will be compressed and stored in |compressed_log_data|. |log_timestamp|
106     // is stored as is. |log_metadata| is any optional metadata that will be
107     // attached to the log.
108     // TODO(crbug/1052796): Make this a ctor instead.
109     void Init(const std::string& log_data,
110               const std::string& log_timestamp,
111               const std::string& signing_key,
112               const LogMetadata& log_metadata);
113
114     // Same as above, but the |timestamp| field will be filled with the current
115     // time.
116     // TODO(crbug/1052796): Make this a ctor instead.
117     void Init(const std::string& log_data,
118               const std::string& signing_key,
119               const LogMetadata& log_metadata);
120
121     // Compressed log data - a serialized protobuf that's been gzipped.
122     std::string compressed_log_data;
123
124     // The SHA1 hash of the log. Computed in Init and stored to catch errors
125     // from memory corruption.
126     std::string hash;
127
128     // The HMAC-SHA256 signature of the log, used to validate the log came from
129     // Chrome. It's computed in Init and stored, instead of computed on demand,
130     // to catch errors from memory corruption.
131     std::string signature;
132
133     // The timestamp of when the log was created as a time_t value.
134     std::string timestamp;
135
136     // Properties of the log.
137     LogMetadata log_metadata;
138   };
139
140   // LogStore:
141   bool has_unsent_logs() const override;
142   bool has_staged_log() const override;
143   const std::string& staged_log() const override;
144   const std::string& staged_log_hash() const override;
145   const std::string& staged_log_signature() const override;
146   absl::optional<uint64_t> staged_log_user_id() const override;
147   void StageNextLog() override;
148   void DiscardStagedLog(base::StringPiece reason = "") override;
149   void MarkStagedLogAsSent() override;
150   void TrimAndPersistUnsentLogs(bool overwrite_in_memory_store) override;
151   void LoadPersistedUnsentLogs() override;
152
153   // Adds a log to the list. |log_metadata| refers to metadata associated with
154   // the log. Before being stored, the data will be compressed, and a hash and
155   // signature will be computed.
156   // TODO(crbug/1052796): Remove this function, and use StoreLogInfo()
157   // everywhere instead.
158   void StoreLog(const std::string& log_data,
159                 const LogMetadata& log_metadata,
160                 MetricsLogsEventManager::CreateReason reason);
161
162   // Adds a log to the list, represented by a LogInfo object. This is useful
163   // if the LogInfo instance needs to be created outside the main thread
164   // (since creating a LogInfo from log data requires heavy work). Note that we
165   // also pass the size of the log data before being compressed. This is simply
166   // for calculating and emitting some metrics, and is otherwise unused.
167   void StoreLogInfo(std::unique_ptr<LogInfo> log_info,
168                     size_t uncompressed_log_size,
169                     MetricsLogsEventManager::CreateReason reason);
170
171   // Gets log data at the given index in the list.
172   const std::string& GetLogAtIndex(size_t index);
173
174   // Replaces the compressed log at |index| in the store with given log data and
175   // |log_metadata| reusing the same timestamp.
176   std::string ReplaceLogAtIndex(size_t index,
177                                 const std::string& new_log_data,
178                                 const LogMetadata& log_metadata);
179
180   // Deletes all logs, in memory and on disk.
181   void Purge();
182
183   // Sets |logs_event_manager_|.
184   void SetLogsEventManager(MetricsLogsEventManager* logs_event_manager);
185
186   // Returns the timestamp of the element in the front of the list.
187   const std::string& staged_log_timestamp() const;
188
189   // The number of elements currently stored.
190   size_t size() const { return list_.size(); }
191
192   // The signing key used to compute the signature for a log.
193   const std::string& signing_key() const { return signing_key_; }
194
195   // Returns |logs_event_manager_|.
196   MetricsLogsEventManager* GetLogsEventManagerForTesting() const {
197     return logs_event_manager_;
198   }
199
200   // Computes the HMAC for |log_data| using the |signing_key| and returns a bool
201   // indicating whether the signing succeeded. The returned HMAC is written to
202   // the |signature|.
203   static bool ComputeHMACForLog(const std::string& log_data,
204                                 const std::string& signing_key,
205                                 std::string* signature);
206
207  private:
208   FRIEND_TEST_ALL_PREFIXES(UnsentLogStoreTest, UnsentLogMetadataMetrics);
209
210   // Reads the list of logs from |list|.
211   void ReadLogsFromPrefList(const base::Value::List& list);
212
213   // Writes the unsent log info to the |metadata_pref_name_| preference.
214   void WriteToMetricsPref(base::HistogramBase::Count unsent_samples_count,
215                           base::HistogramBase::Count sent_samples_count,
216                           size_t persisted_size) const;
217
218   // Records the info in |metadata_pref_name_| as UMA metrics.
219   void RecordMetaDataMetrics();
220
221   // Wrapper functions for the notify functions of |logs_event_manager_|.
222   void NotifyLogCreated(const LogInfo& info,
223                         MetricsLogsEventManager::CreateReason reason);
224   void NotifyLogsCreated(base::span<std::unique_ptr<LogInfo>> logs,
225                          MetricsLogsEventManager::CreateReason reason);
226   void NotifyLogEvent(MetricsLogsEventManager::LogEvent event,
227                       base::StringPiece log_hash,
228                       base::StringPiece message = "");
229   void NotifyLogsEvent(base::span<std::unique_ptr<LogInfo>> logs,
230                        MetricsLogsEventManager::LogEvent event,
231                        base::StringPiece message = "");
232
233   // An object for recording UMA metrics.
234   std::unique_ptr<UnsentLogStoreMetrics> metrics_;
235
236   // A weak pointer to the PrefService object to read and write the preference
237   // from.  Calling code should ensure this object continues to exist for the
238   // lifetime of the UnsentLogStore object.
239   raw_ptr<PrefService> local_state_;
240
241   // The name of the preference to serialize logs to/from.
242   const char* log_data_pref_name_;
243
244   // The name of the preference to store the unsent logs info, it could be
245   // nullptr if the metadata isn't desired.
246   const char* metadata_pref_name_;
247
248   const UnsentLogStoreLimits log_store_limits_;
249
250   // Used to create a signature of log data, in order to verify reported data is
251   // authentic.
252   const std::string signing_key_;
253
254   // Event manager to notify observers of log events.
255   raw_ptr<MetricsLogsEventManager> logs_event_manager_;
256
257   // A list of all of the stored logs, stored with SHA1 hashes to check for
258   // corruption while they are stored in memory.
259   std::vector<std::unique_ptr<LogInfo>> list_;
260
261   // The index and type of the log staged for upload. If nothing has been
262   // staged, the index will be -1.
263   int staged_log_index_;
264
265   // The total number of samples that have been sent from this LogStore.
266   base::HistogramBase::Count total_samples_sent_ = 0;
267 };
268
269 }  // namespace metrics
270
271 #endif  // COMPONENTS_METRICS_UNSENT_LOG_STORE_H_