Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef COMPONENTS_METRICS_PERSISTED_LOGS_H_ | 5 #ifndef COMPONENTS_METRICS_PERSISTED_LOGS_H_ |
| 6 #define COMPONENTS_METRICS_PERSISTED_LOGS_H_ | 6 #define COMPONENTS_METRICS_PERSISTED_LOGS_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <memory> | 10 #include <memory> |
| 11 #include <string> | 11 #include <string> |
| 12 #include <vector> | 12 #include <vector> |
| 13 | 13 |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/macros.h" | 15 #include "base/macros.h" |
| 16 #include "base/values.h" | 16 #include "base/values.h" |
| 17 #include "components/metrics/log_store.h" | |
| 17 | 18 |
| 18 class PrefService; | 19 class PrefService; |
| 19 | 20 |
| 20 namespace metrics { | 21 namespace metrics { |
| 21 | 22 |
| 22 class PersistedLogsMetrics; | 23 class PersistedLogsMetrics; |
| 23 | 24 |
| 24 // Maintains a list of unsent logs that are written and restored from disk. | 25 // Maintains a list of unsent logs that are written and restored from disk. |
| 25 class PersistedLogs { | 26 class PersistedLogs : public LogStore { |
| 26 public: | 27 public: |
| 27 // Used to produce a histogram that keeps track of the status of recalling | |
| 28 // persisted per logs. | |
| 29 enum LogReadStatus { | |
| 30 RECALL_SUCCESS, // We were able to correctly recall a persisted log. | |
| 31 LIST_EMPTY, // Attempting to recall from an empty list. | |
| 32 LIST_SIZE_MISSING, // Failed to recover list size using GetAsInteger(). | |
| 33 LIST_SIZE_TOO_SMALL, // Too few elements in the list (less than 3). | |
| 34 LIST_SIZE_CORRUPTION, // List size is not as expected. | |
| 35 LOG_STRING_CORRUPTION, // Failed to recover log string using GetAsString(). | |
| 36 CHECKSUM_CORRUPTION, // Failed to verify checksum. | |
| 37 CHECKSUM_STRING_CORRUPTION, // Failed to recover checksum string using | |
| 38 // GetAsString(). | |
| 39 DECODE_FAIL, // Failed to decode log. | |
| 40 DEPRECATED_XML_PROTO_MISMATCH, // The XML and protobuf logs have | |
| 41 // inconsistent data. | |
| 42 END_RECALL_STATUS // Number of bins to use to create the histogram. | |
| 43 }; | |
| 44 | |
| 45 // Constructs a PersistedLogs that stores data in |local_state| under the | 28 // Constructs a PersistedLogs that stores data in |local_state| under the |
| 46 // preference |pref_name|. | 29 // preference |pref_name|. |
| 47 // Calling code is responsible for ensuring that the lifetime of |local_state| | 30 // Calling code is responsible for ensuring that the lifetime of |local_state| |
| 48 // is longer than the lifetime of PersistedLogs. | 31 // is longer than the lifetime of PersistedLogs. |
| 49 // | 32 // |
| 50 // When saving logs to disk, stores either the first |min_log_count| logs, or | 33 // When saving logs to disk, stores either the first |min_log_count| logs, or |
| 51 // at least |min_log_bytes| bytes of logs, whichever is greater. | 34 // at least |min_log_bytes| bytes of logs, whichever is greater. |
| 52 // | 35 // |
| 53 // If the optional |max_log_size| parameter is non-zero, all logs larger than | 36 // If the optional |max_log_size| parameter is non-zero, all logs larger than |
| 54 // that limit will be skipped when writing to disk. | 37 // that limit will be skipped when writing to disk. |
| 55 PersistedLogs(std::unique_ptr<PersistedLogsMetrics> metrics, | 38 PersistedLogs(std::unique_ptr<PersistedLogsMetrics> metrics, |
| 56 PrefService* local_state, | 39 PrefService* local_state, |
| 57 const char* pref_name, | 40 const char* pref_name, |
| 58 size_t min_log_count, | 41 size_t min_log_count, |
| 59 size_t min_log_bytes, | 42 size_t min_log_bytes, |
| 60 size_t max_log_size); | 43 size_t max_log_size); |
| 61 ~PersistedLogs(); | 44 ~PersistedLogs(); |
| 62 | 45 |
| 63 // Write list to storage. | 46 // LogStore: |
|
bcwhite
2017/02/22 16:25:09
metrics::LogStore:
Steven Holte
2017/02/22 20:35:50
Done.
| |
| 64 void SerializeLogs() const; | 47 bool has_unsent_logs() const override; |
| 65 | 48 bool has_staged_log() const override; |
| 66 // Reads the list from the preference. | 49 const std::string& staged_log() const override; |
| 67 LogReadStatus DeserializeLogs(); | 50 const std::string& staged_log_hash() const override; |
| 51 void StageNextLog() override; | |
| 52 void DiscardStagedLog() override; | |
| 53 void PersistUnsentLogs() const override; | |
| 54 void LoadPersistedUnsentLogs() override; | |
| 68 | 55 |
| 69 // Adds a log to the list. | 56 // Adds a log to the list. |
| 70 void StoreLog(const std::string& log_data); | 57 void StoreLog(const std::string& log_data); |
| 71 | 58 |
| 72 // Stages the most recent log. The staged_log will remain the same even if | |
| 73 // additional logs are added. | |
| 74 void StageLog(); | |
| 75 | |
| 76 // Remove the staged log. | |
| 77 void DiscardStagedLog(); | |
| 78 | |
| 79 // Delete all logs, in memory and on disk. | 59 // Delete all logs, in memory and on disk. |
| 80 void Purge(); | 60 void Purge(); |
| 81 | 61 |
| 82 // True if a log has been staged. | |
| 83 bool has_staged_log() const { return staged_log_index_ != -1; } | |
| 84 | |
| 85 // Returns the element in the front of the list. | |
| 86 const std::string& staged_log() const { | |
| 87 DCHECK(has_staged_log()); | |
| 88 return list_[staged_log_index_].compressed_log_data; | |
| 89 } | |
| 90 | |
| 91 // Returns the element in the front of the list. | |
| 92 const std::string& staged_log_hash() const { | |
| 93 DCHECK(has_staged_log()); | |
| 94 return list_[staged_log_index_].hash; | |
| 95 } | |
| 96 | |
| 97 // Returns the timestamp of the element in the front of the list. | 62 // Returns the timestamp of the element in the front of the list. |
| 98 const std::string& staged_log_timestamp() const { | 63 const std::string& staged_log_timestamp() const; |
| 99 DCHECK(has_staged_log()); | |
| 100 return list_[staged_log_index_].timestamp; | |
| 101 } | |
| 102 | 64 |
| 103 // The number of elements currently stored. | 65 // The number of elements currently stored. |
| 104 size_t size() const { return list_.size(); } | 66 size_t size() const { return list_.size(); } |
| 105 | 67 |
| 106 // True if there are no stored logs. | |
| 107 bool empty() const { return list_.empty(); } | |
| 108 | |
| 109 private: | 68 private: |
| 110 // Writes the list to the ListValue. | 69 // Writes the list to the ListValue. |
| 111 void WriteLogsToPrefList(base::ListValue* list) const; | 70 void WriteLogsToPrefList(base::ListValue* list) const; |
| 112 | 71 |
| 113 // Reads the list from the ListValue. | 72 // Reads the list from the ListValue. |
| 114 LogReadStatus ReadLogsFromPrefList(const base::ListValue& list); | 73 void ReadLogsFromPrefList(const base::ListValue& list); |
| 115 | 74 |
| 116 // An object for recording UMA metrics. | 75 // An object for recording UMA metrics. |
| 117 std::unique_ptr<PersistedLogsMetrics> metrics_; | 76 std::unique_ptr<PersistedLogsMetrics> metrics_; |
| 118 | 77 |
| 119 // A weak pointer to the PrefService object to read and write the preference | 78 // A weak pointer to the PrefService object to read and write the preference |
| 120 // from. Calling code should ensure this object continues to exist for the | 79 // from. Calling code should ensure this object continues to exist for the |
| 121 // lifetime of the PersistedLogs object. | 80 // lifetime of the PersistedLogs object. |
| 122 PrefService* local_state_; | 81 PrefService* local_state_; |
| 123 | 82 |
| 124 // The name of the preference to serialize logs to/from. | 83 // The name of the preference to serialize logs to/from. |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 157 // The index and type of the log staged for upload. If nothing has been | 116 // The index and type of the log staged for upload. If nothing has been |
| 158 // staged, the index will be -1. | 117 // staged, the index will be -1. |
| 159 int staged_log_index_; | 118 int staged_log_index_; |
| 160 | 119 |
| 161 DISALLOW_COPY_AND_ASSIGN(PersistedLogs); | 120 DISALLOW_COPY_AND_ASSIGN(PersistedLogs); |
| 162 }; | 121 }; |
| 163 | 122 |
| 164 } // namespace metrics | 123 } // namespace metrics |
| 165 | 124 |
| 166 #endif // COMPONENTS_METRICS_PERSISTED_LOGS_H_ | 125 #endif // COMPONENTS_METRICS_PERSISTED_LOGS_H_ |
| OLD | NEW |