1 // 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.
5 #ifndef COMPONENTS_METRICS_METRICS_LOGS_EVENT_MANAGER_H_
6 #define COMPONENTS_METRICS_METRICS_LOGS_EVENT_MANAGER_H_
8 #include "base/observer_list.h"
9 #include "base/strings/string_piece.h"
13 // TODO(crbug/1363747): Add unit tests for the various calls to the notify
14 // functions in ReportingService and UnsentLogStore.
15 class MetricsLogsEventManager {
18 // The log was staged.
20 // The log was discarded.
22 // The log was trimmed.
24 // The log is currently being uploaded.
26 // The log was successfully uploaded.
30 class Observer : public base::CheckedObserver {
32 virtual void OnLogCreated(base::StringPiece log_hash,
33 base::StringPiece log_data,
34 base::StringPiece log_timestamp) = 0;
35 virtual void OnLogEvent(MetricsLogsEventManager::LogEvent event,
36 base::StringPiece log_hash,
37 base::StringPiece message) = 0;
41 ~Observer() override = default;
44 MetricsLogsEventManager();
46 MetricsLogsEventManager(const MetricsLogsEventManager&) = delete;
47 MetricsLogsEventManager& operator=(const MetricsLogsEventManager&) = delete;
49 ~MetricsLogsEventManager();
51 void AddObserver(Observer* observer);
52 void RemoveObserver(Observer* observer);
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
59 // NotifyLogEvent(). |log_data| is the compressed serialized log protobuf
60 // (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);
66 // 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 = "");
76 base::ObserverList<Observer> observers_;
79 } // namespace metrics
81 #endif // COMPONENTS_METRICS_METRICS_LOG_EVENT_MANAGER_H_