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