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 <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 19 matching lines...) Expand all Loading... |
30 LOG_STRING_CORRUPTION, // Failed to recover log string using GetAsString(). | 30 LOG_STRING_CORRUPTION, // Failed to recover log string using GetAsString(). |
31 CHECKSUM_CORRUPTION, // Failed to verify checksum. | 31 CHECKSUM_CORRUPTION, // Failed to verify checksum. |
32 CHECKSUM_STRING_CORRUPTION, // Failed to recover checksum string using | 32 CHECKSUM_STRING_CORRUPTION, // Failed to recover checksum string using |
33 // GetAsString(). | 33 // GetAsString(). |
34 DECODE_FAIL, // Failed to decode log. | 34 DECODE_FAIL, // Failed to decode log. |
35 DEPRECATED_XML_PROTO_MISMATCH, // The XML and protobuf logs have | 35 DEPRECATED_XML_PROTO_MISMATCH, // The XML and protobuf logs have |
36 // inconsistent data. | 36 // inconsistent data. |
37 END_RECALL_STATUS // Number of bins to use to create the histogram. | 37 END_RECALL_STATUS // Number of bins to use to create the histogram. |
38 }; | 38 }; |
39 | 39 |
40 enum StoreType { | |
41 NORMAL_STORE, // A standard store operation. | |
42 PROVISIONAL_STORE, // A store operation that can be easily reverted later. | |
43 }; | |
44 | |
45 // Constructs a PersistedLogs that stores data in |local_state| under the | 40 // Constructs a PersistedLogs that stores data in |local_state| under the |
46 // preference |pref_name| and also reads from legacy pref |old_pref_name|. | 41 // preference |pref_name| and also reads from legacy pref |old_pref_name|. |
47 // Calling code is responsible for ensuring that the lifetime of |local_state| | 42 // Calling code is responsible for ensuring that the lifetime of |local_state| |
48 // is longer than the lifetime of PersistedLogs. | 43 // is longer than the lifetime of PersistedLogs. |
49 // | 44 // |
50 // When saving logs to disk, stores either the first |min_log_count| logs, or | 45 // 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. | 46 // at least |min_log_bytes| bytes of logs, whichever is greater. |
52 // | 47 // |
53 // If the optional |max_log_size| parameter is non-zero, all logs larger than | 48 // If the optional |max_log_size| parameter is non-zero, all logs larger than |
54 // that limit will be skipped when writing to disk. | 49 // that limit will be skipped when writing to disk. |
(...skipping 14 matching lines...) Expand all Loading... |
69 // Adds a log to the list. | 64 // Adds a log to the list. |
70 void StoreLog(const std::string& log_data); | 65 void StoreLog(const std::string& log_data); |
71 | 66 |
72 // Stages the most recent log. The staged_log will remain the same even if | 67 // Stages the most recent log. The staged_log will remain the same even if |
73 // additional logs are added. | 68 // additional logs are added. |
74 void StageLog(); | 69 void StageLog(); |
75 | 70 |
76 // Remove the staged log. | 71 // Remove the staged log. |
77 void DiscardStagedLog(); | 72 void DiscardStagedLog(); |
78 | 73 |
79 // Saves the staged log, then clears staged_log(). | |
80 // If |store_type| is PROVISIONAL_STORE, it can be dropped from storage with | |
81 // a later call to DiscardLastProvisionalStore (if it hasn't already been | |
82 // staged again). | |
83 // This is intended to be used when logs are being saved while an upload is in | |
84 // progress, in case the upload later succeeds. | |
85 // This can only be called if has_staged_log() is true. | |
86 void StoreStagedLogAsUnsent(StoreType store_type); | |
87 | |
88 // Discards the last log stored with StoreStagedLogAsUnsent with |store_type| | |
89 // set to PROVISIONAL_STORE, as long as it hasn't already been re-staged. If | |
90 // the log is no longer present, this is a no-op. | |
91 void DiscardLastProvisionalStore(); | |
92 | |
93 // True if a log has been staged. | 74 // True if a log has been staged. |
94 bool has_staged_log() const { | 75 bool has_staged_log() const { return staged_log_index_ != -1; }; |
95 return !staged_log_.compressed_log_data.empty(); | |
96 } | |
97 | 76 |
98 // Returns the element in the front of the list. | 77 // Returns the element in the front of the list. |
99 const std::string& staged_log() const { | 78 const std::string& staged_log() const { |
100 DCHECK(has_staged_log()); | 79 DCHECK(has_staged_log()); |
101 return staged_log_.compressed_log_data; | 80 return list_[staged_log_index_].compressed_log_data; |
102 } | 81 } |
103 | 82 |
104 // Returns the element in the front of the list. | 83 // Returns the element in the front of the list. |
105 const std::string& staged_log_hash() const { | 84 const std::string& staged_log_hash() const { |
106 DCHECK(has_staged_log()); | 85 DCHECK(has_staged_log()); |
107 return staged_log_.hash; | 86 return list_[staged_log_index_].hash; |
108 } | 87 } |
109 | 88 |
110 // The number of elements currently stored. | 89 // The number of elements currently stored. |
111 size_t size() const { return list_.size(); } | 90 size_t size() const { return list_.size(); } |
112 | 91 |
113 // True if there are no stored logs. | 92 // True if there are no stored logs. |
114 bool empty() const { return list_.empty(); } | 93 bool empty() const { return list_.empty(); } |
115 | 94 |
116 private: | 95 private: |
117 // Writes the list to the ListValue. | 96 // Writes the list to the ListValue. |
(...skipping 24 matching lines...) Expand all Loading... |
142 const size_t min_log_count_; | 121 const size_t min_log_count_; |
143 const size_t min_log_bytes_; | 122 const size_t min_log_bytes_; |
144 | 123 |
145 // Logs greater than this size will not be written to disk. | 124 // Logs greater than this size will not be written to disk. |
146 const size_t max_log_size_; | 125 const size_t max_log_size_; |
147 | 126 |
148 struct LogHashPair { | 127 struct LogHashPair { |
149 // Initializes the members based on uncompressed |log_data|. | 128 // Initializes the members based on uncompressed |log_data|. |
150 void Init(const std::string& log_data); | 129 void Init(const std::string& log_data); |
151 | 130 |
152 // Clears the struct members. | |
153 void Clear(); | |
154 | |
155 // Swap both log and hash from another LogHashPair. | |
156 void Swap(LogHashPair* input); | |
157 | |
158 // Compressed log data - a serialized protobuf that's been gzipped. | 131 // Compressed log data - a serialized protobuf that's been gzipped. |
159 std::string compressed_log_data; | 132 std::string compressed_log_data; |
160 | 133 |
161 // The SHA1 hash of log, stored to catch errors from memory corruption. | 134 // The SHA1 hash of log, stored to catch errors from memory corruption. |
162 std::string hash; | 135 std::string hash; |
163 }; | 136 }; |
164 // A list of all of the stored logs, stored with SHA1 hashes to check for | 137 // A list of all of the stored logs, stored with SHA1 hashes to check for |
165 // corruption while they are stored in memory. | 138 // corruption while they are stored in memory. |
166 std::vector<LogHashPair> list_; | 139 std::vector<LogHashPair> list_; |
167 | 140 |
168 // The log staged for upload. | 141 // The index and type of the log staged for upload. If nothing has been |
169 LogHashPair staged_log_; | 142 // staged, the index will be -1. |
170 | 143 int staged_log_index_; |
171 // The index and type of the last provisional store. If nothing has been | |
172 // provisionally stored, or the last provisional store has already been | |
173 // re-staged, the index will be -1; | |
174 // This is necessary because during an upload there are two logs (staged | |
175 // and current) and a client might store them in either order, so it's | |
176 // not necessarily the case that the provisional store is the last store. | |
177 int last_provisional_store_index_; | |
178 | 144 |
179 DISALLOW_COPY_AND_ASSIGN(PersistedLogs); | 145 DISALLOW_COPY_AND_ASSIGN(PersistedLogs); |
180 }; | 146 }; |
181 | 147 |
182 } // namespace metrics | 148 } // namespace metrics |
183 | 149 |
184 #endif // COMPONENTS_METRICS_PERSISTED_LOGS_H_ | 150 #endif // COMPONENTS_METRICS_PERSISTED_LOGS_H_ |
OLD | NEW |