log_store.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // 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. #ifndef COMPONENTS_METRICS_LOG_STORE_H_
  5. #define COMPONENTS_METRICS_LOG_STORE_H_
  6. #include <string>
  7. #include "third_party/abseil-cpp/absl/types/optional.h"
  8. namespace metrics {
  9. // Interface for local storage of serialized logs to be reported.
  10. // It allows consumers to check if there are logs to consume, consume them one
  11. // at a time by staging and discarding logs, and persist/load the whole set.
  12. class LogStore {
  13. public:
  14. virtual ~LogStore() = default;
  15. // Returns true if there are any logs waiting to be uploaded.
  16. virtual bool has_unsent_logs() const = 0;
  17. // Returns true if there is a log that needs to be, or is being, uploaded.
  18. virtual bool has_staged_log() const = 0;
  19. // The text of the staged log, as a serialized protobuf.
  20. // Will trigger a DCHECK if there is no staged log.
  21. virtual const std::string& staged_log() const = 0;
  22. // The SHA1 hash of the staged log. This is used to detect log corruption.
  23. // Will trigger a DCHECK if there is no staged log.
  24. virtual const std::string& staged_log_hash() const = 0;
  25. // The HMAC-SHA256 signature of the staged log. This is used to validate that
  26. // a log originated from Chrome, and to detect corruption.
  27. // Will trigger a DCHECK if there is no staged log.
  28. virtual const std::string& staged_log_signature() const = 0;
  29. // User id associated with the staged log. Empty if the log was
  30. // recorded during no particular user session or during guest session.
  31. //
  32. // Will trigger a DCHECK if there is no staged log.
  33. virtual absl::optional<uint64_t> staged_log_user_id() const = 0;
  34. // Populates staged_log() with the next stored log to send.
  35. // The order in which logs are staged is up to the implementor.
  36. // The staged_log must remain the same even if additional logs are added.
  37. // Should only be called if has_unsent_logs() is true.
  38. virtual void StageNextLog() = 0;
  39. // Discards the staged log.
  40. virtual void DiscardStagedLog() = 0;
  41. // Marks the staged log as sent, DiscardStagedLog() shall still be called if
  42. // the staged log needs discarded.
  43. virtual void MarkStagedLogAsSent() = 0;
  44. // Trims saved logs and writes them to persistent storage. When
  45. // |overwrite_in_memory_store| is false, we will still not persist logs that
  46. // should be trimmed away, but they will still be available in memory
  47. // (allowing them to still be eligible for upload this session).
  48. // TODO(crbug/1171830): Revisit call sites and determine what value of
  49. // |overwrite_in_memory_store| they should use.
  50. virtual void TrimAndPersistUnsentLogs(bool overwrite_in_memory_store) = 0;
  51. // Loads unsent logs from persistent storage.
  52. virtual void LoadPersistedUnsentLogs() = 0;
  53. };
  54. } // namespace metrics
  55. #endif // COMPONENTS_METRICS_LOG_STORE_H_