blob: 8d2c03dab52904712e9a807c1a6b2ccd5ed050bd [file] [log] [blame]
holteade6fdf2017-02-23 02:42:391// Copyright 2017 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_METRICS_LOG_STORE_H_
6#define COMPONENTS_METRICS_METRICS_LOG_STORE_H_
7
8#include <string>
9
10#include "base/macros.h"
11#include "components/metrics/log_store.h"
12#include "components/metrics/metrics_log.h"
13#include "components/metrics/persisted_logs.h"
14
15class PrefService;
16class PrefRegistrySimple;
17
18namespace metrics {
19
20// A LogStore implementation for storing UMA logs.
21// This implementation keeps track of two types of logs, initial and ongoing,
22// each stored in PersistedLogs. It prioritizes staging initial logs over
23// ongoing logs.
24class MetricsLogStore : public LogStore {
25 public:
26 // Constructs a MetricsLogStore that persists data into |local_state|.
27 // If max_log_size is non-zero, it will not persist ongoing logs larger than
Jesse Dohertybf020162018-12-11 15:40:0128 // |max_ongoing_log_size| bytes. |signing_key| is used to generate a signature
29 // of a log, which will be uploaded to validate data integrity.
30 MetricsLogStore(PrefService* local_state,
31 size_t max_ongoing_log_size,
32 const std::string& signing_key);
holteade6fdf2017-02-23 02:42:3933 ~MetricsLogStore();
34
35 // Registers local state prefs used by this class.
36 static void RegisterPrefs(PrefRegistrySimple* registry);
37
38 // Saves |log_data| as the given type.
39 void StoreLog(const std::string& log_data, MetricsLog::LogType log_type);
40
41 // LogStore:
42 bool has_unsent_logs() const override;
43 bool has_staged_log() const override;
44 const std::string& staged_log() const override;
45 const std::string& staged_log_hash() const override;
Jesse Dohertye1c81482018-11-27 22:12:5746 const std::string& staged_log_signature() const override;
holteade6fdf2017-02-23 02:42:3947 void StageNextLog() override;
48 void DiscardStagedLog() override;
49 void PersistUnsentLogs() const override;
50 void LoadPersistedUnsentLogs() override;
51
52 // Inspection methods for tests.
53 size_t ongoing_log_count() const { return ongoing_log_queue_.size(); }
54 size_t initial_log_count() const { return initial_log_queue_.size(); }
55
56 private:
57 // Tracks whether unsent logs (if any) have been loaded from the serializer.
58 bool unsent_logs_loaded_;
59
60 // Logs stored with the INITIAL_STABILITY_LOG type that haven't been sent yet.
61 // These logs will be staged first when staging new logs.
62 PersistedLogs initial_log_queue_;
63 // Logs stored with the ONGOING_LOG type that haven't been sent yet.
64 PersistedLogs ongoing_log_queue_;
65
66 DISALLOW_COPY_AND_ASSIGN(MetricsLogStore);
67};
68
69} // namespace metrics
70
71#endif // COMPONENTS_METRICS_METRICS_LOG_STORE_H_