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