blob: 37b66370d07fc129938e6388dde02f9060f67d2f [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
28 // |max_ongoing_log_size| bytes.
29 MetricsLogStore(PrefService* local_state, size_t max_ongoing_log_size);
30 ~MetricsLogStore();
31
32 // Registers local state prefs used by this class.
33 static void RegisterPrefs(PrefRegistrySimple* registry);
34
35 // Saves |log_data| as the given type.
36 void StoreLog(const std::string& log_data, MetricsLog::LogType log_type);
37
38 // LogStore:
39 bool has_unsent_logs() const override;
40 bool has_staged_log() const override;
41 const std::string& staged_log() const override;
42 const std::string& staged_log_hash() const override;
Jesse Dohertye1c81482018-11-27 22:12:5743 const std::string& staged_log_signature() const override;
holteade6fdf2017-02-23 02:42:3944 void StageNextLog() override;
45 void DiscardStagedLog() override;
46 void PersistUnsentLogs() const override;
47 void LoadPersistedUnsentLogs() override;
48
49 // Inspection methods for tests.
50 size_t ongoing_log_count() const { return ongoing_log_queue_.size(); }
51 size_t initial_log_count() const { return initial_log_queue_.size(); }
52
53 private:
54 // Tracks whether unsent logs (if any) have been loaded from the serializer.
55 bool unsent_logs_loaded_;
56
57 // Logs stored with the INITIAL_STABILITY_LOG type that haven't been sent yet.
58 // These logs will be staged first when staging new logs.
59 PersistedLogs initial_log_queue_;
60 // Logs stored with the ONGOING_LOG type that haven't been sent yet.
61 PersistedLogs ongoing_log_queue_;
62
63 DISALLOW_COPY_AND_ASSIGN(MetricsLogStore);
64};
65
66} // namespace metrics
67
68#endif // COMPONENTS_METRICS_METRICS_LOG_STORE_H_