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 24 matching lines...) Expand all Loading... | |
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 { | 40 enum StoreType { |
41 NORMAL_STORE, // A standard store operation. | 41 NORMAL_STORE, // A standard store operation. |
42 PROVISIONAL_STORE, // A store operation that can be easily reverted later. | 42 PROVISIONAL_STORE, // A store operation that can be easily reverted later. |
43 }; | 43 }; |
44 | 44 |
45 // Constructs a PersistedLogs that stores data in |local_state| under | 45 // Constructs a PersistedLogs that stores data in |local_state| under the |
46 // the preference |pref_name|. Calling code is responsible for ensuring that | 46 // preference |pref_name| and also reads from legacy pref |old_pref_name|. |
47 // the lifetime of |local_state| is longer than the lifetime of PersistedLogs. | 47 // Calling code is responsible for ensuring that the lifetime of |local_state| |
48 // When saving logs to disk, we will store either the first |min_log_count| | 48 // is longer than the lifetime of PersistedLogs. |
49 // logs, or at least |min_log_bytes| bytes of logs, whichever is more. | 49 // |
50 // If the optional max_log_size parameter is non-zero, all logs larger than | 50 // 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. | |
52 // | |
53 // If the optional |max_log_size| parameter is non-zero, all logs larger than | |
51 // that limit will be dropped before logs are written to disk. | 54 // that limit will be dropped before logs are written to disk. |
52 PersistedLogs(PrefService* local_state, | 55 PersistedLogs(PrefService* local_state, |
53 const char* pref_name, | 56 const char* pref_name, |
57 const char* old_pref_name, | |
54 size_t min_log_count, | 58 size_t min_log_count, |
55 size_t min_log_bytes, | 59 size_t min_log_bytes, |
56 size_t max_log_size); | 60 size_t max_log_size); |
57 ~PersistedLogs(); | 61 ~PersistedLogs(); |
58 | 62 |
59 // Write list to storage. | 63 // Write list to storage. |
60 void SerializeLogs(); | 64 void SerializeLogs(); |
61 | 65 |
62 // Reads the list from the preference. | 66 // Reads the list from the preference. |
63 LogReadStatus DeserializeLogs(); | 67 LogReadStatus DeserializeLogs(); |
64 | 68 |
65 // Adds a log to the list. |input| will be swapped with an empty string. | 69 // Adds a log to the list. |
66 void StoreLog(std::string* input); | 70 void StoreLog(const std::string& log_data); |
67 | 71 |
68 // Stages the most recent log. The staged_log will remain the same even if | 72 // Stages the most recent log. The staged_log will remain the same even if |
69 // additional logs are added. | 73 // additional logs are added. |
70 void StageLog(); | 74 void StageLog(); |
71 | 75 |
72 // Remove the staged log. | 76 // Remove the staged log. |
73 void DiscardStagedLog(); | 77 void DiscardStagedLog(); |
74 | 78 |
75 // Saves the staged log, then clears staged_log(). | 79 // Saves the staged log, then clears staged_log(). |
76 // If |store_type| is PROVISIONAL_STORE, it can be dropped from storage with | 80 // If |store_type| is PROVISIONAL_STORE, it can be dropped from storage with |
77 // a later call to DiscardLastProvisionalStore (if it hasn't already been | 81 // a later call to DiscardLastProvisionalStore (if it hasn't already been |
78 // staged again). | 82 // staged again). |
79 // This is intended to be used when logs are being saved while an upload is in | 83 // This is intended to be used when logs are being saved while an upload is in |
80 // progress, in case the upload later succeeds. | 84 // progress, in case the upload later succeeds. |
81 // This can only be called if has_staged_log() is true. | 85 // This can only be called if has_staged_log() is true. |
82 void StoreStagedLogAsUnsent(StoreType store_type); | 86 void StoreStagedLogAsUnsent(StoreType store_type); |
83 | 87 |
84 // Discards the last log stored with StoreStagedLogAsUnsent with |store_type| | 88 // Discards the last log stored with StoreStagedLogAsUnsent with |store_type| |
85 // set to PROVISIONAL_STORE, as long as it hasn't already been re-staged. If | 89 // set to PROVISIONAL_STORE, as long as it hasn't already been re-staged. If |
86 // the log is no longer present, this is a no-op. | 90 // the log is no longer present, this is a no-op. |
87 void DiscardLastProvisionalStore(); | 91 void DiscardLastProvisionalStore(); |
88 | 92 |
89 // True if a log has been staged. | 93 // True if a log has been staged. |
90 bool has_staged_log() const { return !staged_log_.log.empty(); }; | 94 bool has_staged_log() const { |
95 return !staged_log_.compressed_log_data.empty(); | |
96 } | |
91 | 97 |
92 // Returns the element in the front of the list. | 98 // Returns the element in the front of the list. |
93 const std::string& staged_log() const { | 99 const std::string& staged_log() const { |
94 DCHECK(has_staged_log()); | 100 DCHECK(has_staged_log()); |
95 return staged_log_.log; | 101 return staged_log_.compressed_log_data; |
96 } | 102 } |
97 | 103 |
98 // Returns the element in the front of the list. | 104 // Returns the element in the front of the list. |
99 const std::string& staged_log_hash() const { | 105 const std::string& staged_log_hash() const { |
100 DCHECK(has_staged_log()); | 106 DCHECK(has_staged_log()); |
101 return staged_log_.hash; | 107 return staged_log_.hash; |
102 } | 108 } |
103 | 109 |
104 // The number of elements currently stored. | 110 // The number of elements currently stored. |
105 size_t size() const { return list_.size(); } | 111 size_t size() const { return list_.size(); } |
106 | 112 |
107 // True if there are no stored logs. | 113 // True if there are no stored logs. |
108 bool empty() const { return list_.empty(); } | 114 bool empty() const { return list_.empty(); } |
109 | 115 |
110 private: | 116 private: |
111 // Writes the list to the ListValue. | 117 // Writes the list to the ListValue. |
112 void WriteLogsToPrefList(base::ListValue* list); | 118 void WriteLogsToPrefList(base::ListValue* list); |
113 | 119 |
114 // Reads the list from the ListValue. | 120 // Reads the list from the ListValue. |
115 LogReadStatus ReadLogsFromPrefList(const base::ListValue& list); | 121 LogReadStatus ReadLogsFromPrefList(const base::ListValue& list); |
116 | 122 |
123 // Reads the list from the old pref's ListValue. | |
124 // TODO(asvitkine): Remove the old pref in M39. | |
125 LogReadStatus ReadLogsFromOldPrefList(const base::ListValue& list); | |
126 | |
117 // A weak pointer to the PrefService object to read and write the preference | 127 // A weak pointer to the PrefService object to read and write the preference |
118 // from. Calling code should ensure this object continues to exist for the | 128 // from. Calling code should ensure this object continues to exist for the |
119 // lifetime of the PersistedLogs object. | 129 // lifetime of the PersistedLogs object. |
120 PrefService* local_state_; | 130 PrefService* local_state_; |
121 | 131 |
122 // The name of the preference this object stores logs in. | 132 // The name of the preference to serialize logs to/from. |
123 const char* pref_name_; | 133 const char* pref_name_; |
124 | 134 |
135 // The name of the preference to serialize logs from. | |
136 // TODO(asvitkine): Remove the old pref in M39. | |
137 const char* old_pref_name_; | |
138 | |
125 // We will keep at least this |min_log_count_| logs or |min_log_bytes_| bytes | 139 // We will keep at least this |min_log_count_| logs or |min_log_bytes_| bytes |
126 // of logs, whichever is greater, when writing to disk. These apply after | 140 // of logs, whichever is greater, when writing to disk. These apply after |
127 // skipping logs greater than |max_log_size_|. | 141 // skipping logs greater than |max_log_size_|. |
128 const size_t min_log_count_; | 142 const size_t min_log_count_; |
129 const size_t min_log_bytes_; | 143 const size_t min_log_bytes_; |
130 | 144 |
131 // Logs greater than this size will not be written to disk. | 145 // Logs greater than this size will not be written to disk. |
132 const size_t max_log_size_; | 146 const size_t max_log_size_; |
133 | 147 |
134 struct LogHashPair { | 148 struct LogHashPair { |
135 // Raw log text, typically a serialized protobuf. | 149 // Raw log text, typically a serialized protobuf. |
Ilya Sherman
2014/06/09 23:17:00
nit: Please update this comment.
Alexei Svitkine (slow)
2014/06/10 17:01:15
Done.
| |
136 std::string log; | 150 std::string compressed_log_data; |
151 | |
137 // The SHA1 hash of log, stored to catch errors from memory corruption. | 152 // The SHA1 hash of log, stored to catch errors from memory corruption. |
138 std::string hash; | 153 std::string hash; |
139 // Swap the content of input into log and update the hash. | 154 |
140 void SwapLog(std::string* input); | 155 // Initializes the members based on uncompressed |log_data|. |
156 void Init(const std::string& log_data); | |
157 | |
158 // Clears the struct members. | |
159 void Clear(); | |
160 | |
141 // Swap both log and hash from another LogHashPair. | 161 // Swap both log and hash from another LogHashPair. |
142 void Swap(LogHashPair* input); | 162 void Swap(LogHashPair* input); |
Ilya Sherman
2014/06/09 23:17:00
nit: Member variables should come below methods.
Alexei Svitkine (slow)
2014/06/10 17:01:15
Done.
| |
143 }; | 163 }; |
144 // A list of all of the stored logs, stored with SHA1 hashes to check for | 164 // A list of all of the stored logs, stored with SHA1 hashes to check for |
145 // corruption while they are stored in memory. | 165 // corruption while they are stored in memory. |
146 std::vector<LogHashPair> list_; | 166 std::vector<LogHashPair> list_; |
147 | 167 |
148 // The log staged for upload. | 168 // The log staged for upload. |
149 LogHashPair staged_log_; | 169 LogHashPair staged_log_; |
150 | 170 |
151 // The index and type of the last provisional store. If nothing has been | 171 // The index and type of the last provisional store. If nothing has been |
152 // provisionally stored, or the last provisional store has already been | 172 // provisionally stored, or the last provisional store has already been |
153 // re-staged, the index will be -1; | 173 // re-staged, the index will be -1; |
154 // This is necessary because during an upload there are two logs (staged | 174 // This is necessary because during an upload there are two logs (staged |
155 // and current) and a client might store them in either order, so it's | 175 // and current) and a client might store them in either order, so it's |
156 // not necessarily the case that the provisional store is the last store. | 176 // not necessarily the case that the provisional store is the last store. |
157 int last_provisional_store_index_; | 177 int last_provisional_store_index_; |
158 | 178 |
159 DISALLOW_COPY_AND_ASSIGN(PersistedLogs); | 179 DISALLOW_COPY_AND_ASSIGN(PersistedLogs); |
160 }; | 180 }; |
161 | 181 |
162 } // namespace metrics | 182 } // namespace metrics |
163 | 183 |
164 #endif // COMPONENTS_METRICS_PERSISTED_LOGS_H_ | 184 #endif // COMPONENTS_METRICS_PERSISTED_LOGS_H_ |
OLD | NEW |