blob: 424c963f03bdad25687d29a2278c17f8acddf853 [file] [log] [blame]
Luc Nguyen8901ecf2022-09-23 23:47:271// Copyright 2022 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_METRICS_LOGS_EVENT_MANAGER_H_
6#define COMPONENTS_METRICS_METRICS_LOGS_EVENT_MANAGER_H_
7
8#include "base/observer_list.h"
9#include "base/strings/string_piece.h"
10
11namespace metrics {
Luc Nguyen3e8432e2022-10-06 21:23:4412
13// TODO(crbug/1363747): Add unit tests for the various calls to the notify
14// functions in ReportingService and UnsentLogStore.
Luc Nguyen8901ecf2022-09-23 23:47:2715class MetricsLogsEventManager {
16 public:
Luc Nguyen3e8432e2022-10-06 21:23:4417 enum class LogEvent {
Luc Nguyen8901ecf2022-09-23 23:47:2718 // The log was staged.
19 kLogStaged,
20 // The log was discarded.
21 kLogDiscarded,
22 // The log was trimmed.
23 kLogTrimmed,
24 // The log is currently being uploaded.
25 kLogUploading,
26 // The log was successfully uploaded.
27 kLogUploaded,
28 };
29
30 class Observer : public base::CheckedObserver {
31 public:
32 virtual void OnLogCreated(base::StringPiece log_hash,
33 base::StringPiece log_data,
34 base::StringPiece log_timestamp) = 0;
Luc Nguyen3e8432e2022-10-06 21:23:4435 virtual void OnLogEvent(MetricsLogsEventManager::LogEvent event,
36 base::StringPiece log_hash,
37 base::StringPiece message) = 0;
Luc Nguyen8901ecf2022-09-23 23:47:2738
39 protected:
40 Observer() = default;
41 ~Observer() override = default;
42 };
43
44 MetricsLogsEventManager();
45
46 MetricsLogsEventManager(const MetricsLogsEventManager&) = delete;
47 MetricsLogsEventManager& operator=(const MetricsLogsEventManager&) = delete;
48
49 ~MetricsLogsEventManager();
50
51 void AddObserver(Observer* observer);
52 void RemoveObserver(Observer* observer);
53
54 // Notifies observers that a log was newly created and is now known by the
55 // metrics service. This may occur when closing a log, or when loading a log
56 // from persistent storage. |log_hash| is the SHA1 hash of the log data, used
57 // to uniquely identify the log. This hash may be re-used to notify that an
58 // event occurred on the log (e.g., the log was trimmed, uploaded, etc.). See
Luc Nguyen3e8432e2022-10-06 21:23:4459 // NotifyLogEvent(). |log_data| is the compressed serialized log protobuf
Luc Nguyen8901ecf2022-09-23 23:47:2760 // (see UnsentLogStore::LogInfo for more details on the compression).
61 // |log_timestamp| is the time at which the log was closed.
62 void NotifyLogCreated(base::StringPiece log_hash,
63 base::StringPiece log_data,
64 base::StringPiece log_timestamp);
65
Luc Nguyen3e8432e2022-10-06 21:23:4466 // Notifies observers that an event |event| occurred on the log associated
67 // with |log_hash|. Optionally, a |message| can be associated with the event.
68 // In particular, for |kLogDiscarded|, |message| is the reason the log was
69 // discarded (e.g., log is ill-formed). For |kLogTrimmed|, |message| is the
70 // reason why the log was trimmed (e.g., log is too large).
71 void NotifyLogEvent(LogEvent event,
72 base::StringPiece log_hash,
73 base::StringPiece message = "");
Luc Nguyen8901ecf2022-09-23 23:47:2774
75 private:
76 base::ObserverList<Observer> observers_;
77};
78
79} // namespace metrics
80
81#endif // COMPONENTS_METRICS_METRICS_LOG_EVENT_MANAGER_H_